claude-code-agent 0.7.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
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
//! Wire types for the Claude Code stream-json protocol.
//! Source: the Claude Code 2.1.329 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.329 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> {
    /// Always `"control_request"`.
    #[serde(rename = "type")]
    pub kind: &'static str, // "control_request"
    /// Id the CLI echoes in its `control_response`.
    pub request_id: &'a str,
    /// Request body; its `subtype` names the request.
    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 {
    /// Hook matchers per event; matches arrive as `hook_callback` requests.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<HashMap<HookEventKind, Vec<HookMatcherConfig>>>,
    /// `systemPrompt`: the custom system prompt slot, same as `--system-prompt`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt: Option<String>,
    /// `appendSystemPrompt`: appended to the system prompt.
    #[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 {
    /// `true` only after the user accepted the trust dialog.
    pub accepted: bool,
    /// Directory the user trusted.
    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 {
    /// Tool-name pattern the hook applies to; `None` matches every tool.
    pub matcher: Option<String>,
    /// Callback ids the CLI sends back in `hook_callback` requests.
    pub hook_callback_ids: Vec<String>,
    /// Hook timeout in seconds.
    #[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`: standard behavior; prompts for dangerous operations.
    Default,
    /// `acceptEdits`: auto-accepts file edits.
    AcceptEdits,
    /// `bypassPermissions`: bypasses all permission checks.
    BypassPermissions,
    /// `plan`: planning mode, no actual tool execution.
    Plan,
    /// `dontAsk`: never prompts; denies anything not pre-approved.
    DontAsk,
    /// `auto`: a model classifier approves or denies permission prompts.
    Auto,
}

impl PermissionMode {
    /// Wire value, e.g. `"acceptEdits"`.
    #[must_use]
    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`.
    Low,
    /// `medium`.
    Medium,
    /// `high`.
    High,
    /// `xhigh`.
    Xhigh,
    /// `max`.
    Max,
}

impl Effort {
    /// Wire value, e.g. `"xhigh"`.
    #[must_use]
    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`.
    Enabled,
    /// `adaptive`.
    Adaptive,
    /// `disabled`.
    Disabled,
}

impl Thinking {
    /// Wire value, e.g. `"adaptive"`.
    #[must_use]
    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`.
    Summarized,
    /// `omitted`.
    Omitted,
    /// `highlights`.
    Highlights,
}

impl ThinkingDisplay {
    /// Wire value, e.g. `"summarized"`.
    #[must_use]
    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 {
    /// CLI argument: `"auto"` or the token count.
    #[must_use]
    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 {
    /// CLI argument: `"host"` or `"none"`.
    #[must_use]
    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 {
    /// Always `"user"`.
    #[serde(rename = "type")]
    pub kind: String, // "user"
    /// Role and content blocks.
    pub message: UserMessageBody,
    /// Tool call this message belongs to; `None` for a top-level prompt.
    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,
    /// When the CLI takes the message in during a running turn; `None` = CLI default.
    #[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 {
    /// A plain-text prompt.
    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.
    #[must_use]
    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".
    #[must_use]
    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.
    #[must_use]
    pub fn client_composed(mut self) -> Self {
        self.client_composed = Some(true);
        self
    }

    /// Set when the prompt is taken in during a running turn.
    #[must_use]
    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.
    #[must_use]
    pub fn context_only(mut self) -> Self {
        self.should_query = Some(false);
        self
    }
}

/// Message body: `role` plus content blocks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMessageBody {
    /// Always `"user"`.
    pub role: String,
    /// Content blocks of the message.
    pub content: Vec<ContentBlock>,
}

/// Content block of a user message (Anthropic Messages API shape).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ContentBlock {
    /// Plain text.
    #[serde(rename = "text")]
    Text {
        /// The text.
        text: String,
    },
    /// An image.
    #[serde(rename = "image")]
    Image {
        /// Where the image data comes from.
        source: ImageSource,
    },
    /// A PDF or plain-text document.
    #[serde(rename = "document")]
    Document {
        /// Where the document data comes from.
        source: DocumentSource,
        /// Document title.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        title: Option<String>,
    },
    /// The result of a tool call, answering a `tool_use` block.
    #[serde(rename = "tool_result")]
    ToolResult {
        /// Id of the `tool_use` block answered.
        tool_use_id: String,
        /// Result content.
        content: Vec<ContentBlock>,
        /// `Some(true)`: the tool call failed.
        #[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, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DocumentSource {
    /// Base64-encoded data.
    Base64 {
        /// MIME type, e.g. `"application/pdf"`.
        media_type: String,
        /// Base64 data.
        data: String,
    },
    /// Plain text.
    Text {
        /// MIME type, e.g. `"text/plain"`.
        media_type: String,
        /// The text.
        data: String,
    },
    /// A URL the API fetches.
    Url {
        /// Document URL.
        url: String,
    },
}

/// Anthropic Messages API image source.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ImageSource {
    /// Base64-encoded data.
    Base64 {
        /// MIME type, e.g. `"image/png"`.
        media_type: String,
        /// Base64 data.
        data: String,
    },
    /// A URL the API fetches.
    Url {
        /// Image 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 {
    /// Always `"control_response"`.
    #[serde(rename = "type")]
    pub kind: &'static str, // "control_response"
    /// Success or error, keyed by `request_id`.
    pub response: ControlResponseBody,
}

/// Body of a `control_response`, tagged by `subtype`.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "subtype", rename_all = "lowercase")]
pub enum ControlResponseBody {
    /// The request succeeded.
    Success {
        /// Id of the request answered.
        request_id: String,
        /// Response payload.
        response: serde_json::Value,
    },
    /// The request failed.
    Error {
        /// Id of the request answered.
        request_id: String,
        /// Error message.
        error: String,
    },
}

impl ControlResponse {
    /// Successful answer to the request `request_id`.
    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 },
        }
    }
    /// Error answer to the request `request_id`.
    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, PartialEq, 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 {
        /// `updatedInput`: replacement tool input.
        #[serde(rename = "updatedInput", skip_serializing_if = "Option::is_none")]
        updated_input: Option<serde_json::Value>,
        /// `updatedPermissions`: permission updates to apply.
        #[serde(rename = "updatedPermissions", default, skip_serializing_if = "Option::is_none")]
        updated_permissions: Option<Vec<serde_json::Value>>,
    },
    /// Refuse the tool call.
    #[serde(rename = "deny")]
    Deny {
        /// Reason sent back for the denial.
        message: String,
        /// `Some(true)`: also interrupt the turn.
        #[serde(skip_serializing_if = "Option::is_none")]
        interrupt: Option<bool>,
    },
}

impl ToolDecision {
    /// Allow with the original input.
    #[must_use]
    pub fn allow() -> Self { Self::Allow { updated_input: None, updated_permissions: None } }
    /// Allow with a rewritten input.
    #[must_use]
    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").
    #[must_use]
    pub fn allow_and_remember(updates: Vec<serde_json::Value>) -> Self {
        Self::Allow { updated_input: None, updated_permissions: Some(updates) }
    }
    /// Deny with `message`.
    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, PartialEq, 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>,
    /// Hide stdout from the transcript (default: false).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suppress_output: Option<bool>,
    /// Message shown when `continue` is false.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<String>,
    /// `approve` or `block`.
    #[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>,
}

/// `decision` of a hook result.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HookDecision {
    /// `approve`.
    Approve,
    /// `block`: `reason` is fed back to the model.
    Block,
}

impl HookOutput {
    /// No opinion — the CLI proceeds as if the hook printed nothing.
    #[must_use]
    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`).
    #[must_use]
    pub fn watch_paths(event: HookEventKind, paths: impl IntoIterator<Item = impl Into<String>>) -> Self {
        let paths: Vec<String> = paths.into_iter().map(Into::into).collect();
        Self {
            hook_specific_output: Some(serde_json::json!({
                "hookEventName": event,
                "watchPaths": paths,
            })),
            ..Self::default()
        }
    }

    /// Whether `decision` is `block`.
    #[must_use]
    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 {
    /// Always `"background_tasks"`.
    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 {
    /// Background all foreground tasks.
    #[must_use]
    pub fn all() -> Self {
        Self { subtype: "background_tasks", tool_use_id: None }
    }
    /// Background the task of `tool_use_id`.
    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")]
#[non_exhaustive]
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` and delivers
    /// it only for a stdout line that is not JSON.
    #[doc(hidden)]
    #[serde(other)]
    Unknown,
}

/// Source: binary `control_cancel_request` schema
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ControlCancelRequest {
    /// Id of the withdrawn control request.
    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)]
#[non_exhaustive]
pub struct SystemMessage {
    /// `init`, `hook_started`, `compact_boundary`, …
    pub subtype: String, // "init" | "hook_started" | "hook_response" | …
    /// Session the frame belongs to.
    pub session_id: String,
    /// Working directory (`init`).
    #[serde(default)]
    pub cwd: String,
    /// Active model (`init`).
    #[serde(default)]
    pub model: String,
    /// CLI version (`init`).
    #[serde(default)]
    pub claude_code_version: String,
    /// Permission mode, as its wire value (`init`).
    #[serde(default)]
    pub permission_mode: String,
    /// Available tool names (`init`).
    #[serde(default)]
    pub tools: Vec<String>,
    /// Enabled API betas (`init`).
    #[serde(default)]
    pub betas: Vec<String>,
    /// Protocol capabilities the CLI supports, for feature detection (`init`).
    #[serde(default)]
    pub capabilities: Vec<String>,
    /// Loaded skills (`init`), kept verbatim.
    #[serde(default)]
    pub skills: Vec<serde_json::Value>,
    /// Loaded plugins (`init`), kept verbatim.
    #[serde(default)]
    pub plugins: Vec<serde_json::Value>,
    /// Subtype-specific fields.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

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

/// A complete `assistant` frame.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct AssistantMessage {
    /// The model message.
    pub message: AssistantMessageBody,
    /// Session the frame belongs to.
    #[serde(default)]
    pub session_id: Option<String>,
    /// Tool call (e.g. a subagent) that produced the message; `None` for the main thread.
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
}

/// Body of an `assistant` frame.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct AssistantMessageBody {
    /// Always `"assistant"`.
    pub role: String,
    /// Content blocks of the message.
    pub content: Vec<AssistantBlock>,
    /// Why the model stopped, e.g. `"end_turn"`, `"tool_use"`.
    #[serde(default)]
    pub stop_reason: Option<String>,
}

/// Content block of an assistant message.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum AssistantBlock {
    /// Text.
    #[serde(rename = "text")]
    Text {
        /// The text.
        text: String,
    },
    /// A tool call the CLI runs.
    #[serde(rename = "tool_use")]
    ToolUse {
        /// Tool-use id; the matching `tool_result` carries it.
        id: String,
        /// Tool name.
        name: String,
        /// Tool input.
        input: serde_json::Value,
    },
    /// Extended thinking.
    #[serde(rename = "thinking")]
    Thinking {
        /// The thinking text.
        thinking: String,
    },
    /// Encrypted thinking; opaque to the host.
    #[serde(rename = "redacted_thinking")]
    RedactedThinking {
        /// Encrypted thinking data.
        data: String,
    },
    /// A tool the API runs itself (web search, …).
    #[serde(rename = "server_tool_use")]
    ServerToolUse {
        /// Tool-use id.
        id: String,
        /// Server tool name.
        name: String,
        /// Tool input.
        input: serde_json::Value,
    },
    /// Result of a server-run web search.
    #[serde(rename = "web_search_tool_result")]
    WebSearchToolResult {
        /// Id of the `server_tool_use` block answered.
        tool_use_id: String,
        /// Search results, kept verbatim.
        content: serde_json::Value,
    },
    /// Any other block type.
    #[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)]
#[non_exhaustive]
pub struct InboundUserMessage {
    /// Message uuid; for an echo, the host prompt's `UserMessage::uuid`.
    #[serde(default)]
    pub uuid: Option<String>,
    /// `isReplay`: echo of a host prompt.
    #[serde(default, rename = "isReplay")]
    pub is_replay: Option<bool>,
    /// `isSynthetic`: the CLI added the message itself.
    #[serde(default, rename = "isSynthetic")]
    pub is_synthetic: Option<bool>,
    /// Tool call this message belongs to.
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
    /// Subagent that produced the frame.
    #[serde(default)]
    pub agent_id: Option<String>,
    /// Message body.
    #[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>,
}

/// Body of an inbound `user` frame.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
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)]
#[non_exhaustive]
pub struct InboundToolResult {
    /// Id of the `tool_use` block answered.
    pub tool_use_id: String,
    /// A string or an array of content blocks.
    #[serde(default)]
    pub content: serde_json::Value,
    /// The tool call failed.
    #[serde(default)]
    pub is_error: bool,
}

impl InboundUserMessage {
    /// The `tool_result` blocks of this frame.
    #[must_use]
    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| InboundToolResult::deserialize(b).ok())
            .collect()
    }
}

/// Partial streaming chunk.
/// Source: chunk-20bv8pca.js:15343–15401
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct StreamEventMessage {
    /// Raw Anthropic streaming event (`message_start`, `content_block_delta`, …).
    pub event: serde_json::Value,
    /// Session the frame belongs to.
    #[serde(default)]
    pub session_id: Option<String>,
    /// Time to first token, in ms.
    #[serde(default)]
    pub ttft_ms: Option<u64>,
    /// Tool call (e.g. a subagent) that produced the event; `None` for the main thread.
    #[serde(default)]
    pub parent_tool_use_id: Option<String>,
}

/// Turn complete.
/// Source: chunk-20bv8pca.js:14425–14913
#[derive(Debug, Clone, Default, Deserialize)]
#[non_exhaustive]
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,
    /// The turn failed; a failed API call sets this on a `success` result.
    #[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>>,
    /// Session the turn ran in.
    #[serde(default)]
    pub session_id: Option<String>,
    /// Estimated cost in USD, cumulative across the session's turns.
    #[serde(default)]
    pub total_cost_usd: Option<f64>,
    /// Time to first token, in ms.
    #[serde(default)]
    pub ttft_ms: Option<u64>,
    /// Wall-clock duration, in ms.
    #[serde(default)]
    pub duration_ms: Option<u64>,
    /// Token usage.
    #[serde(default)]
    pub usage: Option<UsageSummary>,
    /// Why the model stopped, e.g. `"end_turn"`.
    #[serde(default)]
    pub stop_reason: Option<String>,
    /// Time spent in API calls, in ms.
    #[serde(default)]
    pub duration_api_ms: Option<u64>,
    /// Number of agent-loop turns.
    #[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>,
    /// Fields not named above.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl ResultMessage {
    /// `success` subtype without `is_error`.
    #[must_use]
    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.
    #[must_use]
    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.
    #[must_use]
    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.
    #[must_use]
    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)]
#[non_exhaustive]
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)]
#[non_exhaustive]
pub struct UsageSummary {
    /// Input tokens.
    #[serde(default)]
    pub input_tokens: u64,
    /// Output tokens.
    #[serde(default)]
    pub output_tokens: u64,
    /// Input tokens read from the prompt cache.
    #[serde(default)]
    pub cache_read_input_tokens: Option<u64>,
    /// Input tokens written to the prompt cache.
    #[serde(default)]
    pub cache_creation_input_tokens: Option<u64>,
    /// Cache writes by TTL, kept verbatim.
    #[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)]
#[non_exhaustive]
pub struct ControlRequest {
    /// Id to answer with in `ControlResponse`.
    pub request_id: String,
    /// Request body, tagged by `subtype`.
    pub request: ControlRequestBody,
}

/// Body of a CLI → host control request, tagged by `subtype`.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "subtype")]
#[non_exhaustive]
pub enum ControlRequestBody {
    /// Only sent when the CLI runs with `--permission-prompt-tool stdio`.
    #[serde(rename = "can_use_tool")]
    CanUseTool(CanUseToolRequest),
    /// A hook matched; run the callback `callback_id`.
    #[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)]
#[non_exhaustive]
pub struct CanUseToolRequest {
    /// Tool the model wants to use.
    pub tool_name: String,
    /// Tool input.
    pub input: serde_json::Value,
    /// Tool-use id of the call.
    #[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>,
    /// Set when a safety check is involved: `false` = one needs manual approval.
    #[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>,
    /// User `permissions.ask` rule that forced this prompt: `{source, tool_name, rule_content}`.
    #[serde(default)]
    pub matched_ask_rule: Option<serde_json::Value>,
    /// Permission-display title.
    #[serde(default)]
    pub title: Option<String>,
    /// Short tool or server label for display.
    #[serde(default)]
    pub display_name: Option<String>,
    /// Permission-display subtitle.
    #[serde(default)]
    pub description: Option<String>,
    /// Subagent that wants to use the tool.
    #[serde(default)]
    pub agent_id: Option<String>,
    /// One-tap Approve/Deny must not be offered.
    #[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)]
#[non_exhaustive]
pub struct ElicitationRequest {
    /// MCP server asking for input.
    pub mcp_server_name: String,
    /// Text shown to the user.
    pub message: String,
    /// "form" or "url".
    #[serde(default)]
    pub mode: Option<String>,
    /// URL to open (`url` mode).
    #[serde(default)]
    pub url: Option<String>,
    /// Elicitation id from the MCP server.
    #[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>>,
    /// Permission-display title from the MCP server.
    #[serde(default)]
    pub title: Option<String>,
    /// Short tool or server label from the MCP server.
    #[serde(default)]
    pub display_name: Option<String>,
    /// Permission-display subtitle from the MCP server.
    #[serde(default)]
    pub description: Option<String>,
    /// Fields not named above.
    #[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 {
    /// `accept`, `decline` or `cancel`.
    pub action: ElicitationAction,
    /// Form values; sent with `accept`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<serde_json::Map<String, serde_json::Value>>,
}

/// What the user did with an elicitation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ElicitationAction {
    /// The user submitted the form.
    Accept,
    /// The user declined.
    Decline,
    /// The user dismissed the request.
    Cancel,
}

impl ElicitationResponse {
    /// Accept with the form values.
    #[must_use]
    pub fn accept(content: serde_json::Map<String, serde_json::Value>) -> Self {
        Self { action: ElicitationAction::Accept, content: Some(content) }
    }
    /// Decline.
    #[must_use]
    pub fn decline() -> Self {
        Self { action: ElicitationAction::Decline, content: None }
    }
    /// Cancel.
    #[must_use]
    pub fn cancel() -> Self {
        Self { action: ElicitationAction::Cancel, content: None }
    }
}

/// Source: binary `request_user_dialog` schema
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct UserDialogRequest {
    /// Which dialog to render; only kinds in `Initialize::supported_dialog_kinds`.
    pub dialog_kind: String,
    /// Dialog-specific data; shape set by `dialog_kind`.
    #[serde(default)]
    pub payload: serde_json::Value,
    /// Tool call that raised the dialog.
    #[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 {
    /// `completed` or `cancelled`.
    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>,
}

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

impl UserDialogResponse {
    /// The user chose `result`.
    pub fn completed(result: impl Into<serde_json::Value>) -> Self {
        Self { behavior: UserDialogBehavior::Completed, result: Some(result.into()) }
    }
    /// The user dismissed the dialog.
    #[must_use]
    pub fn cancelled() -> Self {
        Self { behavior: UserDialogBehavior::Cancelled, result: None }
    }
}

/// Body of an `mcp_message` request.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct McpMessageRequest {
    /// SDK MCP server the message is for.
    pub server_name: String,
    /// JSON-RPC message.
    pub message: serde_json::Value,
}

/// Source: binary `hook_callback` schema (`callback_id`, input, `tool_use_id`, `issued_at`)
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct HookCallbackRequest {
    /// Callback id from `HookMatcherConfig::hook_callback_ids`.
    pub callback_id: String,
    /// Hook input.
    pub input: HookEventPayload,
    /// Tool-use id, for tool events.
    #[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)]
#[non_exhaustive]
pub struct InboundControlResponse {
    /// Success or error, keyed by `request_id`.
    pub response: InboundControlResponseBody,
}

/// Body of an inbound `control_response`.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct InboundControlResponseBody {
    /// `success` or `error`.
    pub subtype: String, // "success" | "error"
    /// Id of the host request answered.
    pub request_id: String,
    /// Response payload on `success`.
    #[serde(default)]
    pub response: Option<serde_json::Value>,
    /// Error message on `error`.
    #[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)]
#[non_exhaustive]
pub struct HookEventPayload {
    /// Event that fired.
    pub hook_event_name: HookEventKind,
    /// Tool name, for tool events.
    #[serde(default)]
    pub tool_name: Option<String>,
    /// Tool input, for tool events.
    #[serde(default)]
    pub tool_input: Option<serde_json::Value>,
    /// Tool-use id, for tool events.
    #[serde(default)]
    pub tool_use_id: Option<String>,
    /// Error message, for `PostToolUseFailure`.
    #[serde(default)]
    pub error: Option<String>,
    /// Whether the failure came from an interrupt, for `PostToolUseFailure`.
    #[serde(default)]
    pub is_interrupt: Option<bool>,
    /// Session the event belongs to.
    #[serde(default)]
    pub session_id: Option<String>,
    /// Working directory.
    #[serde(default)]
    pub cwd: Option<String>,
    /// Changed file, for `FileChanged`.
    #[serde(default)]
    pub file_path: Option<String>,
    /// Change kind, for `FileChanged`.
    #[serde(default)]
    pub event: Option<String>,        // for FileChanged: "change"|"add"|"unlink"
    /// Fields not named above.
    #[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)]
#[non_exhaustive]
pub struct BackgroundTask {
    /// Task id.
    pub task_id: String,
    /// Id of one run of the task; a resumed task gets a new one.
    #[serde(default)]
    pub run_id: Option<String>,
    /// "`local_bash`" | "`local_agent`" | "`local_workflow`" | …
    #[serde(default)]
    pub task_type: String,
    /// Agent type of a `local_agent` task.
    #[serde(default)]
    pub subagent_type: Option<String>,
    /// Task description.
    #[serde(default)]
    pub description: String,
    /// Task id of the subagent that launched this task.
    #[serde(default)]
    pub parent_task_id: Option<String>,
    /// Not activity; exclude from activity indicators.
    #[serde(default)]
    pub ambient: Option<bool>,
    /// A resumed subagent run that the resuming tool call waits for.
    #[serde(default)]
    pub awaited: Option<bool>,
    /// The task's work goes on outside this process.
    #[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)]
#[non_exhaustive]
pub struct KeepAliveMessage {
    /// Frame fields, kept verbatim.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

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

/// A tool of an in-process SDK MCP server.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ToolDef {
    /// Tool name.
    pub name: String,
    /// Description shown to the model.
    pub description: String,
    /// JSON schema of the tool input.
    #[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>,
    /// MCP tool hints.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<ToolAnnotations>,
}

/// MCP tool hints. Source: MCP 2025-06-18 `ToolAnnotations`
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolAnnotations {
    /// `readOnlyHint`: the tool does not modify its environment.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub read_only_hint: Option<bool>,
    /// `destructiveHint`: the tool may perform destructive updates.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub destructive_hint: Option<bool>,
    /// `idempotentHint`: repeated calls with the same arguments have no extra effect.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotent_hint: Option<bool>,
    /// `openWorldHint`: the tool interacts with external entities.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub open_world_hint: Option<bool>,
}

/// An MCP server's name and tools.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpManifest {
    /// Server name.
    pub name: String,
    /// Server tools.
    pub tools: Vec<ToolDef>,
    /// Wire field `server_info`, kept verbatim.
    #[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, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
#[non_exhaustive]
pub enum HookEventKind {
    /// Before tool execution.
    PreToolUse,
    /// After tool execution.
    PostToolUse,
    /// After tool execution fails.
    PostToolUseFailure,
    /// After a batch of tool calls resolves.
    PostToolBatch,
    /// When notifications are sent.
    Notification,
    /// When the user submits a prompt.
    UserPromptSubmit,
    /// When a user-typed slash command expands into a prompt.
    UserPromptExpansion,
    /// When a new session is started.
    SessionStart,
    /// When a session is ending.
    SessionEnd,
    /// Right before Claude concludes its response.
    Stop,
    /// When the turn ends due to an API error.
    StopFailure,
    /// When a subagent (Agent tool call) is started.
    SubagentStart,
    /// Right before a subagent (Agent tool call) concludes its response.
    SubagentStop,
    /// Before conversation compaction.
    PreCompact,
    /// After conversation compaction.
    PostCompact,
    /// Before a requested model switch (`/model`, model picker, `set_model`).
    PreModelSwitch,
    /// After the session model changes (any cause).
    PostModelSwitch,
    /// When a permission dialog is displayed.
    PermissionRequest,
    /// After auto mode denies a tool call.
    PermissionDenied,
    /// Repo setup hooks for init and maintenance.
    Setup,
    /// When a teammate is about to go idle.
    TeammateIdle,
    /// When a task is being created.
    TaskCreated,
    /// When a task is being marked as completed.
    TaskCompleted,
    /// When an MCP server requests user input (elicitation).
    Elicitation,
    /// After a user responds to an MCP elicitation.
    ElicitationResult,
    /// When configuration files change during a session.
    ConfigChange,
    /// Create an isolated worktree for VCS-agnostic isolation.
    WorktreeCreate,
    /// Remove a previously created worktree.
    WorktreeRemove,
    /// When an instruction file (CLAUDE.md or rule) is loaded.
    InstructionsLoaded,
    /// After the working directory changes.
    CwdChanged,
    /// When a watched file changes.
    FileChanged,
    /// After a working directory is added mid-session.
    DirectoryAdded,
    /// While assistant message text is displayed.
    MessageDisplay,
}