Skip to main content

bamboo_engine/runtime/managers/adapters/
llm.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use bamboo_agent_core::tools::ToolSchema;
5use bamboo_agent_core::{AgentError, AgentEvent, Session};
6use bamboo_llm::LLMProvider;
7use tokio::sync::mpsc;
8use tokio_util::sync::CancellationToken;
9
10use crate::runtime::config::AgentLoopConfig;
11use crate::runtime::managers::llm::{LlmManager, LlmRoundOutput};
12
13/// Default LLM manager that delegates to existing runner functions.
14pub struct DefaultLlmManager {
15    llm: Arc<dyn LLMProvider>,
16}
17
18impl DefaultLlmManager {
19    pub fn new(llm: Arc<dyn LLMProvider>) -> Self {
20        Self { llm }
21    }
22}
23
24#[async_trait]
25impl LlmManager for DefaultLlmManager {
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        let result = crate::runtime::runner::round_lifecycle::execute_llm_round(
38            session,
39            config,
40            &self.llm,
41            event_tx,
42            cancel_token,
43            session_id,
44            model_name,
45            tool_schemas,
46            // This dormant lifecycle adapter does not yet carry the trusted
47            // execution-local prompt-memory provenance. `None` is unsupported
48            // coverage, not a zero-exposure observation; #1077 covers the
49            // canonical runner pipeline only.
50            None,
51        )
52        .await?;
53
54        if let Some(error) = result.terminal_validation_error {
55            return Err(error);
56        }
57
58        let (content, reasoning_content, tool_calls) = {
59            let stream = &result.stream_output;
60            (
61                stream.content.clone(),
62                stream.reasoning_content.clone(),
63                stream.tool_calls.clone(),
64            )
65        };
66
67        Ok(LlmRoundOutput {
68            content,
69            reasoning_content,
70            tool_calls,
71            prompt_tokens: result.prompt_tokens,
72            completion_tokens: result.completion_tokens,
73            response_id: None,
74            round_usage: result.attempt_usage,
75        })
76    }
77
78    async fn attempt_overflow_recovery(
79        &self,
80        session: &mut Session,
81        config: &AgentLoopConfig,
82        session_id: &str,
83        tool_schemas: &[ToolSchema],
84        event_tx: &mpsc::Sender<AgentEvent>,
85    ) -> Result<bool, AgentError> {
86        let model_name = config.model_name.as_deref().unwrap_or("unknown");
87        let request_tool_schemas =
88            crate::runtime::runner::round_lifecycle::request_tool_schemas_for_session(
89                session,
90                &self.llm,
91                model_name,
92                tool_schemas,
93            )
94            .await;
95
96        crate::runtime::runner::round_lifecycle::force_overflow_context_recovery(
97            session,
98            config,
99            model_name,
100            session_id,
101            request_tool_schemas.as_ref(),
102            &self.llm,
103            Some(event_tx),
104        )
105        .await
106    }
107}