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: {reason}")]
69    ProducerTaskFailed {
70        run_id: InteractiveRunId,
71        reason: String,
72    },
73    #[error("interactive run {run_id:?} command channel is closed")]
74    CommandChannelClosed { run_id: InteractiveRunId },
75    #[error("interactive run {run_id:?} has unresolved tool invocations")]
76    ToolInvocationsPending { run_id: InteractiveRunId },
77    #[error("interactive run {run_id:?} has no pending tool invocations")]
78    NoPendingToolInvocations { run_id: InteractiveRunId },
79    #[error("interactive run {run_id:?} has an unresolved hook invocation")]
80    HookInvocationPending { run_id: InteractiveRunId },
81    #[error("interactive run {run_id:?} has no pending hook invocation")]
82    NoPendingHookInvocation { run_id: InteractiveRunId },
83    #[error(
84        "interactive run {run_id:?} emitted a hook invocation; consume it with the message protocol"
85    )]
86    HookInvocationRequiresMessageProtocol { run_id: InteractiveRunId },
87    #[error(
88        "interactive run {run_id:?} emitted {count} tool invocations; consume them with the message protocol"
89    )]
90    ToolInvocationsRequireMessageProtocol {
91        run_id: InteractiveRunId,
92        count: usize,
93    },
94    #[error("interactive run {run_id:?} produced an invalid tool invocation batch")]
95    InvalidToolInvocationBatch { run_id: InteractiveRunId },
96    #[error("invalid interactive input: {reason}")]
97    InvalidInput { reason: &'static str },
98    #[error("interactive input is unknown")]
99    UnknownInput,
100    #[error("interactive input is already accepted")]
101    AlreadyAccepted,
102    #[error("interactive input is already removed")]
103    AlreadyRemoved,
104    #[error("interactive pending input order is invalid: {reason}")]
105    InvalidPendingOrder { reason: &'static str },
106    #[error("interactive pending input order is stale: {reason}")]
107    StalePendingOrder { reason: &'static str },
108    #[error("runtime error while running interactive loop: {source}")]
109    Runtime {
110        #[from]
111        source: RuntimeError,
112    },
113    #[error("plan control failed: {source}")]
114    Plan {
115        #[from]
116        source: PlanError,
117    },
118    #[error("plan controls require an idle interactive boundary")]
119    PlanControlRequiresIdle,
120    #[error("session save requires an idle interactive boundary")]
121    SessionSaveRequiresIdle,
122    #[error("context compaction requires an idle interactive boundary")]
123    CompactRequiresIdle,
124}