tools: Tool is Copy, clean dispatch without Arc clone
Tool derives Copy (all fields are Copy: &'static str + fn pointer). dispatch_with_agent copies the Tool out of the agent lock guard, drops the guard, then calls the handler. No Arc cloning needed. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
d9e1c2c59f
commit
d195160b1e
1 changed files with 12 additions and 20 deletions
|
|
@ -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,28 +174,19 @@ pub async fn dispatch_with_agent(
|
|||
agent: Option<std::sync::Arc<tokio::sync::Mutex<super::Agent>>>,
|
||||
) -> 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.
|
||||
pub async fn dispatch_shared(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue