Skip to main content

openbutler_protocol/session/
run.rs

1//! Live run state: the queue, the pending question, the tool approval, and
2//! the busy flags both snapshots and patches carry.
3
4use serde::{Deserialize, Serialize};
5use serde_json::{Map, Value};
6
7/// A queued owner message awaiting its turn.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct QueuedMessage {
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    pub id: Option<String>,
13    pub text: String,
14    pub image_count: u32,
15    #[serde(default, skip_serializing_if = "Option::is_none")]
16    pub editable: Option<bool>,
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub steering: Option<bool>,
19}
20
21/// The queued message as previewed in a snapshot: text plus image count.
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct QueuedMessageBrief {
25    pub text: String,
26    pub image_count: u32,
27}
28
29/// A question the host waits for (`session.answer`).
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct PendingQuestion {
33    pub id: String,
34    pub question: String,
35    /// Free-form options, or `null` for a plain-text question.
36    #[serde(deserialize_with = "Option::<Vec<String>>::deserialize")]
37    pub options: Option<Vec<String>>,
38}
39
40/// Declared effect of one tool call (TS `ToolEffect`), shown by approval UIs.
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42#[serde(rename_all = "kebab-case")]
43pub struct ToolEffect {
44    pub change: ToolChange,
45    pub data: ToolData,
46    pub network: ToolNetwork,
47    pub reversible: ToolReversible,
48    pub outbound: ToolOutbound,
49}
50
51/// What the call touches.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(rename_all = "kebab-case")]
54pub enum ToolChange {
55    None,
56    Create,
57    Modify,
58    Delete,
59    Execute,
60    Unknown,
61}
62
63/// Sensitivity of the data involved.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65#[serde(rename_all = "kebab-case")]
66pub enum ToolData {
67    Public,
68    Local,
69    Sensitive,
70    Secret,
71    Unknown,
72}
73
74/// Whether the call uses the network.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
76#[serde(rename_all = "kebab-case")]
77pub enum ToolNetwork {
78    None,
79    Possible,
80    Used,
81    Unknown,
82}
83
84/// Whether the effect can be undone.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(rename_all = "kebab-case")]
87pub enum ToolReversible {
88    Full,
89    Compensating,
90    None,
91    Unknown,
92}
93
94/// Where results flow.
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "kebab-case")]
97pub enum ToolOutbound {
98    None,
99    Model,
100    ToolServer,
101    ModelAndToolServer,
102    Unknown,
103}
104
105/// A tool call waiting for a decision (`pendingApproval`).
106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
107#[serde(rename_all = "camelCase")]
108pub struct ToolCallRequest {
109    pub request_id: String,
110    pub provider_id: String,
111    pub name: String,
112    #[serde(default, skip_serializing_if = "Option::is_none")]
113    pub title: Option<String>,
114    pub input: Map<String, Value>,
115    pub effect: ToolEffect,
116    #[serde(
117        default,
118        skip_serializing_if = "Option::is_none",
119        rename = "timeoutSeconds"
120    )]
121    pub timeout_seconds: Option<u64>,
122    /// Why the deterministic gates escalated this call to a human or reviewer.
123    #[serde(
124        default,
125        skip_serializing_if = "Option::is_none",
126        rename = "gateReason"
127    )]
128    pub gate_reason: Option<String>,
129}
130
131/// The run state both snapshots and patches carry, field for field identical
132/// in the two TypeScript types.
133///
134/// `busy`, `queuedMessageCount`, and `queuedMessages` are plain required
135/// fields; the `null`-able ones are required *and* nullable — the host always
136/// writes them, `null` included, so absence marks a malformed frame.
137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct RunState {
140    pub busy: bool,
141    /// The turn the running work writes into; `null` before a run has placed
142    /// its message. Clients must not guess the live turn from `busy` plus list
143    /// position.
144    #[serde(deserialize_with = "Option::<String>::deserialize")]
145    pub running_turn_id: Option<String>,
146    #[serde(deserialize_with = "Option::<ToolCallRequest>::deserialize")]
147    pub pending_approval: Option<ToolCallRequest>,
148    #[serde(deserialize_with = "Option::<String>::deserialize")]
149    pub pending_approval_id: Option<String>,
150    #[serde(deserialize_with = "Option::<PendingQuestion>::deserialize")]
151    pub pending_question: Option<PendingQuestion>,
152    pub queued_message_count: u32,
153    #[serde(deserialize_with = "Option::<QueuedMessageBrief>::deserialize")]
154    pub first_queued_message: Option<QueuedMessageBrief>,
155    pub queued_messages: Vec<QueuedMessage>,
156    #[serde(deserialize_with = "Option::<String>::deserialize")]
157    pub session_save_error: Option<String>,
158    /// Absolute index of the entry currently streaming, or `null`.
159    #[serde(deserialize_with = "Option::<i64>::deserialize")]
160    pub streaming_assistant_index: Option<i64>,
161}