use bamboo_agent_core::AgentEvent;
use tokio::sync::broadcast;
use crate::runtime::execution::session_events::get_or_create_event_sender;
use crate::runtime::execution::spawn::{SpawnContext, SpawnJob};
#[derive(Debug, Clone)]
pub struct RunChildInput {
pub child_session_id: String,
pub parent_session_id: String,
pub model: String,
}
pub struct ChildRunner {
ctx: SpawnContext,
}
pub fn child_runner(ctx: SpawnContext) -> ChildRunner {
ChildRunner::new(ctx)
}
impl ChildRunner {
pub fn new(ctx: SpawnContext) -> Self {
Self { ctx }
}
pub(crate) fn build_job(&self, input: &RunChildInput) -> SpawnJob {
SpawnJob {
parent_session_id: input.parent_session_id.clone(),
child_session_id: input.child_session_id.clone(),
model: input.model.clone(),
disabled_tools: None,
}
}
pub async fn run_child(&self, input: RunChildInput) -> Result<(), String> {
let job = self.build_job(&input);
crate::sdk::spawn::run_child_spawn(self.ctx.clone(), job).await
}
pub async fn run_child_stream(
&self,
input: RunChildInput,
) -> Result<broadcast::Receiver<AgentEvent>, String> {
let child_tx =
get_or_create_event_sender(&self.ctx.session_event_senders, &input.child_session_id)
.await;
let rx = child_tx.subscribe();
let job = self.build_job(&input);
crate::sdk::spawn::run_child_spawn(self.ctx.clone(), job).await?;
Ok(rx)
}
}