use std::collections::HashMap;
use crate::{
tool::{Tool, executor::ToolExecutor},
types::ToolCall,
};
use async_trait::async_trait;
pub struct SequentialExecutor {
tools: HashMap<String, Box<dyn Tool + Sync>>,
}
impl SequentialExecutor {
pub fn new() -> Self {
Self {
tools: HashMap::new(),
}
}
}
#[async_trait]
impl ToolExecutor for SequentialExecutor {
fn add(&mut self, name: String, tool: Box<dyn Tool + Sync>) -> Option<Box<dyn Tool + Sync>> {
tracing::trace!("Registering tool: {}", name);
self.tools.insert(name, tool)
}
async fn execute(&self, calls: Vec<ToolCall>) -> Vec<crate::types::ToolResult> {
tracing::debug!("Executing {} tool calls sequentially", calls.len());
let mut results = Vec::new();
for call in calls {
tracing::debug!("Executing tool '{}'", call.function.name);
if let Some(tool) = self.tools.get(&call.function.name) {
results.push(tool.call_timed(call).await);
} else {
tracing::debug!("Tool '{}' not found", call.function.name);
results.push(super::tool_not_found_result(call.id, &call.function.name));
}
}
tracing::debug!("Sequential execution completed");
results
}
}