goose-provider-types 0.1.0-alpha.13

An AI agent
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
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
use crate::base::ThinkingPreservationFormat;
use crate::conversation::message::{Message, MessageContentBlock, ProviderMetadata};
use crate::conversation::token_usage::{CostSource, ProviderUsage, Usage};
use crate::documents::{
    convert_document, document_media_type_is_supported, unsupported_document_text, DocumentFormat,
    ASSISTANT_ROLE_REASON, UNSUPPORTED_MEDIA_TYPE_REASON,
};
use crate::errors::ProviderError;
use crate::images::{convert_image, detect_image_path, load_image_file, ImageFormat};
use crate::json::{parse_tool_arguments, truncation_error_message};
use crate::maybe_send::MaybeSend;
use crate::mcp_utils::extract_text_from_resource;
use crate::model::{is_goose_internal_request_param, ModelConfig};
use crate::thinking::{
    split_think_blocks, ThinkFilter, ThinkingEffort, GEMINI_THOUGHT_SIGNATURE_KEY,
};
use anyhow::{anyhow, Error};
use async_stream::try_stream;
use chrono;
use futures::Stream;
use regex::Regex;
use rmcp::model::{object, CallToolRequestParams, ContentBlock, ErrorCode, ErrorData, Role, Tool};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::OnceLock;

type ToolCallData = HashMap<
    i32,
    (
        String,
        String,
        String,
        Option<serde_json::Map<String, Value>>,
    ),
>;

fn deserialize_null_default_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: serde::Deserializer<'de>,
{
    Ok(Option::<String>::deserialize(deserializer)?.unwrap_or_default())
}

fn describe_json_value(value: &Value) -> &'static str {
    match value {
        Value::Array(_) => "an array",
        Value::String(_) => "a string",
        Value::Number(_) => "a number",
        Value::Bool(_) => "a boolean",
        Value::Null => "null",
        Value::Object(_) => "an object",
    }
}

fn output_token_limit_tool_error(function_name: &str, id: &str) -> ErrorData {
    ErrorData {
        code: ErrorCode::INVALID_PARAMS,
        message: Cow::from(format!(
            "Tool arguments for {function_name} (id {id}) were truncated because the model reached its output token limit"
        )),
        data: None,
    }
}

pub fn is_reserved_request_param_key(key: &str) -> bool {
    matches!(key, "messages" | "model" | "stream" | "stream_options")
}

#[derive(Debug, Clone, Copy, Default)]
pub struct OpenAiFormatOptions {
    pub preserve_thinking_context: bool,
    pub supports_vision: bool,
    pub thinking_preservation_format: Option<ThinkingPreservationFormat>,
}

fn merge_reasoning_text(prefix: &str, suffix: &str) -> String {
    if prefix.is_empty() {
        return suffix.to_string();
    }
    if suffix.is_empty() {
        return prefix.to_string();
    }
    if suffix.starts_with(prefix) {
        return suffix.to_string();
    }
    if prefix.ends_with(suffix) {
        return prefix.to_string();
    }

    format!("{prefix}{suffix}")
}

#[derive(Serialize, Deserialize, Debug, Default)]
struct DeltaToolCallFunction {
    name: Option<String>,
    #[serde(default, deserialize_with = "deserialize_null_default_string")]
    arguments: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct DeltaToolCall {
    id: Option<String>,
    function: DeltaToolCallFunction,
    index: Option<i32>,
    r#type: Option<String>,
    #[serde(flatten)]
    extra: Option<serde_json::Map<String, Value>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
enum DeltaContentBlock {
    String(String),
    Array(Vec<ContentBlockPart>),
}

#[derive(Serialize, Deserialize, Debug)]
struct ContentBlockPart {
    r#type: String,
    #[serde(default)]
    text: Option<String>,
    #[serde(rename = "thoughtSignature")]
    thought_signature: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
struct Delta {
    #[serde(default)]
    content: Option<DeltaContentBlock>,
    role: Option<String>,
    tool_calls: Option<Vec<DeltaToolCall>>,
    reasoning_details: Option<Vec<Value>>,
    reasoning: Option<String>,
    reasoning_content: Option<String>,
}

impl Delta {
    /// Prefer `reasoning_content` (DeepSeek/OpenRouter) over `reasoning`
    /// (vLLM); some servers (gpt-oss via vLLM) emit both. Skip empty values.
    fn reasoning_text(&self) -> Option<&str> {
        self.reasoning_content
            .as_deref()
            .filter(|s| !s.is_empty())
            .or_else(|| self.reasoning.as_deref().filter(|s| !s.is_empty()))
    }
}

#[derive(Serialize, Deserialize, Debug)]
struct StreamingChoice {
    #[serde(default)]
    delta: Delta,
    index: Option<i32>,
    #[serde(default, deserialize_with = "empty_finish_reason_as_none")]
    finish_reason: Option<String>,
}

fn empty_finish_reason_as_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = Option::<String>::deserialize(deserializer)?;
    Ok(value.filter(|reason| !reason.is_empty()))
}

#[derive(Serialize, Deserialize, Debug)]
struct StreamingChunk {
    choices: Vec<StreamingChoice>,
    created: Option<i64>,
    id: Option<String>,
    usage: Option<Value>,
    model: Option<String>,
}

fn extract_content_and_signature(
    delta_content: Option<&DeltaContentBlock>,
) -> (Option<String>, Option<String>) {
    match delta_content {
        Some(DeltaContentBlock::String(s)) => (Some(s.clone()), None),
        Some(DeltaContentBlock::Array(parts)) => {
            let text_parts: Vec<_> = parts.iter().filter(|p| p.r#type == "text").collect();

            let text = text_parts
                .iter()
                .filter_map(|p| p.text.as_deref())
                .collect::<String>();

            let signature = text_parts
                .iter()
                .find_map(|p| p.thought_signature.as_ref())
                .cloned();

            let text = if text.is_empty() { None } else { Some(text) };

            (text, signature)
        }
        None => (None, None),
    }
}

#[derive(Default, Serialize)]
struct OpenAiMessage {
    role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_calls: Option<Vec<OpenAiToolCall>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning_content: Option<String>,
}

#[derive(Serialize)]
struct OpenAiToolCall {
    id: String,
    #[serde(rename = "type")]
    kind: &'static str,
    function: OpenAiFunctionCall,
    #[serde(flatten)]
    metadata: serde_json::Map<String, Value>,
}

#[derive(Serialize)]
struct OpenAiFunctionCall {
    name: String,
    arguments: String,
}

pub fn format_messages(messages: &[Message], image_format: &ImageFormat) -> Vec<Value> {
    format_messages_with_options(
        messages,
        image_format,
        OpenAiFormatOptions {
            preserve_thinking_context: true,
            ..Default::default()
        },
    )
}

pub fn format_messages_with_options(
    messages: &[Message],
    image_format: &ImageFormat,
    options: OpenAiFormatOptions,
) -> Vec<Value> {
    let mut messages_spec = Vec::new();
    let mut pending_assistant_reasoning = String::new();
    // Reasoning to propagate across consecutive tool-call messages in the same turn.
    // DeepSeek/Kimi require reasoning_content on every assistant tool-call message.
    let mut tool_call_turn_reasoning = String::new();
    let mut saw_tool_response = false;

    for message in messages {
        if options.preserve_thinking_context && message.role != Role::Assistant {
            pending_assistant_reasoning.clear();
        }

        if options.preserve_thinking_context && message.role == Role::User {
            if message
                .content
                .iter()
                .any(|c| matches!(c, MessageContentBlock::ToolResponse(_)))
            {
                saw_tool_response = true;
            } else {
                tool_call_turn_reasoning.clear();
                saw_tool_response = false;
            }
        }

        // A new assistant message after tool results creates a new turn.
        // Prevents reasoning from the previous turn leaking into the new one.
        if options.preserve_thinking_context && message.role == Role::Assistant && saw_tool_response
        {
            tool_call_turn_reasoning.clear();
            saw_tool_response = false;
        }

        let mut converted = OpenAiMessage {
            role: match message.role {
                Role::User => "user",
                Role::Assistant => "assistant",
            }
            .to_string(),
            ..Default::default()
        };

        let mut output = Vec::new();
        // Deferred to the end of the message so every tool result in a batch stays
        // consecutive; a strict OpenAI-compatible API rejects a request where a
        // synthetic user image message splits one assistant tool_calls batch.
        let mut pending_image_messages = Vec::new();
        let mut content_array = Vec::new();
        let mut has_non_text_content = false;
        let mut reasoning_text = String::new();

        for content in &message.content {
            match content {
                MessageContentBlock::Text(text) => {
                    if !text.text.is_empty() {
                        if message.role == Role::User {
                            if options.supports_vision {
                                if let Some(image_path) = detect_image_path(&text.text) {
                                    if let Ok(image) = load_image_file(image_path.as_ref()) {
                                        has_non_text_content = true;
                                        content_array
                                            .push(json!({"type": "text", "text": text.text}));
                                        content_array.push(convert_image(&image, image_format));
                                    } else {
                                        content_array
                                            .push(json!({"type": "text", "text": text.text}));
                                    }
                                } else {
                                    content_array.push(json!({"type": "text", "text": text.text}));
                                }
                            } else {
                                content_array.push(json!({"type": "text", "text": text.text}));
                            }
                        } else {
                            content_array.push(json!({"type": "text", "text": text.text}));
                        }
                    }
                }
                MessageContentBlock::Thinking(t) => {
                    reasoning_text.push_str(&t.thinking);
                }
                MessageContentBlock::RedactedThinking(_) => {
                    continue;
                }
                MessageContentBlock::SystemNotification(_) | MessageContentBlock::Error(_) => {
                    continue;
                }
                MessageContentBlock::ToolRequest(request) => match &request.tool_call {
                    Ok(tool_call) => {
                        let sanitized_name = sanitize_function_name(&tool_call.name);
                        let arguments_str = match &tool_call.arguments {
                            Some(args) => {
                                serde_json::to_string(args).unwrap_or_else(|_| "{}".to_string())
                            }
                            None => "{}".to_string(),
                        };

                        converted
                            .tool_calls
                            .get_or_insert_default()
                            .push(OpenAiToolCall {
                                id: request.id.clone(),
                                kind: "function",
                                function: OpenAiFunctionCall {
                                    name: sanitized_name,
                                    arguments: arguments_str,
                                },
                                metadata: request.metadata.clone().unwrap_or_default(),
                            });
                    }
                    Err(_e) => {
                        // An unparseable tool call still needs a valid assistant
                        // `tool_calls` entry. Emitting the error as a bare `role:"tool"`
                        // message (the old behavior) leaves the paired tool response —
                        // which carries the parse error — as an orphan `role:"tool"` with
                        // no preceding assistant `tool_calls`, which strict
                        // OpenAI-compatible APIs reject. Emit a placeholder call with the
                        // same id so the history stays well-formed; the error rides on the
                        // following tool response.
                        converted
                            .tool_calls
                            .get_or_insert_default()
                            .push(OpenAiToolCall {
                                id: request.id.clone(),
                                kind: "function",
                                function: OpenAiFunctionCall {
                                    name: "unparseable_tool_call".to_string(),
                                    arguments: "{}".to_string(),
                                },
                                metadata: serde_json::Map::new(),
                            });
                    }
                },
                MessageContentBlock::ToolResponse(response) => {
                    match &response.tool_result {
                        Ok(result) => {
                            // Process all content, replacing images with placeholder text
                            let mut tool_content = Vec::new();

                            for content in result.content.iter() {
                                match content {
                                    ContentBlock::Image(image) => {
                                        if options.supports_vision {
                                            // Add placeholder text in the tool response
                                            tool_content.push(ContentBlock::text("This tool result included an image that is uploaded in the next message."));

                                            // Create a separate image message
                                            pending_image_messages.push(OpenAiMessage {
                                                role: "user".to_string(),
                                                content: Some(json!([convert_image(
                                                    &image.clone(),
                                                    image_format
                                                )])),
                                                ..Default::default()
                                            });
                                        } else {
                                            // Add placeholder text in the tool response
                                            tool_content.push(ContentBlock::text("This tool result included an image that was omitted as the model does not support vision."));
                                        }
                                    }
                                    ContentBlock::Resource(resource) => {
                                        let text = extract_text_from_resource(&resource.resource);
                                        tool_content.push(ContentBlock::text(text));
                                    }
                                    _ => {
                                        tool_content.push(content.clone());
                                    }
                                }
                            }
                            let tool_response_content: Value = json!(tool_content
                                .iter()
                                .map(|content| match content {
                                    ContentBlock::Text(text) => text.text.clone(),
                                    _ => String::new(),
                                })
                                .collect::<Vec<String>>()
                                .join(" "));

                            output.push(OpenAiMessage {
                                role: "tool".to_string(),
                                content: Some(tool_response_content),
                                tool_call_id: Some(response.id.clone()),
                                ..Default::default()
                            });
                        }
                        Err(e) => {
                            // A tool result error is shown as output so the model can interpret the error message
                            output.push(OpenAiMessage {
                                role: "tool".to_string(),
                                content: Some(json!(format!(
                                    "The tool call returned the following error:\n{}",
                                    e
                                ))),
                                tool_call_id: Some(response.id.clone()),
                                ..Default::default()
                            });
                        }
                    }
                }
                MessageContentBlock::ToolConfirmationRequest(_) => {}
                MessageContentBlock::ActionRequired(_) => {}
                MessageContentBlock::Image(image) => {
                    if message.role == Role::User {
                        if options.supports_vision {
                            has_non_text_content = true;
                            content_array.push(convert_image(image, image_format));
                        } else {
                            content_array.push(json!({
                                "type": "text",
                                "text": "[image omitted: model does not support vision]"
                            }));
                        }
                    } else {
                        content_array.push(json!({
                            "type": "text",
                            "text": "[Image content removed - not supported in assistant messages]"
                        }));
                    }
                }
                MessageContentBlock::Document(document) => {
                    if message.role != Role::User {
                        content_array.push(json!({
                            "type": "text",
                            "text": unsupported_document_text(document, ASSISTANT_ROLE_REASON)
                        }));
                    } else if document_media_type_is_supported(&document.mime_type) {
                        has_non_text_content = true;
                        content_array.push(convert_document(document, &DocumentFormat::OpenAi));
                    } else {
                        content_array.push(json!({
                            "type": "text",
                            "text": unsupported_document_text(document, UNSUPPORTED_MEDIA_TYPE_REASON)
                        }));
                    }
                }
            }
        }

        output.append(&mut pending_image_messages);

        if !content_array.is_empty() {
            if has_non_text_content {
                converted.content = Some(json!(content_array));
            } else {
                let texts: Vec<String> = content_array
                    .iter()
                    .filter_map(|v| v["text"].as_str().map(|s| s.to_string()))
                    .collect();
                converted.content = Some(json!(texts.join("\n")));
            }
        }

        // Some strict OpenAI-compatible providers require "content" to be present
        // (even as null) when tool_calls are provided. See #6717.
        if message.role == Role::Assistant
            && converted.tool_calls.is_some()
            && converted.content.is_none()
        {
            converted.content = Some(Value::Null);
        }

        let has_message_payload = converted.content.is_some() || converted.tool_calls.is_some();

        if options.preserve_thinking_context && message.role == Role::Assistant {
            if !has_message_payload && output.is_empty() && !reasoning_text.is_empty() {
                pending_assistant_reasoning.push_str(&reasoning_text);
                continue;
            }

            if !pending_assistant_reasoning.is_empty() {
                reasoning_text =
                    merge_reasoning_text(&pending_assistant_reasoning, &reasoning_text);
                pending_assistant_reasoning.clear();
            }

            let has_tool_calls = converted.tool_calls.as_ref().is_some_and(|a| !a.is_empty());

            if has_tool_calls {
                if reasoning_text.is_empty() {
                    reasoning_text = tool_call_turn_reasoning.clone();
                } else {
                    tool_call_turn_reasoning = reasoning_text.clone();
                }
            } else {
                // Carry reasoning forward even through non-tool assistant messages
                // (e.g., a visible text chunk that's is sent before a tool-call chunk
                // in the same streaming turn). An empty reasoning_text is equivalent
                // to clear.
                tool_call_turn_reasoning = reasoning_text.clone();
            }
        }

        // Include reasoning_content only when non-empty. Kimi rejects empty
        // reasoning_content (""), so we must omit it entirely.
        if options.preserve_thinking_context && !reasoning_text.is_empty() {
            converted.reasoning_content = Some(reasoning_text);
        }

        if has_message_payload {
            output.insert(0, converted);
        }

        messages_spec.extend(output.into_iter().map(|message| {
            serde_json::to_value(message).expect("OpenAI message fields are JSON serializable")
        }));
    }

    merge_split_tool_call_messages(&mut messages_spec);

    if let Some(format) = options.thinking_preservation_format {
        inline_reasoning_content(&mut messages_spec, format);
    }

    messages_spec
}

/// Rewrites `reasoning_content` into the message `content` for models that reject a
/// separate reasoning field on replay.
///
/// Must run after `merge_split_tool_call_messages`, which relies on `reasoning_content`
/// to identify messages split from the same assistant turn.
fn inline_reasoning_content(messages: &mut [Value], format: ThinkingPreservationFormat) {
    let wrap: fn(&str) -> String = match format {
        ThinkingPreservationFormat::ReasoningContent => return,
        ThinkingPreservationFormat::ContentPrepend => |text| format!("{text}\n\n"),
        ThinkingPreservationFormat::ContentXml => |text| format!("<think>\n{text}\n</think>\n\n"),
    };

    for message in messages {
        let Some(object) = message.as_object_mut() else {
            continue;
        };
        let Some(Value::String(reasoning)) = object.remove("reasoning_content") else {
            continue;
        };
        let prefix = wrap(&reasoning);

        match object.entry("content").or_insert(Value::Null) {
            Value::String(content) => content.insert_str(0, &prefix),
            Value::Array(blocks) => blocks.insert(0, json!({"type": "text", "text": prefix})),
            content => *content = json!(prefix.trim_end()),
        }
    }
}

/// The agent splits a single assistant response with N tool_calls into N
/// interleaved `asst(TC)/tool` pairs, cloning `reasoning_content` onto each.
/// This function merges them back into one assistant message with all tool_calls,
/// followed by the tool results — the standard OpenAI format.
///
/// Only merges when `reasoning_content` is present and matches, since that is
/// the only signal that messages were split from the same turn.
fn merge_split_tool_call_messages(messages: &mut Vec<Value>) {
    let mut i = 0;
    while i < messages.len() {
        let is_assistant_tool_call = messages[i].get("role") == Some(&json!("assistant"))
            && messages[i]
                .get("tool_calls")
                .and_then(|tc| tc.as_array())
                .is_some_and(|a| !a.is_empty());
        let base_reasoning = messages[i].get("reasoning_content");

        if !is_assistant_tool_call || base_reasoning.is_none() {
            i += 1;
            continue;
        }
        let base_reasoning = base_reasoning.unwrap().clone();

        let mut extra_tool_calls: Vec<Value> = Vec::new();
        let mut collected: Vec<Value> = Vec::new();
        let mut scan = i + 1;

        loop {
            if scan >= messages.len() || messages[scan].get("role") != Some(&json!("tool")) {
                break;
            }

            // Skip past tool result and any image-only user messages that
            // format_messages inserts after tool results containing images.
            let mut peek = scan + 1;
            while peek < messages.len() && is_image_only_user_message(&messages[peek]) {
                peek += 1;
            }

            if peek >= messages.len() {
                break;
            }
            let next = &messages[peek];
            let has_no_content = next.get("content").is_none_or(|c| {
                c.is_null()
                    || c.as_str().is_some_and(|s| s.is_empty())
                    || c.as_array().is_some_and(|a| a.is_empty())
            });
            let is_split = next.get("role") == Some(&json!("assistant"))
                && next
                    .get("tool_calls")
                    .and_then(|tc| tc.as_array())
                    .is_some_and(|a| !a.is_empty())
                && has_no_content
                && next.get("reasoning_content") == Some(&base_reasoning);

            if !is_split {
                break;
            }

            collected.extend(messages[scan..peek].iter().cloned());
            if let Some(tc) = messages[peek]
                .get("tool_calls")
                .and_then(|tc| tc.as_array())
            {
                extra_tool_calls.extend(tc.iter().cloned());
            }
            scan = peek + 1;
        }

        if extra_tool_calls.is_empty() {
            i += 1;
            continue;
        }

        if let Some(base_tc) = messages[i]
            .get_mut("tool_calls")
            .and_then(|tc| tc.as_array_mut())
        {
            base_tc.extend(extra_tool_calls);
        }

        let insert_at = i + 1;
        messages.drain(insert_at..scan);
        let num_collected = collected.len();
        for (j, msg) in collected.into_iter().enumerate() {
            messages.insert(insert_at + j, msg);
        }

        i = insert_at + num_collected;
    }
}

/// True if `msg` is a synthetic image-only user message (content is exclusively image_url items).
fn is_image_only_user_message(msg: &Value) -> bool {
    msg.get("role") == Some(&json!("user"))
        && msg
            .get("content")
            .and_then(|c| c.as_array())
            .is_some_and(|arr| {
                !arr.is_empty()
                    && arr
                        .iter()
                        .all(|item| item.get("type") == Some(&json!("image_url")))
            })
}

pub fn format_tools(tools: &[Tool]) -> anyhow::Result<Vec<Value>> {
    let mut tool_names = std::collections::HashSet::new();
    let mut result = Vec::new();

    for tool in tools {
        if !tool_names.insert(&tool.name) {
            return Err(anyhow!("Duplicate tool name: {}", tool.name));
        }

        result.push(json!({
            "type": "function",
            "function": {
                "name": tool.name,
                "description": tool.description,
                "parameters": tool.input_schema,
                "strict": false,
            }
        }));
    }

    Ok(result)
}

pub fn record_response_metadata(usage: &mut ProviderUsage, response: &Value) {
    usage.response_id = response
        .get("id")
        .and_then(Value::as_str)
        .map(str::to_string);

    let finish_reasons = response
        .get("choices")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|choice| choice.get("finish_reason").and_then(Value::as_str))
        .map(str::to_string)
        .collect::<Vec<_>>();
    if !finish_reasons.is_empty() {
        usage.finish_reasons = Some(finish_reasons);
    }
}

/// Convert OpenAI's API response to internal Message format
pub fn response_to_message(response: &Value) -> anyhow::Result<Message> {
    let output_token_limit_reached = response
        .pointer("/choices/0/finish_reason")
        .and_then(Value::as_str)
        == Some("length");

    let Some(original) = response
        .get("choices")
        .and_then(|c| c.get(0))
        .and_then(|m| m.get("message"))
    else {
        if let Some(error) = response.get("error") {
            let error_message = error
                .get("message")
                .and_then(|m| m.as_str())
                .unwrap_or("Unknown error");
            return Err(anyhow::anyhow!("API error: {}", error_message));
        }
        return Err(anyhow::anyhow!(
            "No message in API response. This may indicate a quota limit or other restriction."
        ));
    };

    let mut content = Vec::new();

    // Capture reasoning content if present (DeepSeek uses "reasoning_content", vLLM uses "reasoning")
    let reasoning_value = original
        .get("reasoning_content")
        .or_else(|| original.get("reasoning"));
    let mut has_structured_thinking = false;
    if let Some(reasoning_content) = reasoning_value {
        if let Some(reasoning_str) = reasoning_content.as_str() {
            if !reasoning_str.is_empty() {
                has_structured_thinking = true;
                content.push(MessageContentBlock::thinking(reasoning_str, ""));
            }
        }
    }

    if let Some(text) = original.get("content") {
        if let Some(text_str) = text.as_str() {
            let (cleaned, inline_thinking) = split_think_blocks(text_str);

            if !has_structured_thinking && !inline_thinking.is_empty() {
                content.push(MessageContentBlock::thinking(inline_thinking, ""));
            }

            if !cleaned.is_empty() {
                content.push(MessageContentBlock::text(cleaned));
            }
        }
    }

    if let Some(tool_calls) = original.get("tool_calls") {
        if let Some(tool_calls_array) = tool_calls.as_array() {
            for tool_call in tool_calls_array {
                let id = tool_call["id"].as_str().unwrap_or_default().to_string();
                let function_name = tool_call["function"]["name"]
                    .as_str()
                    .unwrap_or_default()
                    .to_string();

                // Get the raw arguments string from the LLM.
                let arguments_str = tool_call["function"]["arguments"]
                    .as_str()
                    .unwrap_or_default()
                    .to_string();

                // If arguments_str is empty, default to an empty JSON object string.
                let arguments_str = if arguments_str.is_empty() {
                    "{}".to_string()
                } else {
                    arguments_str
                };

                let standard_fields = ["id", "function", "type", "index"];
                let metadata: Option<serde_json::Map<String, Value>> = tool_call
                    .as_object()
                    .map(|obj| {
                        obj.iter()
                            .filter(|(k, _)| !standard_fields.contains(&k.as_str()))
                            .map(|(k, v)| (k.clone(), v.clone()))
                            .collect()
                    })
                    .filter(|m: &serde_json::Map<String, Value>| !m.is_empty());

                if output_token_limit_reached {
                    let error = output_token_limit_tool_error(&function_name, &id);
                    content.push(MessageContentBlock::tool_request_with_metadata(
                        id,
                        Err(error),
                        metadata.as_ref(),
                    ));
                    continue;
                }

                if function_name.is_empty() {
                    let error = ErrorData {
                        code: ErrorCode::INVALID_REQUEST,
                        message: Cow::from(
                            "The provided function name was empty; a tool call must name a tool"
                                .to_string(),
                        ),
                        data: None,
                    };
                    content.push(MessageContentBlock::tool_request_with_metadata(
                        id,
                        Err(error),
                        metadata.as_ref(),
                    ));
                    continue;
                }
                match parse_tool_arguments(&arguments_str) {
                    Some(params) if params.is_object() => {
                        content.push(MessageContentBlock::tool_request_with_metadata(
                            id,
                            Ok(CallToolRequestParams::new(function_name)
                                .with_arguments(object(params))),
                            metadata.as_ref(),
                        ));
                    }
                    Some(other) => {
                        let error = ErrorData {
                            code: ErrorCode::INVALID_PARAMS,
                            message: Cow::from(format!(
                                "Tool arguments for {} (id {}) must be a JSON object, got {}. Raw arguments: '{}'",
                                function_name,
                                id,
                                describe_json_value(&other),
                                arguments_str
                            )),
                            data: None,
                        };
                        content.push(MessageContentBlock::tool_request_with_metadata(
                            id,
                            Err(error),
                            metadata.as_ref(),
                        ));
                    }
                    None => {
                        let message_text =
                            truncation_error_message(&arguments_str).unwrap_or_else(|| {
                                format!("Could not interpret tool use parameters for id {id}")
                            });
                        let error = ErrorData {
                            code: ErrorCode::INVALID_PARAMS,
                            message: Cow::from(message_text),
                            data: None,
                        };
                        content.push(MessageContentBlock::tool_request_with_metadata(
                            id,
                            Err(error),
                            metadata.as_ref(),
                        ));
                    }
                }
            }
        }
    }

    let mut message = Message::new(Role::Assistant, chrono::Utc::now().timestamp(), content);
    message.metadata.output_token_limit_reached = output_token_limit_reached;
    Ok(message)
}

pub fn get_usage(usage: &Value) -> Usage {
    let usage = usage
        .get("usage")
        .filter(|nested| nested.is_object())
        .unwrap_or(usage);

    // Try standard OpenAI fields first, then fall back to Ollama-native fields
    // (prompt_eval_count / eval_count) for compatibility with older Ollama builds
    // that don't translate to OpenAI field names.
    // Parse the value before falling back so that present-but-null keys
    // (e.g. "completion_tokens": null) don't block the fallback.
    let input_tokens = usage
        .get("prompt_tokens")
        .and_then(|v| v.as_i64())
        .or_else(|| usage.get("prompt_eval_count").and_then(|v| v.as_i64()))
        .map(|v| v as i32);

    let output_tokens = usage
        .get("completion_tokens")
        .and_then(|v| v.as_i64())
        .or_else(|| usage.get("eval_count").and_then(|v| v.as_i64()))
        .map(|v| v as i32);

    let cache_read_input_tokens = usage
        .get("cache_read_input_tokens")
        .and_then(|v| v.as_i64())
        .or_else(|| {
            usage
                .get("prompt_tokens_details")
                .and_then(|d| d.get("cached_tokens"))
                .and_then(|v| v.as_i64())
        })
        .map(|v| v as i32);

    let cache_write_input_tokens = usage
        .get("cache_creation_input_tokens")
        .and_then(|v| v.as_i64())
        .or_else(|| {
            usage
                .get("prompt_tokens_details")
                .and_then(|d| d.get("cache_write_tokens"))
                .and_then(|v| v.as_i64())
        })
        .map(|v| v as i32);

    let total_tokens = usage
        .get("total_tokens")
        .and_then(|v| v.as_i64())
        .map(|v| v as i32)
        .or_else(|| match (input_tokens, output_tokens) {
            (Some(input), Some(output)) => Some(input.saturating_add(output)),
            _ => None,
        });

    Usage::new(input_tokens, output_tokens, total_tokens)
        .with_cache_tokens(cache_read_input_tokens, cache_write_input_tokens)
}

pub fn get_cost(usage: &Value) -> Option<f64> {
    usage
        .get("cost")
        .and_then(|v| v.as_f64())
        .filter(|c| c.is_finite() && *c >= 0.0)
}

fn extract_usage_with_output_tokens(
    chunk: &StreamingChunk,
    fallback_model: Option<&str>,
) -> Option<ProviderUsage> {
    chunk
        .usage
        .as_ref()
        .and_then(|u| {
            chunk.model.as_deref().or(fallback_model).map(|model| {
                let usage = ProviderUsage::new(model.to_string(), get_usage(u));
                match get_cost(u) {
                    Some(cost) => usage.with_cost(cost, CostSource::ProviderReported),
                    None => usage,
                }
            })
        })
        .filter(|u| u.usage.output_tokens.is_some())
}

/// Validates and fixes tool schemas to ensure they have proper parameter structure.
/// If parameters exist, ensures they have properties and required fields, or removes parameters entirely.
pub fn validate_tool_schemas(tools: &mut [Value]) {
    for tool in tools.iter_mut() {
        if let Some(function) = tool.get_mut("function") {
            if let Some(parameters) = function.get_mut("parameters") {
                if parameters.is_object() {
                    ensure_valid_json_schema(parameters);
                }
            }
        }
    }
}

/// Ensures that the given JSON value follows the expected JSON Schema structure.
fn ensure_valid_json_schema(schema: &mut Value) {
    if let Some(params_obj) = schema.as_object_mut() {
        if !params_obj.contains_key("type") {
            params_obj.insert("type".to_string(), json!("object"));
        }
    }
    sanitize_schema_node(schema);
}

fn sanitize_schema_node(node: &mut Value) {
    if let Some(obj) = node.as_object_mut() {
        // Moonshot's walle validator rejects `oneOf` behind a `$ref` as
        // "infinite recursion" because its termination check only traverses
        // `anyOf`. The two are interchangeable for tool-argument schemas, so
        // emit the more widely supported form.
        if !obj.contains_key("anyOf") {
            if let Some(one_of) = obj.remove("oneOf") {
                obj.insert("anyOf".to_string(), one_of);
            }
        }
    }

    normalize_nullable(node);

    let Some(obj) = node.as_object_mut() else {
        return;
    };

    if obj.get("type").and_then(|t| t.as_str()) == Some("object") {
        obj.entry("properties").or_insert_with(|| json!({}));
        obj.entry("required").or_insert_with(|| json!([]));
    }

    for key in ["properties", "$defs", "definitions"] {
        if let Some(children) = obj.get_mut(key).and_then(Value::as_object_mut) {
            for child in children.values_mut() {
                sanitize_schema_node(child);
            }
        }
    }
    for key in ["anyOf", "allOf", "prefixItems"] {
        if let Some(children) = obj.get_mut(key).and_then(Value::as_array_mut) {
            for child in children.iter_mut() {
                sanitize_schema_node(child);
            }
        }
    }
    for key in ["items", "additionalProperties"] {
        if let Some(child) = obj.get_mut(key) {
            if child.is_object() {
                sanitize_schema_node(child);
            }
        }
    }
}

/// Normalizes nullable type representations that some providers (e.g. Vertex Gemini via Bifrost)
/// don't support:
/// - `"type": ["integer", "null"]` → `"type": "integer"` (drops the null variant)
/// - `"anyOf": [T, {"type": "null"}]` → T (unwraps to the non-null schema)
///
/// Optional-ness is already conveyed by the field being absent from `required`.
fn normalize_nullable(schema: &mut Value) {
    let Some(obj) = schema.as_object_mut() else {
        return;
    };

    // Handle type: ["T", "null"] array form (schemars 1.x style for nullable primitives)
    if let Some(type_val) = obj.get("type").cloned() {
        if let Some(types) = type_val.as_array() {
            let non_null: Vec<&Value> = types
                .iter()
                .filter(|t| t.as_str() != Some("null"))
                .collect();
            if non_null.len() == 1 {
                let scalar = non_null[0].clone();
                obj.insert("type".to_string(), scalar);
                return;
            }
        }
    }

    // Handle anyOf: [T, {type: "null"}] form — merge the non-null variant's fields
    // into the current object (preserving sibling keys like "description" or "default")
    // rather than replacing the whole schema.
    if let Some(any_of) = obj.remove("anyOf") {
        if let Some(variants) = any_of.as_array() {
            if variants.len() == 2 {
                let is_null = |v: &Value| v.get("type").and_then(|t| t.as_str()) == Some("null");
                let non_null = if is_null(&variants[0]) {
                    Some(&variants[1])
                } else if is_null(&variants[1]) {
                    Some(&variants[0])
                } else {
                    None
                };
                if let Some(replacement) = non_null {
                    if let Some(replacement_obj) = replacement.as_object() {
                        for (k, v) in replacement_obj {
                            obj.entry(k.clone()).or_insert(v.clone());
                        }
                        return;
                    }
                }
            }
        }
        // Put it back if we couldn't simplify
        obj.insert("anyOf".to_string(), any_of);
    }
}

fn strip_data_prefix(line: &str) -> Option<&str> {
    // SSE spec allows both "data: value" and "data:value" (space after colon is optional)
    line.strip_prefix("data: ")
        .or_else(|| line.strip_prefix("data:"))
        .map(|s| s.trim())
}

/// Longest error text pulled out of a stream frame, so a pathological payload cannot be
/// pasted wholesale into a user-facing message.
const MAX_STREAM_ERROR_LEN: usize = 500;

/// Best-effort human-readable text for an error payload that may not be a plain string.
///
/// FastAPI reports `HTTPException` as `{"detail": "..."}` but `RequestValidationError` as
/// `{"detail": [{"loc": [...], "msg": "field required", ...}]}`, so a string-only read would
/// drop the commoner validation shape entirely.
fn stream_error_text(value: &Value) -> Option<String> {
    fn one(value: &Value) -> Option<String> {
        match value {
            Value::String(s) => Some(s.clone()),
            Value::Object(_) => value
                .get("msg")
                .or_else(|| value.get("message"))
                .and_then(|m| m.as_str().map(String::from))
                .or_else(|| Some(value.to_string())),
            Value::Null => None,
            other => Some(other.to_string()),
        }
    }

    let text = match value {
        Value::Array(items) => {
            let parts: Vec<String> = items.iter().filter_map(one).collect();
            if parts.is_empty() {
                return None;
            }
            parts.join("; ")
        }
        other => one(other)?,
    };
    if text.is_empty() {
        return None;
    }
    if text.chars().count() > MAX_STREAM_ERROR_LEN {
        let truncated: String = text.chars().take(MAX_STREAM_ERROR_LEN).collect();
        return Some(format!("{truncated}…"));
    }
    Some(text)
}

/// Decide whether a choice-less SSE frame reports an in-stream failure.
///
/// Returns `Some(err)` when it does, `None` when it is gateway metadata that can be skipped.
///
/// Requires an actual error *signal* — a `status`/`statusCode`/`code` of 400 or above, a
/// `type` of `"error"`, or a `detail` field, which has no benign meaning in this position.
/// Mere prose is not enough: gateways also emit informational frames, and treating
/// `{"message": "processing"}` as a failure would kill a healthy stream, which is the very
/// bug this skip exists to avoid. The converse matters just as much — a gateway that
/// rate-limits with a bare `{"statusCode": 429, "message": …}` on an HTTP 200 must not be
/// silently skipped, or a failed turn is reported as an empty successful one.
fn classify_choiceless_frame(value: &Value) -> Option<ProviderError> {
    let status = ["status", "statusCode", "code"].iter().find_map(|key| {
        let raw = value.get(*key)?;
        raw.as_i64()
            .or_else(|| raw.as_str().and_then(|s| s.parse::<i64>().ok()))
    });

    let has_error_signal = status.is_some_and(|s| s >= 400)
        || value.get("type").and_then(|t| t.as_str()) == Some("error")
        || value.get("detail").is_some_and(|d| !d.is_null());
    if !has_error_signal {
        return None;
    }

    let details = value
        .get("message")
        .and_then(stream_error_text)
        .or_else(|| value.get("detail").and_then(stream_error_text))
        .or_else(|| value.get("error").and_then(stream_error_text))
        // A status with no recoverable text must still be loud rather than vanish.
        .unwrap_or_else(|| match status {
            Some(s) => format!("Gateway returned status {s} mid-stream"),
            None => "Unknown server error".to_string(),
        });
    Some(ProviderError::ServerError(details))
}

/// Parse one SSE `data:` payload.
///
/// Returns `Ok(None)` for a metadata-only frame — a JSON object with no `choices` key at
/// all. Gateways interleave these with the real chunks: Portkey/Azure APIM and friends
/// emit trace/guardrail objects such as `{"hook_results": {...}}` before the first token.
/// They carry nothing this parser consumes, so they are skipped rather than failed on;
/// treating them as decode errors kills the whole turn on an otherwise healthy stream.
///
/// A frame with `"choices": []` is NOT metadata — that is the standard usage-only chunk,
/// so it still deserializes and flows through the empty-choices paths below.
///
/// A choice-less frame that reports an in-stream failure is NOT metadata either — see
/// `classify_choiceless_frame`.
fn parse_streaming_chunk(line: &str) -> Result<Option<StreamingChunk>, ProviderError> {
    let value: Value = serde_json::from_str(line).map_err(|e| {
        ProviderError::stream_decode_error(format!(
            "Failed to parse streaming chunk: {e}: {line:?}"
        ))
    })?;

    if let Some(error) = value.get("error") {
        let message = error
            .get("message")
            .and_then(|m| m.as_str())
            .unwrap_or("Unknown server error");
        return Err(ProviderError::ServerError(message.to_string()));
    }

    if value.get("object").and_then(|o| o.as_str()) == Some("error") {
        let message = value
            .get("message")
            .and_then(|m| m.as_str())
            .unwrap_or("Unknown server error");
        return Err(ProviderError::ServerError(message.to_string()));
    }

    if value
        .as_object()
        .is_some_and(|o| !o.contains_key("choices"))
    {
        if let Some(err) = classify_choiceless_frame(&value) {
            return Err(err);
        }
        return Ok(None);
    }

    serde_json::from_value(value).map(Some).map_err(|e| {
        ProviderError::stream_decode_error(format!(
            "Failed to parse streaming chunk: {e}: {line:?}"
        ))
    })
}

fn output_token_limit_marker(id: Option<String>) -> Message {
    let mut message = Message::assistant();
    if let Some(id) = id {
        message = message.with_id(id);
    }
    message.metadata.output_token_limit_reached = true;
    message
}

pub fn response_to_streaming_message<S>(
    mut stream: S,
) -> impl Stream<Item = anyhow::Result<(Option<Message>, Option<ProviderUsage>)>> + 'static
where
    S: Stream<Item = anyhow::Result<String>> + Unpin + MaybeSend + 'static,
{
    try_stream! {
        use futures::StreamExt;

        let mut accumulated_reasoning: Vec<Value> = Vec::new();
        let mut accumulated_reasoning_content = String::new();
        let mut think_filter = ThinkFilter::new();
        let mut saw_structured_reasoning = false;
        let mut yielded_reasoning_content_len = 0usize;
        let mut last_signature: Option<String> = None;
        // Buffer inline <think>...</think> content until we know whether structured
        // reasoning will arrive. Emitting it immediately and then receiving
        // reasoning_content in a later chunk would produce duplicated reasoning.
        let mut pending_inline_thinking = String::new();
        let mut last_seen_model: Option<String> = None;
        let mut last_response_id: Option<String> = None;
        let mut last_finish_reason: Option<String> = None;
        let mut output_token_limit_reached = false;
        let mut output_token_limit_metadata_emitted = false;
        let mut usage_emitted = false;

        'outer: while let Some(response) = stream.next().await {
            let response_str = response?;
            let line = strip_data_prefix(&response_str);

            if line.is_some_and(|l| l == "[DONE]") {
                break 'outer;
            }

            if line.is_none() || line.is_some_and(|l| l.is_empty()) {
                continue
            }

            let Some(chunk) = parse_streaming_chunk(
                line.ok_or_else(|| anyhow!("unexpected stream format"))?
            )? else {
                continue  // metadata-only frame
            };
            if let Some(model) = &chunk.model {
                last_seen_model = Some(model.clone());
            }
            if let Some(id) = &chunk.id {
                last_response_id = Some(id.clone());
            }

            if !chunk.choices.is_empty() {
                if let Some(details) = &chunk.choices[0].delta.reasoning_details {
                    accumulated_reasoning.extend(details.iter().cloned());
                }
                if let Some(rc) = chunk.choices[0].delta.reasoning_text() {
                    accumulated_reasoning_content.push_str(rc);
                    if !rc.is_empty() {
                        saw_structured_reasoning = true;
                        pending_inline_thinking.clear();
                    }
                }
            }

            if let Some(reason) = chunk.choices.first().and_then(|c| c.finish_reason.clone()) {
                last_finish_reason = Some(reason);
            }
            let mut usage = extract_usage_with_output_tokens(&chunk, last_seen_model.as_deref());
            if let Some(u) = usage.as_mut() {
                if let Some(reason) = &last_finish_reason {
                    u.finish_reasons = Some(vec![reason.clone()]);
                }
                if let Some(id) = &last_response_id {
                    u.response_id = Some(id.clone());
                }
            }
            output_token_limit_reached |= last_finish_reason.as_deref() == Some("length");

            if chunk.choices.is_empty() {
                usage_emitted |= usage.is_some();
                yield (None, usage)
            } else if chunk.choices[0].delta.tool_calls.as_ref().is_some_and(|tc| !tc.is_empty()) {
                let mut tool_call_data: ToolCallData = HashMap::new();

                if let Some(tool_calls) = &chunk.choices[0].delta.tool_calls {
                    for (position, tool_call) in tool_calls.iter().enumerate() {
                        if let (Some(id), Some(name)) = (&tool_call.id, &tool_call.function.name) {
                            let index = tool_call.index.unwrap_or(position as i32);
                            tool_call_data.insert(index, (id.clone(), name.clone(), tool_call.function.arguments.clone(), tool_call.extra.clone()));
                        }
                    }
                }

                let is_complete = matches!(
                    chunk.choices[0].finish_reason.as_deref(),
                    Some("tool_calls" | "length")
                );

                if !is_complete {
                    let mut done = false;
                    while !done {
                        if let Some(response_chunk) = stream.next().await {
                            let response_str = response_chunk?;
                            if let Some(line) = strip_data_prefix(&response_str) {
                                if line == "[DONE]" {
                                    break 'outer;
                                }

                                // A metadata frame here must NOT fall through to the
                                // empty-choices branch below, which ends accumulation and
                                // would truncate this tool call's arguments.
                                let Some(tool_chunk) = parse_streaming_chunk(line)? else {
                                    continue
                                };
                                if let Some(model) = &tool_chunk.model {
                                    last_seen_model = Some(model.clone());
                                }
                                if let Some(id) = &tool_chunk.id {
                                    last_response_id = Some(id.clone());
                                }
                                if let Some(reason) = tool_chunk.choices.first().and_then(|c| c.finish_reason.clone()) {
                                    last_finish_reason = Some(reason);
                                }

                                if let Some(mut chunk_usage) = extract_usage_with_output_tokens(&tool_chunk, last_seen_model.as_deref()) {
                                    if let Some(reason) = &last_finish_reason {
                                        chunk_usage.finish_reasons = Some(vec![reason.clone()]);
                                    }
                                    if let Some(id) = &last_response_id {
                                        chunk_usage.response_id = Some(id.clone());
                                    }
                                    usage = Some(chunk_usage);
                                }

                                if !tool_chunk.choices.is_empty() {
                                    output_token_limit_reached |=
                                        tool_chunk.choices[0].finish_reason.as_deref()
                                            == Some("length");

                                    if let Some(details) = &tool_chunk.choices[0].delta.reasoning_details {
                                        accumulated_reasoning.extend(details.iter().cloned());
                                    }
                                    if let Some(rc) = tool_chunk.choices[0].delta.reasoning_text() {
                                        accumulated_reasoning_content.push_str(rc);
                                        if !rc.is_empty() {
                                            saw_structured_reasoning = true;
                                            pending_inline_thinking.clear();
                                        }
                                    }
                                    if let Some(delta_tool_calls) = &tool_chunk.choices[0].delta.tool_calls {
                                        for delta_call in delta_tool_calls {
                                            if let Some(index) = delta_call.index {
                                                if let Some((_, ref mut stored_name, ref mut args, ref mut extra)) = tool_call_data.get_mut(&index) {
                                                    if let Some(new_name) = &delta_call.function.name {
                                                        if !new_name.is_empty() {
                                                            *stored_name = new_name.clone();
                                                        }
                                                    }
                                                    args.push_str(&delta_call.function.arguments);
                                                    if extra.is_none() && delta_call.extra.is_some() {
                                                        *extra = delta_call.extra.clone();
                                                    } else if let (Some(existing), Some(new_extra)) = (extra.as_mut(), &delta_call.extra) {
                                                        for (key, value) in new_extra {
                                                            existing.entry(key.clone()).or_insert(value.clone());
                                                        }
                                                    }
                                                } else if let (Some(id), Some(name)) = (&delta_call.id, &delta_call.function.name) {
                                                    tool_call_data.insert(index, (id.clone(), name.clone(), delta_call.function.arguments.clone(), delta_call.extra.clone()));
                                                }
                                            }
                                        }
                                    }
                                    if tool_chunk.choices[0].finish_reason.is_some() {
                                        done = true;
                                    }
                                } else {
                                    done = true;
                                }
                            }
                        } else {
                            break;
                        }
                    }
                }

                let _metadata: Option<ProviderMetadata> = if !accumulated_reasoning.is_empty() {
                    let mut map = ProviderMetadata::new();
                    map.insert("reasoning_details".to_string(), json!(accumulated_reasoning));
                    Some(map)
                } else {
                    None
                };

                let filtered = think_filter.push("");
                let mut flush_thinking = String::new();
                if !saw_structured_reasoning {
                    flush_thinking.push_str(&pending_inline_thinking);
                    flush_thinking.push_str(&filtered.thinking);
                }
                pending_inline_thinking.clear();
                if !filtered.content.is_empty() || !flush_thinking.is_empty() {
                    let mut filtered_contents = Vec::new();
                    if !filtered.content.is_empty() {
                        filtered_contents.push(MessageContentBlock::text(filtered.content));
                    }
                    if !flush_thinking.is_empty() {
                        filtered_contents.push(MessageContentBlock::thinking(flush_thinking, ""));
                    }

                    if !filtered_contents.is_empty() {
                        let mut msg = Message::new(
                            Role::Assistant,
                            chrono::Utc::now().timestamp(),
                            filtered_contents,
                        );

                        if let Some(id) = chunk.id.clone() {
                            msg = msg.with_id(id);
                        }

                        yield (Some(msg), None);
                    }
                }

                let mut contents = Vec::new();
                if yielded_reasoning_content_len < accumulated_reasoning_content.len() {
                    if let Some(unyielded_reasoning) =
                        accumulated_reasoning_content.get(yielded_reasoning_content_len..)
                    {
                        if !unyielded_reasoning.is_empty() {
                            contents.push(MessageContentBlock::thinking(unyielded_reasoning, ""));
                        }
                    }
                }
                accumulated_reasoning_content.clear();
                yielded_reasoning_content_len = 0;
                let mut sorted_indices: Vec<_> = tool_call_data.keys().cloned().collect();
                sorted_indices.sort();

                for index in sorted_indices {
                    if let Some((id, function_name, arguments, extra_fields)) = tool_call_data.get(&index) {
                        let metadata = if let Some(sig) = &last_signature {
                            let mut combined = extra_fields.clone().unwrap_or_default();
                            combined.insert(
                                GEMINI_THOUGHT_SIGNATURE_KEY.to_string(),
                                json!(sig)
                            );
                            Some(combined)
                        } else {
                            extra_fields.as_ref().filter(|m| !m.is_empty()).cloned()
                        };

                        let content = if output_token_limit_reached {
                            MessageContentBlock::tool_request_with_provider_index(
                                id.clone(),
                                Err(output_token_limit_tool_error(function_name, id)),
                                metadata.as_ref(),
                                index,
                            )
                        } else if arguments.is_empty() {
                            MessageContentBlock::tool_request_with_provider_index(
                                id.clone(),
                                Ok(CallToolRequestParams::new(function_name.clone()).with_arguments(object(json!({})))),
                                metadata.as_ref(),
                                index,
                            )
                        } else {
                            match parse_tool_arguments(arguments) {
                                Some(params) if params.is_object() => MessageContentBlock::tool_request_with_provider_index(
                                    id.clone(),
                                    Ok(CallToolRequestParams::new(function_name.clone()).with_arguments(object(params))),
                                    metadata.as_ref(),
                                    index,
                                ),
                                // Valid JSON but NOT an object (a bare array/string/number).
                                // Surface a tool error so the model retries instead of
                                // crashing the run (rmcp's `object()` debug-asserts on
                                // non-objects). Mirrors the non-streaming decoder.
                                Some(other) => {
                                    let error = ErrorData {
                                        code: ErrorCode::INVALID_PARAMS,
                                        message: Cow::from(format!(
                                            "Tool arguments for {} (id {}) must be a JSON object, got {}. Raw arguments: '{}'",
                                            function_name, id, describe_json_value(&other), arguments
                                        )),
                                        data: None,
                                    };
                                    MessageContentBlock::tool_request_with_provider_index(id.clone(), Err(error), metadata.as_ref(), index)
                                }
                                None => {
                                    let message_text = truncation_error_message(arguments)
                                        .unwrap_or_else(|| {
                                            format!("Could not interpret tool use parameters for id {id}")
                                        });
                                    let error = ErrorData {
                                        code: ErrorCode::INVALID_PARAMS,
                                        message: Cow::from(message_text),
                                        data: None,
                                    };
                                    MessageContentBlock::tool_request_with_provider_index(id.clone(), Err(error), metadata.as_ref(), index)
                                }
                            }
                        };

                        contents.push(content);
                    }
                }

                let mut msg = Message::new(
                    Role::Assistant,
                    chrono::Utc::now().timestamp(),
                    contents,
                );

                // Add ID if present
                if let Some(id) = chunk.id {
                    msg = msg.with_id(id);
                }
                msg.metadata.output_token_limit_reached = output_token_limit_reached;
                output_token_limit_metadata_emitted |= output_token_limit_reached;

                usage_emitted |= usage.is_some();
                yield (
                    Some(msg),
                    usage,
                )
            } else if chunk.choices[0].delta.content.is_some() || chunk.choices[0].delta.reasoning_text().is_some() {
                let mut content = Vec::new();

                if let Some(reasoning) = chunk.choices[0].delta.reasoning_text() {
                    let signature = last_signature.as_deref().unwrap_or("");
                    content.push(MessageContentBlock::thinking(reasoning, signature));
                    yielded_reasoning_content_len = accumulated_reasoning_content.len();
                }

                let (text_content, thought_signature) = extract_content_and_signature(chunk.choices[0].delta.content.as_ref());

                if let Some(sig) = thought_signature {
                    last_signature = Some(sig);
                }

                if let Some(text) = text_content {
                    let filtered = think_filter.push(&text);

                    if !saw_structured_reasoning && !filtered.thinking.is_empty() {
                        pending_inline_thinking.push_str(&filtered.thinking);
                    }

                    if !filtered.content.is_empty() {
                        content.push(MessageContentBlock::text(filtered.content));
                    }
                }

                if !content.is_empty() {
                    let mut msg = Message::new(
                        Role::Assistant,
                        chrono::Utc::now().timestamp(),
                        content,
                    );

                    if let Some(id) = chunk.id {
                        msg = msg.with_id(id);
                    }

                    let final_usage = if chunk.choices[0].finish_reason.is_some() {
                        usage
                    } else {
                        None
                    };
                    usage_emitted |= final_usage.is_some();
                    yield (Some(msg), final_usage)
                } else if usage.is_some() {
                    usage_emitted = true;
                    yield (None, usage)
                }
            } else if usage.is_some() {
                usage_emitted = true;
                yield (None, usage)
            }
        }

        let filtered = think_filter.finish();
        let mut trailing_thinking = String::new();
        if !saw_structured_reasoning {
            trailing_thinking.push_str(&pending_inline_thinking);
            trailing_thinking.push_str(&filtered.thinking);
        }
        pending_inline_thinking.clear();

        if !filtered.content.is_empty() || !trailing_thinking.is_empty() {
            let mut content = Vec::new();

            if !filtered.content.is_empty() {
                content.push(MessageContentBlock::text(filtered.content));
            }

            if !trailing_thinking.is_empty() {
                content.push(MessageContentBlock::thinking(trailing_thinking, ""));
            }

            let mut message = Message::new(
                Role::Assistant,
                chrono::Utc::now().timestamp(),
                content,
            );
            if let Some(id) = last_response_id.clone() {
                message = message.with_id(id);
            }
            message.metadata.output_token_limit_reached =
                output_token_limit_reached && !output_token_limit_metadata_emitted;
            output_token_limit_metadata_emitted |= message.metadata.output_token_limit_reached;

            yield (Some(message), None)
        }

        if output_token_limit_reached && !output_token_limit_metadata_emitted {
            yield (Some(output_token_limit_marker(last_response_id.clone())), None)
        }

        if !usage_emitted && (last_response_id.is_some() || last_finish_reason.is_some()) {
            let mut usage = ProviderUsage::new(
                last_seen_model.unwrap_or_else(|| "unknown".to_string()),
                Usage::default(),
            );
            usage.response_id = last_response_id;
            usage.finish_reasons = last_finish_reason.map(|reason| vec![reason]);
            yield (None, Some(usage))
        }
    }
}

pub fn create_request(
    model_config: &ModelConfig,
    system: &str,
    messages: &[Message],
    tools: &[Tool],
    image_format: &ImageFormat,
    for_streaming: bool,
) -> anyhow::Result<Value, Error> {
    create_request_with_options(
        model_config,
        system,
        messages,
        tools,
        image_format,
        for_streaming,
        OpenAiFormatOptions {
            preserve_thinking_context: true,
            supports_vision: model_config.supports_vision.unwrap_or_default(),
            ..Default::default()
        },
    )
}

pub fn create_request_with_options(
    model_config: &ModelConfig,
    system: &str,
    messages: &[Message],
    tools: &[Tool],
    image_format: &ImageFormat,
    for_streaming: bool,
    format_options: OpenAiFormatOptions,
) -> anyhow::Result<Value, Error> {
    let (wire_model_name, _) = extract_reasoning_effort(&model_config.model_name);
    create_request_for_model_with_options(
        model_config,
        &wire_model_name,
        &model_config.model_name,
        system,
        messages,
        tools,
        image_format,
        for_streaming,
        format_options,
    )
}

#[allow(clippy::too_many_arguments)]
pub fn create_request_for_model_with_options(
    model_config: &ModelConfig,
    wire_model_name: &str,
    capability_model_name: &str,
    system: &str,
    messages: &[Message],
    tools: &[Tool],
    image_format: &ImageFormat,
    for_streaming: bool,
    format_options: OpenAiFormatOptions,
) -> anyhow::Result<Value, Error> {
    let (model_name, legacy_reasoning_effort) = extract_reasoning_effort(capability_model_name);
    let is_reasoning_model = model_config.openai_reasoning_for_model(&model_name);
    let supports_xai_effort = supports_xai_reasoning_effort(&model_name);
    let reasoning_effort = if is_reasoning_model {
        model_config
            .thinking_effort()
            .map_or(legacy_reasoning_effort, |effort| {
                openai_reasoning_effort_for_thinking(&model_name, effort)
            })
    } else if supports_xai_effort {
        model_config
            .thinking_effort()
            .and_then(|effort| xai_reasoning_effort_for_thinking(&model_name, effort))
    } else {
        None
    };

    let system_message = serde_json::to_value(OpenAiMessage {
        role: if is_reasoning_model {
            "developer"
        } else {
            "system"
        }
        .to_string(),
        content: Some(Value::String(system.to_string())),
        ..Default::default()
    })?;

    let messages_spec = format_messages_with_options(messages, image_format, format_options);
    let mut tools_spec = format_tools(tools)?;

    validate_tool_schemas(&mut tools_spec);

    let mut messages_array = vec![system_message];
    messages_array.extend(messages_spec);

    let mut payload = json!({
        "model": wire_model_name,
        "messages": messages_array
    });

    if let Some(effort) = reasoning_effort {
        payload["reasoning_effort"] = json!(effort);
    }

    if !tools_spec.is_empty() {
        payload["tools"] = json!(tools_spec);
    }

    if !is_reasoning_model && !supports_xai_effort {
        if let Some(temp) = model_config.temperature {
            payload["temperature"] = json!(temp);
        }
    }

    // Only emit max_tokens / max_completion_tokens when the user (via
    // GOOSE_MAX_TOKENS) or a canonical model record has supplied a value.
    // For unknown models on OpenAI-compatible endpoints (e.g. llama_swap,
    // lmstudio) sending the historic 4096 default truncates non-trivial
    // responses; omitting the field lets the server use its own max.
    if let Some(max_tokens) = model_config.max_tokens {
        let key = if is_reasoning_model {
            "max_completion_tokens"
        } else {
            "max_tokens"
        };
        payload
            .as_object_mut()
            .unwrap()
            .insert(key.to_string(), json!(max_tokens));
    }

    if for_streaming {
        payload["stream"] = json!(true);
        payload["stream_options"] = json!({"include_usage": true});
    }

    if let Some(params) = &model_config.request_params {
        if let Some(obj) = payload.as_object_mut() {
            for (key, value) in params {
                if !is_goose_internal_request_param(key) && !is_reserved_request_param_key(key) {
                    obj.insert(key.clone(), value.clone());
                }
            }
        }
    }

    Ok(payload)
}

/// Extract an explicit reasoning-effort suffix from a model name.
///
/// Returns `(base_model_name, Some(effort))` when the user appended a
/// recognised suffix like `-high` or `-xhigh`, e.g. `gpt-5.4-high` →
/// `("gpt-5.4", Some("high"))`.
///
/// When no suffix is present the effort is `None` — callers should omit
/// the `reasoning` field entirely so the API applies its own per-model
/// default. This avoids hard-coding a default that may be invalid for
/// certain models (e.g. `gpt-5-pro` only accepts `high`; older o-series
/// models reject `none` and `xhigh`).
pub fn extract_reasoning_effort(model_name: &str) -> (String, Option<String>) {
    if !is_openai_responses_model(model_name) {
        return (model_name.to_string(), None);
    }

    static RE: OnceLock<Regex> = OnceLock::new();
    let re = RE.get_or_init(|| {
        Regex::new(r"(?i)^(?P<base>.+)-(?P<effort>none|low|medium|high|xhigh)$").unwrap()
    });

    if let Some(captures) = re.captures(model_name) {
        let base = captures["base"].to_string();
        let effort = captures["effort"].to_ascii_lowercase();
        return (base, Some(effort));
    }

    (model_name.to_string(), None)
}

/// True when the model should use the OpenAI Responses API.
///
/// The Responses API is backwards-compatible with all OpenAI reasoning
/// models, so every `o`-series (`o1`, `o3`, `o4`, …), `gpt-5`, and `gpt-6` variant
/// routes here. The matcher intentionally scans the full model identifier so
/// hosted aliases like `databricks-gpt-5.4`, `goose-o3-mini`, or
/// `headless-goose-o3-mini` work without provider-specific normalization.
pub fn is_openai_responses_model(model_name: &str) -> bool {
    static RE: OnceLock<Regex> = OnceLock::new();
    let re = RE.get_or_init(|| {
        Regex::new(r"(?i)(?:^|[-/])(?:o\d+(?:$|-)|gpt-(?:5|6)(?:$|[-.]))").unwrap()
    });
    re.is_match(model_name)
}

/// Returns whether an xAI Chat Completions model accepts `reasoning_effort`.
pub fn supports_xai_reasoning_effort(model_name: &str) -> bool {
    let model_name = model_name.to_ascii_lowercase();

    model_name.starts_with("grok-4.5")
        || model_name.starts_with("grok-4.3")
        || model_name.starts_with("grok-3-mini")
}

/// Returns whether an xAI model performs server-side reasoning.
pub fn is_xai_reasoning_model(model_name: &str) -> bool {
    let model_name = model_name.to_ascii_lowercase();

    if model_name.contains("non-reasoning") || model_name.contains("non_reasoning") {
        return false;
    }

    supports_xai_reasoning_effort(&model_name)
        || model_name.starts_with("grok-4.20")
        || model_name.starts_with("grok-4-0709")
        || model_name.starts_with("grok-4-fast-reasoning")
        || model_name.starts_with("grok-4-1-fast-reasoning")
}

/// Maps Goose's effort levels to values accepted by xAI Chat Completions.
pub fn xai_reasoning_effort_for_thinking(
    model_name: &str,
    effort: ThinkingEffort,
) -> Option<String> {
    let model_name = model_name.to_ascii_lowercase();
    let supports_none = model_name.starts_with("grok-4.3");
    let supports_medium = !model_name.starts_with("grok-3-mini");

    match effort {
        ThinkingEffort::Off if supports_none => Some("none".to_string()),
        ThinkingEffort::Off => Some("low".to_string()),
        ThinkingEffort::Low => Some("low".to_string()),
        ThinkingEffort::Medium if supports_medium => Some("medium".to_string()),
        ThinkingEffort::Medium | ThinkingEffort::High | ThinkingEffort::Max => {
            Some("high".to_string())
        }
    }
}

pub fn openai_reasoning_effort_for_thinking(
    model_name: &str,
    effort: ThinkingEffort,
) -> Option<String> {
    let catalog = crate::canonical::maybe_get_canonical_model("openai", model_name);
    let catalog_efforts = catalog
        .as_ref()
        .and_then(|model| model.reasoning_efforts.as_deref());

    let preferred: &[&str] = match effort {
        ThinkingEffort::Off => &["none", "minimal", "low"],
        ThinkingEffort::Low => &["low", "minimal", "medium", "high", "xhigh"],
        ThinkingEffort::Medium => &["medium", "high", "low", "xhigh"],
        ThinkingEffort::High => &["high", "medium", "xhigh", "low"],
        ThinkingEffort::Max => &["max", "xhigh", "high", "medium", "low"],
    };

    preferred
        .iter()
        .find(|level| match catalog_efforts {
            Some(values) => values.iter().any(|value| value == **level),
            None => openai_reasoning_efforts_for_model(model_name).contains(level),
        })
        .map(|level| (*level).to_string())
}

pub(crate) fn openai_reasoning_efforts_for_model(_model_name: &str) -> &'static [&'static str] {
    // For aliases without catalog effort options, send only widely supported
    // levels. Catalog entries override this fallback, including `none` and `max`.
    &["low", "medium", "high"]
}

const MAX_FUNCTION_NAME_LENGTH: usize = 128;

pub fn sanitize_function_name(name: &str) -> String {
    static RE: OnceLock<Regex> = OnceLock::new();
    let re = RE.get_or_init(|| Regex::new(r"[^a-zA-Z0-9_-]").unwrap());
    re.replace_all(name, "_")
        .chars()
        .take(MAX_FUNCTION_NAME_LENGTH)
        .collect()
}

pub fn is_valid_function_name(name: &str) -> bool {
    static RE: OnceLock<Regex> = OnceLock::new();
    let re = RE.get_or_init(|| Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap());
    re.is_match(name)
}

#[cfg(test)]
mod document_tests {
    use super::*;

    fn format(messages: &[Message]) -> Vec<Value> {
        format_messages_with_options(
            messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                supports_vision: true,
                ..Default::default()
            },
        )
    }

    #[test]
    fn user_document_becomes_a_file_content_part() {
        let spec = format(&[Message::user().with_document(
            "cGRmLWJ5dGVz",
            "application/pdf",
            Some("q3-report.pdf".to_string()),
        )]);

        assert_eq!(spec.len(), 1);
        assert_eq!(
            spec[0]["content"][0],
            json!({
                "type": "file",
                "file": {
                    "filename": "q3-report.pdf",
                    "file_data": "data:application/pdf;base64,cGRmLWJ5dGVz",
                }
            })
        );
    }

    #[test]
    fn assistant_document_becomes_a_text_part() {
        let spec = format(&[Message::assistant().with_document(
            "cGRmLWJ5dGVz",
            "application/pdf",
            Some("q3-report.pdf".to_string()),
        )]);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");
        let text = spec[0]["content"].as_str().unwrap();
        assert!(text.contains("q3-report.pdf"), "{text}");
        assert!(text.contains("user messages"), "{text}");
        assert!(!text.contains("cGRmLWJ5dGVz"), "{text}");
    }

    #[test]
    fn unsupported_document_media_type_becomes_an_explicit_text_part() {
        let spec = format(&[Message::user().with_document(
            "cm93cw==",
            "text/csv",
            Some("rows.csv".to_string()),
        )]);

        let text = spec[0]["content"].as_str().unwrap();
        assert!(text.contains("rows.csv"), "{text}");
        assert!(text.contains("application/pdf"), "{text}");
        assert!(!text.contains("cm93cw=="), "{text}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::conversation::message::Message;
    use rmcp::model::CallToolResult;
    use rmcp::object;
    use serde_json::json;
    use test_case::test_case;
    use tokio::pin;
    use tokio_stream::{self, StreamExt};

    fn test_model_config(model_name: &str) -> ModelConfig {
        ModelConfig::new(model_name)
    }

    #[test]
    fn test_validate_tool_schemas() {
        // Test case 1: Empty parameters object
        // Input JSON with an incomplete parameters object
        let mut actual = vec![json!({
            "type": "function",
            "function": {
                "name": "test_func",
                "description": "test description",
                "parameters": {
                    "type": "object"
                }
            }
        })];

        // Run the function to validate and update schemas
        validate_tool_schemas(&mut actual);

        // Expected JSON after validation
        let expected = vec![json!({
            "type": "function",
            "function": {
                "name": "test_func",
                "description": "test description",
                "parameters": {
                    "type": "object",
                    "properties": {},
                    "required": []
                }
            }
        })];

        // Compare entire JSON structures instead of individual fields
        assert_eq!(actual, expected);

        // Test case 2: Missing type field
        let mut tools = vec![json!({
            "type": "function",
            "function": {
                "name": "test_func",
                "description": "test description",
                "parameters": {
                    "properties": {}
                }
            }
        })];

        validate_tool_schemas(&mut tools);

        let params = tools[0]["function"]["parameters"].as_object().unwrap();
        assert_eq!(params["type"], "object");

        // Test case 3: Complete valid schema should remain unchanged
        let original_schema = json!({
            "type": "function",
            "function": {
                "name": "test_func",
                "description": "test description",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "City and country"
                        }
                    },
                    "required": ["location"]
                }
            }
        });

        let mut tools = vec![original_schema.clone()];
        validate_tool_schemas(&mut tools);
        assert_eq!(tools[0], original_schema);

        // Test case 4: anyOf nullable is unwrapped, preserving sibling metadata
        let mut tools = vec![json!({
            "type": "function",
            "function": {
                "name": "shell",
                "description": "run shell",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "command": { "type": "string" },
                        "timeout_secs": {
                            "description": "timeout in seconds",
                            "anyOf": [
                                { "type": "integer", "format": "uint64", "minimum": 0 },
                                { "type": "null" }
                            ]
                        }
                    },
                    "required": ["command"]
                }
            }
        })];
        validate_tool_schemas(&mut tools);
        let timeout_schema = &tools[0]["function"]["parameters"]["properties"]["timeout_secs"];
        assert_eq!(timeout_schema["type"], "integer");
        assert_eq!(timeout_schema["format"], "uint64");
        assert_eq!(timeout_schema["description"], "timeout in seconds");
        assert!(timeout_schema.get("anyOf").is_none());

        // Test case 4b: type array form (schemars 1.x style for nullable primitives)
        let mut tools = vec![json!({
            "type": "function",
            "function": {
                "name": "shell",
                "description": "run shell",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "command": { "type": "string" },
                        "timeout_secs": {
                            "type": ["integer", "null"],
                            "format": "uint64",
                            "minimum": 0
                        }
                    },
                    "required": ["command"]
                }
            }
        })];
        validate_tool_schemas(&mut tools);
        let timeout_schema = &tools[0]["function"]["parameters"]["properties"]["timeout_secs"];
        assert_eq!(timeout_schema["type"], "integer");
        assert!(!timeout_schema["type"].is_array());
    }

    #[test]
    fn test_validate_tool_schemas_sanitizes_defs() {
        let mut tools = vec![json!({
            "type": "function",
            "function": {
                "name": "cache",
                "description": "manage cache",
                "parameters": {
                    "type": "object",
                    "$defs": {
                        "CacheCommand": {
                            "oneOf": [
                                { "description": "List cached files", "type": "string", "const": "list" },
                                { "description": "Clear cached files", "type": "string", "const": "clear" }
                            ]
                        },
                        "TextStyle": {
                            "type": "object",
                            "properties": {
                                "size": { "type": ["integer", "null"], "format": "int32" }
                            }
                        }
                    },
                    "properties": {
                        "command": { "$ref": "#/$defs/CacheCommand" },
                        "style": { "$ref": "#/$defs/TextStyle" }
                    },
                    "required": ["command"]
                }
            }
        })];
        validate_tool_schemas(&mut tools);
        let defs = &tools[0]["function"]["parameters"]["$defs"];
        assert!(defs["CacheCommand"].get("oneOf").is_none());
        assert_eq!(defs["CacheCommand"]["anyOf"].as_array().unwrap().len(), 2);
        assert_eq!(defs["TextStyle"]["properties"]["size"]["type"], "integer");
    }

    const OPENAI_TOOL_USE_RESPONSE: &str = r#"{
        "choices": [{
            "role": "assistant",
            "message": {
                "tool_calls": [{
                    "id": "1",
                    "function": {
                        "name": "example_fn",
                        "arguments": "{\"param\": \"value\"}"
                    }
                }]
            }
        }],
        "usage": {
            "input_tokens": 10,
            "output_tokens": 25,
            "total_tokens": 35
        }
    }"#;

    #[test]
    fn test_format_messages() -> anyhow::Result<()> {
        let message = Message::user().with_text("Hello");
        let spec = format_messages(&[message], &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "user");
        assert_eq!(spec[0]["content"], "Hello");
        Ok(())
    }

    #[test]
    fn test_format_tools() -> anyhow::Result<()> {
        let tool = Tool::new(
            "test_tool",
            "A test tool",
            object!({
                "type": "object",
                "properties": {
                    "input": {
                        "type": "string",
                        "description": "Test parameter"
                    }
                },
                "required": ["input"]
            }),
        );

        let spec = format_tools(&[tool])?;

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["type"], "function");
        assert_eq!(spec[0]["function"]["name"], "test_tool");
        assert_eq!(
            spec[0]["function"]["strict"],
            json!(false),
            "Some chat-completions upstreams default strict to true when the flag is absent, but MCP tool schemas are not strict-compatible; must explicitly set strict: false"
        );
        Ok(())
    }

    #[test]
    fn test_format_messages_complex() -> anyhow::Result<()> {
        let mut messages = vec![
            Message::assistant().with_text("Hello!"),
            Message::user().with_text("How are you?"),
            Message::assistant().with_tool_request(
                "tool1",
                Ok(CallToolRequestParams::new("example")
                    .with_arguments(object!({"param1": "value1"}))),
            ),
        ];

        // Get the ID from the tool request to use in the response
        let tool_id = if let MessageContentBlock::ToolRequest(request) = &messages[2].content[0] {
            request.id.clone()
        } else {
            panic!("should be tool request");
        };

        messages.push(Message::user().with_tool_response(
            tool_id,
            Ok(CallToolResult::success(vec![ContentBlock::text("Result")])),
        ));

        let spec = format_messages(&messages, &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 4);
        assert_eq!(spec[0]["role"], "assistant");
        assert_eq!(spec[0]["content"], "Hello!");
        assert_eq!(spec[1]["role"], "user");
        assert_eq!(spec[1]["content"], "How are you?");
        assert_eq!(spec[2]["role"], "assistant");
        assert!(spec[2]["tool_calls"].is_array());
        assert_eq!(spec[3]["role"], "tool");
        assert_eq!(spec[3]["content"], "Result");
        assert_eq!(spec[3]["tool_call_id"], spec[2]["tool_calls"][0]["id"]);

        Ok(())
    }

    #[test]
    fn test_format_messages_multiple_content() -> anyhow::Result<()> {
        let mut messages = vec![Message::assistant().with_tool_request(
            "tool1",
            Ok(CallToolRequestParams::new("example").with_arguments(object!({"param1": "value1"}))),
        )];

        // Get the ID from the tool request to use in the response
        let tool_id = if let MessageContentBlock::ToolRequest(request) = &messages[0].content[0] {
            request.id.clone()
        } else {
            panic!("should be tool request");
        };

        messages.push(Message::user().with_tool_response(
            tool_id,
            Ok(CallToolResult::success(vec![ContentBlock::text("Result")])),
        ));

        let spec = format_messages(&messages, &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 2);
        assert_eq!(spec[0]["role"], "assistant");
        assert!(spec[0]["tool_calls"].is_array());
        assert_eq!(spec[1]["role"], "tool");
        assert_eq!(spec[1]["content"], "Result");
        assert_eq!(spec[1]["tool_call_id"], spec[0]["tool_calls"][0]["id"]);

        Ok(())
    }

    #[test]
    fn test_format_tools_duplicate() -> anyhow::Result<()> {
        let tool1 = Tool::new(
            "test_tool",
            "Test tool",
            object!({
                "type": "object",
                "properties": {
                    "input": {
                        "type": "string",
                        "description": "Test parameter"
                    }
                },
                "required": ["input"]
            }),
        );

        let tool2 = Tool::new(
            "test_tool",
            "Test tool",
            object!({
                "type": "object",
                "properties": {
                    "input": {
                        "type": "string",
                        "description": "Test parameter"
                    }
                },
                "required": ["input"]
            }),
        );

        let result = format_tools(&[tool1, tool2]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Duplicate tool name"));

        Ok(())
    }

    #[test]
    fn test_format_tools_empty() -> anyhow::Result<()> {
        let spec = format_tools(&[])?;
        assert!(spec.is_empty());
        Ok(())
    }

    #[test]
    fn test_format_messages_with_image_path() -> anyhow::Result<()> {
        // Create a temporary PNG file with valid PNG magic numbers
        let temp_dir = tempfile::tempdir()?;
        let png_path = temp_dir.path().join("test.png");
        let png_data = [
            0x89, 0x50, 0x4E, 0x47, // PNG magic number
            0x0D, 0x0A, 0x1A, 0x0A, // PNG header
            0x00, 0x00, 0x00, 0x0D, // Rest of fake PNG data
        ];
        std::fs::write(&png_path, png_data)?;
        let png_path_str = png_path.to_str().unwrap();

        // Create user message with image path - should load the image when vision is supported
        let user_message = Message::user().with_text(format!("Here is an image: {}", png_path_str));
        let spec = format_messages_with_options(
            &[user_message],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "user");

        // Content should be an array with text and image
        let content = spec[0]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "text");
        assert!(content[0]["text"].as_str().unwrap().contains(png_path_str));
        assert_eq!(content[1]["type"], "image_url");
        assert!(content[1]["image_url"]["url"]
            .as_str()
            .unwrap()
            .starts_with("data:image/png;base64,"));

        // Create assistant message with same text - should NOT load the image
        let assistant_message =
            Message::assistant().with_text(format!("I saved the output to {}", png_path_str));
        let spec = format_messages_with_options(
            &[assistant_message],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");

        // Content should be plain text, NOT an array with image
        let content = spec[0]["content"].as_str();
        assert!(
            content.is_some(),
            "Assistant message content should be a string, not an array with image"
        );
        assert!(content.unwrap().contains(png_path_str));

        Ok(())
    }

    #[test]
    fn test_format_messages_with_image_path_passthrough_when_not_vision() -> anyhow::Result<()> {
        // Create a temporary PNG file with valid PNG magic numbers
        let temp_dir = tempfile::tempdir()?;
        let png_path = temp_dir.path().join("test.png");
        let png_data = [
            0x89, 0x50, 0x4E, 0x47, // PNG magic number
            0x0D, 0x0A, 0x1A, 0x0A, // PNG header
            0x00, 0x00, 0x00, 0x0D, // Rest of fake PNG data
        ];
        std::fs::write(&png_path, png_data)?;
        let png_path_str = png_path.to_str().unwrap();

        // User message with image path: with vision NOT supported, the path must
        // pass through as plain text (a non-vision model can forward it to a
        // vision subagent instead of 400ing on an injected image_url block).
        let user_message = Message::user().with_text(format!("Here is an image: {}", png_path_str));

        let spec = format_messages_with_options(
            std::slice::from_ref(&user_message),
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: false,
                ..Default::default()
            },
        );
        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "user");
        // Single text block collapses to a plain string — the path survives verbatim.
        let content = spec[0]["content"].as_str().unwrap();
        assert!(content.contains(png_path_str));
        assert!(!content.contains("image_url"));
        assert!(!content.contains("data:image"));

        // Default options (bare format_messages wrapper) must behave the same:
        // unaffirmed vision -> passthrough.
        let spec = format_messages(&[user_message], &ImageFormat::OpenAi);
        let content = spec[0]["content"].as_str().unwrap();
        assert!(content.contains(png_path_str));
        assert!(!content.contains("image_url"));

        Ok(())
    }

    #[test]
    fn test_create_request_threads_supports_vision() -> anyhow::Result<()> {
        // Create a temporary PNG file with valid PNG magic numbers
        let temp_dir = tempfile::tempdir()?;
        let png_path = temp_dir.path().join("test.png");
        let png_data = [
            0x89, 0x50, 0x4E, 0x47, // PNG magic number
            0x0D, 0x0A, 0x1A, 0x0A, // PNG header
            0x00, 0x00, 0x00, 0x0D, // Rest of fake PNG data
        ];
        std::fs::write(&png_path, png_data)?;
        let png_path_str = png_path.to_str().unwrap();
        let message = Message::user().with_text(format!("Here is an image: {}", png_path_str));

        // Vision affirmed: path is converted to an image_url block.
        let vision = ModelConfig::new("gpt-4o").with_vision_support(true);
        let request = create_request(
            &vision,
            "system",
            std::slice::from_ref(&message),
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let messages = request["messages"].as_array().unwrap();
        let content = messages[1]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[1]["type"], "image_url");

        // Unknown (None): passthrough, no image_url.
        let unknown = ModelConfig::new("gpt-4o");
        let request = create_request(
            &unknown,
            "system",
            std::slice::from_ref(&message),
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let messages = request["messages"].as_array().unwrap();
        let content = messages[1]["content"].as_str().unwrap();
        assert!(content.contains(png_path_str));
        assert!(!content.contains("image_url"));

        // Explicitly non-vision: passthrough, no image_url.
        let non_vision = ModelConfig::new("gpt-4o").with_vision_support(false);
        let request = create_request(
            &non_vision,
            "system",
            &[message],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let messages = request["messages"].as_array().unwrap();
        let content = messages[1]["content"].as_str().unwrap();
        assert!(!content.contains("image_url"));

        Ok(())
    }

    #[test]
    fn test_format_messages_with_image_block_passthrough_when_not_vision() -> anyhow::Result<()> {
        let user_message = Message::user().with_image("aW1hZ2VkYXRh", "image/png");

        // Non-vision: explicit image content is replaced with a text placeholder
        // at format time — session history is untouched, so a vision model (or a
        // delegated vision subagent) can still see the real image later.
        let spec = format_messages_with_options(
            std::slice::from_ref(&user_message),
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: false,
                ..Default::default()
            },
        );
        assert_eq!(spec.len(), 1);
        let content = spec[0]["content"].as_str().unwrap();
        assert_eq!(content, "[image omitted: model does not support vision]");
        assert!(!content.contains("image_url"));
        assert!(!content.contains("data:image"));

        // Vision: the image is converted to an image_url block (existing behavior).
        let spec = format_messages_with_options(
            &[user_message],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );
        let content = spec[0]["content"].as_array().unwrap();
        assert_eq!(content.len(), 1);
        assert_eq!(content[0]["type"], "image_url");
        assert!(content[0]["image_url"]["url"]
            .as_str()
            .unwrap()
            .starts_with("data:image/png;base64,"));

        Ok(())
    }

    #[test]
    fn test_tool_response_image_omitted_when_not_vision() -> anyhow::Result<()> {
        let tool_response = Message::user().with_tool_response(
            "tool1",
            Ok(rmcp::model::CallToolResult::success(vec![
                rmcp::model::ContentBlock::image("aW1hZ2VkYXRh", "image/png"),
            ])),
        );

        // Non-vision: the separate user image message is NOT emitted — this is
        // what un-bricks sessions (a converted image in the next request 400s).
        let spec = format_messages_with_options(
            std::slice::from_ref(&tool_response),
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: false,
                ..Default::default()
            },
        );
        let serialized = serde_json::to_value(&spec).unwrap().to_string();
        assert!(!serialized.contains("image_url"));
        assert!(serialized.contains(
            "This tool result included an image that was omitted as the model does not support vision."
        ));

        // Vision: the separate user image message IS emitted (existing behavior).
        let spec = format_messages_with_options(
            &[tool_response],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );
        let serialized = serde_json::to_value(&spec).unwrap().to_string();
        assert!(serialized.contains("image_url"));
        assert!(serialized
            .contains("This tool result included an image that is uploaded in the next message."));

        Ok(())
    }

    #[test]
    fn test_parallel_tool_responses_with_images_are_consecutive() {
        // #11893: a synthetic user image message between the tool results of one
        // tool_calls batch makes strict OpenAI-compatible APIs reject the request.
        let messages = vec![
            Message::assistant()
                .with_tool_request("call_a", Ok(CallToolRequestParams::new("read_image")))
                .with_tool_request("call_b", Ok(CallToolRequestParams::new("read_image"))),
            Message::user()
                .with_tool_response(
                    "call_a",
                    Ok(CallToolResult::success(vec![ContentBlock::image(
                        "aW1hZ2VkYXRhYQ==",
                        "image/png",
                    )])),
                )
                .with_tool_response(
                    "call_b",
                    Ok(CallToolResult::success(vec![ContentBlock::image(
                        "aW1hZ2VkYXRhYg==",
                        "image/png",
                    )])),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );

        let roles: Vec<&str> = spec.iter().map(|m| m["role"].as_str().unwrap()).collect();
        assert_eq!(roles, vec!["assistant", "tool", "tool", "user", "user"]);
        assert_eq!(spec[1]["tool_call_id"], "call_a");
        assert_eq!(spec[2]["tool_call_id"], "call_b");
    }

    #[test]
    fn test_mixed_tool_responses_image_and_text_ordering() {
        // A text-only result between two image results must not let the image
        // messages split the batch either.
        let messages = vec![
            Message::assistant()
                .with_tool_request("call_a", Ok(CallToolRequestParams::new("read_image")))
                .with_tool_request("call_b", Ok(CallToolRequestParams::new("shell")))
                .with_tool_request("call_c", Ok(CallToolRequestParams::new("read_image"))),
            Message::user()
                .with_tool_response(
                    "call_a",
                    Ok(CallToolResult::success(vec![ContentBlock::image(
                        "aW1hZ2VkYXRhYQ==",
                        "image/png",
                    )])),
                )
                .with_tool_response(
                    "call_b",
                    Ok(CallToolResult::success(vec![ContentBlock::text(
                        "text result",
                    )])),
                )
                .with_tool_response(
                    "call_c",
                    Ok(CallToolResult::success(vec![ContentBlock::image(
                        "aW1hZ2VkYXRhYw==",
                        "image/png",
                    )])),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );

        let roles: Vec<&str> = spec.iter().map(|m| m["role"].as_str().unwrap()).collect();
        assert_eq!(
            roles,
            vec!["assistant", "tool", "tool", "tool", "user", "user"]
        );
        assert_eq!(spec[1]["tool_call_id"], "call_a");
        assert_eq!(spec[2]["tool_call_id"], "call_b");
        assert_eq!(spec[3]["tool_call_id"], "call_c");
    }

    #[test]
    fn test_format_messages_with_text_and_image_preserves_order() {
        // Text before image: order should be [text, image]
        let msg_text_first = Message::user()
            .with_text("Describe this image")
            .with_image("aW1hZ2VkYXRh", "image/png");

        let spec = format_messages_with_options(
            &[msg_text_first],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );
        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "user");

        let content = spec[0]["content"]
            .as_array()
            .expect("content should be an array");
        assert_eq!(content.len(), 2, "expected text + image entries");
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[0]["text"], "Describe this image");
        assert_eq!(content[1]["type"], "image_url");

        // Image before text: order should be [image, text]
        let msg_image_first = Message::user()
            .with_image("aW1hZ2VkYXRh", "image/png")
            .with_text("What do you see?");

        let spec2 = format_messages_with_options(
            &[msg_image_first],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: true,
                ..Default::default()
            },
        );
        let content2 = spec2[0]["content"]
            .as_array()
            .expect("content should be an array");
        assert_eq!(content2.len(), 2, "expected image + text entries");
        assert_eq!(content2[0]["type"], "image_url");
        assert_eq!(content2[1]["type"], "text");
        assert_eq!(content2[1]["text"], "What do you see?");
    }

    #[test]
    fn test_response_to_message_text() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "role": "assistant",
                "message": {
                    "content": "Hello from John Cena!"
                }
            }],
            "usage": {
                "input_tokens": 10,
                "output_tokens": 25,
                "total_tokens": 35
            }
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 1);
        if let MessageContentBlock::Text(text) = &message.content[0] {
            assert_eq!(text.text, "Hello from John Cena!");
        } else {
            panic!("Expected Text content");
        }
        assert!(matches!(message.role, Role::Assistant));

        Ok(())
    }

    #[test]
    fn test_record_response_metadata() {
        let response = json!({
            "id": "chatcmpl-123",
            "choices": [
                {"finish_reason": "stop"},
                {"finish_reason": "tool_calls"}
            ]
        });
        let mut usage = ProviderUsage::new("test-model".to_string(), Usage::default());

        record_response_metadata(&mut usage, &response);

        assert_eq!(usage.response_id.as_deref(), Some("chatcmpl-123"));
        assert_eq!(
            usage.finish_reasons,
            Some(vec!["stop".to_string(), "tool_calls".to_string()])
        );
    }

    #[test]
    fn test_response_to_message_marks_length_finish_reason() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "Partial answer"
                },
                "finish_reason": "length"
            }]
        });

        let message = response_to_message(&response)?;

        assert_eq!(message.as_concat_text(), "Partial answer");
        assert!(message.metadata.output_token_limit_reached);

        Ok(())
    }

    #[test]
    fn test_response_to_message_rejects_length_terminated_tool_calls() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [
                        {
                            "id": "call_empty",
                            "type": "function",
                            "function": {
                                "name": "empty_tool",
                                "arguments": ""
                            }
                        },
                        {
                            "id": "call_valid",
                            "type": "function",
                            "function": {
                                "name": "valid_tool",
                                "arguments": "{\"value\":true}"
                            }
                        }
                    ]
                },
                "finish_reason": "length"
            }]
        });

        let message = response_to_message(&response)?;

        assert!(message.metadata.output_token_limit_reached);
        assert_eq!(message.content.len(), 2);
        for (content, expected_id) in message.content.iter().zip(["call_empty", "call_valid"]) {
            let MessageContentBlock::ToolRequest(request) = content else {
                panic!("expected tool request");
            };
            assert_eq!(request.id, expected_id);
            let error = request
                .tool_call
                .as_ref()
                .expect_err("length-terminated tool call must not be executable");
            assert_eq!(error.code, ErrorCode::INVALID_PARAMS);
            assert!(error.message.contains("output token limit"));
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_valid_toolrequest() -> anyhow::Result<()> {
        let response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        let message = response_to_message(&response)?;

        assert_eq!(message.content.len(), 1);
        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            let tool_call = request.tool_call.as_ref().unwrap();
            assert_eq!(tool_call.name, "example_fn");
            assert_eq!(tool_call.arguments, Some(object!({"param": "value"})));
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_empty_func_name() -> anyhow::Result<()> {
        let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        response["choices"][0]["message"]["tool_calls"][0]["function"]["name"] = json!("");

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            match &request.tool_call {
                Err(ErrorData {
                    code: ErrorCode::INVALID_REQUEST,
                    message: msg,
                    data: None,
                }) => {
                    assert!(msg.starts_with("The provided function name"));
                }
                _ => panic!("Expected invalid-request error for empty name"),
            }
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_passes_names_through_to_dispatch() -> anyhow::Result<()> {
        for name in [
            "developer.shell",
            "functions.example_fn",
            "example fn",
            "???!",
        ] {
            let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
            response["choices"][0]["message"]["tool_calls"][0]["function"]["name"] = json!(name);

            let message = response_to_message(&response)?;

            if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
                let tool_call = request.tool_call.as_ref().expect("tool call should parse");
                assert_eq!(tool_call.name, name, "name must pass through verbatim");
            } else {
                panic!("Expected ToolRequest content");
            }
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_fenced_arguments() -> anyhow::Result<()> {
        let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        response["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"] =
            json!("```json\n{\"param\": \"value\"}\n```");

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            let tool_call = request.tool_call.as_ref().expect("tool call should parse");
            assert_eq!(tool_call.arguments, Some(object!({"param": "value"})));
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_json_decode_error() -> anyhow::Result<()> {
        let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        response["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"] =
            json!("invalid json {");

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            match &request.tool_call {
                Err(ErrorData {
                    code: ErrorCode::INVALID_PARAMS,
                    message: msg,
                    data: None,
                }) => {
                    assert!(msg.contains("tool arguments") || msg.contains("truncated"));
                }
                _ => panic!("Expected InvalidParameters error"),
            }
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_non_object_arguments() -> anyhow::Result<()> {
        // Weaker models sometimes emit tool arguments that are valid JSON but
        // not an object (here, a bare array). This must surface as a tool error,
        // NOT panic via rmcp's `object()` debug-assert.
        let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        response["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"] =
            json!("[1, 2, 3]");

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            match &request.tool_call {
                Err(ErrorData {
                    code: ErrorCode::INVALID_PARAMS,
                    message: msg,
                    data: None,
                }) => {
                    assert!(msg.contains("must be a JSON object"));
                    assert!(msg.contains("an array"));
                    assert!(
                        msg.contains("example_fn"),
                        "error must name the original tool so the model can retry it: {msg}"
                    );
                }
                _ => panic!("Expected InvalidParameters error for non-object args"),
            }
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_empty_argument() -> anyhow::Result<()> {
        let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
        response["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"] =
            serde_json::Value::String("".to_string());

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            let tool_call = request.tool_call.as_ref().unwrap();
            assert_eq!(tool_call.name, "example_fn");
            assert_eq!(tool_call.arguments, Some(object!({})));
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_api_error() -> anyhow::Result<()> {
        // Test that API responses with an "error" field return the error message
        let response = json!({
            "error": {
                "message": "You have exceeded your quota",
                "type": "insufficient_quota",
                "code": "quota_exceeded"
            }
        });

        let result = response_to_message(&response);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("API error:"));
        assert!(err.to_string().contains("You have exceeded your quota"));

        Ok(())
    }

    #[test]
    fn test_response_to_message_api_error_unknown() -> anyhow::Result<()> {
        // Test that API responses with an "error" field but no message return "Unknown error"
        let response = json!({
            "error": {
                "type": "some_error"
            }
        });

        let result = response_to_message(&response);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("API error:"));
        assert!(err.to_string().contains("Unknown error"));

        Ok(())
    }

    #[test]
    fn test_response_to_message_no_choices() -> anyhow::Result<()> {
        // Test that responses without "choices" return an error
        let response = json!({
            "id": "chatcmpl-123",
            "object": "chat.completion",
            "created": 1234567890
        });

        let result = response_to_message(&response);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("No message in API response"));

        Ok(())
    }

    #[test]
    fn test_request_serializes_content_before_tool_calls() -> anyhow::Result<()> {
        let messages = vec![
            Message::user().with_text("Find the answer"),
            Message::assistant()
                .with_text("I'll look it up")
                .with_tool_request("call_1", Ok(CallToolRequestParams::new("lookup"))),
            Message::user().with_tool_response(
                "call_1",
                Ok(CallToolResult::success(vec![ContentBlock::text("42")])),
            ),
        ];
        let request = create_request(
            &test_model_config("model-service"),
            "system",
            &messages,
            &[],
            &ImageFormat::OpenAi,
            true,
        )?;
        let wire = serde_json::to_string(&request)?;
        let parsed: Value = serde_json::from_str(&wire)?;
        let assistant = serde_json::to_string(&parsed["messages"][2])?;
        assert!(assistant.find("\"content\"").unwrap() < assistant.find("\"tool_calls\"").unwrap());
        assert_eq!(parsed["messages"].as_array().unwrap().len(), 4);
        assert_eq!(parsed["messages"][3]["role"], "tool");
        assert_eq!(parsed["messages"][3]["tool_call_id"], "call_1");
        Ok(())
    }

    #[test]
    fn test_tool_call_serialization_preserves_provider_metadata() {
        let metadata = json!({
            "extra_content": {"google": {"thought_signature": "signature"}},
        });
        let call = OpenAiToolCall {
            id: "original".to_string(),
            kind: "function",
            function: OpenAiFunctionCall {
                name: "lookup".to_string(),
                arguments: "{}".to_string(),
            },
            metadata: metadata.as_object().unwrap().clone(),
        };
        let wire = serde_json::to_string(&call).unwrap();
        let serialized: Value = serde_json::from_str(&wire).unwrap();
        assert_eq!(serialized["id"], "original");
        assert_eq!(serialized["type"], "function");
        assert_eq!(serialized["function"]["name"], "lookup");
        assert_eq!(serialized["function"]["arguments"], "{}");
        assert_eq!(serialized["extra_content"], metadata["extra_content"]);
    }

    #[test]
    fn test_message_serialization_distinguishes_missing_and_null_content() {
        let missing = serde_json::to_value(OpenAiMessage::default()).unwrap();
        assert!(missing.get("content").is_none());
        let null = serde_json::to_value(OpenAiMessage {
            content: Some(Value::Null),
            ..Default::default()
        })
        .unwrap();
        assert_eq!(null.get("content"), Some(&Value::Null));
    }

    #[test]
    fn test_format_messages_tool_request_with_none_arguments() -> anyhow::Result<()> {
        // Test that tool calls with None arguments are formatted as "{}" string
        let message = Message::assistant()
            .with_tool_request("tool1", Ok(CallToolRequestParams::new("test_tool")));

        let spec = format_messages(&[message], &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");
        assert!(spec[0]["tool_calls"].is_array());

        let tool_call = &spec[0]["tool_calls"][0];
        assert_eq!(tool_call["id"], "tool1");
        assert_eq!(tool_call["type"], "function");
        assert_eq!(tool_call["function"]["name"], "test_tool");
        // This should be the string "{}", not null
        assert_eq!(tool_call["function"]["arguments"], "{}");

        Ok(())
    }

    #[test]
    fn test_format_messages_tool_request_with_some_arguments() -> anyhow::Result<()> {
        // Test that tool calls with Some arguments are properly JSON-serialized
        let message = Message::assistant().with_tool_request(
            "tool1",
            Ok(CallToolRequestParams::new("test_tool")
                .with_arguments(object!({"param": "value", "number": 42}))),
        );

        let spec = format_messages(&[message], &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");
        assert!(spec[0]["tool_calls"].is_array());

        let tool_call = &spec[0]["tool_calls"][0];
        assert_eq!(tool_call["id"], "tool1");
        assert_eq!(tool_call["type"], "function");
        assert_eq!(tool_call["function"]["name"], "test_tool");
        // This should be a JSON string representation
        let args_str = tool_call["function"]["arguments"].as_str().unwrap();
        let parsed_args: Value = serde_json::from_str(args_str)?;
        assert_eq!(parsed_args["param"], "value");
        assert_eq!(parsed_args["number"], 42);

        Ok(())
    }

    #[test]
    fn test_format_messages_multiple_text_blocks() -> anyhow::Result<()> {
        let message = Message::user()
            .with_text("--- Resource: file:///test.md ---\n# Test\n\n---\n")
            .with_text(" What is in the file?");

        let spec = format_messages(&[message], &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "user");
        assert_eq!(
            spec[0]["content"],
            "--- Resource: file:///test.md ---\n# Test\n\n---\n\n What is in the file?"
        );
        Ok(())
    }

    #[test]
    fn test_create_request_gpt_4o() -> anyhow::Result<()> {
        // Test default medium reasoning effort for O3 model
        let model_config = test_model_config("gpt-4o").with_max_tokens(Some(1024));
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();
        let expected = json!({
            "model": "gpt-4o",
            "messages": [
                {
                    "role": "system",
                    "content": "system"
                }
            ],
            "max_tokens": 1024
        });

        for (key, value) in expected.as_object().unwrap() {
            assert_eq!(obj.get(key).unwrap(), value);
        }

        Ok(())
    }

    #[test]
    fn test_create_request_omits_max_tokens_when_unset() -> anyhow::Result<()> {
        // Unknown models on OpenAI-compatible local providers (llama_swap,
        // lmstudio) have no canonical record and no GOOSE_MAX_TOKENS, so the
        // request must not pin the legacy 4096 default. See issue #9007.
        let model_config = test_model_config("some-unknown-local-model");
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();
        assert!(
            !obj.contains_key("max_tokens"),
            "max_tokens should be omitted when model_config.max_tokens is None"
        );
        assert!(
            !obj.contains_key("max_completion_tokens"),
            "max_completion_tokens should be omitted when model_config.max_tokens is None"
        );
        Ok(())
    }

    #[test]
    fn test_request_params_preserve_reserved_fields() -> anyhow::Result<()> {
        let params = std::collections::HashMap::from([
            (
                "thinking".to_string(),
                json!({
                    "type": "enabled",
                    "clear_thinking": false
                }),
            ),
            ("stream".to_string(), json!(false)),
            (
                "stream_options".to_string(),
                json!({"include_usage": false}),
            ),
            ("model".to_string(), json!("wrong-model")),
            ("messages".to_string(), json!([])),
            ("max_tokens".to_string(), json!(1)),
            ("temperature".to_string(), json!(2.0)),
            ("provider_custom".to_string(), json!("allowed")),
            ("thinking_effort".to_string(), json!("high")),
            ("disable_prompt_cache".to_string(), json!(true)),
            ("preserve_thinking_context".to_string(), json!(true)),
        ]);
        let model_config = test_model_config("glm-4.7")
            .with_max_tokens(Some(4096))
            .with_merged_request_params(params);

        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            true,
        )?;

        assert_eq!(
            request["thinking"],
            json!({
                "type": "enabled",
                "clear_thinking": false
            })
        );
        assert_eq!(request["stream"], true);
        assert_eq!(request["stream_options"], json!({"include_usage": true}));
        assert_eq!(request["model"], "glm-4.7");
        assert_eq!(request["messages"][0]["role"], "system");
        assert_eq!(request["max_tokens"], 1);
        assert_eq!(request["temperature"], 2.0);
        assert_eq!(request["provider_custom"], "allowed");
        assert!(request.get("thinking_effort").is_none());
        assert!(request.get("disable_prompt_cache").is_none());
        assert!(request.get("preserve_thinking_context").is_none());

        Ok(())
    }

    #[test]
    fn test_create_request_o1_default() -> anyhow::Result<()> {
        let model_config = test_model_config("o1").with_max_tokens(Some(1024));
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();
        let expected = json!({
            "model": "o1",
            "messages": [
                {
                    "role": "developer",
                    "content": "system"
                }
            ],
            "max_completion_tokens": 1024
        });

        for (key, value) in expected.as_object().unwrap() {
            assert_eq!(obj.get(key).unwrap(), value);
        }
        assert!(
            obj.get("reasoning_effort").is_none(),
            "reasoning_effort should be omitted when no explicit suffix is provided"
        );

        Ok(())
    }

    #[test]
    fn test_create_request_o1_medium_effort() -> anyhow::Result<()> {
        let model_config = test_model_config("o1")
            .with_max_tokens(Some(1024))
            .with_thinking_effort(ThinkingEffort::Medium);
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();

        assert_eq!(obj.get("reasoning_effort"), Some(&json!("medium")));
        assert!(obj.get("thinking_effort").is_none());

        Ok(())
    }

    #[test]
    fn test_create_request_gpt56_max_effort_uses_max() -> anyhow::Result<()> {
        let model_config = test_model_config("gpt-5.6-luna")
            .with_max_tokens(Some(1024))
            .with_thinking_effort(ThinkingEffort::Max);
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();

        assert_eq!(obj.get("reasoning_effort"), Some(&json!("max")));
        assert!(obj.get("thinking_effort").is_none());

        Ok(())
    }

    #[test]
    fn test_openai_reasoning_effort_gpt6_sol_and_luna() {
        for model in ["gpt-6-sol", "gpt-6-luna"] {
            assert_eq!(
                openai_reasoning_effort_for_thinking(model, ThinkingEffort::Off),
                Some("none".to_string())
            );
            assert_eq!(
                openai_reasoning_effort_for_thinking(model, ThinkingEffort::Max),
                Some("max".to_string())
            );
            assert_eq!(
                crate::canonical::maybe_get_canonical_model("openai", model)
                    .unwrap()
                    .reasoning_efforts
                    .unwrap(),
                ["none", "low", "medium", "high", "xhigh", "max"]
            );
            assert!(
                is_openai_responses_model(model),
                "{model} uses /v1/responses"
            );
        }

        assert_eq!(
            openai_reasoning_effort_for_thinking("gpt-6-astra", ThinkingEffort::Max),
            Some("max".to_string())
        );
        assert_eq!(
            openai_reasoning_effort_for_thinking("gpt-5.6-sol", ThinkingEffort::Max),
            Some("max".to_string())
        );
    }

    #[test]
    fn test_openai_reasoning_effort_gpt6_does_not_support_none() {
        for model in [
            "gpt-6-astra",
            "data_workflow_tools.goose.goose-gpt-6-astra",
            "openrouter/openai/gpt-6-astra",
            "gpt-6.1-sol",
            "gpt-6.1-sol-high",
            "data_workflow_tools.goose.goose-gpt-6.1-sol",
            "openrouter/openai/gpt-6.1-sol",
            "gpt-6.1-sol@eu",
            "openai/gpt-6.1-sol-fast",
            "openai/gpt-6.1-sol-fast-high",
            "gpt-6-1-sol",
            "gpt-6-1-sol-high",
            "goose-gpt-6-1-sol",
            "catalog.schema.goose-gpt-6-1-sol",
            "openrouter/openai/gpt-6-1-sol",
            "gpt-6-1-sol@eu",
            "openai/gpt-6-1-sol-fast-high",
            "GOOSE-GPT-6-1-SOL",
        ] {
            assert_eq!(
                openai_reasoning_effort_for_thinking(model, ThinkingEffort::Off),
                Some("low".to_string()),
                "{model} Off should map to low, not none"
            );
            assert_eq!(
                openai_reasoning_effort_for_thinking(model, ThinkingEffort::Medium),
                Some("medium".to_string()),
                "{model} medium should remain medium"
            );
            assert!(
                !openai_reasoning_efforts_for_model(model).contains(&"none"),
                "{model} must not advertise none"
            );
        }

        assert_eq!(
            openai_reasoning_effort_for_thinking("gpt-5.6-luna", ThinkingEffort::Off),
            Some("none".to_string())
        );
        assert_eq!(
            openai_reasoning_effort_for_thinking("gpt-5.4", ThinkingEffort::Off),
            Some("none".to_string())
        );
        assert_eq!(
            openai_reasoning_effort_for_thinking("gpt-5", ThinkingEffort::Off),
            Some("minimal".to_string())
        );
    }

    #[test]
    fn test_gpt6_1_sol_effort_matching_respects_model_boundaries() {
        for model in [
            "gpt-6.1-solstice",
            "gpt-6-1-solstice",
            "gpt-6.10-sol",
            "gpt-6-10-sol",
            "notgpt-6-1-sol",
            "catalog.schema.notgpt-6-1-sol",
        ] {
            assert_eq!(
                openai_reasoning_effort_for_thinking(model, ThinkingEffort::Off),
                Some("low".to_string()),
                "{model} must use the unknown-model fallback"
            );
        }
    }

    #[test]
    fn test_create_request_gpt5_pro_max_effort_uses_supported_level() -> anyhow::Result<()> {
        let model_config = test_model_config("gpt-5.2-pro-2025-12-11")
            .with_max_tokens(Some(1024))
            .with_thinking_effort(ThinkingEffort::Max);
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();

        assert_eq!(obj.get("reasoning_effort"), Some(&json!("xhigh")));
        assert!(obj.get("thinking_effort").is_none());

        Ok(())
    }

    #[test]
    fn test_create_request_o3_custom_reasoning_effort() -> anyhow::Result<()> {
        let model_config = test_model_config("o3-mini")
            .with_max_tokens(Some(1024))
            .with_thinking_effort(ThinkingEffort::High);
        let request = create_request(
            &model_config,
            "system",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )?;
        let obj = request.as_object().unwrap();
        let expected = json!({
            "model": "o3-mini",
            "messages": [
                {
                    "role": "developer",
                    "content": "system"
                }
            ],
            "reasoning_effort": "high",
            "max_completion_tokens": 1024
        });

        for (key, value) in expected.as_object().unwrap() {
            assert_eq!(obj.get(key).unwrap(), value);
        }
        assert!(obj.get("thinking_effort").is_none());

        Ok(())
    }

    struct StreamingUsageTestResult {
        usage_count: usize,
        usage: Option<ProviderUsage>,
        tool_calls: Vec<String>,
        has_text_content: bool,
        text: String,
        output_token_limit_message_ids: Vec<Option<String>>,
    }

    async fn run_streaming_test(response_lines: &str) -> anyhow::Result<StreamingUsageTestResult> {
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let messages = response_to_streaming_message(response_stream);
        pin!(messages);

        let mut result = StreamingUsageTestResult {
            usage_count: 0,
            usage: None,
            tool_calls: Vec::new(),
            has_text_content: false,
            text: String::new(),
            output_token_limit_message_ids: Vec::new(),
        };

        while let Some(Ok((message, usage))) = messages.next().await {
            if let Some(u) = usage {
                result.usage_count += 1;
                result.usage = Some(u);
            }
            if let Some(msg) = message {
                if msg.metadata.output_token_limit_reached {
                    result.output_token_limit_message_ids.push(msg.id.clone());
                }
                for content in &msg.content {
                    match content {
                        MessageContentBlock::ToolRequest(req) => {
                            if let Ok(tool_call) = &req.tool_call {
                                result.tool_calls.push(tool_call.name.to_string());
                            }
                        }
                        MessageContentBlock::Text(text) if !text.text.is_empty() => {
                            result.has_text_content = true;
                            result.text.push_str(&text.text);
                        }
                        _ => {}
                    }
                }
            }
        }

        Ok(result)
    }

    fn assert_usage_yielded_once(
        result: &StreamingUsageTestResult,
        expected_input: i32,
        expected_output: i32,
        expected_total: i32,
    ) {
        assert_eq!(
            result.usage_count, 1,
            "Usage should be yielded exactly once, but was yielded {} times",
            result.usage_count
        );

        let usage = result.usage.as_ref().expect("Expected usage to be present");
        assert_eq!(usage.usage.input_tokens, Some(expected_input));
        assert_eq!(usage.usage.output_tokens, Some(expected_output));
        assert_eq!(usage.usage.total_tokens, Some(expected_total));
    }

    async fn collect_streamed_tool_requests(
        response_lines: &str,
    ) -> Vec<crate::conversation::message::ToolRequest> {
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let messages = response_to_streaming_message(response_stream);
        pin!(messages);

        let mut requests = Vec::new();
        while let Some(Ok((message, _usage))) = messages.next().await {
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(req) = content {
                        requests.push(req.clone());
                    }
                }
            }
        }
        requests
    }

    #[tokio::test]
    async fn test_streaming_reassembles_interleaved_parallel_tool_calls() {
        let response_lines = concat!(
            r#"data: {"id":"chatcmpl-par","model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"id":"call_a","type":"function","function":{"name":"search","arguments":""}},{"index":1,"id":"call_b","type":"function","function":{"name":"write","arguments":""}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"id":"chatcmpl-par","model":"test-model","choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"function":{"arguments":"{\"path\":"}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"id":"chatcmpl-par","model":"test-model","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"query\":"}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"id":"chatcmpl-par","model":"test-model","choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"function":{"arguments":"\"/tmp/a.md\"}"}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"id":"chatcmpl-par","model":"test-model","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"rust\"}"}}]},"finish_reason":"tool_calls"}]}"#,
            "\n",
            "data: [DONE]",
        );

        let requests = collect_streamed_tool_requests(response_lines).await;

        assert_eq!(requests.len(), 2);
        assert_eq!(
            requests
                .iter()
                .map(|r| r.provider_index())
                .collect::<Vec<_>>(),
            vec![Some(0), Some(1)]
        );

        let search = requests[0].tool_call.as_ref().expect("search args parsed");
        assert_eq!(search.name, "search");
        assert_eq!(search.arguments.as_ref().unwrap()["query"], json!("rust"));

        let write = requests[1].tool_call.as_ref().expect("write args parsed");
        assert_eq!(write.name, "write");
        assert_eq!(
            write.arguments.as_ref().unwrap()["path"],
            json!("/tmp/a.md")
        );
    }

    #[tokio::test]
    async fn test_streaming_marks_length_on_empty_terminal_chunk() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"chatcmpl-limit","model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","content":"Partial answer"},"finish_reason":null}]}
data: {"id":"chatcmpl-limit","model":"test-model","choices":[{"index":0,"delta":{},"finish_reason":"length"}],"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15}}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert_eq!(result.text, "Partial answer");
        assert_eq!(
            result.output_token_limit_message_ids,
            vec![Some("chatcmpl-limit".to_string())]
        );
        assert_usage_yielded_once(&result, 10, 5, 15);

        Ok(())
    }

    #[test]
    fn test_get_usage_preserves_provider_totals_with_cache_fields() {
        let usage = get_usage(&json!({
            "prompt_tokens": 120,
            "completion_tokens": 30,
            "total_tokens": 150,
            "cache_read_input_tokens": 80,
            "cache_creation_input_tokens": 20
        }));

        assert_eq!(usage.input_tokens, Some(120));
        assert_eq!(usage.output_tokens, Some(30));
        assert_eq!(usage.total_tokens, Some(150));
        assert_eq!(usage.cache_read_input_tokens, Some(80));
        assert_eq!(usage.cache_write_input_tokens, Some(20));
    }

    #[test]
    fn test_get_usage_reads_openai_prompt_tokens_details() {
        let usage = get_usage(&json!({
            "prompt_tokens": 120,
            "completion_tokens": 30,
            "total_tokens": 150,
            "prompt_tokens_details": {
                "cached_tokens": 80
            }
        }));

        assert_eq!(usage.input_tokens, Some(120));
        assert_eq!(usage.output_tokens, Some(30));
        assert_eq!(usage.total_tokens, Some(150));
        assert_eq!(usage.cache_read_input_tokens, Some(80));
        assert_eq!(usage.cache_write_input_tokens, None);
    }

    #[test]
    fn test_get_usage_reads_nested_usage_with_cache_fields() {
        let usage = get_usage(&json!({
            "id": "chatcmpl_test",
            "object": "chat.completion",
            "usage": {
                "prompt_tokens": 84,
                "completion_tokens": 21,
                "total_tokens": 105,
                "prompt_tokens_details": {
                    "cached_tokens": 60
                },
                "cache_creation_input_tokens": 10
            }
        }));

        assert_eq!(usage.input_tokens, Some(84));
        assert_eq!(usage.output_tokens, Some(21));
        assert_eq!(usage.total_tokens, Some(105));
        assert_eq!(usage.cache_read_input_tokens, Some(60));
        assert_eq!(usage.cache_write_input_tokens, Some(10));
    }

    #[tokio::test]
    async fn test_streamed_multi_tool_response_to_messages() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":"I'll run both"},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288340}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":" `ls` commands in a"},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288340}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":" single turn for you -"},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288340}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":" one on the current directory an"},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288340}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":"d one on the `working_dir`."},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288340}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":1,"id":"toolu_bdrk_01RMTd7R9DzQjEEWgDwzcBsU","type":"function","function":{"name":"developer__shell","arguments":""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":1,"function":{"arguments":""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":1,"function":{"arguments":"{\""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":1,"function":{"arguments":"command\": \"l"}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":1,"function":{"arguments":"s\"}"}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"id":"toolu_bdrk_016bgVTGZdpjP8ehjMWp9cWW","type":"function","function":{"name":"developer__shell","arguments":""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288341}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":"{\""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":"command\""}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":": \"ls wor"}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":"king_dir"}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":2,"function":{"arguments":"\"}"}}]},"index":0,"finish_reason":null}],"usage":{"prompt_tokens":4982,"completion_tokens":null,"total_tokens":null},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: {"model":"us.anthropic.claude-sonnet-4-20250514-v1:0","choices":[{"delta":{"role":"assistant","content":""},"index":0,"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":4982,"completion_tokens":122,"total_tokens":5104},"object":"chat.completion.chunk","id":"msg_bdrk_014pifLTHsNZz6Lmtw1ywgDJ","created":1753288342}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;
        assert_eq!(
            result.tool_calls.len(),
            2,
            "Expected 2 tool calls, got {}",
            result.tool_calls.len()
        );
        assert!(result
            .tool_calls
            .iter()
            .all(|name| name == "developer__shell"));

        assert_usage_yielded_once(&result, 4982, 122, 5104);

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_empty_finish_reason_is_not_terminal() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"model":"m","choices":[{"delta":{"role":"assistant","content":"Checking."},"index":0,"finish_reason":""}],"object":"chat.completion.chunk","id":"1","created":1753288340}
data: {"model":"m","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"developer__shell","arguments":""}}]},"index":0,"finish_reason":""}],"object":"chat.completion.chunk","id":"1","created":1753288340}
data: {"model":"m","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"function":{"arguments":"{\"command\""}}]},"index":0,"finish_reason":""}],"object":"chat.completion.chunk","id":"1","created":1753288340}
data: {"model":"m","choices":[{"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"function":{"arguments":": \"ls\"}"}}]},"index":0,"finish_reason":""}],"object":"chat.completion.chunk","id":"1","created":1753288340}
data: {"model":"m","choices":[{"delta":{"role":"assistant","content":""},"index":0,"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":100,"completion_tokens":20,"total_tokens":120},"object":"chat.completion.chunk","id":"1","created":1753288340}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert!(result.has_text_content, "Expected text content in response");
        assert_eq!(
            result.tool_calls,
            vec!["developer__shell"],
            "tool call must survive intermediate empty-string finish_reason"
        );
        assert_usage_yielded_once(&result, 100, 20, 120);

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_metadata_without_usage() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"chatcmpl-no-usage","model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-no-usage","model":"test-model","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert_eq!(result.usage_count, 1);
        let usage = result.usage.unwrap();
        assert_eq!(usage.model, "test-model");
        assert_eq!(usage.usage, Usage::default());
        assert_eq!(usage.finish_reasons, Some(vec!["stop".to_string()]));
        assert_eq!(usage.response_id.as_deref(), Some("chatcmpl-no-usage"));

        Ok(())
    }

    #[tokio::test]
    async fn test_openrouter_streaming_usage_yielded_once() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning":null,"reasoning_details":[]},"finish_reason":null,"native_finish_reason":null,"logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":"There","reasoning":"","reasoning_details":[]},"finish_reason":null,"native_finish_reason":null,"logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":" are","reasoning":null,"reasoning_details":[]},"finish_reason":null,"native_finish_reason":null,"logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":" **47**","reasoning":null,"reasoning_details":[]},"finish_reason":null,"native_finish_reason":null,"logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":" files.","reasoning":null,"reasoning_details":[]},"finish_reason":null,"native_finish_reason":null,"logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning":null,"reasoning_details":[]},"finish_reason":"stop","native_finish_reason":"stop","logprobs":null}]}
data: {"id":"gen-1768896871-9HgAQqS1Z72C6gApaidi","provider":"OpenInference","model":"openai/gpt-oss-120b:free","object":"chat.completion.chunk","created":1768896871,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null,"native_finish_reason":null,"logprobs":null}],"usage":{"prompt_tokens":7007,"completion_tokens":49,"total_tokens":7056}}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert!(result.has_text_content, "Expected text content in response");
        assert_usage_yielded_once(&result, 7007, 49, 7056);
        let usage = result.usage.as_ref().unwrap();
        assert_eq!(
            usage.finish_reasons.as_deref(),
            Some(&["stop".to_string()][..])
        );
        assert_eq!(
            usage.response_id.as_deref(),
            Some("gen-1768896871-9HgAQqS1Z72C6gApaidi")
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_openai_gpt5_streaming_usage_yielded_once() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"chatcmpl-Bk9Ye6Y0t9E7bC3DOMxCpW8eJkTKU","object":"chat.completion.chunk","created":1737368310,"model":"gpt-5.2-1106-preview","service_tier":"default","system_fingerprint":"fp_5f325d54e6","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_x4CIvBVfQhYMhyO0T1VEddua","type":"function","function":{"name":"developer__shell","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-Bk9Ye6Y0t9E7bC3DOMxCpW8eJkTKU","object":"chat.completion.chunk","created":1737368310,"model":"gpt-5.2-1106-preview","service_tier":"default","system_fingerprint":"fp_5f325d54e6","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"command\":\"ls ~/Desktop | wc -l\"}"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-Bk9Ye6Y0t9E7bC3DOMxCpW8eJkTKU","object":"chat.completion.chunk","created":1737368310,"model":"gpt-5.2-1106-preview","service_tier":"default","system_fingerprint":"fp_5f325d54e6","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}],"usage":null}
data: {"id":"chatcmpl-Bk9Ye6Y0t9E7bC3DOMxCpW8eJkTKU","object":"chat.completion.chunk","created":1737368310,"model":"gpt-5.2-1106-preview","service_tier":"default","system_fingerprint":"fp_5f325d54e6","choices":[],"usage":{"prompt_tokens":8320,"completion_tokens":172,"total_tokens":8492}}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert_eq!(result.tool_calls.len(), 1, "Expected 1 tool call");
        assert_eq!(result.tool_calls[0], "developer__shell");
        assert_usage_yielded_once(&result, 8320, 172, 8492);
        assert_eq!(
            result.usage.as_ref().map(|usage| usage.model.as_str()),
            Some("gpt-5.2-1106-preview")
        );
        let usage = result.usage.as_ref().unwrap();
        assert_eq!(
            usage.finish_reasons.as_deref(),
            Some(&["tool_calls".to_string()][..])
        );
        assert_eq!(
            usage.response_id.as_deref(),
            Some("chatcmpl-Bk9Ye6Y0t9E7bC3DOMxCpW8eJkTKU")
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_tetrate_claude_streaming_usage_yielded_once() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"msg_01BbvMfNhbdm2hmmTbWjaeYt","choices":[{"index":0,"delta":{"role":"assistant"}}],"created":1768898776,"model":"claude-sonnet-4-5-20250929","object":"chat.completion.chunk"}
data: {"id":"msg_01BbvMfNhbdm2hmmTbWjaeYt","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"toolu_011Yj5pGczhs1597iLXp5XJK","type":"function","function":{"name":"developer__shell","arguments":""}}]}}],"created":1768898776,"model":"claude-sonnet-4-5-20250929","object":"chat.completion.chunk"}
data: {"id":"msg_01BbvMfNhbdm2hmmTbWjaeYt","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"type":"function","function":{"arguments":"{\"command\": \"find ~/Desktop -type f | wc -l\"}"}}]}}],"created":1768898776,"model":"claude-sonnet-4-5-20250929","object":"chat.completion.chunk"}
data: {"id":"msg_01BbvMfNhbdm2hmmTbWjaeYt","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}],"created":1768898776,"model":"claude-sonnet-4-5-20250929","object":"chat.completion.chunk","usage":{"completion_tokens":79,"prompt_tokens":12376,"total_tokens":12455}}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert_eq!(result.tool_calls.len(), 1, "Expected 1 tool call");
        assert_eq!(result.tool_calls[0], "developer__shell");
        assert_usage_yielded_once(&result, 12376, 79, 12455);

        Ok(())
    }

    #[tokio::test]
    async fn test_azure_annotation_chunk_without_delta_does_not_fail() -> anyhow::Result<()> {
        let response_lines = r#"
data: {"id":"chatcmpl-test","object":"chat.completion.chunk","created":1234567890,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}],"usage":null}
data: {"choices":[{"content_filter_offsets":{"check_offset":5,"start_offset":5,"end_offset":5},"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}
data: {"id":"chatcmpl-test","object":"chat.completion.chunk","created":1234567891,"model":"gpt-5.4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":1,"total_tokens":11}}
data: [DONE]
"#;

        let result = run_streaming_test(response_lines).await?;

        assert!(result.has_text_content, "Expected text content in response");
        assert_usage_yielded_once(&result, 10, 1, 11);

        Ok(())
    }

    #[test]
    fn test_response_to_message_with_nested_extra_content() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "tool_calls": [{
                        "id": "call_456",
                        "type": "function",
                        "function": {
                            "name": "test_tool",
                            "arguments": "{}"
                        },
                        "extra_content": {
                            "google": {
                                "thought_signature": "nested_sig_xyz789"
                            }
                        }
                    }]
                }
            }]
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 1);

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            assert!(request.tool_call.is_ok());
            assert!(request.metadata.is_some());
            let metadata = request.metadata.as_ref().unwrap();
            let extra_content = metadata.get("extra_content").unwrap();
            assert_eq!(
                extra_content["google"]["thought_signature"],
                "nested_sig_xyz789"
            );
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_with_multiple_extra_fields() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "tool_calls": [{
                        "id": "call_789",
                        "type": "function",
                        "function": {
                            "name": "test_tool",
                            "arguments": "{}"
                        },
                        "thoughtSignature": "sig_top_level",
                        "extra_content": {
                            "google": {
                                "thought_signature": "sig_nested"
                            }
                        },
                        "custom_field": "custom_value"
                    }]
                }
            }]
        });

        let message = response_to_message(&response)?;

        if let MessageContentBlock::ToolRequest(request) = &message.content[0] {
            let metadata = request.metadata.as_ref().unwrap();
            assert_eq!(metadata.get("thoughtSignature").unwrap(), "sig_top_level");
            assert_eq!(
                metadata.get("extra_content").unwrap()["google"]["thought_signature"],
                "sig_nested"
            );
            assert_eq!(metadata.get("custom_field").unwrap(), "custom_value");
        } else {
            panic!("Expected ToolRequest content");
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_response_with_nested_extra_content() -> anyhow::Result<()> {
        let response_lines = r#"data: {"model":"test-model","choices":[{"delta":{"role":"assistant","tool_calls":[{"extra_content":{"google":{"thought_signature":"nested_stream_sig"}},"id":"call_nested","function":{"name":"test_tool","arguments":"{}"},"type":"function","index":0}]},"index":0,"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":100,"completion_tokens":10,"total_tokens":110},"object":"chat.completion.chunk","id":"test-id","created":1234567890}
data: [DONE]"#;

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let messages = response_to_streaming_message(response_stream);
        pin!(messages);

        while let Some(Ok((message, _usage))) = messages.next().await {
            if let Some(msg) = message {
                if let MessageContentBlock::ToolRequest(request) = &msg.content[0] {
                    assert!(request.tool_call.is_ok());
                    assert!(request.metadata.is_some());
                    let metadata = request.metadata.as_ref().unwrap();
                    let extra_content = metadata.get("extra_content").unwrap();
                    assert_eq!(
                        extra_content["google"]["thought_signature"],
                        "nested_stream_sig"
                    );
                    return Ok(());
                }
            }
        }

        panic!("Expected tool call message with nested extra_content metadata");
    }

    #[tokio::test]
    async fn test_streaming_non_object_arguments_does_not_panic() -> anyhow::Result<()> {
        // Streamed tool call whose arguments are valid JSON but NOT an object.
        // Must yield an INVALID_PARAMS tool error, not panic via rmcp `object()`.
        let response_lines = r#"data: {"model":"test-model","choices":[{"delta":{"role":"assistant","tool_calls":[{"id":"call_bad","function":{"name":"test_tool","arguments":"[1, "},"type":"function","index":0}]},"index":0,"finish_reason":null}],"object":"chat.completion.chunk","id":"test-id","created":1234567890}
data: {"model":"test-model","choices":[{"delta":{"tool_calls":[{"function":{"arguments":"2, 3]"},"index":0}]},"index":0,"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":100,"completion_tokens":10,"total_tokens":110},"object":"chat.completion.chunk","id":"test-id","created":1234567890}
data: [DONE]"#;

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let messages = response_to_streaming_message(response_stream);
        pin!(messages);

        let mut found_tool_error = false;
        let mut usage_count = 0;
        while let Some(result) = messages.next().await {
            let (message, usage) = result?;
            if usage.is_some() {
                usage_count += 1;
            }
            if let Some(msg) = message {
                if let MessageContentBlock::ToolRequest(request) = &msg.content[0] {
                    assert!(!msg.metadata.output_token_limit_reached);
                    assert_eq!(msg.id.as_deref(), Some("test-id"));
                    match &request.tool_call {
                        Err(ErrorData {
                            code: ErrorCode::INVALID_PARAMS,
                            message: m,
                            ..
                        }) => {
                            assert!(m.contains("must be a JSON object"));
                            assert!(
                                m.contains("test_tool"),
                                "error must name the original tool so the model can retry it: {m}"
                            );
                            found_tool_error = true;
                        }
                        _ => panic!("expected INVALID_PARAMS for non-object streamed args"),
                    }
                }
            }
        }

        assert!(found_tool_error, "expected a tool request message");
        assert_eq!(usage_count, 1);
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_length_rejects_tool_calls_regardless_of_arguments() -> anyhow::Result<()>
    {
        let response_lines = r#"data: {"model":"test-model","choices":[{"delta":{"role":"assistant","tool_calls":[{"id":"call_empty","function":{"name":"empty_tool","arguments":""},"type":"function","index":0},{"id":"call_valid","function":{"name":"valid_tool","arguments":"{\"value\":"},"type":"function","index":1}]},"index":0,"finish_reason":null}],"object":"chat.completion.chunk","id":"test-id","created":1234567890}
data: {"model":"test-model","choices":[{"delta":{"tool_calls":[{"function":{"arguments":""},"index":0},{"function":{"arguments":"true}"},"index":1}]},"index":0,"finish_reason":"length"}],"usage":{"prompt_tokens":100,"completion_tokens":10,"total_tokens":110},"object":"chat.completion.chunk","id":"test-id","created":1234567890}
data: [DONE]"#;

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let messages = response_to_streaming_message(response_stream);
        pin!(messages);

        let mut tool_error_ids = Vec::new();
        let mut usage_count = 0;
        while let Some(result) = messages.next().await {
            let (message, usage) = result?;
            if usage.is_some() {
                usage_count += 1;
            }
            if let Some(msg) = message {
                assert!(msg.metadata.output_token_limit_reached);
                assert_eq!(msg.id.as_deref(), Some("test-id"));
                for content in msg.content {
                    if let MessageContentBlock::ToolRequest(request) = content {
                        let request_id = request.id.clone();
                        match request.tool_call {
                            Err(error) => {
                                assert_eq!(error.code, ErrorCode::INVALID_PARAMS);
                                assert!(error.message.contains("output token limit"));
                                tool_error_ids.push(request_id);
                            }
                            Ok(_) => panic!("length-terminated tool call must not be executable"),
                        }
                    }
                }
            }
        }

        assert_eq!(tool_error_ids, vec!["call_empty", "call_valid"]);
        assert_eq!(usage_count, 1);
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_response_extracts_inline_think_blocks() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{\"content\":\"<thi\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{\"content\":\"nk>x</thi\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{\"content\":\"nk>y\"},\"index\":0,\"finish_reason\":\"stop\"}]}\n",
            "data: [DONE]\n"
        );

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut text = String::new();
        let mut thinking = String::new();

        while let Some(result) = messages.next().await {
            let (message, _) = result?;
            if let Some(message) = message {
                for item in message.content {
                    match item {
                        MessageContentBlock::Text(text_content) => {
                            text.push_str(&text_content.text)
                        }
                        MessageContentBlock::Thinking(thinking_content) => {
                            thinking.push_str(&thinking_content.thinking)
                        }
                        _ => {}
                    }
                }
            }
        }

        assert_eq!(text, "y");
        assert_eq!(thinking, "x");

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_truncated_inline_think_preserves_output_token_limit(
    ) -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"chunk-1\",\"model\":\"test-model\",\"choices\":[{\"delta\":{\"content\":\"<think>unfinished reasoning\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"length\"}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}\n",
            "data: [DONE]\n"
        );

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));
        let mut streamed_messages = Vec::new();
        let mut usage_count = 0;

        while let Some(result) = messages.next().await {
            let (message, usage) = result?;
            if usage.is_some() {
                usage_count += 1;
            }
            if let Some(message) = message {
                streamed_messages.push(message);
            }
        }

        assert_eq!(usage_count, 1);
        assert_eq!(
            streamed_messages
                .iter()
                .filter(|message| message.metadata.output_token_limit_reached)
                .count(),
            1
        );
        let trailing_message = streamed_messages
            .last()
            .expect("expected trailing thinking");
        assert!(trailing_message.metadata.output_token_limit_reached);
        assert!(matches!(
            trailing_message.content.as_slice(),
            [MessageContentBlock::Thinking(thinking)]
                if thinking.thinking == "unfinished reasoning"
        ));

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_partial_think_tag_emits_one_output_limit_marker() -> anyhow::Result<()>
    {
        let response_lines = concat!(
            "data: {\"id\":\"chunk-1\",\"model\":\"test-model\",\"choices\":[{\"delta\":{\"content\":\"<thi\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"length\"}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}\n",
            "data: [DONE]\n"
        );

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));
        let mut streamed_messages = Vec::new();
        let mut usage_count = 0;

        while let Some(result) = messages.next().await {
            let (message, usage) = result?;
            usage_count += usize::from(usage.is_some());
            if let Some(message) = message {
                streamed_messages.push(message);
            }
        }

        assert_eq!(usage_count, 1);
        let marked_messages: Vec<_> = streamed_messages
            .iter()
            .filter(|message| message.metadata.output_token_limit_reached)
            .collect();
        assert_eq!(marked_messages.len(), 1);
        assert_eq!(marked_messages[0].id.as_deref(), Some("chunk-1"));
        assert_eq!(marked_messages[0].as_concat_text(), "<thi");

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_suppresses_inline_think_when_structured_reasoning_follows(
    ) -> anyhow::Result<()> {
        // Inline <think>...</think> arrives in an early content chunk, then
        // reasoning_content arrives in a later chunk. The inline thinking
        // should be discarded in favor of the structured reasoning so users
        // do not get duplicated reasoning output.
        let response_lines = concat!(
            "data: {\"id\":\"chunk-1\",\"choices\":[{\"delta\":{\"content\":\"<think>inline reasoning</think>Hi\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-2\",\"choices\":[{\"delta\":{\"reasoning_content\":\"structured reasoning\"},\"index\":0,\"finish_reason\":null}]}\n",
            "data: {\"id\":\"chunk-3\",\"choices\":[{\"delta\":{\"content\":\" there\"},\"index\":0,\"finish_reason\":\"stop\"}]}\n",
            "data: [DONE]\n"
        );

        let response_stream =
            tokio_stream::iter(response_lines.lines().map(|line| Ok(line.to_string())));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut text = String::new();
        let mut thinking = String::new();

        while let Some(result) = messages.next().await {
            let (message, _) = result?;
            if let Some(message) = message {
                for item in message.content {
                    match item {
                        MessageContentBlock::Text(text_content) => {
                            text.push_str(&text_content.text)
                        }
                        MessageContentBlock::Thinking(thinking_content) => {
                            thinking.push_str(&thinking_content.thinking)
                        }
                        _ => {}
                    }
                }
            }
        }

        assert_eq!(text, "Hi there");
        assert_eq!(thinking, "structured reasoning");

        Ok(())
    }

    #[test]
    fn test_response_to_message_with_reasoning_content() -> anyhow::Result<()> {
        // Test capturing reasoning_content from DeepSeek reasoning models
        let response = json!({
            "choices": [{
                "role": "assistant",
                "message": {
                    "reasoning_content": "Let me think about this step by step...",
                    "content": "The answer is 9.11 is greater than 9.8"
                }
            }],
            "usage": {
                "input_tokens": 10,
                "output_tokens": 25,
                "total_tokens": 35
            }
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 2);

        // First should be thinking content (reasoning is mapped to thinking)
        if let MessageContentBlock::Thinking(thinking) = &message.content[0] {
            assert_eq!(thinking.thinking, "Let me think about this step by step...");
        } else {
            panic!("Expected Thinking content, got {:?}", message.content[0]);
        }

        // Second should be text content
        if let MessageContentBlock::Text(text) = &message.content[1] {
            assert_eq!(text.text, "The answer is 9.11 is greater than 9.8");
        } else {
            panic!("Expected Text content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_extracts_inline_think_blocks() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "role": "assistant",
                "message": {
                    "content": "<think>internal reasoning</think>Visible answer"
                }
            }]
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 2);

        if let MessageContentBlock::Thinking(thinking) = &message.content[0] {
            assert_eq!(thinking.thinking, "internal reasoning");
        } else {
            panic!("Expected Thinking content, got {:?}", message.content[0]);
        }

        if let MessageContentBlock::Text(text) = &message.content[1] {
            assert_eq!(text.text, "Visible answer");
        } else {
            panic!("Expected Text content");
        }

        Ok(())
    }

    #[test]
    fn test_response_to_message_prefers_structured_reasoning_over_inline_think(
    ) -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "role": "assistant",
                "message": {
                    "reasoning_content": "structured reasoning",
                    "content": "<think>inline reasoning</think>Visible answer"
                }
            }]
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 2);

        if let MessageContentBlock::Thinking(thinking) = &message.content[0] {
            assert_eq!(thinking.thinking, "structured reasoning");
        } else {
            panic!("Expected Thinking content");
        }

        if let MessageContentBlock::Text(text) = &message.content[1] {
            assert_eq!(text.text, "Visible answer");
        } else {
            panic!("Expected Text content");
        }

        Ok(())
    }

    #[test]
    fn test_format_messages_with_reasoning_content() -> anyhow::Result<()> {
        // Test that reasoning_content is properly included in formatted messages
        let mut message = Message::assistant()
            .with_content(MessageContentBlock::thinking(
                "Thinking through the problem...",
                "",
            ))
            .with_text("The result is 42");

        // Add a tool call to test that reasoning_content works with tool calls
        message = message.with_tool_request(
            "tool1",
            Ok(rmcp::model::CallToolRequestParams::new("test_tool")
                .with_arguments(rmcp::object!({"param": "value"}))),
        );

        let spec = format_messages_with_options(
            &[message],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");

        // Should have reasoning_content field
        assert!(spec[0].get("reasoning_content").is_some());
        assert_eq!(
            spec[0]["reasoning_content"],
            "Thinking through the problem..."
        );

        // Should have content
        assert_eq!(spec[0]["content"], "The result is 42");

        // Should have tool_calls
        assert!(spec[0]["tool_calls"].is_array());
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[test]
    fn test_format_messages_preserves_reasoning_content_for_legacy_compat() -> anyhow::Result<()> {
        let message = Message::assistant()
            .with_content(MessageContentBlock::thinking(
                "Thinking through the problem...",
                "",
            ))
            .with_text("The result is 42");

        let spec = format_messages(&[message], &ImageFormat::OpenAi);

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["content"], "The result is 42");
        assert_eq!(
            spec[0]["reasoning_content"],
            "Thinking through the problem..."
        );

        Ok(())
    }

    #[test]
    fn test_format_messages_with_options_can_omit_reasoning_content() -> anyhow::Result<()> {
        let message = Message::assistant()
            .with_content(MessageContentBlock::thinking(
                "Thinking through the problem...",
                "",
            ))
            .with_text("The result is 42");

        let spec = format_messages_with_options(
            &[message],
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: false,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["content"], "The result is 42");
        assert!(spec[0].get("reasoning_content").is_none());

        Ok(())
    }

    #[test]
    fn test_create_request_preserves_reasoning_content_for_legacy_compat() -> anyhow::Result<()> {
        let model_config = test_model_config("deepseek-reasoner").with_max_tokens(Some(1024));
        let message = Message::assistant()
            .with_content(MessageContentBlock::thinking("preserve this", ""))
            .with_tool_request(
                "tool1",
                Ok(rmcp::model::CallToolRequestParams::new("test_tool")
                    .with_arguments(rmcp::object!({}))),
            );

        let request = create_request(
            &model_config,
            "system",
            &[message],
            &[],
            &ImageFormat::OpenAi,
            true,
        )?;

        assert_eq!(request["messages"][1]["reasoning_content"], "preserve this");

        Ok(())
    }

    #[test]
    fn test_format_messages_carries_thinking_only_chunks_to_tool_call() -> anyhow::Result<()> {
        let messages = vec![
            Message::assistant().with_content(MessageContentBlock::thinking("think ", "")),
            Message::assistant().with_content(MessageContentBlock::thinking("once", "")),
            Message::assistant().with_tool_request(
                "tool1",
                Ok(CallToolRequestParams::new("test_tool")
                    .with_arguments(object!({"param": "value"}))),
            ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["role"], "assistant");
        assert_eq!(spec[0]["reasoning_content"], "think once");
        assert_eq!(spec[0]["content"], json!(null));
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[test]
    fn test_format_messages_does_not_duplicate_pending_thinking() -> anyhow::Result<()> {
        let messages = vec![
            Message::assistant().with_content(MessageContentBlock::thinking("think once", "")),
            Message::assistant()
                .with_content(MessageContentBlock::thinking("think once", ""))
                .with_tool_request(
                    "tool1",
                    Ok(CallToolRequestParams::new("test_tool")
                        .with_arguments(object!({"param": "value"}))),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["reasoning_content"], "think once");
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[test]
    fn test_format_messages_merges_pending_thinking_with_tool_call_suffix() -> anyhow::Result<()> {
        let messages = vec![
            Message::assistant().with_content(MessageContentBlock::thinking("think ", "")),
            Message::assistant()
                .with_content(MessageContentBlock::thinking("once", ""))
                .with_tool_request(
                    "tool1",
                    Ok(CallToolRequestParams::new("test_tool")
                        .with_arguments(object!({"param": "value"}))),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["reasoning_content"], "think once");
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[test]
    fn test_format_messages_does_not_carry_thinking_across_user_message() -> anyhow::Result<()> {
        let messages = vec![
            Message::assistant().with_content(MessageContentBlock::thinking("stale", "")),
            Message::user().with_text("new turn"),
            Message::assistant()
                .with_tool_request("tool1", Ok(CallToolRequestParams::new("test_tool"))),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        assert_eq!(spec.len(), 2);
        assert_eq!(spec[0]["role"], "user");
        assert_eq!(spec[1]["role"], "assistant");
        assert!(spec[1].get("reasoning_content").is_none());

        Ok(())
    }

    #[test]
    fn test_format_messages_carries_reasoning_through_text_only_chunks() -> anyhow::Result<()> {
        // Scenario B from the streaming bug: thinking arrives first, then multiple
        // text-only assistant messages, then a tool call with thinking re-attached
        // by agent.rs (via the earlier-chunk lookback).
        // Text-only messages set tool_call_turn_reasoning="" (line 453 else-branch),
        // but the TC's own Thinking content must repopulate it.
        let messages = vec![
            Message::assistant().with_content(MessageContentBlock::thinking("reason", "")),
            Message::assistant().with_text("partial answer"),
            Message::assistant().with_text("more text"),
            // agent.rs attaches the earlier thinking to the TC message
            Message::assistant()
                .with_content(MessageContentBlock::thinking("reason", ""))
                .with_tool_request(
                    "tool1",
                    Ok(CallToolRequestParams::new("test_tool").with_arguments(object!({}))),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        let tool_call_msgs: Vec<_> = spec
            .iter()
            .filter(|m| {
                m.get("tool_calls")
                    .and_then(|tc| tc.as_array())
                    .is_some_and(|a| !a.is_empty())
            })
            .collect();

        assert_eq!(tool_call_msgs.len(), 1);
        assert_eq!(
            tool_call_msgs[0]["reasoning_content"], "reason",
            "reasoning_content must survive text-only chunks between thinking and tool call"
        );

        Ok(())
    }

    #[test]
    fn test_format_messages_carries_reasoning_to_all_split_tool_calls() -> anyhow::Result<()> {
        // Simulates DeepSeek/Kimi streaming: a thinking-only chunk arrives first,
        // then the agent splits two tool calls into separate messages, each with
        // the same reasoning attached (as agent.rs does via response_thinking).
        // The formatter must keep reasoning_content on both so that
        // merge_split_tool_call_messages can reunite them into one assistant message.
        let tool_result1 = Message::user().with_tool_response(
            "tool1",
            Ok(rmcp::model::CallToolResult::success(vec![
                rmcp::model::ContentBlock::text("result1"),
            ])),
        );
        let messages = vec![
            // Standalone thinking message (created by agent.rs alongside request_msgs)
            Message::assistant().with_content(MessageContentBlock::thinking("reasoning", "")),
            // Each request_msg has thinking explicitly attached (agent.rs behaviour)
            Message::assistant()
                .with_content(MessageContentBlock::thinking("reasoning", ""))
                .with_tool_request(
                    "tool1",
                    Ok(CallToolRequestParams::new("tool_a").with_arguments(object!({}))),
                ),
            tool_result1,
            Message::assistant()
                .with_content(MessageContentBlock::thinking("reasoning", ""))
                .with_tool_request(
                    "tool2",
                    Ok(CallToolRequestParams::new("tool_b").with_arguments(object!({}))),
                ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        // After merge: one assistant message with both tool calls
        let assistant_msgs: Vec<_> = spec
            .iter()
            .filter(|m| m.get("role") == Some(&json!("assistant")))
            .collect();
        assert_eq!(assistant_msgs.len(), 1);
        assert_eq!(assistant_msgs[0]["reasoning_content"], "reasoning");
        let tool_calls = assistant_msgs[0]["tool_calls"].as_array().unwrap();
        assert_eq!(tool_calls.len(), 2);

        Ok(())
    }

    #[test]
    fn test_sequential_tool_calls_not_merged() -> anyhow::Result<()> {
        // Verifies that two tool calls from *different* turns are never merged,
        // even when the second call carries no fresh reasoning (the previous
        // turn's reasoning must not leak into it).
        let tool_result1 = Message::user().with_tool_response(
            "tool1",
            Ok(rmcp::model::CallToolResult::success(vec![
                rmcp::model::ContentBlock::text("result1"),
            ])),
        );
        let messages = vec![
            // Turn 1: thinking then tool call
            Message::assistant().with_content(MessageContentBlock::thinking("turn1_reasoning", "")),
            Message::assistant().with_tool_request(
                "tool1",
                Ok(CallToolRequestParams::new("tool_a").with_arguments(object!({}))),
            ),
            tool_result1,
            // Turn 2: new tool call, no fresh thinking
            Message::assistant().with_tool_request(
                "tool2",
                Ok(CallToolRequestParams::new("tool_b").with_arguments(object!({}))),
            ),
        ];

        let spec = format_messages_with_options(
            &messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );

        let assistant_msgs: Vec<_> = spec
            .iter()
            .filter(|m| m.get("role") == Some(&json!("assistant")))
            .collect();

        // Must remain two separate assistant messages — not merged across turns.
        assert_eq!(
            assistant_msgs.len(),
            2,
            "sequential tool calls must not be merged"
        );

        // Turn 1 carries reasoning; turn 2 must not inherit it.
        assert_eq!(assistant_msgs[0]["reasoning_content"], "turn1_reasoning");
        assert!(
            assistant_msgs[1].get("reasoning_content").is_none()
                || assistant_msgs[1]["reasoning_content"].is_null(),
            "turn 2 must not inherit stale reasoning from turn 1"
        );

        // The tool result must appear between the two assistant messages.
        let tool_idx = spec
            .iter()
            .position(|m| m.get("role") == Some(&json!("tool")))
            .expect("tool result must be present");
        let asst1_idx = spec
            .iter()
            .position(|m| m.get("role") == Some(&json!("assistant")))
            .unwrap();
        let asst2_idx = spec
            .iter()
            .rposition(|m| m.get("role") == Some(&json!("assistant")))
            .unwrap();
        assert!(
            asst1_idx < tool_idx && tool_idx < asst2_idx,
            "tool result must sit between the two assistant messages"
        );

        Ok(())
    }

    #[test_case(
        "data: {\"error\":{\"message\":\"Internal server error\",\"type\":\"server_error\",\"code\":500}}\ndata: [DONE]",
        "Internal server error";
        "openai error format"
    )]
    #[test_case(
        "data: {\"object\":\"error\",\"message\":\"CUDA out of memory\",\"code\":500}\ndata: [DONE]",
        "CUDA out of memory";
        "vllm error format"
    )]
    #[test_case(
        "data: {\"error\":{\"message\":\"Rate limit exceeded\",\"type\":\"rate_limit_error\"}}",
        "Rate limit exceeded";
        "error as first chunk"
    )]
    #[tokio::test]
    async fn test_mid_stream_server_error(response_lines: &str, expected_msg: &str) {
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));
        let mut found_error = false;
        while let Some(result) = messages.next().await {
            if let Err(e) = result {
                let err_str = e.to_string();
                assert!(
                    err_str.contains(expected_msg),
                    "unexpected error text: {err_str}"
                );
                found_error = true;
                break;
            }
        }
        assert!(
            found_error,
            "expected an error but stream completed successfully"
        );
    }

    #[test]
    fn test_merge_split_tool_calls_with_reasoning() {
        let mut messages = vec![
            json!({"role": "assistant", "tool_calls": [{"id": "tc1", "type": "function", "function": {"name": "read", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc1", "content": "result1"}),
            json!({"role": "assistant", "tool_calls": [{"id": "tc2", "type": "function", "function": {"name": "write", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc2", "content": "result2"}),
        ];
        merge_split_tool_call_messages(&mut messages);

        assert_eq!(messages.len(), 3);
        assert_eq!(messages[0]["tool_calls"].as_array().unwrap().len(), 2);
        assert_eq!(messages[1]["role"], "tool");
        assert_eq!(messages[2]["role"], "tool");
    }

    #[test]
    fn test_no_merge_without_reasoning() {
        let mut messages = vec![
            json!({"role": "assistant", "tool_calls": [{"id": "tc1", "type": "function", "function": {"name": "read", "arguments": "{}"}}]}),
            json!({"role": "tool", "tool_call_id": "tc1", "content": "result1"}),
            json!({"role": "assistant", "tool_calls": [{"id": "tc2", "type": "function", "function": {"name": "write", "arguments": "{}"}}]}),
            json!({"role": "tool", "tool_call_id": "tc2", "content": "result2"}),
        ];
        merge_split_tool_call_messages(&mut messages);

        assert_eq!(messages.len(), 4);
        assert_eq!(messages[0]["tool_calls"].as_array().unwrap().len(), 1);
        assert_eq!(messages[2]["tool_calls"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn test_merge_split_tool_calls_with_image_gap() {
        let mut messages = vec![
            json!({"role": "assistant", "tool_calls": [{"id": "tc1", "type": "function", "function": {"name": "screenshot", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc1", "content": "This tool result included an image that is uploaded in the next message."}),
            json!({"role": "user", "content": [{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}}]}),
            json!({"role": "assistant", "tool_calls": [{"id": "tc2", "type": "function", "function": {"name": "click", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc2", "content": "clicked"}),
        ];
        merge_split_tool_call_messages(&mut messages);

        assert_eq!(messages.len(), 4);
        assert_eq!(messages[0]["tool_calls"].as_array().unwrap().len(), 2);
        assert_eq!(messages[0]["role"], "assistant");
        assert_eq!(messages[1]["role"], "tool");
        assert_eq!(messages[1]["tool_call_id"], "tc1");
        assert_eq!(messages[2]["role"], "user");
        assert_eq!(messages[3]["role"], "tool");
        assert_eq!(messages[3]["tool_call_id"], "tc2");
    }

    #[test]
    fn test_merge_does_not_skip_real_user_message() {
        let mut messages = vec![
            json!({"role": "assistant", "tool_calls": [{"id": "tc1", "type": "function", "function": {"name": "read", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc1", "content": "result1"}),
            json!({"role": "user", "content": "what happened?"}),
            json!({"role": "assistant", "tool_calls": [{"id": "tc2", "type": "function", "function": {"name": "write", "arguments": "{}"}}], "reasoning_content": "thinking..."}),
            json!({"role": "tool", "tool_call_id": "tc2", "content": "result2"}),
        ];
        merge_split_tool_call_messages(&mut messages);

        assert_eq!(messages.len(), 5);
        assert_eq!(messages[0]["tool_calls"].as_array().unwrap().len(), 1);
        assert_eq!(messages[2]["role"], "user");
        assert_eq!(messages[2]["content"], "what happened?");
        assert_eq!(messages[3]["tool_calls"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn test_get_usage_with_ollama_native_fields() {
        // Ollama-native fields should be picked up as fallback
        let usage = json!({
            "prompt_eval_count": 42,
            "eval_count": 128
        });
        let result = get_usage(&usage);
        assert_eq!(result.input_tokens, Some(42));
        assert_eq!(result.output_tokens, Some(128));
        assert_eq!(result.total_tokens, Some(170));
    }

    #[test]
    fn test_get_usage_prefers_openai_fields_over_ollama() {
        // Standard OpenAI fields should take precedence
        let usage = json!({
            "prompt_tokens": 10,
            "completion_tokens": 20,
            "prompt_eval_count": 42,
            "eval_count": 128
        });
        let result = get_usage(&usage);
        assert_eq!(result.input_tokens, Some(10));
        assert_eq!(result.output_tokens, Some(20));
        assert_eq!(result.total_tokens, Some(30));
    }

    #[test]
    fn test_get_usage_falls_back_when_openai_fields_are_null() {
        // When OpenAI fields exist but are null, should fall back to Ollama-native
        let usage = json!({
            "prompt_tokens": null,
            "completion_tokens": null,
            "prompt_eval_count": 42,
            "eval_count": 128
        });
        let result = get_usage(&usage);
        assert_eq!(result.input_tokens, Some(42));
        assert_eq!(result.output_tokens, Some(128));
        assert_eq!(result.total_tokens, Some(170));
    }

    // vLLM serving gpt-oss emits both `reasoning` and `reasoning_content`
    // in the same payload; the non-streaming path handles it fine today.
    #[test]
    fn test_response_to_message_with_both_reasoning_fields() -> anyhow::Result<()> {
        let response = json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "answer",
                    "reasoning": "thinking...",
                    "reasoning_content": "thinking..."
                }
            }]
        });

        let message = response_to_message(&response)?;
        assert_eq!(message.content.len(), 2);
        if let MessageContentBlock::Thinking(t) = &message.content[0] {
            assert_eq!(t.thinking, "thinking...");
        } else {
            panic!("Expected Thinking content, got {:?}", message.content[0]);
        }
        Ok(())
    }

    // ---- metadata-only SSE frames (gateway trace/guardrail objects) -----------------------
    //
    // Some OpenAI-compatible gateways interleave objects that have no `choices` key at all
    // with the real chunks. A Portkey gateway sends a `hook_results` guardrail trace as the
    // FIRST frame whenever strict-openai-compliance is off. Failing on such a frame killed
    // the whole turn.

    /// A guardrail trace frame, in the shape a Portkey gateway emits it.
    const METADATA_FRAME: &str = concat!(
        r#"data: {"hook_results":{"before_request_hooks":[{"verdict":true,"#,
        r#""id":"guardrail-1","type":"guardrail","deny":false}]}}"#
    );

    fn content_chunk(content: &str) -> String {
        format!(
            concat!(
                r#"data: {{"id":"x","object":"chat.completion.chunk","created":1,"model":"m","#,
                r#""choices":[{{"index":0,"delta":{{"content":"{}"}},"finish_reason":null}}]}}"#
            ),
            content
        )
    }

    #[tokio::test]
    async fn test_metadata_only_first_frame_does_not_abort_stream() -> anyhow::Result<()> {
        // The reported bug: a metadata-only opening frame made the whole turn fail with
        // "Failed to parse streaming chunk: missing field `choices`".
        let response_lines = format!("{METADATA_FRAME}\n{}\ndata: [DONE]", content_chunk("hello"));
        assert_eq!(run_streaming_test(&response_lines).await?.text, "hello");
        Ok(())
    }

    #[tokio::test]
    async fn test_metadata_only_frame_interleaved_mid_stream_is_skipped() -> anyhow::Result<()> {
        let response_lines = format!(
            "{}\n{METADATA_FRAME}\n{}\ndata: [DONE]",
            content_chunk("hel"),
            content_chunk("lo")
        );
        assert_eq!(run_streaming_test(&response_lines).await?.text, "hello");
        Ok(())
    }

    #[tokio::test]
    async fn test_metadata_only_frame_mid_tool_call_keeps_arguments_intact() -> anyhow::Result<()> {
        // A metadata frame arriving between two tool_calls argument deltas must not end
        // argument accumulation. Merely defaulting `choices` to an empty vec would route this
        // frame into the inner loop's empty-choices branch (`done = true`) and silently
        // truncate the arguments to `{"city":"Pa` — a quiet corruption instead of a loud error.
        let response_lines = concat!(
            r#"data: {"id":"x","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"get_weather","arguments":"{\"city\":\"Pa"}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"hook_results":{"before_request_hooks":[{"verdict":true,"type":"guardrail","deny":false}]}}"#,
            "\n",
            r#"data: {"id":"x","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"ris\"}"}}]},"finish_reason":null}]}"#,
            "\n",
            r#"data: {"id":"x","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut tool_calls = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(req) = content {
                        if let Ok(call) = &req.tool_call {
                            tool_calls.push(call.clone());
                        }
                    }
                }
            }
        }

        assert_eq!(tool_calls.len(), 1, "expected exactly one tool call");
        assert_eq!(tool_calls[0].name, "get_weather");
        assert_eq!(
            tool_calls[0]
                .arguments
                .as_ref()
                .and_then(|a| a.get("city"))
                .and_then(Value::as_str),
            Some("Paris"),
            "arguments must survive the interleaved metadata frame intact"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_empty_choices_array_is_not_treated_as_metadata() -> anyhow::Result<()> {
        // `"choices": []` is the standard usage-only chunk, NOT a metadata frame: it must
        // still deserialize and still surface its usage.
        let response_lines = concat!(
            r#"data: {"id":"x","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":"stop"}]}"#,
            "\n",
            r#"data: {"id":"x","object":"chat.completion.chunk","model":"m","choices":[],"usage":{"prompt_tokens":7,"completion_tokens":3,"total_tokens":10}}"#,
            "\n",
            "data: [DONE]"
        );
        let result = run_streaming_test(response_lines).await?;
        assert_eq!(result.usage_count, 1, "the usage-only chunk must be kept");
        let usage = result.usage.expect("usage should be reported");
        assert_eq!(usage.usage.output_tokens, Some(3));
        assert!(result.has_text_content);
        Ok(())
    }

    #[test]
    fn test_error_frames_still_surface_as_server_error() {
        // Skipping choice-less frames must not swallow gateway error frames, which are also
        // choice-less. Every one of these is handled ahead of the metadata skip.
        for line in [
            r#"{"error":{"message":"upstream exploded"}}"#,
            r#"{"object":"error","message":"upstream exploded"}"#,
            // No `error` key and no `object`: the shape Azure APIM rate-limits with, on an
            // HTTP 200. Skipping this would report the failed turn as an empty success.
            r#"{"statusCode":429,"message":"upstream exploded"}"#,
            // Some gateways stringify the status.
            r#"{"status":"503","message":"upstream exploded"}"#,
            // FastAPI's HTTPException shape.
            r#"{"detail":"upstream exploded"}"#,
            // FastAPI's RequestValidationError shape: `detail` is a LIST, so a string-only
            // read would drop it and silently skip the frame.
            r#"{"detail":[{"loc":["body"],"msg":"upstream exploded","type":"value_error"}]}"#,
            // A non-string `message` must not be dropped either.
            r#"{"statusCode":500,"message":{"text":"upstream exploded"}}"#,
            // `type: "error"` is a third error marker some gateways use.
            r#"{"type":"error","message":"upstream exploded"}"#,
        ] {
            match parse_streaming_chunk(line) {
                Err(ProviderError::ServerError(msg)) => assert!(
                    msg.contains("upstream exploded"),
                    "message preserved for {line}, got {msg:?}"
                ),
                other => panic!("expected ServerError for {line}, got {other:?}"),
            }
        }
    }

    #[tokio::test]
    async fn test_informational_choiceless_frame_is_still_skipped() -> anyhow::Result<()> {
        // Prose alone is not an error signal. A keepalive/progress frame carrying a message
        // but no status must NOT abort the turn — doing so would reintroduce exactly the bug
        // the metadata skip exists to fix.
        let response_lines = format!(
            "{}\n{}\ndata: [DONE]",
            r#"data: {"message":"processing"}"#,
            content_chunk("hello")
        );
        assert_eq!(run_streaming_test(&response_lines).await?.text, "hello");
        Ok(())
    }

    #[test]
    fn test_status_only_error_frame_still_fails_loudly() {
        // No message text anywhere: the frame must still surface rather than vanish.
        match parse_streaming_chunk(r#"{"statusCode":503}"#) {
            Err(ProviderError::ServerError(msg)) => {
                assert!(msg.contains("503"), "status should reach the caller: {msg}")
            }
            other => panic!("expected ServerError, got {other:?}"),
        }
    }

    #[test]
    fn test_choiceless_frame_error_text_is_capped() {
        let long = "x".repeat(5_000);
        let line = format!(r#"{{"statusCode":500,"message":"{long}"}}"#);
        match parse_streaming_chunk(&line) {
            Err(ProviderError::ServerError(msg)) => assert!(
                msg.chars().count() <= MAX_STREAM_ERROR_LEN + 1,
                "error text should be truncated, got {} chars",
                msg.chars().count()
            ),
            other => panic!("expected ServerError, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_in_stream_error_frame_aborts_rather_than_ending_empty() {
        // End-to-end counterpart: the turn must fail, not complete with no content. A silent
        // skip here tells the user to resend — the worst possible advice into a 429.
        let response_lines = concat!(
            r#"data: {"statusCode":429,"message":"rate limited"}"#,
            "\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let first = messages.next().await.expect("stream should yield an item");
        let err = first.expect_err("an in-stream error frame must not be skipped");
        assert!(
            err.to_string().contains("rate limited"),
            "the gateway's message must reach the caller: {err}"
        );
    }

    #[test]
    fn test_parse_streaming_chunk_returns_none_for_metadata_frames() {
        // Unit-level counterpart to the streaming tests above.
        let metadata = parse_streaming_chunk(r#"{"hook_results":{"before_request_hooks":[]}}"#)
            .expect("metadata frame must not be an error");
        assert!(metadata.is_none(), "metadata frame should be skipped");

        let real = parse_streaming_chunk(r#"{"choices":[],"usage":{"completion_tokens":1}}"#)
            .expect("usage-only chunk must parse");
        assert!(
            real.is_some(),
            "`choices: []` is a real chunk, not metadata"
        );
    }

    #[tokio::test]
    async fn test_streaming_chunk_with_only_reasoning_content() -> anyhow::Result<()> {
        let response_lines = "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"hi\"},\"finish_reason\":null}]}\ndata: [DONE]";
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));
        while let Some(result) = messages.next().await {
            result?;
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_does_not_duplicate_yielded_reasoning() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"think \"},\"finish_reason\":null}]}\n",
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"once\"},\"finish_reason\":null}]}\n",
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"test_tool\",\"arguments\":\"\"}}]},\"finish_reason\":null}]}\n",
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut thinking = String::new();
        let mut tool_calls = 0;
        let mut history = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    match content {
                        MessageContentBlock::Thinking(t) => thinking.push_str(&t.thinking),
                        MessageContentBlock::ToolRequest(_) => tool_calls += 1,
                        _ => {}
                    }
                }
                history.push(msg);
            }
        }

        assert_eq!(thinking, "think once");
        assert_eq!(tool_calls, 1);

        let spec = format_messages_with_options(
            &history,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );
        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["reasoning_content"], "think once");
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_merges_yielded_reasoning_with_suffix() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"think \"},\"finish_reason\":null}]}\n",
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"once\",\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"test_tool\",\"arguments\":\"\"}}]},\"finish_reason\":null}]}\n",
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut history = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                history.push(msg);
            }
        }

        let spec = format_messages_with_options(
            &history,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                ..Default::default()
            },
        );
        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["reasoning_content"], "think once");
        assert_eq!(spec[0]["tool_calls"][0]["function"]["name"], "test_tool");

        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_preserves_unyielded_reasoning() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\"tool thought\",\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"test_tool\",\"arguments\":\"{}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut thinking = String::new();
        let mut tool_calls = 0;
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    match content {
                        MessageContentBlock::Thinking(t) => thinking.push_str(&t.thinking),
                        MessageContentBlock::ToolRequest(_) => tool_calls += 1,
                        _ => {}
                    }
                }
            }
        }

        assert_eq!(thinking, "tool thought");
        assert_eq!(tool_calls, 1);
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_without_tool_call_index() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"id\":\"functions.get_weather:0\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"{\\\"city\\\": \\\"Paris\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut tool_calls = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(request) = content {
                        let tool_call = request.tool_call.as_ref().expect("tool call should parse");
                        tool_calls.push((tool_call.name.to_string(), tool_call.arguments.clone()));
                    }
                }
            }
        }

        assert_eq!(tool_calls.len(), 1);
        assert_eq!(tool_calls[0].0, "get_weather");
        assert_eq!(tool_calls[0].1, Some(object!({"city": "Paris"})));
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_dotted_name_passes_through() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"index\":0,\"id\":\"tc1\",\"type\":\"function\",\"function\":{\"name\":\"ext__db.query\",\"arguments\":\"{\\\"command\\\": \\\"ls\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut tool_calls = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(request) = content {
                        let tool_call = request.tool_call.as_ref().expect("tool call should parse");
                        tool_calls.push((tool_call.name.to_string(), tool_call.arguments.clone()));
                    }
                }
            }
        }

        assert_eq!(tool_calls.len(), 1);
        assert_eq!(tool_calls[0].0, "ext__db.query");
        assert_eq!(tool_calls[0].1, Some(object!({"command": "ls"})));
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_tool_call_degenerate_name_passes_through() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"index\":0,\"id\":\"tc1\",\"type\":\"function\",\"function\":{\"name\":\"???\",\"arguments\":\"{}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut names = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(request) = content {
                        names.push(
                            request
                                .tool_call
                                .as_ref()
                                .expect("passes through")
                                .name
                                .to_string(),
                        );
                    }
                }
            }
        }

        assert_eq!(names, vec!["???".to_string()]);
        Ok(())
    }

    #[tokio::test]
    async fn test_streaming_multiple_tool_calls_without_tool_call_index() -> anyhow::Result<()> {
        let response_lines = concat!(
            "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"id\":\"functions.get_weather:0\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"{\\\"city\\\": \\\"Paris\\\"}\"}},{\"id\":\"functions.get_weather:1\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"{\\\"city\\\": \\\"Tokyo\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",
            "data: [DONE]"
        );
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut tool_calls = Vec::new();
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for content in &msg.content {
                    if let MessageContentBlock::ToolRequest(request) = content {
                        let tool_call = request.tool_call.as_ref().expect("tool call should parse");
                        tool_calls.push((tool_call.name.to_string(), tool_call.arguments.clone()));
                    }
                }
            }
        }

        assert_eq!(tool_calls.len(), 2);
        assert_eq!(tool_calls[0].0, "get_weather");
        assert_eq!(tool_calls[0].1, Some(object!({"city": "Paris"})));
        assert_eq!(tool_calls[1].0, "get_weather");
        assert_eq!(tool_calls[1].1, Some(object!({"city": "Tokyo"})));
        Ok(())
    }

    // Streaming counterpart: both fields in one delta must parse and yield
    // thinking content, not fail with "duplicate field `reasoning_content`".
    #[tokio::test]
    async fn test_streaming_chunk_with_both_reasoning_fields() -> anyhow::Result<()> {
        let response_lines = "data: {\"id\":\"x\",\"object\":\"chat.completion.chunk\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning\":\"thinking...\",\"reasoning_content\":\"thinking...\"},\"finish_reason\":null}]}\ndata: [DONE]";
        let lines: Vec<String> = response_lines.lines().map(|s| s.to_string()).collect();
        let response_stream = tokio_stream::iter(lines.into_iter().map(Ok));
        let mut messages = std::pin::pin!(response_to_streaming_message(response_stream));

        let mut saw_thinking = false;
        while let Some(result) = messages.next().await {
            let (message, _usage) = result?;
            if let Some(msg) = message {
                for c in &msg.content {
                    if let MessageContentBlock::Thinking(t) = c {
                        assert_eq!(t.thinking, "thinking...");
                        saw_thinking = true;
                    }
                }
            }
        }
        assert!(
            saw_thinking,
            "expected thinking content from merged reasoning fields"
        );
        Ok(())
    }

    #[test]
    fn test_delta_tool_call_function_accepts_null_arguments() {
        let raw = r#"{"arguments":null}"#;
        let parsed: DeltaToolCallFunction =
            serde_json::from_str(raw).expect("null arguments must deserialize");
        assert_eq!(parsed.arguments, "");

        let raw = r#"{}"#;
        let parsed: DeltaToolCallFunction =
            serde_json::from_str(raw).expect("missing arguments must deserialize");
        assert_eq!(parsed.arguments, "");

        let raw = r#"{"arguments":"{\"k\":1}"}"#;
        let parsed: DeltaToolCallFunction = serde_json::from_str(raw).unwrap();
        assert_eq!(parsed.arguments, "{\"k\":1}");
    }

    #[test]
    fn test_is_openai_responses_model_matches_openai_reasoning_families() {
        for model in [
            "o3",
            "o3-mini",
            "o4-mini",
            "gpt-5",
            "gpt-5-pro",
            "gpt-5.4",
            "gpt-5.4-mini",
            "gpt-5-4",
            "gpt-5-2-pro",
            "databricks-gpt-5.4",
            "goose-gpt-5.4-high",
            "gpt-6-astra",
            "data_workflow_tools.goose.goose-gpt-6-astra",
            "headless-goose-o3-mini",
        ] {
            assert!(is_openai_responses_model(model), "{model} should match");
        }
    }

    #[test]
    fn test_is_openai_responses_model_rejects_other_families() {
        for model in [
            "gpt-4o",
            "claude-sonnet-4",
            "databricks-claude-sonnet-4",
            "llama-3-70b",
        ] {
            assert!(
                !is_openai_responses_model(model),
                "{model} should not match"
            );
        }
    }

    #[test]
    fn test_xai_reasoning_model_capabilities_are_model_specific() {
        for model in ["grok-4.5", "grok-4.3", "grok-3-mini"] {
            assert!(supports_xai_reasoning_effort(model), "{model}");
            assert!(is_xai_reasoning_model(model), "{model}");
        }

        for model in [
            "grok-4.20",
            "grok-4.20-0309-reasoning",
            "grok-4.20-multi-agent",
            "grok-4-0709",
            "grok-4-fast-reasoning",
        ] {
            assert!(!supports_xai_reasoning_effort(model), "{model}");
            assert!(is_xai_reasoning_model(model), "{model}");
        }

        for model in [
            "grok-4.20-0309-non-reasoning",
            "grok-4-fast-non-reasoning",
            "grok-3",
            "grok-build-0.1",
        ] {
            assert!(!supports_xai_reasoning_effort(model), "{model}");
            assert!(!is_xai_reasoning_model(model), "{model}");
        }
    }

    #[test]
    fn test_xai_reasoning_effort_uses_each_models_supported_levels() {
        assert_eq!(
            xai_reasoning_effort_for_thinking("grok-4.5", ThinkingEffort::Off),
            Some("low".to_string())
        );
        assert_eq!(
            xai_reasoning_effort_for_thinking("grok-4.5", ThinkingEffort::Medium),
            Some("medium".to_string())
        );
        assert_eq!(
            xai_reasoning_effort_for_thinking("grok-4.3", ThinkingEffort::Off),
            Some("none".to_string())
        );
        assert_eq!(
            xai_reasoning_effort_for_thinking("grok-3-mini", ThinkingEffort::Medium),
            Some("high".to_string())
        );
        assert_eq!(
            xai_reasoning_effort_for_thinking("grok-4.5", ThinkingEffort::Max),
            Some("high".to_string())
        );
    }

    #[test]
    fn test_create_request_applies_grok_4_5_reasoning_controls() {
        let model_config = ModelConfig::new("grok-4.5")
            .with_thinking_effort(ThinkingEffort::Medium)
            .with_temperature(Some(0.7));

        let payload = create_request(
            &model_config,
            "system prompt",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )
        .unwrap();

        assert_eq!(payload.get("reasoning_effort"), Some(&json!("medium")));
        assert!(payload.get("temperature").is_none());
    }

    #[test]
    fn test_create_request_maps_xai_off_to_supported_effort() {
        for (model, expected) in [("grok-4.5", "low"), ("grok-4.3", "none")] {
            let model_config = ModelConfig::new(model).with_thinking_effort(ThinkingEffort::Off);
            let payload = create_request(
                &model_config,
                "system prompt",
                &[],
                &[],
                &ImageFormat::OpenAi,
                false,
            )
            .unwrap();

            assert_eq!(
                payload.get("reasoning_effort"),
                Some(&json!(expected)),
                "{model}"
            );
        }
    }

    #[test]
    fn test_create_request_keeps_grok_4_20_fixed_reasoning_parameters() {
        let model_config = ModelConfig::new("grok-4.20-0309-reasoning")
            .with_thinking_effort(ThinkingEffort::High)
            .with_temperature(Some(0.7));

        let payload = create_request(
            &model_config,
            "system prompt",
            &[],
            &[],
            &ImageFormat::OpenAi,
            false,
        )
        .unwrap();

        assert!(payload.get("reasoning_effort").is_none());
        assert_eq!(payload.get("temperature"), Some(&json!(0.7_f32)));
    }

    #[test]
    fn test_extract_reasoning_effort_for_responses_models() {
        for (model, expected_name, expected_effort) in [
            ("o3-none", "o3", Some("none")),
            ("o3-xhigh", "o3", Some("xhigh")),
            ("gpt-5-low", "gpt-5", Some("low")),
            ("gpt-5.4", "gpt-5.4", None),
            (
                "databricks-gpt-5.4-high",
                "databricks-gpt-5.4",
                Some("high"),
            ),
            ("databricks-o3-low", "databricks-o3", Some("low")),
            ("goose-gpt-5-high", "goose-gpt-5", Some("high")),
            ("gpt-4o", "gpt-4o", None),
        ] {
            let (name, effort) = extract_reasoning_effort(model);
            assert_eq!(name, expected_name, "unexpected base model for {model}");
            assert_eq!(
                effort.as_deref(),
                expected_effort,
                "unexpected effort for {model}"
            );
        }
    }

    #[test]
    fn test_sanitize_function_name() {
        assert_eq!(sanitize_function_name("hello-world"), "hello-world");
        assert_eq!(sanitize_function_name("hello world"), "hello_world");
        assert_eq!(sanitize_function_name("hello@world"), "hello_world");
        assert_eq!(
            sanitize_function_name(&"a".repeat(MAX_FUNCTION_NAME_LENGTH)),
            "a".repeat(MAX_FUNCTION_NAME_LENGTH)
        );
        assert_eq!(
            sanitize_function_name(&"a".repeat(MAX_FUNCTION_NAME_LENGTH + 32)),
            "a".repeat(MAX_FUNCTION_NAME_LENGTH)
        );
    }

    #[test]
    fn test_is_valid_function_name() {
        assert!(is_valid_function_name("hello-world"));
        assert!(is_valid_function_name("hello_world"));
        assert!(is_valid_function_name(
            &"a".repeat(MAX_FUNCTION_NAME_LENGTH + 1)
        ));
        assert!(!is_valid_function_name("hello world"));
        assert!(!is_valid_function_name("hello@world"));
    }

    #[test]
    fn formatter_post_parse_error_history_is_wellformed() {
        use rmcp::model::{ErrorCode, ErrorData};
        let err = ErrorData::new(
            ErrorCode::INVALID_PARAMS,
            "Tool arguments for id call_bad must be a JSON object".to_string(),
            None,
        );
        // Shape the agent loop builds today for a failed parse:
        let request_msg = Message::assistant().with_tool_request("call_bad", Err(err.clone()));
        let mut final_resp = Message::user();
        final_resp.add_tool_response_with_metadata("call_bad", Err(err), None);
        let messages = vec![
            Message::user().with_text("do the thing"),
            request_msg,
            final_resp,
        ];

        let spec = format_messages(&messages, &ImageFormat::OpenAi);

        let mut open = std::collections::HashSet::new();
        for m in &spec {
            match m.get("role").and_then(|v| v.as_str()) {
                Some("assistant") => {
                    for tc in m
                        .get("tool_calls")
                        .and_then(|v| v.as_array())
                        .into_iter()
                        .flatten()
                    {
                        if let Some(id) = tc.get("id").and_then(|v| v.as_str()) {
                            open.insert(id.to_string());
                        }
                    }
                }
                Some("tool") => {
                    let id = m.get("tool_call_id").and_then(|v| v.as_str()).unwrap_or("");
                    assert!(open.contains(id), "orphan role:tool message for id {id:?}");
                }
                _ => {}
            }
        }
    }

    fn format_with_preservation(
        messages: &[Message],
        format: ThinkingPreservationFormat,
    ) -> Vec<Value> {
        format_messages_with_options(
            messages,
            &ImageFormat::OpenAi,
            OpenAiFormatOptions {
                preserve_thinking_context: true,
                supports_vision: false,
                thinking_preservation_format: Some(format),
            },
        )
    }

    #[test]
    fn test_thinking_preservation_content_prepend() {
        let message = Message::assistant()
            .with_thinking("Thinking process", "")
            .with_text("Hello");

        let spec = format_with_preservation(
            std::slice::from_ref(&message),
            ThinkingPreservationFormat::ContentPrepend,
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["content"], json!("Thinking process\n\nHello"));
        assert!(spec[0].get("reasoning_content").is_none());
    }

    #[test]
    fn test_thinking_preservation_content_xml() {
        let message = Message::assistant()
            .with_thinking("Thinking process", "")
            .with_text("Hello");

        let spec = format_with_preservation(
            std::slice::from_ref(&message),
            ThinkingPreservationFormat::ContentXml,
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(
            spec[0]["content"],
            json!("<think>\nThinking process\n</think>\n\nHello")
        );
        assert!(spec[0].get("reasoning_content").is_none());
    }

    #[test]
    fn test_thinking_preservation_reasoning_content_is_unchanged() {
        let message = Message::assistant()
            .with_thinking("Thinking process", "")
            .with_text("Hello");

        let spec = format_with_preservation(
            std::slice::from_ref(&message),
            ThinkingPreservationFormat::ReasoningContent,
        );

        assert_eq!(spec.len(), 1);
        assert_eq!(spec[0]["content"], json!("Hello"));
        assert_eq!(spec[0]["reasoning_content"], json!("Thinking process"));
    }

    #[test]
    fn test_thinking_preservation_runs_after_split_tool_call_merge() {
        // Split tool-call messages are reunited by matching reasoning_content, so
        // inlining must happen afterwards or the merge silently stops working.
        let messages = vec![
            Message::assistant().with_thinking("reasoning", ""),
            Message::assistant()
                .with_thinking("reasoning", "")
                .with_tool_request(
                    "tool1",
                    Ok(CallToolRequestParams::new("tool_a").with_arguments(object!({}))),
                ),
            Message::user().with_tool_response(
                "tool1",
                Ok(rmcp::model::CallToolResult::success(vec![
                    ContentBlock::text("result1"),
                ])),
            ),
            Message::assistant()
                .with_thinking("reasoning", "")
                .with_tool_request(
                    "tool2",
                    Ok(CallToolRequestParams::new("tool_b").with_arguments(object!({}))),
                ),
        ];

        let spec = format_with_preservation(&messages, ThinkingPreservationFormat::ContentXml);

        let assistant: Vec<_> = spec
            .iter()
            .filter(|m| m.get("role") == Some(&json!("assistant")))
            .collect();

        assert_eq!(assistant.len(), 1);
        assert_eq!(assistant[0]["tool_calls"].as_array().unwrap().len(), 2);
        assert!(assistant[0].get("reasoning_content").is_none());
        assert_eq!(
            assistant[0]["content"],
            json!("<think>\nreasoning\n</think>")
        );
    }

    mod cache_prefix_stability {
        use super::*;

        fn turn_context(time: &str, turn_budget: &str) -> String {
            format!(
                "<turn-context>\n\
                 <current-time>{time}</current-time>\n\
                 <working-directory>/Users/me/code/goose</working-directory>\n\
                 <turn-budget>{turn_budget}</turn-budget>\n\
                 </turn-context>"
            )
        }

        fn tool_loop_conversation(turn_context_block: &str) -> Vec<Message> {
            vec![
                Message::user().with_text("What does the main entrypoint do?"),
                Message::assistant().with_text("Let me read it."),
                Message::user()
                    .with_text("Now add error handling to it.")
                    .with_text(turn_context_block),
                Message::assistant().with_tool_request(
                    "tool_1",
                    Ok(CallToolRequestParams::new("read_file")
                        .with_arguments(object!({"path": "src/main.rs"}))),
                ),
                Message::user().with_tool_response(
                    "tool_1",
                    Ok(CallToolResult::success(vec![ContentBlock::text(
                        "fn main() { run(); }",
                    )])),
                ),
            ]
        }

        #[test]
        fn turn_context_stays_on_its_source_message() {
            let block = turn_context("2026-08-03 12:00:00", "14/40 used");
            let spec = format_messages(&tool_loop_conversation(&block), &ImageFormat::OpenAi);

            let occurrences: Vec<usize> = spec
                .iter()
                .enumerate()
                .filter_map(|(i, m)| {
                    m["content"]
                        .as_str()
                        .is_some_and(|c| c.contains("<turn-context>"))
                        .then_some(i)
                })
                .collect();
            assert_eq!(
                occurrences,
                vec![2],
                "turn-context must appear exactly once, on its source message"
            );
        }
    }
}