refactor: Removed the use of the tools.txt file and added tool visibility declarations to the global configuration file

This commit is contained in:
2025-11-04 12:07:58 -07:00
parent 2f3586cbbf
commit 9d363b38c7
3 changed files with 23 additions and 76 deletions
-24
View File
@@ -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
+3 -6
View File
@@ -73,7 +73,6 @@ const FUNCTIONS_DIR_NAME: &str = "functions";
const FUNCTIONS_BIN_DIR_NAME: &str = "bin"; const FUNCTIONS_BIN_DIR_NAME: &str = "bin";
const AGENTS_DIR_NAME: &str = "agents"; const AGENTS_DIR_NAME: &str = "agents";
const GLOBAL_TOOLS_DIR_NAME: &str = "tools"; const GLOBAL_TOOLS_DIR_NAME: &str = "tools";
const GLOBAL_TOOLS_FILE_NAME: &str = "tools.txt";
const GLOBAL_TOOLS_UTILS_DIR_NAME: &str = "utils"; const GLOBAL_TOOLS_UTILS_DIR_NAME: &str = "utils";
const BASH_PROMPT_UTILS_FILE_NAME: &str = "prompt-utils.sh"; const BASH_PROMPT_UTILS_FILE_NAME: &str = "prompt-utils.sh";
const MCP_FILE_NAME: &str = "mcp.json"; const MCP_FILE_NAME: &str = "mcp.json";
@@ -132,6 +131,7 @@ pub struct Config {
pub function_calling: bool, pub function_calling: bool,
pub mapping_tools: IndexMap<String, String>, pub mapping_tools: IndexMap<String, String>,
pub use_tools: Option<String>, pub use_tools: Option<String>,
pub visible_tools: Option<Vec<String>>,
pub mcp_servers: bool, pub mcp_servers: bool,
pub mapping_mcp_servers: IndexMap<String, String>, pub mapping_mcp_servers: IndexMap<String, String>,
@@ -217,6 +217,7 @@ impl Default for Config {
function_calling: true, function_calling: true,
mapping_tools: Default::default(), mapping_tools: Default::default(),
use_tools: None, use_tools: None,
visible_tools: None,
mcp_servers: true, mcp_servers: true,
mapping_mcp_servers: Default::default(), mapping_mcp_servers: Default::default(),
@@ -486,10 +487,6 @@ impl Config {
Self::functions_dir().join(MCP_FILE_NAME) 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 { pub fn global_tools_dir() -> PathBuf {
Self::functions_dir().join(GLOBAL_TOOLS_DIR_NAME) Self::functions_dir().join(GLOBAL_TOOLS_DIR_NAME)
} }
@@ -2828,7 +2825,7 @@ impl Config {
} }
fn load_functions(&mut self) -> Result<()> { fn load_functions(&mut self) -> Result<()> {
self.functions = Functions::init()?; self.functions = Functions::init(self.visible_tools.as_ref().unwrap_or(&Vec::new()))?;
Ok(()) Ok(())
} }
+20 -46
View File
@@ -167,25 +167,19 @@ impl Functions {
Ok(()) Ok(())
} }
pub fn init() -> Result<Self> { pub fn init(visible_tools: &[String]) -> Result<Self> {
Self::install_global_tools()?; Self::install_global_tools()?;
Self::clear_global_functions_bin_dir()?; Self::clear_global_functions_bin_dir()?;
info!(
"Initializing global functions from {}",
Config::global_tools_file().display()
);
let declarations = Self { let declarations = Self {
declarations: Self::build_global_tool_declarations_from_path( declarations: Self::build_global_tool_declarations(visible_tools)?,
&Config::global_tools_file(),
)?,
}; };
info!( info!(
"Building global function binaries in {}", "Building global function binaries in {}",
Config::functions_bin_dir().display() 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) Ok(declarations)
} }
@@ -195,15 +189,14 @@ impl Functions {
Self::clear_agent_bin_dir(name)?; Self::clear_agent_bin_dir(name)?;
let global_tools_declarations = if !global_tools.is_empty() { let global_tools_declarations = if !global_tools.is_empty() {
let enabled_tools = global_tools.join("\n"); info!("Loading global tools for agent: {name}: {global_tools:?}");
info!("Loading global tools for agent: {name}: {enabled_tools}"); let tools_declarations = Self::build_global_tool_declarations(global_tools)?;
let tools_declarations = Self::build_global_tool_declarations(&enabled_tools)?;
info!( info!(
"Building global function binaries required by agent: {name} in {}", "Building global function binaries required by agent: {name} in {}",
Config::functions_bin_dir().display() 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 tools_declarations
} else { } else {
debug!("No global tools found for agent: {}", name); debug!("No global tools found for agent: {}", name);
@@ -311,31 +304,20 @@ impl Functions {
} }
} }
fn build_global_tool_declarations(enabled_tools: &str) -> Result<Vec<FunctionDeclaration>> { fn build_global_tool_declarations(
enabled_tools: &[String],
) -> Result<Vec<FunctionDeclaration>> {
let global_tools_directory = Config::global_tools_dir(); let global_tools_directory = Config::global_tools_dir();
let mut function_declarations = Vec::new(); let mut function_declarations = Vec::new();
for line in enabled_tools.lines() { for tool in enabled_tools {
if line.starts_with('#') { let declaration = Self::generate_declarations(&global_tools_directory.join(tool))?;
continue;
}
let declaration = Self::generate_declarations(&global_tools_directory.join(line))?;
function_declarations.extend(declaration); function_declarations.extend(declaration);
} }
Ok(function_declarations) Ok(function_declarations)
} }
fn build_global_tool_declarations_from_path(
tools_txt_path: &PathBuf,
) -> Result<Vec<FunctionDeclaration>> {
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<Vec<FunctionDeclaration>> { fn generate_declarations(tools_file_path: &Path) -> Result<Vec<FunctionDeclaration>> {
info!( info!(
"Loading tool definitions from {}", "Loading tool definitions from {}",
@@ -384,26 +366,25 @@ impl Functions {
} }
} }
fn build_global_function_binaries(enabled_tools: &str, agent_name: Option<&str>) -> Result<()> { fn build_global_function_binaries(
for line in enabled_tools.lines() { enabled_tools: &[String],
if line.starts_with('#') { agent_name: Option<&str>,
continue; ) -> Result<()> {
} for tool in enabled_tools {
let language = Language::from( let language = Language::from(
&Path::new(line) &Path::new(&tool)
.extension() .extension()
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
.map(|s| s.to_lowercase()) .map(|s| s.to_lowercase())
.ok_or_else(|| { .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() .file_stem()
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
.ok_or_else(|| { .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 { if language == Language::Unsupported {
@@ -416,13 +397,6 @@ impl Functions {
Ok(()) 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<()> { fn clear_agent_bin_dir(name: &str) -> Result<()> {
let agent_bin_directory = Config::agent_bin_dir(name); let agent_bin_directory = Config::agent_bin_dir(name);
if !agent_bin_directory.exists() { if !agent_bin_directory.exists() {