ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
// Source: /data/home/swei/claudecode/openclaudecode/src/utils/filePersistence/types.ts
use std::collections::HashMap;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub role: MessageRole,
    pub content: String,
    /// Unique identifier for this message (used by session memory to track extraction boundary)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub uuid: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub attachments: Option<Vec<Attachment>>,
    /// Tool call ID for tool role messages (required by OpenAI API)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub tool_call_id: Option<String>,
    /// Tool calls for assistant messages (required to pair with tool results)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub tool_calls: Option<Vec<ToolCall>>,
    /// Indicates if this message is an error (for tool results)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub is_error: Option<bool>,
    /// Indicates if this is a meta/system message (e.g., from prependUserContext)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub is_meta: Option<bool>,
    /// Indicates this assistant message was generated from an API error (not model-produced text)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub is_api_error_message: Option<bool>,
    /// Structured error details for API error messages (raw API error string)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub error_details: Option<String>,
}

impl Default for Message {
    fn default() -> Self {
        Self {
            role: MessageRole::User,
            content: String::new(),
            uuid: None,
            attachments: None,
            tool_call_id: None,
            tool_calls: None,
            is_error: None,
            is_meta: None,
            is_api_error_message: None,
            error_details: None,
        }
    }
}

/// A tool call from the model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    pub id: String,
    #[serde(default = "default_tool_call_type")]
    pub r#type: String,
    pub name: String,
    pub arguments: serde_json::Value,
}

fn default_tool_call_type() -> String {
    "function".to_string()
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    #[default]
    User,
    Assistant,
    #[serde(rename = "tool")]
    Tool,
    System,
}

impl MessageRole {
    /// Convert the role to its string representation for API serialization
    pub fn as_str(&self) -> &'static str {
        match self {
            MessageRole::User => "user",
            MessageRole::Assistant => "assistant",
            MessageRole::Tool => "tool",
            MessageRole::System => "system",
        }
    }
}

/// Attachments for messages - files, images, etc.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Attachment {
    /// File attachment (at-mentioned file)
    File { path: String },
    /// Reference to a file previously read
    AlreadyReadFile { path: String, content: String },
    /// PDF reference
    PdfReference { path: String },
    /// Text file that was edited
    EditedTextFile { filename: String, snippet: String },
    /// Image file that was edited
    EditedImageFile { filename: String },
    /// Directory listing
    Directory {
        path: String,
        content: String,
        display_path: String,
    },
    /// Selected lines in IDE
    SelectedLinesInIde {
        ide_name: String,
        filename: String,
        start_line: u32,
        end_line: u32,
    },
    /// Memory file reference
    MemoryFile { path: String },
    /// Skill listing attachment
    SkillListing { skills: Vec<SkillInfo> },
    /// Invoked skills attachment
    InvokedSkills { skills: Vec<InvokedSkill> },
    /// Task status
    TaskStatus {
        task_id: String,
        description: String,
        status: String,
    },
    /// Plan file reference
    PlanFileReference { path: String },
    /// MCP tool resources
    McpResources { tools: Vec<String> },
    /// Deferred tools delta
    DeferredTools { tools: Vec<String> },
    /// Agent listing
    AgentListing { agents: Vec<String> },
    /// Custom attachment
    Custom {
        name: String,
        content: serde_json::Value,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillInfo {
    pub name: String,
    pub description: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvokedSkill {
    pub name: String,
    pub path: String,
    pub content: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TokenUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_creation_input_tokens: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_read_input_tokens: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iterations: Option<Vec<IterationUsage>>,
}

/// Per-iteration usage from the Anthropic API (server-side tool loops)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IterationUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    pub name: String,
    pub description: String,
    pub input_schema: ToolInputSchema,
    /// Optional annotations for tool classification
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<ToolAnnotations>,
    /// When true, this tool is deferred (requires ToolSearch to load)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub should_defer: Option<bool>,
    /// When true, this tool is never deferred (full schema always sent)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub always_load: Option<bool>,
    /// When true, this is an MCP tool
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_mcp: Option<bool>,
    /// Short capability phrase for keyword search (3-10 words)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search_hint: Option<String>,
    /// Optional aliases for backwards compatibility when a tool is renamed
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub aliases: Option<Vec<String>>,
    /// Human-readable name for display in the UI (e.g., "Update" vs "Edit")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_facing_name: Option<String>,
    /// Interrupt behavior: 'cancel' (stop on user interrupt) or 'block' (wait for completion)
    #[serde(rename = "interruptBehavior", default, skip_serializing_if = "Option::is_none")]
    pub interrupt_behavior: Option<String>,
}

impl Default for ToolDefinition {
    fn default() -> Self {
        Self {
            name: String::new(),
            description: String::new(),
            input_schema: ToolInputSchema::default(),
            annotations: None,
            should_defer: None,
            always_load: None,
            is_mcp: None,
            search_hint: None,
            aliases: None,
            user_facing_name: None,
            interrupt_behavior: None,
        }
    }
}

impl ToolDefinition {
    /// Create a new tool definition (annotations defaults to None)
    pub fn new(name: &str, description: &str, input_schema: ToolInputSchema) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
            input_schema,
            annotations: None,
            should_defer: None,
            always_load: None,
            is_mcp: None,
            search_hint: None,
            aliases: None,
            user_facing_name: None,
            interrupt_behavior: None,
        }
    }

    /// Build with deferred loading support
    pub fn with_deferred(mut self, should_defer: bool) -> Self {
        self.should_defer = Some(should_defer);
        self
    }

    /// Mark as always loaded (never deferred)
    pub fn with_always_load(mut self) -> Self {
        self.always_load = Some(true);
        self
    }

    /// Mark as MCP tool
    pub fn with_mcp(mut self) -> Self {
        self.is_mcp = Some(true);
        self
    }

    /// Set search hint for keyword search
    pub fn with_search_hint(mut self, hint: &str) -> Self {
        self.search_hint = Some(hint.to_string());
        self
    }

    /// Get interrupt behavior: 'cancel' (stop on user interrupt) or 'block' (wait for completion)
    /// Default: 'block'
    pub fn interrupt_behavior(&self) -> crate::tools::types::InterruptBehavior {
        match self.interrupt_behavior.as_deref() {
            Some("cancel") => crate::tools::types::InterruptBehavior::Cancel,
            _ => crate::tools::types::InterruptBehavior::Block,
        }
    }

    /// Backfill observable input before observers see it (hooks, events, transcript).
    /// Mutates in place to add legacy/derived fields. Must be idempotent.
    /// Default: no-op. Override via `with_interrupt_behavior` for tools that need it.
    pub fn backfill_observable_input(&self, _input: &mut serde_json::Value) {
        // Default no-op. Tools that need backfilling should set interrupt_behavior
        // or use the Tool trait's backfill_observable_input directly.
    }

    /// Check if tool can run concurrently (default: false)
    pub fn is_concurrency_safe(&self, _input: &serde_json::Value) -> bool {
        self.annotations
            .as_ref()
            .and_then(|a| a.concurrency_safe)
            .unwrap_or(false)
    }

    /// Check if tool only reads data (default: false)
    pub fn is_read_only(&self, _input: &serde_json::Value) -> bool {
        if let Some(ref a) = self.annotations {
            if let Some(ro) = a.read_only {
                return ro;
            }
        }
        // Default: tools that only read
        matches!(
            self.name.as_str(),
            "Read" | "Glob" | "Grep" | "Search" | "WebFetch" | "WebSearch"
        )
    }

    /// Check if tool performs destructive operations (default: false)
    pub fn is_destructive(&self, input: &serde_json::Value) -> bool {
        if let Some(ref a) = self.annotations {
            if let Some(d) = a.destructive {
                return d;
            }
        }
        // Default: check input for destructive commands
        let input_str = input.to_string();
        matches!(self.name.as_str(), "Bash" | "Write" | "Edit")
            && (input_str.contains("rm -rf")
                || input_str.contains("rm /")
                || input_str.contains("dd if=")
                || input_str.contains("format"))
    }

    /// Check if tool is idempotent (can be run multiple times safely)
    pub fn is_idempotent(&self) -> bool {
        self.annotations
            .as_ref()
            .and_then(|a| a.idempotent)
            .unwrap_or(false)
    }

    /// Get tool use summary for compact views
    pub fn get_use_summary(&self, input: &serde_json::Value) -> String {
        match self.name.as_str() {
            "Bash" => {
                if let Some(cmd) = input.get("command").and_then(|v| v.as_str()) {
                    let truncated = if cmd.len() > 50 {
                        format!("{}...", &cmd[..50])
                    } else {
                        cmd.to_string()
                    };
                    format!("Bash: {}", truncated)
                } else {
                    "Bash".to_string()
                }
            }
            "Read" => {
                if let Some(path) = input.get("path").and_then(|v| v.as_str()) {
                    format!("Read: {}", path)
                } else {
                    "Read".to_string()
                }
            }
            "Write" => {
                if let Some(path) = input.get("path").and_then(|v| v.as_str()) {
                    format!("Write: {}", path)
                } else {
                    "Write".to_string()
                }
            }
            "Edit" => {
                if let Some(path) = input.get("file_path").and_then(|v| v.as_str()) {
                    format!("Edit: {}", path)
                } else {
                    "Edit".to_string()
                }
            }
            "Glob" => {
                if let Some(pattern) = input.get("pattern").and_then(|v| v.as_str()) {
                    format!("Glob: {}", pattern)
                } else {
                    "Glob".to_string()
                }
            }
            "Grep" => {
                if let Some(pattern) = input.get("pattern").and_then(|v| v.as_str()) {
                    format!("Grep: {}", pattern)
                } else {
                    "Grep".to_string()
                }
            }
            _ => self.name.clone(),
        }
    }
}

/// Tool annotations for classification
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ToolAnnotations {
    /// Tool can run concurrently with other tools
    #[serde(rename = "concurrencySafe", skip_serializing_if = "Option::is_none")]
    pub concurrency_safe: Option<bool>,
    /// Tool only reads data (doesn't modify files/system)
    #[serde(rename = "readOnly", skip_serializing_if = "Option::is_none")]
    pub read_only: Option<bool>,
    /// Tool performs destructive operations
    #[serde(rename = "destructive", skip_serializing_if = "Option::is_none")]
    pub destructive: Option<bool>,
    /// Tool is idempotent (safe to run multiple times)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub idempotent: Option<bool>,
    /// Tool operates on open world (external URLs, etc.)
    #[serde(rename = "openWorld", skip_serializing_if = "Option::is_none")]
    pub open_world: Option<bool>,
}

impl ToolAnnotations {
    /// Create annotations for read-only tools
    pub fn read_only() -> Self {
        Self {
            read_only: Some(true),
            ..Default::default()
        }
    }

    /// Create annotations for destructive tools
    pub fn destructive() -> Self {
        Self {
            destructive: Some(true),
            ..Default::default()
        }
    }

    /// Create annotations for concurrent-safe tools
    pub fn concurrency_safe() -> Self {
        Self {
            concurrency_safe: Some(true),
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ToolInputSchema {
    #[serde(rename = "type")]
    pub schema_type: String,
    pub properties: serde_json::Value,
    pub required: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ToolContext {
    pub cwd: String,
    #[serde(skip)]
    pub abort_signal: std::sync::Arc<crate::utils::AbortSignal>,
}

// Skip Serialize on ToolContext because Arc<AbortSignal> doesn't implement Serialize
impl Serialize for ToolContext {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        // Serialize only the cwd field, skipping abort_signal
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("ToolContext", 1)?;
        state.serialize_field("cwd", &self.cwd)?;
        state.end()
    }
}

impl Default for ToolContext {
    fn default() -> Self {
        Self {
            cwd: String::new(),
            abort_signal: std::sync::Arc::new(crate::utils::AbortSignal::new(0)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    #[serde(rename = "type")]
    pub result_type: String,
    pub tool_use_id: String,
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub was_persisted: Option<bool>,
}

impl Default for ToolResult {
    fn default() -> Self {
        Self {
            result_type: String::new(),
            tool_use_id: String::new(),
            content: String::new(),
            is_error: None,
            was_persisted: None,
        }
    }
}

/// Content block within a tool_result — either text or tool_reference
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolResultContent {
    /// Plain text content
    Text {
        #[serde(rename = "type")]
        content_type: String,
        text: String,
    },
    /// Reference to a deferred tool that the API should expand
    ToolReference {
        #[serde(rename = "type")]
        content_type: String,
        tool_name: String,
    },
}

impl ToolResultContent {
    pub fn text(text: &str) -> Self {
        Self::Text {
            content_type: "text".to_string(),
            text: text.to_string(),
        }
    }

    pub fn tool_reference(tool_name: &str) -> Self {
        Self::ToolReference {
            content_type: "tool_reference".to_string(),
            tool_name: tool_name.to_string(),
        }
    }
}

/// Tool result with structured content (supports tool_reference blocks)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResultStructured {
    #[serde(rename = "type")]
    pub result_type: String,
    pub tool_use_id: String,
    pub content: Vec<ToolResultContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
}

/// Thinking configuration for the API (matches TypeScript ThinkingConfig)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ThinkingConfig {
    /// Adaptive thinking - model decides the best thinking budget
    Adaptive,
    /// Enabled with a specific budget token count
    Enabled {
        #[serde(rename = "budgetTokens")]
        budget_tokens: u32,
    },
    /// Thinking disabled
    Disabled,
}

impl Default for ThinkingConfig {
    fn default() -> Self {
        // Default to adaptive thinking (matches TypeScript shouldEnableThinkingByDefault)
        ThinkingConfig::Adaptive
    }
}

/// Exit reasons from the query loop (matches TypeScript Terminal type)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExitReason {
    /// Normal completion - no more tool calls needed
    Completed,
    /// Maximum turns reached
    MaxTurns { max_turns: u32, turn_count: u32 },
    /// Aborted during model streaming
    AbortedStreaming { reason: String },
    /// Aborted during tool execution
    AbortedTools { reason: String },
    /// Hook prevented continuation
    HookStopped,
    /// Stop hook prevented continuation
    StopHookPrevented,
    /// Context too long (prompt_too_long)
    PromptTooLong { error: Option<String> },
    /// Media error (image too large, etc.)
    ImageError { error: String },
    /// Model/runtime error
    ModelError { error: String },
    /// Token limit reached (blocking_limit)
    BlockingLimit,
    /// Token budget continuation ended early
    TokenBudgetExhausted { reason: String },
    /// Max output tokens reached (during generation)
    MaxTokens,
    /// USD budget exceeded
    MaxBudgetExceeded { max_budget_usd: f64 },
}

impl Default for ExitReason {
    fn default() -> Self {
        ExitReason::Completed
    }
}

/// Compact progress event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum CompactProgressEvent {
    #[serde(rename = "hooks_start")]
    HooksStart {
        #[serde(rename = "hookType")]
        hook_type: CompactHookType,
    },
    #[serde(rename = "compact_start")]
    CompactStart,
    #[serde(rename = "compact_end")]
    CompactEnd {
        /// Human-readable summary emitted to TUI/CLI after successful compaction,
        /// e.g. "Conversation compacted: 120.3k → 8.2k tokens (93%)"
        #[serde(skip_serializing_if = "Option::is_none")]
        message: Option<String>,
    },
}

/// Compact hook types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CompactHookType {
    PreCompact,
    PostCompact,
    SessionStart,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
    pub text: String,
    pub usage: TokenUsage,
    pub num_turns: u32,
    pub duration_ms: u64,
    /// Why the query loop terminated
    pub exit_reason: ExitReason,
}

/// Agent event types for streaming updates (matches TypeScript behavior)
#[derive(Debug, Clone)]
pub enum AgentEvent {
    /// Tool is about to be executed
    ToolStart {
        tool_name: String,
        tool_call_id: String,
        input: serde_json::Value,
        /// Human-readable display name (e.g., "Update" for edits, "Create" for new files).
        /// Overrides `tool_name` for display in the TUI.
        display_name: Option<String>,
        /// Short summary for compact views (e.g., file path for FileEdit).
        summary: Option<String>,
        /// Activity description for spinner display (e.g., "Updating file.rs").
        activity_description: Option<String>,
    },
    /// Tool execution completed
    ToolComplete {
        tool_name: String,
        tool_call_id: String,
        result: ToolResult,
        /// Human-readable display name (e.g., "Update(file.rs) (1 +, 1 -)").
        display_name: Option<String>,
        /// Rendered result message from Tool::render_tool_result_message.
        rendered_result: Option<String>,
    },
    /// Tool execution failed
    ToolError {
        tool_name: String,
        tool_call_id: String,
        error: String,
    },
    /// LLM is thinking (started a new turn)
    Thinking { turn: u32 },
    /// Final response ready
    Done { result: QueryResult },
    /// Message started (streaming begins)
    MessageStart { message_id: String },
    /// Content block started (matches TypeScript StreamEvent content_block_start)
    ContentBlockStart { index: u32, block_type: String },
    /// Content block delta (matches TypeScript StreamEvent content_block_delta)
    ContentBlockDelta { index: u32, delta: ContentDelta },
    /// Content block stopped (matches TypeScript StreamEvent content_block_stop)
    ContentBlockStop { index: u32 },
    /// Message stopped (streaming ends)
    MessageStop,
    /// Request started (before API call) - matches TypeScript 'stream_request_start'
    RequestStart,
    /// Request completed (API response received, streaming finished)
    /// Matches TypeScript 'stream_request_end' — useful for TUI spinner management.
    StreamRequestEnd,
    /// Rate limit status change — notifies TUI/CLI when a rate limit is hit or cleared
    RateLimitStatus {
        /// true if currently rate-limited, false if rate limit has cleared
        is_rate_limited: bool,
        /// Optional retry-after seconds (if the server provided it)
        retry_after_secs: Option<f64>,
    },
    /// Max turns reached - matches TypeScript 'max_turns_reached' attachment
    MaxTurnsReached { max_turns: u32, turn_count: u32 },
    /// Tombstone event for orphaned messages on streaming fallback
    /// (matches TypeScript 'tombstone' event)
    Tombstone { message: String },
    /// Compact progress event (hooks_start, compact_start, compact_end)
    /// Matches TypeScript ToolUseContext.onCompactProgress
    Compact { event: CompactProgressEvent },
    /// Actual API token usage from message_delta event
    /// Emitted after all content_block_stop events, before message_stop
    TokenUsage {
        usage: TokenUsage,
        cost: f64,
    },
    /// API retry progress — emitted during 429/529 retry backoff
    /// Matches TypeScript's 'api_retry' subtype yielded by QueryEngine
    /// from createSystemAPIErrorMessage in withRetry.ts
    ApiRetry {
        /// Current retry attempt (1-based)
        attempt: u32,
        /// Maximum retries configured
        max_retries: u32,
        /// Delay in milliseconds before next retry
        retry_delay_ms: u64,
        /// HTTP error status code that triggered the retry
        error_status: Option<u16>,
        /// Categorized error type (e.g., "rate_limit", "server_error")
        error: String,
    },
}

/// Content delta types for streaming
#[derive(Debug, Clone)]
pub enum ContentDelta {
    /// Text content delta
    Text { text: String },
    /// Thinking content delta (internal reasoning)
    Thinking { text: String },
    /// Tool use input delta (streaming tool arguments)
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
        is_complete: bool,
    },
}

// --------------------------------------------------------------------------
// MCP Types
// --------------------------------------------------------------------------

/// MCP server configuration (union type)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum McpServerConfig {
    Stdio(McpStdioConfig),
    Sse(McpSseConfig),
    Http(McpHttpConfig),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpStdioConfig {
    #[serde(default = "default_stdio_type")]
    pub transport_type: Option<String>,
    pub command: String,
    pub args: Option<Vec<String>>,
    pub env: Option<std::collections::HashMap<String, String>>,
}

fn default_stdio_type() -> Option<String> {
    Some("stdio".to_string())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpSseConfig {
    pub transport_type: String,
    pub url: String,
    pub headers: Option<std::collections::HashMap<String, String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpHttpConfig {
    pub transport_type: String,
    pub url: String,
    pub headers: Option<std::collections::HashMap<String, String>>,
}

/// MCP connection status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum McpConnectionStatus {
    Connected,
    Disconnected,
    Error,
}

/// MCP tool representation from server
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpTool {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "inputSchema")]
    pub input_schema: Option<serde_json::Value>,
}

// --------------------------------------------------------------------------
// Tool Types (translated from Tool.ts)
// --------------------------------------------------------------------------

/// Query chain tracking for nested agent calls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryChainTracking {
    pub chain_id: String,
    pub depth: u32,
}

/// Validation result for tool input
/// Source: /data/home/swei/claudecode/openclaudecode/src/Tool.ts
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "result")]
pub enum ValidationResult {
    /// Validation passed
    #[serde(rename = "true")]
    Valid,
    /// Validation failed with error details
    Invalid {
        /// Error message describing the validation failure
        message: String,
        /// Error code for the validation failure
        #[serde(rename = "errorCode")]
        error_code: i32,
    },
}

impl ValidationResult {
    /// Create a valid validation result
    pub fn valid() -> Self {
        ValidationResult::Valid
    }

    /// Create an invalid validation result
    pub fn invalid(message: String, error_code: i32) -> Self {
        ValidationResult::Invalid {
            message,
            error_code,
        }
    }

    /// Check if the validation passed
    pub fn is_valid(&self) -> bool {
        matches!(self, ValidationResult::Valid)
    }
}

/// Tool permission mode
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum PermissionMode {
    Default,
    Auto,
    #[serde(rename = "auto-accept")]
    AutoAccept,
    #[serde(rename = "auto-deny")]
    AutoDeny,
    Bypass,
}

/// Additional working directory configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdditionalWorkingDirectory {
    pub path: String,
    #[serde(rename = "permissionMode")]
    pub permission_mode: Option<PermissionMode>,
}

/// Permission result from permission checks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionResult {
    pub behavior: PermissionBehavior,
    #[serde(rename = "updatedInput")]
    pub updated_input: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// Permission behavior types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum PermissionBehavior {
    Allow,
    Deny,
    Ask,
}

/// Additional tool permission rules by source
pub type ToolPermissionRulesBySource = HashMap<String, Vec<String>>;

/// Tool permission context - full context for permission checks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolPermissionContext {
    pub mode: PermissionMode,
    #[serde(rename = "additionalWorkingDirectories")]
    pub additional_working_directories: HashMap<String, AdditionalWorkingDirectory>,
    #[serde(rename = "alwaysAllowRules")]
    pub always_allow_rules: ToolPermissionRulesBySource,
    #[serde(rename = "alwaysDenyRules")]
    pub always_deny_rules: ToolPermissionRulesBySource,
    #[serde(rename = "alwaysAskRules")]
    pub always_ask_rules: ToolPermissionRulesBySource,
    #[serde(rename = "isBypassPermissionsModeAvailable")]
    pub is_bypass_permissions_mode_available: bool,
    #[serde(
        rename = "isAutoModeAvailable",
        skip_serializing_if = "Option::is_none"
    )]
    pub is_auto_mode_available: Option<bool>,
    #[serde(
        rename = "strippedDangerousRules",
        skip_serializing_if = "Option::is_none"
    )]
    pub stripped_dangerous_rules: Option<ToolPermissionRulesBySource>,
    #[serde(
        rename = "shouldAvoidPermissionPrompts",
        skip_serializing_if = "Option::is_none"
    )]
    pub should_avoid_permission_prompts: Option<bool>,
    #[serde(
        rename = "awaitAutomatedChecksBeforeDialog",
        skip_serializing_if = "Option::is_none"
    )]
    pub await_automated_checks_before_dialog: Option<bool>,
    #[serde(rename = "prePlanMode", skip_serializing_if = "Option::is_none")]
    pub pre_plan_mode: Option<PermissionMode>,
}

impl Default for ToolPermissionContext {
    fn default() -> Self {
        Self {
            mode: PermissionMode::Default,
            additional_working_directories: HashMap::new(),
            always_allow_rules: HashMap::new(),
            always_deny_rules: HashMap::new(),
            always_ask_rules: HashMap::new(),
            is_bypass_permissions_mode_available: false,
            is_auto_mode_available: None,
            stripped_dangerous_rules: None,
            should_avoid_permission_prompts: None,
            await_automated_checks_before_dialog: None,
            pre_plan_mode: None,
        }
    }
}

/// Create empty tool permission context
pub fn get_empty_tool_permission_context() -> ToolPermissionContext {
    ToolPermissionContext::default()
}

/// Tool input JSON schema (for MCP tools)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolInputJSONSchema {
    #[serde(flatten)]
    pub properties: serde_json::Value,
    #[serde(rename = "type")]
    pub schema_type: String,
}

// --------------------------------------------------------------------------
// Tool Progress Types (translated from types/tools.ts)
// --------------------------------------------------------------------------

/// Bash tool progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BashProgress {
    #[serde(rename = "shell")]
    pub shell: Option<String>,
    #[serde(rename = "command")]
    pub command: Option<String>,
}

/// REPL tool progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplProgress {
    #[serde(rename = "input")]
    pub input: Option<String>,
    #[serde(rename = "toolName")]
    pub tool_name: Option<String>,
    #[serde(rename = "toolCallId")]
    pub tool_call_id: Option<String>,
}

/// MCP tool progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpProgress {
    #[serde(rename = "serverName")]
    pub server_name: String,
    #[serde(rename = "toolName")]
    pub tool_name: String,
    #[serde(rename = "progress")]
    pub progress: Option<serde_json::Value>,
}

/// Web search progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSearchProgress {
    #[serde(rename = "query")]
    pub query: String,
    #[serde(rename = "currentStep")]
    pub current_step: Option<String>,
}

/// Task output progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskOutputProgress {
    #[serde(rename = "taskId")]
    pub task_id: String,
    #[serde(rename = "output")]
    pub output: Option<String>,
}

/// Skill tool progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillToolProgress {
    #[serde(rename = "skill")]
    pub skill: String,
    #[serde(rename = "step")]
    pub step: Option<String>,
}

/// Agent tool progress data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentToolProgress {
    #[serde(rename = "description")]
    pub description: String,
    #[serde(rename = "subagentType")]
    pub subagent_type: Option<String>,
}

/// Tool progress data - enum of all progress types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ToolProgressData {
    #[serde(rename = "bash_progress")]
    BashProgress(BashProgress),
    #[serde(rename = "repl_progress")]
    ReplProgress(ReplProgress),
    #[serde(rename = "mcp_progress")]
    McpProgress(McpProgress),
    #[serde(rename = "web_search_progress")]
    WebSearchProgress(WebSearchProgress),
    #[serde(rename = "task_output_progress")]
    TaskOutputProgress(TaskOutputProgress),
    #[serde(rename = "skill_progress")]
    SkillProgress(SkillToolProgress),
    #[serde(rename = "agent_progress")]
    AgentProgress(AgentToolProgress),
}

/// Tool progress with tool use ID
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolProgress<P: Clone + serde::Serialize> {
    #[serde(rename = "toolUseID")]
    pub tool_use_id: String,
    pub data: P,
}

/// Filter progress messages to only tool progress (not hook progress)
pub fn filter_tool_progress_messages(
    progress_messages: &[serde_json::Value],
) -> Vec<serde_json::Value> {
    progress_messages
        .iter()
        .filter(|msg| {
            let data_type = msg.get("data").and_then(|d| d.get("type"));
            data_type.map(|t| t != "hook_progress").unwrap_or(true)
        })
        .cloned()
        .collect()
}