Skip to main content

vtcode_desktop_protocol/
turn.rs

1//! Turn submission, acceptance, cancellation, and queue DTOs.
2
3use serde::{Deserialize, Serialize};
4
5/// A turn was rejected before delivery because a startup interaction is pending.
6pub const STARTUP_INTERACTION_PENDING_ERROR: i32 = -32001;
7
8/// Fallback turn id for interactions raised with no active turn (startup
9/// decisions). Produced by the service, matched by the desktop client.
10pub const SESSION_START_TURN_ID: &str = "session-start";
11
12/// Maximum queued-message previews carried by one queue-status notification.
13/// Mirrors the CLI sidebar, which renders the first few queued inputs.
14pub const MAX_QUEUE_STATUS_PREVIEWS: usize = 5;
15
16/// Maximum characters per queued-message preview before truncation.
17/// Mirrors the CLI sidebar preview width.
18pub const MAX_QUEUE_PREVIEW_CHARS: usize = 50;
19
20/// Bounded identity token for one queue entry, minted by the service so the
21/// client can address one message for take-latest and remove without relying
22/// on positional order (which races against concurrent submits).
23pub const MAX_QUEUE_ENTRY_ID_LEN: usize = 64;
24
25/// Build the display preview for one queued message: the full text when it
26/// fits, otherwise the first [`MAX_QUEUE_PREVIEW_CHARS`] characters plus an
27/// ellipsis. Truncation is char-boundary safe; newlines are preserved for the
28/// client to render.
29#[must_use]
30pub fn queue_preview_label(text: &str) -> String {
31    let mut preview = String::new();
32    for (index, ch) in text.chars().enumerate() {
33        if index >= MAX_QUEUE_PREVIEW_CHARS {
34            preview.push('\u{2026}');
35            return preview;
36        }
37        preview.push(ch);
38    }
39    preview
40}
41
42/// Parameters for submitting a prompt to the selected session.
43#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
44#[serde(rename_all = "camelCase")]
45pub struct TurnSubmitParams {
46    /// Session that owns the new turn.
47    pub session_id: String,
48    /// User prompt.
49    pub prompt: String,
50    /// Inline text attachments read by the client, applied as content parts.
51    #[serde(default)]
52    pub attachments: Vec<TurnAttachment>,
53    /// Whether a busy-time submission may merge with adjacent queued text-only
54    /// submissions into one turn. Mirrors the CLI Ctrl+Enter batchable flag;
55    /// submissions carrying attachments always dispatch alone.
56    #[serde(default)]
57    pub batchable: bool,
58}
59
60/// One client-read text attachment for a submitted turn.
61#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
62#[serde(rename_all = "camelCase")]
63pub struct TurnAttachment {
64    /// Display path used for labeling: workspace-relative for workspace
65    /// files, absolute for files attached from anywhere else.
66    pub path: String,
67    /// Bounded file content.
68    pub text: String,
69}
70
71/// Accepted turn response.
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "camelCase")]
74pub struct TurnAccepted {
75    /// Session identifier.
76    pub session_id: String,
77    /// Runtime turn identifier. Empty when `queued` is set: a queued prompt
78    /// owns no turn until its batch dispatches.
79    pub turn_id: String,
80    /// Whether the prompt was queued behind a running turn instead of
81    /// starting immediately. Absent (legacy services) means immediate.
82    #[serde(default)]
83    pub queued: bool,
84    /// Queue depth after this acceptance.
85    #[serde(default)]
86    pub queue_depth: usize,
87    /// Service identity of the accepted queued draft.
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub entry_id: Option<String>,
90    /// Oldest messages discarded by the queue cap while accepting this one.
91    #[serde(default)]
92    pub dropped: usize,
93    /// Queue entries evicted while accepting this prompt.
94    #[serde(default, skip_serializing_if = "Vec::is_empty")]
95    pub discarded_entry_ids: Vec<String>,
96}
97
98/// Parameters for cancelling an active turn.
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101pub struct TurnCancelParams {
102    /// Owning session.
103    pub session_id: String,
104    /// Exact turn identity to cancel.
105    pub turn_id: String,
106    /// Discard queued turns behind the cancelled one (running-switch flow:
107    /// the terminal pump auto-dispatches the queue, so a deferred session
108    /// switch can only land on an empty queue). Absent (legacy clients)
109    /// means keep the queue: a plain stop lets queued prompts run
110    /// afterwards (TUI parity).
111    #[serde(default)]
112    pub clear_queue: bool,
113    /// Skip the cooperative-cancellation grace period and start the
114    /// teardown escalation immediately. The desktop client sets this on a
115    /// repeat Stop press while a cancel is already outstanding; the first
116    /// press stays cooperative so a healthy turn stops without a runtime
117    /// restart. Absent (legacy clients) means cooperative with the
118    /// standard grace backstop.
119    #[serde(default)]
120    pub force: bool,
121}
122
123/// Parameters for reversing one turn's exact file changes.
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126pub struct TurnUndoParams {
127    /// Session that owns the turn.
128    pub session_id: String,
129    /// Canonical turn identity (event `context.turn_id`).
130    pub turn_id: String,
131}
132
133/// One reversed file change.
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase")]
136pub struct TurnUndoFile {
137    /// Workspace-relative path.
138    pub path: String,
139    /// Kind of the reversed change.
140    pub kind: vtcode_exec_events::PatchChangeKind,
141    /// Reversed added-line count.
142    #[serde(default)]
143    pub additions: u64,
144    /// Reversed deleted-line count.
145    #[serde(default)]
146    pub deletions: u64,
147}
148
149/// Outcome of a turn undo request.
150#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
151#[serde(rename_all = "camelCase")]
152pub struct TurnUndoResult {
153    /// Session that owns the turn.
154    pub session_id: String,
155    /// Canonical turn identity.
156    pub turn_id: String,
157    /// Whether every recorded change was reversed. `false` carries the
158    /// refusal reason; nothing is partially reversed without disclosure.
159    pub undone: bool,
160    /// Reversed changes in recorded order: empty on clean refusals, holding
161    /// the applied prefix when a later file aborts the flush.
162    #[serde(default)]
163    pub files: Vec<TurnUndoFile>,
164    /// Refusal or failure explanation; empty on success.
165    #[serde(default)]
166    pub reason: String,
167}
168
169/// Parameters for popping the newest queued message back for editing.
170#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
171#[serde(rename_all = "camelCase")]
172pub struct TurnQueueTakeLatestParams {
173    /// Session that owns the queue.
174    pub session_id: String,
175}
176
177/// Result of popping the newest queued message.
178#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(rename_all = "camelCase")]
180pub struct TurnQueueTakeLatestResult {
181    /// Session that owns the queue.
182    pub session_id: String,
183    /// The entry restored into the composer.
184    #[serde(default, skip_serializing_if = "Option::is_none")]
185    pub entry_id: Option<String>,
186    /// Popped message text, or `None` when the queue was empty.
187    #[serde(default)]
188    pub text: Option<String>,
189    /// Queue depth after the pop.
190    #[serde(default)]
191    pub queue_depth: usize,
192}
193
194/// Authoritative queued-message snapshot published by the service.
195#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
196#[serde(rename_all = "camelCase")]
197pub struct TurnQueueStatus {
198    /// Session that owns the queue.
199    pub session_id: String,
200    /// Number of messages waiting for dispatch.
201    pub queue_depth: usize,
202    /// Display previews for the oldest queued messages, oldest first, up to
203    /// [`MAX_QUEUE_STATUS_PREVIEWS`].
204    #[serde(default)]
205    pub previews: Vec<String>,
206    /// Entry identity tokens matching `previews` one-to-one (when the service
207    /// supplies them). Absent or shorter (legacy services) means the client
208    /// falls back to newest-first take-latest and hides per-row actions.
209    #[serde(default)]
210    pub entry_ids: Vec<String>,
211    /// Entries delivered to the runtime by this update, in submission order.
212    /// Absent on older services. An empty list excludes dispatch as a cause.
213    #[serde(default, skip_serializing_if = "Option::is_none")]
214    pub dispatched_entry_ids: Option<Vec<String>>,
215    /// Entries explicitly discarded by the queue cap or a clear request.
216    #[serde(default, skip_serializing_if = "Vec::is_empty")]
217    pub discarded_entry_ids: Vec<String>,
218    /// Seconds since each entry was queued, oldest first, matching `previews`
219    /// one-to-one. Absent (legacy services) hides the elapsed-time column.
220    #[serde(default)]
221    pub queued_for_secs: Vec<u64>,
222    /// Total oldest messages discarded by the queue cap so far.
223    #[serde(default)]
224    pub dropped_total: usize,
225}
226
227/// Parameters for removing one queued message by entry id.
228#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
229#[serde(rename_all = "camelCase")]
230pub struct TurnQueueRemoveParams {
231    /// Session that owns the queue.
232    pub session_id: String,
233    /// Entry identity token from the latest `turn/queue-status` snapshot.
234    pub entry_id: String,
235}
236
237/// Outcome of a remove-one-queued-message request.
238#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
239#[serde(rename_all = "camelCase")]
240pub struct TurnQueueRemoveResult {
241    /// Session that owns the queue.
242    pub session_id: String,
243    /// The queue entry addressed by the request.
244    #[serde(default, skip_serializing_if = "Option::is_none")]
245    pub entry_id: Option<String>,
246    /// Whether the entry id matched a queued message and it was removed.
247    pub removed: bool,
248    /// Queue depth after the removal.
249    pub queue_depth: usize,
250}
251
252/// Parameters for editing one queued message's prompt in place. The entry id
253/// comes from the latest `turn/queue-status` snapshot; the FIFO position and
254/// id are preserved so in-flight references stay valid.
255#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
256#[serde(rename_all = "camelCase")]
257pub struct TurnQueueEditParams {
258    /// Session that owns the queue.
259    pub session_id: String,
260    /// Entry identity token from the latest `turn/queue-status` snapshot.
261    pub entry_id: String,
262    /// Replacement prompt text.
263    pub text: String,
264}
265
266/// Outcome of editing one queued message.
267#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
268#[serde(rename_all = "camelCase")]
269pub struct TurnQueueEditResult {
270    /// Session that owns the queue.
271    pub session_id: String,
272    /// Whether the entry id matched a queued message and it was edited.
273    pub edited: bool,
274    /// Queue depth after the edit (unchanged).
275    #[serde(default)]
276    pub queue_depth: usize,
277}
278
279/// Parameters for steering the live turn with a prompt. When the prompt comes
280/// from a queued row, `entry_id` asks the service to remove that entry in the
281/// same request so it is never dispatched a second time.
282#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
283#[serde(rename_all = "camelCase")]
284pub struct TurnSteerParams {
285    /// Session that owns the running turn.
286    pub session_id: String,
287    /// The prompt to inject as `SteeringMessage::FollowUpInput`.
288    pub text: String,
289    /// Queue entry to drop before steering, when steering a queued row.
290    #[serde(default)]
291    pub entry_id: Option<String>,
292}
293
294/// Outcome of a steer handoff. `steered: true` means the prompt was accepted
295/// by the live steering channel; the runtime's terminal decision arrives later
296/// in a `turn/steer/result` notification while `pending` is set.
297#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
298#[serde(rename_all = "camelCase")]
299pub struct TurnSteerResult {
300    /// Session that owns the running turn.
301    pub session_id: String,
302    /// Whether the prompt reached the live steering channel.
303    pub steered: bool,
304    /// Queue depth after dropping `entry_id`, when one was supplied.
305    #[serde(default)]
306    pub queue_depth: usize,
307    /// Whether the runtime acknowledgment is still outstanding. The client
308    /// keeps the addressed row until the result notification arrives.
309    #[serde(default)]
310    pub pending: bool,
311    /// Correlation token for the pending acknowledgment.
312    #[serde(default)]
313    pub receipt_id: String,
314}
315
316/// Why the runtime rejected a live steer.
317#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
318#[serde(rename_all = "camelCase")]
319pub enum TurnSteerRejection {
320    /// The turn's admission window closed before the steer was admitted.
321    TurnClosing,
322    /// Cancellation took precedence.
323    CancellationPrecedence,
324    /// The runtime follow-up queue was full.
325    QueueFull,
326    /// There was no live turn.
327    NoLiveTurn,
328    /// The session, turn, or queue entry was superseded.
329    Superseded,
330}
331
332/// Terminal runtime acknowledgment for a `turn/steer` handoff. The addressed
333/// queue row stays owned by the client until this notification arrives.
334#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
335#[serde(rename_all = "camelCase")]
336pub struct TurnSteerResultNotification {
337    /// Session that owns the turn.
338    pub session_id: String,
339    /// Turn that was steered.
340    pub turn_id: String,
341    /// Receipt token echoed from `TurnSteerResult`.
342    pub receipt_id: String,
343    /// Queue entry the steer addressed, when any.
344    #[serde(default)]
345    pub entry_id: String,
346    /// Whether the runtime admitted the steer. `false` leaves the row queued.
347    pub accepted: bool,
348    /// Rejection reason when `accepted` is false.
349    #[serde(default)]
350    pub reason: Option<TurnSteerRejection>,
351    /// Stable identity of the admitted follow-up intent.
352    #[serde(default)]
353    pub intent_id: String,
354    /// Queue depth after the row was removed (accept) or left in place.
355    #[serde(default)]
356    pub queue_depth: usize,
357}