kimi-wire 0.4.0

Typed Rust client for the Kimi Code CLI Wire protocol.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use serde::{Deserialize, Serialize, Serializer};

use super::content::{ContentPart, ToolReturnValue, UserInput};

/// An event emitted by the agent during a turn.
///
/// Events are sent as JSON-RPC notifications (`method: "event"`) and do not
/// require a response.
///
/// Serialization follows the official wire envelope format:
/// `{"type": "TurnBegin", "payload": {"user_input": ...}}`.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Event {
    /// A new turn has started with the given user input.
    TurnBegin {
        /// The user's input that triggered this turn.
        user_input: UserInput,
    },
    /// The current turn has ended.
    TurnEnd,
    /// A new step within the turn has started.
    StepBegin {
        /// Step number, starting from 1.
        n: u32,
    },
    /// The current step was interrupted (e.g. by user input).
    StepInterrupted,
    /// The current step attempt failed and will be retried.
    ///
    /// Added in Wire protocol v1.10.
    StepRetry {
        /// Step number.
        n: u32,
        /// Next attempt number, 1-based.
        next_attempt: u32,
        /// Maximum number of attempts for this step.
        max_attempts: u32,
        /// Seconds to wait before retrying.
        wait_s: u32,
        /// Exception class name that triggered the retry.
        error_type: String,
        /// HTTP status code (if available).
        status_code: Option<u32>,
    },
    /// Context compaction has started.
    CompactionBegin,
    /// Context compaction has finished.
    CompactionEnd,
    /// Server status update (token usage, context size, etc.).
    StatusUpdate(StatusUpdate),
    /// A content part (text, image, etc.) from the model.
    ContentPart(ContentPart),
    /// A tool call from the model.
    ///
    /// Wire envelope type is `"ToolCall"`. The payload carries an inner
    /// `type: "function"` discriminator, matching the official v1.10 spec.
    ToolCall {
        /// Tool call id.
        id: String,
        /// Function name and arguments.
        function: ToolCallFunction,
        /// Extra fields from the wire protocol.
        extras: Option<serde_json::Value>,
    },
    /// A partial tool call (streaming arguments).
    ToolCallPart {
        /// Partial JSON arguments.
        arguments_part: Option<String>,
    },
    /// Result of a tool execution.
    ToolResult {
        /// Id of the corresponding tool call.
        tool_call_id: String,
        /// Return value from the tool.
        return_value: ToolReturnValue,
    },
    /// Response to an approval request (sent by the client).
    ApprovalResponse {
        /// Id of the approval request.
        request_id: String,
        /// Approval decision.
        response: ApprovalResponseKind,
        /// Optional feedback text from the user.
        feedback: Option<String>,
    },
    /// An event from a subagent.
    SubagentEvent {
        /// Id of the parent tool call that spawned the subagent.
        parent_tool_call_id: Option<String>,
        /// Subagent id.
        agent_id: Option<String>,
        /// Subagent type.
        subagent_type: Option<String>,
        /// Nested wire message in envelope form.
        event: SubagentEventPayload,
    },
    /// Additional user input steering the current turn.
    SteerInput {
        /// The steering input.
        user_input: UserInput,
    },
    /// A side question (`/btw`) has started processing.
    ///
    /// Added in Wire protocol v1.9.
    BtwBegin {
        /// Unique ID to pair with the corresponding BtwEnd.
        id: String,
        /// The user's original side question text.
        question: String,
    },
    /// A side question (`/btw`) has finished processing.
    ///
    /// Added in Wire protocol v1.9.
    BtwEnd {
        /// Unique ID matching the corresponding BtwBegin.
        id: String,
        /// The LLM's response text, or null if it failed.
        response: Option<String>,
        /// Error message if the side question failed.
        error: Option<String>,
    },
    /// Plan display content.
    PlanDisplay {
        /// Display content.
        content: String,
        /// File path associated with the plan.
        file_path: String,
    },
    /// A hook was triggered.
    HookTriggered {
        /// Event name.
        event: String,
        /// Target of the hook.
        target: String,
        /// Number of times this hook has fired.
        hook_count: u32,
    },
    /// A hook was resolved.
    HookResolved {
        /// Event name.
        event: String,
        /// Target of the hook.
        target: String,
        /// Action taken.
        action: HookAction,
        /// Reason for the action.
        reason: String,
        /// Duration in milliseconds.
        duration_ms: u64,
    },
}

// ---------------------------------------------------------------------------
// FlatEvent – internal mirror used for (de)serialization
// ---------------------------------------------------------------------------

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub(crate) enum FlatEvent {
    TurnBegin {
        user_input: UserInput,
    },
    TurnEnd,
    StepBegin {
        n: u32,
    },
    StepInterrupted,
    StepRetry {
        n: u32,
        next_attempt: u32,
        max_attempts: u32,
        wait_s: u32,
        error_type: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        status_code: Option<u32>,
    },
    CompactionBegin,
    CompactionEnd,
    StatusUpdate(StatusUpdate),
    ContentPart(ContentPart),
    ToolCall {
        id: String,
        function: ToolCallFunction,
        #[serde(skip_serializing_if = "Option::is_none")]
        extras: Option<serde_json::Value>,
    },
    ToolCallPart {
        #[serde(skip_serializing_if = "Option::is_none")]
        arguments_part: Option<String>,
    },
    ToolResult {
        tool_call_id: String,
        return_value: ToolReturnValue,
    },
    ApprovalResponse {
        request_id: String,
        response: ApprovalResponseKind,
        #[serde(skip_serializing_if = "Option::is_none")]
        feedback: Option<String>,
    },
    SubagentEvent {
        #[serde(skip_serializing_if = "Option::is_none")]
        parent_tool_call_id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        agent_id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        subagent_type: Option<String>,
        event: SubagentEventPayload,
    },
    SteerInput {
        user_input: UserInput,
    },
    BtwBegin {
        id: String,
        question: String,
    },
    BtwEnd {
        id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        response: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<String>,
    },
    PlanDisplay {
        content: String,
        file_path: String,
    },
    HookTriggered {
        event: String,
        target: String,
        hook_count: u32,
    },
    HookResolved {
        event: String,
        target: String,
        action: HookAction,
        reason: String,
        duration_ms: u64,
    },
}

impl From<Event> for FlatEvent {
    fn from(ev: Event) -> Self {
        match ev {
            Event::TurnBegin { user_input } => FlatEvent::TurnBegin { user_input },
            Event::TurnEnd => FlatEvent::TurnEnd,
            Event::StepBegin { n } => FlatEvent::StepBegin { n },
            Event::StepInterrupted => FlatEvent::StepInterrupted,
            Event::StepRetry {
                n,
                next_attempt,
                max_attempts,
                wait_s,
                error_type,
                status_code,
            } => FlatEvent::StepRetry {
                n,
                next_attempt,
                max_attempts,
                wait_s,
                error_type,
                status_code,
            },
            Event::CompactionBegin => FlatEvent::CompactionBegin,
            Event::CompactionEnd => FlatEvent::CompactionEnd,
            Event::StatusUpdate(s) => FlatEvent::StatusUpdate(s),
            Event::ContentPart(c) => FlatEvent::ContentPart(c),
            Event::ToolCall {
                id,
                function,
                extras,
            } => FlatEvent::ToolCall {
                id,
                function,
                extras,
            },
            Event::ToolCallPart { arguments_part } => FlatEvent::ToolCallPart { arguments_part },
            Event::ToolResult {
                tool_call_id,
                return_value,
            } => FlatEvent::ToolResult {
                tool_call_id,
                return_value,
            },
            Event::ApprovalResponse {
                request_id,
                response,
                feedback,
            } => FlatEvent::ApprovalResponse {
                request_id,
                response,
                feedback,
            },
            Event::SubagentEvent {
                parent_tool_call_id,
                agent_id,
                subagent_type,
                event,
            } => FlatEvent::SubagentEvent {
                parent_tool_call_id,
                agent_id,
                subagent_type,
                event,
            },
            Event::SteerInput { user_input } => FlatEvent::SteerInput { user_input },
            Event::BtwBegin { id, question } => FlatEvent::BtwBegin { id, question },
            Event::BtwEnd {
                id,
                response,
                error,
            } => FlatEvent::BtwEnd {
                id,
                response,
                error,
            },
            Event::PlanDisplay { content, file_path } => {
                FlatEvent::PlanDisplay { content, file_path }
            }
            Event::HookTriggered {
                event,
                target,
                hook_count,
            } => FlatEvent::HookTriggered {
                event,
                target,
                hook_count,
            },
            Event::HookResolved {
                event,
                target,
                action,
                reason,
                duration_ms,
            } => FlatEvent::HookResolved {
                event,
                target,
                action,
                reason,
                duration_ms,
            },
        }
    }
}

impl Event {
    /// Return the wire type name for this event.
    ///
    /// Matches the `type` field in the wire envelope.
    #[must_use]
    pub const fn type_name(&self) -> &'static str {
        match self {
            Event::TurnBegin { .. } => "TurnBegin",
            Event::TurnEnd => "TurnEnd",
            Event::StepBegin { .. } => "StepBegin",
            Event::StepInterrupted => "StepInterrupted",
            Event::StepRetry { .. } => "StepRetry",
            Event::CompactionBegin => "CompactionBegin",
            Event::CompactionEnd => "CompactionEnd",
            Event::StatusUpdate(_) => "StatusUpdate",
            Event::ContentPart(_) => "ContentPart",
            Event::ToolCall { .. } => "ToolCall",
            Event::ToolCallPart { .. } => "ToolCallPart",
            Event::ToolResult { .. } => "ToolResult",
            Event::ApprovalResponse { .. } => "ApprovalResponse",
            Event::SubagentEvent { .. } => "SubagentEvent",
            Event::SteerInput { .. } => "SteerInput",
            Event::BtwBegin { .. } => "BtwBegin",
            Event::BtwEnd { .. } => "BtwEnd",
            Event::PlanDisplay { .. } => "PlanDisplay",
            Event::HookTriggered { .. } => "HookTriggered",
            Event::HookResolved { .. } => "HookResolved",
        }
    }
}

impl From<FlatEvent> for Event {
    fn from(ev: FlatEvent) -> Self {
        match ev {
            FlatEvent::TurnBegin { user_input } => Event::TurnBegin { user_input },
            FlatEvent::TurnEnd => Event::TurnEnd,
            FlatEvent::StepBegin { n } => Event::StepBegin { n },
            FlatEvent::StepInterrupted => Event::StepInterrupted,
            FlatEvent::StepRetry {
                n,
                next_attempt,
                max_attempts,
                wait_s,
                error_type,
                status_code,
            } => Event::StepRetry {
                n,
                next_attempt,
                max_attempts,
                wait_s,
                error_type,
                status_code,
            },
            FlatEvent::CompactionBegin => Event::CompactionBegin,
            FlatEvent::CompactionEnd => Event::CompactionEnd,
            FlatEvent::StatusUpdate(s) => Event::StatusUpdate(s),
            FlatEvent::ContentPart(c) => Event::ContentPart(c),
            FlatEvent::ToolCall {
                id,
                function,
                extras,
            } => Event::ToolCall {
                id,
                function,
                extras,
            },
            FlatEvent::ToolCallPart { arguments_part } => Event::ToolCallPart { arguments_part },
            FlatEvent::ToolResult {
                tool_call_id,
                return_value,
            } => Event::ToolResult {
                tool_call_id,
                return_value,
            },
            FlatEvent::ApprovalResponse {
                request_id,
                response,
                feedback,
            } => Event::ApprovalResponse {
                request_id,
                response,
                feedback,
            },
            FlatEvent::SubagentEvent {
                parent_tool_call_id,
                agent_id,
                subagent_type,
                event,
            } => Event::SubagentEvent {
                parent_tool_call_id,
                agent_id,
                subagent_type,
                event,
            },
            FlatEvent::SteerInput { user_input } => Event::SteerInput { user_input },
            FlatEvent::BtwBegin { id, question } => Event::BtwBegin { id, question },
            FlatEvent::BtwEnd {
                id,
                response,
                error,
            } => Event::BtwEnd {
                id,
                response,
                error,
            },
            FlatEvent::PlanDisplay { content, file_path } => {
                Event::PlanDisplay { content, file_path }
            }
            FlatEvent::HookTriggered {
                event,
                target,
                hook_count,
            } => Event::HookTriggered {
                event,
                target,
                hook_count,
            },
            FlatEvent::HookResolved {
                event,
                target,
                action,
                reason,
                duration_ms,
            } => Event::HookResolved {
                event,
                target,
                action,
                reason,
                duration_ms,
            },
        }
    }
}

// ---------------------------------------------------------------------------
// EventEnvelope – {type, payload} wire format
// ---------------------------------------------------------------------------

#[derive(Serialize, Deserialize)]
struct EventEnvelope {
    #[serde(rename = "type")]
    type_name: String,
    payload: serde_json::Value,
}

impl Serialize for Event {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            // ContentPart carries its own "type" field (e.g. "text", "image_url").
            // We must not strip it, otherwise deserialization fails.
            Event::ContentPart(part) => {
                let payload = serde_json::to_value(part).map_err(serde::ser::Error::custom)?;
                EventEnvelope {
                    type_name: "ContentPart".to_string(),
                    payload,
                }
                .serialize(serializer)
            }
            // ToolCall payload carries an inner `type: "function"` discriminator
            // that must be preserved in the payload, separate from the envelope type.
            Event::ToolCall {
                id,
                function,
                extras,
            } => {
                #[derive(Serialize)]
                struct ToolCallPayload<'a> {
                    #[serde(rename = "type")]
                    type_name: &'a str,
                    id: &'a str,
                    function: &'a ToolCallFunction,
                    #[serde(skip_serializing_if = "Option::is_none")]
                    extras: &'a Option<serde_json::Value>,
                }
                let payload = serde_json::to_value(&ToolCallPayload {
                    type_name: "function",
                    id,
                    function,
                    extras,
                })
                .map_err(serde::ser::Error::custom)?;
                EventEnvelope {
                    type_name: "ToolCall".to_string(),
                    payload,
                }
                .serialize(serializer)
            }
            _ => {
                let flat = FlatEvent::from(self.clone());
                let mut value = serde_json::to_value(&flat).map_err(serde::ser::Error::custom)?;
                let obj = value
                    .as_object_mut()
                    .ok_or_else(|| serde::ser::Error::custom("expected object"))?;
                let type_name = obj
                    .remove("type")
                    .and_then(|v| v.as_str().map(String::from))
                    .ok_or_else(|| serde::ser::Error::custom("missing type"))?;
                EventEnvelope {
                    type_name,
                    payload: value,
                }
                .serialize(serializer)
            }
        }
    }
}

impl<'de> Deserialize<'de> for Event {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let envelope = EventEnvelope::deserialize(deserializer)?;
        match envelope.type_name.as_str() {
            "ContentPart" => {
                let part: ContentPart =
                    serde_json::from_value(envelope.payload).map_err(serde::de::Error::custom)?;
                Ok(Event::ContentPart(part))
            }
            _ => {
                let mut value = envelope.payload;
                if let Some(obj) = value.as_object_mut() {
                    obj.insert(
                        "type".to_string(),
                        serde_json::Value::String(envelope.type_name),
                    );
                }
                let flat: FlatEvent =
                    serde_json::from_value(value).map_err(serde::de::Error::custom)?;
                Ok(Event::from(flat))
            }
        }
    }
}

/// Payload of a [`Event::SubagentEvent`].
///
/// This is a generic `{type, payload}` envelope rather than a strongly-typed
/// [`Event`] because subagent events may be any wire message type.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SubagentEventPayload {
    /// The wire type name of the subagent event.
    #[serde(rename = "type")]
    pub type_name: String,
    /// The raw payload of the subagent event.
    pub payload: serde_json::Value,
}

/// Status update from the server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct StatusUpdate {
    /// Fraction of context window used (0.0–1.0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_usage: Option<f64>,
    /// Number of context tokens used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_tokens: Option<u64>,
    /// Maximum context tokens allowed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_context_tokens: Option<u64>,
    /// Detailed token usage breakdown.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_usage: Option<TokenUsage>,
    /// Server-assigned message id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_id: Option<String>,
    /// Whether plan mode is active. `null` means no change.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_mode: Option<bool>,
}

/// Token usage breakdown.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TokenUsage {
    /// Input tokens excluding `input_cache_read` and `input_cache_creation`.
    pub input_other: u64,
    /// Total output tokens.
    pub output: u64,
    /// Cached input tokens.
    pub input_cache_read: u64,
    /// Input tokens used for cache creation (currently only Anthropic API).
    pub input_cache_creation: u64,
}

/// Function name and arguments for a tool call.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ToolCallFunction {
    /// Function name.
    pub name: String,
    /// JSON-encoded arguments.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<String>,
}

/// Client's response to an approval request.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ApprovalResponseKind {
    /// Approve this request once.
    Approve,
    /// Approve this request and remember for the session.
    #[serde(rename = "approve_for_session")]
    ApproveForSession,
    /// Reject this request.
    Reject,
}

/// Action taken by a hook.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum HookAction {
    /// Allow the operation to proceed.
    Allow,
    /// Block the operation.
    Block,
}