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
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
//! The streaming module for the OpenAI Responses API.
//! Please see the `openai_streaming` or `openai_streaming_with_tools` example for more practical usage.
use crate::completion::{self, CompletionError};
use crate::http_client::HttpClientExt;
use crate::http_client::sse::GenericEventSource;
use crate::providers::internal::adapter::{
    AdapterOutput, WireAdapter, WireFrame, run_wire_buffered,
};
use crate::providers::internal::sse_transport::{
    FrameDisposition, OpenLog, SseTransportOptions, open_wire_stream,
};
use crate::providers::internal::wire::{self, WireEvent};
use crate::providers::openai::responses_api::{
    IncompleteDetailsReason, ReasoningSummary, ResponseStatus, ResponsesUsage,
};
use crate::streaming;
use crate::streaming::RawStreamingChoice;
use crate::telemetry::{CompletionOperation, CompletionSpanBuilder};
use crate::wasm_compat::WasmCompatSend;
use futures::StreamExt;
use serde::{Deserialize, Serialize};

use super::{CompletionResponse, GenericResponsesCompletionModel, Output, ResponsesProviderExt};

type StreamingRawChoice = RawStreamingChoice<StreamingCompletionResponse>;

// ================================================================
// OpenAI Responses Streaming API
// ================================================================

/// A streaming completion chunk.
/// Streaming chunks can come in one of two forms:
/// - A response chunk (where the completed response will have the total token usage)
/// - An item chunk commonly referred to as a delta. In the completions API this would be referred to as the message delta.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum StreamingCompletionChunk {
    Response(Box<ResponseChunk>),
    Delta(ItemChunk),
}

/// The final streaming response from the OpenAI Responses API.
///
/// This is the provider-native terminal record carried by
/// [`GenericResponsesCompletionModel::raw_stream`]. The normalized path maps it
/// once, through [`crate::streaming::normalize_stream`], into a
/// [`streaming::StreamFinal`].
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StreamingCompletionResponse {
    /// Token usage
    pub usage: ResponsesUsage,
    /// The complete object-shaped reasoning metadata from the terminal response event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reasoning_metadata: Option<serde_json::Map<String, serde_json::Value>>,
    /// The effective reasoning context from the terminal response event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reasoning_context: Option<String>,
    /// The `status` reported by the terminal `response.completed` event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<ResponseStatus>,
    /// Why the response stopped short, when the provider said so.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub incomplete_details: Option<IncompleteDetailsReason>,
    /// The assistant message ID (`msg_...`) carried by the terminal response's
    /// output items.
    ///
    /// Distinct from [`Self::response_id`] (`resp_...`), which names the whole
    /// response.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message_id: Option<String>,
    /// The response ID (`resp_...`) reported by the terminal
    /// `response.completed` event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub response_id: Option<String>,
    /// The model identifier reported by the terminal response event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// The transport request id from the SSE connection's `x-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>,
}

impl StreamingCompletionResponse {
    /// Create a terminal record carrying only usage; the remaining metadata is
    /// filled in from the terminal `response.completed` event as it arrives.
    pub fn new(usage: ResponsesUsage) -> Self {
        Self {
            usage,
            provider_request_id: None,
            reasoning_metadata: None,
            reasoning_context: None,
            status: None,
            incomplete_details: None,
            message_id: None,
            response_id: None,
            model: None,
        }
    }
}

/// Normalize the Responses API's terminal stream record.
///
/// The provider descriptor name is an input for the same reason it is on the
/// unary conversion: ChatGPT and Copilot stream this exact wire shape, so a
/// baked-in `"openai"` would mislabel them.
///
/// The finish reason is left exactly as the provider reported it;
/// [`crate::streaming::normalize_stream`] applies the tool-call reconciliation
/// afterwards, using the calls the stream actually emitted.
impl From<(&str, StreamingCompletionResponse)> for streaming::StreamFinal {
    fn from((provider, response): (&str, StreamingCompletionResponse)) -> Self {
        let finish_reason = response.status.as_ref().and_then(|status| {
            super::map_finish_reason(status, response.incomplete_details.as_ref())
        });

        streaming::StreamFinal::new(provider, crate::completion::Usage::from(&response.usage))
            .with_optional_finish_reason(finish_reason)
            .with_optional_message_id(response.message_id)
            .with_optional_response_id(response.response_id)
            .with_optional_provider_request_id(response.provider_request_id)
            .with_optional_model(response.model)
    }
}

/// Normalize a provider-native Responses stream for `provider`.
///
/// Maps only the terminal record; every incremental event passes through
/// untouched.
pub(crate) fn normalize_responses_stream(
    provider: &str,
    raw: streaming::RawStreamingResult<StreamingCompletionResponse>,
) -> streaming::StreamingCompletionResponse {
    let provider = provider.to_owned();
    let mapped_provider = provider.clone();
    let normalized = streaming::normalize_stream(raw, move |response| {
        Ok(streaming::StreamFinal::from((
            mapped_provider.as_str(),
            response,
        )))
    });

    streaming::StreamingCompletionResponse::stream(provider, normalized)
}

/// The done item's blocks as ONE authoritative end-of-part restatement.
///
/// Every block — summaries, content texts, `encrypted_content` — belongs to
/// one `rs_*` reasoning item, so it must land in one part: emitting a
/// whole-block choice per entry made every block after the first a sibling
/// part under the same key, and history then replayed duplicate reasoning
/// input items carrying the identical `rs_*` id. The restatement supersedes
/// the delta-built part in place (wire field order: summary, content,
/// encrypted). `None` when the item carries no blocks — an empty done item
/// says nothing at the boundary.
pub(crate) fn reasoning_end_from_done_item(
    id: &crate::streaming::StreamPartId,
    provider_id: Option<&crate::streaming::WireId>,
    summary: Vec<ReasoningSummary>,
    content: Vec<String>,
    encrypted_content: Option<String>,
) -> Option<RawStreamingChoice<StreamingCompletionResponse>> {
    // Same builder as the unary decode, so the restatement and the
    // non-streaming conversion of one item cannot drift.
    let blocks = super::reasoning_content_blocks(summary, content, encrypted_content);

    if blocks.is_empty() {
        return None;
    }

    Some(RawStreamingChoice::ReasoningEnd {
        id: id.clone(),
        reasoning: Some(crate::message::Reasoning {
            id: provider_id.map(|provider_id| provider_id.as_str().to_owned()),
            content: blocks,
        }),
        signature: None,
        wire_sent: true,
    })
}

impl From<&StreamingCompletionResponse> for crate::completion::Usage {
    fn from(response: &StreamingCompletionResponse) -> Self {
        Self::from(&response.usage)
    }
}

/// A response chunk from OpenAI's response API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ResponseChunk {
    /// The response chunk type
    #[serde(rename = "type")]
    pub kind: ResponseChunkKind,
    /// The response itself
    pub response: CompletionResponse,
    /// The item sequence
    pub sequence_number: u64,
}

/// Response chunk type.
/// Renames are used to ensure that this type gets (de)serialized properly.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum ResponseChunkKind {
    #[serde(rename = "response.created")]
    ResponseCreated,
    #[serde(rename = "response.in_progress")]
    ResponseInProgress,
    #[serde(rename = "response.completed")]
    ResponseCompleted,
    #[serde(rename = "response.failed")]
    ResponseFailed,
    #[serde(rename = "response.incomplete")]
    ResponseIncomplete,
}

fn provider_response_from_responses_error_value(
    value: &serde_json::Value,
    data: &str,
) -> CompletionError {
    if let Some(message) = value
        .get("error")
        .and_then(|error| error.get("message"))
        .and_then(serde_json::Value::as_str)
    {
        tracing::warn!(message, "provider returned a streaming error event");
    }

    crate::provider_response::completion_error_from_body(data)
}

/// Whether `kind` is a Responses SSE event type this client models.
///
/// The union of [`ResponseChunkKind`]'s and [`ItemChunkKind`]'s wire names: a
/// frame carrying one of these that still fails to deserialize is a data-level
/// defect in a known event, not an unknown event type, and must surface as an
/// error rather than be skipped.
fn is_known_responses_event_type(kind: &str) -> bool {
    matches!(
        kind,
        "response.created"
            | "response.in_progress"
            | "response.completed"
            | "response.failed"
            | "response.incomplete"
            | "response.output_item.added"
            | "response.output_item.done"
            | "response.content_part.added"
            | "response.content_part.done"
            | "response.output_text.delta"
            | "response.output_text.done"
            | "response.refusal.delta"
            | "response.refusal.done"
            | "response.function_call_arguments.delta"
            | "response.function_call_arguments.done"
            | "response.reasoning_summary_part.added"
            | "response.reasoning_summary_part.done"
            | "response.reasoning_summary_text.delta"
            | "response.reasoning_summary_text.done"
            | "response.reasoning_text.delta"
            | "response.reasoning_text.done"
    )
}

/// Classify one Responses SSE frame; see
/// [`crate::providers::internal::wire`] for the dispatch contract.
///
/// Shared by the live SSE loop, the buffered [`raw_choices_from_sse_body`]
/// path, and the websocket session so all apply the same known/unknown
/// boundary. Provider `error` events (and the websocket-only `response.done`)
/// are checked separately before this, because their `type` is outside the
/// modeled set yet must not be skipped as unknown.
pub(super) fn classify_responses_frame(data: &str) -> WireEvent<StreamingCompletionChunk> {
    wire::classify_tagged_frame(data, "type", is_known_responses_event_type)
}

fn provider_response_from_responses_sse_data(data: &str) -> Option<CompletionError> {
    let value = serde_json::from_str::<serde_json::Value>(data).ok()?;
    (value.get("type").and_then(serde_json::Value::as_str) == Some("error"))
        .then(|| provider_response_from_responses_error_value(&value, data))
}

#[derive(Clone, Copy)]
pub(crate) enum ResponsesStreamOptions {
    Strict,
    StrictWithImmediateToolCalls,
}

impl ResponsesStreamOptions {
    pub(crate) const fn strict() -> Self {
        Self::Strict
    }

    pub(crate) const fn strict_with_immediate_tool_calls() -> Self {
        Self::StrictWithImmediateToolCalls
    }

    const fn emits_completed_tool_calls_immediately(self) -> bool {
        matches!(self, Self::StrictWithImmediateToolCalls)
    }
}

/// The payload of every content-bearing `data:` line in a buffered SSE body.
///
/// Blank lines, non-`data:` fields (SSE comments, `event:`), and the `[DONE]`
/// sentinel are skipped, so both buffered readers below see exactly the frame
/// payloads a live transport would deliver.
fn sse_data_frames(body: &str) -> impl Iterator<Item = &str> {
    body.lines()
        .map(|line| {
            line.strip_prefix("data:")
                .map(str::trim)
                .unwrap_or_default()
        })
        .filter(|data| !data.is_empty() && *data != "[DONE]")
}

pub(crate) fn parse_sse_completion_body(
    body: &str,
    provider_name: &str,
) -> Result<CompletionResponse, CompletionError> {
    let mut completed = None;

    for data in sse_data_frames(body) {
        if let Ok(chunk) = serde_json::from_str::<StreamingCompletionChunk>(data) {
            if let StreamingCompletionChunk::Response(chunk) = chunk {
                let ResponseChunk { kind, response, .. } = *chunk;
                match kind {
                    // `response.incomplete` is a genuine terminal; the unary
                    // conversion maps its status to a finish reason.
                    ResponseChunkKind::ResponseCompleted
                    | ResponseChunkKind::ResponseIncomplete => {
                        completed = Some(response);
                        break;
                    }
                    ResponseChunkKind::ResponseFailed => {
                        return Err(crate::provider_response::completion_error_from_body(data));
                    }
                    _ => {}
                }
            }
            continue;
        }

        let value = match serde_json::from_str::<serde_json::Value>(data) {
            Ok(value) => value,
            Err(_) => continue,
        };

        match value.get("type").and_then(serde_json::Value::as_str) {
            Some("response.completed") | Some("response.incomplete") => {
                if let Some(response) = value.get("response") {
                    completed = Some(serde_json::from_value(response.clone())?);
                    break;
                }
            }
            Some("response.failed") => {
                return Err(crate::provider_response::completion_error_from_body(data));
            }
            Some("error") => {
                return Err(provider_response_from_responses_error_value(&value, data));
            }
            _ => {}
        }
    }

    completed.ok_or_else(|| {
        CompletionError::ProviderError(format!(
            "{provider_name} stream did not yield a terminal response event (response.completed or response.incomplete)"
        ))
    })
}

pub(crate) struct RawChoiceAccumulator {
    final_usage: ResponsesUsage,
    reasoning_metadata: Option<serde_json::Map<String, serde_json::Value>>,
    reasoning_context: Option<String>,
    status: Option<ResponseStatus>,
    incomplete_details: Option<IncompleteDetailsReason>,
    message_id: Option<String>,
    response_id: Option<String>,
    model: Option<String>,
    /// Buffered tool-input end events for calls delivered whole by
    /// `output_item.done`, flushed at the terminal (or before a terminal
    /// error). Assembly and internal-id correlation live in the shared
    /// accumulator, keyed by the function-call item id the added/delta/done
    /// events share.
    tool_calls: Vec<StreamingRawChoice>,
    /// Whether a genuine terminal event (`response.completed` or
    /// `response.incomplete`) arrived. Without one the stream was truncated,
    /// and `finish` withholds the terminal record.
    saw_terminal: bool,
    /// Slot-scoped reasoning identity, mirroring `tool_slots`: one assembly
    /// key per output slot, fixed at the slot's FIRST reasoning event (wire
    /// `rs_*` id when it carries one, else minted `output-{index}`) and
    /// reused by every later frame regardless of the id it carries.
    /// Gateways and ChatGPT's envelope-less replay bodies omit the id on a
    /// subset of a slot's events; per-event resolution split one slot into
    /// `Wire("rs_1")` and `Minted(Output, i)` halves, and the done item
    /// superseded only one of them — the other survived as an orphaned
    /// partial part carrying the same provider id (#2258 F3 and its mixed
    /// generalization).
    reasoning_slots: std::collections::HashMap<u64, crate::streaming::StreamPartId>,
    /// Tool-call identities minted for function-call items whose wire events
    /// carried no `fc_*` id (gateways and the ChatGPT envelope-less replay
    /// bodies), keyed by output slot. Mirrors `minted_reasoning_ids`: the
    /// added/delta/done events of one item must all share one assembly key —
    /// forwarding `""` verbatim would let two parallel id-less calls share the
    /// empty key, and an id-less delta whose done restates a real `fc_*` id
    /// would leave the fragments dangling under a different key.
    /// Slot-scoped tool identity: one assembly key per output slot, fixed at
    /// the slot's first event (wire `fc_*` id, else minted `output-{index}`),
    /// reused by every later event regardless of the id it carries — mixed
    /// id/id-less events on one slot can no longer split assembly keys.
    tool_slots: crate::providers::internal::tool_call_bridge::ToolCallBridge<u64>,
    /// The `call_…` correlator each open slot announced on
    /// `output_item.added`, kept beside the bridge so a slot closed by the
    /// terminal drain (its `output_item.done` frame was lost) still
    /// finalizes with the dual-wire identity Responses replay pairs on.
    pending_call_ids: std::collections::HashMap<u64, String>,
    /// The message item whose text block is currently open. A text or
    /// refusal delta carrying a different `item_id` opens a new text block
    /// (`TextStart` keyed by that item id), so two `message` output items
    /// aggregate as two distinct text parts instead of concatenating.
    /// Deltas without an `item_id` (ChatGPT's envelope-less replays) extend
    /// the open block, or open a boundary-minted one downstream.
    current_text_item: Option<String>,
}

impl RawChoiceAccumulator {
    pub(crate) fn new(initial_usage: ResponsesUsage) -> Self {
        Self {
            final_usage: initial_usage,
            reasoning_metadata: None,
            reasoning_context: None,
            status: None,
            incomplete_details: None,
            message_id: None,
            response_id: None,
            model: None,
            tool_calls: Vec::new(),
            saw_terminal: false,
            reasoning_slots: std::collections::HashMap::new(),
            tool_slots:
                crate::providers::internal::tool_call_bridge::ToolCallBridge::with_minted_namespace(
                    crate::streaming::SyntheticIds::output(),
                ),
            pending_call_ids: std::collections::HashMap::new(),
            current_text_item: None,
        }
    }

    /// Open the text block for the message item a text/refusal delta belongs
    /// to, when the wire identifies it and it differs from the open one.
    fn start_text_item(
        &mut self,
        item_id: &Option<String>,
        immediate: &mut Vec<StreamingRawChoice>,
    ) {
        if let Some(item_id) = item_id
            && self.current_text_item.as_deref() != Some(item_id)
        {
            self.current_text_item = Some(item_id.clone());
            immediate.push(streaming::RawStreamingChoice::TextStart {
                id: crate::streaming::StreamPartId::wire(item_id.clone()),
                additional_params: None,
            });
        }
    }

    /// The slot's reasoning assembly key, fixed at its first reasoning
    /// event: the wire's `rs_*` id when that first frame carries one, else
    /// a minted `output-{index}` identity. Every later frame on the slot
    /// reuses the stored key regardless of the id it carries — the same
    /// discipline as `tool_slots` — so mixed id/id-less frames cannot
    /// split one slot's assembly. A late-arriving wire id upgrades the
    /// part's durable `provider_id` (carried as data on each event), never
    /// the accumulation key.
    fn reasoning_slot_key(
        &mut self,
        output_index: u64,
        item_id: Option<&str>,
    ) -> crate::streaming::StreamPartId {
        if let Some(key) = self.reasoning_slots.get(&output_index) {
            return key.clone();
        }
        // Minted from the bridge's ONE counter (tool_call_bridge's own
        // invariant): a second sequence stamping `Minted{Output, index}`
        // could collide with an assembly the bridge minted the same value
        // for. The per-slot map above, not the mint, is what keeps the key
        // stable across the slot's frames.
        let key = item_id
            .map(crate::streaming::StreamPartId::wire)
            .unwrap_or_else(|| self.tool_slots.minted_ids().mint());
        self.reasoning_slots.insert(output_index, key.clone());
        key
    }

    pub(crate) fn decode_item_chunk(
        &mut self,
        chunk: ItemChunk,
        options: ResponsesStreamOptions,
    ) -> Vec<StreamingRawChoice> {
        let mut immediate = Vec::new();

        let ItemChunk {
            item_id: outer_item_id,
            output_index,
            data: item,
        } = chunk;

        match item {
            ItemChunkKind::OutputItemAdded(StreamingItemDoneOutput {
                item: Output::FunctionCall(func),
                ..
            }) => {
                // A function-call item interleaving a message item closes the
                // open text block; forget it so a later delta for that message
                // re-emits `TextStart` and reactivates its block downstream.
                self.current_text_item = None;
                // Slot identity is established here once (wire `fc_*` id,
                // else a minted `output-{index}`) and reused for every later
                // event on this slot — gateways and ChatGPT's envelope-less
                // replay bodies can omit the id on any subset of a slot's
                // events, and event-scoped resolution would split the
                // assembly key.
                let key = self
                    .tool_slots
                    .open(output_index, Some(&func.id), Some(&func.name))
                    .key()
                    .to_owned();
                if !func.call_id.is_empty() {
                    self.pending_call_ids
                        .insert(output_index, func.call_id.clone());
                }
                immediate.push(streaming::RawStreamingChoice::ToolCallDelta {
                    id: key,
                    content: streaming::ToolCallDeltaContent::Name(func.name),
                });
            }
            ItemChunkKind::OutputItemDone(message) => {
                // Any completed item ends the block it carried; a text delta
                // arriving afterwards belongs to a (re)opened block.
                self.current_text_item = None;
                self.push_output_item_done(
                    message.item,
                    output_index,
                    &mut immediate,
                    options.emits_completed_tool_calls_immediately(),
                );
            }
            // Text and refusal deltas are the same visible-text stream: a
            // refusal is the assistant's message for that turn, and both
            // (re)open the item's text block before their fragment.
            ItemChunkKind::OutputTextDelta(DeltaTextChunk { delta, .. })
            | ItemChunkKind::RefusalDelta(DeltaTextChunk { delta, .. }) => {
                self.start_text_item(&outer_item_id, &mut immediate);
                immediate.push(streaming::RawStreamingChoice::Message(delta));
            }
            // Summary and raw-reasoning deltas differ only in which wire
            // event carries them; both are fragments of the output item's
            // reasoning block and accumulate under its slot identity.
            ItemChunkKind::ReasoningSummaryTextDelta(SummaryTextChunk { delta, .. })
            | ItemChunkKind::ReasoningTextDelta(DeltaTextChunkWithItemId { delta, .. }) => {
                // Reasoning interleaving text closes the open text block
                // downstream (`PartsAccumulator::reasoning_delta`); forget the
                // open message item so a later delta for the *same* item
                // re-emits `TextStart {id}` and reactivates its block instead
                // of silently opening a boundary-minted sibling (#2258 P2).
                self.current_text_item = None;
                let id = self.reasoning_slot_key(output_index, outer_item_id.as_deref());
                immediate.push(streaming::RawStreamingChoice::ReasoningDelta {
                    id,
                    provider_id: outer_item_id
                        .clone()
                        .and_then(crate::streaming::WireId::new),
                    reasoning: delta,
                });
            }
            ItemChunkKind::FunctionCallArgsDelta(delta) => {
                // Tool output interleaving text is a block boundary too.
                self.current_text_item = None;
                // The slot's established identity keys the fragment; an
                // id-less delta on a never-opened slot mints it here so the
                // fragments survive truncation before the authoritative
                // `output_item.done` restatement (#2258 P3). A late wire id
                // updates the slot's reported id without moving the key.
                let slot = self
                    .tool_slots
                    .open(output_index, outer_item_id.as_deref(), None);
                slot.saw_arguments_delta = true;
                let key = slot.key().clone();
                immediate.push(streaming::RawStreamingChoice::ToolCallDelta {
                    id: key,
                    content: streaming::ToolCallDeltaContent::Delta(delta.delta),
                });
            }
            _ => {}
        }

        immediate
    }

    pub(crate) fn record_response_chunk(
        &mut self,
        kind: ResponseChunkKind,
        response: CompletionResponse,
        raw_event_data: &str,
    ) -> Result<(), CompletionError> {
        match kind {
            // `response.incomplete` is a genuine terminal (e.g. hitting
            // `max_output_tokens`): the partial output and usage are kept, and
            // the recorded status/incomplete_details map to the finish reason
            // downstream, matching the unary path's `map_finish_reason`.
            ResponseChunkKind::ResponseCompleted | ResponseChunkKind::ResponseIncomplete => {
                self.saw_terminal = true;
                // The provider proved the turn ended, so a slot still open
                // here lost only its `output_item.done` frame — the same
                // terminal-drain the sibling adapters ship (Interactions at
                // `interaction.completed`, chat-compat at `finish_reason`).
                // Closing it lets the shared accumulator finalize the call
                // from its streamed fragments (parse-or-drop), instead of
                // discarding a provider-completed call as truncation.
                for (index, slot) in self.tool_slots.drain_ordered_indexed() {
                    let mut end = slot.end_event(streaming::UnparseableToolInput::Drop);
                    end.call_id = self.pending_call_ids.remove(&index);
                    self.tool_calls
                        .push(streaming::RawStreamingChoice::ToolInputEnd(end));
                }
                // The terminal event is the only place the stream learns how the
                // turn ended, which model answered, and which assistant message
                // (`msg_...`, not the response's `resp_...`) carried the output.
                if let Some(message_id) = message_id_from_response(&response) {
                    self.message_id = Some(message_id);
                }
                if !response.id.is_empty() {
                    self.response_id = Some(response.id.clone());
                }
                if !response.model.is_empty() {
                    self.model = Some(response.model.clone());
                }
                self.status = Some(response.status);
                if response.incomplete_details.is_some() {
                    self.incomplete_details = response.incomplete_details;
                }
                if let Some(usage) = response.usage {
                    self.final_usage = usage;
                }
                if response.reasoning_metadata.is_some() {
                    self.reasoning_metadata = response.reasoning_metadata;
                }
                if response.reasoning_context.is_some() {
                    self.reasoning_context = response.reasoning_context;
                }
                Ok(())
            }
            ResponseChunkKind::ResponseFailed => Err(
                crate::provider_response::completion_error_from_body(raw_event_data),
            ),
            _ => Ok(()),
        }
    }

    fn push_output_item_done(
        &mut self,
        item: Output,
        output_index: u64,
        immediate: &mut Vec<StreamingRawChoice>,
        emit_completed_tool_calls_immediately: bool,
    ) {
        match item {
            Output::FunctionCall(func) => {
                // The done item restates the call whole; its fields are
                // authoritative over any assembled fragments, and the shared
                // accumulator correlates by the shared item id (minting the
                // internal id if no fragments preceded).
                //
                // Identity mirrors the reasoning arm below: when this slot's
                // added/delta events carried no `fc_*` id they were keyed by
                // the minted `output-{index}` identity, and the done event
                // must find that same key (even when it restates a real id)
                // or the assembled fragments dangle. A slot with no minted
                // identity keeps the wire id, minting only when it is empty.
                let slot = self.tool_slots.remove(output_index);
                // The done item restates its own call_id; the announce-time
                // copy is only for slots the terminal drain must close.
                self.pending_call_ids.remove(&output_index);
                let item_id = match &slot {
                    // The slot's established key wins even when the done item
                    // restates a real `fc_*` id — assembled fragments must
                    // not dangle under a different key.
                    Some(slot) => slot.key().clone(),
                    // Minted from the bridge's ONE counter: a done-only call
                    // stamping `Minted{Output, index}` from a second sequence
                    // could collide with a mid-assembly key the bridge minted
                    // the same value for, consuming that assembly under the
                    // wrong call.
                    None if func.id.is_empty() => self.tool_slots.minted_ids().mint(),
                    None => crate::streaming::StreamPartId::wire(func.id.clone()),
                };
                let mut end = streaming::ToolInputEnd::new(
                    item_id.clone(),
                    streaming::UnparseableToolInput::Drop,
                );
                end.name = Some(func.name);
                // The finalized call reports the authoritative wire id even
                // when assembly keyed on a minted slot identity (the
                // accumulator honors the override).
                end.tool_id = crate::streaming::WireId::new(func.id.clone());
                // The restated arguments are authoritative when they parse. A
                // turn cut by `max_output_tokens` mid-tool-call restates them
                // truncated mid-JSON (item status `incomplete`); routing the
                // raw string through the assembly buffer instead lets the
                // shared accumulator apply the settled truncation policy
                // (`UnparseableToolInput::Drop` — partial arguments never
                // fabricate a call), including when no argument fragments
                // preceded the done item.
                match func.arguments.parse() {
                    Ok(arguments) => end.arguments = Some(arguments),
                    // Fragments already streamed these bytes into the
                    // assembly buffer — re-emitting the restatement doubled
                    // them (rendered twice by delta consumers and
                    // double-charged against the accumulation bound). Only a
                    // fragment-less done item (pure replay of a truncated
                    // restatement) routes its raw string through the buffer,
                    // so the truncation policy still has bytes to judge.
                    Err(_) => {
                        let saw_fragments =
                            slot.as_ref().is_some_and(|slot| slot.saw_arguments_delta);
                        if !saw_fragments {
                            immediate.push(streaming::RawStreamingChoice::ToolCallDelta {
                                id: item_id,
                                content: streaming::ToolCallDeltaContent::Delta(
                                    func.arguments.as_str().to_owned(),
                                ),
                            });
                        }
                    }
                }
                end.call_id = Some(func.call_id);
                let end = streaming::RawStreamingChoice::ToolInputEnd(end);

                if emit_completed_tool_calls_immediately {
                    immediate.push(end);
                } else {
                    self.tool_calls.push(end);
                }
            }
            Output::Reasoning {
                id,
                summary,
                content,
                encrypted_content,
                ..
            } => {
                // The done item resolves through the slot map: its full
                // blocks must share whatever identity the slot's deltas
                // established (wire or minted) to supersede the delta-built
                // part — keying them by the item's own `rs_*` id would
                // append the restated content beside a minted-keyed part.
                // A slot with no established identity keeps the wire id
                // (the pure-replay shape). The durable handle is the item's
                // real `rs_*` id regardless of the accumulation key.
                let provider_id = crate::streaming::WireId::new(id.clone());
                let key = self
                    .reasoning_slots
                    .remove(&output_index)
                    .unwrap_or(crate::streaming::StreamPartId::wire(id));
                immediate.extend(reasoning_end_from_done_item(
                    &key,
                    provider_id.as_ref(),
                    summary,
                    content,
                    encrypted_content,
                ));
            }
            Output::Message(message) => {
                immediate.push(streaming::RawStreamingChoice::MessageId(message.id));
            }
            // An unmodeled output item (e.g. a hosted-tool result such as
            // `web_search_call`) arriving on `response.output_item.done`. Surface
            // the raw item to stream consumers, mirroring how the non-streaming
            // decode preserves it on `CompletionResponse.output`.
            Output::Unknown(value) => {
                immediate.push(streaming::RawStreamingChoice::Unknown(value.into()));
            }
        }
    }

    /// Drain the buffered fully-delivered tool calls without finishing the
    /// stream. The errored-terminal path flushes these before the error and
    /// must not produce a terminal record.
    pub(crate) fn take_tool_calls(&mut self) -> Vec<StreamingRawChoice> {
        std::mem::take(&mut self.tool_calls)
    }

    pub(crate) fn finish(mut self) -> Vec<StreamingRawChoice> {
        let mut choices = Vec::new();
        choices.append(&mut self.tool_calls);
        // Only a genuine terminal event (`response.completed` or
        // `response.incomplete`) counts as the provider ending the turn; a
        // stream that ended without one was truncated,
        // and a synthesized terminal record would present the partial turn as
        // a successful, default-usage completion.
        if !self.saw_terminal {
            return choices;
        }
        choices.push(RawStreamingChoice::FinalResponse(
            StreamingCompletionResponse {
                usage: self.final_usage,
                // Stamped by the transport layer.
                provider_request_id: None,
                reasoning_metadata: self.reasoning_metadata,
                reasoning_context: self.reasoning_context,
                status: self.status,
                incomplete_details: self.incomplete_details,
                message_id: self.message_id,
                response_id: self.response_id,
                model: self.model,
            },
        ));
        choices
    }
}

/// Repair an envelope-less Responses frame so the shared typed decode can
/// interpret it.
///
/// ChatGPT's replayed (unary) SSE bodies omit envelope bookkeeping fields
/// (`sequence_number`, `output_index`, `content_index`, `summary_index`)
/// that the typed frame decode requires. Those fields are bookkeeping only —
/// no semantic decision reads them beyond the reasoning-identity fallback,
/// which treats a missing `output_index` as `0` anyway — so injecting
/// neutral zeros where they are absent turns salvage into a preprocessing
/// step in front of the ONE event interpreter instead of a second one.
/// Data-level fields (`delta`, `item`, `response`, …) are never touched, so
/// a frame that is defective in its content still fails the re-decode.
///
/// **Policy decision — buffered-only, deliberately asymmetric with the live
/// loop (#2258 F8):** a *live* SSE or websocket frame with a known `type` but
/// a missing envelope field classifies `Corrupt` and surfaces as an in-band
/// `Err` item; it is never repaired. Only ChatGPT's replayed unary bodies
/// verifiably omit the envelope bookkeeping (every recorded live Copilot and
/// OpenAI cassette carries full envelopes), so on a live wire an
/// envelope-less known frame is evidence of a defective gateway, and
/// silently repairing it would mask the defect the `Corrupt` classification
/// exists to surface. The old live behavior (skip) hid the frame entirely;
/// the `Err` item is the stated uniform policy for defective known frames.
///
/// **Known limit — identity collapse, and why the obvious fix is worse
/// (#2258 G2):** the injected `output_index: 0` is the reasoning-identity
/// fallback's key when `item_id` is also absent (see `reasoning_item_id` in
/// [`RawChoiceAccumulator::decode_item_chunk`]). A body that omits BOTH
/// `item_id` *and* `output_index` across two or more items therefore collapses
/// them onto the single minted identity `output-0`, merging what the provider
/// sent as separate reasoning parts. A sweep of every recorded cassette found
/// **zero** bodies of that shape: ChatGPT's replayed bodies drop the
/// bookkeeping fields but keep `item_id`, and every body that drops `item_id`
/// carries `output_index`. So the collapse is reachable in principle and
/// unobserved in practice.
///
/// It is deliberately NOT fixed with a per-frame counter (mint `0, 1, 2, …` as
/// frames arrive). Envelope-less bodies are exactly the ones where consecutive
/// frames belong to the SAME item: a counter would hand every delta of one
/// reasoning block a different index, shattering one item into N single-delta
/// parts. That is a real regression against a real recorded shape — it breaks
/// `envelope_less_reasoning_deltas_are_superseded_by_their_done_item`, whose
/// whole point is that the deltas and their `output_item.done` share one
/// identity. Any future fix must key on something the body actually carries
/// (item boundaries), not on arrival order.
///
/// Returns `None` when the frame is not a JSON object (nothing to repair).
fn repair_envelope_less_frame(data: &str) -> Option<String> {
    let mut value = serde_json::from_str::<serde_json::Value>(data).ok()?;
    let object = value.as_object_mut()?;
    for field in [
        "sequence_number",
        "output_index",
        "content_index",
        "summary_index",
    ] {
        object
            .entry(field)
            .or_insert_with(|| serde_json::Value::from(0));
    }
    serde_json::to_string(&value).ok()
}

pub(crate) fn raw_choices_from_sse_body(
    body: &str,
    initial_usage: ResponsesUsage,
) -> Result<Vec<StreamingRawChoice>, CompletionError> {
    // Framing layer for the buffered (unary) Responses SSE body: line
    // splitting, sentinel skipping, and the provider `error` envelope
    // pre-check (which fails the operation, mirroring the live transport).
    // Classification and policy live in the buffered driver.
    let mut frames = Vec::new();
    for data in sse_data_frames(body) {
        if let Some(error) = provider_response_from_responses_sse_data(data) {
            return Err(error);
        }

        frames.push(WireFrame::Text(data.to_owned()));
    }

    // The SAME interpreter as the live loop (`classify_responses_frame`
    // feeding `RawChoiceAccumulator`), under [`run_wire_buffered`]'s
    // no-stream policy: there is no stream to carry `Err` items, so `Corrupt`
    // frames — and adapter-detected data errors like `response.failed` — fail
    // the whole operation instead of returning a silently partial completion.
    // Buffered classification adds the envelope-repair salvage; see
    // [`ResponsesAdapter::buffered`].
    run_wire_buffered(frames, ResponsesAdapter::buffered(initial_usage))
}

pub(crate) async fn completion_response_from_sse_body(
    provider: &str,
    body: &str,
    raw_response: CompletionResponse,
) -> Result<completion::CompletionResponse, CompletionError> {
    let raw_choices = raw_choices_from_sse_body(
        body,
        raw_response
            .usage
            .clone()
            .unwrap_or_else(ResponsesUsage::new),
    )?;
    completion_response_from_raw_choices(provider, raw_choices, &raw_response)
        .await?
        .ok_or_else(|| CompletionError::ResponseError("Response contained no parts".to_owned()))
}

/// Replay accumulated raw choices through [`normalize_responses_stream`] and
/// merge the result with the parsed terminal response body.
///
/// The replayed stream is authoritative where it reported something; the
/// terminal body fills any gap it left (usage, message ID, finish reason,
/// model). Returns `Ok(None)` when the replay produced no content, leaving the
/// caller to decide how to fall back.
pub(crate) async fn completion_response_from_raw_choices(
    provider: &str,
    raw_choices: Vec<StreamingRawChoice>,
    raw_response: &CompletionResponse,
) -> Result<Option<completion::CompletionResponse>, CompletionError> {
    let stream = futures::stream::iter(
        raw_choices
            .into_iter()
            .map(Ok::<_, CompletionError>)
            .collect::<Vec<_>>(),
    );
    let mut stream = normalize_responses_stream(provider, Box::pin(stream));

    while let Some(item) = stream.next().await {
        item?;
    }

    if choice_is_empty(&stream.choice) {
        return Ok(None);
    }

    // Merge per content kind: the replayed choice is authoritative for what it
    // carried (reasoning, tool calls, streamed text), but some backends emit
    // message text only in the terminal body while streaming other kinds as
    // deltas. A replay with no message text takes the body's message content;
    // everything replayed is kept.
    let mut choice = std::mem::take(&mut stream.choice);
    // Presence of ANY streamed text — even whitespace — means the deltas were
    // the content channel; merging the body then would duplicate it.
    let replay_has_message_text = choice.iter().any(|content| {
        matches!(
            content,
            completion::AssistantContent::Text(text) if !text.text.is_empty()
        )
    });
    if !replay_has_message_text {
        choice.extend(
            raw_response
                .output
                .iter()
                .filter(|item| matches!(item, Output::Message(_)))
                .cloned()
                .flat_map(<Vec<completion::AssistantContent>>::from),
        );
    }

    let terminal = stream.response.clone();
    let usage = terminal
        .as_ref()
        .map(|terminal| terminal.usage)
        .unwrap_or_else(|| usage_from_raw_response(raw_response));
    let message_id = stream
        .message_id
        .clone()
        .or_else(|| message_id_from_response(raw_response));
    let finish_reason = terminal
        .as_ref()
        .and_then(|terminal| terminal.finish_reason.clone())
        .or_else(|| {
            super::map_finish_reason(
                &raw_response.status,
                raw_response.incomplete_details.as_ref(),
            )
        });
    let model = terminal
        .as_ref()
        .and_then(|terminal| terminal.model.clone())
        .or_else(|| Some(raw_response.model.clone()).filter(|model| !model.is_empty()));

    let response_id = stream
        .response
        .as_ref()
        .and_then(|terminal| terminal.response_id.clone())
        .or_else(|| Some(raw_response.id.clone()).filter(|id| !id.is_empty()));

    Ok(Some(
        completion::CompletionResponse::new(choice, usage, provider)
            .with_optional_message_id(message_id)
            .with_optional_response_id(response_id)
            .with_optional_model(model)
            .with_optional_finish_reason(finish_reason),
    ))
}

fn choice_is_empty(choice: &[completion::AssistantContent]) -> bool {
    choice.iter().all(|content| match content {
        completion::AssistantContent::Text(text) => text.text.trim().is_empty(),
        completion::AssistantContent::Reasoning(reasoning) => reasoning.content.is_empty(),
        completion::AssistantContent::Image(_) => false,
        completion::AssistantContent::ToolCall(_) => false,
    })
}

fn message_id_from_response(response: &CompletionResponse) -> Option<String> {
    response.output.iter().find_map(|item| match item {
        Output::Message(message) => Some(message.id.clone()),
        _ => None,
    })
}

fn usage_from_raw_response(response: &CompletionResponse) -> completion::Usage {
    response
        .usage
        .as_ref()
        .map(completion::Usage::from)
        .unwrap_or_default()
}

/// Open a Responses SSE stream whose terminal record stays provider-native.
///
/// Pass the result through [`normalize_responses_stream`] to obtain the
/// normalized stream that [`completion::CompletionModel::stream`] returns.
pub(crate) fn raw_stream_from_event_source<HttpClient, RequestBody>(
    event_source: GenericEventSource<HttpClient, RequestBody>,
    span: tracing::Span,
) -> streaming::RawStreamingResult<StreamingCompletionResponse>
where
    HttpClient: HttpClientExt + Clone + 'static,
    RequestBody: Into<bytes::Bytes> + Clone + WasmCompatSend + 'static,
{
    raw_stream_from_event_source_with_options(event_source, span, ResponsesStreamOptions::strict())
}

pub(crate) fn raw_stream_from_event_source_with_options<HttpClient, RequestBody>(
    event_source: GenericEventSource<HttpClient, RequestBody>,
    span: tracing::Span,
    options: ResponsesStreamOptions,
) -> streaming::RawStreamingResult<StreamingCompletionResponse>
where
    HttpClient: HttpClientExt + Clone + 'static,
    RequestBody: Into<bytes::Bytes> + Clone + WasmCompatSend + 'static,
{
    // The wire's in-band provider `error` envelope is a terminal transport
    // condition, detected pre-classification exactly as an HTTP failure
    // would be.
    open_wire_stream(
        event_source,
        SseTransportOptions {
            open_log: OpenLog::Trace,
            stream_ended_is_error: false,
            log_transport_errors: true,
        },
        |data| {
            if data.trim().is_empty() || data == "[DONE]" {
                return FrameDisposition::Skip;
            }
            if let Some(error) = provider_response_from_responses_sse_data(&data) {
                // A terminal failure: the driver flushes fully-delivered
                // content, yields this error last, and emits no terminal
                // record.
                return FrameDisposition::Fail(error);
            }
            FrameDisposition::Frame(data)
        },
        ResponsesAdapter::live(options),
        span,
    )
}

/// One classified Responses frame, carrying its raw payload alongside the
/// decoded chunk: `response.failed` preserves the raw event body as the
/// provider error body, exactly as the pre-migration loop did.
pub(crate) struct ResponsesFrameEvent {
    raw: String,
    chunk: StreamingCompletionChunk,
}

/// The OpenAI Responses SSE wire as a [`WireAdapter`], shared by the live
/// loop ([`run_wire_stream`]) and the buffered unary path
/// ([`run_wire_buffered`]).
///
/// Holds the per-stream assembly state ([`RawChoiceAccumulator`]); frame
/// triage policy lives in the drivers, not here. The two modes differ only in
/// classification: the buffered mode adds the envelope-repair salvage for
/// ChatGPT's replayed bodies (see [`repair_envelope_less_frame`] for why the
/// live wire deliberately does NOT repair).
pub(crate) struct ResponsesAdapter {
    accumulator: RawChoiceAccumulator,
    options: ResponsesStreamOptions,
    /// Buffered-only envelope salvage; `false` on the live wire.
    repair_envelopes: bool,
    /// A `response.failed` event ended the turn: the flush-then-`Err`
    /// sequence has been pushed and the driver stops consuming.
    finished: bool,
}

impl ResponsesAdapter {
    fn live(options: ResponsesStreamOptions) -> Self {
        Self {
            accumulator: RawChoiceAccumulator::new(ResponsesUsage::new()),
            options,
            repair_envelopes: false,
            finished: false,
        }
    }

    fn buffered(initial_usage: ResponsesUsage) -> Self {
        Self {
            accumulator: RawChoiceAccumulator::new(initial_usage),
            options: ResponsesStreamOptions::strict(),
            repair_envelopes: true,
            finished: false,
        }
    }
}

impl WireAdapter for ResponsesAdapter {
    type Frame = WireFrame;
    type Event = ResponsesFrameEvent;
    type Response = StreamingCompletionResponse;

    fn classify(&self, frame: WireFrame) -> WireEvent<ResponsesFrameEvent> {
        let data = frame.as_str().into_owned();
        let event = if self.repair_envelopes {
            // Buffered bodies (ChatGPT's replayed unary SSE) omit envelope
            // bookkeeping fields; salvage through the SAME interpreter, with
            // the operation-error wording the buffered driver surfaces
            // verbatim.
            wire::classify_with_repair(
                &data,
                classify_responses_frame,
                repair_envelope_less_frame,
                |corrupt| {
                    <serde_json::Error as serde::de::Error>::custom(format!(
                        "invalid JSON frame in buffered Responses SSE body: {corrupt}"
                    ))
                },
                || {
                    let kind = serde_json::from_str::<serde_json::Value>(&data)
                        .ok()
                        .and_then(|value| {
                            value
                                .get("type")
                                .and_then(serde_json::Value::as_str)
                                .map(ToOwned::to_owned)
                        })
                        .unwrap_or_default();
                    <serde_json::Error as serde::de::Error>::custom(format!(
                        "malformed `{kind}` event in buffered Responses SSE body"
                    ))
                },
            )
        } else {
            classify_responses_frame(&data)
        };
        event.map(|chunk| ResponsesFrameEvent { raw: data, chunk })
    }

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

        match event.chunk {
            StreamingCompletionChunk::Delta(chunk) => {
                out.extend(
                    self.accumulator
                        .decode_item_chunk(chunk, self.options)
                        .into_iter()
                        .map(Ok),
                );
            }
            StreamingCompletionChunk::Response(chunk) => {
                let ResponseChunk { kind, response, .. } = *chunk;
                if matches!(kind, ResponseChunkKind::ResponseCompleted) {
                    let span = tracing::Span::current();
                    span.record("gen_ai.response.id", response.id.as_str());
                    span.record("gen_ai.response.model", response.model.as_str());
                }
                if let Err(error) = self
                    .accumulator
                    .record_response_chunk(kind, response, &event.raw)
                {
                    // `response.failed`: fully-delivered tool calls flush
                    // before the terminal error, which ends the stream with
                    // no terminal record, preserving the failure signal.
                    out.extend(self.accumulator.take_tool_calls().into_iter().map(Ok));
                    out.push(Err(error));
                    self.finished = true;
                }
            }
        }
    }

    fn finish(&mut self, out: &mut AdapterOutput<Self::Response>) {
        let accumulator = std::mem::replace(
            &mut self.accumulator,
            RawChoiceAccumulator::new(ResponsesUsage::new()),
        );
        let final_usage = accumulator.final_usage.clone();

        // Flush buffered tool calls, then the terminal record when a genuine
        // terminal event arrived; EOF without one is truncation and the
        // accumulator withholds the record (deferral, never synthesis).
        out.extend(accumulator.finish().into_iter().map(Ok));

        let span = tracing::Span::current();
        span.record("gen_ai.usage.input_tokens", final_usage.input_tokens);
        span.record("gen_ai.usage.output_tokens", final_usage.output_tokens);
        let cached_tokens = final_usage
            .input_tokens_details
            .as_ref()
            .map(|d| d.cached_tokens)
            .unwrap_or(0);
        span.record("gen_ai.usage.cache_read.input_tokens", cached_tokens);
    }

    fn flush_before_terminal_error(&mut self, out: &mut AdapterOutput<Self::Response>) {
        // Tool calls the provider fully delivered are content: they flush
        // before the terminal error reaches the consumer.
        out.extend(self.accumulator.take_tool_calls().into_iter().map(Ok));
    }

    fn is_finished(&self) -> bool {
        self.finished
    }
}

/// An item message chunk from OpenAI's Responses API.
/// See
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ItemChunk {
    /// Item ID. Optional.
    pub item_id: Option<String>,
    /// The output index of the item from a given streamed response.
    pub output_index: u64,
    /// The item type chunk, as well as the inner data.
    #[serde(flatten)]
    pub data: ItemChunkKind,
}

/// The item chunk type from OpenAI's Responses API.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum ItemChunkKind {
    #[serde(rename = "response.output_item.added")]
    OutputItemAdded(StreamingItemDoneOutput),
    #[serde(rename = "response.output_item.done")]
    OutputItemDone(StreamingItemDoneOutput),
    #[serde(rename = "response.content_part.added")]
    ContentPartAdded(ContentPartChunk),
    #[serde(rename = "response.content_part.done")]
    ContentPartDone(ContentPartChunk),
    #[serde(rename = "response.output_text.delta")]
    OutputTextDelta(DeltaTextChunk),
    #[serde(rename = "response.output_text.done")]
    OutputTextDone(OutputTextChunk),
    #[serde(rename = "response.refusal.delta")]
    RefusalDelta(DeltaTextChunk),
    #[serde(rename = "response.refusal.done")]
    RefusalDone(RefusalTextChunk),
    #[serde(rename = "response.function_call_arguments.delta")]
    FunctionCallArgsDelta(DeltaTextChunkWithItemId),
    #[serde(rename = "response.function_call_arguments.done")]
    FunctionCallArgsDone(ArgsTextChunk),
    #[serde(rename = "response.reasoning_summary_part.added")]
    ReasoningSummaryPartAdded(SummaryPartChunk),
    #[serde(rename = "response.reasoning_summary_part.done")]
    ReasoningSummaryPartDone(SummaryPartChunk),
    #[serde(rename = "response.reasoning_summary_text.delta")]
    ReasoningSummaryTextDelta(SummaryTextChunk),
    #[serde(rename = "response.reasoning_summary_text.done")]
    ReasoningSummaryTextDone(SummaryTextChunk),
    #[serde(rename = "response.reasoning_text.delta")]
    ReasoningTextDelta(DeltaTextChunkWithItemId),
    /// Terminator for a raw-reasoning block, restating the text the
    /// `response.reasoning_text.delta` events already streamed.
    ///
    /// Modeled but not acted on — it falls into `decode_item_chunk`'s no-op
    /// arm exactly like [`Self::ReasoningSummaryTextDone`], because the
    /// accumulated deltas are already the authoritative content and replaying
    /// the restatement would double the reasoning text.
    ///
    /// The variant is what makes naming the tag in
    /// `is_known_responses_event_type` safe: the classify layer sends every
    /// KNOWN tag straight to `decode_known`, so listing the tag without a
    /// variant to decode into would turn today's benign warn-and-skip into an
    /// in-band `Corrupt`/`Err` on every raw-reasoning block (#2258 G4). The
    /// two edits only make sense together.
    #[serde(rename = "response.reasoning_text.done")]
    ReasoningTextDone(OutputTextChunk),
    // No `#[serde(other)]` catch-all: unknown event types are triaged by the
    // classify layer (`classify_responses_frame` checks the `type` tag against
    // `is_known_responses_event_type` BEFORE decoding), so a frame that
    // reaches this decoder with an unmodeled tag is a known-set/enum drift
    // and must fail loudly (`Corrupt`) rather than be silently absorbed.
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StreamingItemDoneOutput {
    pub sequence_number: u64,
    pub item: Output,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ContentPartChunk {
    pub content_index: u64,
    pub sequence_number: u64,
    pub part: ContentPartChunkPart,
}

#[derive(Debug, Serialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPartChunkPart {
    OutputText {
        text: String,
    },
    SummaryText {
        text: String,
    },
    /// Any part type this client doesn't model — `refusal` and
    /// `reasoning_text` parts appear on real refusal/reasoning-text turns,
    /// and new part types ship without notice. Content-part events are
    /// bookkeeping (the content itself arrives via the corresponding delta
    /// events, e.g. `response.refusal.delta`), so an unmodeled part must
    /// parse as a no-op rather than fail the whole chunk — the same shape as
    /// [`Output::Unknown`](super::Output).
    #[serde(untagged)]
    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 part
/// (`rig-2257-code-review-findings-34ee8ba5.md` P2). Here a known part tag
/// must decode fully or error; only an unmodeled (or absent) tag falls back
/// to [`ContentPartChunkPart::Unknown`], preserving the value verbatim.
///
/// Two documented edges of the hand dispatch (#2258 F8):
/// - A part with **duplicate `type` keys** dispatches on the **last**
///   occurrence, because `serde_json::Value` keeps the last duplicate, while
///   a derived internally-tagged enum takes the first. Duplicate keys are
///   not something any Responses gateway emits; the divergence is accepted
///   and pinned by test rather than papered over with a custom map visitor.
/// - A **non-string `type`** is a data-level defect of the tagged shape, not
///   an unmodeled part kind: it errors (classifying the frame `Corrupt`)
///   instead of degrading to an `Unknown` no-op.
impl<'de> Deserialize<'de> for ContentPartChunkPart {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        let text_field = |part: &str| -> Result<String, D::Error> {
            value
                .get("text")
                .and_then(serde_json::Value::as_str)
                .map(ToOwned::to_owned)
                .ok_or_else(|| {
                    serde::de::Error::custom(format!(
                        "`{part}` content part is missing a string `text` field"
                    ))
                })
        };
        match value.get("type").cloned() {
            Some(serde_json::Value::String(tag)) => match tag.as_str() {
                "output_text" => Ok(Self::OutputText {
                    text: text_field("output_text")?,
                }),
                "summary_text" => Ok(Self::SummaryText {
                    text: text_field("summary_text")?,
                }),
                _ => Ok(Self::Unknown(value)),
            },
            Some(_) => Err(serde::de::Error::custom(
                "content part `type` must be a string",
            )),
            None => Ok(Self::Unknown(value)),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DeltaTextChunk {
    pub content_index: u64,
    pub sequence_number: u64,
    pub delta: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DeltaTextChunkWithItemId {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_index: Option<u64>,
    pub sequence_number: u64,
    pub delta: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct OutputTextChunk {
    pub content_index: u64,
    pub sequence_number: u64,
    pub text: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RefusalTextChunk {
    pub content_index: u64,
    pub sequence_number: u64,
    pub refusal: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ArgsTextChunk {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_index: Option<u64>,
    pub sequence_number: u64,
    pub arguments: serde_json::Value,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SummaryPartChunk {
    pub summary_index: u64,
    pub sequence_number: u64,
    pub part: SummaryPartChunkPart,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SummaryTextChunk {
    pub summary_index: u64,
    pub sequence_number: u64,
    // `response.reasoning_summary_text.delta` carries `delta`;
    // the `.done` sibling carries the full `text` under the same shape.
    #[serde(alias = "text")]
    pub delta: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SummaryPartChunkPart {
    SummaryText { text: String },
}

impl<Ext, H> GenericResponsesCompletionModel<Ext, H>
where
    crate::client::Client<Ext, H>: HttpClientExt + Clone + WasmCompatSend + 'static,
    Ext: crate::client::Provider + ResponsesProviderExt + Clone + 'static,
    H: Clone + WasmCompatSend + 'static,
{
    /// Open a stream whose terminal record stays provider-native.
    ///
    /// This is the escape hatch for Responses-API terminal fields rig does not
    /// normalize. It shares the request builder, transport, telemetry, and
    /// error handling with
    /// [`CompletionModel::stream`](completion::CompletionModel::stream), which
    /// calls it and normalizes the terminal record — one network request either
    /// way.
    pub async fn raw_stream(
        &self,
        completion_request: crate::completion::CompletionRequest,
    ) -> Result<streaming::RawStreamingResult<StreamingCompletionResponse>, CompletionError> {
        let system_instructions = completion_request.preamble.clone();
        let record_telemetry_content = completion_request.record_telemetry_content;
        let (request_model, request) = self.create_provider_request(completion_request, true)?;

        crate::providers::internal::trace_json(
            crate::providers::internal::LogTarget::Completions,
            "Responses streaming completion request",
            &request,
        );

        let body = serde_json::to_vec(&request)?;

        let req = self
            .client
            .post(Ext::RESPONSES_PATH)?
            .body(body)
            .map_err(|e| CompletionError::HttpError(e.into()))?;

        let span = CompletionSpanBuilder::new(
            Ext::PROVIDER_NAME,
            &request_model,
            CompletionOperation::ChatStreaming,
        )
        .system_instructions(system_instructions.as_deref(), record_telemetry_content)
        .build();
        let client = self.client.clone();
        let event_source = GenericEventSource::new(client, 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),
        };

        let options = if Ext::EMITS_COMPLETE_TOOL_CALLS_IMMEDIATELY {
            ResponsesStreamOptions::strict_with_immediate_tool_calls()
        } else {
            ResponsesStreamOptions::strict()
        };
        let stream = raw_stream_from_event_source_with_options(event_source, span, options);
        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: crate::completion::CompletionRequest,
    ) -> Result<streaming::StreamingCompletionResponse, CompletionError> {
        let raw = self.raw_stream(completion_request).await?;

        Ok(normalize_responses_stream(Ext::PROVIDER_NAME, raw))
    }
}

#[cfg(test)]
mod tests {
    use super::{
        ContentPartChunkPart, ItemChunk, ItemChunkKind, RawChoiceAccumulator,
        ResponsesStreamOptions, StreamingCompletionChunk, classify_responses_frame,
        raw_choices_from_sse_body, reasoning_end_from_done_item,
    };
    use crate::completion::CompletionModel;
    use crate::message::ReasoningContent;
    use crate::providers::internal::openai_chat_completions_compatible::test_support::{
        sse_bytes_from_data_lines, sse_bytes_from_json_events,
    };
    use crate::providers::internal::wire::WireEvent;
    use crate::providers::openai::responses_api::{
        AdditionalParameters, CompletionResponse, IncompleteDetailsReason, OutputTokensDetails,
        ReasoningSummary, ResponseError, ResponseObject, ResponseStatus, ResponsesUsage,
    };
    use crate::streaming::{RawStreamingChoice, StreamedAssistantContent};
    use crate::test_utils::MockStreamingClient;
    use crate::{client::CompletionClient, providers::openai};
    use futures::StreamExt;
    use serde_json::{self, json};

    #[test]
    fn classify_known_event_decodes() {
        let frame = json!({
            "type": "response.output_text.delta",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 1,
            "delta": "hi",
        })
        .to_string();
        assert!(matches!(
            classify_responses_frame(&frame),
            WireEvent::Known(StreamingCompletionChunk::Delta(_))
        ));
    }

    #[test]
    fn classify_unknown_event_type_is_unknown() {
        let frame = json!({
            "type": "response.web_search_call.searching",
            "output_index": 0,
            "sequence_number": 1,
        })
        .to_string();
        assert!(matches!(
            classify_responses_frame(&frame),
            WireEvent::Unknown { event_type, .. } if event_type == "response.web_search_call.searching"
        ));
    }

    /// #2258 G4: `response.reasoning_text.done` terminates every raw-reasoning
    /// block on all three Responses surfaces. It used to be absent from the
    /// known-event set, so each block logged a spurious "unknown event" warn
    /// and passed through as `Unknown`.
    ///
    /// Both halves of the fix are asserted here, because either alone is a
    /// regression: the tag must be KNOWN (no `Unknown`), and `ItemChunkKind`
    /// must carry a variant for it (no `Corrupt`, which is what naming the tag
    /// without the variant would have produced — strictly worse than the warn).
    ///
    /// No recorded cassette contains this event; the wire shape is the
    /// Responses spec's, so this unit test is the pin.
    #[test]
    fn classify_reasoning_text_done_is_known_and_decodes() {
        let frame = json!({
            "type": "response.reasoning_text.done",
            "item_id": "rs_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 7,
            "text": "the model's raw chain of thought",
        })
        .to_string();

        let event = classify_responses_frame(&frame);
        assert!(
            !matches!(event, WireEvent::Unknown { .. }),
            "the tag must be in the known-event set: {event:?}"
        );
        assert!(
            !matches!(event, WireEvent::Corrupt(_)),
            "a known tag with no matching ItemChunkKind variant decodes to Corrupt, which the \
             driver surfaces as an in-band Err — worse than the warn it replaced: {event:?}"
        );
        assert!(matches!(
            event,
            WireEvent::Known(StreamingCompletionChunk::Delta(chunk))
                if matches!(chunk.data, ItemChunkKind::ReasoningTextDone(_))
        ));
    }

    /// The done event restates text the deltas already streamed, so it must be
    /// a no-op: replaying it would double every raw-reasoning block.
    #[test]
    fn reasoning_text_done_emits_nothing() {
        let mut accumulator = RawChoiceAccumulator::new(ResponsesUsage::new());
        let chunk: ItemChunk = serde_json::from_value(json!({
            "type": "response.reasoning_text.done",
            "item_id": "rs_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 7,
            "text": "the model's raw chain of thought",
        }))
        .expect("reasoning text done event should deserialize");

        let emitted = accumulator.decode_item_chunk(chunk, ResponsesStreamOptions::strict());
        assert!(
            emitted.is_empty(),
            "the done restatement must not re-emit the reasoning text: {emitted:?}"
        );
    }

    #[test]
    fn classify_invalid_json_is_corrupt() {
        assert!(matches!(
            classify_responses_frame("{not json"),
            WireEvent::Corrupt(_)
        ));
    }

    #[test]
    fn classify_known_event_with_defective_payload_is_corrupt() {
        let frame = json!({
            "type": "response.output_text.delta",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 1,
            "delta": 42,
        })
        .to_string();
        assert!(matches!(
            classify_responses_frame(&frame),
            WireEvent::Corrupt(_)
        ));
    }

    // The P2 probe shape from `rig-2257-code-review-findings-34ee8ba5.md`: a
    // known part tag whose payload is schema-defective must classify as
    // `Corrupt`, not slide into the unknown-part catch-all.
    #[test]
    fn classify_defective_known_content_part_is_corrupt() {
        let frame = json!({
            "type": "response.content_part.added",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 1,
            "part": {"type": "output_text", "text": 42},
        })
        .to_string();
        assert!(matches!(
            classify_responses_frame(&frame),
            WireEvent::Corrupt(_)
        ));
    }

    #[test]
    fn content_part_known_tag_decodes() {
        let part: ContentPartChunkPart =
            serde_json::from_value(json!({"type": "output_text", "text": "hi"})).unwrap();
        assert!(matches!(part, ContentPartChunkPart::OutputText { text } if text == "hi"));
    }

    #[test]
    fn content_part_known_tag_with_defective_payload_errors() {
        let result = serde_json::from_value::<ContentPartChunkPart>(
            json!({"type": "output_text", "text": 42}),
        );
        assert!(result.is_err());
        let result = serde_json::from_value::<ContentPartChunkPart>(
            json!({"type": "summary_text", "text": 42}),
        );
        assert!(result.is_err());
    }

    // A non-string `type` is a data-level defect of the tagged shape, never a
    // skippable unknown part (#2258 F8).
    #[test]
    fn content_part_non_string_type_errors() {
        let result =
            serde_json::from_value::<ContentPartChunkPart>(json!({"type": 42, "text": "hi"}));
        assert!(result.is_err());
        let result =
            serde_json::from_value::<ContentPartChunkPart>(json!({"type": null, "text": "hi"}));
        assert!(result.is_err());
    }

    // Pins the documented duplicate-key edge (#2258 F8): `serde_json::Value`
    // keeps the last duplicate key, so the hand dispatch resolves on the LAST
    // `type` — unlike a derived internally-tagged enum, which takes the first.
    #[test]
    fn content_part_duplicate_type_key_dispatches_on_the_last_occurrence() {
        let part: ContentPartChunkPart =
            serde_json::from_str(r#"{"type":"bogus","type":"output_text","text":"hi"}"#).unwrap();
        assert!(matches!(part, ContentPartChunkPart::OutputText { text } if text == "hi"));
    }

    // `refusal` and `reasoning_text` part tags are not in the modeled set:
    // they must stay skippable no-ops (the content arrives via the
    // corresponding delta events), round-tripping the value verbatim.
    #[test]
    fn content_part_unknown_tag_is_preserved_verbatim() {
        let wire = json!({"type": "refusal", "refusal": "no"});
        let part: ContentPartChunkPart = serde_json::from_value(wire.clone()).unwrap();
        let ContentPartChunkPart::Unknown(value) = &part else {
            panic!("unmodeled part tag must fall back to Unknown");
        };
        assert_eq!(value, &wire);
        assert_eq!(serde_json::to_value(&part).unwrap(), wire);
    }

    fn sample_response(status: ResponseStatus) -> CompletionResponse {
        CompletionResponse {
            id: "resp_123".to_string(),
            object: ResponseObject::Response,
            provider_request_id: None,
            created_at: 0,
            status,
            error: None,
            incomplete_details: None,
            instructions: None,
            max_output_tokens: None,
            model: "gpt-5.4".to_string(),
            provider_reasoning: None,
            reasoning_metadata: None,
            reasoning_context: None,
            usage: None,
            output: Vec::new(),
            tools: Vec::new(),
            additional_parameters: AdditionalParameters::default(),
        }
    }

    async fn first_error_from_event(
        event: serde_json::Value,
    ) -> crate::completion::CompletionError {
        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[event]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        stream
            .next()
            .await
            .expect("stream should yield an item")
            .expect_err("stream should surface a provider error")
    }

    /// The provider-native terminal record, as `raw_stream` exposes it.
    async fn final_response_from_event(
        event: serde_json::Value,
    ) -> super::StreamingCompletionResponse {
        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[event]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model
            .raw_stream(request)
            .await
            .expect("stream should start");

        while let Some(item) = stream.next().await {
            if let RawStreamingChoice::FinalResponse(response) =
                item.expect("completed stream should not error")
            {
                return response;
            }
        }

        panic!("stream should yield a final response");
    }

    /// The normalized terminal record, as `stream` exposes it.
    async fn stream_final_from_event(event: serde_json::Value) -> crate::streaming::StreamFinal {
        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[event]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        while let Some(item) = stream.next().await {
            if let StreamedAssistantContent::Final(response) =
                item.expect("completed stream should not error")
            {
                return response;
            }
        }

        panic!("stream should yield a final response");
    }

    #[test]
    fn parse_sse_completion_body_preserves_error_payloads() {
        let mut response = sample_response(ResponseStatus::Failed);
        response.error = Some(ResponseError {
            code: "server_error".to_string(),
            message: "response failed".to_string(),
        });
        let events = [
            json!({
                "type": "response.failed",
                "sequence_number": 1,
                "response": response,
            }),
            json!({
                "type": "error",
                "error": {
                    "message": "boom",
                    "code": "server_error",
                    "type": "server_error"
                }
            }),
        ];

        for event in events {
            let payload = serde_json::to_string(&event).expect("event should serialize");
            let body = format!("data: {payload}\n");
            let err = super::parse_sse_completion_body(&body, "ChatGPT")
                .expect_err("error payload should surface as provider response");

            assert!(matches!(
                err,
                crate::completion::CompletionError::ProviderResponse(_)
            ));
            assert_eq!(err.provider_response_status(), None);
            assert_eq!(err.provider_response_body(), Some(payload.as_str()));
        }
    }

    #[test]
    fn reasoning_done_item_fuses_summary_content_and_encrypted_into_one_end() {
        let summary = vec![
            ReasoningSummary::SummaryText {
                text: "step 1".to_string(),
            },
            ReasoningSummary::SummaryText {
                text: "step 2".to_string(),
            },
        ];
        let content = vec!["private reasoning".to_string()];
        let end = reasoning_end_from_done_item(
            &crate::streaming::StreamPartId::wire("rs_1"),
            crate::streaming::WireId::new("rs_1").as_ref(),
            summary,
            content,
            Some("enc_blob".to_string()),
        );

        // ONE end event carrying every block in wire field order — never a
        // choice per block, which made siblings under one `rs_*` id.
        let Some(RawStreamingChoice::ReasoningEnd {
            id,
            reasoning: Some(reasoning),
            signature: None,
            wire_sent: true,
        }) = end
        else {
            panic!("expected one wire-sent ReasoningEnd restatement");
        };
        assert_eq!(id, crate::streaming::StreamPartId::wire("rs_1"));
        assert_eq!(reasoning.id.as_deref(), Some("rs_1"));
        assert_eq!(
            reasoning.content,
            vec![
                ReasoningContent::Summary("step 1".to_string()),
                ReasoningContent::Summary("step 2".to_string()),
                ReasoningContent::Text {
                    text: "private reasoning".to_string(),
                    signature: None,
                },
                ReasoningContent::Encrypted("enc_blob".to_string()),
            ]
        );
    }

    #[test]
    fn reasoning_output_item_done_emits_reasoning_text_content() {
        let body = format!(
            "data: {}\n",
            json!({
                "type": "response.output_item.done",
                "output_index": 0,
                "sequence_number": 1,
                "item": {
                    "type": "reasoning",
                    "id": "rs_text_1",
                    "summary": [],
                    "content": [{ "type": "reasoning_text", "text": "visible reasoning" }],
                    "status": "completed"
                },
            })
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("sse body should decode");

        // The done item arrives as one wire-sent end restatement whose
        // single block is the reasoning text.
        assert!(matches!(
            choices.first(),
            Some(RawStreamingChoice::ReasoningEnd {
                id,
                reasoning: Some(reasoning),
                wire_sent: true,
                ..
            }) if id == &crate::streaming::StreamPartId::wire("rs_text_1")
                && reasoning.content
                    == vec![ReasoningContent::Text {
                        text: "visible reasoning".to_string(),
                        signature: None,
                    }]
        ));
    }

    /// Envelope-less replay shape (ChatGPT bodies): an id-less summary
    /// delta mints an Output-kind key, the done item restates the whole
    /// block under the SAME adopted minted key, and visible text follows.
    /// The driver's boundary law must treat the same-key whole block as a
    /// close — this exact body used to abort every debug build
    /// (sequence-law O1, Responses variant).
    #[test]
    fn envelope_less_reasoning_then_text_decodes_without_violation() {
        let body = format!(
            "data: {}\ndata: {}\ndata: {}\n",
            json!({
                "type": "response.reasoning_summary_text.delta",
                "output_index": 0,
                "summary_index": 0,
                "sequence_number": 1,
                "delta": "thinking",
            }),
            json!({
                "type": "response.output_item.done",
                "output_index": 0,
                "sequence_number": 2,
                "item": {
                    "type": "reasoning",
                    "id": "",
                    "summary": [{ "type": "summary_text", "text": "thinking, complete" }],
                    "status": "completed"
                },
            }),
            json!({
                "type": "response.output_text.delta",
                "item_id": "msg_1",
                "output_index": 1,
                "content_index": 0,
                "sequence_number": 3,
                "delta": "the answer",
            }),
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("sse body should decode without a sequence-law violation");
        assert!(choices.iter().any(
            |choice| matches!(choice, RawStreamingChoice::Message(text) if text == "the answer")
        ));
    }

    #[test]
    fn reasoning_text_delta_emits_reasoning_delta() {
        let body = format!(
            "data: {}\n",
            json!({
                "type": "response.reasoning_text.delta",
                "item_id": "rs_delta_1",
                "output_index": 0,
                "content_index": 0,
                "sequence_number": 1,
                "delta": "thinking",
            })
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("sse body should decode");

        assert!(matches!(
            choices.first(),
            Some(RawStreamingChoice::ReasoningDelta { id, provider_id: _, reasoning })
                if id == &crate::streaming::StreamPartId::wire("rs_delta_1") && reasoning == "thinking"
        ));
    }

    #[test]
    fn unknown_output_item_surfaces_as_raw_unknown_choice() {
        // A hosted-tool item (web_search_call) arriving on
        // `response.output_item.done` must surface to stream consumers as
        // `RawStreamingChoice::Unknown` carrying the verbatim item, mirroring how
        // the non-streaming decode preserves it on `CompletionResponse.output`.
        let item = json!({
            "type": "web_search_call",
            "id": "ws_001",
            "status": "completed",
            "action": { "type": "search", "queries": ["rig framework"] },
        });
        let body = format!(
            "data: {}\n",
            json!({
                "type": "response.output_item.done",
                "output_index": 0,
                "sequence_number": 1,
                "item": item,
            })
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("sse body should decode");

        let unknown = choices.iter().find_map(|choice| match choice {
            RawStreamingChoice::Unknown(value) => Some(value),
            _ => None,
        });
        assert_eq!(
            unknown,
            Some(&item.clone().into()),
            "the raw web_search_call item should reach the consumer verbatim",
        );
    }

    #[test]
    fn reasoning_done_item_without_encrypted_emits_summary_only() {
        let summary = vec![ReasoningSummary::SummaryText {
            text: "only summary".to_string(),
        }];
        let end = reasoning_end_from_done_item(
            &crate::streaming::StreamPartId::wire("rs_2"),
            crate::streaming::WireId::new("rs_2").as_ref(),
            summary,
            Vec::new(),
            None,
        );

        let Some(RawStreamingChoice::ReasoningEnd {
            id,
            reasoning: Some(reasoning),
            ..
        }) = end
        else {
            panic!("expected one ReasoningEnd restatement");
        };
        assert_eq!(id, crate::streaming::StreamPartId::wire("rs_2"));
        assert_eq!(
            reasoning.content,
            vec![ReasoningContent::Summary("only summary".to_string())]
        );
    }

    #[test]
    fn empty_encrypted_reasoning_is_not_emitted() {
        let content = vec!["visible reasoning".to_string()];

        let end = reasoning_end_from_done_item(
            &crate::streaming::StreamPartId::wire("rs_1"),
            crate::streaming::WireId::new("rs_1").as_ref(),
            Vec::new(),
            content,
            Some(String::new()),
        );

        let Some(RawStreamingChoice::ReasoningEnd {
            reasoning: Some(reasoning),
            ..
        }) = end
        else {
            panic!("expected one ReasoningEnd restatement");
        };
        assert_eq!(
            reasoning.content,
            vec![ReasoningContent::Text {
                text: "visible reasoning".to_string(),
                signature: None,
            }],
            "an empty encrypted payload contributes no block"
        );

        // An entirely empty done item says nothing at the boundary.
        assert!(
            reasoning_end_from_done_item(
                &crate::streaming::StreamPartId::wire("rs_1"),
                crate::streaming::WireId::new("rs_1").as_ref(),
                Vec::new(),
                Vec::new(),
                Some(String::new()),
            )
            .is_none()
        );
    }

    #[test]
    fn content_part_added_deserializes_snake_case_part_type() {
        let chunk: StreamingCompletionChunk = serde_json::from_value(json!({
            "type": "response.content_part.added",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 3,
            "part": {
                "type": "output_text",
                "text": "hello"
            }
        }))
        .expect("content part event should deserialize");

        assert!(matches!(
            chunk,
            StreamingCompletionChunk::Delta(chunk)
                if matches!(
                    chunk.data,
                    ItemChunkKind::ContentPartAdded(_)
                )
        ));
    }

    #[test]
    fn content_part_done_deserializes_snake_case_part_type() {
        let chunk: StreamingCompletionChunk = serde_json::from_value(json!({
            "type": "response.content_part.done",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 4,
            "part": {
                "type": "summary_text",
                "text": "done"
            }
        }))
        .expect("content part done event should deserialize");

        assert!(matches!(
            chunk,
            StreamingCompletionChunk::Delta(chunk)
                if matches!(
                    chunk.data,
                    ItemChunkKind::ContentPartDone(_)
                )
        ));
    }

    #[test]
    fn reasoning_summary_part_added_deserializes_snake_case_part_type() {
        let chunk: StreamingCompletionChunk = serde_json::from_value(json!({
            "type": "response.reasoning_summary_part.added",
            "item_id": "rs_1",
            "output_index": 0,
            "summary_index": 0,
            "sequence_number": 5,
            "part": {
                "type": "summary_text",
                "text": "step 1"
            }
        }))
        .expect("reasoning summary part event should deserialize");

        assert!(matches!(
            chunk,
            StreamingCompletionChunk::Delta(chunk)
                if matches!(
                    chunk.data,
                    ItemChunkKind::ReasoningSummaryPartAdded(_)
                )
        ));
    }

    #[test]
    fn reasoning_summary_part_done_deserializes_snake_case_part_type() {
        let chunk: StreamingCompletionChunk = serde_json::from_value(json!({
            "type": "response.reasoning_summary_part.done",
            "item_id": "rs_1",
            "output_index": 0,
            "summary_index": 0,
            "sequence_number": 6,
            "part": {
                "type": "summary_text",
                "text": "step 2"
            }
        }))
        .expect("reasoning summary part done event should deserialize");

        assert!(matches!(
            chunk,
            StreamingCompletionChunk::Delta(chunk)
                if matches!(
                    chunk.data,
                    ItemChunkKind::ReasoningSummaryPartDone(_)
                )
        ));
    }

    #[tokio::test]
    async fn response_failed_chunk_surfaces_provider_error_without_empty_code_prefix() {
        let mut response = sample_response(ResponseStatus::Failed);
        response.error = Some(ResponseError {
            code: String::new(),
            message: "maximum context length exceeded".to_string(),
        });

        let event = json!({
            "type": "response.failed",
            "sequence_number": 1,
            "response": response,
        });

        let err = first_error_from_event(event).await;

        assert!(matches!(
            err,
            crate::completion::CompletionError::ProviderResponse(_)
        ));
        assert_eq!(err.provider_response_status(), None);
        assert!(err.provider_response_body().is_some_and(|body| {
            body.contains("response.failed") && body.contains("maximum context length exceeded")
        }));
    }

    #[tokio::test]
    async fn response_failed_chunk_surfaces_provider_error_with_code_prefix() {
        let mut response = sample_response(ResponseStatus::Failed);
        response.error = Some(ResponseError {
            code: "context_length_exceeded".to_string(),
            message: "maximum context length exceeded".to_string(),
        });

        let event = json!({
            "type": "response.failed",
            "sequence_number": 1,
            "response": response,
        });

        let err = first_error_from_event(event).await;

        assert!(matches!(
            err,
            crate::completion::CompletionError::ProviderResponse(_)
        ));
        assert_eq!(err.provider_response_status(), None);
        assert!(err.provider_response_body().is_some_and(|body| {
            body.contains("response.failed")
                && body.contains("context_length_exceeded")
                && body.contains("maximum context length exceeded")
        }));
    }

    #[tokio::test]
    async fn response_incomplete_chunk_is_a_successful_terminal_with_mapped_finish_reason() {
        let text_delta = json!({
            "type": "response.output_text.delta",
            "content_index": 0,
            "delta": "partial",
            "item_id": "msg_incomplete_1",
            "output_index": 0,
            "sequence_number": 1,
        });

        let mut response = sample_response(ResponseStatus::Incomplete);
        response.incomplete_details = Some(IncompleteDetailsReason {
            reason: "max_output_tokens".to_string(),
        });
        response.usage = Some(ResponsesUsage {
            input_tokens: 10,
            input_tokens_details: None,
            output_tokens: 5,
            output_tokens_details: Some(OutputTokensDetails {
                reasoning_tokens: 0,
            }),
            total_tokens: 15,
        });

        let incomplete = json!({
            "type": "response.incomplete",
            "sequence_number": 2,
            "response": response,
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[text_delta, incomplete]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut text = String::new();
        let mut final_response = None;
        while let Some(item) = stream.next().await {
            match item.expect("incomplete stream should not error") {
                StreamedAssistantContent::Text(delta) => text.push_str(&delta.text),
                StreamedAssistantContent::Final(response) => final_response = Some(response),
                _ => {}
            }
        }

        // The partial output survives, and the terminal record maps the
        // incomplete status to the same finish reason as the unary path.
        assert_eq!(text, "partial");
        let final_response = final_response.expect("stream should yield a final response");
        assert_eq!(
            final_response.finish_reason,
            Some(crate::completion::FinishReason::Length)
        );
        assert_eq!(final_response.usage.input_tokens, 10);
        assert_eq!(final_response.usage.output_tokens, 5);
        assert_eq!(final_response.usage.total_tokens, 15);
    }

    /// A multi-block reasoning done item (summaries + `encrypted_content`)
    /// aggregates as exactly ONE reasoning part carrying every block in wire
    /// order — never sibling parts sharing one `rs_*` id, which would replay
    /// as duplicate reasoning input items carrying the identical id on the
    /// next request.
    #[tokio::test]
    async fn multi_block_reasoning_done_item_yields_one_part() {
        let reasoning_done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 1,
            "item": {
                "type": "reasoning",
                "id": "rs_1",
                "summary": [
                    {"type": "summary_text", "text": "step 1"},
                    {"type": "summary_text", "text": "step 2"}
                ],
                "content": [],
                "encrypted_content": "enc_blob"
            }
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 2,
            "response": sample_response(ResponseStatus::Completed),
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[reasoning_done, completed]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut completed_reasoning = Vec::new();
        while let Some(item) = stream.next().await {
            if let StreamedAssistantContent::Reasoning { reasoning, .. } =
                item.expect("stream items should be ok")
            {
                completed_reasoning.push(reasoning);
            }
        }

        assert_eq!(
            completed_reasoning.len(),
            1,
            "one done item must complete exactly one reasoning part, got {completed_reasoning:?}"
        );
        let reasoning = completed_reasoning.first().expect("one part");
        assert_eq!(reasoning.id.as_deref(), Some("rs_1"));
        assert_eq!(
            reasoning.content,
            vec![
                ReasoningContent::Summary("step 1".to_string()),
                ReasoningContent::Summary("step 2".to_string()),
                ReasoningContent::Encrypted("enc_blob".to_string()),
            ],
            "every block survives, in wire order, inside the one part"
        );

        // The aggregated choice replays as exactly one reasoning input item.
        let choice = stream.choice;
        let reasoning_parts = choice
            .iter()
            .filter(|content| matches!(content, crate::message::AssistantContent::Reasoning(_)))
            .count();
        assert_eq!(
            reasoning_parts, 1,
            "history must carry one reasoning part per rs_* id, got {choice:?}"
        );
    }

    /// A `response.failed` after a fully-delivered tool call: the tool call is
    /// content and flushes first, the terminal error follows, and nothing
    /// (least of all a terminal record) comes after it.
    #[tokio::test]
    async fn response_failed_flushes_delivered_tool_calls_before_the_error() {
        let tool_call_done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 1,
            "item": {
                "type": "function_call",
                "id": "fc_123",
                "arguments": "{}",
                "call_id": "call_123",
                "name": "example_tool",
                "status": "completed"
            }
        });

        let mut response = sample_response(ResponseStatus::Failed);
        response.error = Some(ResponseError {
            code: "server_error".to_string(),
            message: "response stream failed".to_string(),
        });

        let failed = json!({
            "type": "response.failed",
            "sequence_number": 2,
            "response": response,
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[tool_call_done, failed]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let tool_call = match stream
            .next()
            .await
            .expect("stream should yield the flushed tool call")
            .expect("the flushed tool call must precede the terminal error")
        {
            StreamedAssistantContent::ToolCall { tool_call, .. } => tool_call,
            other => panic!("expected the flushed tool call first, got {other:?}"),
        };
        // The correlator drives rig's id; the item id rides on `provider`.
        assert_eq!(tool_call.id, "call_123");
        let provider = tool_call.provider.as_ref().expect("provider ids are kept");
        assert_eq!(provider.call_id, "call_123");
        assert_eq!(provider.item_id.as_deref(), Some("fc_123"));
        assert_eq!(tool_call.function.name, "example_tool");

        let err = stream
            .next()
            .await
            .expect("stream should yield an item")
            .expect_err("stream should surface a provider error");
        assert!(matches!(
            err,
            crate::completion::CompletionError::ProviderResponse(_)
        ));
        assert_eq!(err.provider_response_status(), None);
        assert!(err.provider_response_body().is_some_and(|body| {
            body.contains("response.failed") && body.contains("response stream failed")
        }));
        assert!(
            stream.next().await.is_none(),
            "stream should terminate immediately after the terminal error"
        );
        assert!(stream.response.is_none());
    }

    /// Same ordering for a transport failure: fully-delivered tool call, then
    /// the error, then the end — with no terminal record.
    #[tokio::test]
    async fn transport_error_flushes_delivered_tool_calls_before_the_error() {
        use crate::http_client::sse::GenericEventSource;
        use crate::test_utils::SequencedStreamingHttpClient;

        let tool_call_done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 1,
            "item": {
                "type": "function_call",
                "id": "fc_123",
                "arguments": "{}",
                "call_id": "call_123",
                "name": "example_tool",
                "status": "completed"
            }
        });
        let chunks = vec![
            Ok(sse_bytes_from_data_lines([tool_call_done.to_string()])),
            Err(crate::http_client::Error::InvalidStatusCodeWithMessage(
                http::StatusCode::BAD_GATEWAY,
                r#"{"error":{"message":"upstream unavailable"}}"#.to_string(),
            )),
        ];
        let client = SequencedStreamingHttpClient::new(chunks);
        let req = http::Request::builder()
            .method("POST")
            .uri("http://localhost/v1/responses")
            .body(Vec::new())
            .expect("request should build");
        let event_source = GenericEventSource::new(client, req);
        let mut stream = super::normalize_responses_stream(
            "openai",
            super::raw_stream_from_event_source(event_source, tracing::Span::none()),
        );

        match stream
            .next()
            .await
            .expect("stream should yield the flushed tool call")
            .expect("the flushed tool call must precede the transport error")
        {
            StreamedAssistantContent::ToolCall { tool_call, .. } => {
                assert_eq!(tool_call.id, "call_123");
                let provider = tool_call.provider.as_ref().expect("provider ids are kept");
                assert_eq!(provider.item_id.as_deref(), Some("fc_123"));
            }
            other => panic!("expected the flushed tool call first, got {other:?}"),
        }

        let err = stream
            .next()
            .await
            .expect("stream should yield the transport error")
            .expect_err("the transport failure must reach the consumer");
        assert_eq!(
            err.provider_response_status(),
            Some(http::StatusCode::BAD_GATEWAY)
        );

        assert!(
            stream.next().await.is_none(),
            "nothing may follow the terminal error"
        );
        assert!(stream.response.is_none());
    }

    /// A known terminal event with a data-level defect (malformed `usage`) is
    /// a corrupt frame, not silent truncation: the error surfaces and, since
    /// the terminal itself failed to parse, no terminal record is emitted.
    #[tokio::test]
    async fn known_terminal_with_malformed_usage_surfaces_error_without_terminal() {
        let mut event = json!({
            "type": "response.completed",
            "sequence_number": 1,
            "response": sample_response(ResponseStatus::Completed),
        });
        event["response"]["usage"] = json!("banana");

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[event]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut saw_error = false;
        let mut saw_final = false;
        while let Some(item) = stream.next().await {
            match item {
                Ok(StreamedAssistantContent::Final(_)) => saw_final = true,
                Ok(other) => panic!("unexpected stream item: {other:?}"),
                Err(err) => {
                    assert!(
                        matches!(err, crate::completion::CompletionError::JsonError(_)),
                        "expected a parse error item, got {err:?}"
                    );
                    saw_error = true;
                }
            }
        }

        assert!(saw_error, "the corrupt terminal must surface as an error");
        assert!(
            !saw_final,
            "a terminal that failed to parse must not produce a terminal record"
        );
        assert!(stream.response.is_none());
    }

    /// An invented event type stays skippable for forward compatibility; a
    /// later genuine terminal still completes the stream.
    #[tokio::test]
    async fn unknown_event_type_is_skipped_and_stream_completes() {
        let unknown = json!({
            "type": "response.rocket_launch",
            "payload": { "count": 3 }
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 2,
            "response": sample_response(ResponseStatus::Completed),
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[unknown, completed]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut saw_final = false;
        while let Some(item) = stream.next().await {
            if let StreamedAssistantContent::Final(_) =
                item.expect("unknown event types must not surface as errors")
            {
                saw_final = true;
            }
        }
        assert!(
            saw_final,
            "the genuine terminal must still complete the stream"
        );
    }

    #[tokio::test]
    async fn refusal_content_part_frames_are_no_ops_and_refusal_text_streams() {
        // A refusal turn emits `response.content_part.added/.done` with a
        // `refusal` part — a shape outside the modeled text parts — followed
        // by the refusal text via `response.refusal.delta`. The part frames
        // must parse as no-ops (never error items); the deltas carry the
        // content.
        let part_added = json!({
            "type": "response.content_part.added",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 1,
            "part": { "type": "refusal", "refusal": "" }
        });
        let refusal_delta = json!({
            "type": "response.refusal.delta",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 2,
            "delta": "I can't help with that."
        });
        let part_done = json!({
            "type": "response.content_part.done",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 3,
            "part": { "type": "refusal", "refusal": "I can't help with that." }
        });
        let reasoning_part = json!({
            "type": "response.content_part.added",
            "item_id": "rs_1",
            "output_index": 1,
            "content_index": 0,
            "sequence_number": 4,
            "part": { "type": "reasoning_text", "text": "" }
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 5,
            "response": sample_response(ResponseStatus::Completed),
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[
                    part_added,
                    refusal_delta,
                    part_done,
                    reasoning_part,
                    completed,
                ]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut texts = Vec::new();
        let mut saw_final = false;
        while let Some(item) = stream.next().await {
            match item.expect("content-part frames must not surface as errors") {
                StreamedAssistantContent::Text(text) => texts.push(text.text),
                StreamedAssistantContent::Final(_) => saw_final = true,
                _ => {}
            }
        }

        assert_eq!(texts, ["I can't help with that."]);
        assert!(saw_final, "the terminal must still arrive");
    }

    #[tokio::test]
    async fn truncated_stream_does_not_synthesize_a_terminal_record() {
        use crate::providers::internal::openai_chat_completions_compatible::test_support::sse_bytes_from_json_events;
        use crate::test_utils::MockStreamingClient;

        // Deltas then EOF without `response.completed`: the accumulator's
        // `saw_terminal` gate must withhold the terminal record rather than
        // present the truncated turn as a successful completion.
        let deltas = [
            json!({
                "type": "response.output_text.delta",
                "output_index": 0,
                "content_index": 0,
                "sequence_number": 1,
                "delta": "hel"
            }),
            json!({
                "type": "response.output_text.delta",
                "output_index": 0,
                "content_index": 0,
                "sequence_number": 2,
                "delta": "lo"
            }),
        ];

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&deltas),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

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

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

    #[tokio::test]
    async fn streaming_error_event_preserves_full_payload_in_live_loop() {
        use crate::providers::internal::openai_chat_completions_compatible::test_support::sse_bytes_from_json_events;
        use crate::test_utils::MockStreamingClient;

        let payload = json!({
            "type": "error",
            "error": {
                "message": "boom",
                "code": "server_error",
                "type": "server_error"
            }
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[payload]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let err = stream
            .next()
            .await
            .expect("stream should yield an item")
            .expect_err("stream should surface a provider response error");
        assert_eq!(err.provider_response_status(), None);
        assert!(
            err.provider_response_body().is_some_and(|body| {
                body.contains("\"type\":\"error\"") && body.contains("boom")
            })
        );
        assert!(
            stream.next().await.is_none(),
            "stream should terminate after error event"
        );
    }

    #[tokio::test]
    async fn streaming_http_non_success_preserves_status_and_body() {
        use crate::http_client::sse::GenericEventSource;
        use crate::test_utils::HttpErrorStreamingClient;

        let body = r#"{"error":{"message":"quota exceeded"}}"#;
        let client = HttpErrorStreamingClient::new(http::StatusCode::TOO_MANY_REQUESTS, body);
        let req = http::Request::builder()
            .method("POST")
            .uri("http://localhost/v1/responses")
            .body(Vec::new())
            .expect("request should build");
        let event_source = GenericEventSource::new(client, req);
        let span = tracing::Span::none();
        let mut stream = super::normalize_responses_stream(
            "openai",
            super::raw_stream_from_event_source(event_source, span),
        );

        let err = stream
            .next()
            .await
            .expect("stream should yield transport error")
            .expect_err("HTTP non-success should surface as a stream error");
        assert_eq!(
            err.provider_response_status(),
            Some(http::StatusCode::TOO_MANY_REQUESTS)
        );
        assert_eq!(err.provider_response_body(), Some(body));
        assert_eq!(
            err.provider_response_json().expect("valid JSON body"),
            Some(serde_json::json!({"error": {"message": "quota exceeded"}}))
        );
        assert!(
            stream.next().await.is_none(),
            "stream should terminate after HTTP non-success"
        );
    }

    /// The buffered unary path has no stream to carry error items, so a
    /// corrupt known frame fails the whole decode — even when a valid terminal
    /// follows — instead of returning a silently partial completion.
    #[test]
    fn corrupt_known_frame_fails_the_buffered_body() {
        let corrupt = json!({
            "type": "response.output_text.delta",
            "delta": 42
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 2,
            "response": sample_response(ResponseStatus::Completed),
        });
        let body = format!("data: {corrupt}\ndata: {completed}\n");

        let err = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect_err("a corrupt known frame must fail the buffered decode");
        assert!(
            err.to_string().contains("response.output_text.delta"),
            "the error should name the malformed event, got: {err}"
        );

        // Syntactically invalid JSON fails too.
        let body = format!("data: {{not json\ndata: {completed}\n");
        raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect_err("invalid JSON must fail the buffered decode");

        // Unknown event types stay skippable.
        let unknown = json!({ "type": "response.rocket_launch", "count": 3 });
        let body = format!("data: {unknown}\ndata: {completed}\n");
        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("unknown event types must stay skippable");
        assert!(
            choices
                .iter()
                .any(|choice| matches!(choice, RawStreamingChoice::FinalResponse(_))),
            "the genuine terminal must still be recorded"
        );
    }

    /// Envelope-less frames (ChatGPT's replayed bodies) are repaired and fed
    /// through the same typed interpreter as the live loop, so the buffered
    /// path agrees with the live path's semantics.
    #[test]
    fn envelope_less_frames_repair_onto_the_shared_interpreter() {
        let completed = json!({
            "type": "response.completed",
            "response": sample_response(ResponseStatus::Completed),
        });

        // A ChatGPT-style text delta with no envelope bookkeeping fields.
        let body = format!(
            "data: {}\ndata: {completed}\n",
            json!({ "type": "response.output_text.delta", "delta": "hi" })
        );
        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("an envelope-less delta must repair and decode");
        assert!(
            choices
                .iter()
                .any(|choice| matches!(choice, RawStreamingChoice::Message(text) if text == "hi"))
        );

        // Live-path parity, pinned: a function-call-arguments delta with no
        // `item_id` is keyed by the minted slot identity (the repair injects
        // `output_index: 0`), matching the live loop — it must flow into
        // assembly instead of vanishing.
        let body = format!(
            "data: {}\ndata: {completed}\n",
            json!({ "type": "response.function_call_arguments.delta", "delta": "{}" })
        );
        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("an id-less args delta must repair and decode");
        assert!(choices.iter().any(|choice| matches!(
            choice,
            RawStreamingChoice::ToolCallDelta { id, .. } if id == &crate::streaming::MintKind::Output.for_wire_index(0)
        )));

        // Live-path parity, pinned: an envelope-less bookkeeping event whose
        // data is intact (`.done` events) is a no-op, not an error as the old
        // salvage made it.
        let body = format!(
            "data: {}\ndata: {completed}\n",
            json!({ "type": "response.output_text.done", "text": "hi" })
        );
        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("an envelope-less done event must repair to the live no-op");
        assert!(
            choices
                .iter()
                .any(|choice| matches!(choice, RawStreamingChoice::FinalResponse(_)))
        );

        // An envelope-less reasoning summary delta keys by the repaired
        // `output_index`, matching the live derivation.
        let body = format!(
            "data: {}\ndata: {completed}\n",
            json!({ "type": "response.reasoning_summary_text.delta", "delta": "think" })
        );
        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("an envelope-less summary delta must repair and decode");
        assert!(choices.iter().any(|choice| matches!(
            choice,
            RawStreamingChoice::ReasoningDelta { id, provider_id: _, reasoning }
                if id == &crate::streaming::MintKind::Output.for_wire_index(0) && reasoning == "think"
        )));
    }

    /// The `max_output_tokens`-mid-tool-call shape: `arguments_delta`
    /// frames stream partial JSON and the done item restates the same
    /// truncated bytes (unparseable). Re-emitting the restatement as
    /// another raw delta put the partial JSON in the buffer TWICE — a
    /// delta-reassembling consumer rendered it twice and the bytes were
    /// double-charged against the accumulation bound. Fragments seen →
    /// the buffer already holds the bytes; only a fragment-less done item
    /// (pure replay of a truncated restatement) still routes its raw
    /// string through the buffer at all (#2258 P3).
    #[test]
    fn an_unparseable_restatement_is_not_reemitted_over_streamed_fragments() {
        let delta = json!({
            "type": "response.function_call_arguments.delta",
            "item_id": "fc_1",
            "output_index": 0,
            "sequence_number": 1,
            "delta": "{\"x\":481",
        });
        let done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 2,
            "item": {
                "type": "function_call",
                "id": "fc_1",
                "call_id": "call_1",
                "name": "add",
                "arguments": "{\"x\":481",
                "status": "incomplete"
            },
        });
        let body = format!(
            "data: {delta}
data: {done}
"
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the truncated shape must decode");
        let raw_fragments: Vec<&str> = choices
            .iter()
            .filter_map(|choice| match choice {
                RawStreamingChoice::ToolCallDelta {
                    content: crate::streaming::ToolCallDeltaContent::Delta(fragment),
                    ..
                } => Some(fragment.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(
            raw_fragments,
            vec!["{\"x\":481"],
            "the streamed fragment is buffered once; the restatement adds nothing"
        );
    }

    /// The pure-replay half of the same policy: a truncated restatement
    /// with NO preceding fragments must still reach the buffer (else the
    /// bytes never arrive and the truncation policy has nothing to judge).
    #[test]
    fn a_fragmentless_unparseable_restatement_still_reaches_the_buffer() {
        let done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 1,
            "item": {
                "type": "function_call",
                "id": "fc_1",
                "call_id": "call_1",
                "name": "add",
                "arguments": "{\"x\":481",
                "status": "incomplete"
            },
        });
        let body = format!(
            "data: {done}
"
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the replayed truncated shape must decode");
        let raw_fragments = choices
            .iter()
            .filter(|choice| {
                matches!(
                    choice,
                    RawStreamingChoice::ToolCallDelta {
                        content: crate::streaming::ToolCallDeltaContent::Delta(_),
                        ..
                    }
                )
            })
            .count();
        assert_eq!(raw_fragments, 1, "the raw bytes must reach the buffer once");
    }

    /// A slot mixing id-bearing and id-less reasoning frames (gateways and
    /// ChatGPT's envelope-less replay bodies omit the id on a subset of a
    /// slot's events) must key every frame — and the done item — by ONE
    /// slot identity, the same discipline `tool_slots` applies. Per-event
    /// resolution split the slot into `Wire("rs_1")` and `Minted(Output, 0)`,
    /// and the done item superseded only one of them: the other survived as
    /// an orphaned partial part carrying the same provider id.
    #[tokio::test]
    async fn mixed_id_and_id_less_reasoning_frames_share_one_slot_key() {
        let with_id = json!({
            "type": "response.reasoning_summary_text.delta",
            "item_id": "rs_1",
            "output_index": 0,
            "summary_index": 0,
            "sequence_number": 1,
            "delta": "s1 ",
        });
        let id_less = json!({
            "type": "response.reasoning_summary_text.delta",
            "output_index": 0,
            "summary_index": 0,
            "sequence_number": 2,
            "delta": "s2",
        });
        let done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 3,
            "item": {
                "type": "reasoning",
                "id": "rs_1",
                "summary": [{"type": "summary_text", "text": "s1 s2"}],
                "content": [],
                "status": "completed",
            },
        });
        let completed = json!({
            "type": "response.completed",
            "response": sample_response(ResponseStatus::Completed),
        });
        let body = format!("data: {with_id}\ndata: {id_less}\ndata: {done}\ndata: {completed}\n");

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the mixed slot must decode");
        let mut keys = std::collections::HashSet::new();
        for choice in &raw_choices {
            match choice {
                RawStreamingChoice::ReasoningDelta { id, .. } => {
                    keys.insert(id.clone());
                }
                RawStreamingChoice::ReasoningEnd { id, .. } => {
                    keys.insert(id.clone());
                }
                _ => {}
            }
        }
        assert_eq!(
            keys.len(),
            1,
            "one slot, one assembly key — got {keys:?} across {raw_choices:?}"
        );

        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("the mixed slot should normalize")
                .expect("a reasoning-bearing stream is not empty");
        let reasoning_parts = response
            .choice
            .iter()
            .filter(|content| matches!(content, crate::completion::AssistantContent::Reasoning(_)))
            .count();
        assert_eq!(
            reasoning_parts, 1,
            "the done item supersedes the one delta-built part; nothing orphans"
        );
    }

    /// #2258 F3: an id-less reasoning delta is keyed by the minted
    /// `output-{index}` identity, and the slot's `output_item.done` full block
    /// (which always carries the real `rs_*` id) must adopt that minted
    /// identity — otherwise the restated summary appends beside the
    /// delta-built part and duplicates it. This is the ChatGPT envelope-less
    /// replay shape: the repair injects `output_index: 0` into the delta while
    /// the done item arrives envelope-full.
    #[tokio::test]
    async fn envelope_less_reasoning_deltas_are_superseded_by_their_done_item() {
        let delta = json!({ "type": "response.reasoning_summary_text.delta", "delta": "think" });
        let done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 2,
            "item": {
                "type": "reasoning",
                "id": "rs_1",
                "summary": [{"type": "summary_text", "text": "think"}],
                "content": [],
                "status": "completed",
            },
        });
        let completed = json!({
            "type": "response.completed",
            "response": sample_response(ResponseStatus::Completed),
        });
        let body = format!("data: {delta}\ndata: {done}\ndata: {completed}\n");

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the envelope-less reasoning replay must decode");
        // The done item's restatement shares the minted per-slot identity.
        assert!(raw_choices.iter().any(|choice| matches!(
            choice,
            RawStreamingChoice::ReasoningEnd { id, reasoning: Some(_), .. }
                if id == &crate::streaming::MintKind::Output.for_wire_index(0)
        )));

        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("chatgpt", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a reasoning-bearing replay is not empty");

        let reasoning: Vec<_> = response
            .choice
            .iter()
            .filter_map(|content| match content {
                crate::completion::AssistantContent::Reasoning(reasoning) => Some(reasoning),
                _ => None,
            })
            .collect();
        assert_eq!(
            reasoning.len(),
            1,
            "deltas and their full block must collapse to one reasoning item: {reasoning:?}"
        );
        let occurrences = reasoning
            .iter()
            .flat_map(|item| item.content.iter())
            .filter(|content| match content {
                ReasoningContent::Summary(text) | ReasoningContent::Text { text, .. } => {
                    text.contains("think")
                }
                _ => false,
            })
            .count();
        assert_eq!(
            occurrences, 1,
            "the restated summary must supersede its deltas, not duplicate them"
        );
    }

    /// #2258 P2: text deltas for one message item interleaved with reasoning
    /// must aggregate as ONE text part. Interleaving reasoning closes the open
    /// text block downstream, so the adapter must re-emit `TextStart` with the
    /// same item id when the item's text resumes — the accumulator's keyed
    /// reactivation then reopens the block instead of minting a sibling.
    #[tokio::test]
    async fn same_item_text_resumes_as_one_part_across_interleaved_reasoning() {
        let events = [
            json!({
                "type": "response.output_text.delta",
                "item_id": "msg_1",
                "output_index": 0,
                "content_index": 0,
                "sequence_number": 1,
                "delta": "hello "
            }),
            json!({
                "type": "response.reasoning_summary_text.delta",
                "item_id": "rs_2",
                "output_index": 1,
                "summary_index": 0,
                "sequence_number": 2,
                "delta": "because"
            }),
            json!({
                "type": "response.output_text.delta",
                "item_id": "msg_1",
                "output_index": 0,
                "content_index": 0,
                "sequence_number": 3,
                "delta": "world"
            }),
            json!({
                "type": "response.completed",
                "sequence_number": 4,
                "response": sample_response(ResponseStatus::Completed),
            }),
        ];
        let body = events
            .iter()
            .map(|event| format!("data: {event}\n"))
            .collect::<String>();

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the interleaved stream must decode");
        // The resumed item re-announces its block: two `TextStart { msg_1 }`.
        let starts = raw_choices
            .iter()
            .filter(|choice| {
                matches!(
                    choice,
                    RawStreamingChoice::TextStart { id, .. } if id == &crate::streaming::StreamPartId::wire("msg_1")
                )
            })
            .count();
        assert_eq!(
            starts, 2,
            "returning to the same item must re-emit its TextStart: {raw_choices:?}"
        );

        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a text-bearing replay is not empty");
        let texts: Vec<_> = response
            .choice
            .iter()
            .filter_map(|content| match content {
                crate::completion::AssistantContent::Text(text) => Some(text.text.clone()),
                _ => None,
            })
            .collect();
        assert_eq!(
            texts,
            ["hello world"],
            "same-item text must aggregate as one part around the reasoning"
        );
        assert!(
            response.choice.iter().any(|content| matches!(
                content,
                crate::completion::AssistantContent::Reasoning(_)
            )),
            "the interleaved reasoning must survive"
        );
    }

    /// #2258 P3: two parallel function calls whose events all lack `fc_*` ids
    /// must not share the `""` assembly key — each slot gets a minted
    /// `output-{index}` identity shared by its added/delta/done events, so two
    /// distinct calls assemble.
    /// A slot whose `added` event carries a real `fc_*` id but whose later
    /// args delta arrives id-less must keep ONE assembly key: slot-scoped
    /// identity (the bridge) makes event-scoped key-splitting
    /// unrepresentable, and the finalized call reports the wire id.
    #[tokio::test]
    async fn mixed_id_and_id_less_events_share_one_slot_key() {
        let events = [
            json!({
                "type": "response.output_item.added",
                "output_index": 0,
                "sequence_number": 1,
                "item": {
                    "type": "function_call",
                    "id": "fc_real",
                    "call_id": "call_a",
                    "name": "tool_a",
                    "arguments": "",
                    "status": "in_progress",
                },
            }),
            // Id-less delta for the same slot: must resolve to the slot's
            // established key, not mint a second identity.
            json!({
                "type": "response.function_call_arguments.delta",
                "output_index": 0,
                "sequence_number": 2,
                "delta": "{\"x\":1}"
            }),
            json!({
                "type": "response.output_item.done",
                "output_index": 0,
                "sequence_number": 3,
                "item": {
                    "type": "function_call",
                    "id": "fc_real",
                    "call_id": "call_a",
                    "name": "tool_a",
                    "arguments": "{\"x\":1}",
                    "status": "completed",
                },
            }),
            json!({
                "type": "response.completed",
                "sequence_number": 4,
                "response": sample_response(ResponseStatus::Completed),
            }),
        ];
        let body = events
            .iter()
            .map(|event| format!("data: {event}\n"))
            .collect::<String>();

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the mixed-id stream must decode");

        // Every tool event (name delta, args delta, input end) carries the
        // slot's single key — no fragment dangles under a second identity.
        let mut keys: Vec<crate::streaming::StreamPartId> = raw_choices
            .iter()
            .filter_map(|choice| match choice {
                RawStreamingChoice::ToolCallDelta { id, .. } => Some(id.clone()),
                RawStreamingChoice::ToolInputEnd(end) => Some(end.id.clone()),
                _ => None,
            })
            .collect();
        keys.dedup();
        assert_eq!(
            keys,
            [crate::streaming::StreamPartId::wire("fc_real")],
            "one slot, one assembly key"
        );

        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a tool-bearing replay is not empty");
        let call = response
            .choice
            .iter()
            .find_map(|content| match content {
                crate::completion::AssistantContent::ToolCall(call) => Some(call.clone()),
                _ => None,
            })
            .expect("the call finalizes");
        assert_eq!(call.function.name, "tool_a");
        assert_eq!(call.function.arguments, serde_json::json!({"x": 1}));
    }

    #[tokio::test]
    async fn parallel_id_less_function_calls_assemble_distinctly() {
        let call_item = |name: &str, call_id: &str, arguments: &str| {
            json!({
                "type": "function_call",
                "call_id": call_id,
                "name": name,
                "arguments": arguments,
                "status": "completed",
            })
        };
        let events = [
            json!({
                "type": "response.output_item.added",
                "output_index": 0,
                "sequence_number": 1,
                "item": call_item("tool_a", "call_a", ""),
            }),
            json!({
                "type": "response.output_item.added",
                "output_index": 1,
                "sequence_number": 2,
                "item": call_item("tool_b", "call_b", ""),
            }),
            json!({
                "type": "response.function_call_arguments.delta",
                "output_index": 0,
                "sequence_number": 3,
                "delta": "{\"x\":1}"
            }),
            json!({
                "type": "response.function_call_arguments.delta",
                "output_index": 1,
                "sequence_number": 4,
                "delta": "{\"y\":2}"
            }),
            json!({
                "type": "response.output_item.done",
                "output_index": 0,
                "sequence_number": 5,
                "item": call_item("tool_a", "call_a", "{\"x\":1}"),
            }),
            json!({
                "type": "response.output_item.done",
                "output_index": 1,
                "sequence_number": 6,
                "item": call_item("tool_b", "call_b", "{\"y\":2}"),
            }),
            json!({
                "type": "response.completed",
                "sequence_number": 7,
                "response": sample_response(ResponseStatus::Completed),
            }),
        ];
        let body = events
            .iter()
            .map(|event| format!("data: {event}\n"))
            .collect::<String>();

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the id-less parallel-call stream must decode");
        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a tool-bearing replay is not empty");

        let mut calls: Vec<_> = response
            .choice
            .iter()
            .filter_map(|content| match content {
                crate::completion::AssistantContent::ToolCall(call) => Some((
                    call.function.name.clone(),
                    call.function.arguments.to_string(),
                )),
                _ => None,
            })
            .collect();
        calls.sort();
        assert_eq!(
            calls,
            [
                ("tool_a".to_owned(), json!({"x": 1}).to_string()),
                ("tool_b".to_owned(), json!({"y": 2}).to_string()),
            ],
            "each id-less slot must assemble its own call"
        );
    }

    /// A lost `output_item.done` frame followed by a healthy
    /// `response.completed` must not discard the call as truncation: the
    /// provider proved the turn ended, so the still-open slot closes at the
    /// terminal and finalizes from its streamed fragments — with the full
    /// dual-wire identity the added event announced. The same
    /// terminal-drain the sibling adapters ship (Interactions at
    /// `interaction.completed`, chat-compat at `finish_reason`).
    #[tokio::test]
    async fn a_lost_done_frame_does_not_discard_a_provider_completed_call() {
        let events = [
            json!({
                "type": "response.output_item.added",
                "output_index": 0,
                "sequence_number": 1,
                "item": {
                    "type": "function_call",
                    "id": "fc_1",
                    "call_id": "call_abc",
                    "name": "get_weather",
                    "arguments": "",
                    "status": "in_progress",
                },
            }),
            json!({
                "type": "response.function_call_arguments.delta",
                "output_index": 0,
                "sequence_number": 2,
                "delta": "{\"city\":\"Paris\"}"
            }),
            // The output_item.done frame is lost; the terminal still arrives.
            json!({
                "type": "response.completed",
                "sequence_number": 3,
                "response": sample_response(ResponseStatus::Completed),
            }),
        ];
        let body = events
            .iter()
            .map(|event| format!("data: {event}\n"))
            .collect::<String>();

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the stream must decode");
        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a tool-bearing replay is not empty");

        let calls: Vec<_> = response
            .choice
            .iter()
            .filter_map(|content| match content {
                crate::completion::AssistantContent::ToolCall(call) => Some(call),
                _ => None,
            })
            .collect();
        assert_eq!(calls.len(), 1, "the provider-completed call must survive");
        let call = calls[0];
        assert_eq!(call.function.name, "get_weather");
        assert_eq!(call.function.arguments, json!({"city": "Paris"}));
        let provider = call.provider.as_ref().expect("the wire issued ids");
        assert_eq!(provider.call_id, "call_abc");
        assert_eq!(provider.item_id.as_deref(), Some("fc_1"));
    }

    /// #2258 P3: id-less argument fragments must surface as deltas (keyed by
    /// the minted slot identity) rather than vanish; when the stream truncates
    /// before the authoritative `output_item.done` restatement, the settled
    /// truncation policy still applies — partial arguments never fabricate a
    /// call.
    #[tokio::test]
    async fn id_less_args_deltas_surface_and_truncation_fabricates_no_call() {
        let events = [
            json!({
                "type": "response.output_item.added",
                "output_index": 0,
                "sequence_number": 1,
                "item": {
                    "type": "function_call",
                    "call_id": "call_a",
                    "name": "tool_a",
                    "arguments": "",
                    "status": "in_progress",
                },
            }),
            json!({
                "type": "response.function_call_arguments.delta",
                "output_index": 0,
                "sequence_number": 2,
                "delta": "{\"loc\":"
            }),
        ];
        let body = events
            .iter()
            .map(|event| format!("data: {event}\n"))
            .collect::<String>();

        let raw_choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("the truncated id-less stream must decode");
        // The fragment flowed into assembly under the minted identity.
        assert!(
            raw_choices.iter().any(|choice| matches!(
                choice,
                RawStreamingChoice::ToolCallDelta {
                    id,
                    content: crate::streaming::ToolCallDeltaContent::Delta(delta),
                } if id == &crate::streaming::MintKind::Output.for_wire_index(0) && delta == "{\"loc\":"
            )),
            "the id-less args fragment must surface as a delta: {raw_choices:?}"
        );

        // No done restatement arrived: the truncation policy withholds the
        // call rather than fabricating one from partial arguments.
        let raw_response = sample_response(ResponseStatus::Completed);
        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize");
        assert!(
            response.is_none(),
            "partial arguments must not fabricate a call: {response:?}"
        );
    }

    #[test]
    fn refusal_content_part_frames_do_not_fail_the_buffered_body() {
        // The ChatGPT buffered route replays recorded SSE bodies; a refusal
        // turn's `content_part` frames (an unmodeled `refusal` part) must not
        // fail the whole completion — the refusal text arrives via the
        // modeled `response.refusal.delta`.
        let part_added = json!({
            "type": "response.content_part.added",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 1,
            "part": { "type": "refusal", "refusal": "" }
        });
        let refusal_delta = json!({
            "type": "response.refusal.delta",
            "item_id": "msg_1",
            "output_index": 0,
            "content_index": 0,
            "sequence_number": 2,
            "delta": "no"
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 3,
            "response": sample_response(ResponseStatus::Completed),
        });
        let body = format!(
            "data: {part_added}
data: {refusal_delta}
data: {completed}
"
        );

        let choices = raw_choices_from_sse_body(&body, ResponsesUsage::new())
            .expect("refusal content-part frames must not fail the buffered decode");
        assert!(
            choices
                .iter()
                .any(|choice| matches!(choice, RawStreamingChoice::Message(text) if text == "no")),
            "the refusal text must be delivered"
        );
    }

    /// The replayed choice keeps its reasoning/tool calls, but message text
    /// present only in the terminal body's `output` must merge in when no text
    /// deltas were streamed (websocket replays hit exactly this quadrant).
    #[tokio::test]
    async fn terminal_body_message_text_merges_into_reasoning_only_replay() {
        use crate::providers::openai::responses_api::Output;

        let raw_choices = vec![RawStreamingChoice::ReasoningDelta {
            provider_id: crate::streaming::WireId::new("rs_1"),
            id: crate::streaming::StreamPartId::wire("rs_1"),
            reasoning: "thinking".to_string(),
        }];

        let mut raw_response = sample_response(ResponseStatus::Completed);
        raw_response.output = vec![
            serde_json::from_value::<Output>(json!({
                "type": "message",
                "id": "msg_body_1",
                "status": "completed",
                "role": "assistant",
                "content": [{ "type": "output_text", "annotations": [], "text": "full answer" }]
            }))
            .expect("output message should deserialize"),
        ];

        let response =
            super::completion_response_from_raw_choices("openai", raw_choices, &raw_response)
                .await
                .expect("replay should normalize")
                .expect("a reasoning-bearing replay is not empty");

        let text: String = response
            .choice
            .iter()
            .filter_map(|content| match content {
                crate::completion::AssistantContent::Text(text) => Some(text.text.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(text, "full answer");
        assert!(
            response.choice.iter().any(|content| matches!(
                content,
                crate::completion::AssistantContent::Reasoning(_)
            )),
            "the replayed reasoning must be kept"
        );
        assert_eq!(response.message_id.as_deref(), Some("msg_body_1"));
    }

    #[test]
    fn streaming_error_event_preserves_full_payload() {
        let payload = r#"{"type":"error","error":{"message":"boom","code":"server_error","type":"server_error"}}"#;
        let body = format!("data: {payload}\n");

        let err = super::raw_choices_from_sse_body(&body, super::ResponsesUsage::new())
            .expect_err("error event should surface as a provider response error");

        assert_eq!(err.provider_response_status(), None);
        assert_eq!(err.provider_response_body(), Some(payload));
        let json = err
            .provider_response_json()
            .expect("raw body should be valid JSON")
            .expect("parsed JSON should be present");
        assert_eq!(json["error"]["code"], "server_error");
    }

    #[tokio::test]
    async fn streaming_non_http_transport_error_stays_provider_error() {
        use crate::http_client::sse::GenericEventSource;
        use crate::test_utils::SequencedStreamingHttpClient;

        let chunks = vec![Err(crate::http_client::Error::InvalidContentType(
            http::HeaderValue::from_static("application/json"),
        ))];
        let client = SequencedStreamingHttpClient::new(chunks);
        let req = http::Request::builder()
            .method("POST")
            .uri("http://localhost/v1/responses")
            .body(Vec::new())
            .expect("request should build");
        let event_source = GenericEventSource::new(client, req);
        let span = tracing::Span::none();
        let mut stream = super::normalize_responses_stream(
            "openai",
            super::raw_stream_from_event_source(event_source, span),
        );

        let err = stream
            .next()
            .await
            .expect("stream should yield transport error")
            .expect_err("non-HTTP transport failure should surface as provider error");
        assert_eq!(
            err.to_string(),
            "ProviderError: Invalid content type was returned: \"application/json\""
        );
        assert!(matches!(
            err,
            crate::completion::CompletionError::ProviderError(_)
        ));
        // Rig-generated transport diagnostics are not provider response bodies.
        assert_eq!(err.provider_response_body(), None);
        assert_eq!(err.provider_response_status(), None);
    }

    #[tokio::test]
    async fn response_completed_chunk_populates_final_usage() {
        let mut response = sample_response(ResponseStatus::Completed);
        response.usage = Some(ResponsesUsage {
            input_tokens: 10,
            input_tokens_details: None,
            output_tokens: 5,
            output_tokens_details: Some(OutputTokensDetails {
                reasoning_tokens: 0,
            }),
            total_tokens: 15,
        });

        let event = json!({
            "type": "response.completed",
            "sequence_number": 1,
            "response": response,
        });

        let usage = final_response_from_event(event).await.usage;
        assert_eq!(usage.input_tokens, 10);
        assert_eq!(usage.output_tokens, 5);
        assert_eq!(usage.total_tokens, 15);
    }

    #[tokio::test]
    async fn response_completed_chunk_populates_reasoning_metadata_and_context() {
        let response = sample_response(ResponseStatus::Completed);
        let mut event = json!({
            "type": "response.completed",
            "sequence_number": 1,
            "response": response,
        });
        let metadata = json!({
            "context": "all_turns",
            "effort": "ultra",
            "summary": null,
            "future_control": true
        });
        event["response"]["reasoning"] = metadata.clone();

        let response = final_response_from_event(event).await;
        assert_eq!(response.reasoning_context.as_deref(), Some("all_turns"));
        assert_eq!(response.reasoning_metadata.as_ref(), metadata.as_object());
    }

    #[tokio::test]
    async fn terminal_record_normalizes_into_the_stream_final() {
        let mut response = sample_response(ResponseStatus::Completed);
        response.usage = Some(ResponsesUsage {
            input_tokens: 10,
            input_tokens_details: None,
            output_tokens: 5,
            output_tokens_details: None,
            total_tokens: 15,
        });

        let mut event = json!({
            "type": "response.completed",
            "sequence_number": 1,
            "response": response,
        });
        event["response"]["output"] = json!([{
            "type": "message",
            "id": "msg_stream_1",
            "status": "completed",
            "role": "assistant",
            "content": [{ "type": "output_text", "annotations": [], "text": "hi" }]
        }]);

        let final_response = stream_final_from_event(event).await;

        assert_eq!(final_response.provider, "openai");
        assert_eq!(final_response.model.as_deref(), Some("gpt-5.4"));
        // The assistant message ID (`msg_...`), never the response ID
        // (`resp_123`) that the same event carries.
        assert_eq!(final_response.message_id.as_deref(), Some("msg_stream_1"));
        assert_eq!(
            final_response.finish_reason,
            Some(crate::completion::FinishReason::Stop)
        );
        assert_eq!(final_response.usage.input_tokens, 10);
        assert_eq!(final_response.usage.output_tokens, 5);
        assert_eq!(final_response.usage.total_tokens, 15);
    }

    #[tokio::test]
    async fn terminal_record_reports_tool_calls_when_the_stream_called_a_tool() {
        let tool_call_done = json!({
            "type": "response.output_item.done",
            "output_index": 0,
            "sequence_number": 1,
            "item": {
                "type": "function_call",
                "id": "fc_123",
                "arguments": "{}",
                "call_id": "call_123",
                "name": "example_tool",
                "status": "completed"
            }
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 2,
            "response": sample_response(ResponseStatus::Completed),
        });

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: sse_bytes_from_json_events(&[tool_call_done, completed]),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut final_response = None;
        while let Some(item) = stream.next().await {
            if let StreamedAssistantContent::Final(response) =
                item.expect("completed stream should not error")
            {
                final_response = Some(response);
            }
        }

        // `completed` is reconciled up to `ToolCalls` by `normalize_stream`,
        // using the call the stream actually emitted.
        assert_eq!(
            final_response
                .expect("stream should yield a final response")
                .finish_reason,
            Some(crate::completion::FinishReason::ToolCalls)
        );
    }

    #[test]
    fn terminal_record_preserves_an_unknown_incomplete_reason() {
        let response = super::StreamingCompletionResponse {
            status: Some(ResponseStatus::Incomplete),
            incomplete_details: Some(IncompleteDetailsReason {
                reason: "MAX_TOOL_CALLS".to_string(),
            }),
            model: Some("gpt-5.4".to_string()),
            message_id: Some("msg_1".to_string()),
            ..super::StreamingCompletionResponse::new(ResponsesUsage::new())
        };

        let final_response = crate::streaming::StreamFinal::from(("openai", response));

        assert_eq!(
            final_response.finish_reason,
            Some(crate::completion::FinishReason::Other(
                "MAX_TOOL_CALLS".to_string()
            ))
        );
        assert_eq!(final_response.message_id.as_deref(), Some("msg_1"));
        assert_eq!(final_response.model.as_deref(), Some("gpt-5.4"));
    }

    #[tokio::test]
    async fn done_sentinel_is_ignored_without_debug_parse_noise() {
        use std::io::{self, Write};
        use std::sync::{Arc, Mutex};

        #[derive(Clone)]
        struct SharedWriter(Arc<Mutex<Vec<u8>>>);

        impl Write for SharedWriter {
            fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
                self.0
                    .lock()
                    .expect("log buffer mutex should not be poisoned")
                    .extend_from_slice(buf);
                Ok(buf.len())
            }

            fn flush(&mut self) -> io::Result<()> {
                Ok(())
            }
        }

        let mut response = sample_response(ResponseStatus::Completed);
        response.usage = Some(ResponsesUsage {
            input_tokens: 4,
            input_tokens_details: None,
            output_tokens: 2,
            output_tokens_details: Some(OutputTokensDetails {
                reasoning_tokens: 0,
            }),
            total_tokens: 6,
        });

        // Scoped-subscriber tests must not run concurrently; see
        // `test_utils::scoped_tracing_subscriber_guard`.
        let _isolation = crate::test_utils::scoped_tracing_subscriber_guard().await;
        let captured = Arc::new(Mutex::new(Vec::new()));
        let subscriber = tracing_subscriber::fmt()
            .with_max_level(tracing::Level::DEBUG)
            .with_ansi(false)
            .without_time()
            .with_writer({
                let captured = captured.clone();
                move || SharedWriter(captured.clone())
            })
            .finish();
        let _guard = tracing::subscriber::set_default(subscriber);

        let client = openai::Client::builder()
            .http_client(MockStreamingClient {
                sse_bytes: bytes::Bytes::from(format!(
                    "data: {}\n\ndata: [DONE]\n\n",
                    serde_json::to_string(&json!({
                        "type": "response.completed",
                        "sequence_number": 1,
                        "response": response,
                    }))
                    .expect("response event should serialize")
                )),
            })
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut final_usage = None;
        while let Some(item) = stream.next().await {
            if let StreamedAssistantContent::Final(response) =
                item.expect("stream should complete successfully")
            {
                final_usage = Some(response.usage);
            }
        }

        let usage = final_usage.expect("expected final response");
        assert_eq!(usage.input_tokens, 4);
        assert_eq!(usage.output_tokens, 2);
        assert_eq!(usage.total_tokens, 6);

        let logs = String::from_utf8(
            captured
                .lock()
                .expect("log buffer mutex should not be poisoned")
                .clone(),
        )
        .expect("captured logs should be valid UTF-8");
        assert!(
            !logs.contains("Couldn't deserialize SSE data as StreamingCompletionChunk"),
            "expected [DONE] to bypass the parse-failure debug path, logs were: {logs}"
        );
    }

    #[tokio::test]
    async fn malformed_frame_surfaces_error_and_stream_still_completes() {
        let delta = json!({
            "type": "response.output_text.delta",
            "content_index": 0,
            "delta": "hello",
            "item_id": "msg_1",
            "logprobs": [],
            "output_index": 0,
            "sequence_number": 1
        });
        let completed = json!({
            "type": "response.completed",
            "sequence_number": 2,
            "response": sample_response(ResponseStatus::Completed),
        });
        let http_client = MockStreamingClient {
            sse_bytes: sse_bytes_from_data_lines([
                delta.to_string(),
                "{not valid json".to_string(),
                completed.to_string(),
            ]),
        };
        let client = openai::Client::builder()
            .http_client(http_client)
            .api_key("test-key")
            .build()
            .expect("client should build");
        let model = client.completion_model("gpt-5.4");
        let request = model.completion_request("hello").build();
        let mut stream = model.stream(request).await.expect("stream should start");

        let mut text = String::new();
        let mut saw_error = false;
        let mut terminal = None;
        while let Some(item) = stream.next().await {
            match item {
                Ok(StreamedAssistantContent::Text(chunk)) => text.push_str(&chunk.text),
                Ok(StreamedAssistantContent::Final(final_response)) => {
                    terminal = Some(final_response)
                }
                Ok(other) => panic!("unexpected stream item: {other:?}"),
                Err(err) => {
                    assert!(
                        matches!(err, crate::completion::CompletionError::JsonError(_)),
                        "expected a JSON parse error item, got {err:?}"
                    );
                    saw_error = true;
                }
            }
        }

        // The malformed frame is surfaced as an error item, and the content
        // and genuine terminal on either side of it both still arrive.
        assert_eq!(text, "hello");
        assert!(saw_error, "malformed frame should surface an error item");
        assert!(
            terminal.is_some(),
            "stream should still emit its terminal record"
        );
    }
}