Skip to main content

bamboo_engine/runtime/managers/
llm.rs

1use crate::metrics::TokenUsage;
2use async_trait::async_trait;
3use bamboo_agent_core::tools::ToolSchema;
4use bamboo_agent_core::{AgentError, AgentEvent, Session};
5use tokio::sync::mpsc;
6use tokio_util::sync::CancellationToken;
7
8use crate::runtime::config::AgentLoopConfig;
9
10/// Output from a single LLM round execution.
11#[derive(Debug, Clone)]
12pub struct LlmRoundOutput {
13    pub content: String,
14    pub reasoning_content: String,
15    pub tool_calls: Vec<bamboo_agent_core::tools::ToolCall>,
16    pub prompt_tokens: u64,
17    pub completion_tokens: u64,
18    pub response_id: Option<String>,
19    pub round_usage: TokenUsage,
20}
21
22/// Manages LLM interaction: message preparation, stream handling, retry/fallback.
23#[async_trait]
24pub trait LlmManager: Send + Sync {
25    /// Execute a single LLM round.
26    #[allow(clippy::too_many_arguments)]
27    async fn execute_round(
28        &self,
29        session: &mut Session,
30        config: &AgentLoopConfig,
31        event_tx: &mpsc::Sender<AgentEvent>,
32        cancel_token: &CancellationToken,
33        session_id: &str,
34        model_name: &str,
35        tool_schemas: &[ToolSchema],
36    ) -> Result<LlmRoundOutput, AgentError>;
37
38    /// Attempt overflow recovery by compressing context.
39    async fn attempt_overflow_recovery(
40        &self,
41        session: &mut Session,
42        config: &AgentLoopConfig,
43        session_id: &str,
44        event_tx: &mpsc::Sender<AgentEvent>,
45    ) -> Result<bool, AgentError>;
46}