diff --git a/src/agent/tools/mod.rs b/src/agent/tools/mod.rs index 3be298f..5d75995 100644 --- a/src/agent/tools/mod.rs +++ b/src/agent/tools/mod.rs @@ -37,6 +37,7 @@ pub type ToolHandler = fn( /// A tool with its definition and handler — single source of truth. /// Strings are static — the tool list JSON can be built without /// serialization by interpolating these directly. +#[derive(Clone, Copy)] pub struct Tool { pub name: &'static str, pub description: &'static str, @@ -173,27 +174,18 @@ pub async fn dispatch_with_agent( agent: Option>>, ) -> String { // Look up in agent's tools if available, otherwise global - if let Some(ref agent) = agent { - let agent_guard = agent.lock().await; - if let Some(tool) = agent_guard.tools.iter().find(|t| t.name == name) { - let handler = tool.handler; - drop(agent_guard); - return match handler(Some(agent.clone()), args.clone()).await { - Ok(s) => s, - Err(e) => format!("Error: {}", e), - }; - } + let tool = if let Some(ref a) = agent { + let guard = a.lock().await; + guard.tools.iter().find(|t| t.name == name).copied() + } else { + None + }; + let tool = tool.or_else(|| tools().into_iter().find(|t| t.name == name)); + match tool { + Some(t) => (t.handler)(agent, args.clone()).await + .unwrap_or_else(|e| format!("Error: {}", e)), + None => format!("Error: Unknown tool: {}", name), } - // Fallback to global registry - for tool in tools() { - if tool.name == name { - return match (tool.handler)(agent, args.clone()).await { - Ok(s) => s, - Err(e) => format!("Error: {}", e), - }; - } - } - format!("Error: Unknown tool: {}", name) } /// Dispatch shared tools — used by subconscious agents.