Skip to main content

openbutler_protocol/session/
view.rs

1//! Saved-record view: metadata, the full view, the list row, and the
2//! older-entries page.
3
4use serde::{Deserialize, Serialize};
5use serde_json::{Map, Value};
6
7use super::entry::{SessionEntry, SessionProject};
8use crate::runtime::{ReasoningLabel, ReasoningSupport};
9use crate::screen::ScreenLine;
10
11/// Saved-record fields shared by a full view and a patch view: everything
12/// `SavedSession` carries except `entries`, `conversation`, and
13/// `conversationArchive` (the conversation arrays never leave the host).
14///
15/// Used directly as `patch.session`; [`SessionView`] flattens it together with
16/// `entries` for snapshot sessions.
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase")]
19pub struct SessionMeta {
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub project: Option<SessionProject>,
22    /// Saved-record format version (literal `1` in TypeScript).
23    pub version: u32,
24    pub id: String,
25    pub title: String,
26    #[serde(rename = "createdAt")]
27    pub created_at: String,
28    #[serde(rename = "updatedAt")]
29    pub updated_at: String,
30    /// Content-activity stamp used for ordering; absent on records saved
31    /// before the field existed (listing falls back to `updatedAt`).
32    #[serde(
33        default,
34        skip_serializing_if = "Option::is_none",
35        rename = "lastActivityAt"
36    )]
37    pub last_activity_at: Option<String>,
38    pub provider_id: String,
39    pub model_id: String,
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub reasoning: Option<String>,
42    #[serde(
43        default,
44        skip_serializing_if = "Option::is_none",
45        rename = "showReasoning"
46    )]
47    pub show_reasoning: Option<bool>,
48    /// The levels this session's own model offers; keeps the chat's picker
49    /// honest while the shared runtime is bound to another model.
50    #[serde(
51        default,
52        skip_serializing_if = "Option::is_none",
53        rename = "reasoningVariants"
54    )]
55    pub reasoning_variants: Option<Vec<ReasoningLabel>>,
56    #[serde(
57        default,
58        skip_serializing_if = "Option::is_none",
59        rename = "supportsVision"
60    )]
61    pub supports_vision: Option<bool>,
62    #[serde(
63        default,
64        skip_serializing_if = "Option::is_none",
65        rename = "reasoningSupport"
66    )]
67    pub reasoning_support: Option<ReasoningSupport>,
68    /// Optional and nullable as in TypeScript; readers treat `null` and an
69    /// absent key alike (see docs/ipc-protocol.md, open questions).
70    #[serde(
71        default,
72        skip_serializing_if = "Option::is_none",
73        rename = "reasoningMetadataSource"
74    )]
75    pub reasoning_metadata_source: Option<String>,
76    #[serde(
77        default,
78        skip_serializing_if = "Option::is_none",
79        rename = "reasoningTitles"
80    )]
81    pub reasoning_titles: Option<bool>,
82    #[serde(
83        default,
84        skip_serializing_if = "Option::is_none",
85        rename = "workspaceRoot"
86    )]
87    pub workspace_root: Option<String>,
88    #[serde(
89        default,
90        skip_serializing_if = "Option::is_none",
91        rename = "contextTokens"
92    )]
93    pub context_tokens: Option<i64>,
94    #[serde(
95        default,
96        skip_serializing_if = "Option::is_none",
97        rename = "contextEstimateTokens"
98    )]
99    pub context_estimate_tokens: Option<i64>,
100    #[serde(
101        default,
102        skip_serializing_if = "Option::is_none",
103        rename = "contextWindowTokens"
104    )]
105    pub context_window_tokens: Option<i64>,
106    /// Opaque provider/key-pool ids mapped to the last selected key position.
107    #[serde(
108        default,
109        skip_serializing_if = "Option::is_none",
110        rename = "keyPositions"
111    )]
112    pub key_positions: Option<Map<String, Value>>,
113    #[serde(
114        default,
115        skip_serializing_if = "Option::is_none",
116        rename = "hasUserMessage"
117    )]
118    pub has_user_message: Option<bool>,
119    /// Owner stop/Esc policy survives host restarts.
120    #[serde(
121        default,
122        skip_serializing_if = "Option::is_none",
123        rename = "automaticWakeHeld"
124    )]
125    pub automatic_wake_held: Option<bool>,
126    #[serde(default, skip_serializing_if = "Option::is_none")]
127    pub preview: Option<String>,
128    #[serde(
129        default,
130        skip_serializing_if = "Option::is_none",
131        rename = "firstUserMessage"
132    )]
133    pub first_user_message: Option<String>,
134    #[serde(
135        default,
136        skip_serializing_if = "Option::is_none",
137        rename = "lastAssistantMessage"
138    )]
139    pub last_assistant_message: Option<String>,
140}
141
142/// A session record with its transcript — `patch` and window math address the
143/// entries by absolute host index.
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub struct SessionView {
146    #[serde(flatten)]
147    pub meta: SessionMeta,
148    pub entries: Vec<SessionEntry>,
149}
150
151/// Saved-session list row (`SessionSummary`): denormalised so the picker
152/// never parses `entries`.
153#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
154#[serde(rename_all = "camelCase")]
155pub struct SessionSummary {
156    #[serde(default, skip_serializing_if = "Option::is_none")]
157    pub running: Option<bool>,
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    pub project: Option<SessionProject>,
160    pub id: String,
161    pub title: String,
162    pub preview: String,
163    #[serde(rename = "updatedAt")]
164    pub updated_at: String,
165    #[serde(
166        default,
167        skip_serializing_if = "Option::is_none",
168        rename = "lastActivityAt"
169    )]
170    pub last_activity_at: Option<String>,
171    pub provider_id: String,
172    pub model_id: String,
173    #[serde(
174        default,
175        skip_serializing_if = "Option::is_none",
176        rename = "workspaceRoot"
177    )]
178    pub workspace_root: Option<String>,
179}
180
181/// A page of older transcript addressed by absolute index
182/// (`session.entries-range`).
183#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
184#[serde(rename_all = "camelCase")]
185pub struct SessionEntriesRange {
186    pub from: i64,
187    pub to: i64,
188    pub total: i64,
189    pub entries: Vec<SessionEntry>,
190}
191
192/// A saved record with a transcript tail window (`session.load`).
193///
194/// `entriesFrom`/`totalEntries` are present only when the caller asked
195/// for a window; their absence means the full transcript — the shape an
196/// older client always received. A record that outgrows the frame cap
197/// can therefore still be opened: the tail travels in the load answer and
198/// older pages come from `session.entries-range`.
199#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
200#[serde(rename_all = "camelCase")]
201pub struct SessionRecordWindow {
202    #[serde(flatten)]
203    pub session: SessionView,
204    /// Absolute host index of `session.entries[0]` for a windowed answer.
205    #[serde(
206        default,
207        skip_serializing_if = "Option::is_none",
208        rename = "entriesFrom"
209    )]
210    pub entries_from: Option<i64>,
211    /// Full transcript length when `entries_from` is present.
212    #[serde(
213        default,
214        skip_serializing_if = "Option::is_none",
215        rename = "totalEntries"
216    )]
217    pub total_entries: Option<i64>,
218}
219
220/// Answer to `session.list`: the picker rows plus the screen the host
221/// composed. A client prints `lines`; `sessions` stays for programmatic
222/// consumers that want the data rather than the screen.
223#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
224#[serde(rename_all = "camelCase")]
225pub struct SessionListAnswer {
226    pub sessions: Vec<SessionSummary>,
227    pub lines: Vec<ScreenLine>,
228}