Skip to main content

bamboo_engine/runtime/managers/
tool.rs

1use async_trait::async_trait;
2use bamboo_agent_core::tools::{ToolCall, ToolSchema};
3use bamboo_agent_core::{AgentError, AgentEvent, Session};
4use bamboo_metrics::MetricsCollector;
5use tokio::sync::mpsc;
6
7use tokio_util::sync::CancellationToken;
8
9use crate::runtime::config::AgentLoopConfig;
10use crate::runtime::task_context::TaskLoopContext;
11
12/// Result of a round's tool execution.
13#[derive(Debug, Clone)]
14pub struct ToolRoundResult {
15    pub awaiting_clarification: bool,
16    pub should_break: bool,
17    pub tool_calls_count: usize,
18}
19
20/// Manages tool surface, schemas, routing, execution, and output processing.
21#[async_trait]
22pub trait ToolManager: Send + Sync {
23    /// Resolve available tool schemas for the session.
24    fn resolve_tool_schemas(&self, config: &AgentLoopConfig, session: &Session) -> Vec<ToolSchema>;
25
26    /// Execute tool calls for a round.
27    ///
28    /// `cancel` is checked DURING tool execution (not just between rounds): the
29    /// implementation must drop the in-flight tool work and return
30    /// [`AgentError::Cancelled`] if the token fires, mirroring the live pipeline's
31    /// #30 biased-cancel wrap. #104.
32    #[allow(clippy::too_many_arguments)]
33    async fn execute_tool_calls(
34        &self,
35        tool_calls: &[ToolCall],
36        event_tx: &mpsc::Sender<AgentEvent>,
37        metrics_collector: Option<&MetricsCollector>,
38        session_id: &str,
39        round_id: &str,
40        round: usize,
41        session: &mut Session,
42        config: &AgentLoopConfig,
43        task_context: &mut Option<TaskLoopContext>,
44        tool_schemas: &[ToolSchema],
45        cancel: &CancellationToken,
46    ) -> Result<ToolRoundResult, AgentError>;
47}