rig-core 0.42.0

An opinionated library for building LLM powered applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use super::completion::{
    AnthropicCompatibleProvider, AnthropicCompletionRequest, Content, GenericCompletionModel,
    Usage, anthropic_usage_totals, map_finish_reason,
};
use crate::completion::{CompletionError, CompletionRequest};
use crate::http_client::sse::GenericEventSource;
use crate::http_client::{self, HttpClientExt};
use crate::message::ReasoningContent;
use crate::providers::internal::adapter::{AdapterOutput, WireAdapter, WireFrame};
use crate::providers::internal::sse_transport::{
    OpenLog, SseTransportOptions, open_wire_stream, skip_blank_frames,
};
use crate::providers::internal::wire::{self, WireEvent};
use crate::streaming::{
    self, MintKind, RawStreamingChoice, RawStreamingResult, StreamFinal, StreamPartId,
    ToolCallDeltaContent, ToolInputEnd, UnparseableToolInput,
};
use crate::telemetry::{CompletionOperation, SpanCombinator};
use crate::wasm_compat::{WasmCompatSend, WasmCompatSync};
use std::collections::HashMap;

/// Patch the shared typed request into the Anthropic *streaming* request body.
///
/// The body derives from the *same* typed [`AnthropicCompletionRequest`] the
/// blocking path builds (in `completion.rs`), rather than being re-assembled by
/// hand. The previous hand-rolled `json!` body had drifted from the blocking one
/// and silently dropped `output_schema` (structured-output config); reaching for
/// the typed request fixes that and keeps the two in lockstep. Only the two
/// streaming-only differences documented below are applied here.
fn streaming_body(request: &AnthropicCompletionRequest) -> Result<Value, CompletionError> {
    let mut body = serde_json::to_value(request)?;
    if let Some(map) = body.as_object_mut() {
        // `AnthropicCompletionRequest` has no `stream` field (the blocking path
        // omits it, defaulting to non-streaming); set it for the streaming endpoint.
        map.insert("stream".to_string(), Value::Bool(true));

        // Preserve the streaming path's long-standing `tool_choice` shape, which
        // emitted `tool_choice` *iff* a non-empty tool set was advertised (Anthropic
        // rejects `tool_choice` without `tools`). The blocking typed request instead
        // serializes any caller-set `tool_choice` regardless of tools and omits it
        // when unset, so reconcile here:
        //   - tools present, choice unset -> add the explicit `auto` the streaming
        //     wire has always carried (equivalent to Anthropic's default);
        //   - tools absent -> drop a caller-set `tool_choice` that would otherwise
        //     be sent without `tools` and rejected.
        if map.contains_key("tools") {
            map.entry("tool_choice")
                .or_insert_with(|| json!({ "type": "auto" }));
        } else {
            map.remove("tool_choice");
        }
    }

    Ok(body)
}

/// The `type` values this client models on the Anthropic Messages SSE wire.
///
/// [`classify_tagged_frame`] dispatches on this list: a frame whose `type` is
/// outside it classifies `Unknown` (driver policy: warn + skip), while a
/// listed type must pass the full [`StreamingEvent`] decode or classify
/// `Corrupt`. There is no `#[serde(other)]` fallback — policy lives in the
/// classify layer, never in serde. The one modeled exception is a novel
/// *nested* delta type inside `content_block_delta`, which decodes to
/// [`ContentDelta::Unknown`] (a warned no-op) via its hand-written dispatch.
const KNOWN_EVENT_TYPES: &[&str] = &[
    "message_start",
    "content_block_start",
    "content_block_delta",
    "content_block_stop",
    "message_delta",
    "message_stop",
    "ping",
    "error",
];

#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamingEvent {
    MessageStart {
        /// Anthropic-compatible relays (Bedrock's Messages passthrough) can
        /// emit `message_start` with a null `message`; `None` is a no-op
        /// rather than a corrupt frame.
        #[serde(default)]
        message: Option<MessageStart>,
    },
    ContentBlockStart {
        index: usize,
        content_block: Content,
    },
    ContentBlockDelta {
        index: usize,
        delta: ContentDelta,
    },
    ContentBlockStop {
        index: usize,
    },
    MessageDelta {
        delta: MessageDelta,
        usage: PartialUsage,
    },
    MessageStop,
    /// Keep-alive; a Known no-op, not an unknown event to warn about.
    Ping,
    /// Anthropic's top-level error envelope (`{"type":"error","error":{...}}`,
    /// e.g. `overloaded_error`). A modeled event, not an unknown to warn-skip:
    /// it surfaces as a provider error like every other family's error
    /// envelope. The payload stays a raw `Value` so every provider field
    /// (type, message, extras) survives into the error body.
    Error {
        error: serde_json::Value,
    },
}

#[derive(Debug, Deserialize)]
pub struct MessageStart {
    pub id: String,
    pub role: String,
    pub content: Vec<Content>,
    pub model: String,
    pub stop_reason: Option<String>,
    pub stop_sequence: Option<String>,
    pub usage: Usage,
}

#[derive(Debug)]
pub enum ContentDelta {
    TextDelta {
        text: String,
    },
    InputJsonDelta {
        partial_json: String,
    },
    ThinkingDelta {
        thinking: String,
    },
    SignatureDelta {
        signature: String,
    },
    CitationsDelta {
        citation: super::completion::Citation,
    },
    /// Any nested delta type this client doesn't model. Anthropic's
    /// versioning policy reserves the right to add new delta types without
    /// notice, so an unmodeled nested tag must not fail the whole
    /// `content_block_delta` frame (which would classify it `Corrupt` and
    /// surface an `Err` item per frame). It decodes to a no-op, warned at the
    /// interpret site — the same shape as
    /// [`ContentPartChunkPart::Unknown`](crate::providers::openai::responses_api::streaming::ContentPartChunkPart).
    Unknown(serde_json::Value),
}

/// Hand-written tag dispatch instead of a trailing `#[serde(untagged)]`
/// variant: on an internally-tagged enum the untagged fallback also swallows
/// a *known* tag with an invalid payload, silently demoting a data-level
/// defect to a skippable unknown delta. Here a known delta tag must decode
/// fully or error (the frame classifies `Corrupt`); only an unmodeled (or
/// absent) tag falls back to [`ContentDelta::Unknown`], preserving the value
/// verbatim. Same pattern as `ContentPartChunkPart`'s hand dispatch in
/// `openai/responses_api/streaming.rs`.
impl<'de> Deserialize<'de> for ContentDelta {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        // A non-object delta is a data-level defect of the tagged shape, not
        // an unmodeled delta kind: it errors (classifying the frame
        // `Corrupt`) instead of degrading to an `Unknown` no-op — the
        // conformance corpus pins `"delta": 42` as Corrupt.
        if !value.is_object() {
            return Err(serde::de::Error::custom("content delta must be an object"));
        }
        let str_field = |tag: &str, field: &str| -> Result<String, D::Error> {
            value
                .get(field)
                .and_then(serde_json::Value::as_str)
                .map(ToOwned::to_owned)
                .ok_or_else(|| {
                    serde::de::Error::custom(format!(
                        "`{tag}` content delta is missing a string `{field}` field"
                    ))
                })
        };
        match value.get("type").cloned() {
            Some(serde_json::Value::String(tag)) => match tag.as_str() {
                "text_delta" => Ok(Self::TextDelta {
                    text: str_field("text_delta", "text")?,
                }),
                "input_json_delta" => Ok(Self::InputJsonDelta {
                    partial_json: str_field("input_json_delta", "partial_json")?,
                }),
                "thinking_delta" => Ok(Self::ThinkingDelta {
                    thinking: str_field("thinking_delta", "thinking")?,
                }),
                "signature_delta" => Ok(Self::SignatureDelta {
                    signature: str_field("signature_delta", "signature")?,
                }),
                "citations_delta" => {
                    let citation = value.get("citation").cloned().ok_or_else(|| {
                        serde::de::Error::custom(
                            "`citations_delta` content delta is missing a `citation` field",
                        )
                    })?;
                    Ok(Self::CitationsDelta {
                        citation: serde_json::from_value(citation)
                            .map_err(serde::de::Error::custom)?,
                    })
                }
                _ => Ok(Self::Unknown(value)),
            },
            Some(_) => Err(serde::de::Error::custom(
                "content delta `type` must be a string",
            )),
            // A content delta without a `type` is malformed, not novel: an
            // untagged text delta from a compat gateway silently skipping
            // here would yield a successful *empty* completion. Corrupt
            // surfaces in-band and the stream keeps consuming.
            None => Err(serde::de::Error::custom(
                "content delta is missing a `type` field",
            )),
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct MessageDelta {
    pub stop_reason: Option<String>,
    pub stop_sequence: Option<String>,
}

#[derive(Debug, Deserialize, Clone, Serialize, Default)]
pub struct PartialUsage {
    pub output_tokens: usize,
    #[serde(default)]
    pub input_tokens: Option<usize>,
    #[serde(default)]
    pub cache_creation_input_tokens: Option<u64>,
    /// Per-TTL breakdown of `cache_creation_input_tokens`. Anthropic reports
    /// it on `message_start`, not the terminal `message_delta`; the adapter
    /// carries it forward onto the terminal usage.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache_creation: Option<super::completion::CacheCreation>,
    #[serde(default)]
    pub cache_read_input_tokens: Option<u64>,
    /// Breakdown of `output_tokens`. Anthropic reports it on the terminal
    /// `message_delta` — the frame that also carries the final `output_tokens`
    /// — not on `message_start`, so unlike `cache_creation` it needs no
    /// carry-forward.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_tokens_details: Option<super::completion::OutputTokensDetails>,
}

impl From<&PartialUsage> for crate::completion::Usage {
    fn from(value: &PartialUsage) -> crate::completion::Usage {
        anthropic_usage_totals(
            value.input_tokens.unwrap_or_default() as u64,
            value.output_tokens as u64,
            value.cache_read_input_tokens,
            value.cache_creation_input_tokens,
            value.output_tokens_details,
        )
    }
}

impl From<PartialUsage> for crate::completion::Usage {
    fn from(value: PartialUsage) -> crate::completion::Usage {
        (&value).into()
    }
}

// Client tool-call fragment assembly lives in the shared accumulator
// (`PartsAccumulator::tool_input_*`); the adapter tracks only the open block's
// wire id. Server tool use keeps local state because its assembled payload
// becomes text-block metadata (`ANTHROPIC_RAW_CONTENT_KEY`), not a tool call.
struct ServerToolUseState {
    name: String,
    id: String,
    initial_input: Value,
    input_json: String,
}

#[derive(Default)]
struct ThinkingState {
    /// Signature assembled from this block's `signature_delta`s. Only the
    /// signature is adapter-side state — the wire fragments it across
    /// deltas and delivers no completed form, so the adapter assembles it
    /// for the block's end event. Thinking TEXT accumulates in the shared
    /// accumulator via `ReasoningDelta`s; no restatement buffer exists.
    signature: String,
    /// The `signature` `content_block_start` opened the block with.
    ///
    /// Recorded traffic always carries the empty string here and delivers the
    /// whole signature by delta, so this is kept as a FALLBACK for a block
    /// that never sends a delta — not as a prefix the deltas extend. A wire
    /// that ever delivered the signature up front still round-trips; a
    /// delta-bearing block never double-counts the opening value.
    initial_signature: String,
}

impl ThinkingState {
    /// The block's completed signature: deltas win over the opening value,
    /// and an absent signature is `None`.
    fn into_signature(self) -> Option<String> {
        let signature = if self.signature.is_empty() {
            self.initial_signature
        } else {
            self.signature
        };
        (!signature.is_empty()).then_some(signature)
    }
}

/// The Anthropic Messages SSE wire as a [`WireAdapter`].
///
/// Holds the per-stream assembly state (open tool call, server tool uses,
/// open thinking block, terminal metadata); frame-triage policy lives in
/// [`run_wire_stream`](crate::providers::internal::adapter::run_wire_stream),
/// not here.
#[derive(Default)]
struct AnthropicAdapter {
    /// Wire id of the open client tool-use block, when one is streaming.
    current_tool_call: Option<String>,
    server_tool_uses: HashMap<usize, ServerToolUseState>,
    current_thinking: Option<ThinkingState>,
    input_tokens: u64,
    /// Per-TTL cache-write breakdown from `message_start`; the terminal
    /// `message_delta` usage omits it.
    cache_creation: Option<super::completion::CacheCreation>,
    message_id: Option<String>,
    response_model: Option<String>,
    /// A provider `error` event ended the turn; later frames are dead — the
    /// provider aborted, and interpreting more output (or a terminal) would
    /// dress the failure up as a completed turn.
    failed: bool,
}

impl WireAdapter for AnthropicAdapter {
    type Frame = WireFrame;
    type Event = StreamingEvent;
    type Response = StreamingCompletionResponse;

    fn classify(&self, frame: WireFrame) -> WireEvent<StreamingEvent> {
        wire::classify_tagged_frame(&frame.as_str(), "type", |event_type| {
            KNOWN_EVENT_TYPES.contains(&event_type)
        })
    }

    fn interpret(&mut self, event: StreamingEvent, out: &mut AdapterOutput<Self::Response>) {
        if self.failed {
            return;
        }

        match &event {
            StreamingEvent::MessageStart { message } => {
                // Bedrock-compat quirk: a `message_start` without a message
                // body is a no-op, not an error.
                let Some(message) = message else { return };
                self.input_tokens = message.usage.input_tokens;
                self.cache_creation = message.usage.cache_creation.clone();
                self.message_id = Some(message.id.clone());
                self.response_model = Some(message.model.clone());

                let span = tracing::Span::current();
                span.record("gen_ai.response.id", &message.id);
                span.record("gen_ai.response.model", &message.model);
                return;
            }
            StreamingEvent::MessageDelta { delta, usage } => {
                // Only a `message_delta` carrying a stop reason is the
                // provider's genuine terminal; without one it is a no-op.
                let Some(reason) = delta.stop_reason.as_ref() else {
                    return;
                };
                // cache_creation_input_tokens and cache_read_input_tokens are
                // cumulative totals on message_delta.usage per the Anthropic
                // streaming API spec — use them directly.
                //
                // `input_tokens` prefers the terminal `message_delta` and falls
                // back to `message_start`.
                //
                // Anthropic proper sends the count on *both* frames and they
                // agree (every recorded cassette under
                // `tests/cassettes/anthropic/` reporting it on the delta reports
                // the same value on the start), so the preference is what runs
                // there and the fallback is inert. The fallback covers the
                // reverse split — a delta that omits the count, leaving the one
                // `message_start` reported.
                //
                // It does *not* rescue the Bedrock-compat body-less
                // `message_start`: that shape returns early above without
                // setting `self.input_tokens`, so the fallback yields
                // `Some(0)`. Preferring the delta is what carries a real count
                // there — do not drop the preference on the theory that the
                // fallback covers that case.
                //
                // Anthropic-*compatible* gateways do not all agree. OpenRouter's
                // Messages endpoint can send `input_tokens: 0` on
                // `message_start` and the real count on `message_delta`
                // (recorded in `gateway_message_delta_metadata`, which OpenRouter
                // served from an Amazon Bedrock upstream — the split follows what
                // it routes to, so it is not every response from that endpoint).
                // Without this preference such a turn surfaces a silent
                // `Usage { input_tokens: 0 }` — worse than a missing value for a
                // consumer sizing its context window from it.
                //
                // Zero on the delta is read as "not reported" so a gateway with
                // the inverse split cannot erase a count `message_start` got
                // right. Note this is a heuristic, not an invariant: a fully
                // cache-hit prompt legitimately bills zero *uncached* input
                // tokens, and its real size lives in the cache fields. Nothing
                // is lost today because both frames then carry the same zero and
                // the fallback yields it anyway — but do not extend the `> 0`
                // filter to the `message_start` side or the cache fields, where
                // a genuine zero would be discarded.
                let usage = PartialUsage {
                    output_tokens: usage.output_tokens,
                    input_tokens: usage
                        .input_tokens
                        .filter(|tokens| *tokens > 0)
                        .or_else(|| usize::try_from(self.input_tokens).ok()),
                    cache_creation_input_tokens: usage.cache_creation_input_tokens,
                    cache_creation: usage
                        .cache_creation
                        .clone()
                        .or_else(|| self.cache_creation.clone()),
                    cache_read_input_tokens: usage.cache_read_input_tokens,
                    // Taken from this frame alone, with no `message_start`
                    // fallback: unlike `cache_creation`, Anthropic reports the
                    // output-token breakdown on the terminal `message_delta`,
                    // the same frame that carries the final `output_tokens` it
                    // breaks down. `message_start` has none to carry forward.
                    output_tokens_details: usage.output_tokens_details,
                };

                let span = tracing::Span::current();
                span.record_token_usage(&crate::completion::Usage::from(&usage));
                out.push(Ok(RawStreamingChoice::FinalResponse(
                    StreamingCompletionResponse {
                        usage,
                        stop_reason: Some(reason.clone()),
                        // Rides the same `message_delta` as the stop reason,
                        // and only that frame carries it: `message_start`
                        // always opens with `null`.
                        stop_sequence: delta.stop_sequence.clone(),
                        message_id: self.message_id.clone(),
                        model: self.response_model.clone(),
                        // Stamped by the transport layer; the adapter never
                        // sees connection headers.
                        provider_request_id: None,
                    },
                )));
                return;
            }
            StreamingEvent::Error { error } => {
                // The provider aborted the turn in-band. Preserve the full
                // error envelope (code + message + extras) as the error body,
                // matching the interactions wire's handling; the stream
                // carries it as an in-band `Err` item, and EOF without
                // `message_delta` then withholds the terminal record.
                self.failed = true;
                let body = serde_json::json!({ "type": "error", "error": error }).to_string();
                out.push(Err(crate::provider_response::completion_error_from_body(
                    body,
                )));
                return;
            }
            _ => {}
        }

        if let Some(result) = handle_event(
            &event,
            &mut self.current_tool_call,
            &mut self.server_tool_uses,
            &mut self.current_thinking,
        ) {
            out.push(result);
        }
    }

    fn finish(&mut self, _out: &mut AdapterOutput<Self::Response>) {
        // EOF without `message_delta` is truncation: open blocks stay
        // partial, and no terminal record may be synthesized.
    }

    fn is_finished(&self) -> bool {
        // A provider `error` event is the wire's own terminal failure:
        // `interpret` already pushed the in-band `Err`, so the driver must
        // stop reading — a later modeled frame (e.g. a stray `message_delta`)
        // would otherwise dress the aborted turn up as a completed one.
        self.failed
    }
}

/// Anthropic's own terminal stream record, as returned by
/// [`GenericCompletionModel::raw_stream`].
///
/// [`crate::completion::CompletionModel::stream`] maps this once into the
/// normalized [`StreamFinal`]; callers who want the provider-native shape read
/// it here instead.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct StreamingCompletionResponse {
    /// Token usage carried by the terminal `message_delta` event.
    pub usage: PartialUsage,
    /// Anthropic's `stop_reason`, verbatim, when the stream reported one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<String>,
    /// Which of the caller's `stop_sequences` actually fired, verbatim, when
    /// the terminal `message_delta` reported one.
    ///
    /// `stop_reason: "stop_sequence"` says only *that* a sequence matched;
    /// the sequence itself is the part a caller branches on, and Anthropic
    /// strips it from the text, so the wire is its only source. The blocking
    /// twin has carried it on
    /// [`CompletionResponse::stop_sequence`](super::completion::CompletionResponse::stop_sequence)
    /// all along — the streamed record dropped it after parsing, so the same
    /// request answered strictly less when streamed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stop_sequence: Option<String>,
    /// The `message_start` message ID, when the stream reported one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message_id: Option<String>,
    /// The model named by `message_start`, when the stream reported one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// The transport request id from the SSE connection's `request-id`
    /// response header — not part of any stream frame; stamped by the
    /// transport. `None` when the provider did not report one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider_request_id: Option<String>,
}

/// Normalize an Anthropic terminal stream record.
///
/// The provider descriptor name is an *input* rather than a constant: the
/// Anthropic Messages stream format is shared by every Anthropic-compatible
/// provider, so baking in `"anthropic"` here would mislabel all of them.
impl From<(&str, StreamingCompletionResponse)> for StreamFinal {
    fn from((provider, response): (&str, StreamingCompletionResponse)) -> Self {
        StreamFinal::new(provider, crate::completion::Usage::from(&response.usage))
            .with_optional_finish_reason(response.stop_reason.as_deref().map(map_finish_reason))
            .with_optional_message_id(response.message_id)
            .with_optional_provider_request_id(response.provider_request_id)
            .with_optional_model(response.model)
    }
}

impl<Ext, T> GenericCompletionModel<Ext, T>
where
    T: HttpClientExt + Clone + Default + 'static,
    Ext: AnthropicCompatibleProvider + Clone + WasmCompatSend + WasmCompatSync + 'static,
{
    /// Open a stream whose terminal record stays Anthropic-native.
    ///
    /// This is the escape hatch for provider-specific terminal fields rig does
    /// not normalize. It shares the request builder, transport, telemetry, and
    /// error handling with
    /// [`CompletionModel::stream`](crate::completion::CompletionModel::stream),
    /// which calls it and then maps the terminal record once through
    /// [`crate::streaming::normalize_stream`] — one network request either way.
    pub async fn raw_stream(
        &self,
        completion_request: CompletionRequest,
    ) -> Result<RawStreamingResult<StreamingCompletionResponse>, CompletionError> {
        let (span, request) =
            self.prepare_request(completion_request, CompletionOperation::ChatStreaming)?;

        // Logged after the streaming-only patches, not on the shared typed
        // request: `stream` and the reconciled `tool_choice` are exactly what
        // makes this body differ from the blocking one.
        let body = streaming_body(&request)?;
        crate::providers::internal::trace_json(
            crate::providers::internal::LogTarget::Completions,
            "Anthropic completion request",
            &body,
        );

        let body: Vec<u8> = serde_json::to_vec(&body)?;

        let req = self
            .client
            .post("/v1/messages")?
            .body(body)
            .map_err(http_client::Error::Protocol)?;

        let event_source = GenericEventSource::new(self.client.clone(), req);
        let (event_source, request_id_slot) = match Ext::REQUEST_ID_HEADER {
            Some(header) => {
                let (event_source, slot) = event_source.capture_request_id(header);
                (event_source, Some(slot))
            }
            None => (event_source, None),
        };

        // Anthropic's loop historically had no separate `StreamEnded` arm and
        // no transport-error log: `StreamEnded` folds into the generic error
        // mapping, preserved via the options below.
        let stream = open_wire_stream(
            event_source,
            SseTransportOptions {
                open_log: OpenLog::Silent,
                stream_ended_is_error: true,
                log_transport_errors: false,
            },
            skip_blank_frames,
            AnthropicAdapter::default(),
            span,
        );
        Ok(
            crate::providers::internal::sse_transport::stamp_terminal_request_id(
                stream,
                request_id_slot,
                Ext::REQUEST_ID_HEADER,
                |response, id| response.provider_request_id = Some(id),
            ),
        )
    }

    pub(crate) async fn stream(
        &self,
        completion_request: CompletionRequest,
    ) -> Result<streaming::StreamingCompletionResponse, CompletionError> {
        let stream = self.raw_stream(completion_request).await?;
        let normalized = streaming::normalize_stream(stream, |response| {
            Ok(StreamFinal::from((Ext::PROVIDER_NAME, response)))
        });

        Ok(streaming::StreamingCompletionResponse::stream(
            Ext::PROVIDER_NAME,
            normalized,
        ))
    }
}

fn handle_event(
    event: &StreamingEvent,
    current_tool_call: &mut Option<String>,
    server_tool_uses: &mut HashMap<usize, ServerToolUseState>,
    current_thinking: &mut Option<ThinkingState>,
) -> Option<Result<RawStreamingChoice<StreamingCompletionResponse>, CompletionError>> {
    match event {
        StreamingEvent::ContentBlockDelta { index, delta } => match delta {
            ContentDelta::TextDelta { text } => {
                if current_tool_call.is_none() {
                    return Some(Ok(RawStreamingChoice::Message(text.clone())));
                }
                None
            }
            ContentDelta::InputJsonDelta { partial_json } => {
                if let Some(server_tool_use) = server_tool_uses.get_mut(index) {
                    server_tool_use.input_json.push_str(partial_json);
                    return None;
                }

                if let Some(id) = current_tool_call {
                    // Emit the delta so UI can show progress; the shared
                    // accumulator assembles the fragments.
                    return Some(Ok(RawStreamingChoice::ToolCallDelta {
                        id: StreamPartId::wire(id.clone()),
                        content: ToolCallDeltaContent::Delta(partial_json.clone()),
                    }));
                }
                None
            }
            ContentDelta::ThinkingDelta { thinking } => {
                current_thinking.get_or_insert_with(ThinkingState::default);

                Some(Ok(RawStreamingChoice::ReasoningDelta {
                    // Anthropic has no reasoning item id; the content-block
                    // index is stable across a block's deltas and its stop.
                    id: MintKind::Block.for_wire_index(*index as u64),
                    provider_id: None,
                    reasoning: thinking.clone(),
                }))
            }
            ContentDelta::SignatureDelta { signature } => {
                current_thinking
                    .get_or_insert_with(ThinkingState::default)
                    .signature
                    .push_str(signature);

                // Wire quirk: the signature is not emitted as its own chunk —
                // it closes the thinking block, riding on the completed
                // `Reasoning` the `content_block_stop` restatement emits.
                None
            }
            ContentDelta::CitationsDelta { citation } => {
                crate::message::AdditionalParams::from_entries([("citations", json!([citation]))])
                    .map(|params| Ok(RawStreamingChoice::TextAdditionalParams(params)))
            }
            ContentDelta::Unknown(value) => {
                // Structural metadata only: a novel delta type can carry
                // model output, which must not leak into production WARN
                // logs (same policy as the adapter's unknown-event warn).
                tracing::warn!(
                    delta_type = value.get("type").and_then(serde_json::Value::as_str),
                    "skipping unrecognized Anthropic content delta type"
                );
                None
            }
        },
        StreamingEvent::ContentBlockStart {
            index,
            content_block,
        } => match content_block {
            // Keep this destructuring exhaustive so new wire fields force an
            // explicit capture-or-drop decision: block-start `text` arrives
            // via the deltas, and `cache_control` is a request-side
            // directive — both deliberately dropped here.
            Content::Text {
                text: _,
                citations,
                cache_control: _,
            } => {
                let additional_params = crate::message::AdditionalParams::from_entries(
                    (!citations.is_empty()).then(|| ("citations", json!(citations))),
                );
                Some(Ok(RawStreamingChoice::TextStart {
                    // Anthropic has no text item id; the content-block index
                    // is stable for the block's lifetime.
                    id: MintKind::Block.for_wire_index(*index as u64),
                    additional_params,
                }))
            }
            Content::ServerToolUse { id, name, input } => {
                server_tool_uses.insert(
                    *index,
                    ServerToolUseState {
                        name: name.clone(),
                        id: id.clone(),
                        initial_input: input.clone(),
                        input_json: String::new(),
                    },
                );
                None
            }
            raw @ (Content::WebSearchToolResult { .. }
            | Content::CodeExecutionToolResult { .. }) => Some(Ok(RawStreamingChoice::TextStart {
                id: MintKind::Block.for_wire_index(*index as u64),
                additional_params: crate::message::AdditionalParams::from_entries([(
                    super::completion::ANTHROPIC_RAW_CONTENT_KEY,
                    json!(raw),
                )]),
            })),
            Content::ToolUse { id, name, .. } => {
                *current_tool_call = Some(id.clone());
                Some(Ok(RawStreamingChoice::ToolCallDelta {
                    id: StreamPartId::wire(id.clone()),
                    content: ToolCallDeltaContent::Name(name.clone()),
                }))
            }
            Content::Thinking {
                thinking,
                signature,
            } => {
                // `content_block_start` opens the block with its initial
                // payload; the old `..` discarded both fields. Adaptive
                // thinking opens with an empty `thinking`, emits no
                // `thinking_delta` at all, and delivers the whole signature
                // by `signature_delta` — so the block's only content is a
                // signature, which `content_block_stop` must still restate.
                *current_thinking = Some(ThinkingState {
                    signature: String::new(),
                    initial_signature: signature.clone().unwrap_or_default(),
                });
                // The opening payload's text is a delta like any other; the
                // shared accumulator owns the block's text.
                (!thinking.is_empty()).then(|| {
                    Ok(RawStreamingChoice::ReasoningDelta {
                        id: MintKind::Block.for_wire_index(*index as u64),
                        provider_id: None,
                        reasoning: thinking.clone(),
                    })
                })
            }
            Content::RedactedThinking { data } => Some(Ok(RawStreamingChoice::Reasoning {
                // Derive the key from the content-block index (no wire id).
                id: MintKind::Block.for_wire_index(*index as u64),
                provider_id: None,
                content: ReasoningContent::Redacted { data: data.clone() },
            })),
            // Handle other content types - they don't need special handling
            _ => None,
        },
        StreamingEvent::ContentBlockStop { index } => {
            // Drop only a wholly empty block. A signature-only thinking block
            // (empty text, complete signature) is the adaptive-thinking wire
            // shape, and its signature is replay-required provider state that
            // Anthropic accepts back verbatim (the paired non-streaming
            // cassette replays that exact empty-text signed block). The
            // non-streaming path has never gated on text, so gating here was
            // a unary/streaming divergence that silently dropped the
            // signature.
            if let Some(thinking_state) = Option::take(current_thinking) {
                // `content_block_stop` is the wire's own lifecycle end: the
                // shared accumulator holds the block's accumulated text, and
                // the end carries the assembled signature (present for
                // signed and adaptive signature-only blocks alike — replay-
                // required provider state either way). A wholly empty block
                // (no deltas, no signature) closes silently.
                return Some(Ok(RawStreamingChoice::ReasoningEnd {
                    id: MintKind::Block.for_wire_index(*index as u64),
                    reasoning: None,
                    signature: thinking_state.into_signature(),
                    // `content_block_stop` is the wire's own end frame, so
                    // even an unsigned block yields its completed event.
                    wire_sent: true,
                }));
            }

            if let Some(server_tool_use) = server_tool_uses.remove(index) {
                let input = if server_tool_use.input_json.is_empty() {
                    if server_tool_use.initial_input.is_null() {
                        json!({})
                    } else {
                        server_tool_use.initial_input
                    }
                } else {
                    match serde_json::from_str(&server_tool_use.input_json) {
                        Ok(json_value) => json_value,
                        Err(e) => return Some(Err(CompletionError::from(e))),
                    }
                };

                return Some(Ok(RawStreamingChoice::TextStart {
                    id: MintKind::Block.for_wire_index(*index as u64),
                    additional_params: crate::message::AdditionalParams::from_entries([(
                        super::completion::ANTHROPIC_RAW_CONTENT_KEY,
                        json!(Content::ServerToolUse {
                            id: server_tool_use.id,
                            name: server_tool_use.name,
                            input,
                        }),
                    )]),
                }));
            }

            // `content_block_stop` promises a complete block: empty input
            // finalizes to `{}`, malformed input surfaces as an error item
            // (`UnparseableToolInput::Error`) in the accumulator.
            Option::take(current_tool_call).map(|id| {
                Ok(RawStreamingChoice::ToolInputEnd(ToolInputEnd::new(
                    id,
                    UnparseableToolInput::Error,
                )))
            })
        }
        // Interpreted by the adapter (`message_start`/`message_delta`/the
        // `error` envelope) or Known no-ops (`message_stop`, `ping`).
        StreamingEvent::MessageStart { .. }
        | StreamingEvent::MessageDelta { .. }
        | StreamingEvent::MessageStop
        | StreamingEvent::Ping
        | StreamingEvent::Error { .. } => None,
    }
}

#[cfg(test)]
mod tests {
    use super::super::completion::{
        AnthropicRequestParams, CLAUDE_OPUS_4_8, CacheControl, CacheTtl, Message, SystemContent,
        apply_prompt_cache_control, build_tool_definitions, resolve_top_level_cache_control,
    };
    use super::*;
    use crate::completion::Message as RigMessage;
    use crate::completion::request::Document as RigDocument;
    use crate::streaming::RawStreamingToolCall;
    use async_stream::stream;
    use futures::StreamExt;

    /// Normalize a hand-built Anthropic raw stream exactly as
    /// [`GenericCompletionModel::stream`] does, so aggregation assertions run
    /// against the same terminal-record mapping as the real path.
    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
    fn to_stream_result(
        stream: impl futures::Stream<
            Item = Result<RawStreamingChoice<StreamingCompletionResponse>, CompletionError>,
        > + Send
        + 'static,
    ) -> crate::streaming::StreamingResult {
        crate::streaming::normalize_stream(Box::pin(stream), |response| {
            Ok(StreamFinal::from(("anthropic", response)))
        })
    }

    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
    fn to_stream_result(
        stream: impl futures::Stream<
            Item = Result<RawStreamingChoice<StreamingCompletionResponse>, CompletionError>,
        > + 'static,
    ) -> crate::streaming::StreamingResult {
        crate::streaming::normalize_stream(Box::pin(stream), |response| {
            Ok(StreamFinal::from(("anthropic", response)))
        })
    }

    /// Build the streaming request body the way [`GenericCompletionModel::raw_stream`]
    /// does — the shared typed request, then the streaming-only patches — without
    /// needing a client to reach the prelude.
    fn built_streaming_body(
        model: &str,
        request: CompletionRequest,
        strict_tools: bool,
    ) -> Result<Value, CompletionError> {
        let typed = AnthropicCompletionRequest::try_from_params::<
            crate::providers::anthropic::client::AnthropicExt,
        >(
            AnthropicRequestParams {
                model,
                request,
                prompt_caching: false,
                automatic_caching: false,
                automatic_caching_ttl: None,
                static_prefix_cache_ttl: None,
            },
            strict_tools,
        )?;

        streaming_body(&typed)
    }

    #[test]
    fn test_streaming_tool_build_marks_final_combined_tool() {
        let mut additional_params = json!({
            "tools": [{
                "name": "provider_tool",
                "description": "Provider tool",
                "input_schema": {"type": "object"}
            }]
        });

        let mut tools =
            build_tool_definitions::<crate::providers::anthropic::client::AnthropicExt>(
                vec![crate::completion::ToolDefinition {
                    name: "rig_tool".to_string(),
                    description: "Rig tool".to_string(),
                    parameters: json!({"type": "object", "properties": {}}),
                }],
                &mut additional_params,
                false,
            )
            .unwrap();
        let mut system: Vec<SystemContent> = Vec::new();
        let mut messages: Vec<Message> = Vec::new();
        apply_prompt_cache_control(&mut system, &mut messages, &mut tools, true, None, None)
            .unwrap();

        assert_eq!(tools.len(), 2);
        assert!(tools[0].get("cache_control").is_none());
        assert_eq!(tools[1]["name"], "provider_tool");
        assert_eq!(tools[1]["cache_control"]["type"], "ephemeral");
    }

    #[test]
    fn streaming_request_keeps_documents_after_leading_system_messages() {
        let request = CompletionRequest {
            model: None,
            preamble: None,
            chat_history: vec![
                RigMessage::system("System prompt"),
                RigMessage::assistant("Earlier assistant turn"),
                RigMessage::system("Mid-conversation instruction"),
                RigMessage::user("Prompt"),
            ],
            documents: vec![RigDocument {
                id: "doc1".to_string(),
                text: "Document text.".to_string(),
                additional_props: Default::default(),
            }],
            tools: vec![],
            temperature: None,
            max_tokens: Some(64),
            tool_choice: None,
            additional_params: None,
            output_schema: None,
            record_telemetry_content: false,
        };

        let body = built_streaming_body(CLAUDE_OPUS_4_8, request, false)
            .expect("streaming request body should build");

        assert_eq!(body["system"][0]["text"], "System prompt");
        assert_eq!(body["system"][1]["text"], "Mid-conversation instruction");
        let messages = body["messages"]
            .as_array()
            .expect("messages should be array");
        assert_eq!(messages.len(), 3);
        assert_eq!(messages[0]["role"], "user");
        assert!(
            messages[0].to_string().contains("<file id: doc1>"),
            "document message should follow top-level system: {messages:?}"
        );
        assert_eq!(messages[1]["role"], "assistant");
        assert_eq!(messages[2]["role"], "user");
        assert_eq!(
            messages
                .iter()
                .filter(|message| message.to_string().contains("<file id: doc1>"))
                .count(),
            1,
            "document message should appear exactly once: {messages:?}"
        );
    }

    #[test]
    fn streaming_body_is_blocking_body_plus_stream_flag_and_carries_output_schema() {
        let schema: schemars::Schema = serde_json::from_value(json!({
            "title": "WeatherResponse",
            "type": "object",
            "properties": { "city": { "type": "string" } }
        }))
        .expect("schema should deserialize");

        let request = CompletionRequest {
            model: None,
            preamble: Some("You are helpful".to_string()),
            chat_history: vec![RigMessage::user("What's the weather?")],
            documents: vec![],
            tools: vec![],
            temperature: Some(0.5),
            max_tokens: Some(64),
            tool_choice: None,
            additional_params: None,
            output_schema: Some(schema),
            record_telemetry_content: false,
        };

        let streaming_body = built_streaming_body(CLAUDE_OPUS_4_8, request.clone(), false)
            .expect("streaming request body should build");

        // The streaming endpoint flag is set.
        assert_eq!(streaming_body["stream"], serde_json::Value::Bool(true));

        // Regression: `output_schema` now reaches the streaming wire as
        // `output_config` (the hand-rolled body dropped it entirely, so this
        // assertion would have failed before the typed-request unification).
        assert_eq!(
            streaming_body["output_config"]["format"]["type"],
            "json_schema"
        );
        assert!(
            streaming_body["output_config"]["format"]["schema"].is_object(),
            "streaming body must carry the structured-output schema: {streaming_body}"
        );

        // Unification invariant: the streaming body is exactly the blocking body
        // (built via the same typed request) plus `stream: true`. Pins the two
        // wire formats together so a future edit can't reintroduce drift.
        let blocking = AnthropicCompletionRequest::try_from(AnthropicRequestParams {
            model: CLAUDE_OPUS_4_8,
            request,
            prompt_caching: false,
            automatic_caching: false,
            automatic_caching_ttl: None,
            static_prefix_cache_ttl: None,
        })
        .expect("blocking request body should build");
        let mut expected = serde_json::to_value(&blocking).expect("serialize blocking body");
        expected
            .as_object_mut()
            .expect("body is an object")
            .insert("stream".to_string(), serde_json::Value::Bool(true));

        assert_eq!(streaming_body, expected);
    }

    #[test]
    fn streaming_body_keeps_explicit_tool_choice_auto_when_tools_present_but_unset() {
        let request = CompletionRequest {
            model: None,
            preamble: None,
            chat_history: vec![RigMessage::user("Add 2 and 3")],
            documents: vec![],
            tools: vec![crate::completion::ToolDefinition {
                name: "add".to_string(),
                description: "Add x and y".to_string(),
                parameters: json!({
                    "type": "object",
                    "properties": { "x": { "type": "integer" } }
                }),
            }],
            temperature: None,
            max_tokens: Some(64),
            tool_choice: None,
            additional_params: None,
            output_schema: None,
            record_telemetry_content: false,
        };

        let body = built_streaming_body(CLAUDE_OPUS_4_8, request, false)
            .expect("streaming request body should build");

        // Tools advertised + `tool_choice` unset must still carry the explicit
        // `auto` the streaming wire format has always sent (parity with recorded
        // fixtures), even though the blocking typed request omits it.
        assert_eq!(body["tool_choice"], json!({ "type": "auto" }));
        assert!(body["tools"].is_array());
    }

    #[test]
    fn streaming_body_applies_strict_tool_opt_in() {
        let request = CompletionRequest {
            model: None,
            preamble: None,
            chat_history: vec![RigMessage::user("Look this up")],
            documents: vec![],
            tools: vec![crate::completion::ToolDefinition {
                name: "lookup".to_string(),
                description: "Look up a value".to_string(),
                parameters: json!({
                    "type": "object",
                    "properties": { "query": { "type": "string" } },
                    "required": ["query"]
                }),
            }],
            temperature: None,
            max_tokens: Some(64),
            tool_choice: None,
            additional_params: None,
            output_schema: None,
            record_telemetry_content: false,
        };

        let body = built_streaming_body(CLAUDE_OPUS_4_8, request, true)
            .expect("streaming request body should build");

        assert_eq!(body["tools"][0]["strict"], true);
        assert_eq!(
            body["tools"][0]["input_schema"]["additionalProperties"],
            false
        );
        assert_eq!(
            body["tools"][0]["input_schema"]["required"],
            json!(["query"])
        );
    }

    #[test]
    fn streaming_body_drops_tool_choice_when_no_tools_are_advertised() {
        // The typed request serializes a caller-set `tool_choice` regardless of
        // whether tools are present, but the streaming path has always emitted
        // `tool_choice` *only* alongside a non-empty tool set (Anthropic rejects it
        // otherwise). A `tool_choice` set with no tools must not reach the wire.
        let request = CompletionRequest {
            model: None,
            preamble: None,
            chat_history: vec![RigMessage::user("Hi")],
            documents: vec![],
            tools: vec![],
            temperature: None,
            max_tokens: Some(64),
            tool_choice: Some(crate::message::ToolChoice::Auto),
            additional_params: None,
            output_schema: None,
            record_telemetry_content: false,
        };

        let body = built_streaming_body(CLAUDE_OPUS_4_8, request, false)
            .expect("streaming request body should build");

        assert!(
            body.get("tool_choice").is_none(),
            "tool_choice must be omitted when no tools are advertised: {body}"
        );
        assert!(body.get("tools").is_none());
    }

    #[test]
    fn test_streaming_prompt_cache_control_uses_raw_top_level_ttl() {
        let mut additional_params = json!({
            "cache_control": {"type": "ephemeral", "ttl": "1h"}
        });
        let top_level_cache_control =
            resolve_top_level_cache_control(false, None, &mut additional_params).unwrap();
        let mut tools =
            build_tool_definitions::<crate::providers::anthropic::client::AnthropicExt>(
                vec![crate::completion::ToolDefinition {
                    name: "rig_tool".to_string(),
                    description: "Rig tool".to_string(),
                    parameters: json!({"type": "object", "properties": {}}),
                }],
                &mut additional_params,
                false,
            )
            .unwrap();
        let mut system = vec![SystemContent::Text {
            text: "System prompt".to_string(),
            cache_control: None,
        }];
        let mut messages: Vec<Message> = Vec::new();

        apply_prompt_cache_control(
            &mut system,
            &mut messages,
            &mut tools,
            true,
            None,
            top_level_cache_control.as_ref(),
        )
        .unwrap();

        assert_eq!(tools[0]["cache_control"]["type"], "ephemeral");
        assert_eq!(tools[0]["cache_control"]["ttl"], "1h");
        match &system[0] {
            SystemContent::Text {
                cache_control: Some(CacheControl::Ephemeral { ttl }),
                ..
            } => assert_eq!(ttl.as_ref(), Some(&CacheTtl::OneHour)),
            other => panic!("expected system cache_control, got {other:?}"),
        }
        assert!(additional_params.get("cache_control").is_none());
    }

    fn handle_event(
        event: &StreamingEvent,
        current_tool_call: &mut Option<String>,
        current_thinking: &mut Option<ThinkingState>,
    ) -> Option<Result<RawStreamingChoice<StreamingCompletionResponse>, CompletionError>> {
        let mut server_tool_uses = HashMap::new();
        super::handle_event(
            event,
            current_tool_call,
            &mut server_tool_uses,
            current_thinking,
        )
    }

    #[test]
    fn test_thinking_delta_deserialization() {
        let json = r#"{"type": "thinking_delta", "thinking": "Let me think about this..."}"#;
        let delta: ContentDelta = serde_json::from_str(json).unwrap();

        match delta {
            ContentDelta::ThinkingDelta { thinking } => {
                assert_eq!(thinking, "Let me think about this...");
            }
            _ => panic!("Expected ThinkingDelta variant"),
        }
    }

    #[test]
    fn test_signature_delta_deserialization() {
        let json = r#"{"type": "signature_delta", "signature": "abc123def456"}"#;
        let delta: ContentDelta = serde_json::from_str(json).unwrap();

        match delta {
            ContentDelta::SignatureDelta { signature } => {
                assert_eq!(signature, "abc123def456");
            }
            _ => panic!("Expected SignatureDelta variant"),
        }
    }

    #[test]
    fn test_thinking_delta_streaming_event_deserialization() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "thinking_delta",
                "thinking": "First, I need to understand the problem."
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();

        match event {
            StreamingEvent::ContentBlockDelta { index, delta } => {
                assert_eq!(index, 0);
                match delta {
                    ContentDelta::ThinkingDelta { thinking } => {
                        assert_eq!(thinking, "First, I need to understand the problem.");
                    }
                    _ => panic!("Expected ThinkingDelta"),
                }
            }
            _ => panic!("Expected ContentBlockDelta event"),
        }
    }

    #[test]
    fn test_signature_delta_streaming_event_deserialization() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "signature_delta",
                "signature": "ErUBCkYICBgCIkCaGbqC85F4"
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();

        match event {
            StreamingEvent::ContentBlockDelta { index, delta } => {
                assert_eq!(index, 0);
                match delta {
                    ContentDelta::SignatureDelta { signature } => {
                        assert_eq!(signature, "ErUBCkYICBgCIkCaGbqC85F4");
                    }
                    _ => panic!("Expected SignatureDelta"),
                }
            }
            _ => panic!("Expected ContentBlockDelta event"),
        }
    }

    #[test]
    fn test_handle_thinking_delta_event() {
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::ThinkingDelta {
                thinking: "Analyzing the request...".to_string(),
            },
        };

        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        let choice = result.unwrap().unwrap();

        match choice {
            RawStreamingChoice::ReasoningDelta { id, reasoning, .. } => {
                assert_eq!(id, crate::streaming::MintKind::Block.for_wire_index(0));
                assert_eq!(reasoning, "Analyzing the request...");
            }
            _ => panic!("Expected ReasoningDelta choice"),
        }

        // The block is tracked (its signature may still arrive); the text
        // itself accumulates in the shared accumulator, not here.
        assert!(thinking_state.is_some());
    }

    #[test]
    fn test_handle_signature_delta_event() {
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::SignatureDelta {
                signature: "test_signature".to_string(),
            },
        };

        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        // SignatureDelta should not yield anything (returns None)
        assert!(result.is_none());

        // But signature should be captured in thinking state
        assert!(thinking_state.is_some());
        assert_eq!(thinking_state.unwrap().signature, "test_signature");
    }

    #[test]
    fn test_handle_redacted_thinking_content_block_start_event() {
        let event = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::RedactedThinking {
                data: "redacted_blob".to_string(),
            },
        };
        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        match result.unwrap().unwrap() {
            RawStreamingChoice::Reasoning {
                content: ReasoningContent::Redacted { data },
                ..
            } => {
                assert_eq!(data, "redacted_blob");
            }
            _ => panic!("Expected Redacted reasoning chunk"),
        }
    }

    /// The adaptive-thinking wire shape, exactly as recorded in
    /// `tests/cassettes/anthropic/opus_4_7/messages_adaptive_thinking_streaming_smoke.yaml`:
    /// `content_block_start` opens the block with an EMPTY `thinking` and an
    /// EMPTY `signature`, a `signature_delta` carries the whole signature, and
    /// no `thinking_delta` ever arrives. The block's only content is its
    /// signature, and it must survive `content_block_stop`.
    #[test]
    fn signature_only_thinking_block_survives_content_block_stop() {
        let mut tool_call_state = None;
        let mut thinking_state = None;

        let start = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::Thinking {
                thinking: String::new(),
                signature: Some(String::new()),
            },
        };
        assert!(handle_event(&start, &mut tool_call_state, &mut thinking_state).is_none());

        let signature = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::SignatureDelta {
                signature: "the_whole_signature".to_string(),
            },
        };
        assert!(handle_event(&signature, &mut tool_call_state, &mut thinking_state).is_none());

        let stop = StreamingEvent::ContentBlockStop { index: 0 };
        let result = handle_event(&stop, &mut tool_call_state, &mut thinking_state)
            .expect("signature-only thinking block must not be dropped")
            .expect("thinking block should not be an error");

        match result {
            RawStreamingChoice::ReasoningEnd { id, signature, .. } => {
                assert_eq!(id, crate::streaming::MintKind::Block.for_wire_index(0));
                assert_eq!(signature.as_deref(), Some("the_whole_signature"));
            }
            other => panic!("Expected a signed lifecycle end, got {other:?}"),
        }
    }

    /// Forward compat: a block that delivers its whole signature on
    /// `content_block_start` and sends no `signature_delta` keeps it.
    #[test]
    fn signature_delivered_only_on_content_block_start_is_kept() {
        let mut tool_call_state = None;
        let mut thinking_state = None;

        let start = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::Thinking {
                thinking: String::new(),
                signature: Some("up_front_signature".to_string()),
            },
        };
        assert!(handle_event(&start, &mut tool_call_state, &mut thinking_state).is_none());

        let stop = StreamingEvent::ContentBlockStop { index: 0 };
        match handle_event(&stop, &mut tool_call_state, &mut thinking_state)
            .expect("an up-front signature must not be dropped")
            .expect("thinking block should not be an error")
        {
            RawStreamingChoice::ReasoningEnd { signature, .. } => {
                assert_eq!(signature.as_deref(), Some("up_front_signature"));
            }
            other => panic!("Expected a signed lifecycle end, got {other:?}"),
        }
    }

    /// The opening `signature` is a fallback, never a prefix the deltas
    /// extend: a delta-bearing block must publish exactly what the deltas
    /// assembled, or the value replayed to Anthropic is corrupt.
    #[test]
    fn signature_deltas_supersede_the_opening_signature() {
        let mut tool_call_state = None;
        let mut thinking_state = None;

        let start = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::Thinking {
                thinking: String::new(),
                signature: Some("opening".to_string()),
            },
        };
        assert!(handle_event(&start, &mut tool_call_state, &mut thinking_state).is_none());

        for fragment in ["delta_", "assembled"] {
            let signature = StreamingEvent::ContentBlockDelta {
                index: 0,
                delta: ContentDelta::SignatureDelta {
                    signature: fragment.to_string(),
                },
            };
            assert!(handle_event(&signature, &mut tool_call_state, &mut thinking_state).is_none());
        }

        let stop = StreamingEvent::ContentBlockStop { index: 0 };
        match handle_event(&stop, &mut tool_call_state, &mut thinking_state)
            .expect("thinking block should be restated")
            .expect("thinking block should not be an error")
        {
            RawStreamingChoice::ReasoningEnd { signature, .. } => {
                assert_eq!(signature.as_deref(), Some("delta_assembled"))
            }
            other => panic!("Expected a signed lifecycle end, got {other:?}"),
        }
    }

    /// `content_block_start` can carry the block's opening text; discarding it
    /// would truncate the restatement the accumulator supersedes deltas with.
    #[test]
    fn thinking_block_start_text_streams_as_the_first_delta() {
        let mut tool_call_state = None;
        let mut thinking_state = None;

        let start = StreamingEvent::ContentBlockStart {
            index: 2,
            content_block: Content::Thinking {
                thinking: "opening ".to_string(),
                signature: None,
            },
        };
        // The opening payload's text is a delta like any other; the shared
        // accumulator owns the block's text — no adapter-side restatement
        // buffer exists to seed.
        match handle_event(&start, &mut tool_call_state, &mut thinking_state)
            .expect("the opening text streams")
            .expect("not an error")
        {
            RawStreamingChoice::ReasoningDelta { id, reasoning, .. } => {
                assert_eq!(id, crate::streaming::MintKind::Block.for_wire_index(2));
                assert_eq!(reasoning, "opening ");
            }
            other => panic!("Expected the opening delta, got {other:?}"),
        }

        let delta = StreamingEvent::ContentBlockDelta {
            index: 2,
            delta: ContentDelta::ThinkingDelta {
                thinking: "rest".to_string(),
            },
        };
        assert!(handle_event(&delta, &mut tool_call_state, &mut thinking_state).is_some());

        let stop = StreamingEvent::ContentBlockStop { index: 2 };
        match handle_event(&stop, &mut tool_call_state, &mut thinking_state)
            .expect("the stop emits the lifecycle end")
            .expect("not an error")
        {
            RawStreamingChoice::ReasoningEnd {
                id,
                reasoning: None,
                signature: None,
                wire_sent: true,
            } => {
                assert_eq!(id, crate::streaming::MintKind::Block.for_wire_index(2));
            }
            other => panic!("Expected a bare lifecycle end, got {other:?}"),
        }
    }

    /// A block with neither text nor signature carries nothing to replay.
    #[test]
    fn wholly_empty_thinking_block_is_dropped() {
        let mut tool_call_state = None;
        let mut thinking_state = None;

        let start = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::Thinking {
                thinking: String::new(),
                signature: None,
            },
        };
        assert!(handle_event(&start, &mut tool_call_state, &mut thinking_state).is_none());

        let stop = StreamingEvent::ContentBlockStop { index: 0 };
        // The stop emits a bare lifecycle end; with nothing streamed and no
        // signature, the shared accumulator records no part (a bare end for
        // a never-opened key is a no-op).
        match handle_event(&stop, &mut tool_call_state, &mut thinking_state)
            .expect("the stop emits the lifecycle end")
            .expect("not an error")
        {
            RawStreamingChoice::ReasoningEnd {
                reasoning: None,
                signature: None,
                ..
            } => {}
            other => panic!("Expected a bare lifecycle end, got {other:?}"),
        }
    }

    #[test]
    fn test_handle_text_delta_event() {
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::TextDelta {
                text: "Hello, world!".to_string(),
            },
        };

        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        let choice = result.unwrap().unwrap();

        match choice {
            RawStreamingChoice::Message(text) => {
                assert_eq!(text, "Hello, world!");
            }
            _ => panic!("Expected Message choice"),
        }
    }

    #[test]
    fn test_handle_text_block_start_event() {
        let event = StreamingEvent::ContentBlockStart {
            index: 0,
            content_block: Content::Text {
                text: String::new(),
                citations: Vec::new(),
                cache_control: None,
            },
        };

        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        let choice = result.unwrap().unwrap();
        assert!(matches!(
            choice,
            RawStreamingChoice::TextStart {
                additional_params: None,
                ..
            }
        ));
    }

    #[test]
    fn test_thinking_delta_does_not_interfere_with_tool_calls() {
        // Thinking deltas should still be processed even if a tool call is in progress
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::ThinkingDelta {
                thinking: "Thinking while tool is active...".to_string(),
            },
        };

        let mut tool_call_state = Some("tool_123".to_string());
        let mut thinking_state = None;

        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        let choice = result.unwrap().unwrap();

        match choice {
            RawStreamingChoice::ReasoningDelta { reasoning, .. } => {
                assert_eq!(reasoning, "Thinking while tool is active...");
            }
            _ => panic!("Expected ReasoningDelta choice"),
        }

        // Tool call state should remain unchanged
        assert!(tool_call_state.is_some());
    }

    #[test]
    fn test_handle_input_json_delta_event() {
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::InputJsonDelta {
                partial_json: "{\"arg\":\"value".to_string(),
            },
        };

        let mut tool_call_state = Some("tool_123".to_string());
        let mut thinking_state = None;

        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        // Should emit a ToolCallDelta
        assert!(result.is_some());
        let choice = result.unwrap().unwrap();

        match choice {
            RawStreamingChoice::ToolCallDelta { id, content } => {
                assert_eq!(id, crate::streaming::StreamPartId::wire("tool_123"));
                match content {
                    ToolCallDeltaContent::Delta(delta) => assert_eq!(delta, "{\"arg\":\"value"),
                    _ => panic!("Expected Delta content"),
                }
            }
            _ => panic!("Expected ToolCallDelta choice, got {:?}", choice),
        }

        // The open block stays open; assembly of the fragment happens in the
        // shared accumulator.
        assert!(tool_call_state.is_some());
    }

    #[test]
    fn test_tool_call_accumulation_with_multiple_deltas() {
        let mut tool_call_state = Some("tool_123".to_string());
        let mut thinking_state = None;

        // First delta
        let event1 = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::InputJsonDelta {
                partial_json: "{\"location\":".to_string(),
            },
        };
        let result1 = handle_event(&event1, &mut tool_call_state, &mut thinking_state);
        assert!(result1.is_some());

        // Second delta
        let event2 = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::InputJsonDelta {
                partial_json: "\"Paris\",".to_string(),
            },
        };
        let result2 = handle_event(&event2, &mut tool_call_state, &mut thinking_state);
        assert!(result2.is_some());

        // Third delta
        let event3 = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::InputJsonDelta {
                partial_json: "\"temp\":\"20C\"}".to_string(),
            },
        };
        let result3 = handle_event(&event3, &mut tool_call_state, &mut thinking_state);
        assert!(result3.is_some());

        assert!(tool_call_state.is_some());

        // Final ContentBlockStop hands the block to the shared accumulator,
        // which finalizes the assembled fragments (`Error` policy: a stopped
        // block promised complete input). End-to-end assembly of exactly this
        // fragment sequence is pinned in `streaming::parts` unit tests.
        let stop_event = StreamingEvent::ContentBlockStop { index: 0 };
        let final_result = handle_event(&stop_event, &mut tool_call_state, &mut thinking_state);
        assert!(final_result.is_some());

        match final_result.unwrap().unwrap() {
            RawStreamingChoice::ToolInputEnd(end) => {
                assert_eq!(end.id, crate::streaming::StreamPartId::wire("tool_123"));
                assert!(matches!(
                    end.on_unparseable,
                    crate::streaming::UnparseableToolInput::Error
                ));
            }
            other => panic!("Expected ToolInputEnd, got {:?}", other),
        }

        // Tool call state should be taken
        assert!(tool_call_state.is_none());
    }

    #[test]
    fn test_citations_delta_streaming_event_deserialization() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "citations_delta",
                "citation": {
                    "type": "char_location",
                    "cited_text": "The grass is green.",
                    "document_index": 0,
                    "document_title": "Example",
                    "start_char_index": 0,
                    "end_char_index": 20
                }
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();
        let StreamingEvent::ContentBlockDelta { index, delta } = event else {
            panic!("expected ContentBlockDelta");
        };
        assert_eq!(index, 0);
        let ContentDelta::CitationsDelta { citation } = delta else {
            panic!("expected CitationsDelta");
        };
        let crate::providers::anthropic::completion::Citation::CharLocation(citation) = citation
        else {
            panic!("expected CharLocation");
        };
        assert_eq!(citation.start_char_index, 0);
        assert_eq!(citation.end_char_index, 20);
    }

    #[test]
    fn test_search_result_citations_delta_streaming_event_deserialization() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "citations_delta",
                "citation": {
                    "type": "search_result_location",
                    "cited_text": "API requests require a key.",
                    "source": "https://docs.example.com/api-reference",
                    "title": "API Reference",
                    "search_result_index": 0,
                    "start_block_index": 0,
                    "end_block_index": 1
                }
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();
        let StreamingEvent::ContentBlockDelta { delta, .. } = event else {
            panic!("expected ContentBlockDelta");
        };
        let ContentDelta::CitationsDelta { citation } = delta else {
            panic!("expected CitationsDelta");
        };
        assert!(matches!(
            citation,
            crate::providers::anthropic::completion::Citation::SearchResultLocation(
                crate::providers::anthropic::completion::SearchResultLocationCitation {
                    search_result_index: 0,
                    start_block_index: 0,
                    end_block_index: 1,
                    ..
                }
            )
        ));
    }

    #[test]
    fn test_web_search_result_citations_delta_streaming_event_deserialization() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "citations_delta",
                "citation": {
                    "type": "web_search_result_location",
                    "cited_text": "Claude Shannon was a mathematician.",
                    "url": "https://example.com/shannon",
                    "title": "Claude Shannon",
                    "encrypted_index": "encrypted-reference"
                }
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();
        let StreamingEvent::ContentBlockDelta { delta, .. } = event else {
            panic!("expected ContentBlockDelta");
        };
        let ContentDelta::CitationsDelta { citation } = delta else {
            panic!("expected CitationsDelta");
        };
        assert!(matches!(
            citation,
            crate::providers::anthropic::completion::Citation::WebSearchResultLocation(ref citation)
                if citation.url == "https://example.com/shannon"
                    && citation.encrypted_index == "encrypted-reference"
        ));
    }

    #[test]
    fn test_web_search_result_citations_delta_allows_null_title() {
        let json = r#"{
            "type": "content_block_delta",
            "index": 0,
            "delta": {
                "type": "citations_delta",
                "citation": {
                    "type": "web_search_result_location",
                    "cited_text": "Claude Shannon was a mathematician.",
                    "url": "https://example.com/shannon",
                    "title": null,
                    "encrypted_index": "encrypted-reference"
                }
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();
        let StreamingEvent::ContentBlockDelta { delta, .. } = event else {
            panic!("expected ContentBlockDelta");
        };
        let ContentDelta::CitationsDelta { citation } = delta else {
            panic!("expected CitationsDelta");
        };
        assert!(matches!(
            citation,
            crate::providers::anthropic::completion::Citation::WebSearchResultLocation(
                crate::providers::anthropic::completion::WebSearchResultLocationCitation {
                    title: None,
                    ..
                }
            )
        ));
    }

    #[test]
    fn test_text_content_block_start_allows_null_citations() {
        // The Anthropic Messages API emits an explicit `"citations": null` on the
        // first text `content_block_start` event. `#[serde(default)]` alone covers
        // a missing field but not an explicit null, so this must deserialize to an
        // empty citation list rather than failing the whole stream (see #1971).
        let json = r#"{
            "type": "content_block_start",
            "index": 0,
            "content_block": {
                "type": "text",
                "text": "",
                "citations": null
            }
        }"#;

        let event: StreamingEvent = serde_json::from_str(json).unwrap();
        let StreamingEvent::ContentBlockStart { content_block, .. } = event else {
            panic!("expected ContentBlockStart");
        };
        let Content::Text {
            text, citations, ..
        } = content_block
        else {
            panic!("expected text content block");
        };
        assert_eq!(text, "");
        assert!(citations.is_empty());
    }

    #[test]
    fn test_web_search_content_block_start_events_deserialize() {
        let server_tool_use = r#"{
            "type": "content_block_start",
            "index": 1,
            "content_block": {
                "type": "server_tool_use",
                "id": "srvtoolu_01",
                "name": "web_search",
                "input": {
                    "query": "claude shannon birth date"
                }
            }
        }"#;
        let event: StreamingEvent = serde_json::from_str(server_tool_use).unwrap();
        assert!(matches!(
            event,
            StreamingEvent::ContentBlockStart {
                content_block: Content::ServerToolUse {
                    ref id,
                    ref name,
                    ref input
                },
                ..
            } if id == "srvtoolu_01"
                && name == "web_search"
                && input["query"] == "claude shannon birth date"
        ));

        let web_search_tool_result = r#"{
            "type": "content_block_start",
            "index": 2,
            "content_block": {
                "type": "web_search_tool_result",
                "tool_use_id": "srvtoolu_01",
                "content": [{
                    "type": "web_search_result",
                    "url": "https://example.com/shannon",
                    "title": "Claude Shannon",
                    "encrypted_content": "encrypted-content"
                }]
            }
        }"#;
        let event: StreamingEvent = serde_json::from_str(web_search_tool_result).unwrap();
        assert!(matches!(
            event,
            StreamingEvent::ContentBlockStart {
                content_block: Content::WebSearchToolResult {
                    ref tool_use_id,
                    ref content
                },
                ..
            } if tool_use_id == "srvtoolu_01"
                && content[0]["encrypted_content"] == "encrypted-content"
        ));
    }

    #[test]
    fn test_code_execution_tool_result_block_is_preserved() {
        let event: StreamingEvent = serde_json::from_value(serde_json::json!({
            "type": "content_block_start",
            "index": 1,
            "content_block": {
                "type": "code_execution_tool_result",
                "tool_use_id": "srvtoolu_01",
                "content": {
                    "type": "code_execution_result",
                    "return_code": 0,
                    "stdout": "42\n",
                    "stderr": "",
                    "content": []
                }
            }
        }))
        .unwrap();
        let mut tool_call_state = None;
        let mut server_tool_uses = HashMap::new();
        let mut thinking_state = None;

        let choice = super::handle_event(
            &event,
            &mut tool_call_state,
            &mut server_tool_uses,
            &mut thinking_state,
        )
        .expect("code_execution_tool_result block should produce raw metadata")
        .unwrap();

        let RawStreamingChoice::TextStart {
            id,
            additional_params: Some(additional_params),
        } = choice
        else {
            panic!("expected text-start metadata for code_execution_tool_result");
        };
        assert_eq!(id, crate::streaming::MintKind::Block.for_wire_index(1));
        assert_eq!(
            additional_params[crate::providers::anthropic::completion::ANTHROPIC_RAW_CONTENT_KEY]["type"],
            "code_execution_tool_result"
        );
        assert_eq!(
            additional_params[crate::providers::anthropic::completion::ANTHROPIC_RAW_CONTENT_KEY]["content"]
                ["stdout"],
            "42\n"
        );
    }

    #[tokio::test]
    async fn test_streaming_web_search_blocks_are_preserved_on_final_choice() {
        let raw_stream = stream! {
            let mut tool_call_state = None;
            let mut server_tool_uses = HashMap::new();
            let mut thinking_state = None;

            let server_tool_use_start = super::handle_event(
                &StreamingEvent::ContentBlockStart {
                    index: 0,
                    content_block: Content::ServerToolUse {
                        id: "srvtoolu_01".to_string(),
                        name: "web_search".to_string(),
                        input: serde_json::Value::Null,
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            );
            assert!(
                server_tool_use_start.is_none(),
                "server_tool_use start should be accumulated until its input JSON is complete"
            );

            let server_tool_use_delta = super::handle_event(
                &StreamingEvent::ContentBlockDelta {
                    index: 0,
                    delta: ContentDelta::InputJsonDelta {
                        partial_json: r#"{"query":"claude shannon birth date"}"#.to_string(),
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            );
            assert!(
                server_tool_use_delta.is_none(),
                "server_tool_use input JSON should not be emitted as a Rig tool-call delta"
            );

            yield super::handle_event(
                &StreamingEvent::ContentBlockStop { index: 0 },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            )
            .expect("server_tool_use stop should produce completed raw metadata");

            yield super::handle_event(
                &StreamingEvent::ContentBlockStart {
                    index: 1,
                    content_block: Content::WebSearchToolResult {
                        tool_use_id: "srvtoolu_01".to_string(),
                        content: serde_json::json!([{
                            "type": "web_search_result",
                            "url": "https://example.com/shannon",
                            "title": "Claude Shannon",
                            "encrypted_content": "encrypted-content"
                        }]),
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            )
            .expect("web_search_tool_result block should produce raw metadata");

            yield super::handle_event(
                &StreamingEvent::ContentBlockStart {
                    index: 2,
                    content_block: Content::Text {
                        text: String::new(),
                        citations: Vec::new(),
                        cache_control: None,
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            )
            .expect("text block start should produce a raw choice");

            yield super::handle_event(
                &StreamingEvent::ContentBlockDelta {
                    index: 2,
                    delta: ContentDelta::TextDelta {
                        text: "Claude Shannon was born on April 30, 1916.".to_string(),
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            )
            .expect("text delta should produce a raw choice");

            yield super::handle_event(
                &StreamingEvent::ContentBlockDelta {
                    index: 2,
                    delta: ContentDelta::CitationsDelta {
                        citation: crate::providers::anthropic::completion::Citation::WebSearchResultLocation(
                            crate::providers::anthropic::completion::WebSearchResultLocationCitation {
                                cited_text: "Claude Shannon was born on April 30, 1916."
                                    .to_string(),
                                url: "https://example.com/shannon".to_string(),
                                title: Some("Claude Shannon".to_string()),
                                encrypted_index: "encrypted-index".to_string(),
                            },
                        ),
                    },
                },
                &mut tool_call_state,
                &mut server_tool_uses,
                &mut thinking_state,
            )
            .expect("citation delta should produce a raw choice");

            yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse::default()));
        };

        let mut stream = crate::streaming::StreamingCompletionResponse::stream(
            "anthropic",
            to_stream_result(raw_stream),
        );
        while stream.next().await.is_some() {}

        let choice_items: Vec<crate::message::AssistantContent> =
            stream.choice.clone().into_iter().collect();
        assert_eq!(choice_items.len(), 3);
        assert!(
            choice_items
                .iter()
                .all(|item| !matches!(item, crate::message::AssistantContent::ToolCall(_))),
            "provider-owned web-search blocks must not become Rig client tool calls"
        );

        let Some(crate::message::AssistantContent::Text(server_tool_use)) = choice_items.first()
        else {
            panic!("expected raw server_tool_use metadata");
        };
        assert_eq!(
            server_tool_use.additional_params.as_ref().unwrap()
                [crate::providers::anthropic::completion::ANTHROPIC_RAW_CONTENT_KEY]["type"],
            "server_tool_use"
        );
        assert_eq!(
            server_tool_use.additional_params.as_ref().unwrap()
                [crate::providers::anthropic::completion::ANTHROPIC_RAW_CONTENT_KEY]["input"]["query"],
            "claude shannon birth date"
        );

        let Some(crate::message::AssistantContent::Text(web_search_result)) = choice_items.get(1)
        else {
            panic!("expected raw web_search_tool_result metadata");
        };
        assert_eq!(
            web_search_result.additional_params.as_ref().unwrap()
                [crate::providers::anthropic::completion::ANTHROPIC_RAW_CONTENT_KEY]["content"][0]
                ["encrypted_content"],
            "encrypted-content"
        );

        let Some(crate::message::AssistantContent::Text(answer)) = choice_items.get(2) else {
            panic!("expected answer text");
        };
        assert_eq!(answer.text, "Claude Shannon was born on April 30, 1916.");
        let citations = crate::providers::anthropic::completion::anthropic_citations(answer)
            .expect("expected preserved citations");
        assert!(matches!(
            citations.first(),
            Some(crate::providers::anthropic::completion::Citation::WebSearchResultLocation(citation))
                if citation.encrypted_index == "encrypted-index"
        ));
    }

    #[test]
    fn test_handle_citations_delta_event_preserves_metadata() {
        let event = StreamingEvent::ContentBlockDelta {
            index: 0,
            delta: ContentDelta::CitationsDelta {
                citation: crate::providers::anthropic::completion::Citation::CharLocation(
                    crate::providers::anthropic::completion::CharLocationCitation {
                        cited_text: "The grass is green.".to_string(),
                        document_index: 0,
                        document_title: Some("Example".to_string()),
                        start_char_index: 0,
                        end_char_index: 20,
                    },
                ),
            },
        };

        let mut tool_call_state = None;
        let mut thinking_state = None;
        let result = handle_event(&event, &mut tool_call_state, &mut thinking_state);

        assert!(result.is_some());
        let choice = result.unwrap().unwrap();
        let RawStreamingChoice::TextAdditionalParams(additional_params) = choice else {
            panic!("expected TextAdditionalParams choice");
        };
        assert_eq!(additional_params["citations"][0]["type"], "char_location");
    }

    #[tokio::test]
    async fn test_streaming_citation_deltas_are_preserved_on_final_text() {
        let citation = crate::providers::anthropic::completion::Citation::CharLocation(
            crate::providers::anthropic::completion::CharLocationCitation {
                cited_text: "The grass is green.".to_string(),
                document_index: 0,
                document_title: Some("Example".to_string()),
                start_char_index: 0,
                end_char_index: 20,
            },
        );

        let raw_stream = stream! {
            let mut tool_call_state = None;
            let mut thinking_state = None;

            yield handle_event(
                &StreamingEvent::ContentBlockStart {
                    index: 0,
                    content_block: Content::Text {
                        text: String::new(),
                        citations: Vec::new(),
                        cache_control: None,
                    },
                },
                &mut tool_call_state,
                &mut thinking_state,
            )
            .expect("text block start should produce a raw choice");

            yield handle_event(
                &StreamingEvent::ContentBlockDelta {
                    index: 0,
                    delta: ContentDelta::TextDelta {
                        text: "the grass is green".to_string(),
                    },
                },
                &mut tool_call_state,
                &mut thinking_state,
            )
            .expect("text delta should produce a raw choice");

            yield handle_event(
                &StreamingEvent::ContentBlockDelta {
                    index: 0,
                    delta: ContentDelta::CitationsDelta {
                        citation: crate::providers::anthropic::completion::Citation::CharLocation(
                            crate::providers::anthropic::completion::CharLocationCitation {
                                cited_text: "The grass is green.".to_string(),
                                document_index: 0,
                                document_title: Some("Example".to_string()),
                                start_char_index: 0,
                                end_char_index: 20,
                            },
                        ),
                    },
                },
                &mut tool_call_state,
                &mut thinking_state,
            )
            .expect("citation delta should produce a raw choice");

            yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse::default()));
        };

        let mut stream = crate::streaming::StreamingCompletionResponse::stream(
            "anthropic",
            to_stream_result(raw_stream),
        );
        while stream.next().await.is_some() {}

        let choice_items: Vec<crate::message::AssistantContent> =
            stream.choice.clone().into_iter().collect();
        let Some(crate::message::AssistantContent::Text(text)) = choice_items.first() else {
            panic!("expected accumulated text item");
        };

        assert_eq!(text.text, "the grass is green");
        let citations = crate::providers::anthropic::completion::anthropic_citations(text).unwrap();
        assert_eq!(citations, vec![citation]);
    }

    /// The `#[serde(other)]` policy fallbacks are gone: classification is the
    /// only policy site. An unmodeled *top-level* event type is `Unknown`
    /// (driver: warn + skip); a `ping` is Known; and a known tag whose payload
    /// this client cannot decode is `Corrupt`, never silently demoted to an
    /// ignorable unknown. An unmodeled *nested* delta type is the one carved
    /// exception (Anthropic's versioning policy reserves the right to add
    /// them): it decodes to [`ContentDelta::Unknown`] and stays a Known
    /// no-op — see the dedicated tests below.
    #[test]
    fn classify_dispatches_on_the_known_event_list() {
        let adapter = AnthropicAdapter::default();

        let frame =
            WireFrame::Text(r#"{"type":"something_new_from_anthropic","field":"x"}"#.into());
        assert!(matches!(
            adapter.classify(frame),
            crate::providers::internal::wire::WireEvent::Unknown { event_type, .. }
                if event_type == "something_new_from_anthropic"
        ));

        let frame = WireFrame::Text(r#"{"type":"ping"}"#.into());
        assert!(matches!(
            adapter.classify(frame),
            crate::providers::internal::wire::WireEvent::Known(StreamingEvent::Ping)
        ));

        let frame = WireFrame::Text("{not json".into());
        assert!(matches!(
            adapter.classify(frame),
            crate::providers::internal::wire::WireEvent::Corrupt(_)
        ));
    }

    /// Forward compat: a novel nested delta type Anthropic ships tomorrow
    /// must not corrupt the whole `content_block_delta` frame — it decodes
    /// to [`ContentDelta::Unknown`] and interprets as a warned no-op, so the
    /// stream continues.
    #[test]
    fn novel_nested_delta_type_is_a_known_noop() {
        let adapter = AnthropicAdapter::default();
        let frame = WireFrame::Text(
            r#"{"type":"content_block_delta","index":0,"delta":{"type":"banana_delta","x":1}}"#
                .into(),
        );
        let crate::providers::internal::wire::WireEvent::Known(event) = adapter.classify(frame)
        else {
            panic!("a novel nested delta type must stay a Known event");
        };

        let mut adapter = AnthropicAdapter::default();
        let mut out = Vec::new();
        adapter.interpret(event, &mut out);
        assert!(out.is_empty(), "an unmodeled nested delta is a no-op");
    }

    /// Anthropic reports the per-TTL `cache_creation` split on
    /// `message_start` only; the terminal `message_delta` usage omits it. The
    /// adapter must carry it onto the terminal record. Unit-tested (not a
    /// cassette) because the carry-forward is internal adapter state — the
    /// wire evidence lives in the recorded `prompt_caching/matrix_*` streaming
    /// cassettes, whose `message_start` frames hold the split.
    #[test]
    fn per_ttl_cache_creation_split_carries_from_message_start_to_terminal() {
        let mut adapter = AnthropicAdapter::default();
        let mut out = Vec::new();

        let start = WireFrame::Text(
            r#"{"type":"message_start","message":{"id":"msg_1","role":"assistant","content":[],"model":"claude-sonnet-4-6","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":3,"output_tokens":1,"cache_creation_input_tokens":9702,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_1h_input_tokens":9366,"ephemeral_5m_input_tokens":336}}}}"#
                .into(),
        );
        let crate::providers::internal::wire::WireEvent::Known(event) = adapter.classify(start)
        else {
            panic!("message_start must classify Known");
        };
        adapter.interpret(event, &mut out);

        let delta = WireFrame::Text(
            r#"{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":7,"input_tokens":3,"cache_creation_input_tokens":9702,"cache_read_input_tokens":0}}"#
                .into(),
        );
        let crate::providers::internal::wire::WireEvent::Known(event) = adapter.classify(delta)
        else {
            panic!("message_delta must classify Known");
        };
        adapter.interpret(event, &mut out);

        let terminal = out
            .iter()
            .find_map(|item| match item {
                Ok(crate::streaming::RawStreamingChoice::FinalResponse(response)) => {
                    Some(response.clone())
                }
                _ => None,
            })
            .expect("terminal message_delta must yield a final response");
        let split = terminal
            .usage
            .cache_creation
            .expect("terminal usage must carry the message_start cache_creation split");
        assert_eq!(split.ephemeral_1h_input_tokens, 9366);
        assert_eq!(split.ephemeral_5m_input_tokens, 336);
        assert_eq!(terminal.usage.cache_creation_input_tokens, Some(9702));
    }

    /// A `content_block_delta` whose `delta` omits `type` is malformed, not
    /// novel: silently skipping it would turn a compat gateway's untagged
    /// text delta into a successful *empty* completion. It classifies
    /// `Corrupt`, surfacing in-band while the stream keeps consuming
    /// (#2258 B5).
    #[test]
    fn delta_missing_its_type_is_corrupt_not_skipped() {
        let adapter = AnthropicAdapter::default();
        let frame = WireFrame::Text(
            r#"{"type":"content_block_delta","index":0,"delta":{"text":"hello"}}"#.into(),
        );
        assert!(matches!(
            adapter.classify(frame),
            crate::providers::internal::wire::WireEvent::Corrupt(_)
        ));
    }

    /// Policy preserved: a *known* nested delta tag with a defective payload
    /// is a data-level defect, not an unmodeled delta — the frame classifies
    /// `Corrupt` instead of degrading to an `Unknown` no-op.
    #[test]
    fn known_nested_delta_tag_with_defective_payload_is_corrupt() {
        let adapter = AnthropicAdapter::default();
        let frame = WireFrame::Text(
            r#"{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":42}}"#
                .into(),
        );
        assert!(matches!(
            adapter.classify(frame),
            crate::providers::internal::wire::WireEvent::Corrupt(_)
        ));
    }

    /// Anthropic's top-level `{"type":"error"}` envelope (e.g.
    /// `overloaded_error`) is a Known event that surfaces as a provider error
    /// carrying the full envelope — never a warn-skipped unknown — and, since
    /// no `message_delta` follows, the stream ends with no terminal record.
    #[test]
    fn top_level_error_event_surfaces_as_a_provider_error() {
        let adapter = AnthropicAdapter::default();
        let frame = WireFrame::Text(
            r#"{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#.into(),
        );
        let crate::providers::internal::wire::WireEvent::Known(event) = adapter.classify(frame)
        else {
            panic!("the error envelope must classify as a Known event");
        };

        let mut adapter = AnthropicAdapter::default();
        let mut out = Vec::new();
        adapter.interpret(event, &mut out);

        assert_eq!(out.len(), 1, "the error envelope maps to one error item");
        let Some(Err(error)) = out.pop() else {
            panic!("the error envelope must surface as an Err item");
        };
        let body = error
            .provider_response_body()
            .expect("the provider's error payload must be preserved");
        assert!(
            body.contains("overloaded_error") && body.contains("Overloaded"),
            "the full envelope must survive into the error body, got: {body}"
        );
    }

    /// Bedrock-compat quirk: `message_start` without a message body is a
    /// Known no-op, not a corrupt frame.
    #[test]
    fn message_start_with_null_message_is_a_known_noop() {
        let adapter = AnthropicAdapter::default();
        let frame = WireFrame::Text(r#"{"type":"message_start","message":null}"#.into());
        let crate::providers::internal::wire::WireEvent::Known(event) = adapter.classify(frame)
        else {
            panic!("null-message message_start must stay a known event");
        };

        let mut adapter = AnthropicAdapter::default();
        let mut out = Vec::new();
        adapter.interpret(event, &mut out);
        assert!(out.is_empty(), "a message-less message_start is a no-op");
    }

    #[tokio::test]
    async fn terminal_record_normalizes_stop_reason_usage_and_metadata() {
        let raw_stream = stream! {
            yield Ok(RawStreamingChoice::Message("hi".to_string()));
            yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse {
                usage: PartialUsage {
                    output_tokens: 5,
                    input_tokens: Some(3),
                    cache_creation_input_tokens: None,
                    cache_creation: None,
                    cache_read_input_tokens: Some(2),
                    output_tokens_details: None,
                },
                stop_reason: Some("max_tokens".to_string()),
                stop_sequence: None,
                message_id: Some("msg_1".to_string()),
                model: Some(CLAUDE_OPUS_4_8.to_string()),
                provider_request_id: None,
            }));
        };

        let mut stream = crate::streaming::StreamingCompletionResponse::stream(
            "anthropic",
            to_stream_result(raw_stream),
        );
        while stream.next().await.is_some() {}

        let terminal = stream.response.expect("expected a terminal record");
        assert_eq!(terminal.provider, "anthropic");
        assert_eq!(terminal.message_id.as_deref(), Some("msg_1"));
        assert_eq!(terminal.model.as_deref(), Some(CLAUDE_OPUS_4_8));
        assert_eq!(
            terminal.finish_reason,
            Some(crate::completion::FinishReason::Length)
        );
        assert_eq!(terminal.usage.input_tokens, 3);
        assert_eq!(terminal.usage.output_tokens, 5);
        assert_eq!(terminal.usage.cached_input_tokens, 2);
        assert_eq!(terminal.usage.total_tokens, 10);
    }

    #[tokio::test]
    async fn terminal_record_upgrades_end_turn_to_tool_calls_after_a_streamed_tool_call() {
        // Anthropic normally reports `tool_use`, but the reconciliation
        // `normalize_stream` applies must hold whenever the turn actually
        // emitted a tool call.
        let raw_stream = stream! {
            yield Ok(RawStreamingChoice::ToolCall(RawStreamingToolCall::new(
                "toolu_1".to_string(),
                "add".to_string(),
                json!({"x": 1}),
            )));
            yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse {
                stop_reason: Some("end_turn".to_string()),
                ..Default::default()
            }));
        };

        let mut stream = crate::streaming::StreamingCompletionResponse::stream(
            "anthropic",
            to_stream_result(raw_stream),
        );
        while stream.next().await.is_some() {}

        let terminal = stream.response.expect("expected a terminal record");
        assert_eq!(
            terminal.finish_reason,
            Some(crate::completion::FinishReason::ToolCalls)
        );
    }

    #[tokio::test]
    async fn unknown_stop_reason_survives_onto_the_terminal_record() {
        let raw_stream = stream! {
            yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse {
                stop_reason: Some("pause_turn".to_string()),
                ..Default::default()
            }));
        };

        let mut stream = crate::streaming::StreamingCompletionResponse::stream(
            "anthropic",
            to_stream_result(raw_stream),
        );
        while stream.next().await.is_some() {}

        let terminal = stream.response.expect("expected a terminal record");
        assert_eq!(
            terminal.finish_reason,
            Some(crate::completion::FinishReason::Other(
                "pause_turn".to_owned()
            ))
        );
    }

    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
    mod terminal_emission {
        use super::super::super::completion::CLAUDE_SONNET_4_6;
        use crate::client::CompletionClient;
        use crate::completion::CompletionModel as _;
        use crate::providers::anthropic::Client;
        use crate::streaming::StreamedAssistantContent;
        use crate::test_utils::MockStreamingClient;
        use futures::StreamExt;

        const MESSAGE_START: &str = r#"{"type":"message_start","message":{"id":"msg_1","role":"assistant","content":[],"model":"claude-sonnet-4-6","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":5,"output_tokens":0}}}"#;
        const TEXT_START: &str =
            r#"{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}"#;
        const TEXT_DELTA: &str =
            r#"{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hi"}}"#;
        const MESSAGE_DELTA: &str = r#"{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":3}}"#;

        fn sse(frames: &[&str]) -> bytes::Bytes {
            bytes::Bytes::from(
                frames
                    .iter()
                    .map(|frame| format!("data: {frame}\n\n"))
                    .collect::<String>(),
            )
        }

        async fn collect(
            sse_bytes: bytes::Bytes,
        ) -> (
            Vec<String>,
            bool,
            bool,
            crate::streaming::StreamingCompletionResponse,
        ) {
            let client = Client::builder()
                .api_key("test-key")
                .http_client(MockStreamingClient { sse_bytes })
                .build()
                .expect("build client");
            let model = client.completion_model(CLAUDE_SONNET_4_6);
            let request = model.completion_request("hello").build();
            let mut stream = crate::completion::CompletionModel::stream(&model, request)
                .await
                .expect("stream should open");

            let mut texts = Vec::new();
            let mut saw_error = false;
            let mut saw_terminal = false;
            while let Some(item) = stream.next().await {
                match item {
                    Ok(StreamedAssistantContent::Text(text)) => texts.push(text.text),
                    Ok(StreamedAssistantContent::Final(_)) => saw_terminal = true,
                    Ok(_) => {}
                    Err(_) => saw_error = true,
                }
            }
            (texts, saw_error, saw_terminal, stream)
        }

        #[tokio::test]
        async fn truncated_stream_yields_content_but_no_terminal_record() {
            let (texts, saw_error, saw_terminal, stream) =
                collect(sse(&[MESSAGE_START, TEXT_START, TEXT_DELTA])).await;

            assert_eq!(texts, ["hi"]);
            assert!(!saw_error);
            assert!(
                !saw_terminal,
                "EOF without message_delta must not synthesize a terminal record"
            );
            assert!(stream.response.is_none());
        }

        #[tokio::test]
        async fn errored_stream_forwards_the_error_and_no_terminal_record() {
            use crate::test_utils::SequencedStreamingHttpClient;

            // A transport failure injected into the byte stream after some
            // content must be forwarded (via `from_stream_transport`) and must
            // not be papered over with a synthesized terminal record.
            let client = Client::builder()
                .api_key("test-key")
                .http_client(SequencedStreamingHttpClient::new(vec![
                    Ok(sse(&[MESSAGE_START, TEXT_START, TEXT_DELTA])),
                    Err(crate::http_client::Error::InvalidStatusCodeWithMessage(
                        http::StatusCode::BAD_GATEWAY,
                        "connection reset".to_string(),
                    )),
                ]))
                .build()
                .expect("build client");
            let model = client.completion_model(CLAUDE_SONNET_4_6);
            let request = model.completion_request("hello").build();
            let mut stream = crate::completion::CompletionModel::stream(&model, request)
                .await
                .expect("stream should open");

            let mut texts = Vec::new();
            let mut saw_error = false;
            let mut saw_terminal = false;
            while let Some(item) = stream.next().await {
                match item {
                    Ok(StreamedAssistantContent::Text(text)) => texts.push(text.text),
                    Ok(StreamedAssistantContent::Final(_)) => saw_terminal = true,
                    Ok(_) => {}
                    Err(_) => saw_error = true,
                }
            }

            assert_eq!(texts, ["hi"]);
            assert!(saw_error, "the transport failure must reach the consumer");
            assert!(
                !saw_terminal,
                "a failed stream must not synthesize a terminal record"
            );
            assert!(stream.response.is_none());
        }

        #[tokio::test]
        async fn provider_error_event_stops_the_stream_before_a_later_terminal() {
            // The findings-file probe: an in-band provider `error` event
            // followed by a well-formed `message_delta`. The error must reach
            // the consumer and NOTHING may follow it — the adapter is
            // finished, so the later terminal frame must not be interpreted
            // into a successful FinalResponse.
            const ERROR_EVENT: &str =
                r#"{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#;
            let (texts, saw_error, saw_terminal, stream) = collect(sse(&[
                MESSAGE_START,
                TEXT_START,
                TEXT_DELTA,
                ERROR_EVENT,
                MESSAGE_DELTA,
            ]))
            .await;

            assert_eq!(texts, ["hi"]);
            assert!(saw_error, "the provider error must reach the consumer");
            assert!(
                !saw_terminal,
                "a message_delta after an in-band provider error must not read as a completed turn"
            );
            assert!(stream.response.is_none());
        }

        /// `input_tokens` precedence between `message_start` and the terminal
        /// `message_delta`, across all three wire splits at once.
        ///
        /// Not a cassette test: one recording can only witness whichever split
        /// the endpoint it was recorded against happens to use, and the defect
        /// here is the *precedence rule* relating three of them — the gateway
        /// split, Anthropic proper, and the inverse. The gateway split is also
        /// covered end-to-end by the recorded
        /// `anthropic::cassette::streaming::gateway_reports_input_tokens_on_message_delta`;
        /// this pins the two cases a single recording structurally cannot show
        /// beside it.
        #[tokio::test]
        async fn input_tokens_prefer_the_terminal_delta_and_fall_back_to_message_start() {
            fn message_start(input_tokens: usize) -> String {
                format!(
                    r#"{{"type":"message_start","message":{{"id":"msg_1","role":"assistant","content":[],"model":"claude-sonnet-4-6","stop_reason":null,"stop_sequence":null,"usage":{{"input_tokens":{input_tokens},"output_tokens":0}}}}}}"#
                )
            }
            fn message_delta(input_tokens: usize) -> String {
                format!(
                    r#"{{"type":"message_delta","delta":{{"stop_reason":"end_turn","stop_sequence":null}},"usage":{{"input_tokens":{input_tokens},"output_tokens":3}}}}"#
                )
            }

            for (start, delta, expected, case) in [
                // OpenRouter's Anthropic Messages shape: `message_start`
                // reports a placeholder zero and the real prompt size lands on
                // the terminal `message_delta`.
                (
                    message_start(0),
                    message_delta(9),
                    9,
                    "a gateway reporting the prompt size on message_delta must reach the consumer",
                ),
                // A delta that omits `input_tokens` entirely — the Bedrock-compat
                // and older/leaner shapes. (Not current Anthropic, which sends
                // the count on both frames; that case is the one below, since
                // the two always agree.)
                (
                    message_start(5),
                    MESSAGE_DELTA.to_owned(),
                    5,
                    "a delta without input_tokens falls back to message_start",
                ),
                // Anthropic proper: both frames carry the same count.
                (
                    message_start(5),
                    message_delta(5),
                    5,
                    "agreeing frames report that count",
                ),
                // The inverse split: a zero on the delta must not erase the
                // real count `message_start` already gave us.
                (
                    message_start(5),
                    message_delta(0),
                    5,
                    "a zero on the delta must not erase the message_start count",
                ),
            ] {
                let (_texts, _saw_error, saw_terminal, stream) =
                    collect(sse(&[&start, TEXT_START, TEXT_DELTA, &delta])).await;

                assert!(saw_terminal, "{case}: the turn must complete");
                let terminal = stream.response.expect("terminal record");
                assert_eq!(terminal.usage.input_tokens, expected, "{case}");
            }
        }

        #[tokio::test]
        async fn malformed_frame_then_eof_yields_error_and_no_terminal_record() {
            let (texts, saw_error, saw_terminal, stream) =
                collect(sse(&[MESSAGE_START, TEXT_START, TEXT_DELTA, "{not json"])).await;

            assert_eq!(texts, ["hi"]);
            assert!(saw_error, "the malformed frame must reach the consumer");
            assert!(
                !saw_terminal,
                "a parse error followed by EOF must not read as a completed turn"
            );
            assert!(stream.response.is_none());
        }

        #[tokio::test]
        async fn malformed_frame_then_real_terminal_still_completes_the_stream() {
            let (texts, saw_error, saw_terminal, stream) = collect(sse(&[
                MESSAGE_START,
                TEXT_START,
                TEXT_DELTA,
                "{not json",
                MESSAGE_DELTA,
            ]))
            .await;

            assert_eq!(texts, ["hi"]);
            assert!(saw_error, "the malformed frame must reach the consumer");
            assert!(
                saw_terminal,
                "a genuine message_delta after a parse error still completes the stream"
            );
            let terminal = stream.response.expect("terminal record");
            assert_eq!(
                terminal.finish_reason,
                Some(crate::completion::FinishReason::Stop)
            );
            assert_eq!(terminal.message_id.as_deref(), Some("msg_1"));
        }

        /// Raw capture on the streaming terminal, through the real
        /// `CompletionModel::stream` seam over the mock transport:
        /// `normalize_stream` serializes the terminal before mapping it, so
        /// the terminal `StreamFinal.raw` is Anthropic's own
        /// `StreamingCompletionResponse`. A `message_delta` with
        /// `stop_sequence` set is used because the normalized terminal folds
        /// it into `FinishReason::Stop` and keeps neither Anthropic's spelling
        /// nor which sequence fired — both are readable only off the capture.
        #[tokio::test]
        async fn terminal_raw_round_trips_into_the_terminal_type() {
            const STOP_SEQUENCE_DELTA: &str = r#"{"type":"message_delta","delta":{"stop_reason":"stop_sequence","stop_sequence":"alpha"},"usage":{"output_tokens":3}}"#;

            let client = Client::builder()
                .api_key("test-key")
                .http_client(MockStreamingClient {
                    sse_bytes: sse(&[MESSAGE_START, TEXT_START, TEXT_DELTA, STOP_SEQUENCE_DELTA]),
                })
                .build()
                .expect("build client");
            let model = client.completion_model(CLAUDE_SONNET_4_6);
            let request = model.completion_request("hello").build();
            let mut stream = crate::completion::CompletionModel::stream(&model, request)
                .await
                .expect("stream should open");
            while let Some(item) = stream.next().await {
                item.expect("stream item");
            }
            let terminal = stream.response.expect("terminal record");

            let raw = &terminal.raw;
            let typed: super::super::StreamingCompletionResponse =
                serde_json::from_value(raw.clone()).expect("raw must deserialize");
            assert_eq!(
                serde_json::to_value(&typed).expect("re-serialize"),
                *raw,
                "the capture must be exactly what the terminal type serializes to"
            );
            assert_eq!(typed.stop_reason.as_deref(), Some("stop_sequence"));
            assert_eq!(typed.stop_sequence.as_deref(), Some("alpha"));
            assert_eq!(typed.message_id.as_deref(), Some("msg_1"));

            // Re-normalizing the capture tells the same story as the terminal
            // the stream produced.
            let renormalized = crate::streaming::StreamFinal::from(("anthropic", typed));
            assert_eq!(terminal.identity(), renormalized.identity());
            assert_eq!(terminal.finish_reason, renormalized.finish_reason);
            assert_eq!(terminal.model, renormalized.model);
            assert_eq!(terminal.usage, renormalized.usage);
            assert_eq!(
                terminal.finish_reason,
                Some(crate::completion::FinishReason::Stop)
            );
            assert_eq!(terminal.usage.output_tokens, 3);
        }
    }
}