tools: dispatch_with_agent uses agent's tool list
When agent is provided, looks up the tool in agent.tools first. Falls back to global registry for agent-less dispatch (MCP server, subconscious agents). Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
e982cb192f
commit
d9e1c2c59f
1 changed files with 14 additions and 0 deletions
|
|
@ -166,11 +166,25 @@ pub async fn dispatch(
|
|||
}
|
||||
|
||||
/// Dispatch a tool call with optional agent context.
|
||||
/// If agent is provided, uses the agent's tool list.
|
||||
pub async fn dispatch_with_agent(
|
||||
name: &str,
|
||||
args: &serde_json::Value,
|
||||
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
// Fallback to global registry
|
||||
for tool in tools() {
|
||||
if tool.name == name {
|
||||
return match (tool.handler)(agent, args.clone()).await {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue