Skip to main content

bamboo_engine/runtime/managers/adapters/
tool.rs

1use std::sync::Arc;
2
3use crate::metrics::MetricsCollector;
4use async_trait::async_trait;
5use bamboo_agent_core::tools::{ToolCall, ToolExecutor, ToolSchema};
6use bamboo_agent_core::{AgentError, AgentEvent, Session};
7use bamboo_infrastructure::LLMProvider;
8use tokio::sync::mpsc;
9
10use crate::runtime::config::AgentLoopConfig;
11use crate::runtime::managers::tool::{ToolManager, ToolRoundResult};
12use crate::runtime::task_context::TaskLoopContext;
13
14/// Default tool manager that delegates to existing runner functions.
15pub struct DefaultToolManager {
16    tools: Arc<dyn ToolExecutor>,
17    llm: Arc<dyn LLMProvider>,
18}
19
20impl DefaultToolManager {
21    pub fn new(tools: Arc<dyn ToolExecutor>, llm: Arc<dyn LLMProvider>) -> Self {
22        Self { tools, llm }
23    }
24}
25
26#[async_trait]
27impl ToolManager for DefaultToolManager {
28    fn resolve_tool_schemas(&self, config: &AgentLoopConfig, session: &Session) -> Vec<ToolSchema> {
29        crate::runtime::runner::session_setup::tool_schemas::resolve_available_tool_schemas_for_session(
30            config,
31            self.tools.as_ref(),
32            session,
33        )
34    }
35
36    #[allow(clippy::too_many_arguments)]
37    async fn execute_tool_calls(
38        &self,
39        tool_calls: &[ToolCall],
40        event_tx: &mpsc::Sender<AgentEvent>,
41        metrics_collector: Option<&MetricsCollector>,
42        session_id: &str,
43        round_id: &str,
44        round: usize,
45        session: &mut Session,
46        config: &AgentLoopConfig,
47        task_context: &mut Option<TaskLoopContext>,
48        tool_schemas: &[ToolSchema],
49    ) -> Result<ToolRoundResult, AgentError> {
50        let result = crate::runtime::runner::tool_execution::execute_round_tool_calls(
51            tool_calls,
52            event_tx,
53            metrics_collector,
54            session_id,
55            round_id,
56            round,
57            session,
58            &self.tools,
59            config,
60            task_context,
61            &self.llm,
62            config.background_model_name.as_deref(),
63            config.background_model_provider.as_ref(),
64            tool_schemas,
65        )
66        .await?;
67
68        Ok(ToolRoundResult {
69            awaiting_clarification: result.awaiting_clarification,
70            should_break: false,
71            tool_calls_count: tool_calls.len(),
72        })
73    }
74}