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, InteractiveProcessExecutionUpdate, InteractiveSettingsUpdate,
26    InteractiveSubagentSettings,
27};
28pub use self::types::{InteractiveError, InteractiveRunId, InterruptReason, QueuedInputId};
29
30impl Runtime {
31    pub fn start_interactive_agent_run(
32        &self,
33        context: StepContext,
34        config: AgentLoopConfig,
35    ) -> Result<InteractiveAgentRun, RuntimeError> {
36        let loop_permit = self.acquire_active_step_permit()?;
37        let (parent_token, generation_config, context_contract) = context.into_parts();
38        let config = config
39            .merge_context_final_output_contract(context_contract)
40            .map_err(|source| RuntimeError::AgentLoopConfig { source })?;
41        let loop_token = parent_token.child_token();
42        let producer_token = loop_token.clone();
43        let subagent_completion_notify = self.subagent_completion_notify();
44        let run_id = types::next_interactive_run_id();
45        let (message_sender, message_receiver) = mpsc::channel(16);
46        let (rate_sender, rate_receiver) = watch::channel(None);
47        let (command_sender, command_receiver) = mpsc::channel(16);
48        let (bridge_sender, bridge_receiver) = mpsc::channel(4);
49        let bridge_resolution_epoch = Arc::new(AtomicU64::new(0));
50        let (hook_sender, hook_receiver) = mpsc::channel(4);
51        let hook_resolution_epoch = Arc::new(AtomicU64::new(0));
52        let subagent_event_receiver = self.subscribe_subagent_events();
53        let producer = InteractiveProducer::new(InteractiveProducerInput {
54            runtime: self.clone(),
55            command_receiver,
56            subagent_event_receiver,
57            subagent_completion_notify,
58            message_sender,
59            rate_sender,
60            bridge_receiver,
61            bridge_resolution_epoch: Arc::clone(&bridge_resolution_epoch),
62            hook_receiver,
63            hook_resolution_epoch: Arc::clone(&hook_resolution_epoch),
64            loop_token: producer_token,
65            generation_config,
66            config,
67            loop_permit,
68        });
69        let producer_handle = tokio::spawn(async move { producer.run().await });
70
71        Ok(InteractiveAgentRun::new(
72            InteractiveRunEventStream::new(
73                run_id,
74                ReceiverStream::new(message_receiver),
75                rate_receiver,
76                loop_token,
77                producer_handle,
78                bridge_sender,
79                bridge_resolution_epoch,
80                hook_sender,
81                hook_resolution_epoch,
82            ),
83            AgentLoopInput::new(run_id, command_sender.clone()),
84            AgentLoopControl::new(run_id, command_sender),
85        ))
86    }
87}