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, job_agent};
25pub use client::{ChatLog, ClientMessage};
26pub use daemon::{
27    ComponentHealth, ComponentState, ConfirmInfo, ConfirmState, DEFAULT_CONFIRM_SECONDS,
28    DaemonCommand, DaemonStatus, DelegationInfo, DelegationSummary, MAX_CONFIRM_SECONDS,
29    MailCounts, Purpose, 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/// Longest `session.start` `system_prompt`, in bytes.
41pub const MAX_SYSTEM_PROMPT_BYTES: usize = 16 * 1024;
42/// Files one `turn.start` may attach.
43pub const MAX_TURN_ATTACHMENTS: usize = 16;
44/// The tool a chat session's model calls to send a file with its reply.
45pub const CHAT_ATTACH_TOOL: &str = "chat_attach";
46
47/// A prompt waiting for the running turn to finish.
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
49pub struct QueueEntry {
50    /// Stable ID of this queued prompt.
51    pub queue_id: String,
52    /// Bumped on every change; edits must name the current revision.
53    pub revision: u64,
54    /// The prompt text.
55    pub prompt: String,
56    /// The client that queued it.
57    pub submitter: String,
58    /// Files the queued `turn.start` attached; they run with its prompt.
59    #[serde(default, skip_serializing_if = "Vec::is_empty")]
60    pub attachments: Vec<Attachment>,
61}
62
63/// A client's or server's name and version, exchanged at `initialize`.
64#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
65pub struct PeerInfo {
66    /// Program name, such as `scv-tui`.
67    pub name: String,
68    /// Its release.
69    pub version: String,
70}
71
72/// Tokens a turn used, as the provider reported them.
73#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
74pub struct Usage {
75    /// Prompt tokens, when reported.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub input_tokens: Option<u64>,
78    /// Answer tokens, when reported.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub output_tokens: Option<u64>,
81}
82
83#[cfg(test)]
84mod tests;