refactor: Removed the use of the tools.txt file and added tool visibility declarations to the global configuration file
This commit is contained in:
+3
-6
@@ -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<String, String>,
|
||||
pub use_tools: Option<String>,
|
||||
pub visible_tools: Option<Vec<String>>,
|
||||
|
||||
pub mcp_servers: bool,
|
||||
pub mapping_mcp_servers: IndexMap<String, String>,
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
+20
-46
@@ -167,25 +167,19 @@ impl Functions {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn init() -> Result<Self> {
|
||||
pub fn init(visible_tools: &[String]) -> Result<Self> {
|
||||
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<Vec<FunctionDeclaration>> {
|
||||
fn build_global_tool_declarations(
|
||||
enabled_tools: &[String],
|
||||
) -> Result<Vec<FunctionDeclaration>> {
|
||||
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<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>> {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user