Skip to main content

merry_runtime/
interactive.rs

1//! Runtime-owned interactive agent loop.
2//!
3//! The public module is a narrow facade over the interactive producer and its
4//! focused queue, command, plan, settings, and handle components.
5
6mod commands;
7mod handles;
8mod plan;
9mod producer;
10mod queue;
11mod settings;
12mod types;
13
14use crate::{AgentLoopConfig, Runtime, RuntimeError, StepContext};
15use producer::{InteractiveProducer, InteractiveProducerInput};
16use std::sync::{Arc, atomic::AtomicU64};
17use tokio::sync::{mpsc, watch};
18use tokio_stream::wrappers::ReceiverStream;
19
20pub use self::handles::{
21    AgentLoopControl, AgentLoopInput, InteractiveAgentRun, InteractiveInputItem,
22    InteractiveInputSnapshot, InteractiveRunEventStream, InteractiveRunMessage,
23};
24pub use self::settings::{
25    InteractivePrimaryModel, InteractiveSettingsUpdate, InteractiveSubagentSettings,
26};
27pub use self::types::{InteractiveError, InteractiveRunId, InterruptReason, QueuedInputId};
28
29impl Runtime {
30    pub fn start_interactive_agent_run(
31        &self,
32        context: StepContext,
33        config: AgentLoopConfig,
34    ) -> Result<InteractiveAgentRun, RuntimeError> {
35        let loop_permit = self.acquire_active_step_permit()?;
36        let (parent_token, generation_config, context_contract) = context.into_parts();
37        let config = config
38            .merge_context_final_output_contract(context_contract)
39            .map_err(|source| RuntimeError::AgentLoopConfig { source })?;
40        let loop_token = parent_token.child_token();
41        let producer_token = loop_token.clone();
42        let subagent_completion_notify = self.subagent_completion_notify();
43        let run_id = types::next_interactive_run_id();
44        let (message_sender, message_receiver) = mpsc::channel(16);
45        let (rate_sender, rate_receiver) = watch::channel(None);
46        let (command_sender, command_receiver) = mpsc::channel(16);
47        let (bridge_sender, bridge_receiver) = mpsc::channel(4);
48        let bridge_resolution_epoch = Arc::new(AtomicU64::new(0));
49        let subagent_event_receiver = self.subscribe_subagent_events();
50        let producer = InteractiveProducer::new(InteractiveProducerInput {
51            runtime: self.clone(),
52            command_receiver,
53            subagent_event_receiver,
54            subagent_completion_notify,
55            message_sender,
56            rate_sender,
57            bridge_receiver,
58            bridge_resolution_epoch: Arc::clone(&bridge_resolution_epoch),
59            loop_token: producer_token,
60            generation_config,
61            config,
62            loop_permit,
63        });
64        let producer_handle = tokio::spawn(async move { producer.run().await });
65
66        Ok(InteractiveAgentRun::new(
67            InteractiveRunEventStream::new(
68                run_id,
69                ReceiverStream::new(message_receiver),
70                rate_receiver,
71                loop_token,
72                producer_handle,
73                bridge_sender,
74                bridge_resolution_epoch,
75            ),
76            AgentLoopInput::new(run_id, command_sender.clone()),
77            AgentLoopControl::new(run_id, command_sender),
78        ))
79    }
80}