venice-e2ee-proxy 0.1.0

OpenAI-compatible proxy for Venice.ai E2EE models
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
//! HTTP server, route wiring, shared headers, and route errors.
//!
//! Routes include Venice-backed model listing, encrypted chat request
//! construction, response transformation, and OpenAI-compatible errors/headers.

use std::{
    io,
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use axum::{
    Json, Router,
    extract::State,
    http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, Uri},
    response::{
        IntoResponse, Response,
        sse::{Event, Sse},
    },
    routing::{get, post},
};
use serde_json::{Value, json};
use thiserror::Error;
use tokio::net::TcpListener;
use tracing::{debug, error, info, warn};

use crate::{
    attestation::{AttestationError, AttestationVerifier},
    config::{NvidiaRequirement, ProxyConfig},
    e2ee::{E2eeCodec, E2eeCodecError},
    keys::ProxyInstanceKey,
    openai::{
        ErrorResponse,
        chat::{
            ChatCompletionRequest, ChatConstructionError, ChatRequestError, NormalizedChatMessage,
        },
    },
    sessions::{AttestedModelState, SessionContext, SessionError, SessionManager, SessionRequest},
    tools::{ToolEmulationContext, ToolOutputClassification, ValidatedToolCall},
    venice::{VeniceClient, VeniceClientError},
};

pub const HEADER_PROXY_E2EE: &str = "X-Venice-Proxy-E2EE";
pub const HEADER_PROXY_ATTESTATION_MODE: &str = "X-Venice-Proxy-Attestation-Mode";
pub const HEADER_PROXY_ATTESTED_MODEL: &str = "X-Venice-Proxy-Attested-Model";
pub const HEADER_PROXY_TEE_PROVIDER: &str = "X-Venice-Proxy-TEE-Provider";
pub const HEADER_PROXY_TDX_VERIFIED: &str = "X-Venice-Proxy-TDX-Verified";
pub const HEADER_PROXY_TDX_DEBUG: &str = "X-Venice-Proxy-TDX-Debug";
pub const HEADER_PROXY_NVIDIA_VERIFIED: &str = "X-Venice-Proxy-NVIDIA-Verified";
pub const HEADER_PROXY_KEY_BINDING: &str = "X-Venice-Proxy-Key-Binding";
pub const HEADER_PROXY_SESSION_ID: &str = "X-Venice-Proxy-Session-Id";
pub const HEADER_PROXY_SESSION_SCOPE: &str = "X-Venice-Proxy-Session-Scope";
pub const HEADER_PROXY_TOOL_MODE: &str = "X-Venice-Proxy-Tool-Mode";
pub const HEADER_PROXY_TOOL_RETRIES: &str = "X-Venice-Proxy-Tool-Retries";
pub const HEADER_PROXY_ERROR_CODE: &str = "X-Venice-Proxy-Error-Code";

/// Shared HTTP application state used by route handlers.
#[derive(Debug, Clone)]
pub struct AppState {
    config: Arc<ProxyConfig>,
    venice_client: VeniceClient,
    proxy_instance_key: Option<ProxyInstanceKey>,
    session_manager: SessionManager,
    attestation_verifier: AttestationVerifier,
}

impl AppState {
    /// Builds application state from configuration and a Venice client created from that config.
    pub fn new(config: ProxyConfig) -> Result<Self, VeniceClientError> {
        let venice_client = VeniceClient::from_config(&config)?;
        Ok(Self::from_parts(config, venice_client))
    }

    /// Builds application state from configuration and an already-created Venice client.
    pub fn from_parts(config: ProxyConfig, venice_client: VeniceClient) -> Self {
        let proxy_instance_key = ProxyInstanceKey::generate_from_config(&config.keys);
        let session_manager = SessionManager::new(config.session.clone());
        let attestation_verifier = AttestationVerifier::from_config(&config, venice_client.clone());

        Self {
            config: Arc::new(config),
            venice_client,
            proxy_instance_key,
            session_manager,
            attestation_verifier,
        }
    }

    /// Returns the proxy configuration shared by handlers.
    pub fn config(&self) -> &ProxyConfig {
        &self.config
    }

    /// Returns the Venice upstream client shared by handlers.
    pub fn venice_client(&self) -> &VeniceClient {
        &self.venice_client
    }

    /// Returns the startup proxy instance key when key generation is enabled.
    pub fn proxy_instance_key(&self) -> Option<&ProxyInstanceKey> {
        self.proxy_instance_key.as_ref()
    }

    /// Returns the in-memory session manager shared by handlers.
    pub fn session_manager(&self) -> &SessionManager {
        &self.session_manager
    }

    /// Returns the attestation verifier shared by handlers.
    pub fn attestation_verifier(&self) -> &AttestationVerifier {
        &self.attestation_verifier
    }
}

/// Builds the HTTP router using the configured Venice API key environment
/// variable.
pub fn router(config: ProxyConfig) -> Result<Router, VeniceClientError> {
    Ok(router_from_state(AppState::new(config)?))
}

/// Builds the HTTP router with an already-constructed Venice client.
///
/// This keeps route tests deterministic without mutating process-wide
/// environment variables.
pub fn router_with_venice_client(config: ProxyConfig, venice_client: VeniceClient) -> Router {
    router_from_state(AppState::from_parts(config, venice_client))
}

/// Builds route wiring from already-initialized application state.
fn router_from_state(state: AppState) -> Router {
    Router::new()
        .route("/v1/models", get(list_models).fallback(method_not_allowed))
        .route(
            "/v1/chat/completions",
            post(create_chat_completion).fallback(method_not_allowed),
        )
        .fallback(not_found)
        .with_state(state)
}

/// Serves an already-built router on an already-bound listener.
pub async fn serve(listener: TcpListener, router: Router) -> io::Result<()> {
    axum::serve(listener, router).await
}

/// Handles `GET /v1/models` by proxying Venice's supported model list.
async fn list_models(State(state): State<AppState>) -> Result<Response, ProxyError> {
    info!(route = "/v1/models", "listing Venice models");
    let models = state.venice_client().list_models().await?;
    let mut response = Json(models).into_response();
    ProxyMetadataHeaders::from_config(state.config()).apply(response.headers_mut());
    info!(route = "/v1/models", "Venice models response proxied");
    Ok(response)
}

/// Handles `POST /v1/chat/completions` by encrypting the request and transforming Venice's response.
async fn create_chat_completion(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(body): Json<Value>,
) -> Result<Response, ProxyError> {
    let request = ChatCompletionRequest::parse(&body)?;
    let proxy_instance_key = state
        .proxy_instance_key()
        .ok_or(ProxyError::ProxyInstanceKeyUnavailable)?;

    let session_resolution = state
        .session_manager()
        .get_or_create(SessionRequest::new(&request.model, &headers).with_body(&body))?;
    let session_created = session_resolution.created;
    let session_replaced_expired = session_resolution.replaced_expired;
    let session_scope = session_resolution.session.scope;
    let session = ensure_attested_session(&state, session_resolution.session).await?;
    let model_public_key = session
        .attested_model_public_key
        .as_deref()
        .ok_or(ProxyError::MissingAttestedModelKey)?;

    let codec =
        E2eeCodec::from_config(&state.config().e2ee).map_err(ChatConstructionError::E2ee)?;
    let tool_context = ToolEmulationContext::from_request(&state.config().tools, &request)?;
    let metadata = ProxyMetadataHeaders::for_verified_chat(state.config(), &session);

    info!(
        route = "/v1/chat/completions",
        model = %request.model,
        stream = request.stream,
        message_count = request.messages.len(),
        tool_count = request.tools.len(),
        tool_mode = tool_context.is_some(),
        session_created,
        session_replaced_expired = ?session_replaced_expired,
        session_scope = %session_scope,
        "chat completion request accepted"
    );

    if let Some(tool_context) = tool_context {
        info!(model = %request.model, "using tool-emulated chat completion");
        return openai_tool_emulated_chat_response(
            &state,
            &request,
            &tool_context,
            codec,
            proxy_instance_key.clone(),
            model_public_key,
            metadata,
        )
        .await;
    }

    let prepared = request.to_venice_e2ee_request(&codec, model_public_key)?;
    info!(
        model = %request.model,
        client_stream = prepared.client_stream,
        "forwarding encrypted chat completion to Venice"
    );

    let upstream = state
        .venice_client()
        .create_chat_completion_stream(
            &prepared.upstream,
            proxy_instance_key.public_key_hex(),
            model_public_key,
        )
        .await?;

    if prepared.client_stream {
        info!(model = %request.model, "streaming chat completion response to client");
        let include_usage_requested = request.stream_options.include_usage.unwrap_or(false);
        let transformer = OpenAiChatStreamTransformer::new(
            codec,
            proxy_instance_key.clone(),
            request.model.clone(),
            include_usage_requested,
        );
        Ok(chat_sse_response(
            upstream,
            transformer,
            request.model,
            include_usage_requested,
            &CHAT_SSE_LOG,
            metadata,
        ))
    } else {
        info!(model = %request.model, "buffering chat completion response for client");
        openai_chat_buffered_response(
            upstream,
            codec,
            proxy_instance_key.clone(),
            request.model,
            metadata,
        )
        .await
    }
}

/// Returns a session with cached attestation state, fetching and storing it when absent.
async fn ensure_attested_session(
    state: &AppState,
    session: SessionContext,
) -> Result<SessionContext, ProxyError> {
    if session.attested_model_public_key.is_some() {
        info!(model = %session.model_id, session_scope = %session.scope, "using cached model attestation");
        return Ok(session);
    }

    info!(model = %session.model_id, session_scope = %session.scope, "fetching model attestation");
    let attestation = state
        .attestation_verifier()
        .verify_model_attestation(&session.model_id)
        .await?;

    info!(
        model = %attestation.model_id,
        tee_provider = attestation.tee_provider.as_deref().unwrap_or("unknown"),
        tdx_verified = attestation.tdx.verified,
        nvidia_verified = attestation.nvidia.verified.as_header_value(),
        "model attestation verified"
    );

    let state_update = AttestedModelState {
        model_public_key: attestation.model_public_key,
        attestation_report: attestation.attestation_report,
        verified_at: attestation.verified_at,
    };

    Ok(state
        .session_manager()
        .set_attested_model_state(&session.session_key, state_update)?)
}

/// Buffers an upstream SSE chat response and returns one OpenAI chat completion JSON response.
async fn openai_chat_buffered_response(
    upstream: reqwest::Response,
    codec: E2eeCodec,
    proxy_instance_key: ProxyInstanceKey,
    fallback_model: String,
    metadata: ProxyMetadataHeaders,
) -> Result<Response, ProxyError> {
    let completion =
        buffer_openai_chat_completion(upstream, codec, proxy_instance_key, fallback_model).await?;
    let mut response = Json(completion).into_response();
    metadata.apply(response.headers_mut());
    Ok(response)
}

/// Runs a chat request through tool-emulation response handling for streaming or buffered clients.
async fn openai_tool_emulated_chat_response(
    state: &AppState,
    request: &ChatCompletionRequest,
    tool_context: &ToolEmulationContext,
    codec: E2eeCodec,
    proxy_instance_key: ProxyInstanceKey,
    model_public_key: &str,
    metadata: ProxyMetadataHeaders,
) -> Result<Response, ProxyError> {
    info!(
        model = %request.model,
        max_retries = tool_context.max_retries(),
        "starting tool-emulated chat completion"
    );
    if request.stream {
        let upstream = tool_emulated_upstream_stream(
            state,
            request,
            tool_context,
            &codec,
            &proxy_instance_key,
            model_public_key,
            None,
        )
        .await?;

        let include_usage_requested = request.stream_options.include_usage.unwrap_or(false);
        let transformer = OpenAiToolEmulatedChatStreamTransformer::new(
            tool_context,
            codec,
            proxy_instance_key,
            request.model.clone(),
            include_usage_requested,
        )
        .map_err(ProxyError::ChatStream)?;
        return Ok(chat_sse_response(
            upstream,
            transformer,
            request.model.clone(),
            include_usage_requested,
            &TOOL_EMULATED_CHAT_SSE_LOG,
            metadata,
        ));
    }

    let mut retries = 0;
    let mut correction: Option<(String, String)> = None;

    loop {
        let upstream = tool_emulated_upstream_stream(
            state,
            request,
            tool_context,
            &codec,
            &proxy_instance_key,
            model_public_key,
            correction.as_ref(),
        )
        .await?;

        let completion = match tokio::time::timeout(
            tool_context.marker_timeout(),
            buffer_openai_chat_completion(
                upstream,
                codec.clone(),
                proxy_instance_key.clone(),
                request.model.clone(),
            ),
        )
        .await
        {
            Ok(completion) => completion?,
            Err(_) => {
                let validation_error = format!(
                    "tool-emulated completion did not finish within {}",
                    humantime::format_duration(tool_context.config().tool_call_marker_timeout)
                );
                if retries >= tool_context.max_retries() {
                    return Err(ProxyError::ToolCallRetryExhausted {
                        max_retries: tool_context.max_retries(),
                        last_validation_error: validation_error,
                    });
                }
                warn!(
                    model = %request.model,
                    retry = retries + 1,
                    max_retries = tool_context.max_retries(),
                    "tool call marker timed out; retrying with correction"
                );
                retries += 1;
                correction = Some((validation_error, String::new()));
                continue;
            }
        };
        let assistant_content = completion
            .get("choices")
            .and_then(Value::as_array)
            .and_then(|choices| choices.first())
            .and_then(|choice| choice.get("message"))
            .and_then(|message| message.get("content"))
            .and_then(Value::as_str)
            .unwrap_or_default();

        let mut metadata = metadata.clone();
        if retries > 0 {
            metadata.tool_retries = Some(retries);
        }

        match tool_context.classify_assistant_output(assistant_content) {
            ToolOutputClassification::NormalText => {
                info!(model = %request.model, retries, "tool emulation produced normal text");
                let mut response = Json(completion).into_response();
                metadata.apply(response.headers_mut());
                return Ok(response);
            }
            ToolOutputClassification::ToolCalls(tool_calls) => {
                info!(
                    model = %request.model,
                    tool_calls = tool_calls.len(),
                    retries,
                    "tool emulation produced tool calls"
                );
                let body = openai_tool_call_completion(completion, tool_calls);
                let mut response = Json(body).into_response();
                metadata.apply(response.headers_mut());
                return Ok(response);
            }
            ToolOutputClassification::InvalidToolCall {
                error,
                invalid_output,
            } => {
                if retries >= tool_context.max_retries() {
                    warn!(
                        model = %request.model,
                        max_retries = tool_context.max_retries(),
                        validation_error = %error,
                        "tool call validation failed and retries were exhausted"
                    );
                    return Err(ProxyError::ToolCallRetryExhausted {
                        max_retries: tool_context.max_retries(),
                        last_validation_error: error.to_string(),
                    });
                }
                warn!(
                    model = %request.model,
                    retry = retries + 1,
                    max_retries = tool_context.max_retries(),
                    validation_error = %error,
                    "tool call validation failed; retrying with correction"
                );
                retries += 1;
                correction = Some((error.to_string(), invalid_output));
            }
        }
    }
}

/// Builds and sends the encrypted tool-emulated upstream request, optionally
/// appending a correction message from `(validation_error, invalid_output)`.
async fn tool_emulated_upstream_stream(
    state: &AppState,
    request: &ChatCompletionRequest,
    tool_context: &ToolEmulationContext,
    codec: &E2eeCodec,
    proxy_instance_key: &ProxyInstanceKey,
    model_public_key: &str,
    correction: Option<&(String, String)>,
) -> Result<reqwest::Response, ProxyError> {
    let messages = tool_emulated_messages(request, tool_context, correction);
    let mut tool_request = request.clone();
    tool_request.messages = messages;

    let prepared = tool_request.to_venice_e2ee_request(codec, model_public_key)?;

    Ok(state
        .venice_client()
        .create_chat_completion_stream(
            &prepared.upstream,
            proxy_instance_key.public_key_hex(),
            model_public_key,
        )
        .await?)
}

/// Builds the message list for a tool-emulated upstream request, including any correction prompt.
fn tool_emulated_messages(
    request: &ChatCompletionRequest,
    tool_context: &ToolEmulationContext,
    correction: Option<&(String, String)>,
) -> Vec<NormalizedChatMessage> {
    let mut messages = request.messages.clone();
    let mut tool_system_content = tool_context.controller_message().content;

    if let Some((validation_error, invalid_output)) = correction {
        tool_system_content.push_str("\n\n");
        tool_system_content.push_str(
            &tool_context
                .correction_message(validation_error, invalid_output)
                .content,
        );
    }

    append_to_system_message(&mut messages, tool_system_content);
    messages
}

/// Appends controller content to an existing system message or inserts a new system message.
fn append_to_system_message(messages: &mut Vec<NormalizedChatMessage>, content: String) {
    if let Some(system_message) = messages.iter_mut().find(|message| message.role == "system") {
        system_message.content.push_str("\n\n");
        system_message.content.push_str(&content);
    } else {
        messages.insert(0, NormalizedChatMessage::new("system", content));
    }
}

/// Rewrites a buffered text completion into an OpenAI completion containing tool calls.
fn openai_tool_call_completion(completion: Value, tool_calls: Vec<ValidatedToolCall>) -> Value {
    let choice = completion
        .get("choices")
        .and_then(Value::as_array)
        .and_then(|choices| choices.first())
        .cloned()
        .unwrap_or(Value::Null);
    let index = choice.get("index").and_then(Value::as_u64).unwrap_or(0);
    let tool_call_values: Vec<Value> = tool_calls
        .iter()
        .map(ValidatedToolCall::to_openai_value)
        .collect();
    let reasoning_content = choice
        .get("message")
        .and_then(|message| message.get("reasoning_content"))
        .and_then(Value::as_str);
    let mut message = serde_json::Map::new();
    message.insert("role".to_owned(), Value::String("assistant".to_owned()));
    message.insert("content".to_owned(), Value::Null);
    if let Some(reasoning_content) = reasoning_content {
        message.insert(
            "reasoning_content".to_owned(),
            Value::String(reasoning_content.to_owned()),
        );
    }
    message.insert("tool_calls".to_owned(), Value::Array(tool_call_values));

    json!({
        "id": string_field(&completion, "id").unwrap_or("chatcmpl-local"),
        "object": "chat.completion",
        "created": integer_field(&completion, "created").unwrap_or_else(unix_timestamp_now),
        "model": string_field(&completion, "model").unwrap_or("unknown"),
        "choices": [{
            "index": index,
            "message": Value::Object(message),
            "finish_reason": "tool_calls",
        }],
        "usage": completion.get("usage").cloned().unwrap_or(Value::Null),
    })
}

/// Buffers upstream SSE events and returns one decrypted OpenAI completion object.
async fn buffer_openai_chat_completion(
    mut upstream: reqwest::Response,
    codec: E2eeCodec,
    proxy_instance_key: ProxyInstanceKey,
    fallback_model: String,
) -> Result<Value, ChatStreamError> {
    info!(model = %fallback_model, "buffering upstream chat stream");
    let mut parser = SseEventParser::default();
    let mut transformer =
        OpenAiChatCompletionBuffer::new(codec, proxy_instance_key, fallback_model.clone());
    let mut upstream_done = false;
    let mut chunk_count = 0_u64;
    let mut event_count = 0_u64;

    while let Some(chunk) = upstream
        .chunk()
        .await
        .map_err(ChatStreamError::upstream_stream)?
    {
        chunk_count += 1;
        let chunk = std::str::from_utf8(&chunk).map_err(ChatStreamError::invalid_utf8)?;
        let events = parser.push(chunk)?;
        event_count += events.len() as u64;
        debug!(
            model = %fallback_model,
            chunk_count,
            parsed_events = events.len(),
            total_events = event_count,
            "parsed buffered upstream SSE chunk"
        );

        for event in events {
            if transformer.handle_event(event)? {
                upstream_done = true;
                break;
            }
        }

        if upstream_done {
            break;
        }
    }

    if !upstream_done {
        warn!(
            model = %fallback_model,
            chunk_count,
            event_count,
            "buffered upstream stream ended before DONE"
        );
        parser.finish()?;
        return Err(ChatStreamError::malformed_event(
            "upstream stream ended before data: [DONE]",
        ));
    }

    let completion = transformer.into_response();
    info!(
        model = %fallback_model,
        chunk_count,
        event_count,
        "buffered upstream chat stream transformed"
    );
    Ok(completion)
}

/// Per-path log messages for the shared chat SSE pump, preserving the
/// historical "streaming" vs "tool-emulated" wording.
struct ChatSseLogMessages {
    start: &'static str,
    parsed_chunk: &'static str,
    transformed_event: &'static str,
    completed: &'static str,
    ended_early: &'static str,
}

const CHAT_SSE_LOG: ChatSseLogMessages = ChatSseLogMessages {
    start: "starting upstream chat SSE transformation",
    parsed_chunk: "parsed streaming upstream SSE chunk",
    transformed_event: "transformed streaming upstream SSE event",
    completed: "completed upstream chat SSE transformation",
    ended_early: "streaming upstream stream ended before DONE",
};

const TOOL_EMULATED_CHAT_SSE_LOG: ChatSseLogMessages = ChatSseLogMessages {
    start: "starting tool-emulated upstream chat SSE transformation",
    parsed_chunk: "parsed tool-emulated upstream SSE chunk",
    transformed_event: "transformed tool-emulated upstream SSE event",
    completed: "completed tool-emulated upstream chat SSE transformation",
    ended_early: "tool-emulated upstream stream ended before DONE",
};

/// Transforms parsed upstream SSE events into client-facing stream outputs.
trait ChatSseTransformer {
    /// Converts one parsed upstream event into zero or more client-facing stream outputs.
    fn handle_event(&mut self, event: RawSseEvent) -> Result<Vec<StreamOutput>, ChatStreamError>;
}

/// Builds a client-facing SSE response from an upstream response and transformer.
fn chat_sse_response<T>(
    upstream: reqwest::Response,
    transformer: T,
    fallback_model: String,
    include_usage_requested: bool,
    log: &'static ChatSseLogMessages,
    metadata: ProxyMetadataHeaders,
) -> Response
where
    T: ChatSseTransformer + Send + 'static,
{
    let stream = chat_sse_event_stream(
        upstream,
        transformer,
        fallback_model,
        include_usage_requested,
        log,
    );
    let mut response = Sse::new(stream).into_response();
    metadata.apply(response.headers_mut());
    response
}

/// Streams upstream SSE chunks through a transformer into OpenAI-compatible SSE events.
fn chat_sse_event_stream<T>(
    mut upstream: reqwest::Response,
    mut transformer: T,
    fallback_model: String,
    include_usage_requested: bool,
    log: &'static ChatSseLogMessages,
) -> impl futures_core::Stream<Item = Result<Event, axum::BoxError>>
where
    T: ChatSseTransformer + Send + 'static,
{
    async_stream::try_stream! {
        info!(
            model = %fallback_model,
            include_usage_requested,
            "{}", log.start
        );
        let mut parser = SseEventParser::default();
        let mut upstream_done = false;
        let mut chunk_count = 0_u64;
        let mut event_count = 0_u64;
        let mut output_count = 0_u64;

        while let Some(chunk) = upstream
            .chunk()
            .await
            .map_err(ChatStreamError::upstream_stream)
            .map_err(box_chat_stream_error)?
        {
            chunk_count += 1;
            let chunk = std::str::from_utf8(&chunk)
                .map_err(ChatStreamError::invalid_utf8)
                .map_err(box_chat_stream_error)?;
            let events = parser.push(chunk).map_err(box_chat_stream_error)?;
            event_count += events.len() as u64;
            debug!(
                model = %fallback_model,
                chunk_count,
                parsed_events = events.len(),
                total_events = event_count,
                "{}", log.parsed_chunk
            );

            for event in events {
                let outputs = transformer.handle_event(event).map_err(box_chat_stream_error)?;
                output_count += outputs.len() as u64;
                debug!(
                    model = %fallback_model,
                    emitted_outputs = outputs.len(),
                    total_outputs = output_count,
                    "{}", log.transformed_event
                );

                for output in outputs {
                    match output {
                        StreamOutput::Json(value) => yield Event::default().data(value.to_string()),
                        StreamOutput::Done => {
                            upstream_done = true;
                            info!(
                                model = %fallback_model,
                                chunk_count,
                                event_count,
                                output_count,
                                "{}", log.completed
                            );
                            yield Event::default().data("[DONE]");
                            break;
                        }
                    }
                }

                if upstream_done {
                    break;
                }
            }

            if upstream_done {
                break;
            }
        }

        if !upstream_done {
            warn!(
                model = %fallback_model,
                chunk_count,
                event_count,
                output_count,
                "{}", log.ended_early
            );
            parser.finish().map_err(box_chat_stream_error)?;
            Err::<(), axum::BoxError>(box_chat_stream_error(ChatStreamError::malformed_event(
                "upstream stream ended before data: [DONE]",
            )))?;
        }
    }
}

/// Converts a chat-stream error into an Axum boxed stream error after logging it.
fn box_chat_stream_error(error: ChatStreamError) -> axum::BoxError {
    error!(error = %error, "chat stream transformation failed");
    Box::new(error)
}

/// Incremental parser for upstream Server-Sent Event text chunks.
#[derive(Debug, Default)]
struct SseEventParser {
    buffer: String,
}

impl SseEventParser {
    /// Adds one UTF-8 chunk and returns all complete SSE events parsed from the buffer.
    fn push(&mut self, chunk: &str) -> Result<Vec<RawSseEvent>, ChatStreamError> {
        self.buffer.push_str(chunk);
        let mut events = Vec::new();

        while let Some((boundary_start, boundary_len)) = sse_event_boundary(&self.buffer) {
            let raw = self.buffer[..boundary_start].to_owned();
            self.buffer.drain(..boundary_start + boundary_len);
            if let Some(event) = parse_sse_event(&raw)? {
                events.push(event);
            }
        }

        debug!(
            chunk_bytes = chunk.len(),
            buffered_bytes = self.buffer.len(),
            parsed_events = events.len(),
            "SSE parser processed upstream chunk"
        );
        Ok(events)
    }

    /// Validates that no incomplete SSE event remains after the upstream stream ends.
    fn finish(&self) -> Result<(), ChatStreamError> {
        if self.buffer.trim().is_empty() {
            Ok(())
        } else {
            warn!(
                buffered_bytes = self.buffer.len(),
                "upstream SSE stream ended with incomplete event"
            );
            Err(ChatStreamError::malformed_event(
                "upstream stream ended with an incomplete SSE event",
            ))
        }
    }
}

/// Parsed upstream SSE event containing optional event type and joined data lines.
#[derive(Debug, Clone, PartialEq, Eq)]
struct RawSseEvent {
    event: Option<String>,
    data: String,
}

/// Per-path log messages for [`classify_upstream_event`], preserving the
/// historical "buffered" / "streaming" / "tool-emulated" wording. Optional
/// entries are skipped for paths that historically did not emit them.
struct UpstreamEventLogMessages {
    event: &'static str,
    sse_error: &'static str,
    done: &'static str,
    parsing: Option<&'static str>,
    json_error: &'static str,
    missing_choices: &'static str,
    parsed: Option<&'static str>,
    unexpected_choice_count: &'static str,
}

const BUFFERED_UPSTREAM_EVENT_LOG: UpstreamEventLogMessages = UpstreamEventLogMessages {
    event: "buffering upstream SSE event",
    sse_error: "upstream SSE error event while buffering response",
    done: "received upstream DONE while buffering response",
    parsing: Some("parsing buffered upstream chat JSON chunk"),
    json_error: "upstream JSON error chunk while buffering response",
    missing_choices: "buffered upstream chat chunk is missing choices array",
    parsed: Some("parsed buffered upstream chat chunk"),
    unexpected_choice_count: "unexpected buffered upstream choice count",
};

const STREAMING_UPSTREAM_EVENT_LOG: UpstreamEventLogMessages = UpstreamEventLogMessages {
    event: "transforming streaming upstream SSE event",
    sse_error: "upstream SSE error event while streaming response",
    done: "received upstream DONE while streaming response",
    parsing: Some("parsing streaming upstream chat JSON chunk"),
    json_error: "upstream JSON error chunk while streaming response",
    missing_choices: "streaming upstream chat chunk is missing choices array",
    parsed: Some("parsed streaming upstream chat chunk"),
    unexpected_choice_count: "unexpected streaming upstream choice count",
};

const TOOL_EMULATED_UPSTREAM_EVENT_LOG: UpstreamEventLogMessages = UpstreamEventLogMessages {
    event: "transforming tool-emulated streaming upstream SSE event",
    sse_error: "upstream SSE error event while streaming tool-emulated response",
    done: "received upstream DONE while streaming tool-emulated response",
    parsing: None,
    json_error: "upstream JSON error chunk while streaming tool-emulated response",
    missing_choices: "tool-emulated upstream chat chunk is missing choices array",
    parsed: None,
    unexpected_choice_count: "unexpected tool-emulated upstream choice count",
};

/// A validated upstream SSE event, classified for downstream handling.
enum UpstreamEventKind {
    /// The upstream `data: [DONE]` sentinel.
    Done,
    /// A chunk with an empty `choices` array (usage-only chunk).
    Usage(Value),
    /// A chunk with exactly one choice.
    Choice { value: Value, choice: Value },
}

/// Shared prologue for upstream SSE event handling: rejects error events and
/// error payloads, detects `[DONE]`, parses the JSON chunk, and validates the
/// `choices` array.
fn classify_upstream_event(
    event: RawSseEvent,
    log: &UpstreamEventLogMessages,
) -> Result<UpstreamEventKind, ChatStreamError> {
    let event_type = event.event.as_deref().unwrap_or("message");
    let is_done = event.data.trim() == "[DONE]";
    debug!(event_type, is_done, "{}", log.event);

    if event.event.as_deref() == Some("error") {
        warn!("{}", log.sse_error);
        return Err(ChatStreamError::upstream_event(event.data));
    }

    if is_done {
        info!("{}", log.done);
        return Ok(UpstreamEventKind::Done);
    }

    if let Some(parsing) = log.parsing {
        debug!("{}", parsing);
    }
    let value: Value = serde_json::from_str(&event.data).map_err(ChatStreamError::json_event)?;
    if let Some(error) = value.get("error") {
        warn!("{}", log.json_error);
        return Err(ChatStreamError::upstream_event(error.to_string()));
    }

    let Some(choices) = value.get("choices").and_then(Value::as_array) else {
        warn!("{}", log.missing_choices);
        return Err(ChatStreamError::malformed_event(
            "upstream chat chunk is missing choices array",
        ));
    };
    if let Some(parsed) = log.parsed {
        debug!(choice_count = choices.len(), "{}", parsed);
    }

    if choices.is_empty() {
        return Ok(UpstreamEventKind::Usage(value));
    }
    if choices.len() != 1 {
        warn!(
            choice_count = choices.len(),
            "{}", log.unexpected_choice_count
        );
        return Err(ChatStreamError::malformed_event(format!(
            "expected exactly one upstream choice, got {}",
            choices.len(),
        )));
    }

    let choice = choices[0].clone();
    Ok(UpstreamEventKind::Choice { value, choice })
}

/// Shared chunk-transformation state: the E2EE codec/key used to decrypt
/// upstream content plus the fallback identity fields for emitted chunks.
struct ChunkContext {
    codec: E2eeCodec,
    proxy_instance_key: ProxyInstanceKey,
    fallback_id: String,
    fallback_created: i64,
    fallback_model: String,
}

impl ChunkContext {
    /// Creates chunk transformation context with fallback identity fields for emitted chunks.
    fn new(codec: E2eeCodec, proxy_instance_key: ProxyInstanceKey, fallback_model: String) -> Self {
        Self {
            codec,
            proxy_instance_key,
            fallback_id: format!("chatcmpl-local-{}", uuid::Uuid::new_v4()),
            fallback_created: unix_timestamp_now(),
            fallback_model,
        }
    }

    /// Decrypts optional upstream encrypted content using the proxy instance key.
    fn decrypt(&self, content: Option<&str>) -> Result<Option<String>, ChatStreamError> {
        self.codec
            .decrypt_response_content(content, self.proxy_instance_key.private_key())
            .map_err(ChatStreamError::decryption)
    }

    /// Builds an OpenAI chat-completion chunk with one choice and fallback metadata.
    fn chunk_with_choice(
        &self,
        upstream: &Value,
        index: u64,
        delta: Value,
        finish_reason: Value,
    ) -> Value {
        json!({
            "id": string_field(upstream, "id").unwrap_or(&self.fallback_id),
            "object": string_field(upstream, "object").unwrap_or("chat.completion.chunk"),
            "created": integer_field(upstream, "created").unwrap_or(self.fallback_created),
            "model": string_field(upstream, "model").unwrap_or(&self.fallback_model),
            "choices": [{
                "index": index,
                "delta": delta,
                "finish_reason": finish_reason,
            }],
        })
    }

    /// Builds an OpenAI usage-only streaming chunk from upstream metadata and usage fields.
    fn usage_chunk(&self, upstream: &Value, usage: &Value) -> Value {
        json!({
            "id": string_field(upstream, "id").unwrap_or(&self.fallback_id),
            "object": string_field(upstream, "object").unwrap_or("chat.completion.chunk"),
            "created": integer_field(upstream, "created").unwrap_or(self.fallback_created),
            "model": string_field(upstream, "model").unwrap_or(&self.fallback_model),
            "choices": [],
            "usage": usage,
        })
    }
}

/// Accumulates decrypted upstream SSE chunks into a non-streaming OpenAI completion.
struct OpenAiChatCompletionBuffer {
    ctx: ChunkContext,
    id: Option<String>,
    created: Option<i64>,
    model: Option<String>,
    choice_index: Option<u64>,
    saw_encrypted_response_field: bool,
    content: String,
    reasoning_content: String,
    finish_reason: Option<Value>,
    usage: Option<Value>,
}

impl OpenAiChatCompletionBuffer {
    /// Creates an empty buffered completion transformer.
    fn new(codec: E2eeCodec, proxy_instance_key: ProxyInstanceKey, fallback_model: String) -> Self {
        Self {
            ctx: ChunkContext::new(codec, proxy_instance_key, fallback_model),
            id: None,
            created: None,
            model: None,
            choice_index: None,
            saw_encrypted_response_field: false,
            content: String::new(),
            reasoning_content: String::new(),
            finish_reason: None,
            usage: None,
        }
    }

    /// Applies one upstream SSE event and returns whether the stream reached `[DONE]`.
    fn handle_event(&mut self, event: RawSseEvent) -> Result<bool, ChatStreamError> {
        match classify_upstream_event(event, &BUFFERED_UPSTREAM_EVENT_LOG)? {
            UpstreamEventKind::Done => {
                if !self.saw_encrypted_response_field {
                    self.ctx.decrypt(None)?;
                }
                if self.finish_reason.is_none() {
                    self.finish_reason = Some(Value::String("stop".to_owned()));
                }
                Ok(true)
            }
            UpstreamEventKind::Usage(value) => {
                self.record_metadata(&value);
                self.handle_usage_chunk(&value).map(|()| false)
            }
            UpstreamEventKind::Choice { value, choice } => {
                self.record_metadata(&value);
                self.handle_choice_chunk(&choice)?;
                Ok(false)
            }
        }
    }

    /// Stores a usage-only upstream chunk for the final buffered response.
    fn handle_usage_chunk(&mut self, value: &Value) -> Result<(), ChatStreamError> {
        let Some(usage) = value.get("usage") else {
            warn!("buffered upstream chunk has no choices and no usage");
            return Err(ChatStreamError::malformed_event(
                "upstream chunk has no choices and no usage",
            ));
        };

        info!("buffered upstream usage chunk");
        self.usage = Some(usage.clone());
        Ok(())
    }

    /// Decrypts and appends one upstream choice chunk into the buffered completion.
    fn handle_choice_chunk(&mut self, choice: &Value) -> Result<(), ChatStreamError> {
        let choice = choice.as_object().ok_or_else(|| {
            ChatStreamError::malformed_event("upstream choice must be a JSON object")
        })?;
        let index = normalized_choice_index(choice.get("index"))?;
        match self.choice_index {
            Some(existing) if existing != index => {
                return Err(ChatStreamError::malformed_event(
                    "upstream choice index changed while buffering a completion",
                ));
            }
            None => self.choice_index = Some(index),
            Some(_) => {}
        }

        let finish_reason = normalized_finish_reason(choice.get("finish_reason"))?;
        let delta = choice.get("delta").unwrap_or(&Value::Null);
        let content = encrypted_delta_content(delta)?;
        let reasoning_content = encrypted_delta_reasoning_content(delta)?;
        debug!(
            choice_index = index,
            has_encrypted_content = content.is_some(),
            has_encrypted_reasoning_content = reasoning_content.is_some(),
            has_finish_reason = !finish_reason.is_null(),
            "transforming buffered upstream choice chunk"
        );

        if let Some(content) = content {
            let decrypted = self.ctx.decrypt(Some(content))?;
            self.saw_encrypted_response_field = true;
            debug!(
                choice_index = index,
                has_decrypted_content = decrypted.is_some(),
                "decrypted buffered upstream content chunk"
            );
            if let Some(content) = decrypted {
                self.content.push_str(&content);
            }
        }

        if let Some(reasoning_content) = reasoning_content {
            let decrypted = self.ctx.decrypt(Some(reasoning_content))?;
            self.saw_encrypted_response_field = true;
            debug!(
                choice_index = index,
                has_decrypted_reasoning_content = decrypted.is_some(),
                "decrypted buffered upstream reasoning content chunk"
            );
            if let Some(reasoning_content) = decrypted {
                self.reasoning_content.push_str(&reasoning_content);
            }
        }

        if !finish_reason.is_null() {
            self.finish_reason = Some(finish_reason);
        }

        Ok(())
    }

    /// Records the first available upstream id, timestamp, and model metadata.
    fn record_metadata(&mut self, value: &Value) {
        if self.id.is_none()
            && let Some(id) = string_field(value, "id")
        {
            self.id = Some(id.to_owned());
        }
        if self.created.is_none()
            && let Some(created) = integer_field(value, "created")
        {
            self.created = Some(created);
        }
        if self.model.is_none()
            && let Some(model) = string_field(value, "model")
        {
            self.model = Some(model.to_owned());
        }
    }

    /// Consumes the buffer and returns an OpenAI chat-completion response object.
    fn into_response(self) -> Value {
        let mut message = serde_json::Map::new();
        message.insert("role".to_owned(), Value::String("assistant".to_owned()));
        if !self.reasoning_content.is_empty() {
            message.insert(
                "reasoning_content".to_owned(),
                Value::String(self.reasoning_content),
            );
        }
        message.insert("content".to_owned(), Value::String(self.content));

        json!({
            "id": self.id.unwrap_or(self.ctx.fallback_id),
            "object": "chat.completion",
            "created": self.created.unwrap_or(self.ctx.fallback_created),
            "model": self.model.unwrap_or(self.ctx.fallback_model),
            "choices": [{
                "index": self.choice_index.unwrap_or(0),
                "message": Value::Object(message),
                "finish_reason": self.finish_reason.unwrap_or_else(|| Value::String("stop".to_owned())),
            }],
            "usage": self.usage.unwrap_or(Value::Null),
        })
    }
}

/// Finds the earliest complete SSE event delimiter in a parser buffer.
fn sse_event_boundary(buffer: &str) -> Option<(usize, usize)> {
    ["\r\n\r\n", "\n\n", "\r\r"]
        .into_iter()
        .filter_map(|delimiter| buffer.find(delimiter).map(|index| (index, delimiter.len())))
        .min_by_key(|(index, _)| *index)
}

/// Parses one raw SSE event into its event type and data payload.
fn parse_sse_event(raw: &str) -> Result<Option<RawSseEvent>, ChatStreamError> {
    let mut event = None;
    let mut data_lines = Vec::new();
    let mut saw_non_comment_field = false;

    for line in raw.lines() {
        let line = line.strip_suffix('\r').unwrap_or(line);
        if line.is_empty() || line.starts_with(':') {
            continue;
        }

        saw_non_comment_field = true;
        let (field, value) = line.split_once(':').unwrap_or((line, ""));
        let value = value.strip_prefix(' ').unwrap_or(value);
        match field {
            "event" => event = Some(value.to_owned()),
            "data" => data_lines.push(value.to_owned()),
            "id" | "retry" => {}
            other => {
                warn!(field = other, "unsupported upstream SSE field");
                return Err(ChatStreamError::malformed_event(format!(
                    "unsupported upstream SSE field {other:?}",
                )));
            }
        }
    }

    if data_lines.is_empty() {
        return if saw_non_comment_field {
            warn!("upstream SSE event did not contain a data field");
            Err(ChatStreamError::malformed_event(
                "upstream SSE event did not contain a data field",
            ))
        } else {
            debug!("ignored upstream SSE comment or heartbeat event");
            Ok(None)
        };
    }

    debug!(
        event_type = event.as_deref().unwrap_or("message"),
        data_line_count = data_lines.len(),
        "parsed upstream SSE event"
    );

    Ok(Some(RawSseEvent {
        event,
        data: data_lines.join("\n"),
    }))
}

/// Transforms decrypted upstream SSE chunks into normal OpenAI streaming chat chunks.
struct OpenAiChatStreamTransformer {
    ctx: ChunkContext,
    include_usage_requested: bool,
    sent_role: bool,
    sent_final_finish: bool,
}

impl OpenAiChatStreamTransformer {
    /// Creates a streaming transformer for encrypted non-tool chat responses.
    fn new(
        codec: E2eeCodec,
        proxy_instance_key: ProxyInstanceKey,
        fallback_model: String,
        include_usage_requested: bool,
    ) -> Self {
        Self {
            ctx: ChunkContext::new(codec, proxy_instance_key, fallback_model),
            include_usage_requested,
            sent_role: false,
            sent_final_finish: false,
        }
    }

    /// Converts one upstream choice chunk into decrypted client-facing stream chunks.
    fn handle_choice_chunk(
        &mut self,
        value: &Value,
        choice: &Value,
    ) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let choice = choice.as_object().ok_or_else(|| {
            ChatStreamError::malformed_event("upstream choice must be a JSON object")
        })?;
        let finish_reason = normalized_finish_reason(choice.get("finish_reason"))?;
        let delta = choice.get("delta").unwrap_or(&Value::Null);
        let content = encrypted_delta_content(delta)?;
        let reasoning_content = encrypted_delta_reasoning_content(delta)?;
        debug!(
            has_encrypted_content = content.is_some(),
            has_encrypted_reasoning_content = reasoning_content.is_some(),
            has_finish_reason = !finish_reason.is_null(),
            "transforming streaming upstream choice chunk"
        );

        let mut output = Vec::new();

        if content.is_none() && reasoning_content.is_none() {
            if !finish_reason.is_null() {
                output.push(StreamOutput::Json(self.chunk_with_choice(
                    value,
                    choice.get("index"),
                    json!({}),
                    finish_reason,
                )?));
                self.sent_final_finish = true;
            }
            return Ok(output);
        }

        let decrypted_content = match content {
            Some(content) => self.ctx.decrypt(Some(content))?,
            None => None,
        };
        let decrypted_reasoning_content = match reasoning_content {
            Some(reasoning_content) => self.ctx.decrypt(Some(reasoning_content))?,
            None => None,
        };
        debug!(
            has_decrypted_content = decrypted_content.is_some(),
            has_decrypted_reasoning_content = decrypted_reasoning_content.is_some(),
            "decrypted streaming upstream content chunk"
        );

        if decrypted_content.is_some() || decrypted_reasoning_content.is_some() {
            let mut delta = serde_json::Map::new();

            if !self.sent_role {
                delta.insert("role".to_owned(), Value::String("assistant".to_owned()));
                self.sent_role = true;
            }

            if let Some(reasoning_content) = decrypted_reasoning_content {
                delta.insert(
                    "reasoning_content".to_owned(),
                    Value::String(reasoning_content),
                );
            }

            if let Some(content) = decrypted_content {
                delta.insert("content".to_owned(), Value::String(content));
            }

            let final_finish = !finish_reason.is_null();
            let content_finish_reason = if final_finish {
                Value::Null
            } else {
                finish_reason.clone()
            };
            output.push(StreamOutput::Json(self.chunk_with_choice(
                value,
                choice.get("index"),
                Value::Object(delta),
                content_finish_reason,
            )?));
            if final_finish {
                output.push(StreamOutput::Json(self.chunk_with_choice(
                    value,
                    choice.get("index"),
                    json!({}),
                    finish_reason,
                )?));
                self.sent_final_finish = true;
            }
            return Ok(output);
        }

        Ok(output)
    }

    /// Converts an upstream usage-only chunk into a client-facing usage event when requested.
    fn handle_usage_chunk(&self, value: &Value) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let Some(usage) = value.get("usage") else {
            warn!("streaming upstream chunk has no choices and no usage");
            return Err(ChatStreamError::malformed_event(
                "upstream chunk has no choices and no usage",
            ));
        };

        // If a client requests include_usage but Venice omits a usage event,
        // this streaming path omits usage rather than synthesizing unverifiable
        // token counts.
        if !self.include_usage_requested {
            debug!("streaming upstream usage chunk ignored because client did not request usage");
            return Ok(Vec::new());
        }

        info!("streaming upstream usage chunk forwarded");
        Ok(vec![StreamOutput::Json(self.ctx.usage_chunk(value, usage))])
    }

    /// Builds a fallback final stop chunk for streams that reach `[DONE]` without one.
    fn finish_chunk(&self) -> Value {
        self.ctx
            .chunk_with_choice(&Value::Null, 0, json!({}), Value::String("stop".to_owned()))
    }

    /// Builds a stream chunk after validating the optional upstream choice index.
    fn chunk_with_choice(
        &self,
        upstream: &Value,
        index: Option<&Value>,
        delta: Value,
        finish_reason: Value,
    ) -> Result<Value, ChatStreamError> {
        let index = normalized_choice_index(index)?;
        Ok(self
            .ctx
            .chunk_with_choice(upstream, index, delta, finish_reason))
    }
}

impl ChatSseTransformer for OpenAiChatStreamTransformer {
    /// Converts one parsed upstream SSE event into normal streaming chat outputs.
    fn handle_event(&mut self, event: RawSseEvent) -> Result<Vec<StreamOutput>, ChatStreamError> {
        match classify_upstream_event(event, &STREAMING_UPSTREAM_EVENT_LOG)? {
            UpstreamEventKind::Done => {
                let mut output = Vec::new();
                if !self.sent_final_finish {
                    debug!("synthesizing final streaming finish chunk before DONE");
                    output.push(StreamOutput::Json(self.finish_chunk()));
                    self.sent_final_finish = true;
                }
                output.push(StreamOutput::Done);
                Ok(output)
            }
            UpstreamEventKind::Usage(value) => self.handle_usage_chunk(&value),
            UpstreamEventKind::Choice { value, choice } => {
                self.handle_choice_chunk(&value, &choice)
            }
        }
    }
}

const TOOL_CALL_START_MARKER: &str = "<tool_call>";

/// Streaming transformer for tool-emulated responses.
///
/// Plain assistant text is streamed until the first tool-call marker appears.
/// From that marker onward, output is buffered and parsed/validated as a full
/// tool-call block before OpenAI `delta.tool_calls` chunks are emitted.
struct OpenAiToolEmulatedChatStreamTransformer {
    ctx: ChunkContext,
    tool_context: ToolEmulationContext,
    include_usage_requested: bool,
    sent_role: bool,
    sent_final_finish: bool,
    pending_text: String,
    tool_buffer: String,
    buffering_tool_call: bool,
    emitted_tool_calls: bool,
}

impl OpenAiToolEmulatedChatStreamTransformer {
    /// Creates a streaming transformer for encrypted tool-emulated responses.
    fn new(
        tool_context: &ToolEmulationContext,
        codec: E2eeCodec,
        proxy_instance_key: ProxyInstanceKey,
        fallback_model: String,
        include_usage_requested: bool,
    ) -> Result<Self, ChatStreamError> {
        Ok(Self {
            ctx: ChunkContext::new(codec, proxy_instance_key, fallback_model),
            tool_context: tool_context.clone(),
            include_usage_requested,
            sent_role: false,
            sent_final_finish: false,
            pending_text: String::new(),
            tool_buffer: String::new(),
            buffering_tool_call: false,
            emitted_tool_calls: false,
        })
    }

    /// Converts one upstream choice chunk into normal text, reasoning, or tool-call stream chunks.
    fn handle_choice_chunk(
        &mut self,
        value: &Value,
        choice: &Value,
    ) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let choice = choice.as_object().ok_or_else(|| {
            ChatStreamError::malformed_event("upstream choice must be a JSON object")
        })?;
        let index = normalized_choice_index(choice.get("index"))?;
        let finish_reason = normalized_finish_reason(choice.get("finish_reason"))?;
        let delta = choice.get("delta").unwrap_or(&Value::Null);
        let content = encrypted_delta_content(delta)?;
        let reasoning_content = encrypted_delta_reasoning_content(delta)?;

        let mut output = Vec::new();

        if let Some(reasoning_content) = reasoning_content
            && let Some(reasoning_content) = self.ctx.decrypt(Some(reasoning_content))?
            && !self.sent_final_finish
        {
            output.push(self.reasoning_chunk(value, index, reasoning_content));
        }

        if let Some(content) = content
            && let Some(content) = self.ctx.decrypt(Some(content))?
            && !self.sent_final_finish
        {
            output.extend(self.push_decrypted_content(value, index, &content)?);
        }

        if !finish_reason.is_null() && !self.sent_final_finish {
            output.extend(self.finish_buffered_content(value, index, finish_reason)?);
        }

        Ok(output)
    }

    /// Buffers decrypted text until it is safe to stream or parse as tool-call output.
    fn push_decrypted_content(
        &mut self,
        upstream: &Value,
        index: u64,
        content: &str,
    ) -> Result<Vec<StreamOutput>, ChatStreamError> {
        if self.buffering_tool_call {
            self.tool_buffer.push_str(content);
            self.ensure_tool_buffer_within_limit()?;
            return Ok(Vec::new());
        }

        self.pending_text.push_str(content);
        if let Some(marker_index) = self.pending_text.find(TOOL_CALL_START_MARKER) {
            let text = self.pending_text[..marker_index].to_owned();
            self.tool_buffer = self.pending_text[marker_index..].to_owned();
            self.pending_text.clear();
            self.buffering_tool_call = true;
            self.ensure_tool_buffer_within_limit()?;
            return Ok(self.text_chunk_if_not_empty(upstream, index, text));
        }

        let streamable_len = streamable_pending_text_len(&self.pending_text);
        if streamable_len == 0 {
            return Ok(Vec::new());
        }

        let text = self.pending_text[..streamable_len].to_owned();
        self.pending_text.drain(..streamable_len);
        Ok(vec![
            self.text_field_chunk(upstream, index, "content", text),
        ])
    }

    /// Flushes pending text or buffered tool-call output and emits the final finish chunk.
    fn finish_buffered_content(
        &mut self,
        upstream: &Value,
        index: u64,
        finish_reason: Value,
    ) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let mut output = Vec::new();

        if self.buffering_tool_call {
            output.extend(self.buffered_tool_call_chunks(upstream, index)?);
        } else if !self.pending_text.is_empty() {
            let text = std::mem::take(&mut self.pending_text);
            output.push(self.text_field_chunk(upstream, index, "content", text));
        }

        let finish_reason = if self.emitted_tool_calls {
            Value::String("tool_calls".to_owned())
        } else {
            finish_reason
        };
        output.push(StreamOutput::Json(self.ctx.chunk_with_choice(
            upstream,
            index,
            json!({}),
            finish_reason,
        )));
        self.sent_final_finish = true;
        Ok(output)
    }

    /// Parses buffered tool-call text and returns OpenAI streaming tool-call chunks.
    fn buffered_tool_call_chunks(
        &mut self,
        upstream: &Value,
        index: u64,
    ) -> Result<Vec<StreamOutput>, ChatStreamError> {
        self.ensure_tool_buffer_within_limit()?;
        match self
            .tool_context
            .classify_assistant_output(&self.tool_buffer)
        {
            ToolOutputClassification::ToolCalls(tool_calls) => {
                self.emitted_tool_calls = true;
                Ok(tool_calls
                    .iter()
                    .enumerate()
                    .map(|(tool_index, tool_call)| {
                        self.full_tool_call_chunk(upstream, index, tool_index, tool_call)
                    })
                    .collect())
            }
            ToolOutputClassification::NormalText => {
                let text = std::mem::take(&mut self.tool_buffer);
                self.buffering_tool_call = false;
                Ok(self.text_chunk_if_not_empty(upstream, index, text))
            }
            ToolOutputClassification::InvalidToolCall { error, .. } => {
                error!(
                    validation_error = %error,
                    payload_bytes = self.tool_buffer.len(),
                    payload = %self.tool_buffer,
                    "buffered streamed tool-call payload failed validation"
                );
                Err(ChatStreamError::malformed_event(format!(
                    "tool call parsing failed: {error}"
                )))
            }
        }
    }

    /// Validates that buffered tool-call text remains within the configured byte limit.
    fn ensure_tool_buffer_within_limit(&self) -> Result<(), ChatStreamError> {
        if self.tool_buffer.len() > self.tool_context.config().tool_call_max_bytes {
            return Err(ChatStreamError::malformed_event(format!(
                "tool call output exceeded max size of {} bytes",
                self.tool_context.config().tool_call_max_bytes
            )));
        }
        Ok(())
    }

    /// Builds zero or one streaming content chunks from decrypted assistant text.
    fn text_chunk_if_not_empty(
        &mut self,
        upstream: &Value,
        index: u64,
        text: String,
    ) -> Vec<StreamOutput> {
        if text.is_empty() {
            Vec::new()
        } else {
            vec![self.text_field_chunk(upstream, index, "content", text)]
        }
    }

    /// Builds a streaming reasoning-content chunk from decrypted assistant text.
    fn reasoning_chunk(
        &mut self,
        upstream: &Value,
        index: u64,
        reasoning_content: String,
    ) -> StreamOutput {
        self.text_field_chunk(upstream, index, "reasoning_content", reasoning_content)
    }

    /// Builds a streaming delta containing one assistant text field.
    fn text_field_chunk(
        &mut self,
        upstream: &Value,
        index: u64,
        field: &'static str,
        text: String,
    ) -> StreamOutput {
        let mut delta = serde_json::Map::new();
        self.insert_role_if_needed(&mut delta);
        delta.insert(field.to_owned(), Value::String(text));

        StreamOutput::Json(self.ctx.chunk_with_choice(
            upstream,
            index,
            Value::Object(delta),
            Value::Null,
        ))
    }

    /// Inserts the assistant role into the first emitted streaming delta.
    fn insert_role_if_needed(&mut self, delta: &mut serde_json::Map<String, Value>) {
        if !self.sent_role {
            delta.insert("role".to_owned(), Value::String("assistant".to_owned()));
            self.sent_role = true;
        }
    }

    /// Builds a streaming delta containing one complete validated tool call.
    fn full_tool_call_chunk(
        &mut self,
        upstream: &Value,
        index: u64,
        tool_index: usize,
        tool_call: &ValidatedToolCall,
    ) -> StreamOutput {
        let mut delta = serde_json::Map::new();
        self.insert_role_if_needed(&mut delta);

        let mut tool_call_value = tool_call.to_openai_value();

        if let Some(tool_call_object) = tool_call_value.as_object_mut() {
            tool_call_object.insert("index".to_owned(), json!(tool_index));
        }
        delta.insert("tool_calls".to_owned(), Value::Array(vec![tool_call_value]));

        StreamOutput::Json(self.ctx.chunk_with_choice(
            upstream,
            index,
            Value::Object(delta),
            Value::Null,
        ))
    }

    /// Converts a tool-emulated usage-only chunk into a client-facing usage event when requested.
    fn handle_usage_chunk(&self, value: &Value) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let Some(usage) = value.get("usage") else {
            warn!("tool-emulated upstream chunk has no choices and no usage");
            return Err(ChatStreamError::malformed_event(
                "upstream chunk has no choices and no usage",
            ));
        };

        // OpenAI include_usage permits a usage-only event after the finish chunk.
        if !self.include_usage_requested {
            return Ok(Vec::new());
        }

        Ok(vec![StreamOutput::Json(self.ctx.usage_chunk(value, usage))])
    }

    /// Finishes the stream by flushing buffered output and adding the `[DONE]` sentinel.
    fn finish_stream(&mut self) -> Result<Vec<StreamOutput>, ChatStreamError> {
        let upstream = &Value::Null;
        let mut output = Vec::new();

        if !self.sent_final_finish {
            output.extend(self.finish_buffered_content(
                upstream,
                0,
                Value::String("stop".to_owned()),
            )?);
        }

        output.push(StreamOutput::Done);
        Ok(output)
    }
}

/// Returns how many bytes of pending text can be streamed without splitting a possible tool marker.
fn streamable_pending_text_len(pending_text: &str) -> usize {
    let protected_suffix_len = TOOL_CALL_START_MARKER.len().saturating_sub(1);
    if pending_text.len() <= protected_suffix_len {
        return 0;
    }

    let mut split_at = pending_text.len() - protected_suffix_len;
    while !pending_text.is_char_boundary(split_at) {
        split_at -= 1;
    }
    split_at
}

impl ChatSseTransformer for OpenAiToolEmulatedChatStreamTransformer {
    /// Converts one parsed upstream SSE event into tool-emulated streaming outputs.
    fn handle_event(&mut self, event: RawSseEvent) -> Result<Vec<StreamOutput>, ChatStreamError> {
        match classify_upstream_event(event, &TOOL_EMULATED_UPSTREAM_EVENT_LOG)? {
            UpstreamEventKind::Done => self.finish_stream(),
            UpstreamEventKind::Usage(value) => self.handle_usage_chunk(&value),
            UpstreamEventKind::Choice { value, choice } => {
                self.handle_choice_chunk(&value, &choice)
            }
        }
    }
}

/// Client-facing output emitted by chat SSE transformers.
#[derive(Debug, Clone, PartialEq, Eq)]
enum StreamOutput {
    Json(Value),
    Done,
}

/// Reads an optional upstream choice index as a non-negative integer, defaulting to zero.
fn normalized_choice_index(index: Option<&Value>) -> Result<u64, ChatStreamError> {
    match index {
        Some(Value::Number(number)) => number.as_u64().ok_or_else(|| {
            ChatStreamError::malformed_event("upstream choice index must be a non-negative integer")
        }),
        Some(_) => Err(ChatStreamError::malformed_event(
            "upstream choice index must be a non-negative integer",
        )),
        None => Ok(0),
    }
}

/// Reads an upstream finish reason as a string or null value.
fn normalized_finish_reason(value: Option<&Value>) -> Result<Value, ChatStreamError> {
    match value {
        Some(Value::Null) | None => Ok(Value::Null),
        Some(Value::String(reason)) => Ok(Value::String(reason.clone())),
        Some(_) => Err(ChatStreamError::malformed_event(
            "upstream finish_reason must be a string or null",
        )),
    }
}

/// Reads the encrypted `delta.content` field from an upstream chunk.
fn encrypted_delta_content(delta: &Value) -> Result<Option<&str>, ChatStreamError> {
    encrypted_delta_text_field(delta, "content")
}

/// Reads the encrypted `delta.reasoning_content` field from an upstream chunk.
fn encrypted_delta_reasoning_content(delta: &Value) -> Result<Option<&str>, ChatStreamError> {
    encrypted_delta_text_field(delta, "reasoning_content")
}

/// Reads an optional encrypted text field from an upstream delta object.
fn encrypted_delta_text_field<'a>(
    delta: &'a Value,
    field: &'static str,
) -> Result<Option<&'a str>, ChatStreamError> {
    match delta.get(field) {
        Some(Value::Null) => {
            debug!(field, "ignoring null upstream delta text field");
            Ok(None)
        }
        Some(Value::String(content)) if content.is_empty() => {
            debug!(field, "ignoring empty upstream delta text field");
            Ok(None)
        }
        Some(Value::String(content)) => Ok(Some(content.as_str())),
        Some(_) => Err(ChatStreamError::malformed_event(format!(
            "upstream delta.{field} must be a string or null"
        ))),
        None => Ok(None),
    }
}

/// Reads an optional string field from a JSON object.
fn string_field<'a>(value: &'a Value, field: &str) -> Option<&'a str> {
    value.get(field).and_then(Value::as_str)
}

/// Reads an optional signed integer field from a JSON object.
fn integer_field(value: &Value, field: &str) -> Option<i64> {
    value.get(field).and_then(Value::as_i64)
}

/// Returns the current Unix timestamp in seconds, or zero if the system clock is before the epoch.
fn unix_timestamp_now() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs() as i64)
        .unwrap_or(0)
}

/// Builds a method-not-allowed proxy error for an unsupported route method.
async fn method_not_allowed(method: Method, uri: Uri) -> ProxyError {
    ProxyError::MethodNotAllowed { method, uri }
}

/// Builds a not-found proxy error for unmatched routes.
async fn not_found(uri: Uri) -> ProxyError {
    ProxyError::NotFound { uri }
}

/// Errors returned while parsing, decrypting, or transforming upstream chat streams.
#[derive(Debug, Error)]
pub enum ChatStreamError {
    #[error("Venice upstream stream failed: {message}")]
    UpstreamStream { message: String },
    #[error("Venice upstream stream emitted an error event: {message}")]
    UpstreamEvent { message: String },
    #[error("Venice upstream stream event is malformed: {message}")]
    MalformedEvent { message: String },
    #[error("failed to decrypt Venice E2EE response chunk: {source}")]
    Decryption { source: E2eeCodecError },
}

impl ChatStreamError {
    /// Converts an upstream chunk-read failure into a stream error.
    fn upstream_stream(source: reqwest::Error) -> Self {
        Self::UpstreamStream {
            message: source.to_string(),
        }
    }

    /// Creates an error for an upstream SSE or JSON error event.
    fn upstream_event(message: impl Into<String>) -> Self {
        Self::UpstreamEvent {
            message: message.into(),
        }
    }

    /// Creates an error for malformed upstream stream content.
    fn malformed_event(message: impl Into<String>) -> Self {
        Self::MalformedEvent {
            message: message.into(),
        }
    }

    /// Converts invalid UTF-8 upstream bytes into a malformed-event error.
    fn invalid_utf8(source: std::str::Utf8Error) -> Self {
        Self::MalformedEvent {
            message: format!("upstream SSE bytes are not valid UTF-8: {source}"),
        }
    }

    /// Converts invalid upstream JSON SSE data into a malformed-event error.
    fn json_event(source: serde_json::Error) -> Self {
        Self::MalformedEvent {
            message: format!("upstream SSE data is not valid JSON: {source}"),
        }
    }

    /// Converts E2EE decryption failure into a chat-stream error.
    fn decryption(source: E2eeCodecError) -> Self {
        Self::Decryption { source }
    }

    /// Returns the OpenAI-compatible error type exposed for this stream error.
    fn api_error_type(&self) -> &'static str {
        match self {
            Self::UpstreamStream { .. }
            | Self::UpstreamEvent { .. }
            | Self::MalformedEvent { .. } => "proxy_upstream_error",
            Self::Decryption { .. } => "proxy_e2ee_error",
        }
    }

    /// Returns the proxy error code exposed for this stream error.
    fn api_error_code(&self) -> &'static str {
        match self {
            Self::UpstreamStream { .. } => "upstream_stream_error",
            Self::UpstreamEvent { .. } => "upstream_stream_error",
            Self::MalformedEvent { .. } => "upstream_malformed_response",
            Self::Decryption { .. } => "e2ee_response_decryption_failed",
        }
    }
}

/// Errors returned by HTTP handlers and rendered as OpenAI-compatible JSON errors.
#[derive(Debug, Error)]
pub enum ProxyError {
    #[error(transparent)]
    Venice(#[from] VeniceClientError),
    #[error(transparent)]
    Attestation(#[from] AttestationError),
    #[error(transparent)]
    Session(#[from] SessionError),
    #[error(transparent)]
    ChatRequest(#[from] ChatRequestError),
    #[error(transparent)]
    ChatConstruction(#[from] ChatConstructionError),
    #[error(transparent)]
    ChatStream(#[from] ChatStreamError),
    #[error("The model failed to produce a valid tool call after correction attempts.")]
    ToolCallRetryExhausted {
        max_retries: u32,
        last_validation_error: String,
    },
    #[error(
        "proxy instance key is unavailable; keys.generate_proxy_instance_key_on_startup must be enabled for E2EE chat requests"
    )]
    ProxyInstanceKeyUnavailable,
    #[error("session does not contain an attested model public key after attestation verification")]
    MissingAttestedModelKey,
    #[error("method {method} is not supported for {uri}")]
    MethodNotAllowed { method: Method, uri: Uri },
    #[error("route {uri} was not found")]
    NotFound { uri: Uri },
}

impl ProxyError {
    /// Returns the HTTP status code for this proxy error.
    fn status(&self) -> StatusCode {
        match self {
            Self::Venice(_) => StatusCode::BAD_GATEWAY,
            Self::Attestation(error) if error.verifier_unavailable() => {
                StatusCode::SERVICE_UNAVAILABLE
            }
            Self::Attestation(_) => StatusCode::BAD_GATEWAY,
            Self::Session(
                SessionError::MissingSessionIdentifier | SessionError::InvalidHeaderValue { .. },
            ) => StatusCode::BAD_REQUEST,
            Self::Session(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::ChatRequest(_) => StatusCode::BAD_REQUEST,
            Self::ChatConstruction(_)
            | Self::ChatStream(_)
            | Self::ToolCallRetryExhausted { .. } => StatusCode::BAD_GATEWAY,
            Self::ProxyInstanceKeyUnavailable | Self::MissingAttestedModelKey => {
                StatusCode::INTERNAL_SERVER_ERROR
            }
            Self::MethodNotAllowed { .. } => StatusCode::METHOD_NOT_ALLOWED,
            Self::NotFound { .. } => StatusCode::NOT_FOUND,
        }
    }

    /// Returns the OpenAI-compatible error type for this proxy error.
    fn error_type(&self) -> &'static str {
        match self {
            Self::Venice(error) => error.api_error_type(),
            Self::Attestation(error) => error.api_error_type(),
            Self::Session(
                SessionError::MissingSessionIdentifier | SessionError::InvalidHeaderValue { .. },
            ) => "invalid_request_error",
            Self::Session(_) => "proxy_session_error",
            Self::ChatRequest(_) => "invalid_request_error",
            Self::ChatConstruction(_) => "proxy_e2ee_error",
            Self::ChatStream(error) => error.api_error_type(),
            Self::ToolCallRetryExhausted { .. } => "proxy_tool_call_error",
            Self::ProxyInstanceKeyUnavailable => "proxy_configuration_error",
            Self::MissingAttestedModelKey => "proxy_attestation_error",
            Self::MethodNotAllowed { .. } | Self::NotFound { .. } => "invalid_request_error",
        }
    }

    /// Returns the stable proxy error code for this proxy error.
    fn code(&self) -> &'static str {
        match self {
            Self::Venice(error) => error.api_error_code(),
            Self::Attestation(error) => error.api_error_code(),
            Self::Session(SessionError::MissingSessionIdentifier) => "session_identifier_missing",
            Self::Session(SessionError::InvalidHeaderValue { .. }) => "invalid_session_header",
            Self::Session(_) => "session_error",
            Self::ChatRequest(error) => error.api_error_code(),
            Self::ChatConstruction(error) => error.api_error_code(),
            Self::ChatStream(error) => error.api_error_code(),
            Self::ToolCallRetryExhausted { .. } => "invalid_tool_call",
            Self::ProxyInstanceKeyUnavailable => "proxy_instance_key_unavailable",
            Self::MissingAttestedModelKey => "attestation_failed",
            Self::MethodNotAllowed { .. } => "method_not_allowed",
            Self::NotFound { .. } => "not_found",
        }
    }
}

impl IntoResponse for ProxyError {
    /// Converts a proxy error into an OpenAI-compatible JSON HTTP response.
    fn into_response(self) -> Response {
        let status = self.status();
        let error_code = self.code();
        let error_type = self.error_type();

        if status.is_server_error() {
            error!(
                status = status.as_u16(),
                error_code,
                error_type,
                error = %self,
                "proxy request failed"
            );
        } else {
            warn!(
                status = status.as_u16(),
                error_code,
                error_type,
                error = %self,
                "proxy request rejected"
            );
        }

        let mut response = if let Self::ToolCallRetryExhausted {
            max_retries,
            last_validation_error,
        } = &self
        {
            let body = json!({
                "error": {
                    "message": self.to_string(),
                    "type": error_type,
                    "code": error_code,
                    "details": {
                        "max_retries": max_retries,
                        "last_validation_error": last_validation_error,
                    },
                }
            });
            (status, Json(body)).into_response()
        } else {
            let body = ErrorResponse::new(self.to_string(), error_type, error_code);
            (status, Json(body)).into_response()
        };

        apply_error_headers(response.headers_mut(), error_code);
        response
    }
}

/// Safe proxy metadata headers.
///
/// Fields are optional so handlers never claim E2EE, attestation, key-binding,
/// or session verification that has not happened yet.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProxyMetadataHeaders {
    pub e2ee: Option<String>,
    pub attestation_mode: Option<String>,
    pub attested_model: Option<String>,
    pub tee_provider: Option<String>,
    pub tdx_verified: Option<bool>,
    pub tdx_debug: Option<bool>,
    pub nvidia_verified: Option<String>,
    pub key_binding: Option<bool>,
    pub session_id: Option<String>,
    pub session_scope: Option<String>,
    pub tool_mode: Option<String>,
    pub tool_retries: Option<u32>,
}

impl ProxyMetadataHeaders {
    /// Creates safe non-assertive metadata from config before a route has
    /// verification/session state.
    pub fn from_config(config: &ProxyConfig) -> Self {
        Self {
            attestation_mode: Some(config.attestation.mode.as_str().to_owned()),
            tool_mode: Some(config.tools.mode.as_str().to_owned()),
            ..Self::default()
        }
    }

    /// Creates metadata headers for a chat response after session attestation succeeds.
    pub fn for_verified_chat(config: &ProxyConfig, session: &SessionContext) -> Self {
        let evidence = session
            .attestation_report
            .as_ref()
            .and_then(|report| report.get("attestation"))
            .and_then(Value::as_object);
        let tee_provider = evidence
            .and_then(|evidence| evidence.get("tee_provider"))
            .and_then(Value::as_str)
            .unwrap_or("unknown")
            .to_owned();
        let tdx_debug = evidence.and_then(|evidence| {
            evidence
                .get("debug")
                .or_else(|| evidence.get("tdx_debug"))
                .and_then(Value::as_bool)
        });
        let nvidia_payload_present = evidence
            .and_then(|evidence| evidence.get("nvidia_payload"))
            .is_some_and(|value| !value.is_null());
        let nvidia_verified = match (config.attestation.require_nvidia, nvidia_payload_present) {
            (_, false) => "not-present",
            (NvidiaRequirement::Never, true) => "ignored",
            (_, true) => "verified",
        }
        .to_owned();

        Self {
            e2ee: Some("verified".to_owned()),
            attestation_mode: Some(config.attestation.mode.as_str().to_owned()),
            attested_model: Some(session.model_id.clone()),
            tee_provider: Some(tee_provider),
            tdx_verified: config.attestation.require_tdx.then_some(true),
            tdx_debug,
            nvidia_verified: Some(nvidia_verified),
            key_binding: Some(true),
            session_id: Some(session.agent_session_id.clone()),
            session_scope: Some(session.scope.as_str().to_owned()),
            tool_mode: Some(config.tools.mode.as_str().to_owned()),
            tool_retries: None,
        }
    }

    /// Applies all present metadata fields to an HTTP header map.
    pub fn apply(&self, headers: &mut HeaderMap) {
        insert_optional_header(headers, HEADER_PROXY_E2EE, self.e2ee.as_deref());
        insert_optional_header(
            headers,
            HEADER_PROXY_ATTESTATION_MODE,
            self.attestation_mode.as_deref(),
        );
        insert_optional_header(
            headers,
            HEADER_PROXY_ATTESTED_MODEL,
            self.attested_model.as_deref(),
        );
        insert_optional_header(
            headers,
            HEADER_PROXY_TEE_PROVIDER,
            self.tee_provider.as_deref(),
        );
        insert_optional_bool_header(headers, HEADER_PROXY_TDX_VERIFIED, self.tdx_verified);
        insert_optional_bool_header(headers, HEADER_PROXY_TDX_DEBUG, self.tdx_debug);
        insert_optional_header(
            headers,
            HEADER_PROXY_NVIDIA_VERIFIED,
            self.nvidia_verified.as_deref(),
        );
        insert_optional_bool_header(headers, HEADER_PROXY_KEY_BINDING, self.key_binding);
        insert_optional_header(headers, HEADER_PROXY_SESSION_ID, self.session_id.as_deref());
        insert_optional_header(
            headers,
            HEADER_PROXY_SESSION_SCOPE,
            self.session_scope.as_deref(),
        );
        insert_optional_header(headers, HEADER_PROXY_TOOL_MODE, self.tool_mode.as_deref());
        if let Some(tool_retries) = self.tool_retries {
            insert_header(
                headers,
                HEADER_PROXY_TOOL_RETRIES,
                &tool_retries.to_string(),
            );
        }
    }
}

/// Applies proxy error metadata headers to an HTTP error response.
pub fn apply_error_headers(headers: &mut HeaderMap, error_code: &str) {
    insert_header(headers, HEADER_PROXY_ERROR_CODE, error_code);
}

/// Inserts a string header only when a value is present.
fn insert_optional_header(headers: &mut HeaderMap, name: &'static str, value: Option<&str>) {
    if let Some(value) = value {
        insert_header(headers, name, value);
    }
}

/// Inserts a boolean header only when a value is present.
fn insert_optional_bool_header(headers: &mut HeaderMap, name: &'static str, value: Option<bool>) {
    if let Some(value) = value {
        insert_header(headers, name, if value { "true" } else { "false" });
    }
}

/// Inserts a header when both the name and value are valid HTTP header components.
fn insert_header(headers: &mut HeaderMap, name: &'static str, value: &str) {
    let Ok(name) = HeaderName::from_bytes(name.as_bytes()) else {
        return;
    };
    let Ok(value) = HeaderValue::from_str(value) else {
        return;
    };
    headers.insert(name, value);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        collections::{HashMap, VecDeque},
        sync::{Arc, Mutex},
        time::Duration,
    };

    use axum::{
        body::Body,
        extract::Query,
        http::Request,
        routing::{get, post},
    };
    use serde_json::json;

    use crate::config::NvidiaRequirement;
    use tower::ServiceExt;

    fn test_app() -> Router {
        router_with_venice_client(ProxyConfig::default(), test_venice_client())
    }

    fn test_venice_client() -> VeniceClient {
        test_venice_client_for_base_url("http://127.0.0.1:1/api/v1")
    }

    fn test_venice_client_for_base_url(base_url: impl AsRef<str>) -> VeniceClient {
        VeniceClient::new(base_url.as_ref(), "test-api-key", Duration::from_secs(1))
            .expect("test Venice client should build")
    }

    fn chat_config_with_basic_test_attestation() -> ProxyConfig {
        let mut config = ProxyConfig::default();
        config.attestation.require_tdx = false;
        config.attestation.require_nvidia = NvidiaRequirement::Never;
        config
    }

    #[test]
    fn app_state_initializes_key_and_session_managers_from_config() {
        let state = AppState::from_parts(ProxyConfig::default(), test_venice_client());

        let key = state
            .proxy_instance_key()
            .expect("default config should generate startup key");
        assert_eq!(key.public_key_hex().len(), 130);
        assert!(state.session_manager().is_empty());
        assert_eq!(
            state.attestation_verifier().policy(),
            &ProxyConfig::default().attestation
        );

        let mut config = ProxyConfig::default();
        config.keys.generate_proxy_instance_key_on_startup = false;
        let state = AppState::from_parts(config, test_venice_client());
        assert!(state.proxy_instance_key().is_none());
    }

    async fn error_body(response: Response) -> ErrorResponse {
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("response body should buffer");
        serde_json::from_slice(&bytes).expect("response should be OpenAI-style error JSON")
    }

    #[tokio::test]
    async fn chat_route_ignores_upstream_role_only_chunk_before_encrypted_content() {
        let response = streaming_chat_response(
            "chat-route-role-only",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![
                MockStreamFrame::Role,
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let data = sse_data(&body);
        assert_eq!(data.len(), 3);
        let first: Value = serde_json::from_str(data[0]).expect("first chunk should be JSON");
        assert_eq!(first["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(first["choices"][0]["delta"]["content"], "Hello");
        assert_eq!(data[2], "[DONE]");
    }

    #[tokio::test]
    async fn chat_route_streams_decrypted_normal_assistant_text() {
        let response = streaming_chat_response(
            "chat-route-test",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![
                MockStreamFrame::NullContent,
                MockStreamFrame::EmptyContent,
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(HEADER_PROXY_E2EE).unwrap(),
            "verified"
        );
        assert_eq!(
            response.headers().get(HEADER_PROXY_ATTESTED_MODEL).unwrap(),
            "e2ee-test"
        );

        let body = response_body(response).await;
        let data = sse_data(&body);
        assert_eq!(data.len(), 3);

        let first: Value = serde_json::from_str(data[0]).expect("first chunk should be JSON");
        assert_eq!(first["object"], "chat.completion.chunk");
        assert_eq!(first["model"], "e2ee-test");
        assert_eq!(first["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(first["choices"][0]["delta"]["content"], "Hello");
        assert!(first["choices"][0]["finish_reason"].is_null());

        let final_chunk: Value = serde_json::from_str(data[1]).expect("final chunk should be JSON");
        assert_eq!(final_chunk["choices"][0]["delta"], json!({}));
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "stop");
        assert_eq!(data[2], "[DONE]");
    }

    #[tokio::test]
    async fn chat_route_streams_decrypted_reasoning_content() {
        let response = streaming_chat_response(
            "chat-route-reasoning-stream",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true,"reasoning":{"effort":"high"}}"#,
            vec![
                MockStreamFrame::Reasoning("Thinking"),
                MockStreamFrame::Text("Answer"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let data = sse_data(&body);
        assert_eq!(data.len(), 4);
        let reasoning: Value =
            serde_json::from_str(data[0]).expect("reasoning chunk should be JSON");
        let answer: Value = serde_json::from_str(data[1]).expect("answer chunk should be JSON");

        assert_eq!(reasoning["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(
            reasoning["choices"][0]["delta"]["reasoning_content"],
            "Thinking"
        );
        assert!(answer["choices"][0]["delta"].get("role").is_none());
        assert_eq!(answer["choices"][0]["delta"]["content"], "Answer");
        assert_eq!(data.last().copied(), Some("[DONE]"));
    }

    #[tokio::test]
    async fn chat_route_streams_multiple_decrypted_content_chunks() {
        let response = streaming_chat_response(
            "chat-route-multiple-chunks",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Text(" world"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let data = sse_data(&body);
        let first: Value = serde_json::from_str(data[0]).expect("first chunk should be JSON");
        let second: Value = serde_json::from_str(data[1]).expect("second chunk should be JSON");

        assert_eq!(first["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(first["choices"][0]["delta"]["content"], "Hello");
        assert!(second["choices"][0]["delta"].get("role").is_none());
        assert_eq!(second["choices"][0]["delta"]["content"], " world");
        assert_eq!(data.last().copied(), Some("[DONE]"));
    }

    #[tokio::test]
    async fn chat_route_passes_through_usage_chunk_when_requested_and_upstream_provides_it() {
        let response = streaming_chat_response(
            "chat-route-usage",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true,"stream_options":{"include_usage":true}}"#,
            vec![
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Usage,
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let data = sse_data(&body);
        assert_eq!(data.len(), 4);
        let usage_chunk: Value = serde_json::from_str(data[2]).expect("usage chunk should be JSON");
        assert_eq!(usage_chunk["choices"], json!([]));
        assert_eq!(usage_chunk["usage"]["total_tokens"], 3);
        assert_eq!(data[3], "[DONE]");
    }

    #[tokio::test]
    async fn chat_route_returns_buffered_non_streaming_completion() {
        let response = chat_response(
            "chat-route-non-streaming-success",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            vec![
                MockStreamFrame::NullContent,
                MockStreamFrame::EmptyContent,
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Text(" world"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(HEADER_PROXY_E2EE).unwrap(),
            "verified"
        );
        let body = json_body(response).await;
        assert_eq!(body["object"], "chat.completion");
        assert_eq!(body["id"], "chatcmpl-upstream-test");
        assert_eq!(body["created"], 1_717_171_717);
        assert_eq!(body["model"], "e2ee-test");
        assert_eq!(body["choices"][0]["index"], 0);
        assert_eq!(body["choices"][0]["message"]["role"], "assistant");
        assert_eq!(body["choices"][0]["message"]["content"], "Hello world");
        assert_eq!(body["choices"][0]["finish_reason"], "stop");
        assert!(body["usage"].is_null());
    }

    #[tokio::test]
    async fn chat_route_returns_buffered_reasoning_content() {
        let response = chat_response(
            "chat-route-reasoning-non-streaming",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false,"reasoning_effort":"medium"}"#,
            vec![
                MockStreamFrame::Reasoning("Think "),
                MockStreamFrame::Reasoning("first."),
                MockStreamFrame::Text("Answer"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(
            body["choices"][0]["message"]["reasoning_content"],
            "Think first."
        );
        assert_eq!(body["choices"][0]["message"]["content"], "Answer");
    }

    #[tokio::test]
    async fn chat_route_treats_omitted_stream_as_buffered_non_streaming() {
        let response = chat_response(
            "chat-route-omitted-stream",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}]}"#,
            vec![MockStreamFrame::Text("Hello"), MockStreamFrame::Done],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(body["object"], "chat.completion");
        assert_eq!(body["choices"][0]["message"]["content"], "Hello");
        assert_eq!(body["choices"][0]["finish_reason"], "stop");
    }

    #[tokio::test]
    async fn chat_route_streams_incremental_tool_call_chunks() {
        let response = streaming_chat_response(
            "chat-route-tool-stream",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>\n{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}\n</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        assert_eq!(chunks[0]["choices"][0]["delta"]["role"], "assistant");

        let tool_calls = streamed_tool_call_deltas(&chunks);
        assert!(!tool_calls.is_empty());
        let first = tool_calls[0];
        assert_eq!(first["index"], 0);
        assert!(first["id"].as_str().unwrap().starts_with("call_"));
        assert_eq!(first["type"], "function");
        assert_eq!(first["function"]["name"], "search_web");
        for later in &tool_calls[1..] {
            assert!(later.get("id").is_none());
            assert!(later.get("type").is_none());
            assert!(later["function"].get("name").is_none());
        }
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"example"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["delta"], json!({}));
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_streams_text_then_incremental_tool_call() {
        let response = streaming_chat_response(
            "chat-route-tool-stream-mixed-text",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::NullContent,
                MockStreamFrame::EmptyContent,
                MockStreamFrame::Text("I'll check that. "),
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}"),
                MockStreamFrame::Text("</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        assert_eq!(chunks[0]["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(streamed_content(&chunks), "I'll check that. ");

        let tool_calls = streamed_tool_call_deltas(&chunks);
        assert!(!tool_calls.is_empty());
        assert_eq!(tool_calls[0]["function"]["name"], "search_web");
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"example"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_fails_closed_on_unterminated_streamed_tool_call() {
        // A tool call truncated mid-JSON is beyond the lenient closing-marker
        // recovery; the stream fails closed.
        let response = streaming_chat_response(
            "chat-route-tool-stream-missing-close",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("I'll check that. "),
                MockStreamFrame::Text("<tool_call>{\"name\":"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_stream_body_fails(response).await;
    }

    #[tokio::test]
    async fn chat_route_streams_hermes_format_tool_call_from_glm_model() {
        // Observed live: e2ee-glm-5-1 emits the prompt-instructed Hermes
        // format. All models parse as Hermes regardless of model ID.
        let response = streaming_chat_response(
            "chat-route-tool-stream-glm-hermes",
            r#"{"model":"e2ee-glm-5-1","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>\n{\"name\":\"search_web\",\"arguments\":"),
                MockStreamFrame::Text("{\"query\":\"example\"}}\n</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        let tool_calls = streamed_tool_call_deltas(&chunks);
        assert!(!tool_calls.is_empty());
        assert_eq!(tool_calls[0]["function"]["name"], "search_web");
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"example"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_recovers_streamed_tool_call_with_truncated_closing_marker() {
        // Observed live: Venice cuts `</tool_call>` for some models. A
        // complete call missing only the closing marker is recovered.
        let response = streaming_chat_response(
            "chat-route-tool-stream-truncated-close",
            r#"{"model":"e2ee-glm-4-7-flash-p","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>\n{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}\n"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        let tool_calls = streamed_tool_call_deltas(&chunks);
        assert!(!tool_calls.is_empty());
        assert_eq!(tool_calls[0]["function"]["name"], "search_web");
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"example"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_streams_multiple_tool_calls_split_across_chunks() {
        let response = streaming_chat_response(
            "chat-route-tool-stream-multiple-calls",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"first\"}}"),
                MockStreamFrame::Text("</tool_call><tool_call>{\"name\":\"search_web\",\"arguments\":"),
                MockStreamFrame::Text("{\"query\":\"second\"}}</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        assert_eq!(chunks[0]["choices"][0]["delta"]["role"], "assistant");
        let tool_calls = streamed_tool_call_deltas(&chunks);
        let first = tool_calls
            .iter()
            .find(|tool_call| tool_call["index"] == 0 && tool_call.get("id").is_some())
            .expect("first call should have an id-bearing fragment");
        let second = tool_calls
            .iter()
            .find(|tool_call| tool_call["index"] == 1 && tool_call.get("id").is_some())
            .expect("second call should have an id-bearing fragment");
        assert_eq!(first["function"]["name"], "search_web");
        assert_eq!(second["function"]["name"], "search_web");
        assert_ne!(first["id"], second["id"]);
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"first"}"#
        );
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 1),
            r#"{"query":"second"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["delta"], json!({}));
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_tool_stream_passes_through_usage_chunk_when_requested() {
        let response = streaming_chat_response(
            "chat-route-tool-stream-usage",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Usage,
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        // OpenAI include_usage can arrive after the finish chunk and must still pass through.
        let usage_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(usage_chunk["choices"], json!([]));
        assert_eq!(usage_chunk["usage"]["total_tokens"], 3);
        let finish_chunk = &chunks[chunks.len() - 2];
        assert_eq!(finish_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_fails_closed_when_streamed_tool_call_exceeds_max_bytes() {
        let model_public_key = ProxyInstanceKey::generate().public_key_hex().to_owned();
        let base_url = spawn_streaming_venice_server(
            model_public_key,
            true,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"this argument body is much longer than the configured cap\"}}</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;
        let mut config = chat_config_with_basic_test_attestation();
        config.tools.tool_call_max_bytes = 16;

        let response = request_chat_with_config(
            config,
            "chat-route-tool-stream-max-bytes",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            base_url,
        )
        .await;

        assert_stream_body_fails(response).await;
    }

    #[tokio::test]
    async fn chat_route_streams_all_tool_calls_when_parallel_tool_calls_false() {
        // `parallel_tool_calls` is accepted for OpenAI compatibility but
        // ignored; all parsed tool calls are streamed.
        let response = streaming_chat_response(
            "chat-route-tool-stream-parallel-false",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":true,"parallel_tool_calls":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"],"additionalProperties":false}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"first\"}}</tool_call>"),
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"second\"}}</tool_call>"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);

        assert_eq!(
            streamed_tool_call_arguments(&chunks, 0),
            r#"{"query":"first"}"#
        );
        assert_eq!(
            streamed_tool_call_arguments(&chunks, 1),
            r#"{"query":"second"}"#
        );

        let final_chunk = chunks.last().expect("stream should have chunks");
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "tool_calls");
    }

    #[tokio::test]
    async fn chat_route_returns_non_streaming_tool_call_body_from_mixed_text() {
        let response = chat_response(
            "chat-route-tool-non-stream-mixed-text",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]}"#,
            vec![
                MockStreamFrame::Text("I'll check that. <tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}</tool_call>"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(body["choices"][0]["finish_reason"], "tool_calls");
        let tool_call = &body["choices"][0]["message"]["tool_calls"][0];
        assert_eq!(tool_call["function"]["name"], "search_web");
        assert_eq!(tool_call["function"]["arguments"], r#"{"query":"example"}"#);
    }

    #[tokio::test]
    async fn chat_route_returns_non_streaming_tool_call_body() {
        let response = chat_response(
            "chat-route-tool-non-stream",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}</tool_call>"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(body["object"], "chat.completion");
        assert!(body["choices"][0]["message"]["content"].is_null());
        assert_eq!(body["choices"][0]["finish_reason"], "tool_calls");
        let tool_call = &body["choices"][0]["message"]["tool_calls"][0];
        assert!(tool_call["id"].as_str().unwrap().starts_with("call_"));
        assert_eq!(tool_call["type"], "function");
        assert_eq!(tool_call["function"]["name"], "search_web");
        assert_eq!(tool_call["function"]["arguments"], r#"{"query":"example"}"#);
    }

    #[tokio::test]
    async fn chat_route_returns_non_streaming_multiple_tool_calls() {
        let response = chat_response(
            "chat-route-tool-non-stream-multiple-calls",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"first\"}}</tool_call>\n<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"second\"}}</tool_call>"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(body["choices"][0]["finish_reason"], "tool_calls");
        assert!(body["choices"][0]["message"]["content"].is_null());
        let tool_calls = body["choices"][0]["message"]["tool_calls"]
            .as_array()
            .expect("tool_calls should be an array");
        assert_eq!(tool_calls.len(), 2);
        assert_eq!(tool_calls[0]["function"]["name"], "search_web");
        assert_eq!(
            tool_calls[0]["function"]["arguments"],
            r#"{"query":"first"}"#
        );
        assert_eq!(
            tool_calls[1]["function"]["arguments"],
            r#"{"query":"second"}"#
        );
        assert_ne!(tool_calls[0]["id"], tool_calls[1]["id"]);
    }

    #[tokio::test]
    async fn chat_route_tool_mode_leaves_normal_text_unaffected() {
        let response = streaming_chat_response(
            "chat-route-tool-normal-text",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object"}}}]}"#,
            vec![
                MockStreamFrame::Text("Hello without tools"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);
        assert_eq!(chunks[0]["choices"][0]["delta"]["role"], "assistant");
        assert_eq!(streamed_content(&chunks), "Hello without tools");
        assert!(streamed_tool_call_deltas(&chunks).is_empty());
    }

    #[tokio::test]
    async fn chat_route_treats_marker_like_non_protocol_text_as_normal_text() {
        let response = streaming_chat_response(
            "chat-route-tool-marker-like-text",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object"}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_cal>{not actually a marker}"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let chunks = sse_json_chunks(&body);
        assert_eq!(
            streamed_content(&chunks),
            "<tool_cal>{not actually a marker}"
        );
        assert!(streamed_tool_call_deltas(&chunks).is_empty());
    }

    #[tokio::test]
    async fn chat_route_retries_invalid_tool_call_and_returns_success() {
        let response = chat_response_sequence(
            "chat-route-tool-retry-success",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]}"#,
            vec![
                vec![
                    MockStreamFrame::Text("<tool_call>{\"name\":\"unknown\",\"arguments\":{\"query\":\"example\"}}</tool_call>"),
                    MockStreamFrame::Done,
                ],
                vec![
                    MockStreamFrame::Text("<tool_call>{\"name\":\"search_web\",\"arguments\":{\"query\":\"example\"}}</tool_call>"),
                    MockStreamFrame::Done,
                ],
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(HEADER_PROXY_TOOL_RETRIES).unwrap(),
            "1"
        );
        let body = json_body(response).await;
        assert_eq!(body["choices"][0]["finish_reason"], "tool_calls");
        assert_eq!(
            body["choices"][0]["message"]["tool_calls"][0]["function"]["name"],
            "search_web"
        );
    }

    #[tokio::test]
    async fn chat_route_returns_retry_failure_error_shape() {
        let response = chat_response(
            "chat-route-tool-retry-failure",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"search"}],"stream":false,"tools":[{"type":"function","function":{"name":"search_web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]}"#,
            vec![
                MockStreamFrame::Text("<tool_call>{\"name\":\"unknown\",\"arguments\":{}}</tool_call>"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "invalid_tool_call"
        );
        let body = json_body(response).await;
        assert_eq!(body["error"]["type"], "proxy_tool_call_error");
        assert_eq!(body["error"]["code"], "invalid_tool_call");
        assert_eq!(body["error"]["details"]["max_retries"], 2);
        assert!(
            body["error"]["details"]["last_validation_error"]
                .as_str()
                .unwrap()
                .contains("unknown tool name")
        );
    }

    #[tokio::test]
    async fn chat_route_non_streaming_fails_closed_on_upstream_error_response() {
        let response = chat_response_with_upstream_status(
            "chat-route-non-streaming-upstream-error",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "upstream_status_error"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "proxy_upstream_error");
        assert_eq!(body.error.code, "upstream_status_error");
    }

    #[tokio::test]
    async fn chat_route_non_streaming_fails_closed_on_malformed_upstream_payload() {
        let response = chat_response(
            "chat-route-non-streaming-malformed",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            vec![MockStreamFrame::Raw("data: {\"choices\":\"bad\"}\n\n")],
        )
        .await;

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "upstream_malformed_response"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "proxy_upstream_error");
        assert_eq!(body.error.code, "upstream_malformed_response");
    }

    #[tokio::test]
    async fn chat_route_non_streaming_fails_closed_on_missing_encrypted_content() {
        let response = chat_response(
            "chat-route-non-streaming-missing-content",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            vec![MockStreamFrame::Finish("stop"), MockStreamFrame::Done],
        )
        .await;

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "e2ee_response_decryption_failed"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "proxy_e2ee_error");
        assert_eq!(body.error.code, "e2ee_response_decryption_failed");
    }

    #[tokio::test]
    async fn chat_route_non_streaming_fails_closed_on_decryption_failure() {
        let response = chat_response(
            "chat-route-non-streaming-decryption-failure",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            vec![MockStreamFrame::TextForWrongRecipient(" secret"), MockStreamFrame::Done],
        )
        .await;

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "e2ee_response_decryption_failed"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "proxy_e2ee_error");
        assert_eq!(body.error.code, "e2ee_response_decryption_failed");
    }

    #[tokio::test]
    async fn chat_route_non_streaming_passes_through_usage_when_available() {
        let response = chat_response(
            "chat-route-non-streaming-usage",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
            vec![
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::Finish("stop"),
                MockStreamFrame::Usage,
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = json_body(response).await;
        assert_eq!(body["choices"][0]["message"]["content"], "Hello");
        assert_eq!(body["usage"]["prompt_tokens"], 1);
        assert_eq!(body["usage"]["completion_tokens"], 2);
        assert_eq!(body["usage"]["total_tokens"], 3);
    }

    #[tokio::test]
    async fn chat_route_fails_closed_on_upstream_stream_error_event() {
        let response = streaming_chat_response(
            "chat-route-upstream-error",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![MockStreamFrame::Error("model failed")],
        )
        .await;

        assert_stream_body_fails(response).await;
    }

    #[tokio::test]
    async fn chat_route_fails_closed_on_malformed_upstream_event() {
        let response = streaming_chat_response(
            "chat-route-malformed-event",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![MockStreamFrame::Raw("data: {\"choices\":\n\n")],
        )
        .await;

        assert_stream_body_fails(response).await;
    }

    #[tokio::test]
    async fn chat_route_fails_closed_on_decryption_failure_mid_stream() {
        let response = streaming_chat_response(
            "chat-route-decryption-failure",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![
                MockStreamFrame::Text("Hello"),
                MockStreamFrame::TextForWrongRecipient(" secret"),
                MockStreamFrame::Done,
            ],
        )
        .await;

        assert_stream_body_fails(response).await;
    }

    #[tokio::test]
    async fn chat_route_synthesizes_final_finish_chunk_before_done_when_needed() {
        let response = streaming_chat_response(
            "chat-route-final-done",
            r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":true}"#,
            vec![MockStreamFrame::Text("Hello"), MockStreamFrame::Done],
        )
        .await;

        assert_eq!(response.status(), StatusCode::OK);
        let body = response_body(response).await;
        let data = sse_data(&body);
        assert_eq!(data.len(), 3);
        let final_chunk: Value = serde_json::from_str(data[1]).expect("final chunk should be JSON");
        assert_eq!(final_chunk["choices"][0]["delta"], json!({}));
        assert_eq!(final_chunk["choices"][0]["finish_reason"], "stop");
        assert_eq!(data[2], "[DONE]");
    }

    #[tokio::test]
    async fn chat_route_attestation_failure_prevents_request_construction() {
        let model_public_key = ProxyInstanceKey::generate().public_key_hex().to_owned();
        let base_url = spawn_attestation_server(model_public_key, false).await;
        let app = router_with_venice_client(
            chat_config_with_basic_test_attestation(),
            test_venice_client_for_base_url(base_url),
        );

        let response = app
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/v1/chat/completions")
                    .header("content-type", "application/json")
                    .header(HEADER_PROXY_SESSION_ID, "chat-route-attestation-failure")
                    .body(Body::from(
                        r#"{"model":"e2ee-test","messages":[{"role":"user","content":"hello"}],"stream":false}"#,
                    ))
                    .expect("request should build"),
            )
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "attestation_upstream_not_verified"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "proxy_attestation_error");
        assert_eq!(body.error.code, "attestation_upstream_not_verified");
    }

    #[tokio::test]
    async fn unknown_route_returns_openai_style_not_found() {
        let response = test_app()
            .oneshot(
                Request::builder()
                    .uri("/v1/unknown")
                    .body(Body::empty())
                    .expect("request should build"),
            )
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "not_found"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "invalid_request_error");
        assert_eq!(body.error.code, "not_found");
    }

    #[tokio::test]
    async fn unsupported_method_returns_openai_style_method_error() {
        let response = test_app()
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/v1/models")
                    .body(Body::empty())
                    .expect("request should build"),
            )
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "method_not_allowed"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "invalid_request_error");
        assert_eq!(body.error.code, "method_not_allowed");
    }

    #[tokio::test]
    async fn malformed_chat_json_uses_axum_extractor_rejection() {
        let response = test_app()
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/v1/chat/completions")
                    .header("content-type", "application/json")
                    .body(Body::from("{"))
                    .expect("request should build"),
            )
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        assert!(response.headers().get(HEADER_PROXY_ERROR_CODE).is_none());
    }

    #[tokio::test]
    async fn non_object_chat_json_returns_structured_invalid_request() {
        let response = test_app()
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/v1/chat/completions")
                    .header("content-type", "application/json")
                    .body(Body::from("[]"))
                    .expect("request should build"),
            )
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        assert_eq!(
            response.headers().get(HEADER_PROXY_ERROR_CODE).unwrap(),
            "invalid_request"
        );
        let body = error_body(response).await;
        assert_eq!(body.error.kind, "invalid_request_error");
        assert_eq!(body.error.code, "invalid_request");
    }

    #[derive(Debug, Clone)]
    enum MockStreamFrame {
        Role,
        NullContent,
        EmptyContent,
        Text(&'static str),
        Reasoning(&'static str),
        TextForWrongRecipient(&'static str),
        Finish(&'static str),
        Usage,
        Done,
        Error(&'static str),
        Raw(&'static str),
    }

    async fn streaming_chat_response(
        session_id: &'static str,
        request_body: &'static str,
        frames: Vec<MockStreamFrame>,
    ) -> Response {
        chat_response(session_id, request_body, frames).await
    }

    async fn chat_response(
        session_id: &'static str,
        request_body: &'static str,
        frames: Vec<MockStreamFrame>,
    ) -> Response {
        let model_public_key = ProxyInstanceKey::generate().public_key_hex().to_owned();
        let base_url = spawn_streaming_venice_server(model_public_key, true, frames).await;
        request_chat(session_id, request_body, base_url).await
    }

    async fn chat_response_sequence(
        session_id: &'static str,
        request_body: &'static str,
        attempts: Vec<Vec<MockStreamFrame>>,
    ) -> Response {
        let model_public_key = ProxyInstanceKey::generate().public_key_hex().to_owned();
        let base_url =
            spawn_streaming_venice_server_sequence(model_public_key, true, attempts).await;
        request_chat(session_id, request_body, base_url).await
    }

    async fn chat_response_with_upstream_status(
        session_id: &'static str,
        request_body: &'static str,
        upstream_status: StatusCode,
    ) -> Response {
        let model_public_key = ProxyInstanceKey::generate().public_key_hex().to_owned();
        let base_url =
            spawn_venice_server_with_chat_status(model_public_key, upstream_status).await;
        request_chat(session_id, request_body, base_url).await
    }

    async fn request_chat(
        session_id: &'static str,
        request_body: &'static str,
        base_url: String,
    ) -> Response {
        request_chat_with_config(
            chat_config_with_basic_test_attestation(),
            session_id,
            request_body,
            base_url,
        )
        .await
    }

    async fn request_chat_with_config(
        config: ProxyConfig,
        session_id: &'static str,
        request_body: &'static str,
        base_url: String,
    ) -> Response {
        let app = router_with_venice_client(config, test_venice_client_for_base_url(base_url));

        app.oneshot(
            Request::builder()
                .method(Method::POST)
                .uri("/v1/chat/completions")
                .header("content-type", "application/json")
                .header(HEADER_PROXY_SESSION_ID, session_id)
                .body(Body::from(request_body))
                .expect("request should build"),
        )
        .await
        .expect("request should complete")
    }

    async fn json_body(response: Response) -> Value {
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("response body should buffer");
        serde_json::from_slice(&bytes).expect("response should be JSON")
    }

    async fn response_body(response: Response) -> String {
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("response body should buffer");
        String::from_utf8(bytes.to_vec()).expect("response body should be UTF-8")
    }

    async fn assert_stream_body_fails(response: Response) {
        assert_eq!(response.status(), StatusCode::OK);
        let result = axum::body::to_bytes(response.into_body(), usize::MAX).await;
        assert!(
            result.is_err(),
            "stream body should fail closed instead of completing successfully"
        );
    }

    fn sse_data(body: &str) -> Vec<&str> {
        body.lines()
            .filter_map(|line| line.strip_prefix("data: "))
            .collect()
    }

    /// Parses all SSE data chunks before the trailing `[DONE]` as JSON.
    fn sse_json_chunks(body: &str) -> Vec<Value> {
        let data = sse_data(body);
        assert_eq!(data.last().copied(), Some("[DONE]"));
        data[..data.len() - 1]
            .iter()
            .map(|chunk| serde_json::from_str(chunk).expect("SSE chunk should be JSON"))
            .collect()
    }

    /// Concatenates all `delta.content` text across stream chunks.
    fn streamed_content(chunks: &[Value]) -> String {
        chunks
            .iter()
            .filter_map(|chunk| chunk["choices"][0]["delta"]["content"].as_str())
            .collect()
    }

    /// Flattens all `delta.tool_calls` entries across stream chunks.
    fn streamed_tool_call_deltas(chunks: &[Value]) -> Vec<&Value> {
        chunks
            .iter()
            .filter_map(|chunk| chunk["choices"][0]["delta"]["tool_calls"].as_array())
            .flatten()
            .collect()
    }

    /// Concatenates streamed argument fragments for one tool-call index.
    fn streamed_tool_call_arguments(chunks: &[Value], index: u64) -> String {
        streamed_tool_call_deltas(chunks)
            .iter()
            .filter(|tool_call| tool_call["index"] == json!(index))
            .filter_map(|tool_call| tool_call["function"]["arguments"].as_str())
            .collect()
    }

    async fn spawn_streaming_venice_server(
        model_public_key: String,
        verified: bool,
        frames: Vec<MockStreamFrame>,
    ) -> String {
        spawn_streaming_venice_server_sequence(model_public_key, verified, vec![frames]).await
    }

    async fn spawn_streaming_venice_server_sequence(
        model_public_key: String,
        verified: bool,
        attempts: Vec<Vec<MockStreamFrame>>,
    ) -> String {
        let chat_attempts = Arc::new(Mutex::new(VecDeque::from(attempts)));
        let attestation_key = model_public_key.clone();
        let app = Router::new()
            .route(
                "/api/v1/tee/attestation",
                get(move |Query(query): Query<HashMap<String, String>>| {
                    let model_public_key = attestation_key.clone();
                    async move {
                        Json(json!({
                            "attestation": {
                                "verified": verified,
                                "nonce": query.get("nonce").cloned().unwrap_or_default(),
                                "model": query.get("model").cloned().unwrap_or_default(),
                                "tee_provider": "tdx",
                                "signing_key": model_public_key,
                            }
                        }))
                    }
                }),
            )
            .route(
                "/api/v1/chat/completions",
                post(move |headers: HeaderMap, Json(body): Json<Value>| {
                    let chat_attempts = chat_attempts.clone();
                    async move {
                        let Some(client_public_key) = headers
                            .get(crate::venice::HEADER_VENICE_TEE_CLIENT_PUB_KEY)
                            .and_then(|value| value.to_str().ok())
                        else {
                            return (
                                StatusCode::BAD_REQUEST,
                                [("content-type", "text/plain")],
                                "missing client key".to_owned(),
                            );
                        };
                        if body.get("stream").and_then(Value::as_bool) != Some(true) {
                            return (
                                StatusCode::BAD_REQUEST,
                                [("content-type", "text/plain")],
                                "upstream request must stream".to_owned(),
                            );
                        }
                        let messages = body.get("messages").and_then(Value::as_array);
                        if messages.is_none_or(|messages| {
                            messages.is_empty()
                                || !messages.iter().all(|message| {
                                    message.get("role").and_then(Value::as_str).is_some()
                                        && message
                                            .get("content")
                                            .and_then(Value::as_str)
                                            .is_some_and(|content| {
                                                !content.is_empty()
                                                    && content
                                                        .chars()
                                                        .all(|ch| ch.is_ascii_hexdigit())
                                            })
                                })
                        }) {
                            return (
                                StatusCode::BAD_REQUEST,
                                [("content-type", "text/plain")],
                                "messages must be encrypted message objects".to_owned(),
                            );
                        }

                        let frames = {
                            let mut attempts = chat_attempts
                                .lock()
                                .expect("mock chat attempts mutex should not be poisoned");
                            if attempts.len() > 1 {
                                attempts.pop_front().expect("attempts length checked above")
                            } else {
                                attempts.front().cloned().unwrap_or_default()
                            }
                        };

                        (
                            StatusCode::OK,
                            [("content-type", "text/event-stream")],
                            render_mock_sse(&frames, client_public_key),
                        )
                    }
                }),
            );
        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("mock Venice listener should bind");
        let addr = listener
            .local_addr()
            .expect("mock Venice listener should have local address");

        tokio::spawn(async move {
            axum::serve(listener, app)
                .await
                .expect("mock Venice server should run");
        });

        format!("http://{addr}/api/v1")
    }

    async fn spawn_venice_server_with_chat_status(
        model_public_key: String,
        upstream_status: StatusCode,
    ) -> String {
        let attestation_key = model_public_key.clone();
        let app = Router::new()
            .route(
                "/api/v1/tee/attestation",
                get(move |Query(query): Query<HashMap<String, String>>| {
                    let model_public_key = attestation_key.clone();
                    async move {
                        Json(json!({
                            "attestation": {
                                "verified": true,
                                "nonce": query.get("nonce").cloned().unwrap_or_default(),
                                "model": query.get("model").cloned().unwrap_or_default(),
                                "tee_provider": "tdx",
                                "signing_key": model_public_key,
                            }
                        }))
                    }
                }),
            )
            .route(
                "/api/v1/chat/completions",
                post(move || async move { upstream_status }),
            );
        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("mock Venice listener should bind");
        let addr = listener
            .local_addr()
            .expect("mock Venice listener should have local address");

        tokio::spawn(async move {
            axum::serve(listener, app)
                .await
                .expect("mock Venice server should run");
        });

        format!("http://{addr}/api/v1")
    }

    fn render_mock_sse(frames: &[MockStreamFrame], client_public_key: &str) -> String {
        let codec = E2eeCodec::default();
        let mut output = String::new();
        for frame in frames {
            match frame {
                MockStreamFrame::Role => {
                    output.push_str(&format!("data: {}\n\n", upstream_role_chunk()));
                }
                MockStreamFrame::NullContent => {
                    output.push_str(&format!("data: {}\n\n", upstream_null_content_chunk()));
                }
                MockStreamFrame::EmptyContent => {
                    output.push_str(&format!(
                        "data: {}\n\n",
                        upstream_content_chunk(String::new())
                    ));
                }
                MockStreamFrame::Text(content) => {
                    let encrypted = codec
                        .encrypt_content(content, client_public_key)
                        .expect("mock content should encrypt")
                        .into_hex();
                    output.push_str(&format!("data: {}\n\n", upstream_content_chunk(encrypted)));
                }
                MockStreamFrame::Reasoning(content) => {
                    let encrypted = codec
                        .encrypt_content(content, client_public_key)
                        .expect("mock reasoning content should encrypt")
                        .into_hex();
                    output.push_str(&format!(
                        "data: {}\n\n",
                        upstream_reasoning_content_chunk(encrypted)
                    ));
                }
                MockStreamFrame::TextForWrongRecipient(content) => {
                    let wrong_key = ProxyInstanceKey::generate();
                    let encrypted = codec
                        .encrypt_content(content, wrong_key.public_key_hex())
                        .expect("mock content should encrypt")
                        .into_hex();
                    output.push_str(&format!("data: {}\n\n", upstream_content_chunk(encrypted)));
                }
                MockStreamFrame::Finish(reason) => {
                    output.push_str(&format!("data: {}\n\n", upstream_finish_chunk(reason)));
                }
                MockStreamFrame::Usage => {
                    output.push_str(&format!("data: {}\n\n", upstream_usage_chunk()));
                }
                MockStreamFrame::Done => output.push_str("data: [DONE]\n\n"),
                MockStreamFrame::Error(message) => {
                    output.push_str(&format!(
                        "event: error\ndata: {}\n\n",
                        json!({ "message": message })
                    ));
                }
                MockStreamFrame::Raw(raw) => output.push_str(raw),
            }
        }
        output
    }

    fn upstream_role_chunk() -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [{
                "index": 0,
                "delta": { "role": "assistant" },
                "finish_reason": null,
            }],
        })
    }

    fn upstream_content_chunk(encrypted_content: String) -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [{
                "index": 0,
                "delta": { "content": encrypted_content },
                "finish_reason": null,
            }],
        })
    }

    fn upstream_reasoning_content_chunk(encrypted_content: String) -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [{
                "index": 0,
                "delta": { "reasoning_content": encrypted_content },
                "finish_reason": null,
            }],
        })
    }

    fn upstream_null_content_chunk() -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [{
                "index": 0,
                "delta": { "content": Value::Null },
                "finish_reason": null,
            }],
        })
    }

    fn upstream_finish_chunk(reason: &str) -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [{
                "index": 0,
                "delta": {},
                "finish_reason": reason,
            }],
        })
    }

    fn upstream_usage_chunk() -> Value {
        json!({
            "id": "chatcmpl-upstream-test",
            "object": "chat.completion.chunk",
            "created": 1_717_171_717,
            "model": "e2ee-test",
            "choices": [],
            "usage": {
                "prompt_tokens": 1,
                "completion_tokens": 2,
                "total_tokens": 3,
            },
        })
    }

    async fn spawn_attestation_server(model_public_key: String, verified: bool) -> String {
        let app = Router::new().route(
            "/api/v1/tee/attestation",
            get(move |Query(query): Query<HashMap<String, String>>| {
                let model_public_key = model_public_key.clone();
                async move {
                    Json(json!({
                        "attestation": {
                            "verified": verified,
                            "nonce": query.get("nonce").cloned().unwrap_or_default(),
                            "model": query.get("model").cloned().unwrap_or_default(),
                            "signing_key": model_public_key,
                        }
                    }))
                }
            }),
        );
        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("mock attestation listener should bind");
        let addr = listener
            .local_addr()
            .expect("mock attestation listener should have local address");

        tokio::spawn(async move {
            axum::serve(listener, app)
                .await
                .expect("mock attestation server should run");
        });

        format!("http://{addr}/api/v1")
    }

    #[test]
    fn metadata_header_helper_only_emits_safe_config_headers_by_default() {
        let config = ProxyConfig::default();
        let metadata = ProxyMetadataHeaders::from_config(&config);
        let mut headers = HeaderMap::new();

        metadata.apply(&mut headers);

        assert_eq!(
            headers.get(HEADER_PROXY_ATTESTATION_MODE).unwrap(),
            "independent"
        );
        assert_eq!(headers.get(HEADER_PROXY_TOOL_MODE).unwrap(), "emulated");
        assert!(headers.get(HEADER_PROXY_E2EE).is_none());
        assert!(headers.get(HEADER_PROXY_KEY_BINDING).is_none());
    }
}