From 9d363b38c73aeb14157e69bd1266ddfbcf7173af Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Tue, 4 Nov 2025 12:07:58 -0700 Subject: [PATCH] refactor: Removed the use of the tools.txt file and added tool visibility declarations to the global configuration file --- assets/functions/tools.txt | 24 -------------- src/config/mod.rs | 9 ++---- src/function/mod.rs | 66 ++++++++++++-------------------------- 3 files changed, 23 insertions(+), 76 deletions(-) delete mode 100644 assets/functions/tools.txt diff --git a/assets/functions/tools.txt b/assets/functions/tools.txt deleted file mode 100644 index f09e7ad..0000000 --- a/assets/functions/tools.txt +++ /dev/null @@ -1,24 +0,0 @@ -# demo_py.py -# demo_sh.sh -execute_command.sh -# execute_py_code.py -# execute_sql_code.sh -# fetch_url_via_curl.sh -# fetch_url_via_jina.sh -fs_cat.sh -fs_ls.sh -# fs_mkdir.sh -# fs_patch.sh -# fs_write.sh -get_current_time.sh -# get_current_weather.py -get_current_weather.sh -query_jira_issues.sh -# search_arxiv.sh -# search_wikipedia.sh -# search_wolframalpha.sh -# send_mail.sh -# send_twilio.sh -# web_search_loki.sh -# web_search_perplexity.sh -# web_search_tavily.sh diff --git a/src/config/mod.rs b/src/config/mod.rs index 4bb16bd..4493d2e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -73,7 +73,6 @@ const FUNCTIONS_DIR_NAME: &str = "functions"; const FUNCTIONS_BIN_DIR_NAME: &str = "bin"; const AGENTS_DIR_NAME: &str = "agents"; const GLOBAL_TOOLS_DIR_NAME: &str = "tools"; -const GLOBAL_TOOLS_FILE_NAME: &str = "tools.txt"; const GLOBAL_TOOLS_UTILS_DIR_NAME: &str = "utils"; const BASH_PROMPT_UTILS_FILE_NAME: &str = "prompt-utils.sh"; const MCP_FILE_NAME: &str = "mcp.json"; @@ -132,6 +131,7 @@ pub struct Config { pub function_calling: bool, pub mapping_tools: IndexMap, pub use_tools: Option, + pub visible_tools: Option>, pub mcp_servers: bool, pub mapping_mcp_servers: IndexMap, @@ -217,6 +217,7 @@ impl Default for Config { function_calling: true, mapping_tools: Default::default(), use_tools: None, + visible_tools: None, mcp_servers: true, mapping_mcp_servers: Default::default(), @@ -486,10 +487,6 @@ impl Config { Self::functions_dir().join(MCP_FILE_NAME) } - pub fn global_tools_file() -> PathBuf { - Self::functions_dir().join(GLOBAL_TOOLS_FILE_NAME) - } - pub fn global_tools_dir() -> PathBuf { Self::functions_dir().join(GLOBAL_TOOLS_DIR_NAME) } @@ -2828,7 +2825,7 @@ impl Config { } fn load_functions(&mut self) -> Result<()> { - self.functions = Functions::init()?; + self.functions = Functions::init(self.visible_tools.as_ref().unwrap_or(&Vec::new()))?; Ok(()) } diff --git a/src/function/mod.rs b/src/function/mod.rs index e9fd261..ce5cf88 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -167,25 +167,19 @@ impl Functions { Ok(()) } - pub fn init() -> Result { + pub fn init(visible_tools: &[String]) -> Result { Self::install_global_tools()?; Self::clear_global_functions_bin_dir()?; - info!( - "Initializing global functions from {}", - Config::global_tools_file().display() - ); let declarations = Self { - declarations: Self::build_global_tool_declarations_from_path( - &Config::global_tools_file(), - )?, + declarations: Self::build_global_tool_declarations(visible_tools)?, }; info!( "Building global function binaries in {}", Config::functions_bin_dir().display() ); - Self::build_global_function_binaries_from_path(Config::global_tools_file())?; + Self::build_global_function_binaries(visible_tools, None)?; Ok(declarations) } @@ -195,15 +189,14 @@ impl Functions { Self::clear_agent_bin_dir(name)?; let global_tools_declarations = if !global_tools.is_empty() { - let enabled_tools = global_tools.join("\n"); - info!("Loading global tools for agent: {name}: {enabled_tools}"); - let tools_declarations = Self::build_global_tool_declarations(&enabled_tools)?; + info!("Loading global tools for agent: {name}: {global_tools:?}"); + let tools_declarations = Self::build_global_tool_declarations(global_tools)?; info!( "Building global function binaries required by agent: {name} in {}", Config::functions_bin_dir().display() ); - Self::build_global_function_binaries(&enabled_tools, Some(name))?; + Self::build_global_function_binaries(global_tools, Some(name))?; tools_declarations } else { debug!("No global tools found for agent: {}", name); @@ -311,31 +304,20 @@ impl Functions { } } - fn build_global_tool_declarations(enabled_tools: &str) -> Result> { + fn build_global_tool_declarations( + enabled_tools: &[String], + ) -> Result> { let global_tools_directory = Config::global_tools_dir(); let mut function_declarations = Vec::new(); - for line in enabled_tools.lines() { - if line.starts_with('#') { - continue; - } - - let declaration = Self::generate_declarations(&global_tools_directory.join(line))?; + for tool in enabled_tools { + let declaration = Self::generate_declarations(&global_tools_directory.join(tool))?; function_declarations.extend(declaration); } Ok(function_declarations) } - fn build_global_tool_declarations_from_path( - tools_txt_path: &PathBuf, - ) -> Result> { - let enabled_tools = fs::read_to_string(tools_txt_path) - .with_context(|| format!("failed to load functions at {}", tools_txt_path.display()))?; - - Self::build_global_tool_declarations(&enabled_tools) - } - fn generate_declarations(tools_file_path: &Path) -> Result> { info!( "Loading tool definitions from {}", @@ -384,26 +366,25 @@ impl Functions { } } - fn build_global_function_binaries(enabled_tools: &str, agent_name: Option<&str>) -> Result<()> { - for line in enabled_tools.lines() { - if line.starts_with('#') { - continue; - } - + fn build_global_function_binaries( + enabled_tools: &[String], + agent_name: Option<&str>, + ) -> Result<()> { + for tool in enabled_tools { let language = Language::from( - &Path::new(line) + &Path::new(&tool) .extension() .and_then(OsStr::to_str) .map(|s| s.to_lowercase()) .ok_or_else(|| { - anyhow::format_err!("Unable to extract file extension from path: {line:?}") + anyhow::format_err!("Unable to extract file extension from path: {tool:?}") })?, ); - let binary_name = Path::new(line) + let binary_name = Path::new(&tool) .file_stem() .and_then(OsStr::to_str) .ok_or_else(|| { - anyhow::format_err!("Unable to extract file name from path: {line:?}") + anyhow::format_err!("Unable to extract file name from path: {tool:?}") })?; if language == Language::Unsupported { @@ -416,13 +397,6 @@ impl Functions { Ok(()) } - fn build_global_function_binaries_from_path(tools_txt_path: PathBuf) -> Result<()> { - let enabled_tools = fs::read_to_string(&tools_txt_path) - .with_context(|| format!("failed to load functions at {}", tools_txt_path.display()))?; - - Self::build_global_function_binaries(&enabled_tools, None) - } - fn clear_agent_bin_dir(name: &str) -> Result<()> { let agent_bin_directory = Config::agent_bin_dir(name); if !agent_bin_directory.exists() {