bamboo_engine/runtime/managers/adapters/
lifecycle.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use bamboo_agent_core::tools::ToolExecutor;
5use bamboo_agent_core::{AgentError, AgentEvent, Session};
6use bamboo_domain::{AgentRuntimeState, AgentStatusState};
7use bamboo_infrastructure::LLMProvider;
8use tokio::sync::mpsc;
9use tokio_util::sync::CancellationToken;
10
11use crate::metrics::MetricsCollector;
12use crate::runtime::config::AgentLoopConfig;
13use crate::runtime::managers::lifecycle::LifecycleManager;
14use crate::runtime::runner::state_bridge;
15use crate::runtime::task_context::TaskLoopContext;
16
17pub struct DefaultLifecycleManager {
19 llm: Arc<dyn LLMProvider>,
20}
21
22impl DefaultLifecycleManager {
23 pub fn new(llm: Arc<dyn LLMProvider>) -> Self {
24 Self { llm }
25 }
26}
27
28#[async_trait]
29impl LifecycleManager for DefaultLifecycleManager {
30 fn initialize_run(&self, session: &Session, config: &AgentLoopConfig) -> AgentRuntimeState {
31 let mut state = AgentRuntimeState::new(&session.id);
32 state.llm.model_name = config.model_name.clone();
33 state.llm.provider_name = config.provider_name.clone();
34 state.llm.fast_model_name = config.fast_model_name.clone();
35 state.llm.background_model_name = config.background_model_name.clone();
36 state.round.max_rounds = config.max_rounds as u32;
37 state.status = AgentStatusState::Initializing;
38 state
39 }
40
41 #[allow(clippy::too_many_arguments)]
42 async fn prepare_round(
43 &self,
44 session: &mut Session,
45 task_context: &mut Option<TaskLoopContext>,
46 _runtime_state: &mut AgentRuntimeState,
47 round: usize,
48 max_rounds: usize,
49 config: &AgentLoopConfig,
50 cancel_token: &CancellationToken,
51 metrics_collector: Option<&MetricsCollector>,
52 session_id: &str,
53 model_name: &str,
54 tools: &dyn ToolExecutor,
55 _llm: &dyn LLMProvider,
56 ) -> Result<String, AgentError> {
57 crate::runtime::runner::round_prelude::prepare_round(
58 session,
59 task_context,
60 round,
61 max_rounds,
62 cancel_token,
63 metrics_collector,
64 session_id,
65 model_name,
66 false, config,
68 self.llm.clone(),
69 tools,
70 )
71 .await
72 }
73
74 async fn handle_round_outcome(
75 &self,
76 session: &mut Session,
77 runtime_state: &mut AgentRuntimeState,
78 _task_context: &mut Option<TaskLoopContext>,
79 round: usize,
80 should_break: bool,
81 ) -> Result<bool, AgentError> {
82 runtime_state.round.current_round = round as u32;
83
84 if should_break {
85 runtime_state.status = AgentStatusState::Finalizing;
86 } else if round as u32 >= runtime_state.round.max_rounds {
87 tracing::info!(
88 "[{}] Reached max rounds ({})",
89 session.id,
90 runtime_state.round.max_rounds
91 );
92 return Ok(true);
93 }
94
95 state_bridge::write_runtime_state(session, runtime_state);
96 Ok(should_break)
97 }
98
99 #[allow(clippy::too_many_arguments)]
100 async fn finalize_run(
101 &self,
102 session: &mut Session,
103 runtime_state: &mut AgentRuntimeState,
104 event_tx: &mpsc::Sender<AgentEvent>,
105 session_id: &str,
106 config: &AgentLoopConfig,
107 metrics_collector: Option<&MetricsCollector>,
108 task_context: Option<TaskLoopContext>,
109 ) {
110 runtime_state.status = AgentStatusState::Completed;
111 state_bridge::write_runtime_state(session, runtime_state);
112
113 crate::runtime::runner::session_finalize::finalize_session(
114 task_context,
115 session,
116 event_tx,
117 session_id,
118 config,
119 metrics_collector,
120 false,
121 runtime_state,
122 )
123 .await;
124 }
125}