prux 1.1.0

A minimal terminal coding agent: TUI, built-in coding tools, and an extension system
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
//! OpenAI chat completions 协议(deepseek / 多数 openai 兼容端点)。

use super::{
    AgentMessage, ContentBlock, Cost, ModelConfig, PartialTranslator, RawStreamEvent, StreamResult,
    Usage, azure,
    convert::{
        BRANCH_SUMMARY_PREFIX, BRANCH_SUMMARY_SUFFIX, COMPACTION_SUMMARY_PREFIX,
        COMPACTION_SUMMARY_SUFFIX, collapse_mid_convo_system_messages, convert_system_message,
        flush_pending_system_messages, instruction_role, sanitize_surrogates,
        supports_mid_convo_system_messages, truncate_for_error,
    },
    provider_extra_headers, track_stream_bytes,
    usage::{compute_cost, parse_streaming_json},
};
use crate::{
    core::{
        self, model_resolver,
        provider::{apply_payload_hook, grammar_input_property_for_tool},
        tools::tool_defs,
    },
    error::{Error, Result},
    utils::{http, time::now_ms},
};
use futures_util::StreamExt;
use serde::Serialize;
use serde_json::{Value, json};
use std::collections::HashMap;

/// 工具结果转换结果。
struct ConvertedToolResult {
    /// `role: "tool"` 消息
    tool: Value,
    /// 工具结果里的图片(data URL),由调用方延后到本批次末尾补发
    image_urls: Vec<String>,
}

// 请求/响应结构(OpenAI chat completions)
/// 按模型能力(工具、thinking、图片)组装的请求体,序列化后发往兼容端点。
#[derive(Serialize)]
struct ChatRequest {
    /// 请求使用的模型 id(取自 `ModelConfig.model_id`)。
    model: String,
    /// 已转成 OpenAI 格式的对话消息数组。
    messages: Vec<Value>,
    /// 固定为 true,向端点请求 SSE 流式响应。
    stream: bool,
    /// 输出 token 上限;None 表示不限制,序列化时按模型字段名注入。
    // max_tokens 不直接序列化:由 stream() 按 model.max_tokens_field 注入
    // (避免与 max_completion_tokens 同体出现导致 400)
    #[serde(skip)]
    max_tokens: Option<u32>,
    /// 请求流式用量统计(`include_usage`);模型不支持流式用量时为 None。
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_options: Option<Value>,
    /// 已转换的工具定义;无工具时为 None(避免发空数组)。
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<Vec<Value>>,
    /// zai/GLM 等 thinkingFormat 的 thinking 对象参数,按思考档位构造。
    #[serde(skip_serializing_if = "Option::is_none")]
    thinking: Option<Value>,
    /// 思考强度档位(如 low/high/none),模型不支持或关闭时不发。
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning_effort: Option<String>,
    /// qwen thinkingFormat:enable_thinking 布尔
    #[serde(skip_serializing_if = "Option::is_none")]
    enable_thinking: Option<bool>,
    /// openrouter/together/baseten thinkingFormat:reasoning 对象
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning: Option<Value>,
    /// qwen-chat-template thinkingFormat:chat_template_kwargs 对象
    #[serde(skip_serializing_if = "Option::is_none")]
    chat_template_kwargs: Option<Value>,
    /// 采样温度;None 时由 samplingParams 的默认值兜底。
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f64>,
    /// "auto" | "none"(provider-neutral)
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_choice: Option<String>,
}

/// 跨协议 tool call id 归一化:responses 的 `call|item` 复合 id 重放到
/// completions 时替换非法字符并截断(对齐 pi transformMessages)
fn normalize_tool_call_id(id: &str) -> String {
    let sanitized: String = id
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
                c
            } else {
                '_'
            }
        })
        .collect();
    sanitized.chars().take(64).collect()
}

/// 为仍未配对的 tool_calls 补 synthetic tool result。
///
/// `pending` 在收到对应 toolResult 时立即移除,因此这里剩下的就是真正没有结果的调用,
/// 无需再维护 `existing` 集合去重。
fn insert_synthetic_tool_results(params: &mut Vec<Value>, pending: &mut Vec<(String, String)>) {
    for (id, _name) in pending.drain(..) {
        params.push(json!({
            "role": "tool",
            "content": "No result provided",
            "tool_call_id": id
        }));
    }
}

/// 把 user 消息转成 openai 的 `user` 消息(content 为 parts 数组)。
/// 模型不支持图片时把图片块替换成占位文本;无任何可发内容时返回 None。
fn convert_user_message(msg: &AgentMessage, model: &ModelConfig) -> Option<Value> {
    let supports_images = model.input.iter().any(|x| x == "image");
    let mut content_arr: Vec<Value> = Vec::new();
    let mut previous_was_placeholder = false;
    for block in &msg.content {
        match block {
            ContentBlock::Text { text, .. } => {
                // 纯图片 user 消息不能带空 text part,否则部分 OpenAI-compatible provider 直接拒绝。
                if !text.is_empty() {
                    content_arr.push(json!({ "type": "text", "text": sanitize_surrogates(text) }));
                }
                previous_was_placeholder = text == "(image omitted: model does not support images)";
            }
            ContentBlock::Image { data, mime_type } => {
                if supports_images {
                    content_arr.push(json!({
                        "type": "image_url",
                        "image_url": {
                            "url": format!("data:{};base64,{}", mime_type, data)
                        }
                    }));
                    previous_was_placeholder = false;
                } else if !previous_was_placeholder {
                    content_arr.push(json!({
                        "type": "text",
                        "text": "(image omitted: model does not support images)"
                    }));
                    previous_was_placeholder = true;
                }
            }
            _ => {}
        }
    }
    if content_arr.is_empty() {
        return None;
    }
    Some(json!({ "role": "user", "content": content_arr }))
}

/// 转换 assistant 消息;同时把本消息中未配对的 tool_calls 记入 pending,供
/// 后续消息插入 synthetic tool results 使用
fn convert_assistant_message(
    msg: &AgentMessage,
    model: &ModelConfig,
    pending_tool_calls: &mut Vec<(String, String)>,
) -> Option<Value> {
    // 跳过 error/aborted 的 assistant 消息
    if msg.stop_reason.as_deref() == Some("error") || msg.stop_reason.as_deref() == Some("aborted")
    {
        return None;
    }
    let assistant_text: String = msg
        .content
        .iter()
        .filter_map(|b| match b {
            ContentBlock::Text { text, .. } if !text.trim().is_empty() => Some(text.clone()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("");

    let mut assistant_msg = serde_json::Map::new();
    assistant_msg.insert("role".into(), json!("assistant"));
    // openai SDK 默认 content 为 null;deepseek 需要 content
    assistant_msg.insert("content".into(), Value::Null);

    let non_empty_thinking: Vec<String> = msg
        .content
        .iter()
        .filter_map(|b| match b {
            ContentBlock::Thinking { thinking, .. } if !thinking.trim().is_empty() => {
                Some(thinking.clone())
            }
            _ => None,
        })
        .collect();

    if !non_empty_thinking.is_empty() {
        if !assistant_text.is_empty() {
            assistant_msg.insert("content".into(), json!(assistant_text));
        }
        assistant_msg.insert(
            "reasoning_content".into(),
            json!(non_empty_thinking.join("\n")),
        );
    } else if !assistant_text.is_empty() {
        assistant_msg.insert("content".into(), json!(assistant_text));
    }

    let tool_calls: Vec<&ContentBlock> = msg
        .content
        .iter()
        .filter(|b| matches!(b, ContentBlock::ToolCall { .. }))
        .collect();

    if !tool_calls.is_empty() {
        let grammar_supported =
            model_resolver::supports_openai_grammar_tools(&model.provider, &model.model_id);

        let calls: Vec<Value> = tool_calls
            .iter()
            .map(|b| match b {
                ContentBlock::ToolCall { id, name, arguments, .. } => {
                    // grammar 工具的历史调用要回放成 `custom` 形状(裸源码),否则模型看到的是一段被 JSON 转义过的代码
                    if grammar_supported
                        && let Some(property) = grammar_input_property_for_tool(name)
                    {
                        return json!({
                            "id": normalize_tool_call_id(id),
                            "type": "custom",
                            "custom": {
                                "name": name,
                                "input": arguments
                                    .get(&property)
                                    .and_then(|v| v.as_str())
                                    .unwrap_or_default(),
                            }
                        });
                    }
                    json!({
                        "id": normalize_tool_call_id(id),
                        "type": "function",
                        "function": {
                            "name": name,
                            "arguments": serde_json::to_string(arguments).unwrap_or_else(|_| "{}".into())
                        }
                    })
                }
                _ => Value::Null,
            })
            .collect();
        assistant_msg.insert("tool_calls".into(), Value::Array(calls));
        for tc in &tool_calls {
            if let ContentBlock::ToolCall { id, name, .. } = tc {
                pending_tool_calls.push((normalize_tool_call_id(id), name.clone()));
            }
        }
    }

    if model.requires_reasoning_content_on_assistant_messages
        && model.reasoning
        && assistant_msg.get("reasoning_content").is_none()
    {
        assistant_msg.insert("reasoning_content".into(), json!(""));
    }

    let has_content = match assistant_msg.get("content") {
        Some(Value::Null) => false,
        Some(Value::String(s)) => !s.is_empty(),
        _ => false,
    };
    if !has_content && !assistant_msg.contains_key("tool_calls") {
        return None;
    }
    Some(Value::Object(assistant_msg))
}

/// 把压缩/分支摘要消息包上前后缀,转成一条 `user` 文本消息(上游不认 summary 角色)。
fn convert_summary_message(msg: &AgentMessage) -> Value {
    let (prefix, suffix) = if msg.role == "compactionSummary" {
        (COMPACTION_SUMMARY_PREFIX, COMPACTION_SUMMARY_SUFFIX)
    } else {
        (BRANCH_SUMMARY_PREFIX, BRANCH_SUMMARY_SUFFIX)
    };
    let text = format!("{}{}{}", prefix, msg.text(), suffix);
    json!({ "role": "user", "content": [{"type": "text", "text": sanitize_surrogates(&text)}] })
}

/// 工具结果消息:返回 `tool` 消息;若含图片,图片块单独返回给调用方延后补发。
///
/// 上游不允许 `tool` 消息直接带图片,也不允许 user 消息插在 `assistant.tool_calls`
/// 与它的 `tool` 结果之间(见 [`finish_tool_batch`]),所以图片不能在这里自行追加消息。
fn convert_tool_result_message(msg: &AgentMessage, model: &ModelConfig) -> ConvertedToolResult {
    let supports_images = model.input.iter().any(|x| x == "image");
    let mut text_result: Vec<String> = Vec::new();
    let mut image_parts: Vec<String> = Vec::new();
    let mut previous_was_placeholder = false;
    for block in &msg.content {
        match block {
            ContentBlock::Text { text, .. } => {
                text_result.push(text.clone());
                previous_was_placeholder =
                    text == "(tool image omitted: model does not support images)";
            }
            ContentBlock::Image { data, mime_type } => {
                if supports_images {
                    image_parts.push(format!("data:{};base64,{}", mime_type, data));
                    previous_was_placeholder = false;
                } else if !previous_was_placeholder {
                    text_result
                        .push("(tool image omitted: model does not support images)".to_string());
                    previous_was_placeholder = true;
                }
            }
            _ => {}
        }
    }
    let text = text_result.join("\n");
    let has_images = !image_parts.is_empty();
    let has_text = !text.is_empty();
    let tool_result_text = if has_text {
        text
    } else if has_images {
        "(see attached image)".to_string()
    } else {
        "(no tool output)".to_string()
    };
    let mut tool_msg = serde_json::Map::new();
    tool_msg.insert("role".into(), json!("tool"));
    tool_msg.insert(
        "content".into(),
        json!(sanitize_surrogates(&tool_result_text)),
    );
    tool_msg.insert(
        "tool_call_id".into(),
        json!(
            msg.tool_call_id
                .clone()
                .map(|id| normalize_tool_call_id(&id))
                .unwrap_or_default()
        ),
    );

    ConvertedToolResult {
        tool: Value::Object(tool_msg),
        image_urls: image_parts,
    }
}

/// 收尾一个 `assistant.tool_calls` 批次:补发工具结果里的图片 + 可选的空 assistant。
///
/// 上游(opencode zen / OpenAI-compatible)要求 `assistant(tool_calls)` 之后紧跟着
/// **全部** `tool` 消息,中间插入 user 消息会被拒:
///
/// ```text
/// provider returned 400 Bad Request: {"model":"deepseek-v4.1-flash"}
/// ```
///
/// 因此一批里若只有部分工具结果带图片,图片消息必须等到批次末尾(`pending` 清空)才能插入。
/// 历史一旦落库带上了这个顺序,该会话之后每次请求都会 400。
fn finish_tool_batch(
    params: &mut Vec<Value>,
    deferred_image_urls: &mut Vec<String>,
    model: &ModelConfig,
) {
    if !deferred_image_urls.is_empty() {
        let mut arr: Vec<Value> = vec![json!({
            "type": "text",
            "text": "Attached image(s) from tool result:"
        })];

        arr.extend(deferred_image_urls.drain(..).map(|url| {
            json!({
                "type": "image_url",
                "image_url": { "url": url }
            })
        }));
        params.push(json!({ "role": "user", "content": arr }));
    }

    // 工具结果(含补发的图片)之后插 assistant 空消息
    if model.requires_assistant_after_tool_result {
        params.push(json!({ "role": "assistant", "content": [] }));
    }
}

/// 关闭一个还没走到末尾的工具批次(例:下一轮 user 消息提前到达、历史从中间截断):
/// 先给未配对的 tool_calls 补 synthetic 结果,再补发图片。
///
/// `had_results`:本批次是否真的发出过 tool 结果(纯 synthetic 补位不算,与旧行为一致)。
fn close_tool_batch(
    params: &mut Vec<Value>,
    pending: &mut Vec<(String, String)>,
    deferred_image_urls: &mut Vec<String>,
    model: &ModelConfig,
    had_results: bool,
) {
    if pending.is_empty() {
        return; // 已经收尾过(图片也早已补发)
    }

    insert_synthetic_tool_results(params, pending);

    if !had_results {
        return; // 只有 synthetic 补位:不发空 assistant
    }

    finish_tool_batch(params, deferred_image_urls, model);
}

/// 把内部 [`AgentMessage`] 历史整体转成 openai `chat/completions` 的 messages 数组。
///
/// 除逐条转换外还负责修补协议约束:跳过 error/aborted 的 assistant、为孤儿 tool_calls 补
/// synthetic tool result、丢弃对不上 tool_calls 的孤儿 tool 结果,并把工具结果里的图片
/// 延后到批次末尾补发(见 [`finish_tool_batch`])。
///
/// `role == "system"` 的中途消息按模型 `compat.supportsMidConvoSystemMessages` 处理:为真时作为
/// 原生对话中途指令下发,且**缓发**到「下一条非 toolResult 消息之前」(或序列末尾),
/// 以免打断 `assistant(tool_calls)` 与紧随其后的 `tool` 结果;为假时跳过
/// (由 [`collapse_mid_convo_system_messages`] 并入首部提示词)。
pub fn convert_messages(
    messages: &[AgentMessage],
    system_prompt: &str,
    model: &ModelConfig,
) -> Vec<Value> {
    let mut params: Vec<Value> = Vec::new();
    let supports_mid_convo = supports_mid_convo_system_messages(model);
    let mut pending_system: Vec<Value> = Vec::new();

    if !system_prompt.is_empty() {
        params.push(json!({
            "role": instruction_role(model),
            "content": sanitize_surrogates(system_prompt)
        }));
    }

    // - 跳过 stopReason=error/aborted 的 assistant 消息
    // - 为孤儿 tool_calls 插入 synthetic tool results
    // - 丢弃无法对上 assistant tool_calls 的孤儿 tool 结果
    let mut pending_tool_calls: Vec<(String, String)> = Vec::new(); // (id, name)
    //
    // 本批次工具结果里的图片:必须等 `pending_tool_calls` 清空后才能插进消息流(见 [`finish_tool_batch`];插在批次中间会 400)。
    let mut deferred_image_urls: Vec<String> = Vec::new();

    // 本批次是否已发出真实 tool 结果(用于决定收尾时是否补空 assistant)
    let mut batch_emitted_results = false;

    for msg in messages {
        match msg.role.as_str() {
            "user" => {
                close_tool_batch(
                    &mut params,
                    &mut pending_tool_calls,
                    &mut deferred_image_urls,
                    model,
                    batch_emitted_results,
                );
                batch_emitted_results = false;
                flush_pending_system_messages(&mut params, &mut pending_system);
                if let Some(user_msg) = convert_user_message(msg, model) {
                    params.push(user_msg);
                }
            }
            "assistant" => {
                close_tool_batch(
                    &mut params,
                    &mut pending_tool_calls,
                    &mut deferred_image_urls,
                    model,
                    batch_emitted_results,
                );
                batch_emitted_results = false;
                flush_pending_system_messages(&mut params, &mut pending_system);
                if let Some(assistant_msg) =
                    convert_assistant_message(msg, model, &mut pending_tool_calls)
                {
                    params.push(assistant_msg);
                }
            }
            "compactionSummary" | "branchSummary" => {
                close_tool_batch(
                    &mut params,
                    &mut pending_tool_calls,
                    &mut deferred_image_urls,
                    model,
                    batch_emitted_results,
                );
                batch_emitted_results = false;
                flush_pending_system_messages(&mut params, &mut pending_system);
                params.push(convert_summary_message(msg));
            }
            // custom 角色转 user(扩展注入消息进上下文)
            "custom" => {
                close_tool_batch(
                    &mut params,
                    &mut pending_tool_calls,
                    &mut deferred_image_urls,
                    model,
                    batch_emitted_results,
                );
                batch_emitted_results = false;
                flush_pending_system_messages(&mut params, &mut pending_system);
                if let Some(user_msg) = convert_user_message(msg, model) {
                    params.push(user_msg);
                }
            }
            "system" => {
                if supports_mid_convo && let Some(system_msg) = convert_system_message(msg, model) {
                    pending_system.push(system_msg);
                }
            }
            "toolResult" => {
                // 只有能对上前面 assistant tool_calls 的结果才发送;否则丢弃。
                // 历史可能因扩展 transform_context 删除 assistant、或压缩保留尾部从批次中间
                // 截断而留下孤儿 tool 结果,直接发送会触发上游 400:
                // "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'"。
                let norm_id = msg.tool_call_id.as_deref().map(normalize_tool_call_id);
                match norm_id {
                    Some(id) if pending_tool_calls.iter().any(|(p, _)| *p == id) => {
                        pending_tool_calls.retain(|(p, _)| *p != id);

                        let converted = convert_tool_result_message(msg, model);
                        params.push(converted.tool);
                        deferred_image_urls.extend(converted.image_urls);
                        batch_emitted_results = true;

                        // 批次内最后一个 tool 结果已到位 → 现在才能插图片消息
                        if pending_tool_calls.is_empty() {
                            finish_tool_batch(&mut params, &mut deferred_image_urls, model);
                            batch_emitted_results = false;
                        }
                    }
                    _ => {}
                }
            }
            _ => {}
        }
    }

    // 列表尾部若仍有未配对的 tool_calls(中止批次 / 崩溃恢复等半截历史),补 synthetic tool results,
    // 避免上游校验 "assistant message with 'tool_calls' must be followed by tool messages ..." 报 400。
    close_tool_batch(
        &mut params,
        &mut pending_tool_calls,
        &mut deferred_image_urls,
        model,
        batch_emitted_results,
    );

    flush_pending_system_messages(&mut params, &mut pending_system);

    params
}

/// 把 `(name, description, schema)` 工具定义转成 openai 的 function 工具数组(不启用 strict / grammar)。
pub fn convert_tools(tool_defs: &[(String, String, Value)]) -> Vec<Value> {
    convert_tools_inner(tool_defs, false, false)
}

/// 带 strict mode 的工具转换
pub fn convert_tools_with_strict(
    tool_defs: &[(String, String, Value)],
    strict: bool,
) -> Vec<Value> {
    convert_tools_inner(tool_defs, strict, false)
}

/// 带全部能力的工具转换:`strict` = 模型支持 strict mode,`grammar` = 模型支持 grammar 约束工具。
pub(crate) fn convert_tools_with_capabilities(
    tool_defs: &[(String, String, Value)],
    strict: bool,
    grammar: bool,
) -> Vec<Value> {
    convert_tools_inner(tool_defs, strict, grammar)
}

/// 工具是否声明了 constrainedSampling。内置工具查 ToolDef,扩展工具查 ExtensionTool。
fn tool_has_constrained_sampling(name: &str) -> bool {
    if let Some(def) = tool_defs(std::slice::from_ref(&name.to_string())).first()
        && def.constrained_sampling
    {
        return true;
    }

    core::extensions::registered().iter().any(|ext| {
        ext.tools()
            .iter()
            .any(|t| t.name == name && t.constrained_sampling)
    })
}

/// 工具转换的公共实现:`strict_supported` 时给声明了 constrainedSampling 的工具补 `strict`
/// 字段,并追加 `additionalProperties: false` 与全字段 required;`grammar_supported` 时把
/// 声明了语法约束的工具转成 `custom` 工具(裸源码输入,对齐 pi/OpenAI)。
fn convert_tools_inner(
    tool_defs: &[(String, String, Value)],
    strict_supported: bool,
    grammar_supported: bool,
) -> Vec<Value> {
    tool_defs
        .iter()
        .map(|(name, description, parameters)| {
            if grammar_supported && let Some(grammar) = core::provider::grammar_tool(parameters) {
                return json!({
                    "type": "custom",
                    "custom": {
                        "name": name,
                        "description": description,
                        "format": {
                            "type": "grammar",
                            "grammar": {
                                "syntax": grammar.syntax,
                                "definition": grammar.definition,
                            }
                        }
                    }
                });
            }

            let mut params = parameters.clone();
            // 逐工具 strict(仅声明 constrainedSampling 的工具在模型支持时 strict 化;
            // 其余保留原始 schema)。strict_supported 对应 provider 的 supportsStrictMode。
            let this_strict = strict_supported && tool_has_constrained_sampling(name);
            if this_strict && params.is_object() {
                if !params.get("additionalProperties").is_some() {
                    params["additionalProperties"] = json!(false);
                }

                // required 全字段(strict 模式要求)
                if let Some(props) = params.get("properties").and_then(|p| p.as_object()) {
                    let required: Vec<Value> = props.keys().map(|k| json!(k)).collect();
                    if !params.get("required").is_some() {
                        params["required"] = Value::Array(required);
                    }
                }
            }

            let mut function =
                json!({ "name": name, "description": description, "parameters": params });

            // provider 支持 strict mode 时才带 strict 字段(部分 provider 拒绝未知字段)
            if strict_supported {
                function["strict"] = json!(this_strict);
            }

            json!({ "type": "function", "function": function })
        })
        .collect()
}

/// 以 openai `chat/completions` SSE 流式发起一次请求,边解析边通过 `on_event` 转发事件。
///
/// 返回累积完成的 [`StreamResult`];HTTP 传输/状态错误返回 `Err`,流内 provider 错误则
/// 由 [`StreamState`] 编码进结果(stopReason=error + errorMessage)。
#[allow(clippy::too_many_arguments)]
pub(crate) async fn stream(
    model: &ModelConfig,
    messages: &[AgentMessage],
    system_prompt: &str,
    tools: &[(String, String, Value)],
    reasoning_effort: Option<&str>,
    temperature: Option<f64>,
    max_tokens: Option<u32>,
    mut on_event: Option<&mut PartialTranslator<'_>>,
    on_payload: Option<crate::core::provider::PayloadHook>,
    tool_choice: Option<&str>,
) -> Result<StreamResult> {
    // 按模型能力钳制级别;"off" 归一到 None(避免把 off 当启用级别发给 provider)
    // 级别覆盖项要在归一之前按级别名查出(归一后 off 已丢失)
    let level_sampling_params = model.sampling_params_for_level(reasoning_effort).cloned();
    let reasoning_effort = normalize_reasoning_effort(model, reasoning_effort);
    let client = http::build_client()?;

    // 不支持原生对话中途系统消息的模型:这些消息并入首部系统提示词
    let effective_system_prompt =
        collapse_mid_convo_system_messages(messages, system_prompt, model);
    let mut openai_messages = convert_messages(messages, &effective_system_prompt, model);
    openai_messages.retain(|m| !m.is_null()); // 忽略空消息

    let mut request = build_chat_request(model, &openai_messages, tools, temperature, max_tokens);
    if let Some(tc) = tool_choice {
        request.tool_choice = Some(tc.to_string());
    }
    apply_thinking_config(&mut request, model, reasoning_effort.as_deref());
    let mut body = serialize_request_body(&request, model, level_sampling_params.as_ref())?;
    apply_payload_hook(&mut body, on_payload.as_ref());

    let response = send_completion_request(&client, model, body).await?;
    let mut stream = response.bytes_stream();

    let mut state = StreamState::new();
    let mut buf: Vec<u8> = Vec::new();
    let mut total_bytes: usize = 0;

    // 供应商原始流事件:无扩展订阅时零开销(一次流只查一次)
    let capture_stream_events = core::extensions::has_provider_stream_event_handlers();

    while let Some(chunk) = stream.next().await {
        let chunk = match chunk {
            Ok(c) => c,
            Err(e) => return Err(Error::msg(format!("stream read failed: {}", e))),
        };
        track_stream_bytes(&mut total_bytes, chunk.len())?;
        // 按行拆分 SSE(可能跨 chunk)
        let (lines, carry) = split_sse_lines(&mut buf, &chunk);
        buf = carry;

        for line in lines {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let Some(data) = line.strip_prefix("data:") else {
                continue;
            };
            let data = data.trim();
            if data == "[DONE]" {
                break;
            }
            let chunk_val: Value = match serde_json::from_str(data) {
                Ok(v) => v,
                Err(_) => continue,
            };
            if capture_stream_events {
                core::extensions::dispatch_provider_stream_event(
                    &model.provider,
                    &model.api,
                    &model.model_id,
                    &chunk_val,
                );
            }
            state.handle_chunk(&chunk_val, model, &mut on_event)?;
        }
    }

    state.finish(model, &mut on_event)
}

/// 按模型能力钳制级别;"off" 归一到 None
fn normalize_reasoning_effort(
    model: &ModelConfig,
    reasoning_effort: Option<&str>,
) -> Option<String> {
    match reasoning_effort {
        Some(level) => {
            let clamped = model.clamp_thinking_level(level);
            if clamped == "off" {
                None
            } else {
                Some(clamped)
            }
        }
        None => None,
    }
}

/// 组装 openai 兼容请求体:模型 id、messages、stream、max_tokens、usage 流选项与工具定义。
/// thinking 相关字段留空,由 [`apply_thinking_config`] 按模型配置补。
fn build_chat_request(
    model: &ModelConfig,
    openai_messages: &[Value],
    tools: &[(String, String, Value)],
    temperature: Option<f64>,
    max_tokens: Option<u32>,
) -> ChatRequest {
    ChatRequest {
        model: azure::request_model_name(model).into_owned(),
        messages: openai_messages.to_vec(),
        stream: true,
        max_tokens: max_tokens.or(model.max_tokens),
        stream_options: if model.supports_usage_in_streaming {
            Some(json!({ "include_usage": true }))
        } else {
            None
        },
        tools: if tools.is_empty() {
            None
        } else {
            Some(convert_tools_with_capabilities(
                tools,
                model.supports_strict_mode,
                model_resolver::supports_openai_grammar_tools(&model.provider, &model.model_id),
            ))
        },
        thinking: None,
        reasoning_effort: None,
        enable_thinking: None,
        reasoning: None,
        chat_template_kwargs: None,
        temperature,
        tool_choice: None,
    }
}

/// openai 兼容思考参数:按 `thinkingFormat` 构造 thinking / reasoning_effort /
/// reasoning / enable_thinking / chat_template_kwargs。
/// 关闭档(reasoning_effort 为 None)时:`thinkingLevelMap.off` 显式为 null → 什么都不发;
/// 缺失或为字符串 → 发对应关闭值。
fn apply_thinking_config(
    request: &mut ChatRequest,
    model: &ModelConfig,
    reasoning_effort: Option<&str>,
) {
    if !model.reasoning {
        return;
    }

    // thinkingLevelMap 命中值;缺省回退到级别名
    let mapped = |level: &str| -> String {
        model
            .thinking_level_map
            .as_ref()
            .and_then(|m| m.get(level))
            .and_then(|v| v.as_deref())
            .unwrap_or(level)
            .to_string()
    };
    // off 的显式值:None=未声明,Some(None)=显式不支持,Some(Some(v))=关闭值
    let off_value = || model.thinking_level_map.as_ref().and_then(|m| m.get("off"));
    // 缺失或字符串时允许显式关闭
    let can_disable = || off_value().is_none_or(|v| v.is_some());

    match model.thinking_format.as_str() {
        // zai/GLM:thinking 对象 + 可选 reasoning_effort
        "zai" => {
            request.thinking = Some(if reasoning_effort.is_some() {
                json!({ "type": "enabled", "clear_thinking": false })
            } else {
                json!({ "type": "disabled" })
            });
            if let Some(level) = reasoning_effort
                && model.supports_reasoning_effort
            {
                request.reasoning_effort = Some(mapped(level));
            }
        }
        // qwen/DashScope:enable_thinking 布尔 + 可选 reasoning_effort
        "qwen" => {
            request.enable_thinking = Some(reasoning_effort.is_some());
            if let Some(level) = reasoning_effort
                && model.supports_reasoning_effort
            {
                request.reasoning_effort = Some(mapped(level));
            }
        }
        "qwen-chat-template" => {
            request.chat_template_kwargs = Some(json!({
                "enable_thinking": reasoning_effort.is_some(),
                "preserve_thinking": true,
            }));
        }
        "chat-template" => {
            // 由 model_overrides/自定义模板处理;此处不注入
        }
        // baseten:仅 reasoning_effort(关闭时用 off 映射)
        "baseten" => {
            if model.supports_reasoning_effort {
                request.reasoning_effort = match reasoning_effort {
                    Some(level) => Some(mapped(level)),
                    None => off_value().and_then(|v| v.clone()),
                };
            }
        }
        // deepseek:thinking 对象 + reasoning_effort
        "deepseek" => {
            if reasoning_effort.is_some() {
                request.thinking = Some(json!({ "type": "enabled" }));
            } else if can_disable() {
                request.thinking = Some(json!({ "type": "disabled" }));
            }
            if let Some(level) = reasoning_effort
                && model.supports_reasoning_effort
            {
                request.reasoning_effort = Some(mapped(level));
            }
        }
        // openrouter:reasoning.effort(关闭回退 "none")
        "openrouter" => {
            if let Some(level) = reasoning_effort {
                request.reasoning = Some(json!({ "effort": mapped(level) }));
            } else if can_disable() {
                let off = off_value()
                    .and_then(|v| v.clone())
                    .unwrap_or_else(|| "none".to_string());
                request.reasoning = Some(json!({ "effort": off }));
            }
        }
        // ant-ling:仅显式映射的启用级别才发 reasoning.effort
        "ant-ling" => {
            if let Some(level) = reasoning_effort
                && let Some(Some(effort)) =
                    model.thinking_level_map.as_ref().and_then(|m| m.get(level))
            {
                request.reasoning = Some(json!({ "effort": effort.clone() }));
            }
        }
        // together:reasoning.enabled + 可选 reasoning_effort
        "together" => {
            request.reasoning = Some(json!({ "enabled": reasoning_effort.is_some() }));
            if let Some(level) = reasoning_effort
                && model.supports_reasoning_effort
            {
                request.reasoning_effort = Some(mapped(level));
            }
        }
        // string-thinking:thinking 为字符串
        "string-thinking" => {
            if let Some(level) = reasoning_effort {
                request.thinking = Some(json!(mapped(level)));
            } else if can_disable() {
                let off = off_value()
                    .and_then(|v| v.clone())
                    .unwrap_or_else(|| "none".to_string());
                request.thinking = Some(json!(off));
            }
        }
        // 默认 openai 风格:reasoning_effort(关闭时用 off 映射的字符串值)
        _ => {
            if !model.supports_reasoning_effort {
                return;
            }
            if let Some(level) = reasoning_effort {
                request.reasoning_effort = Some(mapped(level));
            } else if let Some(Some(off)) = off_value() {
                request.reasoning_effort = Some(off.clone());
            }
        }
    }
}

/// 序列化请求;max_tokens 字段名由模型兼容配置决定(max_completion_tokens / max_tokens)。
///
/// `level_sampling_params` 是当前 thinking level 的 `samplingParamsByThinkingLevel`
/// 覆盖项([`ModelConfig::sampling_params_for_level`]),与模型目录 `samplingParams` 不同:
/// 后写覆盖请求体里已有的同名键。
fn serialize_request_body(
    request: &ChatRequest,
    model: &ModelConfig,
    level_sampling_params: Option<&Value>,
) -> Result<String> {
    let mut v = serde_json::to_value(request).map_err(|source| Error::Json {
        context: "failed to serialize request".to_string(),
        source,
    })?;
    if let Some(mt) = request.max_tokens {
        v[&model.max_tokens_field] = json!(mt);
    }

    // samplingParams 全键合并进请求体(在 pi 自设字段之后,
    // 即合并只补默认未设的键,不覆盖显式 temperature/max_tokens/thinking 等)
    if let Some(sp) = &model.sampling_params
        && let Some(obj) = sp.as_object()
    {
        for (k, val) in obj {
            if !v.as_object().is_some_and(|o| o.contains_key(k)) {
                v[k] = val.clone();
            }
        }
    }

    // compat 的 routing 发到请求体 `provider` 字段
    if let Some(r) = &model.provider_routing
        && !v.get("provider").is_some()
    {
        v["provider"] = r.clone();
    }

    // 默认请求带 store=false(避免 store 语义差异)
    if model.supports_store && !v.get("store").is_some() {
        v["store"] = json!(false);
    }

    // 级别覆盖最后写
    if let Some(obj) = v.as_object_mut() {
        core::provider::apply_level_sampling_params(obj, level_sampling_params);
    }

    serde_json::to_string(&v).map_err(|source| Error::Json {
        context: "failed to serialize request".to_string(),
        source,
    })
}

/// 发送请求;非 2xx 时提取响应体并返回 ProviderStatus 错误
async fn send_completion_request(
    client: &reqwest::Client,
    model: &ModelConfig,
    body: String,
) -> Result<reqwest::Response> {
    let req = client
        .post(model_endpoint(model))
        .header("Content-Type", "application/json")
        .bearer_auth(&model.api_key)
        .headers(provider_extra_headers(model));

    let response = req.body(body).send().await.map_err(Error::from)?;

    let status = response.status();
    if !status.is_success() {
        let text = response.text().await.unwrap_or_default();
        return Err(Error::ProviderStatus {
            status,
            body: truncate_for_error(&text),
        });
    }
    Ok(response)
}

/// 追加 chunk 并按 \n 切出完整 SSE 行;返回 (完整行, 未完成的尾部字节)
fn split_sse_lines(buf: &mut Vec<u8>, chunk: &[u8]) -> (Vec<String>, Vec<u8>) {
    buf.extend_from_slice(chunk);
    let mut lines: Vec<String> = Vec::new();
    let mut start = 0usize;
    for i in 0..buf.len() {
        if buf[i] == b'\n' {
            lines.push(String::from_utf8_lossy(&buf[start..i]).to_string());
            start = i + 1;
        }
    }
    let carry = buf[start..].to_vec();
    (lines, carry)
}

/// 流式累积状态:文本 / 思考 / tool call / 元信息
struct StreamState {
    /// 累积的助手可见文本(正文 delta 依次拼接)。
    assistant_text: String,
    /// 累积的思考/推理文本(reasoning delta 依次拼接)。
    thinking_text: String,
    /// 归一化后的停止原因(stop/length/toolUse/error)。
    stop_reason: Option<String>,
    /// 端点原始 finish_reason,仅供调试保留,当前未被读取。
    _raw_stop_reason: Option<String>,
    /// finish_reason 映射出的错误说明;正常结束时为 None。
    error_message: Option<String>,
    /// 流式累计的用量;端点未返回 usage 时为 None。
    usage: Option<Usage>,
    /// 响应 id(首个 chunk 的 id),用于追踪一次请求。
    response_id: Option<String>,
    /// 端点实际返回的模型名;与请求相同或未给出时为 None。
    response_model: Option<String>,
    tool_calls: Vec<StreamingToolCall>, // 按 index 排序
    /// tool call id → `tool_calls` 下标,用于按 id 归位后续 delta。
    tool_calls_by_id: HashMap<String, usize>,
    // 流式事件状态
    /// 文本块在内容块序列中的下标;未产生文本时为 None。
    text_index: Option<usize>,
    /// 思考块在内容块序列中的下标;未产生思考时为 None。
    thinking_index: Option<usize>,
    /// `tool_calls` 下标 → 内容块下标,供流式事件定位。
    toolcall_indices: HashMap<usize, usize>,
    /// 下一个可用内容块下标,按内容首次出现的顺序递增分配。
    next_content_index: usize,
}

impl StreamState {
    /// 建一个空的流式状态(无文本、无思考、无工具调用、无 usage)。
    fn new() -> Self {
        Self {
            assistant_text: String::new(),
            thinking_text: String::new(),
            stop_reason: None,
            _raw_stop_reason: None,
            error_message: None,
            usage: None,
            response_id: None,
            response_model: None,
            tool_calls: Vec::new(),
            tool_calls_by_id: HashMap::new(),
            text_index: None,
            thinking_index: None,
            toolcall_indices: HashMap::new(),
            next_content_index: 0,
        }
    }

    /// 处理一个 SSE data 块(error / 元信息 / usage / choice delta)
    fn handle_chunk(
        &mut self,
        chunk_val: &Value,
        model: &ModelConfig,
        on_event: &mut Option<&mut PartialTranslator<'_>>,
    ) -> Result<()> {
        // error 字段(OpenAI 错误格式)
        if let Some(err) = chunk_val.get("error") {
            let message = err
                .get("message")
                .and_then(|m| m.as_str())
                .unwrap_or("unknown error")
                .to_string();
            return Err(Error::ProviderMessage(message));
        }

        if let Some(id) = chunk_val.get("id").and_then(|v| v.as_str())
            && self.response_id.is_none()
        {
            self.response_id = Some(id.to_string());
        }

        if let Some(m) = chunk_val.get("model").and_then(|v| v.as_str())
            && self.response_model.is_none()
            && m != model.model_id
        {
            self.response_model = Some(m.to_string());
        }
        if let Some(u) = chunk_val.get("usage") {
            self.usage = parse_usage(u);
        }
        let choice = chunk_val
            .get("choices")
            .and_then(|c| c.as_array())
            .and_then(|a| a.first());
        let Some(choice) = choice else { return Ok(()) };

        if let Some(u) = choice.get("usage")
            && self.usage.is_none()
        {
            self.usage = parse_usage(u);
        }

        if let Some(fr) = choice.get("finish_reason").and_then(|v| v.as_str())
            && !fr.is_empty()
        {
            self._raw_stop_reason = Some(fr.to_string());
            let mapped = map_stop_reason(fr);
            self.stop_reason = Some(mapped.0.to_string());
            if let Some(em) = mapped.1 {
                self.error_message = Some(em.to_string());
            }
        }

        let delta = choice.get("delta");
        let Some(delta) = delta else { return Ok(()) };
        self.handle_delta(delta, on_event);
        Ok(())
    }

    /// 把 delta 分发给三类内容(文本 / 思考 / tool call)
    fn handle_delta(&mut self, delta: &Value, on_event: &mut Option<&mut PartialTranslator<'_>>) {
        self.handle_content_delta(delta, on_event);
        self.handle_reasoning_delta(delta, on_event);
        self.handle_tool_call_delta(delta, on_event);
    }

    /// 累积 `delta.content` 文本;首次出现时分配内容块下标并发出 TextStart。
    fn handle_content_delta(
        &mut self,
        delta: &Value,
        on_event: &mut Option<&mut PartialTranslator<'_>>,
    ) {
        let Some(content) = delta.get("content").and_then(|v| v.as_str()) else {
            return;
        };
        if content.is_empty() {
            return;
        }
        if self.text_index.is_none() {
            self.text_index = Some(self.next_content_index);
            self.next_content_index += 1;
            if let Some(f) = on_event.as_mut() {
                f.push(RawStreamEvent::TextStart {
                    content_index: self.text_index.unwrap(),
                });
            }
        }
        self.assistant_text.push_str(content);
        if let Some(f) = on_event.as_mut() {
            f.push(RawStreamEvent::TextDelta {
                content_index: self.text_index.unwrap(),
                delta: content.to_string(),
            });
        }
    }

    /// 累积 `delta` 里的思考字段(reasoning_content / reasoning / reasoning_text),
    /// 首次出现时分配内容块下标并发出 ThinkingStart。
    fn handle_reasoning_delta(
        &mut self,
        delta: &Value,
        on_event: &mut Option<&mut PartialTranslator<'_>>,
    ) {
        // reasoning 字段
        let mut found_reasoning: Option<&str> = None;
        for field in ["reasoning_content", "reasoning", "reasoning_text"] {
            if let Some(v) = delta.get(field).and_then(|v| v.as_str())
                && !v.is_empty()
            {
                found_reasoning = Some(field);
                break;
            }
        }
        let Some(field) = found_reasoning else { return };
        let d = delta.get(field).and_then(|v| v.as_str()).unwrap_or("");
        if d.is_empty() {
            return;
        }
        if self.thinking_index.is_none() {
            self.thinking_index = Some(self.next_content_index);
            self.next_content_index += 1;
            if let Some(f) = on_event.as_mut() {
                f.push(RawStreamEvent::ThinkingStart {
                    content_index: self.thinking_index.unwrap(),
                });
            }
        }
        self.thinking_text.push_str(d);
        if let Some(f) = on_event.as_mut() {
            f.push(RawStreamEvent::ThinkingDelta {
                content_index: self.thinking_index.unwrap(),
                delta: d.to_string(),
            });
        }
    }

    /// 按 delta 里的 index(缺失时回退用 id)归位并累积 tool call 的 id / 函数名 / 参数片段;
    /// 首次拿到参数时发出 ToolCallStart,后续片段发 ToolCallDelta。
    fn handle_tool_call_delta(
        &mut self,
        delta: &Value,
        on_event: &mut Option<&mut PartialTranslator<'_>>,
    ) {
        let Some(tool_calls_delta) = delta.get("tool_calls").and_then(|v| v.as_array()) else {
            return;
        };
        for tc in tool_calls_delta {
            let index = tc.get("index").and_then(|v| v.as_u64()).map(|i| i as usize);
            let id = tc.get("id").and_then(|v| v.as_str()).map(|s| s.to_string());
            let function = tc.get("function");
            let custom = tc.get("custom");
            let name = function
                .and_then(|f| f.get("name"))
                .and_then(|v| v.as_str())
                .or_else(|| custom.and_then(|c| c.get("name")).and_then(|v| v.as_str()))
                .map(|s| s.to_string());
            let args = function
                .and_then(|f| f.get("arguments"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            // grammar 约束工具的裸源码:`custom.input` 是**累积值**
            let custom_input = custom
                .and_then(|c| c.get("input"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            // 找到或创建 block
            let slot: Option<usize> = if let Some(i) = index {
                Some(i)
            } else {
                id.as_ref()
                    .and_then(|id| self.tool_calls_by_id.get(id))
                    .copied()
            };

            let position = match slot {
                Some(i) => {
                    while self.tool_calls.len() <= i {
                        self.tool_calls.push(StreamingToolCall {
                            index: self.tool_calls.len(),
                            id: None,
                            name: None,
                            arguments: String::new(),
                            grammar_input: None,
                            grammar_source: String::new(),
                            grammar_started: false,
                            grammar_closed: false,
                        });
                    }
                    i
                }
                None => {
                    let pos = self.tool_calls.len();
                    self.tool_calls.push(StreamingToolCall {
                        index: pos,
                        id: None,
                        name: None,
                        arguments: String::new(),
                        grammar_input: None,
                        grammar_source: String::new(),
                        grammar_started: false,
                        grammar_closed: false,
                    });
                    pos
                }
            };

            if let Some(id) = id
                && self.tool_calls[position].id.is_none()
            {
                self.tool_calls[position].id = Some(id.clone());
                self.tool_calls_by_id.insert(id, position);
            }
            if let Some(name) = name
                && self.tool_calls[position].name.is_none()
            {
                // grammar 工具(`custom` 形状):记录源码属性名(推不出时回退 `input`)
                if custom.is_some() && function.is_none() {
                    self.tool_calls[position].grammar_input = Some(
                        grammar_input_property_for_tool(&name)
                            .unwrap_or_else(|| "input".to_string()),
                    );
                }
                self.tool_calls[position].name = Some(name);
            }
            if let Some(args) = args {
                // 首次出现该 tool call 时发 toolcall_start
                if !self.toolcall_indices.contains_key(&position) {
                    let ci = self.next_content_index;
                    self.next_content_index += 1;
                    self.toolcall_indices.insert(position, ci);
                    if let Some(f) = on_event.as_mut() {
                        f.push(RawStreamEvent::ToolCallStart { content_index: ci });
                    }
                }
                self.tool_calls[position].arguments.push_str(&args);
                if let Some(f) = on_event.as_mut() {
                    f.push(RawStreamEvent::ToolCallDelta {
                        content_index: self.toolcall_indices[&position],
                        delta: args,
                    });
                }
            }
            // grammar 调用:把裸源码包成 `{"<prop>":"…"}` 的 JSON 参数,后续走同一条路
            if let Some(input) = custom_input {
                if !self.toolcall_indices.contains_key(&position) {
                    let ci = self.next_content_index;
                    self.next_content_index += 1;
                    self.toolcall_indices.insert(position, ci);
                    if let Some(f) = on_event.as_mut() {
                        f.push(RawStreamEvent::ToolCallStart { content_index: ci });
                    }
                }

                let property = self.tool_calls[position]
                    .grammar_input
                    .clone()
                    .unwrap_or_else(|| "input".to_string());

                if !self.tool_calls[position].grammar_started {
                    self.tool_calls[position].grammar_input = Some(property.clone());
                    self.tool_calls[position].grammar_started = true;
                    self.tool_calls[position].arguments.push_str(&format!(
                        "{{\"{}\":\"",
                        core::provider::escape_json_text(&property)
                    ));
                }

                let previous = std::mem::take(&mut self.tool_calls[position].grammar_source);

                // 累积值:只拼新增部分(模型不会回退已发过的前缀)
                let delta_text = input
                    .strip_prefix(previous.as_str())
                    .unwrap_or(input.as_str())
                    .to_string();

                if !delta_text.is_empty() {
                    let escaped = core::provider::escape_json_text(&delta_text);
                    self.tool_calls[position].arguments.push_str(&escaped);
                    self.tool_calls[position].grammar_source = input;
                    if let Some(f) = on_event.as_mut() {
                        f.push(RawStreamEvent::ToolCallDelta {
                            content_index: self.toolcall_indices[&position],
                            delta: escaped,
                        });
                    }
                }
            }
        }
    }

    /// 流结束后组装 assistant 消息与结果
    fn finish(
        mut self,
        model: &ModelConfig,
        on_event: &mut Option<&mut PartialTranslator<'_>>,
    ) -> Result<StreamResult> {
        // grammar 调用的 JSON 包装还可能没闭合:`{"<prop>":"…` → `…"}`
        for tc in self.tool_calls.iter_mut() {
            if tc.grammar_input.is_some() && !tc.grammar_closed {
                if tc.grammar_started {
                    tc.arguments.push_str("\"}");
                } else {
                    tc.arguments = "{}".to_string();
                }
                tc.grammar_closed = true;
            }
        }

        let mut content_blocks: Vec<ContentBlock> = Vec::new();
        if !self.thinking_text.trim().is_empty() {
            content_blocks.push(ContentBlock::Thinking {
                thinking: self.thinking_text.clone(),
                thinking_signature: Some("reasoning_content".to_string()),
                redacted: None,
            });
            if let Some(f) = on_event.as_mut()
                && let Some(ci) = self.thinking_index
            {
                f.push(RawStreamEvent::ThinkingEnd {
                    content_index: ci,
                    content: self.thinking_text.clone(),
                });
            }
        }
        if !self.assistant_text.is_empty() {
            content_blocks.push(ContentBlock::Text {
                text: self.assistant_text.clone(),
                text_signature: None,
            });
            if let Some(f) = on_event.as_mut()
                && let Some(ci) = self.text_index
            {
                f.push(RawStreamEvent::TextEnd {
                    content_index: ci,
                    content: self.assistant_text,
                });
            }
        }
        for tc in &self.tool_calls {
            let id = tc
                .id
                .clone()
                .unwrap_or_else(|| format!("call_{}", tc.index));
            let name = tc.name.clone().unwrap_or_default();
            let arguments: Value = parse_streaming_json(&tc.arguments);
            content_blocks.push(ContentBlock::ToolCall {
                id: id.clone(),
                name: name.clone(),
                arguments: arguments.clone(),
                thought_signature: None,
                namespace: None,
            });
            if let Some(f) = on_event.as_mut()
                && let Some(ci) = self.toolcall_indices.get(&tc.index)
            {
                f.push(RawStreamEvent::ToolCallEnd {
                    content_index: *ci,
                    id,
                    name,
                    arguments,
                });
            }
        }

        // 流结束时无 finish_reason 帧,按是否存在工具调用推断 stop/toolUse
        let stop_reason = self.stop_reason.or_else(|| {
            if model.supports_finish_reason {
                None
            } else if !self.tool_calls.is_empty() {
                Some("toolUse".to_string())
            } else {
                Some("stop".to_string())
            }
        });

        let mut usage = self.usage;
        if let Some(u) = usage.as_mut() {
            compute_cost(u, model.cost.as_ref());
        }

        let message = AgentMessage {
            role: "assistant".to_string(),
            thinking_level: None,
            content: content_blocks,
            tool_call_id: None,
            tool_name: None,
            is_error: false,
            stop_reason,
            error_message: self.error_message.clone(),
            model: self.response_model.or(Some(model.model_id.clone())),
            provider: Some(model.provider.clone()),
            api: Some("openai-completions".to_string()),
            usage: usage.clone(),
            response_model: None,
            response_id: None,
            diagnostics: None,
            deferred: None,
            raw_stop_reason: None,
            end_turn: None,
            timestamp: now_ms(),
            duration_ms: None,
            entry_id: None,
            details: None,
            citations: None,
        };

        Ok(StreamResult {
            message,
            usage,
            error_message: self.error_message,
        })
    }
}

/// 流式响应里按 index 归位、逐块累积参数的 tool call 缓冲。
#[derive(Debug, Clone)]
struct StreamingToolCall {
    /// 该 tool call 在本次响应中的序号(delta 里的 index)。
    index: usize,
    /// 调用 id;首个 delta 给出前为 None,缺失时回退成 `call_{index}`。
    id: Option<String>,
    /// 函数名;端点未给出时为 None。
    name: Option<String>,
    /// 逐块拼接的 JSON 参数字符串。
    arguments: String,
    /// grammar 约束调用的源码属性名;None = 普通 function 调用。
    grammar_input: Option<String>,
    /// grammar 调用已接收的裸源码(累积值,用来算增量)。
    grammar_source: String,
    /// grammar 调用的 JSON 包装前缀是否已写入(`{"<prop>":"`)。
    grammar_started: bool,
    /// grammar 调用的 JSON 包装是否已闭合(流末补 `"}`)。
    grammar_closed: bool,
}

/// 把上游 `finish_reason` 映射成内部 stopReason,并给出可选的错误说明。
/// 未知值一律归为 `error`,说明里带上原始 reason 便于排查。
fn map_stop_reason(reason: &str) -> (&'static str, Option<String>) {
    match reason {
        "stop" | "end" => ("stop", None),
        "length" => ("length", None),
        "function_call" | "tool_calls" => ("toolUse", None),
        "content_filter" => (
            "error",
            Some("Provider finish_reason: content_filter".to_string()),
        ),
        "network_error" => (
            "error",
            Some("Provider finish_reason: network_error".to_string()),
        ),
        other => ("error", Some(format!("Provider finish_reason: {}", other))),
    }
}

/// cached_tokens 是 cache-read,cache_write_tokens 单独映射;
/// input 需要扣掉 cacheRead 与 cacheWrite。
fn parse_usage(u: &Value) -> Option<Usage> {
    let prompt_tokens = u.get("prompt_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
    let cache_read = u
        .get("prompt_tokens_details")
        .and_then(|d| d.get("cached_tokens"))
        .and_then(|v| v.as_u64())
        .or_else(|| u.get("prompt_cache_hit_tokens").and_then(|v| v.as_u64()))
        .unwrap_or(0) as u32;
    let cache_write = u
        .get("prompt_tokens_details")
        .and_then(|d| d.get("cache_write_tokens"))
        .and_then(|v| v.as_u64())
        .unwrap_or(0) as u32;
    let output = u
        .get("completion_tokens")
        .and_then(|v| v.as_u64())
        .unwrap_or(0) as u32;
    let reasoning = u
        .pointer("/completion_tokens_details/reasoning_tokens")
        .and_then(|v| v.as_u64())
        .unwrap_or(0) as u32;
    let input = prompt_tokens
        .saturating_sub(cache_read)
        .saturating_sub(cache_write);
    let total_tokens = input + output + cache_read + cache_write;
    Some(Usage {
        input,
        output,
        cache_read,
        cache_write,
        cache_write_1h: None,
        reasoning: (reasoning > 0).then_some(reasoning),
        total_tokens,
        cost: Cost::default(),
    })
}

/// 本次请求应使用的 chat/completions 端点。
/// github-copilot 从 API key 的 `proxy-ep=` 段推导 API base(个人版/企业版实际端点由 token 决定),
/// 其余情况直接用 [`ModelConfig::endpoint`]。
fn model_endpoint(model: &ModelConfig) -> String {
    if model.provider != "github-copilot" {
        return model.endpoint();
    }

    // GitHub Copilot 专用:从 Copilot token 的 proxy-ep 推导 API base
    // 覆盖目录 base_url(个人版 / 企业版实际端点由 token 决定)。
    let base = model
        .api_key
        .split(';')
        .find_map(|seg| seg.strip_prefix("proxy-ep="))
        .map(|ep| {
            let ep = ep.trim();
            let host = if let Some(rest) = ep.strip_prefix("proxy.") {
                format!("api.{}", rest)
            } else {
                ep.to_string()
            };
            format!("https://{}", host)
        });

    match base {
        Some(b) => azure::with_api_version(
            model,
            format!("{}/chat/completions", b.trim_end_matches('/')),
        ),
        None => model.endpoint(),
    }
}

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

    fn copilot_model(token: &str) -> ModelConfig {
        ModelConfig {
            model_type: Default::default(),
            image_resize: crate::utils::image::ImageResizeLimits::default(),
            allowed_fallback_models: Vec::new(),
            provider: "github-copilot".into(),
            model_id: "claude-fable-5".into(),
            base_url: "https://api.individual.githubcopilot.com".into(),
            api_key: token.into(),
            api: "openai-completions".into(),
            output: Vec::new(),
            input: vec!["text".into(), "image".into()],
            reasoning: false,
            max_tokens: None,
            temperature: None,
            context_window: 200000,
            thinking_format: String::new(),
            supports_reasoning_effort: false,
            thinking_level_map: None,
            auth_header: false,
            supports_developer_role: false,
            requires_reasoning_content_on_assistant_messages: false,
            supports_usage_in_streaming: true,
            supports_store: true,
            supports_finish_reason: true,
            requires_assistant_after_tool_result: false,
            supports_strict_mode: false,
            send_session_affinity_headers: None,
            session_affinity_format: None,
            max_tokens_field: "max_tokens".into(),
            cost: None,
            session_id: None,
            max_retry_delay_ms: None,
            sampling_params: None,
            sampling_params_by_thinking_level: None,
            provider_routing: None,
            thinking_budgets: None,
        }
    }

    #[test]
    fn convert_messages_custom_role_becomes_user() {
        // 对齐 pi convertToLlm:custom 角色转 user(扩展注入消息进上下文)
        let m = model(false, None);
        let mut custom = AgentMessage::user_text("injected");
        custom.role = "custom".to_string();
        let msgs = convert_messages(&[custom], "", &m);
        assert_eq!(msgs[0]["role"], "user");
        assert_eq!(msgs[0]["content"][0]["text"], "injected");
    }

    #[test]
    fn trailing_orphan_tool_call_gets_synthetic_result() {
        // 对齐 pi transformMessages / responses 转换器:列表尾部未配对的 tool_calls
        // 补 synthetic tool result,否则上游报
        // "assistant message with 'tool_calls' must be followed by tool messages..."。
        let m = model(false, None);
        let mut a = AgentMessage::user_text("");
        a.role = "assistant".into();
        a.content = vec![ContentBlock::ToolCall {
            id: "call_1".into(),
            name: "read".into(),
            arguments: json!({}),
            thought_signature: None,
            namespace: None,
        }];
        let out = convert_messages(&[a], "", &m);
        assert_eq!(out[0]["role"], "assistant");
        assert_eq!(out[0]["tool_calls"][0]["id"], "call_1");
        // 尾部孤儿 → synthetic tool 消息
        assert_eq!(out[1]["role"], "tool");
        assert_eq!(out[1]["tool_call_id"], "call_1");
        assert_eq!(out[1]["content"], "No result provided");
    }

    #[test]
    fn trailing_partial_tool_batch_backfills_missing_results() {
        // 中止/崩溃恢复的历史:assistant(A,B) + tool(A),以 tool 消息结尾。
        // 缺 B 的结果必须补 synthetic,否则上游报 "insufficient tool messages following tool_calls message"。
        let m = model(false, None);
        let mut a = AgentMessage::user_text("");
        a.role = "assistant".into();
        a.content = vec![
            ContentBlock::ToolCall {
                id: "call_A".into(),
                name: "read".into(),
                arguments: json!({}),
                thought_signature: None,
                namespace: None,
            },
            ContentBlock::ToolCall {
                id: "call_B".into(),
                name: "bash".into(),
                arguments: json!({}),
                thought_signature: None,
                namespace: None,
            },
        ];
        let mut tr = AgentMessage::user_text("");
        tr.role = "toolResult".into();
        tr.tool_call_id = Some("call_A".into());
        tr.content = vec![ContentBlock::Text {
            text: "result A".into(),
            text_signature: None,
        }];
        let out = convert_messages(&[a, tr], "", &m);
        assert_eq!(out.len(), 3);
        assert_eq!(out[0]["role"], "assistant");
        assert_eq!(out[1]["role"], "tool");
        assert_eq!(out[1]["tool_call_id"], "call_A");
        assert_eq!(out[1]["content"], "result A");
        assert_eq!(out[2]["role"], "tool");
        assert_eq!(out[2]["tool_call_id"], "call_B");
        assert_eq!(out[2]["content"], "No result provided");
    }

    #[test]
    fn orphan_tool_result_is_dropped() {
        // 历史中带 tool_calls 的 assistant 被扩展 transform_context 删除(或压缩保留尾部
        // 从批次中间截断)后,会留下孤儿 tool 结果。直接发送会触发上游 400:
        // "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'"。
        // 转换层必须丢弃它。
        let m = model(false, None);
        let mut user = AgentMessage::user_text("go");
        user.role = "user".into();
        let mut orphan = AgentMessage::user_text("");
        orphan.role = "toolResult".into();
        orphan.tool_call_id = Some("call_missing".into());
        orphan.content = vec![ContentBlock::Text {
            text: "orphan result".into(),
            text_signature: None,
        }];
        let out = convert_messages(&[user, orphan], "", &m);
        assert_eq!(out.len(), 1, "孤儿 tool 结果必须被丢弃: {out:?}");
        assert_eq!(out[0]["role"], "user");
    }

    #[test]
    fn answered_tool_result_still_emitted_once() {
        // pending 在收到结果时即移除:重复结果不会重复发送(去重不依赖外部集合)。
        let m = model(false, None);
        let mut a = AgentMessage::user_text("");
        a.role = "assistant".into();
        a.content = vec![ContentBlock::ToolCall {
            id: "call_1".into(),
            name: "read".into(),
            arguments: json!({}),
            thought_signature: None,
            namespace: None,
        }];
        let mk = |text: &str| {
            let mut tr = AgentMessage::user_text("");
            tr.role = "toolResult".into();
            tr.tool_call_id = Some("call_1".into());
            tr.content = vec![ContentBlock::Text {
                text: text.into(),
                text_signature: None,
            }];
            tr
        };
        let out = convert_messages(&[a, mk("first"), mk("second")], "", &m);
        assert_eq!(out.len(), 2);
        assert_eq!(out[1]["role"], "tool");
        assert_eq!(out[1]["content"], "first");
    }

    /// assistant 消息(带若干 tool_calls)
    fn assistant_with_calls(ids: &[&str]) -> AgentMessage {
        let mut a = AgentMessage::user_text("");
        a.role = "assistant".into();
        a.content = ids
            .iter()
            .map(|id| ContentBlock::ToolCall {
                id: (*id).into(),
                name: "read".into(),
                arguments: json!({}),
                thought_signature: None,
                namespace: None,
            })
            .collect();
        a
    }

    /// toolResult 消息(文本 + 可选图片)
    fn tool_result_with_image(id: &str, text: &str, with_image: bool) -> AgentMessage {
        let mut tr = AgentMessage::user_text("");
        tr.role = "toolResult".into();
        tr.tool_call_id = Some(id.into());
        tr.content = vec![ContentBlock::Text {
            text: text.into(),
            text_signature: None,
        }];
        if with_image {
            tr.content.push(ContentBlock::Image {
                data: "AA==".into(),
                mime_type: "image/png".into(),
            });
        }
        tr
    }

    /// 回归:一批 tool_calls 里只有部分结果带图片时,图片 user 消息必须排在**全部**
    /// tool 消息之后。插在中间会被上游拒绝:
    /// `provider returned 400 Bad Request: {"model":"deepseek-v4.1-flash"}`,
    /// 而且这个顺序一旦落库,整个会话之后每次请求都会 400。
    #[test]
    fn tool_result_images_wait_until_batch_end() {
        let m = model(true, None);
        let out = convert_messages(
            &[
                assistant_with_calls(&["call_read", "call_bash"]),
                tool_result_with_image("call_read", "Read image file [image/png]", true),
                tool_result_with_image("call_bash", "total 0", false),
                AgentMessage::user_text("继续"),
            ],
            "",
            &m,
        );
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(
            roles,
            vec!["assistant", "tool", "tool", "user", "user"],
            "图片消息不得插在两个 tool 消息之间: {out:?}"
        );
        assert_eq!(out[1]["tool_call_id"], "call_read");
        assert_eq!(out[2]["tool_call_id"], "call_bash");
        assert_eq!(
            out[3]["content"][0]["text"],
            "Attached image(s) from tool result:"
        );
        assert_eq!(out[3]["content"][1]["type"], "image_url");
        assert_eq!(out[4]["content"][0]["text"], "继续");
    }

    /// 批次被列表末尾截断时同样要等全部 tool 消息发完。
    #[test]
    fn tool_result_image_at_list_end_follows_all_tool_messages() {
        let m = model(true, None);
        let out = convert_messages(
            &[
                assistant_with_calls(&["call_read", "call_bash"]),
                tool_result_with_image("call_read", "Read image file [image/png]", true),
                tool_result_with_image("call_bash", "total 0", false),
            ],
            "",
            &m,
        );
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(roles, vec!["assistant", "tool", "tool", "user"], "{out:?}");
    }

    /// 只有一个 tool 结果(最常见情况):tool 后紧跟图片,顺序与旧行为一致。
    #[test]
    fn single_tool_result_image_follows_tool_message() {
        let m = model(true, None);
        let out = convert_messages(
            &[
                assistant_with_calls(&["call_read"]),
                tool_result_with_image("call_read", "Read image file [image/png]", true),
            ],
            "",
            &m,
        );
        assert_eq!(out.len(), 3, "{out:?}");
        assert_eq!(out[0]["role"], "assistant");
        assert_eq!(out[1]["role"], "tool");
        assert_eq!(out[1]["content"], "Read image file [image/png]");
        assert_eq!(out[2]["role"], "user");
        assert_eq!(out[2]["content"][1]["type"], "image_url");
        assert_eq!(
            out[2]["content"][1]["image_url"]["url"],
            "data:image/png;base64,AA=="
        );
    }

    /// 一批里多个工具结果都带图片 → 合并成一条 user 消息。
    #[test]
    fn multiple_tool_result_images_merge_into_one_user_message() {
        let m = model(true, None);
        let out = convert_messages(
            &[
                assistant_with_calls(&["call_a", "call_b"]),
                tool_result_with_image("call_a", "a", true),
                tool_result_with_image("call_b", "b", true),
            ],
            "",
            &m,
        );
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(roles, vec!["assistant", "tool", "tool", "user"], "{out:?}");
        let parts = out[3]["content"].as_array().unwrap();
        assert_eq!(parts.len(), 3, "标题 + 两张图: {out:?}");
    }

    /// requiresAssistantAfterToolResult:空 assistant 也只在批次末尾补一次。
    #[test]
    fn empty_assistant_appended_once_per_tool_batch() {
        let mut m = model(true, None);
        m.requires_assistant_after_tool_result = true;
        let out = convert_messages(
            &[
                assistant_with_calls(&["call_read", "call_bash"]),
                tool_result_with_image("call_read", "Read image file [image/png]", true),
                tool_result_with_image("call_bash", "total 0", false),
            ],
            "",
            &m,
        );
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(
            roles,
            vec!["assistant", "tool", "tool", "user", "assistant"],
            "{out:?}"
        );
        assert!(out[4]["content"].as_array().unwrap().is_empty());
    }

    #[test]
    fn tool_choice_serialized_when_set() {
        // 对齐 pi SimpleStreamOptions.toolChoice:\"auto\" | \"none\"
        let m = model(false, None);
        let mut request = build_chat_request(&m, &[], &[], None, None);
        let body = serialize_request_body(&request, &m, None).unwrap();
        let v: Value = serde_json::from_str(&body).unwrap();
        assert!(
            v.get("tool_choice").is_none(),
            "未设置时不含 tool_choice: {body}"
        );
        request.tool_choice = Some("none".to_string());
        let body2 = serialize_request_body(&request, &m, None).unwrap();
        let v2: Value = serde_json::from_str(&body2).unwrap();
        assert_eq!(v2["tool_choice"], "none");
    }

    #[test]
    fn model_endpoint_forwards_proxy_ep() {
        // 个人版 token:proxy-ep=proxy.individual... → api.individual...
        let m = copilot_model("tid=1;exp=9;proxy-ep=proxy.individual.githubcopilot.com;sku=1");
        assert_eq!(
            model_endpoint(&m),
            "https://api.individual.githubcopilot.com/chat/completions"
        );
        // 无 proxy-ep → 回退目录 base_url
        let m2 = copilot_model("tid=1");
        assert_eq!(
            model_endpoint(&m2),
            "https://api.individual.githubcopilot.com/chat/completions"
        );
        // 非 copilot provider 不受影响
        let m3 = model(false, None);
        assert_eq!(
            model_endpoint(&m3),
            "https://example.com/v1/chat/completions"
        );
    }

    #[test]
    fn provider_extra_headers_follows_provider() {
        // copilot provider:模型条目定义有 IDE 网关头
        let m = copilot_model("x");
        let h = crate::core::provider::provider_extra_headers(&m);
        assert_eq!(
            h.get("User-Agent").and_then(|v| v.to_str().ok()),
            Some("GitHubCopilotChat/0.35.0")
        );
        assert!(h.get("Copilot-Integration-Id").is_some());
        // 未定义 headers 的 provider:空
        let m2 = model(false, None);
        let h2 = crate::core::provider::provider_extra_headers(&m2);
        assert!(h2.is_empty());
    }

    fn model(supports_image: bool, cost: Option<Value>) -> ModelConfig {
        ModelConfig {
            model_type: Default::default(),
            image_resize: crate::utils::image::ImageResizeLimits::default(),
            allowed_fallback_models: Vec::new(),
            provider: "test".into(),
            model_id: "m".into(),
            base_url: "https://example.com/v1".into(),
            api_key: String::new(),
            api: "openai-completions".into(),
            output: Vec::new(),
            input: if supports_image {
                vec!["text".into(), "image".into()]
            } else {
                vec!["text".into()]
            },
            reasoning: false,
            max_tokens: None,
            temperature: None,
            context_window: 1000,
            thinking_format: String::new(),
            supports_reasoning_effort: false,
            thinking_level_map: None,
            auth_header: false,
            supports_developer_role: false,
            requires_reasoning_content_on_assistant_messages: false,

            supports_usage_in_streaming: true,

            supports_store: true,

            supports_finish_reason: true,

            requires_assistant_after_tool_result: false,
            max_tokens_field: "max_tokens".into(),
            supports_strict_mode: false,
            send_session_affinity_headers: None,
            session_affinity_format: None,
            cost,
            session_id: None,
            max_retry_delay_ms: None,
            sampling_params: None,
            sampling_params_by_thinking_level: None,
            provider_routing: None,
            thinking_budgets: None,
        }
    }
    #[test]
    fn non_vision_model_replaces_images() {
        let m = model(false, None);
        let msg = AgentMessage {
            role: "user".into(),
            thinking_level: None,
            content: vec![
                ContentBlock::Text {
                    text: "look".into(),
                    text_signature: None,
                },
                ContentBlock::Image {
                    data: "AA==".into(),
                    mime_type: "image/png".into(),
                },
                ContentBlock::Image {
                    data: "AA==".into(),
                    mime_type: "image/png".into(),
                },
            ],
            ..AgentMessage::user_text("")
        };
        let out = convert_messages(&[msg], "", &m);
        let content = out[0].get("content").unwrap();
        let parts = content.as_array().unwrap();
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0]["text"], "look");
        assert_eq!(
            parts[1]["text"],
            "(image omitted: model does not support images)"
        );
    }

    #[test]
    fn image_only_user_message_omits_empty_text_part() {
        // 回归 #9797:带空 text block 的纯图片 user 消息不得带空 text part。
        let m = model(true, None);
        let msg = AgentMessage {
            role: "user".into(),
            thinking_level: None,
            content: vec![
                ContentBlock::Text {
                    text: String::new(),
                    text_signature: None,
                },
                ContentBlock::Image {
                    data: "AA==".into(),
                    mime_type: "image/png".into(),
                },
            ],
            ..AgentMessage::user_text("")
        };
        let out = convert_messages(&[msg], "", &m);
        let parts = out[0]["content"].as_array().unwrap();
        assert_eq!(parts.len(), 1, "空 text part 必须被省略: {out:?}");
        assert_eq!(parts[0]["type"], "image_url");
    }

    #[test]
    fn image_block_serializes_pi_compatible() {
        let msg = AgentMessage {
            role: "user".into(),
            thinking_level: None,
            content: vec![ContentBlock::Image {
                data: "AA==".into(),
                mime_type: "image/png".into(),
            }],
            ..AgentMessage::user_text("")
        };
        let v = serde_json::to_value(&msg).unwrap();
        assert_eq!(v["content"][0]["mimeType"], "image/png");
    }

    #[test]
    fn summary_roles_become_user_context() {
        let m = model(false, None);
        let mut c = AgentMessage::user_text("sum");
        c.role = "compactionSummary".into();
        let out = convert_messages(&[c], "", &m);
        let text = out[0]["content"][0]["text"].as_str().unwrap();
        assert!(text.starts_with("The conversation history before this point"));
        assert!(text.contains("sum"));
    }

    #[test]
    fn vision_model_emits_image_urls() {
        let m = model(true, None);
        let msg = AgentMessage {
            role: "user".into(),
            thinking_level: None,
            content: vec![ContentBlock::Image {
                data: "AA==".into(),
                mime_type: "image/png".into(),
            }],
            ..AgentMessage::user_text("")
        };
        let out = convert_messages(&[msg], "", &m);
        let part = &out[0]["content"][0];
        assert_eq!(part["type"], "image_url");
        assert_eq!(part["image_url"]["url"], "data:image/png;base64,AA==");
    }

    #[test]
    fn serialize_merges_sampling_params_and_routing() {
        let mut m = copilot_model("x");
        m.sampling_params = Some(json!({ "top_p": 0.9, "min_p": 0.05 }));
        m.provider_routing = Some(json!({ "order": ["openai"], "allow_fallbacks": false }));
        let req = ChatRequest {
            model: m.model_id.clone(),
            messages: vec![json!({ "role": "user", "content": "hi" })],
            stream: true,
            max_tokens: None,
            stream_options: None,
            tools: None,
            thinking: None,
            reasoning_effort: None,
            enable_thinking: None,
            reasoning: None,
            chat_template_kwargs: None,
            temperature: Some(0.7),
            tool_choice: None,
        };
        let body = serialize_request_body(&req, &m, None).unwrap();
        let v: serde_json::Value = serde_json::from_str(&body).unwrap();
        // samplingParams 全键透传(不覆盖显式 temperature)
        assert_eq!(v["top_p"], 0.9);
        assert_eq!(v["min_p"], 0.05);
        assert_eq!(v["temperature"], 0.7);
        // routing → provider 字段(对齐 pi openRouterRouting)
        assert_eq!(v["provider"]["order"][0], "openai");
        assert_eq!(v["provider"]["allow_fallbacks"], false);
    }

    /// 级别覆盖项按 pi 语义后写覆盖(含 temperature),目录 `samplingParams` 只补空键。
    #[test]
    fn level_sampling_params_override_named_fields() {
        let mut m = copilot_model("x");
        m.sampling_params = Some(json!({ "temperature": 1.0, "top_p": 0.95 }));
        let req = ChatRequest {
            model: m.model_id.clone(),
            messages: vec![json!({ "role": "user", "content": "hi" })],
            stream: true,
            max_tokens: None,
            stream_options: None,
            tools: None,
            thinking: None,
            reasoning_effort: None,
            enable_thinking: None,
            reasoning: None,
            chat_template_kwargs: None,
            temperature: Some(1.0),
            tool_choice: None,
        };
        let level = json!({ "temperature": 0.6, "top_k": 64 });
        let body = serialize_request_body(&req, &m, Some(&level)).unwrap();
        let v: Value = serde_json::from_str(&body).unwrap();
        assert_eq!(v["temperature"], 0.6, "级别覆盖应压过显式 temperature");
        assert_eq!(v["top_k"], 64);
        assert_eq!(v["top_p"], 0.95, "目录 samplingParams 仍按补空键合并");

        // 没有级别覆盖项时行为与旧版一致
        let body = serialize_request_body(&req, &m, None).unwrap();
        let v: Value = serde_json::from_str(&body).unwrap();
        assert_eq!(v["temperature"], 1.0);
        assert!(v.get("top_k").is_none());
    }

    #[test]
    fn usage_in_streaming_omits_include_usage_when_unsupported() {
        let mut m = copilot_model("x");
        m.supports_usage_in_streaming = false;
        let req = build_chat_request(&m, &[], &[], None, Some(8));
        assert!(
            req.stream_options.is_none(),
            "supportsUsageInStreaming=false 不发 include_usage"
        );
        let m2 = copilot_model("x");
        let req2 = build_chat_request(&m2, &[], &[], None, Some(8));
        assert!(
            req2.stream_options.is_some(),
            "supportsUsageInStreaming=true 默认发 include_usage"
        );
    }
    #[test]
    fn thinking_format_variants_serialize() {
        let with_map = |mut m: ModelConfig, pairs: &[(&str, Option<&str>)]| {
            m.thinking_level_map = Some(
                pairs
                    .iter()
                    .map(|(k, v)| (k.to_string(), v.map(|s| s.to_string())))
                    .collect(),
            );
            m
        };

        // qwen:enable_thinking 布尔;supportsReasoningEffort 时附 reasoning_effort
        let mut m = copilot_model("x");
        m.reasoning = true;
        m.thinking_format = "qwen".into();
        m.supports_reasoning_effort = true;
        let mut req = build_chat_request(&m, &[], &[], None, None);
        apply_thinking_config(&mut req, &m, Some("high"));
        assert_eq!(req.enable_thinking, Some(true));
        assert_eq!(req.reasoning_effort.as_deref(), Some("high"));
        let body = serialize_request_body(&req, &m, None).unwrap();
        assert!(body.contains("\"enable_thinking\":true"), "{body}");

        // together:开启发 reasoning.enabled=true,关闭发 false(无 off 映射时不发 reasoning_effort)
        let mut m2 = copilot_model("x");
        m2.reasoning = true;
        m2.thinking_format = "together".into();
        let mut req2 = build_chat_request(&m2, &[], &[], None, None);
        apply_thinking_config(&mut req2, &m2, Some("medium"));
        assert_eq!(req2.reasoning, Some(json!({ "enabled": true })));
        let mut req2_off = build_chat_request(&m2, &[], &[], None, None);
        apply_thinking_config(&mut req2_off, &m2, None);
        assert_eq!(req2_off.reasoning, Some(json!({ "enabled": false })));

        // openrouter:关闭回退 effort="none"
        let mut m3 = copilot_model("x");
        m3.reasoning = true;
        m3.thinking_format = "openrouter".into();
        let mut req3 = build_chat_request(&m3, &[], &[], None, None);
        apply_thinking_config(&mut req3, &m3, None);
        assert_eq!(req3.reasoning, Some(json!({ "effort": "none" })));

        // deepseek:无 off 映射 → 关闭发 thinking.disabled
        let mut m4 = copilot_model("x");
        m4.reasoning = true;
        m4.thinking_format = "deepseek".into();
        let mut req4 = build_chat_request(&m4, &[], &[], None, None);
        apply_thinking_config(&mut req4, &m4, None);
        assert_eq!(req4.thinking, Some(json!({ "type": "disabled" })));

        // deepseek:off 显式为 null → 保持 API 默认(不发任何思考字段)
        let m5 = with_map(
            {
                let mut m = copilot_model("x");
                m.reasoning = true;
                m.thinking_format = "deepseek".into();
                m
            },
            &[("off", None), ("high", Some("high"))],
        );
        let mut req5 = build_chat_request(&m5, &[], &[], None, None);
        apply_thinking_config(&mut req5, &m5, None);
        assert_eq!(req5.thinking, None);
        assert_eq!(req5.reasoning_effort, None);

        // zai:关闭发 thinking.disabled
        let mut m6 = copilot_model("x");
        m6.reasoning = true;
        m6.thinking_format = "zai".into();
        let mut req6 = build_chat_request(&m6, &[], &[], None, None);
        apply_thinking_config(&mut req6, &m6, None);
        assert_eq!(req6.thinking, Some(json!({ "type": "disabled" })));

        // 通用 openai 风格(无 thinkingFormat):off 映射为字符串 → reasoning_effort
        // 这是 opencode-go/hy3、huggingface/tencent/Hy3 等模型 off 失效的修复点
        let m7 = with_map(
            {
                let mut m = copilot_model("x");
                m.reasoning = true;
                m.thinking_format = "openai".into();
                m.supports_reasoning_effort = true;
                m
            },
            &[("off", Some("none")), ("high", Some("high"))],
        );
        let mut req7 = build_chat_request(&m7, &[], &[], None, None);
        apply_thinking_config(&mut req7, &m7, None);
        assert_eq!(req7.reasoning_effort.as_deref(), Some("none"));
        let mut req7_on = build_chat_request(&m7, &[], &[], None, None);
        apply_thinking_config(&mut req7_on, &m7, Some("high"));
        assert_eq!(req7_on.reasoning_effort.as_deref(), Some("high"));

        // 通用分支:supportsReasoningEffort=false 时保持不动(如 github-copilot)
        let mut m8 = copilot_model("x");
        m8.reasoning = true;
        m8.thinking_format = "openai".into();
        m8.supports_reasoning_effort = false;
        let mut req8 = build_chat_request(&m8, &[], &[], None, None);
        apply_thinking_config(&mut req8, &m8, Some("high"));
        assert_eq!(req8.reasoning_effort, None);
    }
    #[test]
    fn strict_mode_follows_tool_constrained_sampling() {
        // 对齐 pi resolveJsonSchemaStrictSampling:仅声明 constrainedSampling 的工具 strict 化
        let defs = vec![
            (
                "read".to_string(),
                "Read".to_string(),
                json!({ "type": "object", "properties": { "path": { "type": "string" } } }),
            ),
            (
                "ls".to_string(),
                "Ls".to_string(),
                json!({ "type": "object", "properties": { "path": { "type": "string" } } }),
            ),
        ];
        // 不支持 strict 的 provider:不带 strict 字段、不 strict 化
        let plain = convert_tools_with_strict(&defs, false);
        assert!(plain[0]["function"].get("strict").is_none());
        assert!(plain[0]["function"]["parameters"].get("required").is_none());

        // 内置 read/bash/powershell/edit/write 默认声明 constrainedSampling → strict 化;
        // ls 未声明,保持原始 schema。
        let strict = convert_tools_with_strict(&defs, true);
        assert_eq!(strict[0]["function"]["strict"], true);
        assert_eq!(
            strict[0]["function"]["parameters"]["additionalProperties"],
            false
        );
        assert_eq!(strict[0]["function"]["parameters"]["required"][0], "path");
        assert_eq!(strict[1]["function"]["strict"], false);
        assert!(
            strict[1]["function"]["parameters"]
                .get("required")
                .is_none()
        );
    }

    #[test]
    fn finish_message_usage_cost_set_from_model_pricing_before_clone() {
        // 回归:cost 必须先结算再克隆进 message——否则底栏/session 恒为 $0
        let m = model(
            false,
            Some(json!({
                "input": 1.0,
                "output": 2.0,
                "cacheRead": 0.5,
                "cacheWrite": 0.25,
            })),
        );
        let mut st = StreamState::new();
        st.usage = Some(Usage {
            input: 1000,
            output: 2000,
            cache_read: 0,
            cache_write: 0,
            cache_write_1h: None,
            reasoning: None,
            total_tokens: 3000,
            cost: Cost::default(),
        });
        let res = st.finish(&m, &mut None).unwrap();
        // input 1000*1 + output 2000*2 = 0.005($/1M tokens)
        let u = res.message.usage.expect("usage attached to message");
        assert!(
            (u.cost.total - 0.005).abs() < 1e-12,
            "message cost.total={} expected 0.005",
            u.cost.total
        );
        assert!((res.usage.unwrap().cost.total - 0.005).abs() < 1e-12);
    }

    /// grammar 能力开启时,带语法约束的工具下发为 `custom` 工具;关闭时仍是 function。
    #[test]
    fn grammar_tool_conversion_emits_custom_tool() {
        let tools = vec![(
            "codemode".to_string(),
            "run js".to_string(),
            json!({
                "type": "object",
                "properties": { "code": { "type": "string" } },
                "required": ["code"],
                "x-prux-grammar": { "openai_lark": "start: SOURCE", "input": "code" }
            }),
        )];

        let with_grammar = convert_tools_with_capabilities(&tools, false, true);
        assert_eq!(with_grammar[0]["type"], "custom");
        assert_eq!(with_grammar[0]["custom"]["name"], "codemode");
        assert_eq!(with_grammar[0]["custom"]["format"]["type"], "grammar");
        assert_eq!(
            with_grammar[0]["custom"]["format"]["grammar"]["syntax"],
            "lark"
        );
        assert_eq!(
            with_grammar[0]["custom"]["format"]["grammar"]["definition"],
            "start: SOURCE"
        );
        // grammar 工具不带 parameters(输入由语法约束)
        assert!(with_grammar[0]["custom"].get("parameters").is_none());

        let without = convert_tools_with_capabilities(&tools, false, false);
        assert_eq!(without[0]["type"], "function");
        assert_eq!(without[0]["function"]["name"], "codemode");
    }

    /// grammar 调用的裸源码(累积的 `custom.input`)被包成 `{"<prop>":"…"}` 的 JSON 参数。
    #[test]
    fn grammar_custom_input_is_wrapped_into_json_arguments() {
        let m = model(false, None);
        let mut st = StreamState::new();
        let mut on_event = None;

        st.handle_tool_call_delta(
            &json!({"tool_calls": [{
                "index": 0,
                "id": "call_1",
                "type": "custom",
                "custom": { "name": "codemode", "input": "" }
            }]}),
            &mut on_event,
        );
        // 累积值:后续 delta 给完整输入,只应拼新增部分
        st.handle_tool_call_delta(
            &json!({"tool_calls": [{
                "index": 0,
                "type": "custom",
                "custom": { "name": "codemode", "input": "return \"a\";" }
            }]}),
            &mut on_event,
        );
        st.handle_tool_call_delta(
            &json!({"tool_calls": [{
                "index": 0,
                "type": "custom",
                "custom": { "name": "codemode", "input": "return \"a\";\n// 换行" }
            }]}),
            &mut on_event,
        );

        let res = st.finish(&m, &mut None).unwrap();
        let arguments = res
            .message
            .content
            .iter()
            .find_map(|b| match b {
                ContentBlock::ToolCall { arguments, .. } => Some(arguments.clone()),
                _ => None,
            })
            .expect("tool call block");
        // 未注册 `codemode` 扩展时属性名回退 `input`(与 pi 同)
        assert_eq!(arguments, json!({ "input": "return \"a\";\n// 换行" }));
    }

    /// 注册一个名为 `codemode` 的 grammar 工具(`code` 是唯一必填 string 参数),
    /// 供回放形状的测试使用;返回后由调用方 `unregister_extension` 清理。
    fn register_codemode_grammar_probe() {
        struct GrammarProbe;
        impl crate::core::extensions::Extension for GrammarProbe {
            fn name(&self) -> &str {
                "zz-codemode-replay-probe"
            }
            fn modes(&self) -> Vec<crate::core::extensions::ExtensionMode> {
                crate::core::extensions::ExtensionMode::ALL.to_vec()
            }
            fn tools(&self) -> Vec<crate::core::extensions::ExtensionTool> {
                let mut tool = crate::core::extensions::ExtensionTool::simple(
                    "codemode",
                    "run js",
                    json!({
                        "type": "object",
                        "properties": { "code": { "type": "string" } },
                        "required": ["code"]
                    }),
                    "run js",
                );
                tool.grammar_sampling = Some(crate::core::extensions::GrammarSampling {
                    openai_lark: Some("start: SOURCE".to_string()),
                    openai_regex: None,
                });
                vec![tool]
            }
        }
        crate::core::extensions::register_extension(GrammarProbe);
        assert_eq!(
            grammar_input_property_for_tool("codemode").as_deref(),
            Some("code"),
            "探针扩展未生效(模式/禁用状态异常)"
        );
    }

    /// 回放历史里的 grammar 工具调用时,`custom` 形状**只在模型声明支持 grammar 工具时**使用。
    ///
    /// 回归:曾经只看工具名,于是不支持该形状的模型(opencode-go 的 deepseek/mimo 等)收到
    /// `{"type":"custom",...}`,上游报
    /// `messages.N.tool_calls.0.type: Input should be 'function'`;这条历史一旦落库,
    /// 之后每次请求都 400,整个会话作废。
    #[test]
    fn grammar_tool_replay_follows_model_capability() {
        let _g = crate::test_support::AUTH_TEST_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let _ad = crate::test_support::AgentDirGuard::temp();
        register_codemode_grammar_probe();

        let mut msg = AgentMessage::user_text("");
        msg.role = "assistant".into();
        msg.stop_reason = Some("toolUse".into());
        msg.content = vec![ContentBlock::ToolCall {
            id: "call_1".into(),
            name: "codemode".into(),
            arguments: json!({ "code": "text(1)" }),
            thought_signature: None,
            namespace: None,
        }];

        // 不支持 grammar 的模型(不在目录里 → false):必须是 function 形状
        let plain = convert_messages(std::slice::from_ref(&msg), "", &model(false, None));
        let call = &plain[0]["tool_calls"][0];
        assert_eq!(call["type"], "function");
        assert_eq!(call["function"]["name"], "codemode");
        assert_eq!(call["function"]["arguments"], "{\"code\":\"text(1)\"}");

        // 目录里声明 supportsOpenAIGrammarTools 的模型:回放为 custom(裸源码)
        let mut grammar_model = model(false, None);
        grammar_model.provider = "opencode".into();
        grammar_model.model_id = "gpt-5".into();
        assert!(model_resolver::supports_openai_grammar_tools(
            &grammar_model.provider,
            &grammar_model.model_id
        ));
        let custom = convert_messages(&[msg], "", &grammar_model);
        let call = &custom[0]["tool_calls"][0];
        assert_eq!(call["type"], "custom");
        assert_eq!(call["custom"]["input"], "text(1)");

        crate::core::extensions::unregister_extension("zz-codemode-replay-probe");
    }

    /// 目录里声明 `compat.supportsMidConvoSystemMessages` 的 openai-completions 模型。
    fn mid_convo_model() -> ModelConfig {
        let mut m = model(false, None);
        m.provider = "moonshotai".into();
        m.model_id = "kimi-k2.6".into();
        m.api = "openai-completions".into();
        assert!(supports_mid_convo_system_messages(&m));
        m
    }

    /// tool_calls 的 assistant 消息,用于驱动「缓发」场景。
    fn tool_call_assistant(id: &str) -> AgentMessage {
        let mut a = AgentMessage::user_text("");
        a.role = "assistant".into();
        a.content = vec![ContentBlock::ToolCall {
            id: id.into(),
            name: "read".into(),
            arguments: json!({}),
            thought_signature: None,
            namespace: None,
        }];
        a
    }

    #[test]
    fn native_mid_convo_system_messages_are_held_until_the_next_non_tool_message() {
        // 系统消息缓发到 tool 结果之后,不打断 assistant(tool_calls) 与 tool 的配对
        let m = mid_convo_model();
        let mut note = AgentMessage::user_text("mid-run note");
        note.role = "system".into();
        let mut tr = AgentMessage::user_text("contents");
        tr.role = "toolResult".into();
        tr.tool_call_id = Some("call_1".into());
        let mut a2 = AgentMessage::user_text("");
        a2.role = "assistant".into();
        a2.content = vec![ContentBlock::Text {
            text: "done".into(),
            text_signature: None,
        }];

        let out = convert_messages(&[tool_call_assistant("call_1"), note, tr, a2], "", &m);
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(roles, vec!["assistant", "tool", "system", "assistant"]);
        assert_eq!(out[2]["content"], "mid-run note");
    }

    #[test]
    fn system_before_a_missing_tool_result_lands_after_the_synthetic_result() {
        let m = mid_convo_model();
        let mut note = AgentMessage::user_text("late note");
        note.role = "system".into();

        let out = convert_messages(&[tool_call_assistant("call_1"), note], "", &m);
        assert_eq!(out.len(), 3);
        assert_eq!(out[0]["role"], "assistant");
        assert_eq!(out[1]["role"], "tool");
        assert_eq!(out[1]["content"], "No result provided");
        assert_eq!(out[2]["role"], "system");
        assert_eq!(out[2]["content"], "late note");
    }

    #[test]
    fn held_system_message_is_flushed_before_the_user_turn() {
        // 对齐 pi:缓存的系统消息在边界先于 user 消息落位
        let m = mid_convo_model();
        let mut note = AgentMessage::user_text("note");
        note.role = "system".into();
        let mut u = AgentMessage::user_text("next");
        u.role = "user".into();

        let out = convert_messages(&[tool_call_assistant("call_1"), note, u], "", &m);
        let roles: Vec<&str> = out.iter().map(|v| v["role"].as_str().unwrap()).collect();
        assert_eq!(roles, vec!["assistant", "tool", "system", "user"]);
    }

    #[test]
    fn inline_system_messages_are_dropped_without_the_compat_flag() {
        // provider "test" 无目录条目:系统消息不进 wire(改由提示词承载)
        let m = model(false, None);
        let mut note = AgentMessage::user_text("note");
        note.role = "system".into();
        let out = convert_messages(&[AgentMessage::user_text("hi"), note], "", &m);
        assert!(!out.iter().any(|v| v["role"] == "system"), "{out:?}");
    }

    #[test]
    fn collapse_folds_mid_convo_system_messages_for_unsupported_models() {
        let mut note = AgentMessage::user_text("note");
        note.role = "system".into();
        let messages = vec![AgentMessage::user_text("hi"), note];
        assert_eq!(
            collapse_mid_convo_system_messages(&messages, "base", &model(false, None)),
            "base\n\nnote"
        );
        // 支持原生中途系统消息的模型不合并
        assert_eq!(
            collapse_mid_convo_system_messages(&messages, "base", &mid_convo_model()),
            "base"
        );
    }
}