Skip to main content

openbutler_protocol/session/
patch.rs

1//! Incremental change frames: the transcript patch, the state snapshot, and
2//! the patch envelope that carries them.
3
4use serde::{Deserialize, Serialize};
5
6use super::entry::SessionEntry;
7use super::run::RunState;
8use super::view::{SessionMeta, SessionView};
9use crate::runtime::RuntimeViewData;
10
11/// One queued message reference inside a delta patch.
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct IndexedEntry {
15    pub index: i64,
16    pub entry: SessionEntry,
17}
18
19/// Which stream a text continuation belongs to. The host labels every
20/// delta with the entry it grows, so a client never infers it from the
21/// index; an older host that predates the field reads as `text`.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
23#[serde(rename_all = "lowercase")]
24pub enum TextDeltaKind {
25    /// The assistant answer.
26    #[default]
27    Text,
28    /// A reasoning/thinking stream: shown only when the host says so.
29    Reasoning,
30}
31
32impl TextDeltaKind {
33    /// Whether this is the ordinary answer stream (the one an older frame
34    /// implies when the field is absent).
35    pub fn is_text(&self) -> bool {
36        matches!(self, Self::Text)
37    }
38}
39
40/// One text continuation inside a delta patch.
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct TextDelta {
44    pub index: i64,
45    pub delta: String,
46    /// Absent on an older host, and omitted for ordinary text so a plain
47    /// frame keeps its old bytes; present as `reasoning` when it is one.
48    #[serde(default, skip_serializing_if = "TextDeltaKind::is_text")]
49    pub kind: TextDeltaKind,
50}
51
52/// Incremental transcript change (TS `SessionEntriesPatch`).
53///
54/// All indices are absolute host indices; the client translates them into its
55/// window, drops anything below it, and trims its front when the window
56/// doubles.
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58#[serde(tag = "kind", rename_all = "lowercase")]
59pub enum SessionEntriesPatch {
60    /// Append new entries and/or rewrite existing ones in place.
61    Delta {
62        append: Vec<SessionEntry>,
63        replace: Vec<IndexedEntry>,
64        #[serde(rename = "appendText")]
65        append_text: Vec<TextDelta>,
66    },
67    /// Replace the whole transcript (session switch, compaction).
68    #[serde(rename = "replace-all")]
69    ReplaceAll { entries: Vec<SessionEntry> },
70}
71
72/// Wire snapshot of a session (TS `SessionSnapshotData`): the record with
73/// entries, the run state, and the window markers.
74///
75/// `entriesFrom`/`totalEntries` are present only when the host sent a tail
76/// window; their absence means *full transcript*, which is what an older
77/// client that never asked for a window always received.
78#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
79#[serde(rename_all = "camelCase")]
80pub struct SessionSnapshotData {
81    pub session: SessionView,
82    #[serde(flatten)]
83    pub run: RunState,
84    /// Absolute host index of `session.entries[0]` for a windowed frame.
85    #[serde(
86        default,
87        skip_serializing_if = "Option::is_none",
88        rename = "entriesFrom"
89    )]
90    pub entries_from: Option<i64>,
91    /// Full transcript length when `entries_from` is present.
92    #[serde(
93        default,
94        skip_serializing_if = "Option::is_none",
95        rename = "totalEntries"
96    )]
97    pub total_entries: Option<i64>,
98}
99
100/// Incremental session state (TS `SessionPatchData`): the record *without*
101/// entries, an optional entries patch, the run state, and the runtime view.
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103#[serde(rename_all = "camelCase")]
104pub struct SessionPatch {
105    pub session: SessionMeta,
106    #[serde(
107        default,
108        skip_serializing_if = "Option::is_none",
109        rename = "entriesPatch"
110    )]
111    pub entries_patch: Option<SessionEntriesPatch>,
112    #[serde(flatten)]
113    pub run: RunState,
114    pub runtime: RuntimeViewData,
115}