Skip to main content

openbutler_protocol/session/
entry.rs

1//! Transcript entries: the tagged union a renderer consumes.
2//!
3//! An entry is a body (`kind`-tagged) flattened with shared metadata
4//! (`at`, `turnId`, `internal`, `turnOutcome`, `turnEndedAt`) — one flat JSON
5//! object on the wire, exactly like `SessionEntry = SessionEntryBody &
6//! SessionEntryMeta` in TypeScript.
7
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10
11/// File-backed payload carried by user entries and inbox messages.
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct FileAttachment {
15    pub id: String,
16    pub path: String,
17    pub name: String,
18    pub media_type: String,
19    pub size_bytes: i64,
20    pub sha256: String,
21}
22
23/// Project binding a session belongs to (`SessionProject`). `id` and `name`
24/// are nullable: a locked folder set can outlive its project.
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct SessionProject {
28    #[serde(deserialize_with = "Option::<String>::deserialize")]
29    pub id: Option<String>,
30    #[serde(deserialize_with = "Option::<String>::deserialize")]
31    pub name: Option<String>,
32    pub folders: Vec<String>,
33    pub workspace_root: String,
34    pub locked: bool,
35}
36
37/// Severity of a notice entry.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39#[serde(rename_all = "lowercase")]
40pub enum NoticeTone {
41    Info,
42    Warn,
43    Error,
44}
45
46/// Where a user entry came from (TS `TURN_SOURCES` — the single authority
47/// shared by the type and the save validator).
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(rename_all = "kebab-case")]
50pub enum UserEntrySource {
51    Terminal,
52    Web,
53    Telegram,
54    Inbox,
55    OwnerInbox,
56    Schedule,
57    Background,
58    Team,
59    Worker,
60}
61
62/// Machine-readable internal marker; a compaction checkpoint is stored so the
63/// record stays complete, but readers show the compaction row instead of the
64/// raw summary.
65#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
66#[serde(rename_all = "lowercase")]
67pub enum EntryInternal {
68    Compaction,
69}
70
71/// Outcome recorded when a turn ends (`TurnCompletion`).
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "lowercase")]
74pub enum TurnOutcome {
75    Completed,
76    Cancelled,
77    Failed,
78}
79
80/// Progress of a compaction row in the transcript.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82#[serde(rename_all = "lowercase")]
83pub enum CompactionStatus {
84    Running,
85    Done,
86    Failed,
87}
88
89/// Metadata every entry carries so a client can group and time the work.
90#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
91pub struct SessionEntryMeta {
92    /// Epoch milliseconds when the entry was first written.
93    #[serde(default, skip_serializing_if = "Option::is_none", rename = "at")]
94    pub at: Option<i64>,
95    /// Id of the user message that opened the turn this entry belongs to.
96    #[serde(default, skip_serializing_if = "Option::is_none", rename = "turnId")]
97    pub turn_id: Option<String>,
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub internal: Option<EntryInternal>,
100    #[serde(
101        default,
102        skip_serializing_if = "Option::is_none",
103        rename = "turnOutcome"
104    )]
105    pub turn_outcome: Option<TurnOutcome>,
106    #[serde(
107        default,
108        skip_serializing_if = "Option::is_none",
109        rename = "turnEndedAt"
110    )]
111    pub turn_ended_at: Option<i64>,
112}
113
114/// One transcript entry on the wire (TS `SessionEntry`).
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116#[serde(tag = "kind", rename_all = "lowercase")]
117pub enum SessionEntry {
118    /// Owner text (or an inbox message), with optional attachments.
119    User {
120        #[serde(flatten)]
121        meta: SessionEntryMeta,
122        text: String,
123        #[serde(
124            default,
125            skip_serializing_if = "Option::is_none",
126            rename = "workspaceRoot"
127        )]
128        workspace_root: Option<String>,
129        #[serde(
130            default,
131            skip_serializing_if = "Option::is_none",
132            rename = "imageCount"
133        )]
134        image_count: Option<u32>,
135        #[serde(default, skip_serializing_if = "Option::is_none")]
136        attachments: Option<Vec<FileAttachment>>,
137        #[serde(default, skip_serializing_if = "Option::is_none")]
138        source: Option<UserEntrySource>,
139        #[serde(default, skip_serializing_if = "Option::is_none", rename = "eventId")]
140        event_id: Option<String>,
141    },
142    /// The model's reply.
143    Assistant {
144        #[serde(flatten)]
145        meta: SessionEntryMeta,
146        text: String,
147    },
148    /// A statement from the machinery (durable, survives reconnects when it
149    /// arrives as a promoted notification).
150    Notice {
151        #[serde(flatten)]
152        meta: SessionEntryMeta,
153        text: String,
154        #[serde(default, skip_serializing_if = "Option::is_none")]
155        detail: Option<String>,
156        #[serde(default, skip_serializing_if = "Option::is_none")]
157        tone: Option<NoticeTone>,
158        /// Machine-readable reason this notice exists (e.g.
159        /// `compaction:exceeds-window`); absent on ordinary statements.
160        #[serde(default, skip_serializing_if = "Option::is_none")]
161        cause: Option<String>,
162    },
163    /// Turn separator.
164    Divider {
165        #[serde(flatten)]
166        meta: SessionEntryMeta,
167        #[serde(default, skip_serializing_if = "Option::is_none")]
168        text: Option<String>,
169    },
170    /// A reasoning block the owner chose to show.
171    Reasoning {
172        #[serde(flatten)]
173        meta: SessionEntryMeta,
174        text: String,
175        #[serde(
176            default,
177            skip_serializing_if = "Option::is_none",
178            rename = "durationMs"
179        )]
180        duration_ms: Option<i64>,
181    },
182    /// Compaction is a visible event: it changes how much context the model
183    /// can see, and a reader deserves to see when and how far.
184    Compaction {
185        #[serde(flatten)]
186        meta: SessionEntryMeta,
187        status: CompactionStatus,
188        #[serde(
189            default,
190            skip_serializing_if = "Option::is_none",
191            rename = "beforeTokens"
192        )]
193        before_tokens: Option<i64>,
194        #[serde(
195            default,
196            skip_serializing_if = "Option::is_none",
197            rename = "afterTokens"
198        )]
199        after_tokens: Option<i64>,
200        #[serde(
201            default,
202            skip_serializing_if = "Option::is_none",
203            rename = "durationMs"
204        )]
205        duration_ms: Option<i64>,
206    },
207    /// One tool invocation: complete redacted input for the expanded view.
208    Tool {
209        #[serde(flatten)]
210        meta: SessionEntryMeta,
211        #[serde(default, skip_serializing_if = "Option::is_none")]
212        command: Option<String>,
213        #[serde(default, skip_serializing_if = "Option::is_none")]
214        input: Option<Map<String, Value>>,
215        result: String,
216        #[serde(default, skip_serializing_if = "Option::is_none")]
217        running: Option<bool>,
218        #[serde(default, skip_serializing_if = "Option::is_none")]
219        preparing: Option<bool>,
220        #[serde(default, skip_serializing_if = "Option::is_none")]
221        tool: Option<String>,
222        #[serde(default, skip_serializing_if = "Option::is_none", rename = "toolTitle")]
223        tool_title: Option<String>,
224        #[serde(default, skip_serializing_if = "Option::is_none")]
225        session: Option<String>,
226        #[serde(
227            default,
228            skip_serializing_if = "Option::is_none",
229            rename = "durationMs"
230        )]
231        duration_ms: Option<i64>,
232        #[serde(default, skip_serializing_if = "Option::is_none")]
233        outcome: Option<String>,
234    },
235}
236
237impl SessionEntry {
238    /// Shared metadata of this entry, whatever its body.
239    pub fn meta(&self) -> &SessionEntryMeta {
240        match self {
241            SessionEntry::User { meta, .. }
242            | SessionEntry::Assistant { meta, .. }
243            | SessionEntry::Notice { meta, .. }
244            | SessionEntry::Divider { meta, .. }
245            | SessionEntry::Reasoning { meta, .. }
246            | SessionEntry::Compaction { meta, .. }
247            | SessionEntry::Tool { meta, .. } => meta,
248        }
249    }
250
251    /// The mutable shared metadata of this entry, whatever its body.
252    pub fn meta_mut(&mut self) -> &mut SessionEntryMeta {
253        match self {
254            SessionEntry::User { meta, .. }
255            | SessionEntry::Assistant { meta, .. }
256            | SessionEntry::Notice { meta, .. }
257            | SessionEntry::Divider { meta, .. }
258            | SessionEntry::Reasoning { meta, .. }
259            | SessionEntry::Compaction { meta, .. }
260            | SessionEntry::Tool { meta, .. } => meta,
261        }
262    }
263}