aither-core 0.4.1

Core trait abstractions for aither
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
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
//! # LLM Tool Calling Framework
//!
//!
//! Type-safe tool calling system for Large Language Models. Enables LLMs to execute external
//! functions, access APIs, and interact with systems through well-defined interfaces.
//!
//! ## How a model calls a tool
//!
//! A [`Tool`] declares a name, a description, and an `Arguments` type whose
//! JSON schema is sent to the provider. When the model decides to use a tool it
//! emits a [`ToolCall`](crate::llm::ToolCall) event carrying the tool name and
//! arguments as JSON; this crate does not execute it. Executing the call and
//! feeding the result back is the job of a higher layer such as `aither-agent`,
//! which keeps tool execution under the caller's control.
//!
//! ## Core Components
//!
//! - [`Tool`] - Trait for defining executable tools
//! - [`Tools`] - Registry for managing multiple tools  
//! - [`tool::ToolDefinition`] - Metadata and schema for LLM consumption
//!
//! ## Quick Start
//!
//! ```rust
//! use aither_core::llm::{Tool, ToolResult};
//! use schemars::JsonSchema;
//! use serde::Deserialize;
//! use std::borrow::Cow;
//!
//! /// Performs basic math operations.
//! #[derive(JsonSchema, Deserialize)]
//! struct MathArgs {
//!     /// Operation: "add", "subtract", "multiply", "divide"
//!     operation: String,
//!     /// First number
//!     a: f64,
//!     /// Second number
//!     b: f64,
//! }
//!
//! struct Calculator;
//!
//! impl Tool for Calculator {
//!     fn name(&self) -> Cow<'static, str> {
//!         Cow::Borrowed("calculator")
//!     }
//!
//!     type Arguments = MathArgs;
//!     type Res = ToolResult;
//!
//!     async fn call(&self, args: Self::Arguments) -> aither_core::Result<Self::Res> {
//!         let result = match args.operation.as_str() {
//!             "add" => args.a + args.b,
//!             "subtract" => args.a - args.b,
//!             "multiply" => args.a * args.b,
//!             "divide" if args.b != 0.0 => args.a / args.b,
//!             "divide" => return Err(anyhow::Error::msg("Division by zero")),
//!             _ => return Err(anyhow::Error::msg("Unknown operation")),
//!         };
//!         Ok(ToolResult::text(result.to_string()))
//!     }
//! }
//! ```
//!
//! ## Schema Design Best Practices
//!
//! ### 1. Use Clear Documentation Comments
//! Doc comments automatically become schema descriptions:
//!
//! ```rust,ignore
//! use schemars::JsonSchema;
//! use serde::Deserialize;
//!
//! #[derive(JsonSchema, Deserialize)]
//! struct WeatherArgs {
//!     /// City name (e.g., "London", "Tokyo", "New York")
//!     city: String,
//!     /// Temperature unit: "celsius" or "fahrenheit"
//!     #[serde(default = "default_celsius")]
//!     unit: String,
//! }
//!
//! fn default_celsius() -> String { "celsius".to_string() }
//! ```
//!
//! ### 2. Prefer Enums Over Strings
//! Enums provide clear constraints for LLMs:
//!
//! ```rust,ignore
//! use schemars::JsonSchema;
//! use serde::Deserialize;
//!
//! #[derive(JsonSchema, Deserialize)]
//! enum Priority { Low, Medium, High, Critical }
//!
//! #[derive(JsonSchema, Deserialize)]  
//! struct TaskArgs {
//!     /// Task description
//!     description: String,
//!     /// Task priority level
//!     priority: Priority,
//! }
//! ```
//!
//! ### 3. Add Validation Constraints
//! Use schemars attributes for validation:
//!
//! ```rust,ignore
//! use schemars::JsonSchema;
//! use serde::Deserialize;
//!
//! #[derive(JsonSchema, Deserialize)]
//! struct UserArgs {
//!     /// Valid email address
//!     #[schemars(regex(pattern = "^[^@]+@[^@]+\\.[^@]+$"))]
//!     email: String,
//!     /// Age between 13 and 120
//!     #[schemars(range(min = 13, max = 120))]
//!     age: u8,
//!     /// Bio text, max 500 characters
//!     #[schemars(length(max = 500))]
//!     bio: Option<String>,
//! }
//! ```
//!
//! ### 4. Structure Complex Data
//! Break down complex parameters into nested types:
//!
//! ```rust,ignore
//! use schemars::JsonSchema;
//! use serde::Deserialize;
//!
//! #[derive(JsonSchema, Deserialize)]
//! struct Address {
//!     street: String,
//!     city: String,
//!     /// Two-letter country code (e.g., "US", "GB", "JP")
//!     country: String,
//! }
//!
//! #[derive(JsonSchema, Deserialize)]
//! struct CreateUserArgs {
//!     name: String,
//!     address: Address,
//!     /// List of user interests
//!     #[schemars(length(max = 10))]
//!     interests: Vec<String>,
//! }
//! ```
//!

// Re-export procedural macros
#[cfg(feature = "derive")]
pub use aither_derive::tool;
use alloc::borrow::Cow;
use serde_json::Value;

use crate::Result;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use alloc::{boxed::Box, collections::BTreeMap};
use core::any::Any;
use core::fmt::{Debug, Display};
use core::{future::Future, pin::Pin};
pub use mime::Mime;
use schemars::{JsonSchema, Schema, schema_for};
use serde::{Serialize, de::DeserializeOwned};

/// Final structured result from a tool execution.
///
/// This keeps successful content and tool-level failures in distinct variants so
/// UIs and runtimes do not need to infer errors from free-form strings.
///
/// # Example
///
/// ```rust,ignore
/// use aither::llm::tool::ToolResult;
///
/// fn search_tool() -> ToolResult {
///     ToolResult::text("Found 3 results...")
/// }
///
/// fn delete_tool() -> ToolResult {
///     ToolResult::Done
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
pub enum ToolResult {
    /// Tool completed with no output to return.
    Done,

    /// UTF-8 plain text output.
    Text {
        /// Plain text content.
        text: String,
    },

    /// Tab-separated tabular output.
    Tsv {
        /// TSV content.
        text: String,
    },

    /// Structured JSON output.
    Json {
        /// JSON content.
        value: Value,
    },

    /// Binary or media payload.
    Binary {
        /// MIME type of the payload (for example `image/png`).
        mime: String,
        /// Raw content bytes.
        content: Vec<u8>,
    },

    /// Tool-level error. This is distinct from transport/runtime failures.
    Error {
        /// Tool-level error message.
        message: String,
    },
}

impl ToolResult {
    /// Creates a plain text result.
    #[must_use]
    pub fn text(s: impl Into<String>) -> Self {
        Self::Text { text: s.into() }
    }

    /// Creates a TSV result.
    #[must_use]
    pub fn tsv(s: impl Into<String>) -> Self {
        Self::Tsv { text: s.into() }
    }

    /// Creates a JSON result from a serializable value.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails.
    pub fn json<T: Serialize>(value: &T) -> Result<Self> {
        Ok(Self::Json {
            value: serde_json::to_value(value)?,
        })
    }

    /// Creates a JSON result from an already-materialized JSON value.
    #[must_use]
    pub const fn json_value(value: Value) -> Self {
        Self::Json { value }
    }

    /// Creates an image result.
    #[must_use]
    pub fn image(data: Vec<u8>, media_type: &str) -> Self {
        Self::Binary {
            mime: parse_media_type_or_octet_stream(media_type),
            content: data,
        }
    }

    /// Creates a binary result.
    #[must_use]
    pub fn binary(data: Vec<u8>) -> Self {
        Self::Binary {
            mime: mime::APPLICATION_OCTET_STREAM.essence_str().to_string(),
            content: data,
        }
    }

    /// Creates a typed tool error result.
    #[must_use]
    pub fn error(message: impl Into<String>) -> Self {
        Self::Error {
            message: message.into(),
        }
    }

    /// Returns `true` if this is a `Done` variant.
    #[must_use]
    pub const fn is_done(&self) -> bool {
        matches!(self, Self::Done)
    }

    /// Returns `true` if this is a typed tool error.
    #[must_use]
    pub const fn is_error(&self) -> bool {
        matches!(self, Self::Error { .. })
    }

    /// Returns plain textual content for text-like variants.
    #[must_use]
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Self::Text { text } | Self::Tsv { text } => Some(text),
            Self::Error { message } => Some(message),
            Self::Done | Self::Json { .. } | Self::Binary { .. } => None,
        }
    }

    /// Returns the error message if this is a typed tool error.
    #[must_use]
    pub fn error_message(&self) -> Option<&str> {
        match self {
            Self::Error { message } => Some(message),
            Self::Done
            | Self::Text { .. }
            | Self::Tsv { .. }
            | Self::Json { .. }
            | Self::Binary { .. } => None,
        }
    }

    /// Projects the result into a textual representation safe to re-inject into model context.
    ///
    /// # Errors
    ///
    /// Returns an error if JSON serialization fails.
    pub fn render_for_model(&self) -> Result<String> {
        match self {
            Self::Done => Ok(String::new()),
            Self::Text { text } | Self::Tsv { text } => Ok(text.clone()),
            Self::Json { value } => Ok(serde_json::to_string(value)?),
            Self::Binary { mime, content } => {
                let mut rendered = String::new();
                rendered.push_str("[binary tool result: ");
                rendered.push_str(mime);
                rendered.push_str(", ");
                rendered.push_str(content.len().to_string().as_str());
                rendered.push_str(" bytes]");
                Ok(rendered)
            }
            Self::Error { message } => Ok(message.clone()),
        }
    }

    /// Renders the result for CLI display.
    ///
    /// # Errors
    ///
    /// Returns an error if JSON serialization fails.
    pub fn render_for_cli(&self) -> Result<String> {
        match self {
            Self::Done => Ok(String::new()),
            Self::Text { text } | Self::Tsv { text } => Ok(text.clone()),
            Self::Json { value } => Ok(serde_json::to_string_pretty(value)?),
            Self::Binary { mime, content } => {
                let mut rendered = String::new();
                rendered.push_str("[binary tool result: ");
                rendered.push_str(mime);
                rendered.push_str(", ");
                rendered.push_str(content.len().to_string().as_str());
                rendered.push_str(" bytes]");
                Ok(rendered)
            }
            Self::Error { message } => Ok(message.clone()),
        }
    }

    /// Parses and returns the MIME type when this result carries binary content.
    #[must_use]
    pub fn mime(&self) -> Option<Mime> {
        match self {
            Self::Binary { mime, .. } => mime.parse().ok(),
            Self::Done
            | Self::Text { .. }
            | Self::Tsv { .. }
            | Self::Json { .. }
            | Self::Error { .. } => None,
        }
    }

    /// Returns raw bytes for binary results.
    #[must_use]
    pub fn content(&self) -> Option<&[u8]> {
        match self {
            Self::Binary { content, .. } => Some(content),
            Self::Done
            | Self::Text { .. }
            | Self::Tsv { .. }
            | Self::Json { .. }
            | Self::Error { .. } => None,
        }
    }
}

/// Conversion trait for values returned by [`Tool::call`].
///
/// The conversion itself is fallible so tool authors can return types like
/// `Result<T: Serialize, E: Error>` and still surface serialization failures as
/// framework errors while preserving typed tool errors inside [`ToolResult`].
pub trait IntoToolResult {
    /// Converts the value into a final [`ToolResult`].
    ///
    /// # Errors
    ///
    /// Returns an error if the conversion cannot be completed.
    fn into_tool_result(self) -> Result<ToolResult>;
}

impl IntoToolResult for ToolResult {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(self)
    }
}

impl IntoToolResult for () {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(ToolResult::Done)
    }
}

impl IntoToolResult for String {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(ToolResult::text(self))
    }
}

impl IntoToolResult for &str {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(ToolResult::text(self))
    }
}

impl IntoToolResult for Cow<'_, str> {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(ToolResult::text(self.into_owned()))
    }
}

impl IntoToolResult for Value {
    fn into_tool_result(self) -> Result<ToolResult> {
        Ok(ToolResult::json_value(self))
    }
}

impl<T> IntoToolResult for Option<T>
where
    T: IntoToolResult,
{
    fn into_tool_result(self) -> Result<ToolResult> {
        self.map_or_else(|| Ok(ToolResult::Done), IntoToolResult::into_tool_result)
    }
}

impl<T, E> IntoToolResult for core::result::Result<T, E>
where
    T: Serialize,
    E: Display,
{
    fn into_tool_result(self) -> Result<ToolResult> {
        match self {
            Ok(value) => serialize_success_value(&value),
            Err(error) => Ok(ToolResult::error(error.to_string())),
        }
    }
}

fn parse_media_type_or_octet_stream(media_type: &str) -> String {
    media_type
        .parse::<Mime>()
        .unwrap_or(mime::APPLICATION_OCTET_STREAM)
        .essence_str()
        .to_string()
}

fn serialize_success_value<T: Serialize>(value: &T) -> Result<ToolResult> {
    let value = serde_json::to_value(value)?;
    if let Some(tsv) = json_value_to_tsv(&value) {
        return Ok(ToolResult::tsv(tsv));
    }

    match value {
        Value::String(text) => Ok(ToolResult::text(text)),
        other => Ok(ToolResult::json_value(other)),
    }
}

/// Converts a JSON value into TSV when it represents an object or non-empty array.
#[must_use]
pub fn json_value_to_tsv(value: &Value) -> Option<String> {
    let rows = match value {
        Value::Array(arr) if !arr.is_empty() => arr
            .iter()
            .map(|value| flatten_json_value(value, ""))
            .collect::<Vec<_>>(),
        Value::Object(_) => alloc::vec![flatten_json_value(value, "")],
        Value::Array(_) | Value::String(_) | Value::Number(_) | Value::Bool(_) | Value::Null => {
            return None;
        }
    };

    if rows.is_empty() {
        return None;
    }

    let mut columns: Vec<String> = Vec::new();
    let mut seen: alloc::collections::BTreeSet<String> = alloc::collections::BTreeSet::new();
    for row in &rows {
        for (key, _) in row {
            if seen.insert(key.clone()) {
                columns.push(key.clone());
            }
        }
    }

    if columns.is_empty() {
        return None;
    }

    let mut tsv = String::new();
    for (index, column) in columns.iter().enumerate() {
        if index > 0 {
            tsv.push('\t');
        }
        tsv.push_str(&escape_tsv_field(column));
    }
    tsv.push('\n');

    for row in &rows {
        let row_map: alloc::collections::BTreeMap<&str, &str> = row
            .iter()
            .map(|(key, value)| (key.as_str(), value.as_str()))
            .collect::<alloc::collections::BTreeMap<&str, &str>>();
        for (index, column) in columns.iter().enumerate() {
            if index > 0 {
                tsv.push('\t');
            }
            if let Some(value) = row_map.get(column.as_str()) {
                tsv.push_str(&escape_tsv_field(value));
            }
        }
        tsv.push('\n');
    }

    Some(tsv)
}

fn flatten_json_value(value: &Value, prefix: &str) -> Vec<(String, String)> {
    let mut flattened = Vec::new();
    match value {
        Value::Object(map) => {
            for (key, child) in map {
                let full_key = if prefix.is_empty() {
                    key.clone()
                } else {
                    format!("{prefix}.{key}")
                };
                flattened.extend(flatten_json_value(child, &full_key));
            }
        }
        Value::Array(_) => {
            let serialized = serde_json::to_string(value).unwrap_or_default();
            flattened.push((prefix.to_string(), serialized));
        }
        Value::String(text) => {
            flattened.push((prefix.to_string(), text.clone()));
        }
        Value::Number(number) => {
            flattened.push((prefix.to_string(), number.to_string()));
        }
        Value::Bool(boolean) => {
            flattened.push((prefix.to_string(), boolean.to_string()));
        }
        Value::Null => {
            flattened.push((prefix.to_string(), String::new()));
        }
    }
    flattened
}

fn escape_tsv_field(value: &str) -> String {
    value.replace(['\t', '\n', '\r'], " ")
}

/// Tools that can be called by language models.
///
/// # Example
///
/// ```rust,ignore
/// use aither::llm::{Tool, ToolResult};
/// use schemars::JsonSchema;
/// use serde::Deserialize;
///
/// #[derive(JsonSchema, Deserialize)]
/// struct CalculatorArgs {
///     operation: String,
///     a: f64,
///     b: f64,
/// }
///
/// struct Calculator;
///
/// impl Tool for Calculator {
///     type Arguments = CalculatorArgs;
///     type Res = ToolResult;
///
///     async fn call(&mut self, args: Self::Arguments) -> aither::Result<Self::Res> {
///         match args.operation.as_str() {
///             "add" => Ok(ToolResult::text((args.a + args.b).to_string())),
///             "subtract" => Ok(ToolResult::text((args.a - args.b).to_string())),
///             "multiply" => Ok(ToolResult::text((args.a * args.b).to_string())),
///             "divide" => {
///                 if args.b != 0.0 {
///                     Ok(ToolResult::text((args.a / args.b).to_string()))
///                 } else {
///                     Err(anyhow::Error::msg("Division by zero"))
///                 }
///             }
///             _ => Err(anyhow::Error::msg("Unknown operation")),
///         }
///     }
/// }
/// ```
pub trait Tool: Send + Sync {
    /// Tool name. Must be unique.
    fn name(&self) -> Cow<'static, str>;

    /// What the tool does, as shown to the model.
    ///
    /// This is the single most important thing a model uses to decide whether
    /// to call a tool, so it must not be empty — [`Tools::register`] rejects a
    /// tool whose description is blank.
    ///
    /// The default implementation reads the rustdoc comment on
    /// [`Self::Arguments`], which `schemars` records in the generated schema.
    /// Override it to supply the description directly.
    fn description(&self) -> Cow<'static, str> {
        description_from_schema::<Self::Arguments>().unwrap_or_default()
    }

    /// Tool arguments type. Must implement [`schemars::JsonSchema`] and [`serde::de::DeserializeOwned`].
    /// Its rustdoc becomes the default tool description.
    type Arguments: Send + JsonSchema + DeserializeOwned;

    /// Raw return type from the tool implementation.
    type Res: IntoToolResult + Send;

    /// Executes the tool with the provided arguments.
    ///
    /// Returns a value that can be converted into a final [`ToolResult`].
    ///
    /// Tools that need mutable state should use interior mutability (e.g., `Mutex`).
    fn call(&self, arguments: Self::Arguments) -> impl Future<Output = Result<Self::Res>> + Send;
}

/// Utility to convert a serializable value to a pretty-printed JSON string.
///
/// # Example
/// ```rust,ignore
/// use aither::llm::tool::json;
/// use serde::Serialize;
/// #[derive(Serialize)]
/// struct Data {
///     name: String,
///     value: u32,
/// }
/// let data = Data {
///     name: "example".to_string(),
///     value: 42,
/// };
/// let json_str = json(&data);
/// println!("{}", json_str);
/// ```
///
/// # Errors
///
/// Returns an error if the value cannot be serialized to JSON, which happens
/// for types such as maps with non-string keys.
pub fn json<T: Serialize>(value: &T) -> Result<String> {
    let value = serde_json::to_value(value)?;

    Ok(value
        .as_str()
        .map_or_else(|| format!("{value:#}"), ToString::to_string))
}

trait ToolImpl: Send + Sync + Any {
    fn call(&self, args: &str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + '_>>;

    /// The cached definition. Borrowed, so registering a tool does not have to
    /// clone its argument schema.
    fn definition(&self) -> &ToolDefinition;

    /// Upcast to [`Any`] so [`Tools::get`] can recover the concrete tool.
    ///
    /// Casting the `Box<dyn ToolImpl>` itself would downcast the box rather
    /// than the tool inside it, and so never match.
    fn as_any(&self) -> &dyn Any;

    /// Mutable counterpart of [`Self::as_any`].
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Dynamic tool implementation for type-erased tools.
struct DynToolImpl<F>
where
    F: Fn(&str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send>> + Send + Sync,
{
    definition: ToolDefinition,
    handler: F,
}

impl<F> ToolImpl for DynToolImpl<F>
where
    F: Fn(&str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send>> + Send + Sync + 'static,
{
    fn call(&self, args: &str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + '_>> {
        (self.handler)(args)
    }

    fn definition(&self) -> &ToolDefinition {
        &self.definition
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Whether a schema describes a JSON object, and so can be sent to a provider
/// as tool arguments without being wrapped in [`ToolArgument`].
fn schema_is_object(value: &Value) -> bool {
    matches!(value.get("type").and_then(Value::as_str), Some("object"))
        || value.get("properties").is_some()
        || value.get("oneOf").is_some()
        || value.get("anyOf").is_some()
        || value.get("$defs").is_some()
}

fn is_object<T: JsonSchema>() -> bool {
    schema_is_object(&schema_for!(T).to_value())
}

/// Builds the argument schema for a tool, wrapping scalars so the root is
/// always an object as providers require.
fn arguments_schema<T: JsonSchema>() -> Schema {
    if is_object::<T>() {
        schema_for!(T)
    } else {
        schema_for!(ToolArgument<T>)
    }
}

/// Reads the `description` a `JsonSchema` derive records from a type's rustdoc.
fn description_from_schema<T: JsonSchema>() -> Option<Cow<'static, str>> {
    schema_for!(T)
        .to_value()
        .get("description")
        .and_then(Value::as_str)
        .filter(|text| !text.trim().is_empty())
        .map(|text| Cow::Owned(text.to_string()))
}

/// A registered tool together with everything derived from its type.
///
/// The argument schema and the "are these arguments an object?" decision are
/// properties of `T::Arguments` alone, so they are computed once here rather
/// than rebuilt on every invocation.
struct RegisteredTool<T: Tool> {
    tool: T,
    definition: ToolDefinition,
    args_are_object: bool,
}

impl<T: Tool> RegisteredTool<T> {
    fn new(tool: T) -> Self {
        let definition = ToolDefinition::new(&tool);
        let args_are_object = is_object::<T::Arguments>();
        Self {
            tool,
            definition,
            args_are_object,
        }
    }
}

impl<T: Tool + 'static> ToolImpl for RegisteredTool<T> {
    fn call(&self, args: &str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + '_>> {
        let result = if self.args_are_object {
            serde_json::from_str::<T::Arguments>(args)
        } else {
            serde_json::from_str::<ToolArgument<T::Arguments>>(args).map(|wrapper| wrapper.value)
        };

        let Ok(arguments) = result else {
            // Cold path: spelling the schema out for the model is worth the
            // allocation only when it has actually got the call wrong.
            let name = self.definition.name().to_string();
            let schema_str =
                serde_json::to_string_pretty(&self.definition.arguments_openai_schema())
                    .unwrap_or_else(|_| "{}".to_string());
            return Box::pin(async move {
                Err(anyhow::Error::msg(format!(
                    "Invalid arguments for tool '{name}'. Expected schema:\n{schema_str}"
                )))
            });
        };

        Box::pin(async move { Tool::call(&self.tool, arguments).await?.into_tool_result() })
    }

    fn definition(&self) -> &ToolDefinition {
        &self.definition
    }

    fn as_any(&self) -> &dyn Any {
        &self.tool
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        &mut self.tool
    }
}

/// A tool definition carried something that is not a JSON schema.
///
/// JSON Schema allows an object or a bare boolean; anything else — a string, an
/// array, a number — describes nothing a model could fill in.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidSchema {
    /// The tool whose schema was rejected.
    name: Cow<'static, str>,
}

impl InvalidSchema {
    /// The name of the tool whose schema was rejected.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }
}

impl Display for InvalidSchema {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "tool '{}' has an argument schema that is neither an object nor a boolean",
            self.name
        )
    }
}

impl core::error::Error for InvalidSchema {}

/// Why a tool could not be added to a [`Tools`] registry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RegisterError {
    /// A tool with this name is already registered.
    ///
    /// Names address tools in a model's tool-call, so they must be unique.
    DuplicateName(Cow<'static, str>),

    /// The tool's description is empty.
    ///
    /// A description is what a model uses to decide whether to call a tool, so
    /// an empty one makes the tool unusable rather than merely undocumented.
    EmptyDescription(Cow<'static, str>),
}

impl Display for RegisterError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::DuplicateName(name) => {
                write!(f, "a tool named '{name}' is already registered")
            }
            Self::EmptyDescription(name) => write!(
                f,
                "tool '{name}' has an empty description; add a rustdoc comment to its \
                 Arguments type or implement Tool::description"
            ),
        }
    }
}

impl core::error::Error for RegisterError {}

/// Tool registry for managing and calling tools by name.
///
///
/// # Example
///
/// ```rust,ignore
/// use aither::llm::tool::Tools;
///
/// let mut tools = Tools::new();
/// // tools.register(Calculator);
/// let definitions = tools.definitions();
/// // let result = tools.call("calculator", r#"{"operation": "add", "a": 5, "b": 3}"#).await;
/// ```
pub struct Tools {
    tools: BTreeMap<Cow<'static, str>, Box<dyn ToolImpl>>,
}

impl Debug for Tools {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Tools")
            .field("tools", &self.tools.keys().collect::<Vec<_>>())
            .finish()
    }
}

/// Tool definition including schema for language models.
///
/// Used to provide language models with information about available [`Tool`]s.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ToolDefinition {
    /// Tool name.
    name: Cow<'static, str>,
    /// Tool description.
    description: Cow<'static, str>,
    /// JSON schema for tool arguments.
    arguments: Schema,
}

impl ToolDefinition {
    /// Creates a tool definition for a given tool type.
    ///
    /// The description comes from [`Tool::description`], which by default reads
    /// the rustdoc on the tool's `Arguments` type.
    #[must_use]
    pub fn new<T: Tool>(tool: &T) -> Self {
        Self {
            name: tool.name(),
            description: tool.description(),
            arguments: arguments_schema::<T::Arguments>(),
        }
    }

    /// Creates a tool definition from raw parts.
    ///
    /// This is useful for creating definitions from external sources like MCP servers.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidSchema`] if the value is not a JSON schema — that is,
    /// anything other than an object or a boolean. The schema often comes from
    /// a remote server, so this is a rejection to report, not a bug to panic
    /// on.
    pub fn from_parts(
        name: Cow<'static, str>,
        description: Cow<'static, str>,
        schema: Value,
    ) -> core::result::Result<Self, InvalidSchema> {
        let arguments: Schema = schema
            .try_into()
            .map_err(|_| InvalidSchema { name: name.clone() })?;

        Ok(Self {
            name,
            description,
            arguments,
        })
    }

    /// Returns the tool's name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the tool's description.
    #[must_use]
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Return an OpenAI-compatible JSON schema for the tool's arguments.
    ///
    /// This schema would have an object type at the root, as required by `OpenAI`.
    #[must_use]
    pub fn arguments_openai_schema(&self) -> serde_json::Value {
        let mut inner = self.arguments.clone().to_value();
        clean_schema(&mut inner);

        inner
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
struct ToolArgument<T> {
    value: T,
}

fn clean_schema(value: &mut Value) {
    // First pass: extract $defs for reference resolution
    let defs = extract_defs(value);

    // Second pass: resolve refs and clean
    resolve_and_clean(value, &defs);

    // Clean up root-level schema
    if let Value::Object(map) = value {
        // Remove root-level description - it's already used as tool description
        // Keeping it duplicates the description in the API request
        map.remove("description");

        // Ensure root has type: object (required by OpenAI function calling)
        if map.contains_key("properties") && !map.contains_key("type") {
            map.insert("type".to_string(), Value::String("object".to_string()));
        }
    }
}

/// Extracts `$defs` or `definitions` from the root schema.
fn extract_defs(value: &Value) -> serde_json::Map<String, Value> {
    if let Value::Object(map) = value
        && let Some(Value::Object(defs)) = map.get("$defs").or_else(|| map.get("definitions"))
    {
        return defs.clone();
    }
    serde_json::Map::new()
}

/// Resolves `$ref` and cleans the schema recursively.
#[allow(clippy::too_many_lines)]
fn resolve_and_clean(value: &mut Value, defs: &serde_json::Map<String, Value>) {
    resolve_and_clean_inner(value, defs, false);
}

/// Inner recursive function with flag to track if we're inside a properties object.
#[allow(clippy::too_many_lines)]
fn resolve_and_clean_inner(
    value: &mut Value,
    defs: &serde_json::Map<String, Value>,
    inside_properties: bool,
) {
    match value {
        Value::Object(map) => {
            // Handle $ref - inline the referenced definition, preserving sibling properties
            if let Some(Value::String(ref_path)) = map.remove("$ref")
                && let Some(Value::Object(resolved_map)) = resolve_ref(&ref_path, defs)
            {
                // Merge resolved definition with any existing properties (like description)
                // Resolved definition takes precedence for conflicts except description
                let existing_description = map.remove("description");
                for (k, v) in resolved_map {
                    map.entry(k).or_insert(v);
                }
                // Preserve the field-level description if it exists
                if let Some(desc) = existing_description {
                    map.insert("description".to_string(), desc);
                }
            }

            // Convert "const" to "enum" with single value (before filtering)
            if let Some(const_val) = map.remove("const") {
                map.insert("enum".to_string(), Value::Array(alloc::vec![const_val]));
            }

            // Flatten oneOf/anyOf variants (before filtering, since oneOf is not in allowed list)
            if let Some(Value::Array(variants)) =
                map.remove("oneOf").or_else(|| map.remove("anyOf"))
            {
                // Check if this is a simple string enum (variants have const/type but no properties)
                let is_simple_enum = variants.iter().all(|v| {
                    if let Value::Object(vm) = v {
                        (vm.contains_key("const") || vm.contains_key("enum"))
                            && !vm.contains_key("properties")
                    } else {
                        false
                    }
                });

                if is_simple_enum {
                    // Collect all const/enum values into a single enum array
                    let mut enum_values: alloc::vec::Vec<Value> = alloc::vec::Vec::new();
                    let mut variant_type: Option<String> = None;

                    for variant in &variants {
                        if let Value::Object(vm) = variant {
                            if let Some(const_val) = vm.get("const")
                                && !enum_values.contains(const_val)
                            {
                                enum_values.push(const_val.clone());
                            }
                            if let Some(Value::Array(arr)) = vm.get("enum") {
                                for val in arr {
                                    if !enum_values.contains(val) {
                                        enum_values.push(val.clone());
                                    }
                                }
                            }
                            if variant_type.is_none()
                                && let Some(Value::String(t)) = vm.get("type")
                            {
                                variant_type = Some(t.clone());
                            }
                        }
                    }

                    if !enum_values.is_empty() {
                        map.insert("enum".to_string(), Value::Array(enum_values));
                        if let Some(t) = variant_type {
                            map.insert("type".to_string(), Value::String(t));
                        }
                    }
                } else {
                    // Complex variants with properties - merge them
                    let mut all_properties = serde_json::Map::new();

                    for variant in variants {
                        if let Value::Object(variant_map) = variant
                            && let Some(Value::Object(props)) = variant_map.get("properties")
                        {
                            for (key, val) in props {
                                // Extract enum value - handle both "enum" and "const"
                                let new_values: Option<alloc::vec::Vec<Value>> =
                                    if let Value::Object(val_obj) = val {
                                        if let Some(Value::Array(arr)) = val_obj.get("enum") {
                                            Some(arr.clone())
                                        } else {
                                            val_obj
                                                .get("const")
                                                .map(|const_val| alloc::vec![const_val.clone()])
                                        }
                                    } else {
                                        None
                                    };

                                if all_properties.contains_key(key) {
                                    // Merge enum/const values into existing
                                    if let Some(values) = new_values
                                        && let Some(Value::Object(existing_obj)) =
                                            all_properties.get_mut(key)
                                        && let Some(Value::Array(existing_enum)) =
                                            existing_obj.get_mut("enum")
                                    {
                                        for e in values {
                                            if !existing_enum.contains(&e) {
                                                existing_enum.push(e);
                                            }
                                        }
                                    }
                                } else {
                                    // First time seeing this property - convert const to enum
                                    let mut val_clone = val.clone();
                                    if let Value::Object(obj) = &mut val_clone
                                        && let Some(const_val) = obj.remove("const")
                                    {
                                        obj.insert(
                                            "enum".to_string(),
                                            Value::Array(alloc::vec![const_val]),
                                        );
                                    }
                                    all_properties.insert(key.clone(), val_clone);
                                }
                            }
                        }
                    }

                    // Set type as object if we have properties
                    if !all_properties.is_empty() {
                        map.insert("type".to_string(), Value::String("object".to_string()));
                        map.insert("properties".to_string(), Value::Object(all_properties));
                    }
                }
            }

            // Only filter schema keywords, not property names inside "properties"
            // OpenAPI schema subset supported by most LLM providers
            if !inside_properties {
                let allowed = [
                    "type",
                    "description",
                    "properties",
                    "required",
                    "items",
                    "enum",
                    "nullable",
                ];
                map.retain(|k, _| allowed.contains(&k.as_str()));
            }

            // Simplify "type" arrays like ["string", "null"] to single type
            if let Some(Value::Array(types)) = map.get("type") {
                // Filter out "null" and take the first non-null type
                let non_null: Vec<&Value> = types
                    .iter()
                    .filter(|t| !matches!(t, Value::String(s) if s == "null"))
                    .collect();
                if non_null.len() == 1 {
                    map.insert("type".to_string(), non_null[0].clone());
                }
            }

            // Recursively clean all values
            for (key, v) in map.iter_mut() {
                // When entering "properties", its children are property definitions
                let child_inside_props = key == "properties";
                resolve_and_clean_inner(v, defs, child_inside_props);
            }
        }
        Value::Array(arr) => {
            for v in arr {
                resolve_and_clean_inner(v, defs, false);
            }
        }
        _ => {}
    }
}

/// Resolves a `$ref` path like `#/$defs/FsOperation` to its definition.
fn resolve_ref(ref_path: &str, defs: &serde_json::Map<String, Value>) -> Option<Value> {
    // Handle common patterns: #/$defs/Name or #/definitions/Name
    let name = ref_path
        .strip_prefix("#/$defs/")
        .or_else(|| ref_path.strip_prefix("#/definitions/"))?;

    defs.get(name).cloned()
}

impl Default for Tools {
    fn default() -> Self {
        Self::new()
    }
}

impl Tools {
    /// Creates a new empty tools registry.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            tools: BTreeMap::new(),
        }
    }

    /// Retrieves a tool by type.
    ///
    /// Returns `None` if the tool is not found.
    #[must_use]
    pub fn get<T>(&self) -> Option<&T>
    where
        T: Tool + 'static,
    {
        self.tools
            .values()
            .find_map(|tool| tool.as_any().downcast_ref::<T>())
    }

    /// Retrieves a mutable reference to a tool by type.
    ///
    /// Returns `None` if the tool is not found.
    #[must_use]
    pub fn get_mut<T>(&mut self) -> Option<&mut T>
    where
        T: Tool + 'static,
    {
        self.tools
            .values_mut()
            .find_map(|tool| tool.as_any_mut().downcast_mut::<T>())
    }

    /// Returns definitions of all registered tools.
    #[must_use]
    pub fn definitions(&self) -> Vec<ToolDefinition> {
        self.tools
            .values()
            .map(|tool| tool.definition().clone())
            .collect()
    }

    /// Registers a new tool.
    ///
    /// The tool must implement [`Tool`] and be `'static`.
    ///
    /// # Errors
    ///
    /// Returns [`RegisterError::DuplicateName`] if a tool of that name is
    /// already registered, or [`RegisterError::EmptyDescription`] if the tool
    /// has no description — a model cannot use a tool it cannot read about, so
    /// this is rejected rather than silently passed on.
    pub fn register<T: Tool + 'static>(
        &mut self,
        tool: T,
    ) -> core::result::Result<(), RegisterError> {
        self.insert(Box::new(RegisteredTool::new(tool)))
    }

    /// Registers a dynamic tool with a pre-made definition and handler.
    ///
    /// This is useful for type-erased tools (e.g., child terminal tools for subagents)
    /// where the concrete type isn't known at compile time.
    ///
    /// # Errors
    ///
    /// Same conditions as [`Self::register`].
    pub fn register_dyn<F>(
        &mut self,
        definition: ToolDefinition,
        handler: F,
    ) -> core::result::Result<(), RegisterError>
    where
        F: Fn(&str) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send>>
            + Send
            + Sync
            + 'static,
    {
        self.insert(Box::new(DynToolImpl {
            definition,
            handler,
        }))
    }

    fn insert(&mut self, tool: Box<dyn ToolImpl>) -> core::result::Result<(), RegisterError> {
        let name = tool.definition().name.clone();
        if self.tools.contains_key(&name) {
            return Err(RegisterError::DuplicateName(name));
        }
        if tool.definition().description().trim().is_empty() {
            return Err(RegisterError::EmptyDescription(name));
        }
        self.tools.insert(name, tool);
        Ok(())
    }

    /// Removes a tool from the registry.
    pub fn unregister(&mut self, name: &str) {
        self.tools.remove(name);
    }

    /// Calls a tool by name with JSON arguments.
    ///
    /// # Errors
    ///
    /// Returns an error if the tool is not found, arguments cannot be parsed,
    /// or tool execution fails.
    pub async fn call(&self, name: &str, args: &str) -> Result<ToolResult> {
        if let Some(tool) = self.tools.get(name) {
            tool.call(args).await
        } else {
            Err(anyhow::Error::msg(format!("Tool '{name}' not found")))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{format, string::ToString, vec};
    use schemars::JsonSchema;
    use serde::{Deserialize, Serialize};

    /// Performs basic mathematical operations.
    #[derive(JsonSchema, Deserialize, Debug, PartialEq)]
    struct CalculatorArgs {
        operation: String,
        a: f64,
        b: f64,
    }

    struct Calculator;

    impl Tool for Calculator {
        fn name(&self) -> Cow<'static, str> {
            "calculator".into()
        }
        type Arguments = CalculatorArgs;
        type Res = ToolResult;

        fn call(&self, args: Self::Arguments) -> impl Future<Output = Result<Self::Res>> + Send {
            core::future::ready(match args.operation.as_str() {
                "add" => Ok(ToolResult::text((args.a + args.b).to_string())),
                "subtract" => Ok(ToolResult::text((args.a - args.b).to_string())),
                "multiply" => Ok(ToolResult::text((args.a * args.b).to_string())),
                "divide" => {
                    if args.b == 0.0 {
                        Err(anyhow::Error::msg("Division by zero"))
                    } else {
                        Ok(ToolResult::text((args.a / args.b).to_string()))
                    }
                }
                _ => Err(anyhow::Error::msg(format!(
                    "Unknown operation: {}",
                    args.operation
                ))),
            })
        }
    }

    /// Greets a person by name.
    #[derive(JsonSchema, Deserialize)]
    struct GreetArgs {
        name: String,
    }

    struct Greeter;

    impl Tool for Greeter {
        fn name(&self) -> Cow<'static, str> {
            "greeter".into()
        }
        type Arguments = GreetArgs;
        type Res = ToolResult;

        fn call(&self, args: Self::Arguments) -> impl Future<Output = Result<Self::Res>> + Send {
            core::future::ready(Ok(ToolResult::text(format!("Hello, {}!", args.name))))
        }
    }

    #[test]
    fn from_parts_accepts_object_and_boolean_schemas() {
        // JSON Schema allows a bare boolean as well as an object.
        for schema in [
            serde_json::json!({"type": "object"}),
            serde_json::json!(true),
        ] {
            assert!(
                ToolDefinition::from_parts("t".into(), "does a thing".into(), schema.clone())
                    .is_ok(),
                "{schema} should be accepted"
            );
        }
    }

    #[test]
    fn from_parts_rejects_non_schema_values() {
        // A server that sends any of these describes nothing a model could
        // fill in. Rejecting must not panic: the value came off the wire.
        for schema in [
            serde_json::json!("a string"),
            serde_json::json!([1, 2, 3]),
            serde_json::json!(7),
            serde_json::json!(null),
        ] {
            let result = ToolDefinition::from_parts("weird".into(), "d".into(), schema.clone());
            let Err(err) = result else {
                panic!("{schema} should be rejected");
            };
            assert_eq!(err.name(), "weird");
        }
    }

    #[test]
    fn json_utility() {
        let value = serde_json::json!({
            "name": "test",
            "value": 42
        });

        let json_str = json(&value).expect("a JSON value always serializes");
        assert!(json_str.contains("\"name\": \"test\""));
        assert!(json_str.contains("\"value\": 42"));
    }

    #[test]
    fn tool_definition_creation() {
        let calculator = Calculator;
        let definition = ToolDefinition::new(&calculator);

        assert_eq!(definition.name, "calculator");
        assert_eq!(
            definition.description,
            "Performs basic mathematical operations."
        );
        // Schema should be present - just check it exists
        // The exact structure of schemars::Schema is implementation detail
    }

    #[test]
    fn tools_creation() {
        let tools = Tools::new();
        assert_eq!(tools.definitions().len(), 0);
    }

    #[test]
    fn tools_default() {
        let tools = Tools::default();
        assert_eq!(tools.definitions().len(), 0);
    }

    #[tokio::test]
    async fn tools_register_and_call() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");

        let definitions = tools.definitions();
        assert_eq!(definitions.len(), 1);
        assert_eq!(definitions[0].name, "calculator");

        let result = tools
            .call("calculator", r#"{"operation": "add", "a": 5, "b": 3}"#)
            .await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_text(), Some("8"));
    }

    #[tokio::test]
    async fn calculator_operations() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");

        // Test addition
        let result = tools
            .call("calculator", r#"{"operation": "add", "a": 10, "b": 5}"#)
            .await;
        assert_eq!(result.unwrap().as_text(), Some("15"));

        // Test subtraction
        let result = tools
            .call(
                "calculator",
                r#"{"operation": "subtract", "a": 10, "b": 3}"#,
            )
            .await;
        assert_eq!(result.unwrap().as_text(), Some("7"));

        // Test multiplication
        let result = tools
            .call("calculator", r#"{"operation": "multiply", "a": 4, "b": 3}"#)
            .await;
        assert_eq!(result.unwrap().as_text(), Some("12"));

        // Test division
        let result = tools
            .call("calculator", r#"{"operation": "divide", "a": 15, "b": 3}"#)
            .await;
        assert_eq!(result.unwrap().as_text(), Some("5"));
    }

    #[tokio::test]
    async fn calculator_division_by_zero() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");

        let result = tools
            .call("calculator", r#"{"operation": "divide", "a": 10, "b": 0}"#)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Division by zero"));
    }

    #[tokio::test]
    async fn calculator_unknown_operation() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");

        let result = tools
            .call("calculator", r#"{"operation": "modulo", "a": 10, "b": 3}"#)
            .await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Unknown operation")
        );
    }

    #[tokio::test]
    async fn multiple_tools() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");
        tools.register(Greeter).expect("greeter registers");

        let definitions = tools.definitions();
        assert_eq!(definitions.len(), 2);

        // Find calculator and greeter in definitions
        let calc_def = definitions.iter().find(|d| d.name == "calculator").unwrap();
        let greet_def = definitions.iter().find(|d| d.name == "greeter").unwrap();

        assert_eq!(
            calc_def.description,
            "Performs basic mathematical operations."
        );
        assert_eq!(greet_def.description, "Greets a person by name.");

        // Test both tools
        let calc_result = tools
            .call("calculator", r#"{"operation": "add", "a": 2, "b": 3}"#)
            .await;
        assert_eq!(calc_result.unwrap().as_text(), Some("5"));

        let greet_result = tools.call("greeter", r#"{"name": "Alice"}"#).await;
        assert_eq!(greet_result.unwrap().as_text(), Some("Hello, Alice!"));
    }

    #[derive(Debug, Serialize)]
    struct TableRow {
        name: &'static str,
        count: u32,
    }

    #[derive(Debug, Serialize)]
    struct NestedTableRow {
        user: TableRow,
        ok: bool,
    }

    #[derive(Debug)]
    struct ToolFailure(&'static str);

    impl core::fmt::Display for ToolFailure {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            f.write_str(self.0)
        }
    }

    impl core::error::Error for ToolFailure {}

    #[test]
    fn into_tool_result_string_is_plain_text() {
        assert_eq!(
            String::from("hello").into_tool_result().unwrap(),
            ToolResult::text("hello")
        );
    }

    #[test]
    fn into_tool_result_str_is_plain_text() {
        assert_eq!(
            "hello".into_tool_result().unwrap(),
            ToolResult::text("hello")
        );
    }

    #[test]
    fn into_tool_result_option_none_is_done() {
        let result = Option::<String>::None.into_tool_result().unwrap();
        assert_eq!(result, ToolResult::Done);
    }

    #[test]
    fn into_tool_result_option_some_delegates() {
        let result = Some("hello").into_tool_result().unwrap();
        assert_eq!(result, ToolResult::text("hello"));
    }

    #[test]
    fn into_tool_result_result_ok_string_is_text() {
        let result = core::result::Result::<String, ToolFailure>::Ok(String::from("hello"))
            .into_tool_result()
            .unwrap();
        assert_eq!(result, ToolResult::text("hello"));
    }

    #[test]
    fn into_tool_result_result_ok_object_is_tsv() {
        let result = core::result::Result::<TableRow, ToolFailure>::Ok(TableRow {
            name: "alpha",
            count: 3,
        })
        .into_tool_result()
        .unwrap();

        assert_eq!(result, ToolResult::tsv("count\tname\n3\talpha\n"));
    }

    #[test]
    fn into_tool_result_result_ok_array_of_objects_is_tsv() {
        let result = core::result::Result::<Vec<TableRow>, ToolFailure>::Ok(vec![
            TableRow {
                name: "alpha",
                count: 3,
            },
            TableRow {
                name: "beta",
                count: 5,
            },
        ])
        .into_tool_result()
        .unwrap();

        assert_eq!(result, ToolResult::tsv("count\tname\n3\talpha\n5\tbeta\n"));
    }

    #[test]
    fn into_tool_result_result_ok_scalar_is_json() {
        let result = core::result::Result::<bool, ToolFailure>::Ok(true)
            .into_tool_result()
            .unwrap();
        assert_eq!(result, ToolResult::json_value(Value::Bool(true)));
    }

    #[test]
    fn into_tool_result_result_err_is_typed_error() {
        let result = core::result::Result::<TableRow, ToolFailure>::Err(ToolFailure("boom"))
            .into_tool_result()
            .unwrap();
        assert_eq!(result, ToolResult::error("boom"));
        assert!(result.is_error());
        assert_eq!(result.error_message(), Some("boom"));
    }

    #[test]
    fn json_value_to_tsv_flattens_nested_objects() {
        let value = serde_json::to_value(NestedTableRow {
            user: TableRow {
                name: "alpha",
                count: 3,
            },
            ok: true,
        })
        .unwrap();

        assert_eq!(
            json_value_to_tsv(&value),
            Some("ok\tuser.count\tuser.name\ntrue\t3\talpha\n".to_string())
        );
    }

    #[tokio::test]
    async fn tool_not_found() {
        let tools = Tools::new();

        let result = tools.call("nonexistent", "{}").await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Tool 'nonexistent' not found")
        );
    }

    #[tokio::test]
    async fn invalid_json() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");

        let result = tools.call("calculator", "invalid json").await;
        assert!(result.is_err());
    }

    #[test]
    fn tools_unregister() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");
        tools.register(Greeter).expect("greeter registers");

        assert_eq!(tools.definitions().len(), 2);

        tools.unregister("calculator");
        assert_eq!(tools.definitions().len(), 1);

        let remaining = &tools.definitions()[0];
        assert_eq!(remaining.name, "greeter");

        tools.unregister("greeter");
        assert_eq!(tools.definitions().len(), 0);
    }

    #[test]
    fn tools_debug() {
        let mut tools = Tools::new();
        tools.register(Calculator).expect("calculator registers");
        tools.register(Greeter).expect("greeter registers");

        let debug_str = format!("{tools:?}");
        assert!(debug_str.contains("Tools"));
        assert!(debug_str.contains("calculator"));
        assert!(debug_str.contains("greeter"));
    }

    #[test]
    fn tool_definition_debug() {
        let calculator = Calculator;
        let definition = ToolDefinition::new(&calculator);
        let debug_str = format!("{definition:?}");

        assert!(debug_str.contains("ToolDefinition"));
        assert!(debug_str.contains("calculator"));
        assert!(debug_str.contains("Performs basic mathematical operations"));
    }

    #[test]
    fn tool_definition_clone() {
        let calculator = Calculator;
        let original = ToolDefinition::new(&calculator);
        let cloned = original.clone();

        assert_eq!(original.name, cloned.name);
        assert_eq!(original.description, cloned.description);
    }

    #[test]
    fn schema_preserves_enum() {
        #[derive(JsonSchema, Deserialize)]
        #[serde(rename_all = "snake_case")]
        enum Status {
            Pending,
            InProgress,
            Completed,
        }

        #[allow(dead_code)]
        #[derive(JsonSchema, Deserialize)]
        struct Item {
            status: Status,
        }

        #[allow(dead_code)]
        #[derive(JsonSchema, Deserialize)]
        struct Args {
            items: Vec<Item>,
        }

        struct TestTool;

        impl Tool for TestTool {
            fn name(&self) -> Cow<'static, str> {
                "test".into()
            }
            type Arguments = Args;
            type Res = ToolResult;

            fn call(
                &self,
                _args: Self::Arguments,
            ) -> impl Future<Output = Result<Self::Res>> + Send {
                core::future::ready(Ok(ToolResult::text("ok")))
            }
        }

        let tool = TestTool;
        let def = ToolDefinition::new(&tool);
        let schema = def.arguments_openai_schema();

        // Check that status has enum values
        let schema_obj = schema.as_object().expect("schema should be object");
        let properties = schema_obj
            .get("properties")
            .expect("should have properties")
            .as_object()
            .unwrap();
        let items = properties
            .get("items")
            .expect("should have items")
            .as_object()
            .unwrap();
        let item_props = items
            .get("items")
            .expect("items should have items schema")
            .as_object()
            .unwrap();
        let item_properties = item_props
            .get("properties")
            .expect("item should have properties")
            .as_object()
            .unwrap();
        let status = item_properties
            .get("status")
            .expect("should have status")
            .as_object()
            .unwrap();

        // Status should have enum
        assert!(
            status.contains_key("enum"),
            "Status should have enum field. Full schema: {}",
            serde_json::to_string_pretty(&schema).unwrap()
        );
    }

    #[test]
    fn schema_ref_resolution() {
        // Test that $ref schemas get resolved and enum values preserved
        let raw_schema = serde_json::json!({
            "type": "object",
            "properties": {
                "status": {
                    "$ref": "#/$defs/Status"
                }
            },
            "$defs": {
                "Status": {
                    "type": "string",
                    "enum": ["pending", "in_progress", "completed"]
                }
            }
        });

        let mut schema = raw_schema;
        clean_schema(&mut schema);

        let props = schema.get("properties").unwrap().as_object().unwrap();
        let status = props.get("status").unwrap().as_object().unwrap();

        assert!(
            status.contains_key("enum"),
            "Status should have enum after ref resolution. Got: {}",
            serde_json::to_string_pretty(&schema).unwrap()
        );
    }

    #[test]
    fn schema_nested_ref_in_array() {
        // Test nested $ref inside array items (like TodoWriteArgs)
        let raw_schema = serde_json::json!({
            "type": "object",
            "properties": {
                "todos": {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/TodoItem"
                    }
                }
            },
            "$defs": {
                "TodoItem": {
                    "type": "object",
                    "properties": {
                        "content": { "type": "string" },
                        "status": { "$ref": "#/$defs/TodoStatus" }
                    },
                    "required": ["content", "status"]
                },
                "TodoStatus": {
                    "type": "string",
                    "enum": ["pending", "in_progress", "completed"]
                }
            }
        });

        let mut schema = raw_schema;
        clean_schema(&mut schema);

        // Navigate to status
        let props = schema.get("properties").unwrap().as_object().unwrap();
        let todos = props.get("todos").unwrap().as_object().unwrap();
        let items = todos.get("items").unwrap().as_object().unwrap();
        let item_props = items.get("properties").unwrap().as_object().unwrap();
        let status = item_props.get("status").unwrap().as_object().unwrap();

        assert!(
            status.contains_key("enum"),
            "Nested status should have enum. Full schema: {}",
            serde_json::to_string_pretty(&schema).unwrap()
        );
    }
}