claude-api 0.5.3

Type-safe Rust client for the Anthropic API
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
//! Agents: full CRUD + version history.
//!
//! An agent defines how Claude behaves within a session: model
//! (optionally with `fast` inference speed), system prompt, MCP
//! servers, skills, tools (built-in toolset + MCP toolsets + custom
//! tools), description, metadata. Agents are versioned -- creating one
//! starts at version 1 and any [`Agents::update`] increments the
//! version. Sessions reference agents either at the latest version
//! (string ID) or pinned to a specific version (see
//! [`AgentRef`](super::sessions::AgentRef)).
//!
//! # Forward compatibility
//!
//! All wrapper enums in this module follow the
//! `Known | Other(serde_json::Value)` pattern. Brand-new server
//! variants -- a new permission policy, a new toolset shape, a new
//! skill type -- deserialize into `Other` preserving the raw JSON, so
//! upgrading the server without upgrading the SDK doesn't break parsing.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::client::Client;
use crate::error::Result;
use crate::pagination::Paginated;

use super::MANAGED_AGENTS_BETA;

// =====================================================================
// Model + speed
// =====================================================================

/// Inference speed mode. `Fast` charges premium pricing; not all models
/// support it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ModelSpeed {
    /// Default inference speed.
    Standard,
    /// Premium-priced fast inference.
    Fast,
}

/// Model identifier. Wire form is either a bare string or a
/// `{id, speed}` object.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum AgentModel {
    /// Bare model string. Use this for the common case where you don't
    /// need to override the inference speed.
    String(String),
    /// Object form with explicit speed.
    Config {
        /// Model ID (e.g. `claude-opus-4-7`).
        id: String,
        /// Inference speed.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        speed: Option<ModelSpeed>,
    },
}

impl AgentModel {
    /// Bare model string (latest speed default).
    #[must_use]
    pub fn id(id: impl Into<String>) -> Self {
        Self::String(id.into())
    }

    /// Model with an explicit speed override.
    #[must_use]
    pub fn config(id: impl Into<String>, speed: ModelSpeed) -> Self {
        Self::Config {
            id: id.into(),
            speed: Some(speed),
        }
    }

    /// Borrow the underlying model ID regardless of variant.
    #[must_use]
    pub fn model_id(&self) -> &str {
        match self {
            Self::String(s) => s,
            Self::Config { id, .. } => id,
        }
    }
}

impl From<&str> for AgentModel {
    fn from(s: &str) -> Self {
        Self::String(s.to_owned())
    }
}

impl From<String> for AgentModel {
    fn from(s: String) -> Self {
        Self::String(s)
    }
}

impl From<crate::types::ModelId> for AgentModel {
    fn from(m: crate::types::ModelId) -> Self {
        Self::String(m.as_str().to_owned())
    }
}

// =====================================================================
// MCP server
// =====================================================================

/// MCP server reference on an agent.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum AgentMcpServer {
    /// HTTP(S)-reachable MCP server.
    Url {
        /// Server name; referenced by `mcp_toolset` configs.
        name: String,
        /// Endpoint URL.
        url: String,
    },
}

impl AgentMcpServer {
    /// Build a URL-typed MCP server reference.
    #[must_use]
    pub fn url(name: impl Into<String>, url: impl Into<String>) -> Self {
        Self::Url {
            name: name.into(),
            url: url.into(),
        }
    }
}

// =====================================================================
// Permission policies
// =====================================================================

/// Permission policy controlling whether tool calls require user
/// confirmation. Forward-compatible: unknown policy types fall through
/// to [`Self::Other`].
#[derive(Debug, Clone, PartialEq)]
pub enum PermissionPolicy {
    /// Auto-approve every invocation.
    AlwaysAllow,
    /// Require a `user.tool_confirmation` event before each invocation.
    AlwaysAsk,
    /// Unknown policy type; raw JSON preserved.
    Other(serde_json::Value),
}

const KNOWN_PERMISSION_POLICY_TAGS: &[&str] = &["always_allow", "always_ask"];

impl Serialize for PermissionPolicy {
    fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        match self {
            Self::AlwaysAllow => {
                let mut map = s.serialize_map(Some(1))?;
                map.serialize_entry("type", "always_allow")?;
                map.end()
            }
            Self::AlwaysAsk => {
                let mut map = s.serialize_map(Some(1))?;
                map.serialize_entry("type", "always_ask")?;
                map.end()
            }
            Self::Other(v) => v.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for PermissionPolicy {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
        let raw = serde_json::Value::deserialize(d)?;
        let tag = raw.get("type").and_then(serde_json::Value::as_str);
        match tag {
            Some("always_allow") if KNOWN_PERMISSION_POLICY_TAGS.contains(&"always_allow") => {
                Ok(Self::AlwaysAllow)
            }
            Some("always_ask") => Ok(Self::AlwaysAsk),
            _ => Ok(Self::Other(raw)),
        }
    }
}

// =====================================================================
// Toolset configs
// =====================================================================

/// Built-in agent tool identifier.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BuiltinToolName {
    /// Shell.
    #[default]
    Bash,
    /// File edit.
    Edit,
    /// File read.
    Read,
    /// File write.
    Write,
    /// Glob match.
    Glob,
    /// Grep search.
    Grep,
    /// Fetch a URL.
    WebFetch,
    /// Web search.
    WebSearch,
}

/// Per-tool override on a built-in toolset.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BuiltinToolConfig {
    /// Tool identifier.
    pub name: BuiltinToolName,
    /// Whether this tool is enabled. Overrides the toolset default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Permission policy. Overrides the toolset default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub permission_policy: Option<PermissionPolicy>,
}

/// Per-tool override on an MCP toolset.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct McpToolConfig {
    /// MCP tool name.
    pub name: String,
    /// Whether this tool is enabled. Overrides the toolset default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Permission policy. Overrides the toolset default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub permission_policy: Option<PermissionPolicy>,
}

/// Default configuration for all tools in a toolset.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolsetDefaultConfig {
    /// Whether tools are enabled by default. Defaults to `true`
    /// server-side when omitted.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Default permission policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub permission_policy: Option<PermissionPolicy>,
}

/// JSON Schema for a custom tool's input parameters.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CustomToolInputSchema {
    /// JSON Schema `properties` map.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub properties: Option<serde_json::Value>,
    /// Required property names.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,
    /// Always `"object"`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(rename = "type")]
    pub ty: Option<String>,
}

/// Custom tool executed by the API client (not the agent). Calls
/// surface as `agent.custom_tool_use` events; respond with
/// `user.custom_tool_result`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CustomTool {
    /// Tool name. 1-128 chars; letters, digits, underscores, hyphens.
    pub name: String,
    /// Description shown to the agent. 1-1024 chars.
    pub description: String,
    /// Input JSON Schema.
    pub input_schema: CustomToolInputSchema,
}

// =====================================================================
// AgentTool wrapper
// =====================================================================

/// One tool entry on an agent.
///
/// Forward-compatible: unknown wire `type` tags fall through to
/// [`Self::Other`] preserving the raw JSON.
#[derive(Debug, Clone, PartialEq)]
pub enum AgentTool {
    /// Pre-built `agent_toolset_20260401` (bash / edit / read / write /
    /// glob / grep / `web_fetch` / `web_search`) with optional per-tool
    /// overrides and a default config.
    BuiltinToolset(BuiltinToolset),
    /// MCP toolset bound to a server name from the agent's
    /// `mcp_servers` array.
    McpToolset(McpToolset),
    /// Custom client-executed tool.
    Custom(CustomTool),
    /// Unknown tool kind; raw JSON preserved.
    Other(serde_json::Value),
}

/// Body of [`AgentTool::BuiltinToolset`].
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BuiltinToolset {
    /// Per-tool overrides.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub configs: Vec<BuiltinToolConfig>,
    /// Default config applied to tools not overridden in `configs`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_config: Option<ToolsetDefaultConfig>,
}

/// Body of [`AgentTool::McpToolset`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct McpToolset {
    /// MCP server name from the agent's `mcp_servers` array.
    pub mcp_server_name: String,
    /// Per-tool overrides.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub configs: Vec<McpToolConfig>,
    /// Default config applied to tools not overridden in `configs`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_config: Option<ToolsetDefaultConfig>,
}

const KNOWN_AGENT_TOOL_TAGS: &[&str] = &["agent_toolset_20260401", "mcp_toolset", "custom"];

impl Serialize for AgentTool {
    fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        match self {
            Self::BuiltinToolset(b) => {
                let mut map = s.serialize_map(None)?;
                map.serialize_entry("type", "agent_toolset_20260401")?;
                if !b.configs.is_empty() {
                    map.serialize_entry("configs", &b.configs)?;
                }
                if let Some(d) = &b.default_config {
                    map.serialize_entry("default_config", d)?;
                }
                map.end()
            }
            Self::McpToolset(m) => {
                let mut map = s.serialize_map(None)?;
                map.serialize_entry("type", "mcp_toolset")?;
                map.serialize_entry("mcp_server_name", &m.mcp_server_name)?;
                if !m.configs.is_empty() {
                    map.serialize_entry("configs", &m.configs)?;
                }
                if let Some(d) = &m.default_config {
                    map.serialize_entry("default_config", d)?;
                }
                map.end()
            }
            Self::Custom(c) => {
                let mut map = s.serialize_map(None)?;
                map.serialize_entry("type", "custom")?;
                map.serialize_entry("name", &c.name)?;
                map.serialize_entry("description", &c.description)?;
                map.serialize_entry("input_schema", &c.input_schema)?;
                map.end()
            }
            Self::Other(v) => v.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for AgentTool {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
        let raw = serde_json::Value::deserialize(d)?;
        let tag = raw.get("type").and_then(serde_json::Value::as_str);
        match tag {
            Some("agent_toolset_20260401")
                if KNOWN_AGENT_TOOL_TAGS.contains(&"agent_toolset_20260401") =>
            {
                let b = serde_json::from_value::<BuiltinToolset>(raw)
                    .map_err(serde::de::Error::custom)?;
                Ok(Self::BuiltinToolset(b))
            }
            Some("mcp_toolset") => {
                let m =
                    serde_json::from_value::<McpToolset>(raw).map_err(serde::de::Error::custom)?;
                Ok(Self::McpToolset(m))
            }
            Some("custom") => {
                let c =
                    serde_json::from_value::<CustomTool>(raw).map_err(serde::de::Error::custom)?;
                Ok(Self::Custom(c))
            }
            _ => Ok(Self::Other(raw)),
        }
    }
}

impl AgentTool {
    /// Enable the pre-built `agent_toolset_20260401` (no overrides).
    #[must_use]
    pub fn builtin_toolset() -> Self {
        Self::BuiltinToolset(BuiltinToolset::default())
    }

    /// Expose tools from a named MCP server.
    #[must_use]
    pub fn mcp_toolset(server_name: impl Into<String>) -> Self {
        Self::McpToolset(McpToolset {
            mcp_server_name: server_name.into(),
            configs: Vec::new(),
            default_config: None,
        })
    }

    /// Build a custom tool.
    #[must_use]
    pub fn custom(
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: CustomToolInputSchema,
    ) -> Self {
        Self::Custom(CustomTool {
            name: name.into(),
            description: description.into(),
            input_schema,
        })
    }
}

// =====================================================================
// Skills
// =====================================================================

/// A skill referenced on an agent. Forward-compatible: unknown skill
/// types fall through to [`Self::Other`].
#[derive(Debug, Clone, PartialEq)]
pub enum Skill {
    /// Anthropic-managed skill.
    Anthropic(AnthropicSkill),
    /// User-created custom skill.
    Custom(CustomSkill),
    /// Unknown skill type; raw JSON preserved.
    Other(serde_json::Value),
}

/// Body of [`Skill::Anthropic`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AnthropicSkill {
    /// Skill ID (e.g. `"xlsx"`).
    pub skill_id: String,
    /// Pinned version. Defaults to latest if omitted on requests; the
    /// resolved version is always echoed on responses.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

/// Body of [`Skill::Custom`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CustomSkill {
    /// Skill ID (e.g. `"skill_01XJ5..."`).
    pub skill_id: String,
    /// Pinned version. Defaults to latest if omitted.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

const KNOWN_SKILL_TAGS: &[&str] = &["anthropic", "custom"];

impl Serialize for Skill {
    fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        match self {
            Self::Anthropic(a) => {
                let mut map = s.serialize_map(None)?;
                map.serialize_entry("type", "anthropic")?;
                map.serialize_entry("skill_id", &a.skill_id)?;
                if let Some(v) = &a.version {
                    map.serialize_entry("version", v)?;
                }
                map.end()
            }
            Self::Custom(c) => {
                let mut map = s.serialize_map(None)?;
                map.serialize_entry("type", "custom")?;
                map.serialize_entry("skill_id", &c.skill_id)?;
                if let Some(v) = &c.version {
                    map.serialize_entry("version", v)?;
                }
                map.end()
            }
            Self::Other(v) => v.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for Skill {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
        let raw = serde_json::Value::deserialize(d)?;
        let tag = raw.get("type").and_then(serde_json::Value::as_str);
        match tag {
            Some("anthropic") if KNOWN_SKILL_TAGS.contains(&"anthropic") => {
                let a = serde_json::from_value::<AnthropicSkill>(raw)
                    .map_err(serde::de::Error::custom)?;
                Ok(Self::Anthropic(a))
            }
            Some("custom") => {
                let c =
                    serde_json::from_value::<CustomSkill>(raw).map_err(serde::de::Error::custom)?;
                Ok(Self::Custom(c))
            }
            _ => Ok(Self::Other(raw)),
        }
    }
}

impl Skill {
    /// Reference an Anthropic-managed skill (latest version).
    #[must_use]
    pub fn anthropic(skill_id: impl Into<String>) -> Self {
        Self::Anthropic(AnthropicSkill {
            skill_id: skill_id.into(),
            version: None,
        })
    }

    /// Reference an Anthropic-managed skill pinned to a version.
    #[must_use]
    pub fn anthropic_pinned(skill_id: impl Into<String>, version: impl Into<String>) -> Self {
        Self::Anthropic(AnthropicSkill {
            skill_id: skill_id.into(),
            version: Some(version.into()),
        })
    }

    /// Reference a user-created custom skill (latest version).
    #[must_use]
    pub fn custom(skill_id: impl Into<String>) -> Self {
        Self::Custom(CustomSkill {
            skill_id: skill_id.into(),
            version: None,
        })
    }
}

// =====================================================================
// Callable agents (multi-agent / threads)
// =====================================================================

/// Reference to another agent that this agent is permitted to call.
///
/// Used in [`CreateAgentRequest::callable_agents`] and
/// [`UpdateAgentRequest::callable_agents`] when configuring a
/// multi-agent coordinator. At runtime, when the coordinator delegates
/// to one of these, the platform spawns a new
/// [`Thread`](crate::managed_agents::threads::Thread).
///
/// Wire form is `{type: "agent", id, version}` -- the same shape as
/// the pinned [`AgentRef`](super::sessions::AgentRef).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CallableAgent {
    /// Always `"agent"`.
    #[serde(rename = "type")]
    pub ty: String,
    /// Agent ID.
    pub id: String,
    /// Pinned version. Required.
    pub version: u32,
}

impl CallableAgent {
    /// Build a callable-agent reference pinned to a version.
    #[must_use]
    pub fn new(id: impl Into<String>, version: u32) -> Self {
        Self {
            ty: "agent".into(),
            id: id.into(),
            version,
        }
    }
}

// =====================================================================
// Agent (response shape)
// =====================================================================

/// An agent definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Agent {
    /// Stable identifier (`agent_...`).
    pub id: String,
    /// Wire type tag (`"agent"`).
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub ty: Option<String>,
    /// Agent name.
    pub name: String,
    /// Description. May be `null` on the wire when no description was
    /// provided at create time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Model identifier and configuration.
    pub model: AgentModel,
    /// System prompt. May be `null` on the wire when no system prompt
    /// was provided at create time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,
    /// Configured MCP servers.
    #[serde(default)]
    pub mcp_servers: Vec<AgentMcpServer>,
    /// Configured skills.
    #[serde(default)]
    pub skills: Vec<Skill>,
    /// Configured tools.
    #[serde(default)]
    pub tools: Vec<AgentTool>,
    /// Free-form metadata.
    #[serde(default)]
    pub metadata: HashMap<String, String>,
    /// Other agents this agent is permitted to call (multi-agent
    /// coordinators only). Empty for non-coordinator agents.
    #[serde(default)]
    pub callable_agents: Vec<CallableAgent>,
    /// Current version. Starts at 1, increments on every update.
    pub version: u32,
    /// Creation timestamp (RFC3339).
    pub created_at: String,
    /// Last-modified timestamp (RFC3339).
    pub updated_at: String,
    /// Set when archived (RFC3339), `None` for live agents.
    #[serde(default)]
    pub archived_at: Option<String>,
}

// =====================================================================
// Create request
// =====================================================================

/// Request body for `POST /v1/agents`.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct CreateAgentRequest {
    /// Agent name. 1-256 chars.
    pub name: String,
    /// Model.
    pub model: AgentModel,
    /// Description. Up to 2048 chars.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// System prompt. Up to 100,000 chars.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,
    /// MCP servers. Max 20.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub mcp_servers: Vec<AgentMcpServer>,
    /// Skills. Max 20.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub skills: Vec<Skill>,
    /// Tools. Max 128 across all toolsets.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<AgentTool>,
    /// Metadata. Max 16 pairs (64-char keys, 512-char values).
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, String>,
    /// Other agents this agent is permitted to call. Setting this
    /// makes the agent a multi-agent coordinator: at runtime,
    /// delegations spawn new
    /// [`Thread`](crate::managed_agents::threads::Thread)s. Only one
    /// level of delegation is supported -- callable agents cannot
    /// themselves have callable agents.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub callable_agents: Vec<CallableAgent>,
}

impl CreateAgentRequest {
    /// Begin configuring a request.
    #[must_use]
    pub fn builder() -> CreateAgentRequestBuilder {
        CreateAgentRequestBuilder::default()
    }
}

/// Builder for [`CreateAgentRequest`].
#[derive(Debug, Default)]
pub struct CreateAgentRequestBuilder {
    name: Option<String>,
    model: Option<AgentModel>,
    description: Option<String>,
    system: Option<String>,
    mcp_servers: Vec<AgentMcpServer>,
    skills: Vec<Skill>,
    tools: Vec<AgentTool>,
    metadata: HashMap<String, String>,
    callable_agents: Vec<CallableAgent>,
}

impl CreateAgentRequestBuilder {
    /// Set the name. Required.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the model. Required.
    #[must_use]
    pub fn model(mut self, model: impl Into<AgentModel>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Set the description.
    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the system prompt.
    #[must_use]
    pub fn system(mut self, system: impl Into<String>) -> Self {
        self.system = Some(system.into());
        self
    }

    /// Append an MCP server.
    #[must_use]
    pub fn mcp_server(mut self, server: AgentMcpServer) -> Self {
        self.mcp_servers.push(server);
        self
    }

    /// Append a skill.
    #[must_use]
    pub fn skill(mut self, skill: Skill) -> Self {
        self.skills.push(skill);
        self
    }

    /// Append a tool.
    #[must_use]
    pub fn tool(mut self, tool: AgentTool) -> Self {
        self.tools.push(tool);
        self
    }

    /// Insert a metadata entry.
    #[must_use]
    pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Append a callable-agent reference (for multi-agent coordinators).
    #[must_use]
    pub fn callable_agent(mut self, callable: CallableAgent) -> Self {
        self.callable_agents.push(callable);
        self
    }

    /// Finalize.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidConfig`](crate::Error::InvalidConfig)
    /// if `name` or `model` was not set.
    pub fn build(self) -> Result<CreateAgentRequest> {
        let name = self
            .name
            .ok_or_else(|| crate::Error::InvalidConfig("name is required".into()))?;
        let model = self
            .model
            .ok_or_else(|| crate::Error::InvalidConfig("model is required".into()))?;
        Ok(CreateAgentRequest {
            name,
            model,
            description: self.description,
            system: self.system,
            mcp_servers: self.mcp_servers,
            skills: self.skills,
            tools: self.tools,
            metadata: self.metadata,
            callable_agents: self.callable_agents,
        })
    }
}

// =====================================================================
// Update request
// =====================================================================

/// Request body for `POST /v1/agents/{id}` (update).
///
/// **Optimistic concurrency**: pass the agent's current `version`. The
/// request is rejected if the server's stored version no longer
/// matches.
///
/// **Field semantics**:
/// - `Option::None` → omit the field → preserve the existing value.
/// - `Option::Some` with empty string / empty array / `null` → clear,
///   except for `name` and `model` which cannot be cleared.
/// - For `metadata`, see [`MetadataPatch`] for per-key delete semantics.
#[derive(Debug, Clone, Default, Serialize)]
#[non_exhaustive]
pub struct UpdateAgentRequest {
    /// Current version, used for optimistic concurrency. Required.
    pub version: u32,
    /// New name (cannot be cleared).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// New model (cannot be cleared).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<AgentModel>,
    /// New description. `Some("")` clears.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// New system prompt. `Some("")` clears.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,
    /// Replacement MCP-servers list. `Some(vec![])` clears.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcp_servers: Option<Vec<AgentMcpServer>>,
    /// Replacement skills list. `Some(vec![])` clears.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skills: Option<Vec<Skill>>,
    /// Replacement tools list. `Some(vec![])` clears.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<AgentTool>>,
    /// Per-key metadata patch. See [`MetadataPatch`].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<MetadataPatch>,
    /// Replacement callable-agents list. `Some(vec![])` clears (turns
    /// the agent back into a non-coordinator).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callable_agents: Option<Vec<CallableAgent>>,
}

impl UpdateAgentRequest {
    /// Build a minimal update request (pass `version`, then chain
    /// setters).
    #[must_use]
    pub fn at_version(version: u32) -> Self {
        Self {
            version,
            ..Self::default()
        }
    }

    /// Set the name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the model.
    #[must_use]
    pub fn model(mut self, model: impl Into<AgentModel>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Set or clear (`Some("")`) the description.
    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set or clear (`Some("")`) the system prompt.
    #[must_use]
    pub fn system(mut self, system: impl Into<String>) -> Self {
        self.system = Some(system.into());
        self
    }

    /// Replace the MCP servers list. Pass an empty Vec to clear.
    #[must_use]
    pub fn mcp_servers(mut self, servers: Vec<AgentMcpServer>) -> Self {
        self.mcp_servers = Some(servers);
        self
    }

    /// Replace the skills list. Pass an empty Vec to clear.
    #[must_use]
    pub fn skills(mut self, skills: Vec<Skill>) -> Self {
        self.skills = Some(skills);
        self
    }

    /// Replace the tools list. Pass an empty Vec to clear.
    #[must_use]
    pub fn tools(mut self, tools: Vec<AgentTool>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Apply a metadata patch.
    #[must_use]
    pub fn metadata(mut self, patch: MetadataPatch) -> Self {
        self.metadata = Some(patch);
        self
    }

    /// Replace the callable-agents list. Pass an empty Vec to clear.
    #[must_use]
    pub fn callable_agents(mut self, callable: Vec<CallableAgent>) -> Self {
        self.callable_agents = Some(callable);
        self
    }
}

/// Metadata patch on [`UpdateAgentRequest`]. Each entry is either a
/// `String` (upsert that key to the given value) or `None` (delete that
/// key). Keys not present in the patch are preserved.
#[derive(Debug, Clone, Default, Serialize)]
pub struct MetadataPatch(pub HashMap<String, Option<String>>);

impl MetadataPatch {
    /// Empty patch.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Upsert a key to a value.
    #[must_use]
    pub fn set(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.0.insert(key.into(), Some(value.into()));
        self
    }

    /// Delete a key.
    #[must_use]
    pub fn delete(mut self, key: impl Into<String>) -> Self {
        self.0.insert(key.into(), None);
        self
    }
}

// =====================================================================
// List params
// =====================================================================

/// Optional knobs for [`Agents::list`].
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ListAgentsParams {
    /// Created at or after this RFC3339 time.
    pub created_at_gte: Option<String>,
    /// Created at or before this RFC3339 time.
    pub created_at_lte: Option<String>,
    /// Include archived agents. Defaults to `false`.
    pub include_archived: Option<bool>,
    /// Page size. Default 20, max 100.
    pub limit: Option<u32>,
    /// Pagination cursor from a previous response's `next_page`.
    pub page: Option<String>,
}

impl ListAgentsParams {
    fn to_query(&self) -> Vec<(&'static str, String)> {
        let mut q = Vec::new();
        if let Some(t) = &self.created_at_gte {
            q.push(("created_at[gte]", t.clone()));
        }
        if let Some(t) = &self.created_at_lte {
            q.push(("created_at[lte]", t.clone()));
        }
        if let Some(b) = self.include_archived {
            q.push(("include_archived", b.to_string()));
        }
        if let Some(l) = self.limit {
            q.push(("limit", l.to_string()));
        }
        if let Some(p) = &self.page {
            q.push(("page", p.clone()));
        }
        q
    }
}

/// Optional knobs for [`Agents::list_versions`].
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ListAgentVersionsParams {
    /// Page size. Default 20, max 100.
    pub limit: Option<u32>,
    /// Pagination cursor.
    pub page: Option<String>,
}

impl ListAgentVersionsParams {
    fn to_query(&self) -> Vec<(&'static str, String)> {
        let mut q = Vec::new();
        if let Some(l) = self.limit {
            q.push(("limit", l.to_string()));
        }
        if let Some(p) = &self.page {
            q.push(("page", p.clone()));
        }
        q
    }
}

// =====================================================================
// Namespace handle
// =====================================================================

/// Namespace handle for the Agents API.
pub struct Agents<'a> {
    client: &'a Client,
}

impl<'a> Agents<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// `POST /v1/agents`.
    pub async fn create(&self, request: CreateAgentRequest) -> Result<Agent> {
        let body = &request;
        self.client
            .execute_with_retry(
                || {
                    self.client
                        .request_builder(reqwest::Method::POST, "/v1/agents")
                        .json(body)
                },
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }

    /// `GET /v1/agents/{id}`. Pass `version = Some(n)` to retrieve a
    /// specific historical version; `None` returns the latest.
    pub async fn retrieve(&self, agent_id: &str, version: Option<u32>) -> Result<Agent> {
        let path = format!("/v1/agents/{agent_id}");
        let v = version;
        self.client
            .execute_with_retry(
                || {
                    let mut req = self.client.request_builder(reqwest::Method::GET, &path);
                    if let Some(version) = v {
                        req = req.query(&[("version", version.to_string())]);
                    }
                    req
                },
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }

    /// `GET /v1/agents`.
    pub async fn list(&self, params: ListAgentsParams) -> Result<Paginated<Agent>> {
        let query = params.to_query();
        self.client
            .execute_with_retry(
                || {
                    let mut req = self
                        .client
                        .request_builder(reqwest::Method::GET, "/v1/agents");
                    for (k, v) in &query {
                        req = req.query(&[(k, v)]);
                    }
                    req
                },
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }

    /// `POST /v1/agents/{id}` (update). Bumps the version on success.
    /// Fails with HTTP 409 if `request.version` doesn't match the
    /// server's current version.
    pub async fn update(&self, agent_id: &str, request: UpdateAgentRequest) -> Result<Agent> {
        let path = format!("/v1/agents/{agent_id}");
        let body = &request;
        self.client
            .execute_with_retry(
                || {
                    self.client
                        .request_builder(reqwest::Method::POST, &path)
                        .json(body)
                },
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }

    /// `POST /v1/agents/{id}/archive`.
    pub async fn archive(&self, agent_id: &str) -> Result<Agent> {
        let path = format!("/v1/agents/{agent_id}/archive");
        self.client
            .execute_with_retry(
                || self.client.request_builder(reqwest::Method::POST, &path),
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }

    /// `GET /v1/agents/{id}/versions`. Returns the agent's full version
    /// history, newest first.
    pub async fn list_versions(
        &self,
        agent_id: &str,
        params: ListAgentVersionsParams,
    ) -> Result<Paginated<Agent>> {
        let path = format!("/v1/agents/{agent_id}/versions");
        let query = params.to_query();
        self.client
            .execute_with_retry(
                || {
                    let mut req = self.client.request_builder(reqwest::Method::GET, &path);
                    for (k, v) in &query {
                        req = req.query(&[(k, v)]);
                    }
                    req
                },
                &[MANAGED_AGENTS_BETA],
            )
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use serde_json::json;
    use wiremock::matchers::{body_partial_json, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn client_for(mock: &MockServer) -> Client {
        Client::builder()
            .api_key("sk-ant-test")
            .base_url(mock.uri())
            .build()
            .unwrap()
    }

    fn fake_agent_response() -> serde_json::Value {
        json!({
            "id": "agent_01",
            "type": "agent",
            "name": "Reviewer",
            "description": "",
            "model": "claude-opus-4-7",
            "system": "",
            "mcp_servers": [],
            "skills": [],
            "tools": [],
            "metadata": {},
            "version": 1,
            "created_at": "2026-04-30T12:00:00Z",
            "updated_at": "2026-04-30T12:00:00Z"
        })
    }

    #[test]
    fn agent_model_serializes_string_form_untagged() {
        let m = AgentModel::id("claude-opus-4-7");
        let v = serde_json::to_value(&m).unwrap();
        assert_eq!(v, json!("claude-opus-4-7"));
    }

    #[test]
    fn agent_model_serializes_config_form_with_speed() {
        let m = AgentModel::config("claude-opus-4-7", ModelSpeed::Fast);
        let v = serde_json::to_value(&m).unwrap();
        assert_eq!(v, json!({"id": "claude-opus-4-7", "speed": "fast"}));
        let parsed: AgentModel = serde_json::from_value(v).unwrap();
        assert_eq!(parsed, m);
    }

    #[test]
    fn permission_policy_round_trips_known_variants() {
        assert_eq!(
            serde_json::to_value(PermissionPolicy::AlwaysAllow).unwrap(),
            json!({"type": "always_allow"})
        );
        assert_eq!(
            serde_json::to_value(PermissionPolicy::AlwaysAsk).unwrap(),
            json!({"type": "always_ask"})
        );
    }

    #[test]
    fn permission_policy_unknown_variant_falls_to_other() {
        let raw = json!({"type": "future_policy", "x": 1});
        let parsed: PermissionPolicy = serde_json::from_value(raw.clone()).unwrap();
        match parsed {
            PermissionPolicy::Other(v) => assert_eq!(v, raw),
            PermissionPolicy::AlwaysAllow | PermissionPolicy::AlwaysAsk => panic!("expected Other"),
        }
    }

    #[test]
    fn agent_tool_builtin_toolset_serializes_with_configs() {
        let tool = AgentTool::BuiltinToolset(BuiltinToolset {
            configs: vec![BuiltinToolConfig {
                name: BuiltinToolName::Bash,
                enabled: Some(true),
                permission_policy: Some(PermissionPolicy::AlwaysAsk),
            }],
            default_config: Some(ToolsetDefaultConfig {
                enabled: Some(true),
                permission_policy: Some(PermissionPolicy::AlwaysAllow),
            }),
        });
        let v = serde_json::to_value(&tool).unwrap();
        assert_eq!(v["type"], "agent_toolset_20260401");
        assert_eq!(v["configs"][0]["name"], "bash");
        assert_eq!(v["configs"][0]["permission_policy"]["type"], "always_ask");
        assert_eq!(v["default_config"]["enabled"], true);
    }

    #[test]
    fn agent_tool_mcp_toolset_round_trips_with_server_name() {
        let tool = AgentTool::mcp_toolset("github");
        let v = serde_json::to_value(&tool).unwrap();
        assert_eq!(
            v,
            json!({"type": "mcp_toolset", "mcp_server_name": "github"})
        );
        let parsed: AgentTool = serde_json::from_value(v).unwrap();
        assert_eq!(parsed, tool);
    }

    #[test]
    fn agent_tool_custom_round_trips_with_input_schema() {
        let tool = AgentTool::custom(
            "lookup",
            "Find a record by id",
            CustomToolInputSchema {
                properties: Some(json!({"id": {"type": "string"}})),
                required: vec!["id".into()],
                ty: Some("object".into()),
            },
        );
        let v = serde_json::to_value(&tool).unwrap();
        assert_eq!(v["type"], "custom");
        assert_eq!(v["name"], "lookup");
        assert_eq!(v["input_schema"]["required"], json!(["id"]));
        let parsed: AgentTool = serde_json::from_value(v).unwrap();
        assert_eq!(parsed, tool);
    }

    #[test]
    fn agent_tool_unknown_kind_falls_to_other() {
        let raw = json!({"type": "future_tool", "x": 1});
        let parsed: AgentTool = serde_json::from_value(raw.clone()).unwrap();
        match parsed {
            AgentTool::Other(v) => assert_eq!(v, raw),
            AgentTool::BuiltinToolset(_) | AgentTool::McpToolset(_) | AgentTool::Custom(_) => {
                panic!("expected Other")
            }
        }
    }

    #[test]
    fn skill_round_trips_anthropic_and_custom_with_version() {
        let a = Skill::anthropic_pinned("xlsx", "1.2.3");
        let v = serde_json::to_value(&a).unwrap();
        assert_eq!(
            v,
            json!({"type": "anthropic", "skill_id": "xlsx", "version": "1.2.3"})
        );
        let parsed: Skill = serde_json::from_value(v).unwrap();
        assert_eq!(parsed, a);

        let c = Skill::custom("skill_01XJ5");
        let v = serde_json::to_value(&c).unwrap();
        assert_eq!(v, json!({"type": "custom", "skill_id": "skill_01XJ5"}));
    }

    #[test]
    fn skill_unknown_type_falls_to_other() {
        let raw = json!({"type": "future_skill", "blob": 1});
        let parsed: Skill = serde_json::from_value(raw.clone()).unwrap();
        match parsed {
            Skill::Other(v) => assert_eq!(v, raw),
            Skill::Anthropic(_) | Skill::Custom(_) => panic!("expected Other"),
        }
    }

    #[test]
    fn metadata_patch_serializes_set_and_delete() {
        let p = MetadataPatch::new().set("env", "prod").delete("legacy_key");
        let v = serde_json::to_value(&p).unwrap();
        assert_eq!(v["env"], "prod");
        assert_eq!(v["legacy_key"], serde_json::Value::Null);
    }

    #[tokio::test]
    async fn create_agent_posts_minimal_payload() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/agents"))
            .and(body_partial_json(json!({
                "name": "Reviewer",
                "model": "claude-opus-4-7"
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_agent_response()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateAgentRequest::builder()
            .name("Reviewer")
            .model("claude-opus-4-7")
            .build()
            .unwrap();
        let agent = client.managed_agents().agents().create(req).await.unwrap();
        assert_eq!(agent.id, "agent_01");
        assert_eq!(agent.version, 1);
    }

    #[tokio::test]
    async fn create_coordinator_agent_includes_callable_agents() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/agents"))
            .and(body_partial_json(json!({
                "name": "Engineering Lead",
                "model": "claude-opus-4-7",
                "callable_agents": [
                    {"type": "agent", "id": "agent_reviewer", "version": 2},
                    {"type": "agent", "id": "agent_test_writer", "version": 5}
                ]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json({
                let mut r = fake_agent_response();
                r["callable_agents"] = json!([
                    {"type": "agent", "id": "agent_reviewer", "version": 2},
                    {"type": "agent", "id": "agent_test_writer", "version": 5}
                ]);
                r
            }))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateAgentRequest::builder()
            .name("Engineering Lead")
            .model("claude-opus-4-7")
            .callable_agent(CallableAgent::new("agent_reviewer", 2))
            .callable_agent(CallableAgent::new("agent_test_writer", 5))
            .build()
            .unwrap();
        let agent = client.managed_agents().agents().create(req).await.unwrap();
        assert_eq!(agent.callable_agents.len(), 2);
        assert_eq!(agent.callable_agents[0].id, "agent_reviewer");
        assert_eq!(agent.callable_agents[0].version, 2);
    }

    #[test]
    fn callable_agent_serializes_with_type_tag() {
        let c = CallableAgent::new("agent_x", 3);
        let v = serde_json::to_value(&c).unwrap();
        assert_eq!(v, json!({"type": "agent", "id": "agent_x", "version": 3}));
    }

    #[tokio::test]
    async fn create_agent_full_payload_round_trips() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/agents"))
            .and(body_partial_json(json!({
                "name": "Reviewer",
                "model": {"id": "claude-opus-4-7", "speed": "fast"},
                "system": "Be helpful.",
                "description": "Code review assistant.",
                "mcp_servers": [
                    {"type": "url", "name": "github", "url": "https://api.githubcopilot.com/mcp/"}
                ],
                "tools": [
                    {"type": "agent_toolset_20260401"},
                    {"type": "mcp_toolset", "mcp_server_name": "github"}
                ],
                "skills": [{"type": "anthropic", "skill_id": "xlsx"}],
                "metadata": {"env": "prod"}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_agent_response()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = CreateAgentRequest::builder()
            .name("Reviewer")
            .model(AgentModel::config("claude-opus-4-7", ModelSpeed::Fast))
            .system("Be helpful.")
            .description("Code review assistant.")
            .mcp_server(AgentMcpServer::url(
                "github",
                "https://api.githubcopilot.com/mcp/",
            ))
            .tool(AgentTool::builtin_toolset())
            .tool(AgentTool::mcp_toolset("github"))
            .skill(Skill::anthropic("xlsx"))
            .metadata("env", "prod")
            .build()
            .unwrap();
        client.managed_agents().agents().create(req).await.unwrap();
    }

    #[tokio::test]
    async fn retrieve_agent_passes_version_query_when_supplied() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/agents/agent_01"))
            .and(wiremock::matchers::query_param("version", "3"))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_agent_response()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let _ = client
            .managed_agents()
            .agents()
            .retrieve("agent_01", Some(3))
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn list_agents_passes_created_at_brackets_in_query() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/agents"))
            .and(wiremock::matchers::query_param(
                "created_at[gte]",
                "2026-04-01T00:00:00Z",
            ))
            .and(wiremock::matchers::query_param("include_archived", "true"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "data": [fake_agent_response()],
                "next_page": null
            })))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let page = client
            .managed_agents()
            .agents()
            .list(ListAgentsParams {
                created_at_gte: Some("2026-04-01T00:00:00Z".into()),
                include_archived: Some(true),
                ..Default::default()
            })
            .await
            .unwrap();
        assert_eq!(page.data.len(), 1);
    }

    #[tokio::test]
    async fn update_agent_sends_version_for_optimistic_concurrency() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/agents/agent_01"))
            .and(body_partial_json(json!({
                "version": 1,
                "name": "Reviewer v2",
                "metadata": {"env": "staging", "old_key": null}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(fake_agent_response()))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let req = UpdateAgentRequest::at_version(1)
            .name("Reviewer v2")
            .metadata(MetadataPatch::new().set("env", "staging").delete("old_key"));
        client
            .managed_agents()
            .agents()
            .update("agent_01", req)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn archive_agent_posts_to_archive_subpath() {
        let mock = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/agents/agent_01/archive"))
            .respond_with(ResponseTemplate::new(200).set_body_json({
                let mut a = fake_agent_response();
                a["archived_at"] = json!("2026-04-30T12:00:00Z");
                a
            }))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let agent = client
            .managed_agents()
            .agents()
            .archive("agent_01")
            .await
            .unwrap();
        assert!(agent.archived_at.is_some());
    }

    #[tokio::test]
    async fn list_versions_returns_paginated_history() {
        let mock = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/agents/agent_01/versions"))
            .and(wiremock::matchers::query_param("limit", "5"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "data": [fake_agent_response()],
                "next_page": "cursor_x"
            })))
            .mount(&mock)
            .await;

        let client = client_for(&mock);
        let page = client
            .managed_agents()
            .agents()
            .list_versions(
                "agent_01",
                ListAgentVersionsParams {
                    limit: Some(5),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(page.data.len(), 1);
    }
}