claude-code-agent 0.6.0

Rust SDK for Claude Code — persistent subprocess, hook callbacks, in-process tools, record/replay
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
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
//! Wire types for the Claude Code stream-json protocol.
//! Source: the Claude Code 2.1.328 binary's zod schemas and the official
//! Python SDK (claude_agent_sdk 0.1.48); citations are on the types they cover.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ─── Outbound: host → CLI ────────────────────────────────────────────────────
//
// Wire format verified against the official Python SDK
// (claude_agent_sdk 0.1.48, _internal/query.py:150–160, :238–375) and the
// zod schemas inside the Claude Code 2.1.328 binary.

/// Envelope for every host → CLI control request:
/// `{"type":"control_request","request_id":"…","request":{"subtype":"…",…}}`.
/// Source: query.py:368–372
#[derive(Debug, Clone, Serialize)]
pub struct OutboundControlRequest<'a, B: Serialize> {
    #[serde(rename = "type")]
    pub kind: &'static str, // "control_request"
    pub request_id: &'a str,
    pub request: &'a B,
}

/// Body of the `initialize` control request — the first stdin line.
///
/// Process-level settings (cwd, model, permission mode, session id) are CLI
/// flags, not initialize fields; they live on `SessionOptions`.
/// `hooks` is filled in by `Session::spawn` from `SessionOptions::hooks`.
/// Source: binary initialize schema (subtype "initialize", hooks, agents,
/// systemPrompt, appendSystemPrompt, jsonSchema, …); query.py:150–155
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Initialize {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<HashMap<HookEventKind, Vec<HookMatcherConfig>>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub append_system_prompt: Option<String>,
    /// Subagent definitions, keyed by agent name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agents: Option<serde_json::Value>,
    /// JSON schema for structured output; the result arrives in
    /// `TurnResult::structured_output`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub json_schema: Option<serde_json::Value>,
    /// Session title; skips automatic title generation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Load only the skills with these names.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skills: Option<Vec<String>>,
    /// Plugins to load, e.g. `{"type": "local", "path": "./my-plugin"}`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plugins: Option<Vec<serde_json::Value>>,
    /// MCP servers the CLI runs or connects to, keyed by name: stdio
    /// (`{"command": …, "args": […]}`), `http` or `sse` configs.
    /// In-process servers go in `SessionOptions::mcp_servers`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcp_servers: Option<serde_json::Map<String, serde_json::Value>>,
    /// Replaces the default plan-mode workflow text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_mode_instructions: Option<String>,
    /// Omit per-user dynamic sections (working directory, memory path) from
    /// the system prompt, so it caches across users.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude_dynamic_sections: Option<bool>,
    /// Record the system prompt once per conversation and reuse it verbatim.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt_snapshot: Option<bool>,
    /// Emit `prompt_suggestion` frames after turns.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_suggestions: Option<bool>,
    /// Emit progress summaries of running subagents.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent_progress_summaries: Option<bool>,
    /// Forward subagent text and thinking.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub forward_subagent_text: Option<bool>,
    /// Tool-name aliases applied before tool-name resolution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_aliases: Option<HashMap<String, String>>,
    /// `request_user_dialog` kinds `SessionOptions::on_user_dialog` renders;
    /// the CLI sends only these kinds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported_dialog_kinds: Option<Vec<String>>,
    /// Declares that the host renders a per-task stop control (`stop_task`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub per_task_stop_affordance: Option<bool>,
    /// Workspace trust the host obtained from the user for `directory`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace_trust: Option<WorkspaceTrust>,
}

/// Send `accepted: true` only after the user accepted a trust dialog for
/// `directory`. Source: binary initialize.workspaceTrust schema
#[derive(Debug, Clone, Serialize)]
pub struct WorkspaceTrust {
    pub accepted: bool,
    pub directory: String,
}

/// One hook matcher in the initialize request.
/// Source: query.py:142–147 (`matcher`, `hookCallbackIds`, `timeout`)
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HookMatcherConfig {
    pub matcher: Option<String>,
    pub hook_callback_ids: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout: Option<u32>,
}

/// Permission modes accepted by `--permission-mode` and `set_permission_mode`.
/// Source: binary permission-mode enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionMode {
    Default,
    AcceptEdits,
    BypassPermissions,
    Plan,
    DontAsk,
    Auto,
}

impl PermissionMode {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::AcceptEdits => "acceptEdits",
            Self::BypassPermissions => "bypassPermissions",
            Self::Plan => "plan",
            Self::DontAsk => "dontAsk",
            Self::Auto => "auto",
        }
    }
}

/// Effort levels accepted by `--effort` and `set_effort`.
/// Source: `claude --help` (2.1.329)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Effort {
    Low,
    Medium,
    High,
    Xhigh,
    Max,
}

impl Effort {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::Xhigh => "xhigh",
            Self::Max => "max",
        }
    }
}

/// `--thinking`. Source: binary `--thinking <mode>` option (hidden)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Thinking {
    Enabled,
    Adaptive,
    Disabled,
}

impl Thinking {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Enabled => "enabled",
            Self::Adaptive => "adaptive",
            Self::Disabled => "disabled",
        }
    }
}

/// How thinking content appears (`--thinking-display`, `set_max_thinking_tokens`).
/// Source: binary thinking-display enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingDisplay {
    Summarized,
    Omitted,
    Highlights,
}

impl ThinkingDisplay {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Summarized => "summarized",
            Self::Omitted => "omitted",
            Self::Highlights => "highlights",
        }
    }
}

/// `--autocompact`: when the CLI compacts the context on its own.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Autocompact {
    /// The CLI's default window.
    Auto,
    /// Compact at this many tokens (at least 100 000).
    Tokens(u64),
}

impl Autocompact {
    pub fn to_arg(self) -> String {
        match self {
            Self::Auto => "auto".into(),
            Self::Tokens(n) => n.to_string(),
        }
    }
}

/// `--permission-prompts`: who answers a permission prompt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PermissionPrompts {
    /// The SDK host (`SessionOptions::can_use_tool`).
    Host,
    /// Nobody: anything that would prompt is denied.
    None,
}

impl PermissionPrompts {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Host => "host",
            Self::None => "none",
        }
    }
}

/// When a prompt sent during a running turn is taken in.
/// Source: binary user-message `priority` enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Priority {
    /// Aborts the running turn, then runs. The aborted turn still ends with
    /// a `success` result (`terminal_reason: "aborted_streaming"`).
    Now,
    /// Taken in at the next opportunity inside the running turn.
    Next,
    /// Queued until the running turn ends.
    Later,
}

/// A user turn.
/// Source: _internal/client.py:126–131
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMessage {
    #[serde(rename = "type")]
    pub kind: String, // "user"
    pub message: UserMessageBody,
    pub parent_tool_use_id: Option<String>,
    /// Echoed back (`isReplay: true`) when the CLI starts this message's turn,
    /// because the session runs with `--replay-user-messages`.
    pub uuid: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub priority: Option<Priority>,
    /// `Some(false)`: append to the conversation without starting a turn.
    #[serde(default, rename = "shouldQuery", skip_serializing_if = "Option::is_none")]
    pub should_query: Option<bool>,
    /// ISO 8601 time the message was created. Part of the wire schema, but
    /// CLI 2.1.329 records its own receive time in the echo and transcript.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    /// `Some(true)`: the host composed this text (the user did not type it).
    /// The CLI delivers it as written: no `@path` expansion, no slash-command
    /// dispatch, and no turn-start context attachments.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_composed: Option<bool>,
}

impl UserMessage {
    pub fn text(content: impl Into<String>) -> Self {
        Self::blocks(vec![ContentBlock::Text { text: content.into() }])
    }

    /// A message with several blocks, e.g. text plus images.
    pub fn blocks(content: Vec<ContentBlock>) -> Self {
        Self {
            kind: "user".into(),
            message: UserMessageBody { role: "user".into(), content },
            parent_tool_use_id: None,
            uuid: uuid::Uuid::new_v4().to_string(),
            priority: None,
            should_query: None,
            timestamp: None,
            client_composed: None,
        }
    }

    /// Creation time as an ISO 8601 string, e.g. "2026-11-20T07:03:32.670Z".
    pub fn with_timestamp(mut self, timestamp: impl Into<String>) -> Self {
        self.timestamp = Some(timestamp.into());
        self
    }

    /// Mark the text as host-composed: delivered verbatim, with no `@path`
    /// expansion, slash-command dispatch or turn-start attachments.
    pub fn client_composed(mut self) -> Self {
        self.client_composed = Some(true);
        self
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Add the message to the conversation without starting a turn. Its
    /// content is seen by the model on the next prompt.
    pub fn context_only(mut self) -> Self {
        self.should_query = Some(false);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMessageBody {
    pub role: String,
    pub content: Vec<ContentBlock>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ContentBlock {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "image")]
    Image { source: ImageSource },
    /// A PDF or plain-text document.
    #[serde(rename = "document")]
    Document {
        source: DocumentSource,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        title: Option<String>,
    },
    #[serde(rename = "tool_result")]
    ToolResult {
        tool_use_id: String,
        content: Vec<ContentBlock>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
    },
}

impl ContentBlock {
    /// An image from base64 data; `media_type` e.g. "image/png".
    pub fn image_base64(media_type: impl Into<String>, data: impl Into<String>) -> Self {
        Self::Image {
            source: ImageSource::Base64 { media_type: media_type.into(), data: data.into() },
        }
    }

    /// A PDF from base64 data.
    pub fn pdf_base64(data: impl Into<String>) -> Self {
        Self::Document {
            source: DocumentSource::Base64 { media_type: "application/pdf".into(), data: data.into() },
            title: None,
        }
    }
}

/// Anthropic Messages API document source.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DocumentSource {
    Base64 { media_type: String, data: String },
    Text { media_type: String, data: String },
    Url { url: String },
}

/// Anthropic Messages API image source.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ImageSource {
    Base64 { media_type: String, data: String },
    Url { url: String },
}

/// Envelope for every host → CLI answer to a CLI-initiated control request.
/// Source: query.py:325–345
#[derive(Debug, Clone, Serialize)]
pub struct ControlResponse {
    #[serde(rename = "type")]
    pub kind: &'static str, // "control_response"
    pub response: ControlResponseBody,
}

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "subtype", rename_all = "lowercase")]
pub enum ControlResponseBody {
    Success { request_id: String, response: serde_json::Value },
    Error { request_id: String, error: String },
}

impl ControlResponse {
    pub fn success(request_id: impl Into<String>, response: serde_json::Value) -> Self {
        Self {
            kind: "control_response",
            response: ControlResponseBody::Success { request_id: request_id.into(), response },
        }
    }
    pub fn error(request_id: impl Into<String>, error: impl Into<String>) -> Self {
        Self {
            kind: "control_response",
            response: ControlResponseBody::Error { request_id: request_id.into(), error: error.into() },
        }
    }
}

/// Answer to a `can_use_tool` request.
/// Source: binary can_use_tool response schema (`behavior`, `updatedInput`,
/// `updatedPermissions`, `message`, `interrupt`)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "behavior", rename_all = "camelCase")]
pub enum ToolDecision {
    /// `updated_input: None` → the original input is sent back unchanged.
    /// `updated_permissions`: permission updates to apply, e.g. the
    /// request's `permission_suggestions` for "always allow".
    #[serde(rename = "allow")]
    Allow {
        #[serde(rename = "updatedInput", skip_serializing_if = "Option::is_none")]
        updated_input: Option<serde_json::Value>,
        #[serde(rename = "updatedPermissions", default, skip_serializing_if = "Option::is_none")]
        updated_permissions: Option<Vec<serde_json::Value>>,
    },
    #[serde(rename = "deny")]
    Deny {
        message: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        interrupt: Option<bool>,
    },
}

impl ToolDecision {
    pub fn allow() -> Self { Self::Allow { updated_input: None, updated_permissions: None } }
    /// Allow with a rewritten input.
    pub fn allow_with_input(input: serde_json::Value) -> Self {
        Self::Allow { updated_input: Some(input), updated_permissions: None }
    }
    /// Allow and apply permission updates, so the CLI stops asking (pass the
    /// request's `permission_suggestions` for "always allow").
    pub fn allow_and_remember(updates: Vec<serde_json::Value>) -> Self {
        Self::Allow { updated_input: None, updated_permissions: Some(updates) }
    }
    pub fn deny(message: impl Into<String>) -> Self {
        Self::Deny { message: message.into(), interrupt: None }
    }
}

/// Hook result — the same JSON a shell hook prints on stdout.
/// Source: binary hook-output schema (continue, suppressOutput, stopReason,
/// decision, systemMessage, reason, hookSpecificOutput).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct HookOutput {
    /// `false` stops the whole agent loop.
    #[serde(rename = "continue", skip_serializing_if = "Option::is_none")]
    pub continue_: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suppress_output: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub decision: Option<HookDecision>,
    /// Warning shown to the user.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_message: Option<String>,
    /// Reason fed back to the model when `decision` is `block`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// Event-specific output; must carry `hookEventName`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hook_specific_output: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HookDecision {
    Approve,
    Block,
}

impl HookOutput {
    /// No opinion — the CLI proceeds as if the hook printed nothing.
    pub fn allow() -> Self { Self::default() }

    /// Block the action; `reason` is shown to the model.
    pub fn block(reason: impl Into<String>) -> Self {
        Self { decision: Some(HookDecision::Block), reason: Some(reason.into()), ..Self::default() }
    }

    /// Inject `additionalContext` for the given event.
    pub fn with_context(event: HookEventKind, context: impl Into<String>) -> Self {
        Self {
            hook_specific_output: Some(serde_json::json!({
                "hookEventName": event,
                "additionalContext": context.into(),
            })),
            ..Self::default()
        }
    }

    /// Register `watchPaths` for FileChanged hooks (valid for CwdChanged / FileChanged).
    pub fn watch_paths(event: HookEventKind, paths: Vec<String>) -> Self {
        Self {
            hook_specific_output: Some(serde_json::json!({
                "hookEventName": event,
                "watchPaths": paths,
            })),
            ..Self::default()
        }
    }

    pub fn is_block(&self) -> bool { self.decision == Some(HookDecision::Block) }
}

/// Body of a `background_tasks` control request (Ctrl+B equivalent).
/// Send with `Session::control(&body)`.
/// Source: chunk-20bv8pca.js:20970–20978
#[derive(Debug, Clone, Serialize)]
pub struct BackgroundTasksRequest {
    pub subtype: &'static str, // "background_tasks"
    /// None = background all foreground tasks; Some = background specific tool_use_id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_use_id: Option<String>,
}

impl BackgroundTasksRequest {
    pub fn all() -> Self {
        Self { subtype: "background_tasks", tool_use_id: None }
    }
    pub fn one(tool_use_id: impl Into<String>) -> Self {
        Self { subtype: "background_tasks", tool_use_id: Some(tool_use_id.into()) }
    }
}

// ─── Inbound: CLI → host ─────────────────────────────────────────────────────

/// Every stdout line deserializes to this.
/// Source: chunk-20bv8pca.js (various schema locations)
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
pub enum InboundMessage {
    /// Per-turn metadata. First frame before each turn.
    #[serde(rename = "system")]
    System(SystemMessage),
    /// A user message: the echo of a host prompt (`is_replay`) or a tool result.
    #[serde(rename = "user")]
    User(InboundUserMessage),
    /// Complete assistant message block.
    #[serde(rename = "assistant")]
    Assistant(AssistantMessage),
    /// Partial streaming chunk (requires --include-partial-messages).
    #[serde(rename = "stream_event")]
    StreamEvent(StreamEventMessage),
    /// Turn complete signal.
    #[serde(rename = "result")]
    Result(ResultMessage),
    /// CLI asking the host to answer: a hook callback, a tool permission
    /// (`can_use_tool`) or an SDK MCP message. Source: chunk-20bv8pca.js:18214
    #[serde(rename = "control_request")]
    ControlRequest(ControlRequest),
    /// CLI answering a host-initiated control request (initialize, interrupt, …).
    /// Source: query.py:180–192
    #[serde(rename = "control_response")]
    ControlResponse(InboundControlResponse),
    /// Process keepalive with startup marker.
    /// Source: chunk-f89nsf5q.query.js:1403–1410
    #[serde(rename = "keep_alive")]
    KeepAlive(KeepAliveMessage),
    /// The CLI withdrew a control request it sent (e.g. a `can_use_tool`
    /// whose turn was interrupted); its answer is no longer wanted.
    #[serde(rename = "control_cancel_request")]
    ControlCancelRequest(ControlCancelRequest),
    /// Any other frame type (`rate_limit_event`, `tool_progress`,
    /// `prompt_suggestion`, `command_lifecycle`, …), kept verbatim.
    #[serde(skip_deserializing)]
    Other(serde_json::Value),
    /// Decoding placeholder; `Session` turns it into `Other`.
    #[serde(other)]
    Unknown,
}

/// Source: binary control_cancel_request schema
#[derive(Debug, Clone, Deserialize)]
pub struct ControlCancelRequest {
    pub request_id: String,
}

/// Every `type:"system"` frame: `init` (first frame of each turn) plus
/// `hook_started`, `hook_response`, `compact_boundary`, `model_fallback`,
/// `background_tasks_changed`, `task_started`, `task_updated`, `task_notification`,
/// `thinking_tokens`, `model_refusal_no_fallback`, `peer_message_hold`, … —
/// only `subtype` and `session_id` are common to all; subtype-specific fields
/// are in `extra`. Source: binary system-message schemas
/// Source: chunk-hp1k7mph.js:194 (WGt function), chunk-20bv8pca.js:15125
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemMessage {
    pub subtype: String, // "init" | "hook_started" | "hook_response" | …
    pub session_id: String,
    #[serde(default)]
    pub cwd: String,
    #[serde(default)]
    pub model: String,
    #[serde(default)]
    pub claude_code_version: String,
    #[serde(default)]
    pub permission_mode: String,
    #[serde(default)]
    pub tools: Vec<String>,
    #[serde(default)]
    pub betas: Vec<String>,
    #[serde(default)]
    pub capabilities: Vec<String>,
    #[serde(default)]
    pub skills: Vec<serde_json::Value>,
    #[serde(default)]
    pub plugins: Vec<serde_json::Value>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl SystemMessage {
    /// `REPL` or `Eval` in `tools[]` = Code Mode active (see `SessionOptions::code_mode`).
    pub fn code_mode_active(&self) -> bool {
        self.tools.iter().any(|t| t == "REPL" || t == "Eval")
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct AssistantMessage {
    pub message: AssistantMessageBody,
    #[serde(default)]
    pub session_id: Option<String>,
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct AssistantMessageBody {
    pub role: String,
    pub content: Vec<AssistantBlock>,
    #[serde(default)]
    pub stop_reason: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
pub enum AssistantBlock {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "tool_use")]
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
    #[serde(rename = "thinking")]
    Thinking { thinking: String },
    /// Encrypted thinking; opaque to the host.
    #[serde(rename = "redacted_thinking")]
    RedactedThinking { data: String },
    /// A tool the API runs itself (web search, …).
    #[serde(rename = "server_tool_use")]
    ServerToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
    #[serde(rename = "web_search_tool_result")]
    WebSearchToolResult {
        tool_use_id: String,
        content: serde_json::Value,
    },
    #[serde(other)]
    Unknown,
}

/// A `user` frame: the echo of a prompt, tool results, or a message the
/// CLI added itself (`is_synthetic`).
#[derive(Debug, Clone, Deserialize)]
pub struct InboundUserMessage {
    #[serde(default)]
    pub uuid: Option<String>,
    #[serde(default, rename = "isReplay")]
    pub is_replay: Option<bool>,
    #[serde(default, rename = "isSynthetic")]
    pub is_synthetic: Option<bool>,
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
    /// Subagent that produced the frame.
    #[serde(default)]
    pub agent_id: Option<String>,
    #[serde(default)]
    pub message: Option<InboundUserBody>,
    /// The tool's full structured output (e.g. Bash `{stdout, stderr, …}`),
    /// beside the text sent to the model.
    #[serde(default)]
    pub tool_use_result: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct InboundUserBody {
    /// A string or an array of content blocks.
    #[serde(default)]
    pub content: serde_json::Value,
}

/// One `tool_result` block of a `user` frame.
#[derive(Debug, Clone, Deserialize)]
pub struct InboundToolResult {
    pub tool_use_id: String,
    /// A string or an array of content blocks.
    #[serde(default)]
    pub content: serde_json::Value,
    #[serde(default)]
    pub is_error: bool,
}

impl InboundUserMessage {
    /// The `tool_result` blocks of this frame.
    pub fn tool_results(&self) -> Vec<InboundToolResult> {
        let Some(blocks) = self.message.as_ref().and_then(|m| m.content.as_array()) else {
            return vec![];
        };
        blocks
            .iter()
            .filter(|b| b.get("type").and_then(|t| t.as_str()) == Some("tool_result"))
            .filter_map(|b| serde_json::from_value(b.clone()).ok())
            .collect()
    }
}

/// Partial streaming chunk.
/// Source: chunk-20bv8pca.js:15343–15401
#[derive(Debug, Clone, Deserialize)]
pub struct StreamEventMessage {
    pub event: serde_json::Value,
    #[serde(default)]
    pub session_id: Option<String>,
    #[serde(default)]
    pub ttft_ms: Option<u64>,
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
}

/// Turn complete.
/// Source: chunk-20bv8pca.js:14425–14913
#[derive(Debug, Clone, Default, Deserialize)]
pub struct ResultMessage {
    /// "success" | "error_*". A failed API call still comes as "success"
    /// with `is_error: true` and the message in `result`.
    #[serde(default)]
    pub subtype: String,
    #[serde(default)]
    pub is_error: bool,
    /// Final text of the turn, or the error message when `is_error`.
    #[serde(default)]
    pub result: Option<String>,
    /// Error details of `error_*` subtypes.
    #[serde(default)]
    pub errors: Option<Vec<serde_json::Value>>,
    #[serde(default)]
    pub session_id: Option<String>,
    #[serde(default)]
    pub total_cost_usd: Option<f64>,
    #[serde(default)]
    pub ttft_ms: Option<u64>,
    #[serde(default)]
    pub duration_ms: Option<u64>,
    #[serde(default)]
    pub usage: Option<UsageSummary>,
    #[serde(default)]
    pub stop_reason: Option<String>,
    #[serde(default)]
    pub duration_api_ms: Option<u64>,
    #[serde(default)]
    pub num_turns: Option<u64>,
    /// Why the loop ended ("completed", …).
    #[serde(default)]
    pub terminal_reason: Option<String>,
    /// Output matching `Initialize::json_schema`.
    #[serde(default)]
    pub structured_output: Option<serde_json::Value>,
    /// Tool calls the permission system denied during the turn.
    #[serde(default)]
    pub permission_denials: Vec<serde_json::Value>,
    /// Per-model token and cost breakdown.
    #[serde(default, rename = "modelUsage")]
    pub model_usage: Option<serde_json::Value>,
    /// The prompts (`UserMessage::uuid`) this turn answered.
    #[serde(default)]
    pub user_message_uuids: Vec<String>,
    /// Set when the CLI started the turn itself (e.g. `{"kind":"task-notification"}`
    /// after a background task finished); absent for turns the host prompted.
    #[serde(default)]
    pub origin: Option<serde_json::Value>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl ResultMessage {
    pub fn is_success(&self) -> bool {
        self.subtype == "success" && !self.is_error
    }

    /// Whether this turn answered the prompt `uuid`. A prompt the CLI
    /// refuses up front (e.g. budget spent) gets a result but no echo.
    pub fn answers(&self, uuid: &str) -> bool {
        self.user_message_uuids.iter().any(|u| u.eq_ignore_ascii_case(uuid))
    }

    /// Category of a failed turn.
    pub fn error_kind(&self) -> TurnErrorKind {
        match self.subtype.as_str() {
            "error_during_execution" => TurnErrorKind::Execution,
            "error_max_turns" => TurnErrorKind::MaxTurns,
            "error_max_budget_usd" => TurnErrorKind::MaxBudget,
            "error_max_structured_output_retries" => TurnErrorKind::StructuredOutputRetries,
            "success" => TurnErrorKind::Api,
            other => TurnErrorKind::Other(other.to_string()),
        }
    }

    /// Why the turn failed: for `is_error` the CLI's message; for
    /// `error_*` subtypes the subtype, then its `errors` details.
    pub fn error_message(&self) -> String {
        if let Some(r) = self.result.as_ref().filter(|r| self.is_error && !r.is_empty()) {
            return r.clone();
        }
        let details: Vec<String> = self.errors.iter().flatten()
            .map(|v| v.as_str().map_or_else(|| v.to_string(), str::to_owned))
            .collect();
        if details.is_empty() { self.subtype.clone() } else { format!("{}: {}", self.subtype, details.join("; ")) }
    }
}

/// Why a turn failed. Source: binary result error-subtype enum
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TurnErrorKind {
    /// `error_during_execution`: includes interrupted turns.
    Execution,
    /// `error_max_turns`: `SessionOptions::max_turns` reached.
    MaxTurns,
    /// `error_max_budget_usd`: `SessionOptions::max_budget_usd` reached.
    MaxBudget,
    /// `error_max_structured_output_retries`.
    StructuredOutputRetries,
    /// The API call failed (`is_error` on a `success` result).
    Api,
    /// The CLI exited before the turn ended.
    SessionClosed,
    /// The prompt was withdrawn before it ran (`Session::cancel_prompt`,
    /// `Session::interrupt_all`).
    Cancelled,
    /// A subtype this SDK does not know.
    Other(String),
}

/// Token usage breakdown.
/// Source: chunk-20bv8pca.js:11526–11530
#[derive(Debug, Clone, Deserialize, Default)]
pub struct UsageSummary {
    #[serde(default)]
    pub input_tokens: u64,
    #[serde(default)]
    pub output_tokens: u64,
    #[serde(default)]
    pub cache_read_input_tokens: Option<u64>,
    #[serde(default)]
    pub cache_creation_input_tokens: Option<u64>,
    #[serde(default)]
    pub cache_creation: Option<serde_json::Value>,
}

/// CLI → host control request envelope:
/// `{"type":"control_request","request_id":"…","request":{"subtype":"…",…}}`.
/// Source: query.py:236–240
#[derive(Debug, Clone, Deserialize)]
pub struct ControlRequest {
    pub request_id: String,
    pub request: ControlRequestBody,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "subtype")]
pub enum ControlRequestBody {
    /// Only sent when the CLI runs with `--permission-prompt-tool stdio`.
    #[serde(rename = "can_use_tool")]
    CanUseTool(CanUseToolRequest),
    #[serde(rename = "hook_callback")]
    HookCallback(HookCallbackRequest),
    /// JSON-RPC message for an SDK MCP server. Source: query.py:305–320
    #[serde(rename = "mcp_message")]
    McpMessage(McpMessageRequest),
    /// An MCP server asks the user for input.
    #[serde(rename = "elicitation")]
    Elicitation(ElicitationRequest),
    /// A tool asks the host to render a blocking dialog; sent only for kinds
    /// listed in `Initialize::supported_dialog_kinds`.
    #[serde(rename = "request_user_dialog")]
    RequestUserDialog(UserDialogRequest),
    /// Future subtypes — answered with an error response.
    #[serde(other)]
    Unsupported,
}

/// Source: binary can_use_tool schema; query.py:244–262
#[derive(Debug, Clone, Deserialize)]
pub struct CanUseToolRequest {
    pub tool_name: String,
    pub input: serde_json::Value,
    #[serde(default)]
    pub tool_use_id: Option<String>,
    /// Permission updates that would stop the CLI asking again; return them
    /// with `ToolDecision::allow_and_remember` for "always allow".
    #[serde(default)]
    pub permission_suggestions: Option<Vec<serde_json::Value>>,
    /// Human-readable reason the CLI asks; may carry ANSI escapes.
    #[serde(default)]
    pub decision_reason: Option<String>,
    /// Why the ask escalated: "rule", "hook", "safetyCheck", "classifier", …
    #[serde(default)]
    pub decision_reason_type: Option<String>,
    /// MCP server of the tool: `{"name", "source"}`.
    #[serde(default)]
    pub mcp_server: Option<serde_json::Value>,
    /// Path outside the allowed directories that triggered the ask.
    #[serde(default)]
    pub blocked_path: Option<String>,
    #[serde(default)]
    pub classifier_approvable: Option<bool>,
    /// Do not offer "always allow" for this ask.
    #[serde(default)]
    pub suppress_always_allow_rule: Option<bool>,
    /// The ask must not be approvable by a single stray keystroke.
    #[serde(default)]
    pub default_to_no: Option<bool>,
    #[serde(default)]
    pub matched_ask_rule: Option<serde_json::Value>,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    /// Subagent that wants to use the tool.
    #[serde(default)]
    pub agent_id: Option<String>,
    #[serde(default)]
    pub requires_user_interaction: Option<bool>,
    /// Fields not named above.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Source: binary elicitation schema
#[derive(Debug, Clone, Deserialize)]
pub struct ElicitationRequest {
    pub mcp_server_name: String,
    pub message: String,
    /// "form" or "url".
    #[serde(default)]
    pub mode: Option<String>,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub elicitation_id: Option<String>,
    /// JSON schema of the requested form fields.
    #[serde(default)]
    pub requested_schema: Option<serde_json::Value>,
    /// Top-level keys of `requested_schema.properties`, in the server's order.
    #[serde(default)]
    pub property_order: Option<Vec<String>>,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Answer to an `elicitation` request. Source: binary elicitation response schema
#[derive(Debug, Clone, Serialize)]
pub struct ElicitationResponse {
    pub action: ElicitationAction,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ElicitationAction {
    Accept,
    Decline,
    Cancel,
}

impl ElicitationResponse {
    pub fn accept(content: serde_json::Map<String, serde_json::Value>) -> Self {
        Self { action: ElicitationAction::Accept, content: Some(content) }
    }
    pub fn decline() -> Self {
        Self { action: ElicitationAction::Decline, content: None }
    }
    pub fn cancel() -> Self {
        Self { action: ElicitationAction::Cancel, content: None }
    }
}

/// Source: binary request_user_dialog schema
#[derive(Debug, Clone, Deserialize)]
pub struct UserDialogRequest {
    pub dialog_kind: String,
    #[serde(default)]
    pub payload: serde_json::Value,
    #[serde(default)]
    pub tool_use_id: Option<String>,
}

/// The user's answer to a `request_user_dialog`.
/// Source: binary request_user_dialog response schema (behavior, result)
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct UserDialogResponse {
    pub behavior: UserDialogBehavior,
    /// Dialog-specific choice, e.g. `"retry_fallback"` for
    /// `refusal_fallback_prompt`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum UserDialogBehavior {
    Completed,
    /// The user dismissed the dialog.
    Cancelled,
}

impl UserDialogResponse {
    pub fn completed(result: impl Into<serde_json::Value>) -> Self {
        Self { behavior: UserDialogBehavior::Completed, result: Some(result.into()) }
    }
    pub fn cancelled() -> Self {
        Self { behavior: UserDialogBehavior::Cancelled, result: None }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct McpMessageRequest {
    pub server_name: String,
    pub message: serde_json::Value,
}

/// Source: binary hook_callback schema (callback_id, input, tool_use_id, issued_at)
#[derive(Debug, Clone, Deserialize)]
pub struct HookCallbackRequest {
    pub callback_id: String,
    pub input: HookEventPayload,
    #[serde(default)]
    pub tool_use_id: Option<String>,
    /// Epoch ms on the CLI's clock when the request was issued.
    #[serde(default)]
    pub issued_at: Option<u64>,
}

/// CLI → host answer to a host-initiated control request.
/// Source: query.py:180–192
#[derive(Debug, Clone, Deserialize)]
pub struct InboundControlResponse {
    pub response: InboundControlResponseBody,
}

#[derive(Debug, Clone, Deserialize)]
pub struct InboundControlResponseBody {
    pub subtype: String, // "success" | "error"
    pub request_id: String,
    #[serde(default)]
    pub response: Option<serde_json::Value>,
    #[serde(default)]
    pub error: Option<String>,
}

/// Input to a hook handler. Contains all event-specific fields.
/// Source: chunk-0msz7hbn.js:7–323
#[derive(Debug, Clone, Deserialize)]
pub struct HookEventPayload {
    pub hook_event_name: HookEventKind,
    #[serde(default)]
    pub tool_name: Option<String>,
    #[serde(default)]
    pub tool_input: Option<serde_json::Value>,
    #[serde(default)]
    pub tool_use_id: Option<String>,
    #[serde(default)]
    pub error: Option<String>,
    #[serde(default)]
    pub is_interrupt: Option<bool>,
    #[serde(default)]
    pub session_id: Option<String>,
    #[serde(default)]
    pub cwd: Option<String>,
    #[serde(default)]
    pub file_path: Option<String>,
    #[serde(default)]
    pub event: Option<String>,        // for FileChanged: "change"|"add"|"unlink"
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// One entry of a `background_tasks_changed` snapshot.
/// Source: binary background_tasks_changed schema
#[derive(Debug, Clone, Deserialize)]
pub struct BackgroundTask {
    pub task_id: String,
    #[serde(default)]
    pub run_id: Option<String>,
    /// "local_bash" | "local_agent" | "local_workflow" | …
    #[serde(default)]
    pub task_type: String,
    #[serde(default)]
    pub subagent_type: Option<String>,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub parent_task_id: Option<String>,
    #[serde(default)]
    pub ambient: Option<bool>,
    #[serde(default)]
    pub awaited: Option<bool>,
    #[serde(default)]
    pub off_host: Option<bool>,
}

/// Process keepalive with startup marker for readiness probing.
/// Source: chunk-f89nsf5q.query.js:1403–1410
#[derive(Debug, Clone, Deserialize)]
pub struct KeepAliveMessage {
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ─── Tool/Hook types ─────────────────────────────────────────────────────────

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolDef {
    pub name: String,
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_schema: Option<serde_json::Value>,
    /// Display name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// JSON schema of `ToolResult::structured`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_schema: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<ToolAnnotations>,
}

/// MCP tool hints. Source: MCP 2025-06-18 `ToolAnnotations`
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolAnnotations {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub read_only_hint: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub destructive_hint: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotent_hint: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub open_world_hint: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpManifest {
    pub name: String,
    pub tools: Vec<ToolDef>,
    #[serde(default)]
    pub server_info: Option<serde_json::Value>,
}

/// All 33 hook event kinds.
/// Source: chunk-20bv8pca.js:12053–12087 (N2 array)
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum HookEventKind {
    PreToolUse,
    PostToolUse,
    PostToolUseFailure,
    PostToolBatch,
    Notification,
    UserPromptSubmit,
    UserPromptExpansion,
    SessionStart,
    SessionEnd,
    Stop,
    StopFailure,
    SubagentStart,
    SubagentStop,
    PreCompact,
    PostCompact,
    PreModelSwitch,
    PostModelSwitch,
    PermissionRequest,
    PermissionDenied,
    Setup,
    TeammateIdle,
    TaskCreated,
    TaskCompleted,
    Elicitation,
    ElicitationResult,
    ConfigChange,
    WorktreeCreate,
    WorktreeRemove,
    InstructionsLoaded,
    CwdChanged,
    FileChanged,
    DirectoryAdded,
    MessageDisplay,
}