yapi-types 0.1.0

Serde types for pi's JSON formats: messages, events, session entries, settings, models and extension payloads.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! RPC mode: commands read from stdin and the responses written to stdout, as
//! in `modes/rpc/rpc-types.ts` of pi `v1.0.0`. Events use the JSON mode shapes
//! in [`crate::event`].
#![allow(
    missing_docs,
    reason = "fields mirror pi's TypeScript types; contracts are noted where they differ from the name"
)]

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::message::{ImageContent, ThinkingLevel};
use crate::session::FileEntry;
use crate::settings::QueueMode;

/// How a prompt sent while the agent runs is queued.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum StreamingBehavior {
    /// Delivered after the current tool calls.
    Steer,
    /// Delivered when the run would otherwise end.
    FollowUp,
}

/// What became of a prompt once it passed preflight.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PromptDisposition {
    /// A run started.
    Started,
    /// Queued behind the running agent.
    Queued,
    /// Consumed without a run, such as by an extension command.
    Handled,
}

/// A command line on stdin, without its `id`.
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(
    tag = "type",
    rename_all = "snake_case",
    rename_all_fields = "camelCase"
)]
pub enum RpcCommand {
    Prompt {
        message: String,
        #[serde(default)]
        images: Vec<ImageContent>,
        #[serde(default)]
        streaming_behavior: Option<StreamingBehavior>,
    },
    Steer {
        message: String,
        #[serde(default)]
        images: Vec<ImageContent>,
    },
    FollowUp {
        message: String,
        #[serde(default)]
        images: Vec<ImageContent>,
    },
    Abort,
    ClearQueue,
    NewSession {
        #[serde(default)]
        parent_session: Option<String>,
    },
    GetState,
    SetModel {
        provider: String,
        model_id: String,
    },
    CycleModel,
    GetAvailableModels,
    SetThinkingLevel {
        level: ThinkingLevel,
    },
    CycleThinkingLevel,
    GetAvailableThinkingLevels,
    SetSteeringMode {
        mode: QueueMode,
    },
    SetFollowUpMode {
        mode: QueueMode,
    },
    Compact {
        #[serde(default)]
        custom_instructions: Option<String>,
    },
    SetAutoCompaction {
        enabled: bool,
    },
    SetAutoRetry {
        enabled: bool,
    },
    AbortRetry,
    Bash {
        command: String,
        #[serde(default)]
        exclude_from_context: Option<bool>,
    },
    AbortBash,
    GetSessionStats,
    ExportHtml {
        #[serde(default)]
        output_path: Option<String>,
    },
    SwitchSession {
        session_path: String,
    },
    Fork {
        entry_id: String,
    },
    Clone,
    GetForkMessages,
    GetEntries {
        #[serde(default)]
        since: Option<String>,
    },
    GetTree,
    GetLastAssistantText,
    SetSessionName {
        name: String,
    },
    GetMessages,
    GetCommands,
    /// Any other `type`.
    #[serde(other)]
    Unknown,
}

/// A response line: `data` is serialized JSON, or absent. A line without a
/// `type` gets a response without `command`, as `JSON.stringify` drops pi's
/// `undefined`.
pub fn response_line(
    id: Option<&Value>,
    command: Option<&str>,
    outcome: Result<Option<&str>, &str>,
) -> String {
    let mut line = String::from("{");
    if let Some(id) = id
        && let Ok(id) = crate::json::to_string(id)
    {
        line.push_str("\"id\":");
        line.push_str(&id);
        line.push(',');
    }
    line.push_str("\"type\":\"response\"");
    if let Some(command) = command {
        line.push_str(",\"command\":");
        line.push_str(&crate::json::to_string(command).unwrap_or_else(|_| "\"\"".into()));
    }
    match outcome {
        Ok(data) => {
            line.push_str(",\"success\":true");
            if let Some(data) = data {
                line.push_str(",\"data\":");
                line.push_str(data);
            }
        }
        Err(error) => {
            line.push_str(",\"success\":false,\"error\":");
            line.push_str(&crate::json::to_string(error).unwrap_or_else(|_| "\"\"".into()));
        }
    }
    line.push('}');
    line
}

/// `get_state` data.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionState {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<Value>,
    pub thinking_level: ThinkingLevel,
    pub is_streaming: bool,
    pub is_compacting: bool,
    pub steering_mode: QueueMode,
    pub follow_up_mode: QueueMode,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_file: Option<String>,
    pub session_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_name: Option<String>,
    pub auto_compaction_enabled: bool,
    pub message_count: usize,
    pub pending_message_count: usize,
}

/// Token totals in [`SessionStats`].
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenTotals {
    pub input: u64,
    pub output: u64,
    pub cache_read: u64,
    pub cache_write: u64,
    pub total: u64,
}

/// How full the context window is; `tokens` and `percent` are `null` while
/// unknown.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContextUsage {
    pub tokens: Option<u64>,
    pub context_window: u64,
    pub percent: Option<f64>,
}

/// `get_session_stats` data.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionStats {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_file: Option<String>,
    pub session_id: String,
    pub user_messages: usize,
    pub assistant_messages: usize,
    pub tool_calls: usize,
    pub tool_results: usize,
    pub total_messages: usize,
    pub tokens: TokenTotals,
    pub cost: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_usage: Option<ContextUsage>,
}

/// What a user `!` command produced: the `bash` response data.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BashResult {
    /// Combined, sanitized output; the tail when truncated.
    pub output: String,
    /// The exit code; absent when cancelled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exit_code: Option<i32>,
    /// The command was cancelled.
    pub cancelled: bool,
    /// The output was truncated.
    pub truncated: bool,
    /// The full output, when it was too long to keep.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub full_output_path: Option<String>,
}

/// A fork point in `get_fork_messages` data.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ForkMessage {
    pub entry_id: String,
    pub text: String,
}

/// Where a resource was found, as pi's `SourceInfo`: `source` is `auto` for
/// discovered files, `cli` for command-line paths and the package for
/// package resources; `scope` is `user`, `project` or `temporary`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceInfo {
    pub path: String,
    pub source: String,
    pub scope: String,
    pub origin: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base_dir: Option<String>,
}

/// Where a [`SlashCommand`] comes from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CommandSource {
    Extension,
    Prompt,
    Skill,
}

/// A command a prompt can invoke, in `get_commands` data.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SlashCommand {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub source: CommandSource,
    pub source_info: SourceInfo,
}

/// One node of `get_tree` data, as pi's `SessionTreeNode`.
pub struct TreeNode<'a> {
    pub entry: &'a FileEntry,
    /// Indexes into the same node list.
    pub children: Vec<usize>,
    pub label: Option<&'a str>,
    pub label_timestamp: Option<&'a str>,
}

/// Serializes the trees under `roots` without recursion, so a long linear
/// session cannot exhaust the stack.
pub fn tree_json(nodes: &[TreeNode<'_>], roots: &[usize]) -> serde_json::Result<String> {
    enum Step {
        Open(usize),
        Close(usize),
        Comma,
    }
    let mut out = String::from("[");
    let mut stack: Vec<Step> = Vec::new();
    let push_list = |stack: &mut Vec<Step>, list: &[usize]| {
        for (index, &node) in list.iter().enumerate().rev() {
            stack.push(Step::Open(node));
            if index > 0 {
                stack.push(Step::Comma);
            }
        }
    };
    push_list(&mut stack, roots);
    while let Some(step) = stack.pop() {
        match step {
            Step::Comma => out.push(','),
            Step::Open(index) => {
                let node = &nodes[index];
                out.push_str("{\"entry\":");
                out.push_str(&crate::json::to_string(node.entry)?);
                out.push_str(",\"children\":[");
                stack.push(Step::Close(index));
                push_list(&mut stack, &node.children);
            }
            Step::Close(index) => {
                out.push(']');
                let node = &nodes[index];
                if let Some(label) = node.label {
                    out.push_str(",\"label\":");
                    out.push_str(&crate::json::to_string(label)?);
                }
                if let Some(timestamp) = node.label_timestamp {
                    out.push_str(",\"labelTimestamp\":");
                    out.push_str(&crate::json::to_string(timestamp)?);
                }
                out.push('}');
            }
        }
    }
    out.push(']');
    Ok(out)
}

/// An `extension_ui_response` line on stdin.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ExtensionUiResponse {
    pub id: String,
    #[serde(default)]
    pub value: Option<String>,
    #[serde(default)]
    pub confirmed: Option<bool>,
    #[serde(default)]
    pub cancelled: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn commands_parse_like_pi() {
        let prompt: RpcCommand = serde_json::from_str(
            r#"{"type":"prompt","message":"hi","streamingBehavior":"followUp"}"#,
        )
        .unwrap();
        assert_eq!(
            prompt,
            RpcCommand::Prompt {
                message: "hi".into(),
                images: Vec::new(),
                streaming_behavior: Some(StreamingBehavior::FollowUp),
            }
        );
        let mode: RpcCommand =
            serde_json::from_str(r#"{"type":"set_steering_mode","mode":"one-at-a-time"}"#).unwrap();
        assert_eq!(
            mode,
            RpcCommand::SetSteeringMode {
                mode: QueueMode::OneAtATime
            }
        );
        let unknown: RpcCommand = serde_json::from_str(r#"{"type":"dance"}"#).unwrap();
        assert_eq!(unknown, RpcCommand::Unknown);
    }

    #[test]
    fn responses_match_pi() {
        let id = Value::from("7");
        assert_eq!(
            response_line(Some(&id), Some("abort"), Ok(None)),
            r#"{"id":"7","type":"response","command":"abort","success":true}"#
        );
        assert_eq!(
            response_line(None, Some("cycle_model"), Ok(Some("null"))),
            r#"{"type":"response","command":"cycle_model","success":true,"data":null}"#
        );
        assert_eq!(
            response_line(Some(&Value::from(3)), Some("x"), Err("Unknown command: x")),
            r#"{"id":3,"type":"response","command":"x","success":false,"error":"Unknown command: x"}"#
        );
    }

    #[test]
    fn deep_trees_serialize_without_recursion() {
        let entry: FileEntry = serde_json::from_str(
            r#"{"type":"label","id":"a","parentId":null,"timestamp":"t","targetId":"b"}"#,
        )
        .unwrap();
        let depth = 200_000;
        let nodes: Vec<TreeNode<'_>> = (0..depth)
            .map(|index| TreeNode {
                entry: &entry,
                children: if index + 1 < depth {
                    vec![index + 1]
                } else {
                    Vec::new()
                },
                label: (index == 0).then_some("start"),
                label_timestamp: None,
            })
            .collect();
        let json = tree_json(&nodes, &[0]).unwrap();
        assert!(json.ends_with(&format!(
            "{}],\"label\":\"start\"}}]",
            "]}".repeat(depth - 1)
        )));
        let small = tree_json(&nodes[depth - 2..depth - 1], &[]).unwrap();
        assert_eq!(small, "[]");
    }
}