autoagents-llm 0.4.0

Agent Framework for Building Autonomous Agents
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
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
//! OpenAI-compatible API client base implementation
//!
//! This module provides a generic base for OpenAI-compatible APIs that can be reused
//! across multiple providers like OpenAI, Mistral, XAI, Groq, DeepSeek, etc.

// `OpenAICompatibleProvider` is used on every target (native chat-completions
// backends and the WASI Preview2 OpenAI Responses backend), but most of the
// chat-completions / streaming types and helpers below are unreachable on
// `wasm32-wasip2`. The module is annotated `#[allow(dead_code)]` at its
// declaration in `providers/mod.rs` (covering all targets) rather than with a
// second, wasm-only attribute here, which would duplicate that lint allow.

#[cfg(not(target_arch = "wasm32"))]
use crate::FunctionCall;
#[cfg(not(target_arch = "wasm32"))]
use crate::chat::{ChatMessage, ChatRole, MessageType};
#[cfg(not(target_arch = "wasm32"))]
use crate::chat::{
    ChatProvider, StreamChoice, StreamChunk as ChatStreamChunk, StreamDelta, StreamResponse,
};
use crate::config::resolve_request_timeout;
#[cfg(not(target_arch = "wasm32"))]
use crate::error::LLMError;
#[cfg(not(target_arch = "wasm32"))]
use crate::http::ensure_success;
use crate::{
    ToolCall,
    chat::ChatResponse,
    chat::{StructuredOutputFormat, Tool, ToolChoice, Usage},
    default_call_type,
};
#[cfg(not(target_arch = "wasm32"))]
use async_trait::async_trait;
#[cfg(not(target_arch = "wasm32"))]
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use either::*;
#[cfg(not(target_arch = "wasm32"))]
use futures::{StreamExt, stream::Stream};
#[cfg(not(target_arch = "wasm32"))]
use reqwest::Client;
use serde::{Deserialize, Serialize};
#[cfg(not(target_arch = "wasm32"))]
use std::collections::HashMap;
use std::marker::PhantomData;
#[cfg(not(target_arch = "wasm32"))]
use std::pin::Pin;
use url::Url;

/// Generic OpenAI-compatible provider
///
/// This struct provides a base implementation for any OpenAI-compatible API.
/// Different providers can customize behavior by implementing the `OpenAICompatibleConfig` trait.
pub struct OpenAICompatibleProvider<T: OpenAIProviderConfig> {
    pub api_key: String,
    pub base_url: Url,
    pub model: String,
    pub max_tokens: Option<u32>,
    pub temperature: Option<f32>,
    pub timeout_seconds: u64,
    pub top_p: Option<f32>,
    pub top_k: Option<u32>,
    pub tool_choice: Option<ToolChoice>,
    pub reasoning_effort: Option<String>,
    #[allow(dead_code)]
    pub voice: Option<String>,
    pub extra_body: serde_json::Map<String, serde_json::Value>,
    pub parallel_tool_calls: bool,
    pub embedding_encoding_format: Option<String>,
    pub embedding_dimensions: Option<u32>,
    pub normalize_response: bool,
    /// Native HTTP client. Only present on non-wasm32 targets; the WASI Preview2
    /// transport uses `golem-wasi-http` over the `wasi:http` host interface.
    #[cfg(not(target_arch = "wasm32"))]
    pub client: Client,
    _phantom: PhantomData<T>,
}

/// Configuration trait for OpenAI-compatible providers
///
/// This trait allows different providers to customize behavior while reusing
/// the common OpenAI-compatible implementation.
pub trait OpenAIProviderConfig: Send + Sync {
    /// The name of the provider (e.g., "OpenAI", "Mistral", "XAI")
    const PROVIDER_NAME: &'static str;
    /// Default base URL for the provider
    const DEFAULT_BASE_URL: &'static str;
    /// Default model for the provider
    const DEFAULT_MODEL: &'static str;
    /// Chat completions endpoint path (usually "chat/completions")
    const CHAT_ENDPOINT: &'static str = "chat/completions";
    /// Whether this provider supports reasoning effort
    const SUPPORTS_REASONING_EFFORT: bool = false;
    /// Whether this provider supports structured output
    const SUPPORTS_STRUCTURED_OUTPUT: bool = false;
    /// Whether this provider supports parallel tool calls
    const SUPPORTS_PARALLEL_TOOL_CALLS: bool = false;
    /// Whether this provider supports stream options (like include_usage)
    const SUPPORTS_STREAM_OPTIONS: bool = false;
    /// Custom headers to add to requests
    fn custom_headers() -> Option<Vec<(String, String)>> {
        None
    }
}

/// Generic OpenAI-compatible chat message
#[derive(Serialize, Debug)]
pub struct OpenAIChatMessage<'a> {
    pub role: &'a str,
    #[serde(
        skip_serializing_if = "Option::is_none",
        with = "either::serde_untagged_optional"
    )]
    pub content: Option<Either<Vec<OpenAIMessageContent<'a>>, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
}

#[derive(Serialize, Debug)]
pub struct OpenAIMessageContent<'a> {
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub message_type: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_url: Option<ImageUrlContent>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "tool_call_id")]
    pub tool_call_id: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "content")]
    pub tool_output: Option<&'a str>,
}

#[derive(Serialize, Debug)]
pub struct ImageUrlContent {
    pub url: String,
}

/// Generic OpenAI-compatible chat request
#[derive(Serialize, Debug)]
pub struct OpenAIChatRequest<'a> {
    pub model: &'a str,
    pub messages: Vec<OpenAIChatMessage<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    pub stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_effort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<OpenAIResponseFormat>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<OpenAIStreamOptions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,
    #[serde(flatten)]
    pub extra_body: serde_json::Map<String, serde_json::Value>,
}

/// Generic OpenAI-compatible chat response
#[derive(Deserialize, Debug)]
pub struct OpenAIChatResponse {
    pub choices: Vec<OpenAIChatChoice>,
    pub usage: Option<Usage>,
}

#[derive(Deserialize, Debug)]
pub struct OpenAIChatChoice {
    pub message: OpenAIChatMsg,
}

#[derive(Deserialize, Debug)]
pub struct OpenAIChatMsg {
    #[allow(dead_code)]
    pub role: String,
    pub content: Option<String>,
    #[serde(default, alias = "reasoning")]
    pub reasoning_content: Option<String>,
    pub tool_calls: Option<Vec<ToolCall>>,
}

#[derive(Deserialize, Debug, Serialize)]
pub enum OpenAIResponseType {
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "json_schema")]
    JsonSchema,
    #[serde(rename = "json_object")]
    JsonObject,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct OpenAIResponseFormat {
    #[serde(rename = "type")]
    pub response_type: OpenAIResponseType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub json_schema: Option<StructuredOutputFormat>,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct OpenAIStreamOptions {
    pub include_usage: bool,
}

/// Streaming response structures
#[derive(Deserialize, Debug)]
pub struct StreamChunk {
    pub choices: Vec<OpenAIStreamChoice>,
    pub usage: Option<Usage>,
}

#[derive(Deserialize, Debug)]
pub struct OpenAIStreamChoice {
    pub delta: OpenAIStreamDelta,
}

#[derive(Deserialize, Debug)]
pub struct OpenAIStreamDelta {
    pub content: Option<String>,
    #[serde(default, alias = "reasoning")]
    pub reasoning_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<StreamToolCall>>,
}

/// Tool call represents a function call that an LLM wants to make.
/// This is a standardized structure used across all providers.
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct StreamToolCall {
    /// The ID of the tool call.
    pub id: Option<String>,
    /// The type of the tool call (defaults to "function" if not provided).
    #[serde(rename = "type", default = "default_call_type")]
    pub call_type: String,
    /// The function to call.
    pub function: StreamFunctionCall,
}

/// FunctionCall contains details about which function to call and with what arguments.
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct StreamFunctionCall {
    /// The name of the function to call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The arguments to pass to the function, typically serialized as a JSON string.
    pub arguments: String,
}

impl From<StructuredOutputFormat> for OpenAIResponseFormat {
    fn from(structured_response_format: StructuredOutputFormat) -> Self {
        match structured_response_format.schema {
            None => OpenAIResponseFormat {
                response_type: OpenAIResponseType::JsonSchema,
                json_schema: Some(structured_response_format),
            },
            Some(mut schema) => {
                schema = if schema.get("additionalProperties").is_none() {
                    schema["additionalProperties"] = serde_json::json!(false);
                    schema
                } else {
                    schema
                };
                OpenAIResponseFormat {
                    response_type: OpenAIResponseType::JsonSchema,
                    json_schema: Some(StructuredOutputFormat {
                        name: structured_response_format.name,
                        description: structured_response_format.description,
                        schema: Some(schema),
                        strict: structured_response_format.strict,
                    }),
                }
            }
        }
    }
}

impl ChatResponse for OpenAIChatResponse {
    fn text(&self) -> Option<String> {
        self.choices.first().and_then(|c| c.message.content.clone())
    }

    fn tool_calls(&self) -> Option<Vec<ToolCall>> {
        self.choices
            .first()
            .and_then(|c| c.message.tool_calls.clone())
    }

    fn thinking(&self) -> Option<String> {
        self.choices
            .first()
            .and_then(|c| c.message.reasoning_content.clone())
    }

    fn usage(&self) -> Option<Usage> {
        self.usage.clone()
    }
}

impl std::fmt::Display for OpenAIChatResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (
            &self.choices.first().unwrap().message.content,
            &self.choices.first().unwrap().message.tool_calls,
        ) {
            (Some(content), Some(tool_calls)) => {
                for tool_call in tool_calls {
                    write!(f, "{tool_call}")?;
                }
                write!(f, "{content}")
            }
            (Some(content), None) => write!(f, "{content}"),
            (None, Some(tool_calls)) => {
                for tool_call in tool_calls {
                    write!(f, "{tool_call}")?;
                }
                Ok(())
            }
            (None, None) => write!(f, ""),
        }
    }
}

impl<T: OpenAIProviderConfig> OpenAICompatibleProvider<T> {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        api_key: impl Into<String>,
        base_url: Option<String>,
        model: Option<String>,
        max_tokens: Option<u32>,
        temperature: Option<f32>,
        timeout_seconds: Option<u64>,
        top_p: Option<f32>,
        top_k: Option<u32>,
        tool_choice: Option<ToolChoice>,
        reasoning_effort: Option<String>,
        voice: Option<String>,
        extra_body: Option<serde_json::Value>,
        parallel_tool_calls: Option<bool>,
        normalize_response: Option<bool>,
        embedding_encoding_format: Option<String>,
        embedding_dimensions: Option<u32>,
    ) -> Self {
        let timeout_seconds = resolve_request_timeout(timeout_seconds);
        #[cfg(not(target_arch = "wasm32"))]
        let client = {
            let _ = timeout_seconds; // silence unused warnings on wasm32 below
            Client::builder()
                .timeout(std::time::Duration::from_secs(timeout_seconds))
                .build()
                .expect("Failed to build reqwest Client")
        };
        let extra_body = match extra_body {
            Some(serde_json::Value::Object(map)) => map,
            _ => serde_json::Map::new(), // Should we panic here?
        };
        Self {
            api_key: api_key.into(),
            base_url: Url::parse(&format!(
                "{}/",
                base_url
                    .unwrap_or_else(|| T::DEFAULT_BASE_URL.to_owned())
                    .trim_end_matches("/")
            ))
            .expect("Failed to parse base URL"),
            model: model.unwrap_or_else(|| T::DEFAULT_MODEL.to_string()),
            max_tokens,
            temperature,
            timeout_seconds,
            top_p,
            top_k,
            tool_choice,
            reasoning_effort,
            voice,
            extra_body,
            parallel_tool_calls: parallel_tool_calls.unwrap_or(false),
            normalize_response: normalize_response.unwrap_or(true),
            embedding_encoding_format,
            embedding_dimensions,
            #[cfg(not(target_arch = "wasm32"))]
            client,
            _phantom: PhantomData,
        }
    }

    /// Builds the OpenAI-compatible chat message list for a request.
    ///
    /// Only used by the native chat-completions / streaming paths; the WASI
    /// Preview2 transport only supports the Responses API and does not call this.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn prepare_messages(
        &self,
        messages: &[ChatMessage],
    ) -> Result<Vec<OpenAIChatMessage<'_>>, LLMError> {
        let mut openai_msgs = Vec::new();
        for msg in messages {
            if let MessageType::ToolResult(ref results) = msg.message_type {
                // Expand ToolResult into multiple messages.
                openai_msgs.extend(results.iter().map(|result| OpenAIChatMessage {
                    role: "tool",
                    tool_call_id: Some(result.id.clone()),
                    tool_calls: None,
                    content: Some(Right(result.function.arguments.clone())),
                }));
            } else {
                openai_msgs.push(chat_message_to_openai_message(msg.clone())?);
            }
        }
        Ok(openai_msgs)
    }
}

/// Native chat-completions / streaming transport. These impls and helpers go
/// through `reqwest` and are only compiled on non-wasm32 targets; the WASI
/// Preview2 transport uses `golem-wasi-http` over the `wasi:http` host interface.
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl<T: OpenAIProviderConfig> ChatProvider for OpenAICompatibleProvider<T> {
    /// Perform a chat request with tool calls
    async fn chat_with_tools(
        &self,
        messages: &[ChatMessage],
        tools: Option<&[Tool]>,
        json_schema: Option<StructuredOutputFormat>,
    ) -> Result<Box<dyn ChatResponse>, LLMError> {
        if self.api_key.is_empty() {
            return Err(LLMError::missing_api_key(format!(
                "Missing {} API key",
                T::PROVIDER_NAME
            )));
        }
        let openai_msgs = self.prepare_messages(messages)?;
        let response_format: Option<OpenAIResponseFormat> = if T::SUPPORTS_STRUCTURED_OUTPUT {
            json_schema.clone().map(|s| s.into())
        } else {
            None
        };
        let request_tools = tools.map(|t| t.to_vec());
        let request_tool_choice = if request_tools.is_some() {
            self.tool_choice.clone()
        } else {
            None
        };
        let reasoning_effort = if T::SUPPORTS_REASONING_EFFORT {
            self.reasoning_effort.clone()
        } else {
            None
        };
        let parallel_tool_calls = if T::SUPPORTS_PARALLEL_TOOL_CALLS {
            Some(self.parallel_tool_calls)
        } else {
            None
        };
        let body = OpenAIChatRequest {
            model: &self.model,
            messages: openai_msgs,
            max_tokens: self.max_tokens,
            temperature: self.temperature,
            stream: false,
            top_p: self.top_p,
            top_k: self.top_k,
            tools: request_tools,
            tool_choice: request_tool_choice,
            reasoning_effort,
            response_format,
            stream_options: None,
            parallel_tool_calls,
            extra_body: self.extra_body.clone(),
        };
        let url = self
            .base_url
            .join(T::CHAT_ENDPOINT)
            .map_err(|e| LLMError::HttpError(e.to_string()))?;
        let mut request = self.client.post(url).bearer_auth(&self.api_key).json(&body);
        // Add custom headers if provider specifies them
        if let Some(headers) = T::custom_headers() {
            for (key, value) in headers {
                request = request.header(key, value);
            }
        }
        if log::log_enabled!(log::Level::Trace) {
            log::trace!(
                "{}",
                crate::request_diagnostics::summarize_json_request(
                    T::PROVIDER_NAME,
                    "chat request",
                    &body
                )
            );
        }
        let response = request.send().await?;
        log::debug!("{} HTTP status: {}", T::PROVIDER_NAME, response.status());
        let response = ensure_success(response, T::PROVIDER_NAME).await?;
        let resp_text = response.text().await?;
        let json_resp: Result<OpenAIChatResponse, serde_json::Error> =
            serde_json::from_str(&resp_text);
        match json_resp {
            Ok(response) => Ok(Box::new(response)),
            Err(e) => Err(LLMError::ResponseFormatError {
                message: format!("Failed to decode {} API response: {e}", T::PROVIDER_NAME),
                raw_response: resp_text,
            }),
        }
    }

    /// Perform a chat request without tool calls
    async fn chat(
        &self,
        messages: &[ChatMessage],
        json_schema: Option<StructuredOutputFormat>,
    ) -> Result<Box<dyn ChatResponse>, LLMError> {
        self.chat_with_tools(messages, None, json_schema).await
    }

    /// Stream chat responses as a stream of strings
    async fn chat_stream(
        &self,
        messages: &[ChatMessage],
        json_schema: Option<StructuredOutputFormat>,
    ) -> Result<std::pin::Pin<Box<dyn Stream<Item = Result<String, LLMError>> + Send>>, LLMError>
    {
        let struct_stream = self.chat_stream_struct(messages, None, json_schema).await?;
        let content_stream = struct_stream.filter_map(|result| async move {
            match result {
                Ok(stream_response) => {
                    if let Some(choice) = stream_response.choices.first()
                        && let Some(content) = &choice.delta.content
                        && !content.is_empty()
                    {
                        return Some(Ok(content.clone()));
                    }
                    None
                }
                Err(e) => Some(Err(e)),
            }
        });
        Ok(Box::pin(content_stream))
    }

    /// Stream chat responses as `ChatMessage` structured objects, including usage information
    async fn chat_stream_struct(
        &self,
        messages: &[ChatMessage],
        tools: Option<&[Tool]>,
        json_schema: Option<StructuredOutputFormat>,
    ) -> Result<
        std::pin::Pin<Box<dyn Stream<Item = Result<StreamResponse, LLMError>> + Send>>,
        LLMError,
    > {
        if self.api_key.is_empty() {
            return Err(LLMError::missing_api_key(format!(
                "Missing {} API key",
                T::PROVIDER_NAME
            )));
        }
        let openai_msgs = self.prepare_messages(messages)?;
        let request_tools = tools.map(|t| t.to_vec());

        let response_format: Option<OpenAIResponseFormat> = if T::SUPPORTS_STRUCTURED_OUTPUT {
            json_schema.clone().map(|s| s.into())
        } else {
            None
        };

        let body = OpenAIChatRequest {
            model: &self.model,
            messages: openai_msgs,
            max_tokens: self.max_tokens,
            temperature: self.temperature,
            stream: true,
            top_p: self.top_p,
            top_k: self.top_k,
            tools: request_tools,
            tool_choice: self.tool_choice.clone(),
            reasoning_effort: if T::SUPPORTS_REASONING_EFFORT {
                self.reasoning_effort.clone()
            } else {
                None
            },
            response_format,
            stream_options: if T::SUPPORTS_STREAM_OPTIONS {
                Some(OpenAIStreamOptions {
                    include_usage: true,
                })
            } else {
                None
            },
            parallel_tool_calls: if T::SUPPORTS_PARALLEL_TOOL_CALLS {
                Some(self.parallel_tool_calls)
            } else {
                None
            },
            extra_body: self.extra_body.clone(),
        };
        let url = self
            .base_url
            .join(T::CHAT_ENDPOINT)
            .map_err(|e| LLMError::HttpError(e.to_string()))?;
        let mut request = self.client.post(url).bearer_auth(&self.api_key).json(&body);
        if let Some(headers) = T::custom_headers() {
            for (key, value) in headers {
                request = request.header(key, value);
            }
        }
        if log::log_enabled!(log::Level::Trace) {
            log::trace!(
                "{}",
                crate::request_diagnostics::summarize_json_request(
                    T::PROVIDER_NAME,
                    "stream request",
                    &body
                )
            );
        }
        let response = request.send().await?;
        log::debug!("{} HTTP status: {}", T::PROVIDER_NAME, response.status());
        let response = ensure_success(response, T::PROVIDER_NAME).await?;
        Ok(create_sse_stream(response, self.normalize_response))
    }

    /// Sends a streaming chat request with tool support.
    ///
    /// Returns a stream of `StreamChunk` which can be text deltas or tool call events.
    /// This method provides a unified interface for streaming with tools across
    /// OpenAI-compatible providers.
    ///
    /// # Arguments
    ///
    /// * `messages` - Slice of chat messages representing the conversation
    /// * `tools` - Optional slice of tools available for the model to use
    ///
    /// # Returns
    ///
    /// A stream of `StreamChunk` items or an error
    async fn chat_stream_with_tools(
        &self,
        messages: &[ChatMessage],
        tools: Option<&[Tool]>,
        json_schema: Option<StructuredOutputFormat>,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<ChatStreamChunk, LLMError>> + Send>>, LLMError>
    {
        if self.api_key.is_empty() {
            return Err(LLMError::missing_api_key(format!(
                "Missing {} API key",
                T::PROVIDER_NAME
            )));
        }

        let openai_msgs = self.prepare_messages(messages)?;

        let requested_tools = tools.map(|t| t.to_vec());

        let response_format: Option<OpenAIResponseFormat> = if T::SUPPORTS_STRUCTURED_OUTPUT {
            json_schema.clone().map(|s| s.into())
        } else {
            None
        };

        let body = OpenAIChatRequest {
            model: &self.model,
            messages: openai_msgs,
            max_tokens: self.max_tokens,
            temperature: self.temperature,
            stream: true,
            top_p: self.top_p,
            top_k: self.top_k,
            tools: requested_tools,
            tool_choice: self.tool_choice.clone(),
            reasoning_effort: if T::SUPPORTS_REASONING_EFFORT {
                self.reasoning_effort.clone()
            } else {
                None
            },
            response_format,
            stream_options: if T::SUPPORTS_STREAM_OPTIONS {
                Some(OpenAIStreamOptions {
                    include_usage: true,
                })
            } else {
                None
            },
            parallel_tool_calls: if T::SUPPORTS_PARALLEL_TOOL_CALLS {
                Some(self.parallel_tool_calls)
            } else {
                None
            },
            extra_body: self.extra_body.clone(),
        };

        let url = self
            .base_url
            .join(T::CHAT_ENDPOINT)
            .map_err(|e| LLMError::HttpError(e.to_string()))?;

        let mut request = self.client.post(url).bearer_auth(&self.api_key).json(&body);

        if let Some(headers) = T::custom_headers() {
            for (key, value) in headers {
                request = request.header(key, value);
            }
        }

        if log::log_enabled!(log::Level::Trace) {
            log::trace!(
                "{}",
                crate::request_diagnostics::summarize_json_request(
                    T::PROVIDER_NAME,
                    "streaming tools request",
                    &body
                )
            );
        }

        log::debug!(
            "{} request: POST {} (streaming with tools)",
            T::PROVIDER_NAME,
            T::CHAT_ENDPOINT
        );
        let response = request.send().await?;
        log::debug!("{} HTTP status: {}", T::PROVIDER_NAME, response.status());
        let response = ensure_success(response, T::PROVIDER_NAME).await?;

        Ok(create_openai_tool_stream(response))
    }

    fn model(&self) -> &str {
        &self.model
    }
}

/// State for tracking tool use blocks during OpenAI-compatible streaming
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Default)]
struct OpenAIToolUseState {
    /// Tool ID
    id: String,
    /// Tool name
    name: String,
    /// Accumulated JSON arguments
    arguments_buffer: String,
    /// Whether we've emitted the start event
    started: bool,
}

/// Creates an SSE stream that parses OpenAI-compatible tool use events into ChatStreamChunk.
#[cfg(not(target_arch = "wasm32"))]
fn create_openai_tool_stream(
    response: reqwest::Response,
) -> Pin<Box<dyn Stream<Item = Result<ChatStreamChunk, LLMError>> + Send>> {
    let stream = response
        .bytes_stream()
        .scan(
            (
                Vec::<u8>::new(),
                HashMap::<usize, OpenAIToolUseState>::default(),
            ),
            move |(buffer, tool_states), chunk| {
                let result = match chunk {
                    Ok(bytes) => {
                        let mut results = Vec::new();
                        buffer.extend_from_slice(&bytes);

                        // Process complete SSE events separated by LF or CRLF blank lines.
                        while let Some((pos, delimiter_len)) = find_sse_event_boundary(buffer) {
                            let event_bytes: Vec<u8> = buffer[..pos].to_vec();
                            buffer.drain(..pos + delimiter_len);

                            let event = String::from_utf8_lossy(&event_bytes).into_owned();
                            let event = event.trim();
                            if event.is_empty() {
                                continue;
                            }

                            match parse_openai_sse_chunk_with_tools(event, tool_states) {
                                Ok(chunks) => results.extend(chunks.into_iter().map(Ok)),
                                Err(e) => results.push(Err(e)),
                            }
                        }

                        Some(results)
                    }
                    Err(e) => Some(vec![Err(LLMError::HttpError(e.to_string()))]),
                };

                async move { result }
            },
        )
        .flat_map(futures::stream::iter);

    Box::pin(stream)
}

#[cfg(not(target_arch = "wasm32"))]
fn find_sse_event_boundary(buffer: &[u8]) -> Option<(usize, usize)> {
    let lf = buffer
        .windows(2)
        .position(|window| window == b"\n\n")
        .map(|pos| (pos, 2));
    let crlf = buffer
        .windows(4)
        .position(|window| window == b"\r\n\r\n")
        .map(|pos| (pos, 4));

    match (lf, crlf) {
        (Some(left), Some(right)) => Some(if left.0 <= right.0 { left } else { right }),
        (Some(boundary), None) | (None, Some(boundary)) => Some(boundary),
        (None, None) => None,
    }
}

/// Parses OpenAI-compatible SSE chunks with tool use support.
#[cfg(not(target_arch = "wasm32"))]
fn parse_openai_sse_chunk_with_tools(
    event: &str,
    tool_states: &mut HashMap<usize, OpenAIToolUseState>,
) -> Result<Vec<ChatStreamChunk>, LLMError> {
    let mut results = Vec::new();

    for line in event.lines() {
        let line = line.trim();

        // Accept both "data: " and "data:" prefixes (tolerant parsing).
        let data_opt = line
            .strip_prefix("data: ")
            .or_else(|| line.strip_prefix("data:").map(|d| d.trim_start()));

        if let Some(data) = data_opt {
            let data_trimmed = data.trim();

            // End-of-stream sentinel.
            if data_trimmed == "[DONE]" {
                // Emit any remaining tool completions
                for (index, state) in tool_states.drain() {
                    if state.started {
                        results.push(ChatStreamChunk::ToolUseComplete {
                            index,
                            tool_call: ToolCall {
                                id: state.id,
                                call_type: "function".to_string(),
                                function: FunctionCall {
                                    name: state.name,
                                    arguments: state.arguments_buffer,
                                },
                            },
                        });
                    }
                }

                // No usage available here (usage is often sent in a prior event).
                results.push(ChatStreamChunk::Done {
                    stop_reason: "end_turn".to_string(),
                });
                return Ok(results);
            }

            // Try to parse JSON chunk; return parse error so caller can turn into an Err item.
            let chunk: OpenAIToolStreamChunk = serde_json::from_str(data_trimmed)
                .map_err(|e| LLMError::JsonError(e.to_string()))?;

            // Capture usage if present (we may emit it either immediately or after completions).
            let mut usage_opt = chunk.usage.clone();

            for choice in &chunk.choices {
                // Handle text content
                if let Some(content) = &choice.delta.content
                    && !content.is_empty()
                {
                    results.push(ChatStreamChunk::Text(content.clone()));
                }
                if let Some(reasoning_content) = &choice.delta.reasoning_content
                    && !reasoning_content.is_empty()
                {
                    results.push(ChatStreamChunk::ReasoningContent(reasoning_content.clone()));
                }

                // Handle tool calls (per-index)
                if let Some(tool_calls) = &choice.delta.tool_calls {
                    for tc in tool_calls {
                        let index = tc.index.unwrap_or(0);
                        let state = tool_states.entry(index).or_default();

                        // First chunk can contain id and name
                        if let Some(id) = &tc.id {
                            state.id = id.clone();
                        }
                        if let Some(name) = &tc.function.name {
                            state.name = name.clone();

                            // Emit ToolUseStart if not already started
                            if !state.started {
                                state.started = true;
                                results.push(ChatStreamChunk::ToolUseStart {
                                    index,
                                    id: state.id.clone(),
                                    name: state.name.clone(),
                                });
                            }
                        }

                        // Accumulate arguments and emit the delta event so consumers can stream partial JSON.
                        if !tc.function.arguments.is_empty() {
                            state.arguments_buffer.push_str(&tc.function.arguments);
                            results.push(ChatStreamChunk::ToolUseInputDelta {
                                index,
                                partial_json: tc.function.arguments.clone(),
                            });
                        }
                    }
                }

                // Handle finish_reason (map to Done stop_reason and flush completions)
                if let Some(finish_reason) = &choice.finish_reason {
                    // Emit tool completions before Done
                    for (index, state) in tool_states.drain() {
                        if state.started {
                            results.push(ChatStreamChunk::ToolUseComplete {
                                index,
                                tool_call: ToolCall {
                                    id: state.id,
                                    call_type: "function".to_string(),
                                    function: FunctionCall {
                                        name: state.name,
                                        arguments: state.arguments_buffer,
                                    },
                                },
                            });
                        }
                    }

                    // If usage was present in this chunk, emit it now (after completions, before Done).
                    if let Some(u) = usage_opt.take() {
                        results.push(ChatStreamChunk::Usage(u));
                    }

                    let stop_reason = match finish_reason.as_str() {
                        "tool_calls" => "tool_use",
                        "stop" => "end_turn",
                        other => other,
                    };
                    results.push(ChatStreamChunk::Done {
                        stop_reason: stop_reason.to_string(),
                    });
                }
            }

            // If no finish_reason was found but usage was present in this chunk,
            // emit the usage now (consume it so it is not emitted again).
            if let Some(u) = usage_opt.take() {
                results.push(ChatStreamChunk::Usage(u));
            }
        }
    }

    Ok(results)
}

/// OpenAI streaming chunk structure for tool parsing
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Deserialize)]
struct OpenAIToolStreamChunk {
    choices: Vec<OpenAIToolStreamChoice>,
    /// Optional usage metadata (often appears in its own SSE event).
    #[serde(default)]
    usage: Option<Usage>,
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Deserialize)]
struct OpenAIToolStreamChoice {
    delta: OpenAIToolStreamDelta,
    finish_reason: Option<String>,
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Deserialize)]
struct OpenAIToolStreamDelta {
    content: Option<String>,
    #[serde(default, alias = "reasoning")]
    reasoning_content: Option<String>,
    tool_calls: Option<Vec<OpenAIToolStreamToolCall>>,
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Deserialize)]
struct OpenAIToolStreamToolCall {
    index: Option<usize>,
    id: Option<String>,
    function: OpenAIToolStreamFunction,
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Deserialize)]
struct OpenAIToolStreamFunction {
    name: Option<String>,
    #[serde(default)]
    arguments: String,
}

/// Create OpenAICompatibleChatMessage` that doesn't borrow from any temporary variables
#[cfg(not(target_arch = "wasm32"))]
pub fn chat_message_to_openai_message(
    chat_msg: ChatMessage,
) -> Result<OpenAIChatMessage<'static>, LLMError> {
    let message = OpenAIChatMessage {
        role: match chat_msg.role {
            ChatRole::User => "user",
            ChatRole::Assistant => "assistant",
            ChatRole::System => "system",
            ChatRole::Tool => "user",
        },
        tool_call_id: None,
        content: match &chat_msg.message_type {
            MessageType::Text => Some(Right(chat_msg.content.clone())),
            MessageType::Image((mime, bytes)) => {
                let url = format!("data:{};base64,{}", mime.mime_type(), BASE64.encode(bytes));
                Some(Left(vec![OpenAIMessageContent {
                    message_type: Some("image_url"),
                    text: None,
                    image_url: Some(ImageUrlContent { url }),
                    tool_output: None,
                    tool_call_id: None,
                }]))
            }
            MessageType::Pdf(_) => {
                return Err(LLMError::invalid_request(
                    "PDF input is not supported by OpenAI-compatible chat completions backends"
                        .to_string(),
                ));
            }
            MessageType::ImageURL(url) => Some(Left(vec![OpenAIMessageContent {
                message_type: Some("image_url"),
                text: None,
                image_url: Some(ImageUrlContent { url: url.clone() }),
                tool_output: None,
                tool_call_id: None,
            }])),
            MessageType::ToolUse(_) => None,
            MessageType::ToolResult(_) => None,
        },
        tool_calls: match &chat_msg.message_type {
            MessageType::ToolUse(calls) => {
                let owned_calls: Vec<ToolCall> = calls
                    .iter()
                    .map(|c| ToolCall {
                        id: c.id.clone(),
                        call_type: "function".to_string(),
                        function: FunctionCall {
                            name: c.function.name.clone(),
                            arguments: c.function.arguments.clone(),
                        },
                    })
                    .collect();
                Some(owned_calls)
            }
            _ => None,
        },
    };
    Ok(message)
}

#[cfg(not(target_arch = "wasm32"))]
struct SSEStreamParser {
    event_buffer: Vec<u8>,
    tool_buffer: ToolCall,
    usage: Option<Usage>,
    results: Vec<Result<StreamResponse, LLMError>>,
    normalize_response: bool,
}

#[cfg(not(target_arch = "wasm32"))]
impl SSEStreamParser {
    fn new(normalize_response: bool) -> Self {
        Self {
            event_buffer: Vec::default(),
            usage: None,
            results: Vec::default(),
            tool_buffer: ToolCall {
                id: String::default(),
                call_type: "function".to_string(),
                function: FunctionCall {
                    name: String::default(),
                    arguments: String::default(),
                },
            },
            normalize_response,
        }
    }

    /// Push the current `tool_buffer` as a `StreamResponse` and reset it
    fn push_tool_call(&mut self) {
        if self.normalize_response && !self.tool_buffer.function.name.is_empty() {
            self.results.push(Ok(StreamResponse {
                choices: vec![StreamChoice {
                    delta: StreamDelta {
                        content: None,
                        reasoning_content: None,
                        tool_calls: Some(vec![self.tool_buffer.clone()]),
                    },
                }],
                usage: None,
            }));
        }
        self.tool_buffer = ToolCall {
            id: String::default(),
            call_type: "function".to_string(),
            function: FunctionCall {
                name: String::default(),
                arguments: String::default(),
            },
        };
    }

    /// Parse the accumulated event_buffer as one SSE event
    fn parse_event(&mut self) {
        let event = String::from_utf8_lossy(&self.event_buffer);
        let mut data_payload = String::default();
        for line in event.lines() {
            if let Some(data) = line.strip_prefix("data: ") {
                if data == "[DONE]" {
                    self.push_tool_call();
                    if let Some(usage) = self.usage.clone() {
                        self.results.push(Ok(StreamResponse {
                            choices: vec![StreamChoice {
                                delta: StreamDelta {
                                    content: None,
                                    reasoning_content: None,
                                    tool_calls: None,
                                },
                            }],
                            usage: Some(usage),
                        }));
                    }
                    return;
                }
                data_payload.push_str(data);
            } else {
                data_payload.push_str(line);
            }
        }
        if data_payload.is_empty() {
            return;
        }
        if let Ok(response) = serde_json::from_str::<StreamChunk>(&data_payload) {
            if let Some(resp_usage) = response.usage.clone() {
                self.usage = Some(resp_usage);
            }
            for choice in &response.choices {
                let content = choice.delta.content.clone();
                let reasoning_content = choice.delta.reasoning_content.clone();
                let tool_calls: Option<Vec<ToolCall>> =
                    choice.delta.tool_calls.clone().map(|calls| {
                        calls
                            .into_iter()
                            .map(|c| ToolCall {
                                id: c.id.unwrap_or_default(),
                                call_type: c.call_type,
                                function: FunctionCall {
                                    name: c.function.name.unwrap_or_default(),
                                    arguments: c.function.arguments,
                                },
                            })
                            .collect::<Vec<ToolCall>>()
                    });
                if content.is_some() || reasoning_content.is_some() || tool_calls.is_some() {
                    if self.normalize_response && tool_calls.is_some() {
                        if let Some(calls) = &tool_calls {
                            for call in calls {
                                if !call.function.name.is_empty() {
                                    self.push_tool_call();
                                    self.tool_buffer
                                        .function
                                        .name
                                        .clone_from(&call.function.name);
                                }
                                if !call.function.arguments.is_empty() {
                                    self.tool_buffer
                                        .function
                                        .arguments
                                        .push_str(&call.function.arguments);
                                }
                                if !call.id.is_empty() {
                                    self.tool_buffer.id.clone_from(&call.id);
                                }
                                if !call.call_type.is_empty() {
                                    self.tool_buffer.call_type.clone_from(&call.call_type);
                                }
                            }
                        }
                    } else {
                        self.push_tool_call();
                        self.results.push(Ok(StreamResponse {
                            choices: vec![StreamChoice {
                                delta: StreamDelta {
                                    content,
                                    reasoning_content,
                                    tool_calls,
                                },
                            }],
                            usage: None,
                        }));
                    }
                }
            }
        }
    }

    fn consume_bytes(&mut self, bytes: &[u8]) -> Vec<Result<StreamResponse, LLMError>> {
        self.event_buffer.extend_from_slice(bytes);

        while let Some((pos, delimiter_len)) = find_sse_event_boundary(&self.event_buffer) {
            let event_bytes = self.event_buffer[..pos].to_vec();
            self.event_buffer.drain(..pos + delimiter_len);
            self.event_buffer = event_bytes;
            self.parse_event();
            self.event_buffer.clear();
        }

        self.results.drain(..).collect::<Vec<_>>()
    }

    #[cfg(test)]
    fn consume_text(&mut self, text: &str) -> Vec<Result<StreamResponse, LLMError>> {
        self.consume_bytes(text.as_bytes())
    }
}

/// Creates a structured SSE stream that returns `StreamResponse` objects
///
/// Buffer required to accumulate JSON payload lines that are split across multiple SSE chunks
#[cfg(not(target_arch = "wasm32"))]
pub fn create_sse_stream(
    response: reqwest::Response,
    normalize_response: bool,
) -> std::pin::Pin<Box<dyn Stream<Item = Result<StreamResponse, LLMError>> + Send>> {
    let bytes_stream = response.bytes_stream();
    let stream = bytes_stream
        .scan(SSEStreamParser::new(normalize_response), |parser, chunk| {
            let results = match chunk {
                Ok(bytes) => parser.consume_bytes(&bytes),
                Err(e) => vec![Err(LLMError::HttpError(e.to_string()))],
            };
            futures::future::ready(Some(results))
        })
        .flat_map(futures::stream::iter);
    Box::pin(stream)
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use crate::chat::FunctionTool;
    use futures::StreamExt;
    use httpmock::{Method::POST, MockServer};
    use serde_json::json;

    struct TestConfig;

    impl OpenAIProviderConfig for TestConfig {
        const PROVIDER_NAME: &'static str = "Test";
        const DEFAULT_BASE_URL: &'static str = "https://example.com/v1/";
        const DEFAULT_MODEL: &'static str = "test-model";
    }

    struct FullSupportConfig;

    impl OpenAIProviderConfig for FullSupportConfig {
        const PROVIDER_NAME: &'static str = "FullTest";
        const DEFAULT_BASE_URL: &'static str = "https://example.com/v1/";
        const DEFAULT_MODEL: &'static str = "full-model";
        const SUPPORTS_REASONING_EFFORT: bool = true;
        const SUPPORTS_STRUCTURED_OUTPUT: bool = true;
        const SUPPORTS_PARALLEL_TOOL_CALLS: bool = true;
        const SUPPORTS_STREAM_OPTIONS: bool = true;

        fn custom_headers() -> Option<Vec<(String, String)>> {
            Some(vec![("x-provider".to_string(), "enabled".to_string())])
        }
    }

    fn sample_function_tool() -> Tool {
        Tool {
            tool_type: "function".to_string(),
            function: FunctionTool {
                name: "lookup".to_string(),
                description: "Lookup data".to_string(),
                parameters: json!({
                    "type": "object",
                    "properties": {
                        "q": { "type": "string" }
                    },
                    "required": ["q"]
                }),
            },
        }
    }

    fn sample_schema() -> StructuredOutputFormat {
        StructuredOutputFormat {
            name: "Answer".to_string(),
            description: Some("Structured answer".to_string()),
            schema: Some(json!({
                "type": "object",
                "properties": {
                    "answer": { "type": "string" }
                },
                "required": ["answer"]
            })),
            strict: Some(true),
        }
    }

    fn full_support_provider(base_url: String) -> OpenAICompatibleProvider<FullSupportConfig> {
        OpenAICompatibleProvider::<FullSupportConfig>::new(
            "key",
            Some(base_url),
            Some("full-model".to_string()),
            Some(128),
            Some(0.2),
            Some(5),
            Some(0.9),
            Some(10),
            Some(ToolChoice::Auto),
            Some("high".to_string()),
            None,
            Some(json!({"seed": 7})),
            Some(true),
            Some(false),
            None,
            None,
        )
    }

    #[test]
    fn test_parse_openai_stream_text_delta() {
        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}"#;
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::Text(text) => assert_eq!(text, "Hello"),
            _ => panic!("Expected Text chunk, got {:?}", results[0]),
        }
    }

    #[test]
    fn test_parse_openai_stream_reasoning_delta() {
        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"reasoning_content":"think"},"finish_reason":null}]}"#;
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::ReasoningContent(text) => assert_eq!(text, "think"),
            _ => panic!("Expected ReasoningContent chunk, got {:?}", results[0]),
        }
    }

    #[test]
    fn test_parse_openai_stream_tool_call_start() {
        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_abc123","type":"function","function":{"name":"get_weather","arguments":""}}]},"finish_reason":null}]}"#;
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::ToolUseStart { index, id, name } => {
                assert_eq!(*index, 0);
                assert_eq!(id, "call_abc123");
                assert_eq!(name, "get_weather");
            }
            _ => panic!("Expected ToolUseStart chunk, got {:?}", results[0]),
        }

        // Verify state was stored
        assert!(tool_states.contains_key(&0));
        assert_eq!(tool_states[&0].id, "call_abc123");
        assert_eq!(tool_states[&0].name, "get_weather");
        assert!(tool_states[&0].started);
    }

    #[test]
    fn test_parse_openai_stream_tool_call_arguments_delta() {
        // First, set up tool state as if start was already processed
        let mut tool_states = HashMap::default();
        tool_states.insert(
            0,
            OpenAIToolUseState {
                id: "call_abc123".to_string(),
                name: "get_weather".to_string(),
                arguments_buffer: String::default(),
                started: true,
            },
        );

        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"location\":"}}]},"finish_reason":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::ToolUseInputDelta {
                index,
                partial_json,
            } => {
                assert_eq!(*index, 0);
                assert_eq!(partial_json, "{\"location\":");
            }
            _ => panic!("Expected ToolUseInputDelta chunk, got {:?}", results[0]),
        }

        // Verify arguments were accumulated
        assert_eq!(tool_states[&0].arguments_buffer, "{\"location\":");
    }

    #[test]
    fn test_parse_openai_stream_finish_reason_tool_calls() {
        let mut tool_states = HashMap::new();
        tool_states.insert(
            0,
            OpenAIToolUseState {
                id: "call_abc123".to_string(),
                name: "get_weather".to_string(),
                arguments_buffer: r#"{"location": "Paris"}"#.to_string(),
                started: true,
            },
        );

        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#;
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        // Should have ToolUseComplete and Done
        assert_eq!(results.len(), 2);

        match &results[0] {
            ChatStreamChunk::ToolUseComplete { index, tool_call } => {
                assert_eq!(*index, 0);
                assert_eq!(tool_call.id, "call_abc123");
                assert_eq!(tool_call.function.name, "get_weather");
                assert_eq!(tool_call.function.arguments, r#"{"location": "Paris"}"#);
            }
            _ => panic!("Expected ToolUseComplete chunk, got {:?}", results[0]),
        }

        match &results[1] {
            ChatStreamChunk::Done { stop_reason } => {
                assert_eq!(stop_reason, "tool_use");
            }
            _ => panic!("Expected Done chunk, got {:?}", results[1]),
        }

        // Verify state was cleared
        assert!(tool_states.is_empty());
    }

    #[test]
    fn test_parse_openai_stream_finish_reason_stop() {
        let event = r#"data: {"id":"chatcmpl-123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#;
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::Done { stop_reason } => {
                assert_eq!(stop_reason, "end_turn");
            }
            _ => panic!("Expected Done chunk, got {:?}", results[0]),
        }
    }

    #[test]
    fn test_parse_openai_stream_done_marker() {
        let event = "data: [DONE]";
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 1);
        match &results[0] {
            ChatStreamChunk::Done { stop_reason } => {
                assert_eq!(stop_reason, "end_turn");
            }
            _ => panic!("Expected Done chunk, got {:?}", results[0]),
        }
    }

    #[test]
    fn test_parse_openai_stream_done_marker_with_pending_tool() {
        let mut tool_states = HashMap::new();
        tool_states.insert(
            0,
            OpenAIToolUseState {
                id: "call_xyz".to_string(),
                name: "some_function".to_string(),
                arguments_buffer: "{}".to_string(),
                started: true,
            },
        );

        let event = "data: [DONE]";
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        // Should emit ToolUseComplete before Done
        assert_eq!(results.len(), 2);
        assert!(matches!(
            &results[0],
            ChatStreamChunk::ToolUseComplete { .. }
        ));
        assert!(matches!(&results[1], ChatStreamChunk::Done { .. }));
    }

    #[test]
    fn test_parse_openai_stream_full_tool_sequence() {
        let mut tool_states = HashMap::new();

        // 1. Tool call start with name
        let start_event = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_abc","type":"function","function":{"name":"get_weather","arguments":""}}]},"finish_reason":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(start_event, &mut tool_states).unwrap();
        assert!(
            matches!(&results[0], ChatStreamChunk::ToolUseStart { name, .. } if name == "get_weather")
        );

        // 2. Arguments delta 1
        let delta1 = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"loc"}}]},"finish_reason":null}]}"#;
        let _ = parse_openai_sse_chunk_with_tools(delta1, &mut tool_states).unwrap();

        // 3. Arguments delta 2
        let delta2 = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"ation\":\"Tokyo\"}"}}]},"finish_reason":null}]}"#;
        let _ = parse_openai_sse_chunk_with_tools(delta2, &mut tool_states).unwrap();

        // Verify accumulated arguments
        assert_eq!(tool_states[&0].arguments_buffer, "{\"location\":\"Tokyo\"}");

        // 4. Finish reason
        let finish_event = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#;
        let results = parse_openai_sse_chunk_with_tools(finish_event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 2);
        match &results[0] {
            ChatStreamChunk::ToolUseComplete { tool_call, .. } => {
                assert_eq!(tool_call.function.arguments, "{\"location\":\"Tokyo\"}");
            }
            _ => panic!("Expected ToolUseComplete"),
        }
        assert!(matches!(
            &results[1],
            ChatStreamChunk::Done { stop_reason } if stop_reason == "tool_use"
        ));
    }

    #[test]
    fn test_parse_openai_stream_parallel_tool_calls() {
        let mut tool_states = HashMap::new();

        // Two tool calls in one chunk
        let event = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"get_weather","arguments":""}},{"index":1,"id":"call_2","type":"function","function":{"name":"get_time","arguments":""}}]},"finish_reason":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert_eq!(results.len(), 2);
        assert!(
            matches!(&results[0], ChatStreamChunk::ToolUseStart { index: 0, name, .. } if name == "get_weather")
        );
        assert!(
            matches!(&results[1], ChatStreamChunk::ToolUseStart { index: 1, name, .. } if name == "get_time")
        );

        // Verify both states exist
        assert!(tool_states.contains_key(&0));
        assert!(tool_states.contains_key(&1));
    }

    #[test]
    fn test_parse_openai_stream_ignores_empty_content() {
        let event = r#"data: {"id":"chatcmpl-123","choices":[{"index":0,"delta":{"content":""},"finish_reason":null}]}"#;
        let mut tool_states = HashMap::new();
        let results = parse_openai_sse_chunk_with_tools(event, &mut tool_states).unwrap();

        assert!(results.is_empty());
    }

    #[test]
    fn test_parse_vllm_stream_tool_calls() {
        // vLLM includes extra fields like reasoning_content, type, token_ids
        let mut tool_states = HashMap::new();

        // First chunk from vLLM - just role, empty content
        let first_chunk = r#"data: {"id":"chatcmpl-be8d6d925ff14741","object":"chat.completion.chunk","created":1765374283,"model":"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":null},"logprobs":null,"finish_reason":null}],"prompt_token_ids":null}"#;
        let results = parse_openai_sse_chunk_with_tools(first_chunk, &mut tool_states).unwrap();
        assert!(results.is_empty(), "First chunk should produce no results");

        // Second chunk - tool call start with id, type, index, function.name and function.arguments
        let tool_start = r#"data: {"id":"chatcmpl-be8d6d925ff14741","object":"chat.completion.chunk","created":1765374283,"model":"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ","choices":[{"index":0,"delta":{"reasoning_content":null,"tool_calls":[{"id":"chatcmpl-tool-a331788bab1045a8","type":"function","index":0,"function":{"name":"db_list_databases","arguments":"{\"catalog\":"}}]},"logprobs":null,"finish_reason":null,"token_ids":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(tool_start, &mut tool_states).unwrap();

        // Should have ToolUseStart and ToolUseInputDelta
        assert!(
            !results.is_empty(),
            "Expected at least 1 result, got {:?}",
            results
        );
        assert!(
            matches!(&results[0], ChatStreamChunk::ToolUseStart { name, .. } if name == "db_list_databases"),
            "Expected ToolUseStart, got {:?}",
            results[0]
        );

        // Arguments delta
        let args_delta = r#"data: {"id":"chatcmpl-be8d6d925ff14741","object":"chat.completion.chunk","created":1765374283,"model":"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ","choices":[{"index":0,"delta":{"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":"\"default\"}"}}]},"logprobs":null,"finish_reason":null,"token_ids":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(args_delta, &mut tool_states).unwrap();
        assert!(
            matches!(&results[0], ChatStreamChunk::ToolUseInputDelta { partial_json, .. } if partial_json == "\"default\"}"),
            "Expected ToolUseInputDelta, got {:?}",
            results
        );

        // Finish with stop reason
        let finish = r#"data: {"id":"chatcmpl-be8d6d925ff14741","object":"chat.completion.chunk","created":1765374283,"model":"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ","choices":[{"index":0,"delta":{"reasoning_content":null,"tool_calls":[{"index":0,"function":{"arguments":""}}]},"logprobs":null,"finish_reason":"stop","stop_reason":null,"token_ids":null}]}"#;
        let results = parse_openai_sse_chunk_with_tools(finish, &mut tool_states).unwrap();

        // Should have ToolUseComplete and Done
        assert!(
            results.len() >= 2,
            "Expected ToolUseComplete and Done, got {:?}",
            results
        );
        assert!(
            matches!(&results[0], ChatStreamChunk::ToolUseComplete { tool_call, .. } if tool_call.function.name == "db_list_databases"),
            "Expected ToolUseComplete, got {:?}",
            results[0]
        );
    }

    #[test]
    fn test_sse_stream_parser_preserves_split_utf8_content() {
        let mut parser = SSEStreamParser::new(false);
        let event = b"data: {\"choices\":[{\"delta\":{\"content\":\"Hi \xF0\x9F\x98\x80\"}}]}\n\n";

        let first = parser.consume_bytes(&event[..43]);
        assert!(first.is_empty());

        let second = parser.consume_bytes(&event[43..]);
        assert_eq!(second.len(), 1);
        match &second[0] {
            Ok(StreamResponse { choices, .. }) => {
                assert_eq!(choices[0].delta.content.as_deref(), Some("Hi 😀"));
            }
            other => panic!("Expected content delta, got {other:?}"),
        }
    }

    #[test]
    fn test_sse_stream_parser_handles_crlf_event_boundaries() {
        let mut parser = SSEStreamParser::new(false);
        let results = parser
            .consume_bytes(b"data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\r\n\r\n");

        assert_eq!(results.len(), 1);
        match &results[0] {
            Ok(StreamResponse { choices, .. }) => {
                assert_eq!(choices[0].delta.content.as_deref(), Some("hello"));
            }
            other => panic!("Expected content delta, got {other:?}"),
        }
    }

    #[test]
    fn test_response_format_adds_additional_properties() {
        let format = StructuredOutputFormat {
            name: "Test".to_string(),
            description: None,
            schema: Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "foo": { "type": "string" }
                },
                "required": ["foo"]
            })),
            strict: Some(true),
        };

        let response_format: OpenAIResponseFormat = format.into();
        let schema = response_format.json_schema.unwrap().schema.unwrap();
        assert_eq!(
            schema.get("additionalProperties"),
            Some(&serde_json::json!(false))
        );
    }

    #[test]
    fn test_response_format_preserves_additional_properties() {
        let format = StructuredOutputFormat {
            name: "Test".to_string(),
            description: None,
            schema: Some(serde_json::json!({
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "foo": { "type": "string" }
                }
            })),
            strict: None,
        };

        let response_format: OpenAIResponseFormat = format.into();
        let schema = response_format.json_schema.unwrap().schema.unwrap();
        assert_eq!(
            schema.get("additionalProperties"),
            Some(&serde_json::json!(true))
        );
    }

    #[test]
    fn test_prepare_messages_expands_tool_results() {
        let tool_calls = vec![ToolCall {
            id: "call_1".to_string(),
            call_type: "function".to_string(),
            function: FunctionCall {
                name: "lookup".to_string(),
                arguments: "{\"q\":\"value\"}".to_string(),
            },
        }];
        let messages = vec![ChatMessage {
            role: ChatRole::Assistant,
            message_type: MessageType::ToolResult(tool_calls.clone()),
            content: "tool result".to_string(),
        }];

        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "key", None, None, None, None, None, None, None, None, None, None, None, None, None,
            None, None,
        );
        let prepared = provider
            .prepare_messages(&messages)
            .expect("tool result messages should prepare");
        assert_eq!(prepared.len(), 1);
        assert_eq!(prepared[0].role, "tool");
        assert_eq!(prepared[0].tool_call_id.as_deref(), Some("call_1"));
        match &prepared[0].content {
            Some(Right(text)) => assert_eq!(text, "{\"q\":\"value\"}"),
            other => panic!("Unexpected content: {other:?}"),
        }
        assert!(prepared[0].tool_calls.is_none());
    }

    #[test]
    fn test_chat_message_to_openai_message_image_url() {
        let msg = ChatMessage {
            role: ChatRole::User,
            message_type: MessageType::ImageURL("https://example.com/image.png".to_string()),
            content: "describe".to_string(),
        };
        let openai_msg = chat_message_to_openai_message(msg).expect("image URL should convert");
        assert_eq!(openai_msg.role, "user");
        match openai_msg.content.unwrap() {
            Left(parts) => {
                assert_eq!(parts.len(), 1);
                assert_eq!(parts[0].message_type, Some("image_url"));
                assert!(parts[0].text.is_none());
                assert_eq!(
                    parts[0].image_url.as_ref().unwrap().url,
                    "https://example.com/image.png"
                );
            }
            Right(_) => panic!("Expected multipart content"),
        }
    }

    #[test]
    fn test_chat_message_to_openai_message_tool_use() {
        let msg = ChatMessage {
            role: ChatRole::Assistant,
            message_type: MessageType::ToolUse(vec![ToolCall {
                id: "call_1".to_string(),
                call_type: "function".to_string(),
                function: FunctionCall {
                    name: "lookup".to_string(),
                    arguments: "{\"q\":\"value\"}".to_string(),
                },
            }]),
            content: "call tool".to_string(),
        };
        let openai_msg = chat_message_to_openai_message(msg).expect("tool use should convert");
        assert!(openai_msg.content.is_none());
        assert!(openai_msg.tool_calls.is_some());
        let calls = openai_msg.tool_calls.unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].function.name, "lookup");
    }

    #[test]
    fn test_chat_message_to_openai_message_image_base64() {
        use crate::chat::ImageMime;

        let msg = ChatMessage {
            role: ChatRole::User,
            message_type: MessageType::Image((ImageMime::PNG, vec![1, 2, 3, 4])),
            content: "caption".to_string(),
        };
        let openai_msg = chat_message_to_openai_message(msg).expect("image should convert");
        let content = openai_msg.content.unwrap();
        match content {
            Left(parts) => {
                assert_eq!(parts.len(), 1);
                let url = parts[0].image_url.as_ref().unwrap().url.clone();
                assert!(url.starts_with("data:image/png;base64,"));
            }
            Right(_) => panic!("Expected multipart content"),
        }
    }

    #[test]
    fn test_chat_message_to_openai_message_tool_use_and_result() {
        let tool_call = ToolCall {
            id: "call_1".to_string(),
            call_type: "function".to_string(),
            function: FunctionCall {
                name: "lookup".to_string(),
                arguments: "{\"q\":\"value\"}".to_string(),
            },
        };

        let tool_use_msg = ChatMessage {
            role: ChatRole::Assistant,
            message_type: MessageType::ToolUse(vec![tool_call.clone()]),
            content: "call".to_string(),
        };
        let openai_msg =
            chat_message_to_openai_message(tool_use_msg).expect("tool use should convert");
        assert!(openai_msg.content.is_none());
        let calls = openai_msg.tool_calls.unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].function.name, "lookup");

        let tool_result_msg = ChatMessage {
            role: ChatRole::Tool,
            message_type: MessageType::ToolResult(vec![tool_call]),
            content: "result".to_string(),
        };
        let openai_msg =
            chat_message_to_openai_message(tool_result_msg).expect("tool result should convert");
        assert!(openai_msg.content.is_none());
        assert!(openai_msg.tool_calls.is_none());
    }

    #[test]
    fn test_prepare_messages_rejects_pdf() {
        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "key", None, None, None, None, None, None, None, None, None, None, None, None, None,
            None, None,
        );
        let messages = vec![ChatMessage {
            role: ChatRole::User,
            message_type: MessageType::Pdf(vec![1, 2, 3]),
            content: "doc".to_string(),
        }];

        let err = provider
            .prepare_messages(&messages)
            .expect_err("PDF input should be rejected");

        assert!(matches!(
            err,
            LLMError::InvalidRequest { message, .. }
                if message == "PDF input is not supported by OpenAI-compatible chat completions backends"
        ));
    }

    #[test]
    fn test_openai_response_format_inserts_additional_properties() {
        let structured = StructuredOutputFormat {
            name: "TestSchema".to_string(),
            description: Some("desc".to_string()),
            schema: Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "name": { "type": "string" }
                }
            })),
            strict: Some(true),
        };

        let response_format: OpenAIResponseFormat = structured.into();
        assert!(matches!(
            response_format.response_type,
            OpenAIResponseType::JsonSchema
        ));
        let schema = response_format.json_schema.unwrap().schema.unwrap();
        assert_eq!(
            schema.get("additionalProperties"),
            Some(&serde_json::json!(false))
        );
    }

    #[test]
    fn test_provider_new_base_url_and_extra_body() {
        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "key",
            Some("https://example.com/api".to_string()),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            Some(serde_json::json!("not-an-object")),
            None,
            None,
            None,
            None,
        );

        assert_eq!(provider.base_url.as_str(), "https://example.com/api/");
        assert!(provider.extra_body.is_empty());
    }

    #[tokio::test]
    async fn test_missing_api_key_returns_error() {
        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "", None, None, None, None, None, None, None, None, None, None, None, None, None, None,
            None,
        );
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider.chat(&messages, None).await.unwrap_err();
        assert!(matches!(err, LLMError::AuthError { .. }));
    }

    #[tokio::test]
    async fn test_missing_api_key_stream_returns_error() {
        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "", None, None, None, None, None, None, None, None, None, None, None, None, None, None,
            None,
        );
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider
            .chat_stream(&messages, None)
            .await
            .err()
            .expect("expected auth error");
        assert!(matches!(err, LLMError::AuthError { .. }));
    }

    #[tokio::test]
    async fn test_missing_api_key_stream_with_tools_returns_error() {
        let provider = OpenAICompatibleProvider::<TestConfig>::new(
            "", None, None, None, None, None, None, None, None, None, None, None, None, None, None,
            None,
        );
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider
            .chat_stream_with_tools(&messages, None, None)
            .await
            .err()
            .expect("expected auth error");
        assert!(matches!(err, LLMError::AuthError { .. }));
    }

    #[test]
    fn test_openai_chat_response_helpers() {
        let response = OpenAIChatResponse {
            choices: vec![OpenAIChatChoice {
                message: OpenAIChatMsg {
                    role: "assistant".to_string(),
                    content: Some("hi".to_string()),
                    reasoning_content: Some("plan".to_string()),
                    tool_calls: Some(vec![ToolCall {
                        id: "call_1".to_string(),
                        call_type: "function".to_string(),
                        function: FunctionCall {
                            name: "lookup".to_string(),
                            arguments: "{\"q\":\"value\"}".to_string(),
                        },
                    }]),
                },
            }],
            usage: Some(Usage {
                prompt_tokens: 1,
                completion_tokens: 2,
                total_tokens: 3,
                prompt_tokens_details: None,
                completion_tokens_details: None,
            }),
        };

        assert_eq!(response.text(), Some("hi".to_string()));
        assert_eq!(response.thinking(), Some("plan".to_string()));
        assert_eq!(response.tool_calls().unwrap().len(), 1);
        assert_eq!(response.usage().unwrap().total_tokens, 3);
        let display = format!("{response}");
        assert!(display.contains("lookup"));
        assert!(display.contains("hi"));
    }

    #[test]
    fn test_sse_stream_parser_emits_content() {
        let mut parser = SSEStreamParser::new(false);
        let results =
            parser.consume_text("data: {\"choices\":[{\"delta\":{\"content\":\"Hello\"}}]}\n\n");
        assert_eq!(results.len(), 1);
        let response = results[0].as_ref().unwrap();
        assert_eq!(response.choices.len(), 1);
        assert_eq!(response.choices[0].delta.content.as_deref(), Some("Hello"));
    }

    #[test]
    fn test_sse_stream_parser_emits_reasoning_content() {
        let mut parser = SSEStreamParser::new(false);
        let results = parser.consume_text(
            "data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"think\"}}]}\n\n",
        );
        assert_eq!(results.len(), 1);
        let response = results[0].as_ref().unwrap();
        assert_eq!(response.choices.len(), 1);
        assert_eq!(
            response.choices[0].delta.reasoning_content.as_deref(),
            Some("think")
        );
    }

    #[test]
    fn test_sse_stream_parser_emits_usage_on_done() {
        let mut parser = SSEStreamParser::new(false);
        let usage_event = "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":2,\"total_tokens\":3}}\n\n";
        let results = parser.consume_text(usage_event);
        assert!(results.is_empty());

        let done_results = parser.consume_text("data: [DONE]\n\n");
        assert_eq!(done_results.len(), 1);
        let response = done_results[0].as_ref().unwrap();
        assert_eq!(response.usage.as_ref().unwrap().total_tokens, 3);
    }

    #[test]
    fn test_sse_stream_parser_normalizes_tool_calls() {
        let mut parser = SSEStreamParser::new(true);
        let tool_event = "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"lookup\",\"arguments\":\"{\\\"q\\\":1}\"}}]}}]}\n\n";
        let results = parser.consume_text(tool_event);
        assert!(results.is_empty());

        let done_results = parser.consume_text("data: [DONE]\n\n");
        assert_eq!(done_results.len(), 1);
        let response = done_results[0].as_ref().unwrap();
        let calls = response.choices[0].delta.tool_calls.as_ref().unwrap();
        assert_eq!(calls[0].function.name, "lookup");
        assert_eq!(calls[0].function.arguments, "{\"q\":1}");
        assert_eq!(calls[0].id, "call_1");
    }

    #[tokio::test]
    async fn test_chat_with_tools_sends_supported_fields_and_decodes_response() {
        let server = MockServer::start();
        let response_mock = server.mock(|when, then| {
            when.method(POST)
                .path("/v1/chat/completions")
                .header("authorization", "Bearer key")
                .header("x-provider", "enabled")
                .body_includes("\"reasoning_effort\":\"high\"")
                .body_includes("\"parallel_tool_calls\":true")
                .body_includes("\"tool_choice\":\"auto\"")
                .body_includes("\"response_format\"")
                .body_includes("\"seed\":7");
            then.status(200).json_body(json!({
                "choices": [{
                    "message": {
                        "role": "assistant",
                        "content": "hello from mock"
                    }
                }],
                "usage": {
                    "prompt_tokens": 2,
                    "completion_tokens": 3,
                    "total_tokens": 5
                }
            }));
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let messages = vec![ChatMessage::user().content("hello").build()];
        let tools = vec![sample_function_tool()];
        let response = provider
            .chat_with_tools(&messages, Some(&tools), Some(sample_schema()))
            .await
            .expect("chat_with_tools should succeed");

        assert_eq!(response.text().as_deref(), Some("hello from mock"));
        assert_eq!(response.usage().map(|usage| usage.total_tokens), Some(5));
        response_mock.assert();
    }

    #[tokio::test]
    async fn test_chat_with_tools_returns_error_for_status_and_invalid_json() {
        let server = MockServer::start();
        let status_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(500).body("provider exploded");
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider
            .chat_with_tools(&messages, None, None)
            .await
            .expect_err("non-success status should fail");

        match err {
            LLMError::HttpStatusError {
                status_code,
                response_body,
                ..
            } => {
                assert_eq!(status_code, 500);
                assert_eq!(response_body.as_ref(), "provider exploded");
            }
            other => panic!("unexpected error: {other:?}"),
        }
        status_mock.assert();

        let server = MockServer::start();
        let invalid_json_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(200).body("not-json");
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let err = provider
            .chat_with_tools(&messages, None, None)
            .await
            .expect_err("invalid json should fail");
        match err {
            LLMError::ResponseFormatError {
                message,
                raw_response,
            } => {
                assert!(message.contains("Failed to decode"));
                assert_eq!(raw_response, "not-json");
            }
            other => panic!("unexpected error: {other:?}"),
        }
        invalid_json_mock.assert();
    }

    #[tokio::test]
    async fn test_chat_stream_struct_and_chat_stream_with_tools_parse_mocked_sse() {
        let server = MockServer::start();
        let struct_mock = server.mock(|when, then| {
            when.method(POST)
                .path("/v1/chat/completions")
                .body_includes("\"stream\":true")
                .body_includes("\"include_usage\":true");
            then.status(200)
                .header("content-type", "text/event-stream")
                .body(
                    "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\n\n\
                     data: {\"choices\":[],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":2,\"total_tokens\":3}}\n\n\
                     data: [DONE]\n\n",
                );
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let messages = vec![ChatMessage::user().content("stream").build()];
        let mut stream = provider
            .chat_stream_struct(&messages, None, None)
            .await
            .expect("structured stream should build");

        let first = stream
            .next()
            .await
            .expect("first item should exist")
            .expect("first item should be ok");
        assert_eq!(first.choices[0].delta.content.as_deref(), Some("hello"));
        let rest: Vec<_> = stream.collect().await;
        assert!(rest.into_iter().all(|item| item.is_ok()));
        struct_mock.assert();

        let server = MockServer::start();
        let tool_mock = server.mock(|when, then| {
            when.method(POST)
                .path("/v1/chat/completions")
                .body_includes("\"stream\":true")
                .body_includes("\"tools\":[");
            then.status(200)
                .header("content-type", "text/event-stream")
                .body(
                    "data: {\"id\":\"chatcmpl-1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"lookup\",\"arguments\":\"\"}}]},\"finish_reason\":null}]}\n\n\
                     data: {\"id\":\"chatcmpl-1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"q\\\":\\\"value\\\"}\"}}]},\"finish_reason\":null}]}\n\n\
                     data: {\"id\":\"chatcmpl-1\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
                );
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let tools = vec![sample_function_tool()];
        let mut stream = provider
            .chat_stream_with_tools(&messages, Some(&tools), None)
            .await
            .expect("tool stream should build");
        let items = [
            stream.next().await.expect("tool start"),
            stream.next().await.expect("tool delta"),
            stream.next().await.expect("tool complete"),
            stream.next().await.expect("done"),
        ];

        assert!(matches!(
            &items[0],
            Ok(ChatStreamChunk::ToolUseStart { id, name, .. }) if id == "call_1" && name == "lookup"
        ));
        assert!(matches!(
            &items[1],
            Ok(ChatStreamChunk::ToolUseInputDelta { partial_json, .. }) if partial_json == "{\"q\":\"value\"}"
        ));
        assert!(matches!(
            &items[2],
            Ok(ChatStreamChunk::ToolUseComplete { tool_call, .. })
                if tool_call.function.arguments == "{\"q\":\"value\"}"
        ));
        assert!(matches!(
            &items[3],
            Ok(ChatStreamChunk::Done { stop_reason }) if stop_reason == "tool_use"
        ));
        tool_mock.assert();
    }

    #[tokio::test]
    async fn test_429_maps_to_rate_limit_error() {
        let server = MockServer::start();
        let rate_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(429)
                .header("Retry-After", "15")
                .body(r#"{"error":{"message":"rate limited","code":"rate_limit_exceeded"}}"#);
        });

        let provider = full_support_provider(format!("{}/v1", server.base_url()));
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider
            .chat_with_tools(&messages, None, None)
            .await
            .expect_err("429 should fail");

        match err {
            LLMError::RateLimitError {
                status_code,
                message,
                retry_after,
                ..
            } => {
                assert_eq!(status_code, 429);
                assert_eq!(message, "rate limited");
                assert_eq!(retry_after, Some(std::time::Duration::from_secs(15)));
            }
            other => panic!("unexpected error: {other:?}"),
        }
        rate_mock.assert();
    }

    /// Reqwest client timeout bounds the full request lifecycle, including reading
    /// a streaming response body. A slow-to-start SSE response should fail once
    /// the configured timeout elapses.
    #[tokio::test]
    async fn test_streaming_request_times_out_when_body_is_slow() {
        let server = MockServer::start();
        let slow_stream_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(200)
                .header("content-type", "text/event-stream")
                .delay(std::time::Duration::from_secs(3))
                .body("data: {\"choices\":[{\"delta\":{\"content\":\"late\"}}]}\n\n");
        });

        let provider = OpenAICompatibleProvider::<FullSupportConfig>::new(
            "key",
            Some(format!("{}/v1", server.base_url())),
            Some("full-model".to_string()),
            Some(128),
            Some(0.2),
            Some(1),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        );
        let messages = vec![ChatMessage::user().content("stream").build()];
        let err = match provider.chat_stream_struct(&messages, None, None).await {
            Err(err) => err,
            Ok(_) => panic!("slow stream should time out during setup/read"),
        };

        assert!(matches!(err, LLMError::HttpError(_)));
        assert!(err.is_transport_retryable());
        slow_stream_mock.assert();
    }

    #[tokio::test]
    async fn test_chat_times_out_when_response_exceeds_limit() {
        let server = MockServer::start();
        let slow_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(200).delay(std::time::Duration::from_secs(3)).body(
                r#"{"choices":[{"message":{"role":"assistant","content":"late"}}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}"#,
            );
        });

        let provider = OpenAICompatibleProvider::<FullSupportConfig>::new(
            "key",
            Some(format!("{}/v1", server.base_url())),
            Some("full-model".to_string()),
            Some(128),
            Some(0.2),
            Some(1),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        );
        let messages = vec![ChatMessage::user().content("hello").build()];
        let err = provider
            .chat_with_tools(&messages, None, None)
            .await
            .expect_err("slow response should time out");

        assert!(matches!(err, LLMError::HttpError(_)));
        assert!(err.is_transport_retryable());
        slow_mock.assert();
    }
}