vtcode-core 0.104.1

Core library for VT Code - a Rust-based terminal coding agent
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Semantic streaming events for Open Responses.
//!
//! Streaming is modeled as a series of semantic events, not raw text deltas.
//! Events describe meaningful transitions like state changes or content deltas.

use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};

use super::{ContentPart, OutputItem, Response, ResponseId};

/// Semantic streaming events per the Open Responses specification.
///
/// These events describe meaningful transitions during response generation,
/// enabling predictable, provider-agnostic streaming clients.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ResponseStreamEvent {
    // ============================================================
    // Response lifecycle events
    // ============================================================
    /// Initial response creation event.
    #[serde(rename = "response.created")]
    ResponseCreated {
        /// The response object with initial state.
        response: Response,
    },

    /// Response has started processing.
    #[serde(rename = "response.in_progress")]
    ResponseInProgress {
        /// The response object.
        response: Response,
    },

    /// Response completed successfully.
    #[serde(rename = "response.completed")]
    ResponseCompleted {
        /// The final response object.
        response: Response,
    },

    /// Response failed with an error.
    #[serde(rename = "response.failed")]
    ResponseFailed {
        /// The response object with error details.
        response: Response,
    },

    /// Response is incomplete (e.g., token limit reached).
    #[serde(rename = "response.incomplete")]
    ResponseIncomplete {
        /// The response object with incomplete details.
        response: Response,
    },

    // ============================================================
    // Output item events
    // ============================================================
    /// New output item added to the response.
    #[serde(rename = "response.output_item.added")]
    OutputItemAdded {
        /// ID of the containing response.
        response_id: ResponseId,
        /// Index of the item in the output array.
        output_index: usize,
        /// The output item being added.
        item: OutputItem,
    },

    /// Output item is complete.
    #[serde(rename = "response.output_item.done")]
    OutputItemDone {
        /// ID of the containing response.
        response_id: ResponseId,
        /// Index of the item in the output array.
        output_index: usize,
        /// The completed output item.
        item: OutputItem,
    },

    // ============================================================
    // Content part events
    // ============================================================
    /// New content part added to an output item.
    #[serde(rename = "response.content_part.added")]
    ContentPartAdded {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the containing output item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// Index of the content part within the item.
        content_index: usize,
        /// The content part being added.
        part: ContentPart,
    },

    /// Content part is complete.
    #[serde(rename = "response.content_part.done")]
    ContentPartDone {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the containing output item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// Index of the content part within the item.
        content_index: usize,
        /// The completed content part.
        part: ContentPart,
    },

    // ============================================================
    // Text streaming events
    // ============================================================
    /// Text content delta for incremental streaming.
    #[serde(rename = "response.output_text.delta")]
    OutputTextDelta {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the containing output item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// Index of the content part within the item.
        content_index: usize,
        /// The text delta to append.
        delta: String,
    },

    /// Text content is complete.
    #[serde(rename = "response.output_text.done")]
    OutputTextDone {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the containing output item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// Index of the content part within the item.
        content_index: usize,
        /// The complete text content.
        text: String,
    },

    // ============================================================
    // Function call streaming events
    // ============================================================
    /// Function call arguments delta.
    #[serde(rename = "response.function_call_arguments.delta")]
    FunctionCallArgumentsDelta {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the function call item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// The arguments delta to append.
        delta: String,
    },

    /// Function call arguments are complete.
    #[serde(rename = "response.function_call_arguments.done")]
    FunctionCallArgumentsDone {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the function call item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// The complete arguments JSON string.
        arguments: String,
    },

    // ============================================================
    // Reasoning events
    // ============================================================
    /// Reasoning content delta.
    #[serde(rename = "response.reasoning.delta")]
    ReasoningDelta {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the reasoning item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// The reasoning delta to append.
        delta: String,
    },

    /// Reasoning content is complete.
    #[serde(rename = "response.reasoning.done")]
    ReasoningDone {
        /// ID of the containing response.
        response_id: ResponseId,
        /// ID of the reasoning item.
        item_id: String,
        /// Index of the item in the output array.
        output_index: usize,
        /// The reasoning item with complete content.
        item: OutputItem,
    },

    // ============================================================
    // Extension events
    // ============================================================
    /// Custom/extension streaming event.
    ///
    /// Custom event types must be prefixed with the implementor slug
    /// (e.g., `vtcode.trace_event`).
    #[serde(rename = "response.custom_event")]
    CustomEvent {
        /// ID of the containing response.
        response_id: ResponseId,
        /// Custom event type (must be prefixed, e.g., `vtcode.telemetry`).
        event_type: String,
        /// Sequence number for ordering.
        sequence_number: u64,
        /// Custom event data.
        data: serde_json::Value,
    },
}

impl ResponseStreamEvent {
    /// Returns the response ID associated with this event.
    pub fn response_id(&self) -> &str {
        match self {
            Self::ResponseCreated { response, .. }
            | Self::ResponseInProgress { response, .. }
            | Self::ResponseCompleted { response, .. }
            | Self::ResponseFailed { response, .. }
            | Self::ResponseIncomplete { response, .. } => &response.id,

            Self::OutputItemAdded { response_id, .. }
            | Self::OutputItemDone { response_id, .. }
            | Self::ContentPartAdded { response_id, .. }
            | Self::ContentPartDone { response_id, .. }
            | Self::OutputTextDelta { response_id, .. }
            | Self::OutputTextDone { response_id, .. }
            | Self::FunctionCallArgumentsDelta { response_id, .. }
            | Self::FunctionCallArgumentsDone { response_id, .. }
            | Self::ReasoningDelta { response_id, .. }
            | Self::ReasoningDone { response_id, .. }
            | Self::CustomEvent { response_id, .. } => response_id,
        }
    }

    /// Returns the event type name.
    pub fn event_type(&self) -> &'static str {
        match self {
            Self::ResponseCreated { .. } => "response.created",
            Self::ResponseInProgress { .. } => "response.in_progress",
            Self::ResponseCompleted { .. } => "response.completed",
            Self::ResponseFailed { .. } => "response.failed",
            Self::ResponseIncomplete { .. } => "response.incomplete",
            Self::OutputItemAdded { .. } => "response.output_item.added",
            Self::OutputItemDone { .. } => "response.output_item.done",
            Self::ContentPartAdded { .. } => "response.content_part.added",
            Self::ContentPartDone { .. } => "response.content_part.done",
            Self::OutputTextDelta { .. } => "response.output_text.delta",
            Self::OutputTextDone { .. } => "response.output_text.done",
            Self::FunctionCallArgumentsDelta { .. } => "response.function_call_arguments.delta",
            Self::FunctionCallArgumentsDone { .. } => "response.function_call_arguments.done",
            Self::ReasoningDelta { .. } => "response.reasoning.delta",
            Self::ReasoningDone { .. } => "response.reasoning.done",
            Self::CustomEvent { .. } => "response.custom_event",
        }
    }

    /// Returns true if this is a response lifecycle event.
    pub fn is_response_event(&self) -> bool {
        matches!(
            self,
            Self::ResponseCreated { .. }
                | Self::ResponseInProgress { .. }
                | Self::ResponseCompleted { .. }
                | Self::ResponseFailed { .. }
                | Self::ResponseIncomplete { .. }
        )
    }

    /// Returns true if this is a terminal event.
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            Self::ResponseCompleted { .. }
                | Self::ResponseFailed { .. }
                | Self::ResponseIncomplete { .. }
        )
    }
}

/// Callback type for streaming events.
#[allow(dead_code)]
pub type StreamEventCallback = Arc<Mutex<Box<dyn FnMut(&ResponseStreamEvent) + Send>>>;

/// Trait for emitting Open Responses streaming events.
pub trait StreamEventEmitter: Send {
    /// Emit a streaming event.
    fn emit(&mut self, event: ResponseStreamEvent);

    /// Emit a response created event.
    fn response_created(&mut self, response: Response) {
        self.emit(ResponseStreamEvent::ResponseCreated { response });
    }

    /// Emit a response in progress event.
    fn response_in_progress(&mut self, response: Response) {
        self.emit(ResponseStreamEvent::ResponseInProgress { response });
    }

    /// Emit a response completed event.
    fn response_completed(&mut self, response: Response) {
        self.emit(ResponseStreamEvent::ResponseCompleted { response });
    }

    /// Emit a response failed event.
    fn response_failed(&mut self, response: Response) {
        self.emit(ResponseStreamEvent::ResponseFailed { response });
    }

    /// Emit an output item added event.
    fn output_item_added(&mut self, response_id: &str, output_index: usize, item: OutputItem) {
        self.emit(ResponseStreamEvent::OutputItemAdded {
            response_id: response_id.to_string(),
            output_index,
            item,
        });
    }

    /// Emit an output item done event.
    fn output_item_done(&mut self, response_id: &str, output_index: usize, item: OutputItem) {
        self.emit(ResponseStreamEvent::OutputItemDone {
            response_id: response_id.to_string(),
            output_index,
            item,
        });
    }

    /// Emit a text delta event.
    fn output_text_delta(
        &mut self,
        response_id: &str,
        item_id: &str,
        output_index: usize,
        content_index: usize,
        delta: &str,
    ) {
        self.emit(ResponseStreamEvent::OutputTextDelta {
            response_id: response_id.to_string(),
            item_id: item_id.to_string(),
            output_index,
            content_index,
            delta: delta.to_string(),
        });
    }

    /// Emit a reasoning delta event.
    fn reasoning_delta(
        &mut self,
        response_id: &str,
        item_id: &str,
        output_index: usize,
        delta: &str,
    ) {
        self.emit(ResponseStreamEvent::ReasoningDelta {
            response_id: response_id.to_string(),
            item_id: item_id.to_string(),
            output_index,
            delta: delta.to_string(),
        });
    }
}

/// Vector-based event emitter for collecting events.
#[derive(Debug, Default)]
pub struct VecStreamEmitter {
    events: Vec<ResponseStreamEvent>,
}

impl VecStreamEmitter {
    /// Creates a new vector emitter.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the collected events.
    pub fn events(&self) -> &[ResponseStreamEvent] {
        &self.events
    }

    /// Consumes and returns the collected events.
    pub fn into_events(self) -> Vec<ResponseStreamEvent> {
        self.events
    }
}

impl StreamEventEmitter for VecStreamEmitter {
    fn emit(&mut self, event: ResponseStreamEvent) {
        self.events.push(event);
    }
}

/// Wrapper for streaming events with sequence number for ordering.
/// Used when serializing events for SSE transport.
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize)]
pub struct SequencedEvent<'a> {
    /// Monotonically increasing sequence number within the stream.
    pub sequence_number: u64,
    /// The underlying event.
    #[serde(flatten)]
    pub event: &'a ResponseStreamEvent,
}

impl<'a> SequencedEvent<'a> {
    /// Creates a new sequenced event.
    #[allow(dead_code)]
    pub fn new(sequence_number: u64, event: &'a ResponseStreamEvent) -> Self {
        Self {
            sequence_number,
            event,
        }
    }
}

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

    #[test]
    fn test_event_type() {
        let response = Response::new("resp_1", "gpt-5");
        let event = ResponseStreamEvent::ResponseCreated { response };
        assert_eq!(event.event_type(), "response.created");
    }

    #[test]
    fn test_terminal_events() {
        let response = Response::new("resp_1", "gpt-5");
        let created = ResponseStreamEvent::ResponseCreated {
            response: response.clone(),
        };
        let completed = ResponseStreamEvent::ResponseCompleted { response };
        assert!(!created.is_terminal());
        assert!(completed.is_terminal());
    }

    #[test]
    fn test_vec_emitter() {
        let mut emitter = VecStreamEmitter::new();
        let response = Response::new("resp_1", "gpt-5");
        emitter.response_created(response);
        assert_eq!(emitter.events().len(), 1);
    }
}