Skip to main content

scv_protocol/
lib.rs

1//! Dependency-light wire types shared by SCV clients and the server.
2//!
3//! [`ClientMessage`] is everything a client sends and [`ServerEvent`]
4//! everything the server answers, one JSON object per line ([`FrameDecoder`]
5//! bounds each line). Additive fields, and new values of the enums that
6//! parse unknown values as `Unknown` ([`ErrorCode`], [`ToolErrorKind`],
7//! [`ServerEvent`]), keep [`PROTOCOL_VERSION`]; a breaking change bumps it.
8//! This crate holds no runtime policy and does no I/O.
9
10#![forbid(unsafe_code)]
11#![warn(missing_docs)]
12
13mod attachment;
14mod background;
15mod client;
16mod daemon;
17mod error;
18mod frame;
19mod server;
20
21use serde::{Deserialize, Serialize};
22
23pub use attachment::{Attachment, ReplyAttachment, reply_attachment};
24pub use background::{JobChange, JobStatus, OriginKind, TurnOrigin};
25pub use client::ClientMessage;
26pub use daemon::{
27    ComponentHealth, ComponentState, ConfirmInfo, ConfirmState, DEFAULT_CONFIRM_SECONDS,
28    DaemonCommand, DaemonStatus, DelegationInfo, DelegationSummary, MAX_CONFIRM_SECONDS,
29    RemoteTools, RestartInfo, Senders,
30};
31pub use error::{ErrorCode, ToolErrorKind};
32pub use frame::{Frame, FrameDecoder, Overflow, Step, encode_frame, trim_line};
33pub use server::ServerEvent;
34
35/// The protocol version. Client and server must speak the same one.
36pub const PROTOCOL_VERSION: u32 = 3;
37
38/// The longest `session.start` channel name.
39pub const MAX_CHANNEL_NAME_BYTES: usize = 32;
40/// Files one `turn.start` may attach.
41pub const MAX_TURN_ATTACHMENTS: usize = 16;
42/// The tool a chat session's model calls to send a file with its reply.
43pub const CHAT_ATTACH_TOOL: &str = "chat_attach";
44
45/// A prompt waiting for the running turn to finish.
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
47pub struct QueueEntry {
48    /// Stable ID of this queued prompt.
49    pub queue_id: String,
50    /// Bumped on every change; edits must name the current revision.
51    pub revision: u64,
52    /// The prompt text.
53    pub prompt: String,
54    /// The client that queued it.
55    pub submitter: String,
56    /// Files the queued `turn.start` attached; they run with its prompt.
57    #[serde(default, skip_serializing_if = "Vec::is_empty")]
58    pub attachments: Vec<Attachment>,
59}
60
61/// A client's or server's name and version, exchanged at `initialize`.
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
63pub struct PeerInfo {
64    /// Program name, such as `scv-tui`.
65    pub name: String,
66    /// Its release.
67    pub version: String,
68}
69
70/// Tokens a turn used, as the provider reported them.
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
72pub struct Usage {
73    /// Prompt tokens, when reported.
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub input_tokens: Option<u64>,
76    /// Answer tokens, when reported.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub output_tokens: Option<u64>,
79}
80
81#[cfg(test)]
82mod tests;