Skip to main content

merry_runtime/interactive/
types.rs

1use crate::{PlanError, RuntimeError};
2use std::sync::atomic::{AtomicU64, Ordering};
3use thiserror::Error;
4
5static NEXT_INTERACTIVE_RUN_ID: AtomicU64 = AtomicU64::new(1);
6
7pub(super) fn next_interactive_run_id() -> InteractiveRunId {
8    InteractiveRunId(NEXT_INTERACTIVE_RUN_ID.fetch_add(1, Ordering::Relaxed))
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct InteractiveRunId(pub(super) u64);
13
14impl InteractiveRunId {
15    #[must_use]
16    pub fn as_u64(self) -> u64 {
17        self.0
18    }
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum InterruptReason {
23    User,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub(super) struct InputRecord {
28    pub id: QueuedInputId,
29    pub text: String,
30}
31
32#[derive(Debug, Clone, Default, PartialEq, Eq)]
33pub(super) struct InputRecords {
34    pub items: Vec<InputRecord>,
35    pub paused: bool,
36}
37
38/// Stable runtime-assigned identifier for one pending interactive input.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
40pub struct QueuedInputId(u64);
41
42impl QueuedInputId {
43    #[must_use]
44    pub const fn from_u64(value: u64) -> Self {
45        Self(value)
46    }
47
48    #[must_use]
49    pub const fn as_u64(self) -> u64 {
50        self.0
51    }
52}
53
54#[derive(Debug, Error)]
55pub enum InteractiveError {
56    /// The interactive producer ended without emitting the terminal closed event.
57    #[error("interactive run {run_id:?} is closed")]
58    RunClosed { run_id: InteractiveRunId },
59    /// The producer stopped before the stream's normal terminal event.
60    #[error(
61        "interactive run {run_id:?} producer stopped before the terminal closed event: {message}"
62    )]
63    ProducerStopped {
64        run_id: InteractiveRunId,
65        message: &'static str,
66    },
67    /// The producer task could not be joined successfully.
68    #[error("interactive run {run_id:?} producer task failed")]
69    ProducerTaskFailed { run_id: InteractiveRunId },
70    #[error("interactive run {run_id:?} command channel is closed")]
71    CommandChannelClosed { run_id: InteractiveRunId },
72    #[error("interactive run {run_id:?} has unresolved tool invocations")]
73    ToolInvocationsPending { run_id: InteractiveRunId },
74    #[error("interactive run {run_id:?} has no pending tool invocations")]
75    NoPendingToolInvocations { run_id: InteractiveRunId },
76    #[error(
77        "interactive run {run_id:?} emitted {count} tool invocations; consume them with the message protocol"
78    )]
79    ToolInvocationsRequireMessageProtocol {
80        run_id: InteractiveRunId,
81        count: usize,
82    },
83    #[error("interactive run {run_id:?} produced an invalid tool invocation batch")]
84    InvalidToolInvocationBatch { run_id: InteractiveRunId },
85    #[error("invalid interactive input: {reason}")]
86    InvalidInput { reason: &'static str },
87    #[error("interactive input is unknown")]
88    UnknownInput,
89    #[error("interactive input is already accepted")]
90    AlreadyAccepted,
91    #[error("interactive input is already removed")]
92    AlreadyRemoved,
93    #[error("interactive pending input order is invalid: {reason}")]
94    InvalidPendingOrder { reason: &'static str },
95    #[error("interactive pending input order is stale: {reason}")]
96    StalePendingOrder { reason: &'static str },
97    #[error("runtime error while running interactive loop: {source}")]
98    Runtime {
99        #[from]
100        source: RuntimeError,
101    },
102    #[error("plan control failed: {source}")]
103    Plan {
104        #[from]
105        source: PlanError,
106    },
107    #[error("plan controls require an idle interactive boundary")]
108    PlanControlRequiresIdle,
109    #[error("session save requires an idle interactive boundary")]
110    SessionSaveRequiresIdle,
111    #[error("context compaction requires an idle interactive boundary")]
112    CompactRequiresIdle,
113}