Skip to main content

bamboo_engine/runtime/managers/
lifecycle.rs

1use async_trait::async_trait;
2use bamboo_agent_core::tools::ToolExecutor;
3use bamboo_agent_core::{AgentError, AgentEvent, Session};
4use bamboo_domain::AgentRuntimeState;
5use bamboo_infrastructure::LLMProvider;
6use tokio::sync::mpsc;
7use tokio_util::sync::CancellationToken;
8
9use crate::runtime::config::AgentLoopConfig;
10use crate::runtime::task_context::TaskLoopContext;
11
12/// Manages agent run state machine and round transitions.
13#[async_trait]
14pub trait LifecycleManager: Send + Sync {
15    /// Initialize runtime state for a new agent run.
16    fn initialize_run(&self, session: &Session, config: &AgentLoopConfig) -> AgentRuntimeState;
17
18    /// Prepare for a new round. Returns the round ID.
19    #[allow(clippy::too_many_arguments)]
20    async fn prepare_round(
21        &self,
22        session: &mut Session,
23        task_context: &mut Option<TaskLoopContext>,
24        runtime_state: &mut AgentRuntimeState,
25        round: usize,
26        max_rounds: usize,
27        config: &AgentLoopConfig,
28        cancel_token: &CancellationToken,
29        metrics_collector: Option<&crate::metrics::MetricsCollector>,
30        session_id: &str,
31        model_name: &str,
32        tools: &dyn ToolExecutor,
33        llm: &dyn LLMProvider,
34    ) -> Result<String, AgentError>;
35
36    /// Handle post-round processing and determine next action.
37    /// Returns `true` if the agent run should break out of the round loop.
38    async fn handle_round_outcome(
39        &self,
40        session: &mut Session,
41        runtime_state: &mut AgentRuntimeState,
42        task_context: &mut Option<TaskLoopContext>,
43        round: usize,
44        should_break: bool,
45    ) -> Result<bool, AgentError>;
46
47    /// Finalize the agent run.
48    #[allow(clippy::too_many_arguments)]
49    async fn finalize_run(
50        &self,
51        session: &mut Session,
52        runtime_state: &mut AgentRuntimeState,
53        event_tx: &mpsc::Sender<AgentEvent>,
54        session_id: &str,
55        config: &AgentLoopConfig,
56        metrics_collector: Option<&crate::metrics::MetricsCollector>,
57        task_context: Option<TaskLoopContext>,
58    );
59}