1use anyhow::Result;
2use serde_json::Value;
3
4use crate::migrations::migrate_settings;
5
6const AGENT_KEY: &str = "agent";
7const PROFILES_KEY: &str = "profiles";
8const TOOL_PERMISSIONS_KEY: &str = "tool_permissions";
9const TOOLS_KEY: &str = "tools";
10const OLD_TOOL_NAME: &str = "web_search";
11const NEW_TOOL_NAME: &str = "search_web";
12
13pub fn rename_web_search_to_search_web(value: &mut Value) -> Result<()> {
14 migrate_settings(value, &mut migrate_agent_value)
15}
16
17fn migrate_agent_value(object: &mut serde_json::Map<String, Value>) -> Result<()> {
18 let Some(agent) = object.get_mut(AGENT_KEY).and_then(|v| v.as_object_mut()) else {
19 return Ok(());
20 };
21
22 if let Some(tools) = agent
23 .get_mut(TOOL_PERMISSIONS_KEY)
24 .and_then(|v| v.as_object_mut())
25 .and_then(|tp| tp.get_mut(TOOLS_KEY))
26 .and_then(|v| v.as_object_mut())
27 {
28 rename_key(tools);
29 }
30
31 if let Some(profiles) = agent.get_mut(PROFILES_KEY).and_then(|v| v.as_object_mut()) {
32 for (_profile_name, profile) in profiles.iter_mut() {
33 if let Some(tools) = profile
34 .as_object_mut()
35 .and_then(|p| p.get_mut(TOOLS_KEY))
36 .and_then(|v| v.as_object_mut())
37 {
38 rename_key(tools);
39 }
40 }
41 }
42
43 Ok(())
44}
45
46fn rename_key(tools: &mut serde_json::Map<String, Value>) {
47 if let Some(value) = tools.remove(OLD_TOOL_NAME) {
48 tools.insert(NEW_TOOL_NAME.to_string(), value);
49 }
50}