File size: 63,998 Bytes
b48ae75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Local protocol bridge and run-scoped provider usage observation for native agents.

One process serves one protocol against an OpenAI-compatible Chat Completions
upstream:

  * ``--protocol anthropic``: POST /v1/messages   -> /chat/completions
  * ``--protocol responses``: POST /v1/responses  -> /chat/completions

The Responses path also handles Codex namespace/custom tools; the Anthropic
path translates Claude Code's text/tool_use/tool_result blocks.  Both paths
stream SSE, forward usage where available, and never log prompts or credentials.
Each POST is submitted exactly once.
"""

from __future__ import annotations

import argparse
import hmac
from contextlib import contextmanager
from collections import OrderedDict
import hashlib
import json
import os
import queue
import re
import sys
import threading
import time
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any
from urllib.parse import urlsplit, parse_qs

import requests


sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "eval" / "native_harness"))
from native_bench.usage import TransportMeter, provider_usage


def usage_endpoint(handler):
    route = urlsplit(handler.path)
    if route.path != "/__native_usage":
        return False
    send = getattr(handler, "send_json", None) or handler._json
    token = os.environ.get("NATIVE_BENCH_USAGE_TOKEN", "")
    if not token or not hmac.compare_digest(handler.headers.get("Authorization", ""), "Bearer " + token):
        send(403, {"error": "invalid usage observer credential"})
        return True
    try:
        if handler.command == "POST":
            size = int(handler.headers.get("Content-Length", "0"))
            if size > 8192 or size < 0:
                raise ValueError("invalid reset body")
            handler.rfile.read(size)
            payload = handler.server.usage_meter.reset()
        else:
            value = parse_qs(route.query).get("at_ns", [None])[0]
            payload = handler.server.usage_meter.snapshot(int(value) if value is not None else None)
        send(200, payload)
    except (ValueError, KeyError, TypeError):
        send(409, {"error": "invalid usage snapshot or active requests"})
    return True


class ObservedResponse:
    def __init__(self, response, meter, record):
        self.response, self.meter, self.record = response, meter, record
        self.raw_usage = {}
        self.kind = "openai"
        self.final = False

    def __getattr__(self, name):
        return getattr(self.response, name)

    def observe(self, payload):
        if not isinstance(payload, dict):
            return
        typ = payload.get("type", "")
        response_payload = payload.get("response")
        response_payload = response_payload if isinstance(response_payload, dict) else {}
        if typ == "message_start":
            self.kind = "anthropic"
            self.raw_usage.update(payload.get("message", {}).get("usage") or {})
        elif typ == "message_delta":
            self.kind = "anthropic"
            self.raw_usage.update(payload.get("usage") or {})
            delta = payload.get("delta")
            stop_reason = delta.get("stop_reason") if isinstance(delta, dict) else None
            if isinstance(stop_reason, str) and stop_reason and isinstance(payload.get("usage"), dict):
                self.final = provider_usage(self.raw_usage, self.kind) is not None
        elif typ == "message_stop":
            self.final = True
        elif typ == "message" and isinstance(payload.get("usage"), dict):
            self.kind = "anthropic"
            self.raw_usage.update(payload["usage"])
            self.final = True
        elif isinstance(response_payload.get("usage"), dict):
            self.raw_usage.update(response_payload["usage"])
            self.final = typ in ("response.completed", "response.incomplete", "response.failed")
        elif isinstance(payload.get("usage"), dict):
            self.raw_usage.update(payload["usage"])
            self.final = bool("choices" in payload or payload.get("object") == "response"
                              or payload.get("object") == "response.compaction" or bool(payload.get("error")))
        if self.final and self.record["finished_ns"] is None:
            self.meter.finish(self.record, provider_usage(self.raw_usage, self.kind))

    @property
    def content(self):
        raw = self.response.content
        try:
            self.observe(json.loads(raw))
        except (ValueError, TypeError):
            pass
        return raw

    def json(self, **kwargs):
        payload = self.response.json(**kwargs)
        self.observe(payload)
        return payload

    def iter_lines(self, **kwargs):
        pending = []
        for line in self.response.iter_lines(**kwargs):
            raw = line.decode("utf-8", "replace") if isinstance(line, bytes) else line
            if raw.startswith("data:"):
                value = raw[5:].strip()
                if value != "[DONE]":
                    pending.append(value)
                    try:
                        payload = json.loads("\n".join(pending))
                    except ValueError:
                        pass
                    else:
                        self.observe(payload)
                        pending = []
            elif not raw.strip():
                pending = []
            yield line


@contextmanager
def metered_post(server, session, *args, **kwargs):
    record = server.usage_meter.begin()
    try:
        with session.post(*args, **kwargs) as response:
            yield ObservedResponse(response, server.usage_meter, record)
    finally:
        if record["finished_ns"] is None:
            server.usage_meter.finish(record, None)


class PassthroughHandler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"

    def log_message(self, *args):
        pass

    def send_json(self, status, payload):
        raw = json.dumps(payload).encode()
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(raw)))
        self.end_headers()
        self.wfile.write(raw)

    def do_GET(self):
        if not usage_endpoint(self):
            self.forward()

    def do_POST(self):
        if not usage_endpoint(self):
            self.forward()

    def forward(self):
        route = urlsplit(self.path)
        if route.scheme or route.netloc or route.path.startswith("//"):
            self.send_json(400, {"error": "invalid upstream path"})
            return
        try:
            size = int(self.headers.get("Content-Length", "0"))
            if size < 0 or size > 100 * 1024 * 1024:
                raise ValueError("invalid request size")
            raw = self.rfile.read(size) if size else b""
            body = json.loads(raw) if raw else {}
            stream = bool(body.get("stream"))
            if stream and route.path.rstrip("/").endswith("/chat/completions"):
                body.setdefault("stream_options", {})["include_usage"] = True
                raw = json.dumps(body, ensure_ascii=False).encode("utf-8")
            headers = {k: v for k, v in self.headers.items() if k.lower() not in
                {"host", "content-length", "connection", "transfer-encoding", "proxy-authorization"}}
            cfg = self.server.config
            if not any(k.lower() in {"authorization", "x-api-key"} for k in headers) and cfg["api_key"]:
                headers["Authorization"] = "Bearer " + cfg["api_key"]
            base_path = urlsplit(cfg["upstream"]).path.rstrip("/")
            suffix = self.path
            if base_path and (route.path == base_path or route.path.startswith(base_path + "/")):
                suffix = route.path[len(base_path):] + ("?" + route.query if route.query else "")
            url = cfg["upstream"].rstrip("/") + suffix
            generation = self.command == "POST" and route.path.rstrip("/").endswith(
                ("/responses", "/responses/compact", "/messages", "/chat/completions"))
            with requests.Session() as session:
                kwargs = dict(data=raw or None, headers=headers, stream=stream,
                    timeout=(15, cfg["timeout"]), allow_redirects=False)
                context = metered_post(self.server, session, url, **kwargs) if generation else session.request(self.command, url, **kwargs)
                with context as response:
                    self.send_response(response.status_code)
                    for key, value in response.headers.items():
                        if key.lower() not in {"content-length", "content-encoding", "transfer-encoding", "connection"}:
                            self.send_header(key, value)
                    self.send_header("Connection", "close")
                    self.end_headers()
                    self.close_connection = True
                    if stream:
                        for line in response.iter_lines(chunk_size=1, decode_unicode=False):
                            self.wfile.write(line + b"\n")
                            self.wfile.flush()
                    else:
                        if generation:
                            try:
                                response.json()
                            except ValueError:
                                pass
                        self.wfile.write(response.content)
        except (BrokenPipeError, ConnectionResetError):
            self.close_connection = True
        except (ValueError, TypeError, requests.RequestException):
            self.close_connection = True


def passthrough_main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", default="127.0.0.1")
    parser.add_argument("--port", type=int, required=True)
    parser.add_argument("--upstream", required=True)
    parser.add_argument("--name", default="")
    parser.add_argument("--timeout", type=float, default=3600)
    args = parser.parse_args(argv)
    parsed = urlsplit(args.upstream)
    if parsed.scheme not in ("http", "https") or not parsed.netloc or parsed.query or parsed.fragment:
        parser.error("invalid upstream base URL")
    server = ThreadingHTTPServer((args.host, args.port), PassthroughHandler)
    server.usage_meter = TransportMeter()
    server.config = {"upstream": args.upstream, "api_key": os.environ.get("PROXY_API_KEY", ""), "timeout": args.timeout}
    start_parent_watch()
    try:
        server.serve_forever()
    finally:
        server.server_close()


def new_id(prefix: str) -> str:
    return prefix + uuid.uuid4().hex


def start_parent_watch() -> None:
    """Exit if the launching bridge process disappears (including SIGKILL)."""
    parent = int(os.environ.get("PROXY_PARENT_PID", "0") or 0)
    if parent <= 0:
        return

    def watch() -> None:
        while True:
            time.sleep(2)
            try:
                os.kill(parent, 0)
            except OSError:
                os._exit(0)

    threading.Thread(target=watch, name="parent-watch", daemon=True).start()


class BadRequest(ValueError):
    pass


class ToolTypes(dict):
    """Kinds plus reversible Responses namespace / Chat name mappings."""

    def __init__(self):
        super().__init__()
        self.names = {}
        self.aliases = {}

    def wire_name(self, name: str, namespace: str | None = None) -> str:
        qualified = namespace + "." + name if namespace else name
        if qualified in self.aliases:
            return self.aliases[qualified]
        if name in self:
            return name
        matches = [alias for alias, original in self.names.items() if original["name"] == name]
        if len(matches) == 1:
            return matches[0]
        if len(matches) > 1:
            raise BadRequest("Ambiguous unqualified historical tool name.")
        return safe_tool_name(qualified.replace(".", "__"))


def safe_tool_name(name: str) -> str:
    safe = re.sub(r"[^a-zA-Z0-9_-]", "_", name)
    if len(safe) > 64:
        safe = safe[:51] + "_" + hashlib.sha256(name.encode()).hexdigest()[:12]
    return safe


def convert_tools(definitions: list) -> tuple[list, ToolTypes]:
    kinds = ToolTypes()
    translated = []

    def add(tool: dict, namespace: str | None = None, namespace_description: str = "") -> None:
        kind = tool.get("type")
        if kind == "namespace":
            if namespace:
                raise BadRequest("Nested namespace tools are not supported.")
            if not tool.get("name") or not isinstance(tool.get("tools"), list):
                raise BadRequest("Namespace tools require a name and a tools list.")
            for child in tool["tools"]:
                add(child, tool["name"], tool.get("description", ""))
            return
        if kind not in ("function", "custom"):
            raise BadRequest("Unsupported Responses tool type: " + str(kind))
        name = tool.get("name")
        if not isinstance(name, str) or not name:
            raise BadRequest("Tools require a name.")
        qualified = namespace + "." + name if namespace else name
        alias = safe_tool_name((namespace + "__" + name) if namespace else name)
        if alias in kinds or qualified in kinds.aliases:
            raise BadRequest("Tool names collide after namespace translation.")
        kinds[alias] = kind
        kinds.names[alias] = {"name": name, "namespace": namespace}
        kinds.aliases[qualified] = alias
        description = tool.get("description", "")
        if namespace_description:
            description = namespace_description + "\n" + description
        fn = {"name": alias, "description": description}
        if kind == "custom":
            if tool.get("format"):
                fn["description"] += "\nInput format: " + json.dumps(tool["format"], ensure_ascii=False)
            fn["parameters"] = {"type": "object", "properties": {"input": {"type": "string", "description": "The complete raw input for this tool."}}, "required": ["input"], "additionalProperties": False}
        else:
            fn["parameters"] = tool.get("parameters") or {"type": "object", "properties": {}}
            if "strict" in tool:
                fn["strict"] = tool["strict"]
        translated.append({"type": "function", "function": fn})

    for definition in definitions:
        add(definition)
    return translated, kinds


def text_content(value: Any) -> str:
    if isinstance(value, str):
        return value
    if value is None:
        return ""
    return json.dumps(value, ensure_ascii=False, separators=(",", ":"))


def message_content(content: Any) -> Any:
    if isinstance(content, str):
        return content
    if content is None:
        return ""
    if not isinstance(content, list):
        raise BadRequest("Message content must be a string or a list.")
    result = []
    for part in content:
        kind = part.get("type")
        if kind in ("input_text", "output_text", "text"):
            result.append({"type": "text", "text": part.get("text", "")})
        elif kind == "input_image":
            image = part.get("image_url")
            if not isinstance(image, str):
                raise BadRequest("Only image_url input images are supported.")
            image_data = {"url": image}
            if part.get("detail"):
                image_data["detail"] = part["detail"]
            result.append({"type": "image_url", "image_url": image_data})
        elif kind == "refusal":
            result.append({"type": "text", "text": part.get("refusal", "")})
        else:
            raise BadRequest("Unsupported message content type: " + str(kind))
    if all(part["type"] == "text" for part in result):
        return "".join(part["text"] for part in result)
    return result


def reasoning_text(item: dict) -> str:
    parts = item.get("content") or item.get("summary") or []
    return "".join(part.get("text", "") for part in parts if isinstance(part, dict) and part.get("type") in ("summary_text", "reasoning_text", "text"))


def convert_request(body: dict, model_name: str, reasoning_lookup=None) -> tuple[dict, dict]:
    if body.get("previous_response_id"):
        raise BadRequest("previous_response_id is not supported; send the complete input history with store=false.")
    if body.get("background"):
        raise BadRequest("Background Responses requests are not supported.")
    tools, tool_types = convert_tools(body.get("tools") or [])
    messages = []
    if body.get("instructions"):
        messages.append({"role": "system", "content": body["instructions"]})
    inputs = body.get("input", [])
    if isinstance(inputs, str):
        inputs = [{"role": "user", "content": inputs}]
    if not isinstance(inputs, list):
        raise BadRequest("input must be a string or a list.")
    pending_reasoning = ""
    for item in inputs:
        if not isinstance(item, dict):
            raise BadRequest("Every input item must be an object.")
        kind = item.get("type", "message")
        if kind == "message":
            role = item.get("role", "user")
            if role == "developer":
                role = "system"
            if role not in ("system", "user", "assistant", "tool"):
                raise BadRequest("Unsupported message role: " + str(role))
            message = {"role": role, "content": message_content(item.get("content", ""))}
            if role == "assistant" and pending_reasoning:
                message["reasoning_content"] = pending_reasoning
                pending_reasoning = ""
            if role == "tool":
                message["tool_call_id"] = item.get("tool_call_id")
            messages.append(message)
        elif kind in ("function_call", "custom_tool_call"):
            call_id = item.get("call_id")
            if not call_id or not item.get("name"):
                raise BadRequest("Tool calls require call_id and name.")
            arguments = item.get("arguments", "") if kind == "function_call" else json.dumps({"input": item.get("input", "")}, ensure_ascii=False)
            tool_call = {"id": call_id, "type": "function", "function": {"name": tool_types.wire_name(item["name"], item.get("namespace")), "arguments": text_content(arguments)}}
            if messages and messages[-1]["role"] == "assistant":
                messages[-1].setdefault("tool_calls", []).append(tool_call)
            else:
                messages.append({"role": "assistant", "content": None, "tool_calls": [tool_call]})
            prior_reasoning = pending_reasoning or (reasoning_lookup(call_id) if reasoning_lookup else "")
            if prior_reasoning and not messages[-1].get("reasoning_content"):
                messages[-1]["reasoning_content"] = prior_reasoning
            pending_reasoning = ""
        elif kind in ("function_call_output", "custom_tool_call_output"):
            if not item.get("call_id"):
                raise BadRequest("Tool outputs require call_id.")
            output = item.get("output", "")
            if isinstance(output, list):
                output = message_content(output)
            messages.append({"role": "tool", "tool_call_id": item["call_id"], "content": text_content(output)})
        elif kind == "reasoning":
            restored = reasoning_text(item)
            if item.get("encrypted_content") and not restored:
                raise BadRequest("Opaque encrypted reasoning cannot be translated; replay this bridge's reasoning summary instead.")
            pending_reasoning += restored
        elif kind == "compaction":
            raise BadRequest("Opaque compaction items cannot be translated; send complete text/tool history.")
        else:
            raise BadRequest("Unsupported input item type: " + str(kind))
    if not messages:
        raise BadRequest("The request contains no supported input messages.")

    upstream = {"model": model_name, "messages": messages, "stream": bool(body.get("stream", False))}
    if tools:
        upstream["tools"] = tools
    choice = body.get("tool_choice")
    if isinstance(choice, dict):
        if choice.get("type") not in ("function", "custom") or not choice.get("name"):
            raise BadRequest("Only named function/custom tool_choice is supported.")
        upstream["tool_choice"] = {"type": "function", "function": {"name": tool_types.wire_name(choice["name"], choice.get("namespace"))}}
    elif choice is not None:
        upstream["tool_choice"] = choice
    for field in ("temperature", "top_p", "parallel_tool_calls", "user"):
        if field in body and body[field] is not None:
            upstream[field] = body[field]
    if body.get("max_output_tokens") is not None:
        upstream["max_tokens"] = body["max_output_tokens"]
    if isinstance(body.get("reasoning"), dict) and body["reasoning"].get("effort"):
        upstream["reasoning_effort"] = body["reasoning"]["effort"]
    if upstream["stream"]:
        upstream["stream_options"] = {"include_usage": True}
    return upstream, tool_types


def convert_usage(usage: Any) -> dict | None:
    if not isinstance(usage, dict):
        return None
    converted = {}
    for original, target in (("prompt_tokens", "input_tokens"), ("completion_tokens", "output_tokens"), ("total_tokens", "total_tokens")):
        if original in usage:
            converted[target] = usage[original]
    prompt_details = usage.get("prompt_tokens_details") or {}
    completion_details = usage.get("completion_tokens_details") or {}
    if "cached_tokens" in prompt_details:
        converted["input_tokens_details"] = {"cached_tokens": prompt_details["cached_tokens"]}
    if "reasoning_tokens" in completion_details:
        converted["output_tokens_details"] = {"reasoning_tokens": completion_details["reasoning_tokens"]}
    return converted or None


def response_object(response_id: str, model: str, created: int, status: str, output: list, usage: dict | None = None, error: dict | None = None, incomplete: dict | None = None) -> dict:
    return {"id": response_id, "object": "response", "created_at": created, "status": status, "error": error, "incomplete_details": incomplete, "model": model, "output": output, "usage": usage, "store": False, "parallel_tool_calls": True}


def tool_item(call: dict, tool_types: dict, status: str) -> dict:
    name = call.get("name", "")
    if not name:
        raise ValueError("Upstream returned an unnamed tool call.")
    custom = tool_types.get(name) == "custom"
    item = {"id": call.get("item_id") or new_id("ctc_" if custom else "fc_"), "type": "custom_tool_call" if custom else "function_call", "call_id": call.get("call_id") or new_id("call_"), "name": name, "status": status}
    if isinstance(tool_types, ToolTypes) and name in tool_types.names:
        original = tool_types.names[name]
        item["name"] = original["name"]
        if original["namespace"]:
            item["namespace"] = original["namespace"]
    arguments = call.get("arguments", "")
    if custom:
        try:
            decoded = json.loads(arguments)
            if not isinstance(decoded, dict) or not isinstance(decoded.get("input"), str):
                raise ValueError("Custom tool arguments require a string input field.")
            item["input"] = decoded["input"]
        except (ValueError, TypeError):
            if status == "incomplete":
                item["input"] = ""
            else:
                raise ValueError("Upstream returned invalid custom tool arguments.") from None
    else:
        item["arguments"] = arguments
    return item


class BridgeServer(ThreadingHTTPServer):
    daemon_threads = True

    def __init__(self, address: tuple, config: dict):
        self.config = config
        self.usage_meter = TransportMeter()
        self.reasoning_cache = OrderedDict()
        self.reasoning_cache_bytes = 0
        self.reasoning_lock = threading.Lock()
        super().__init__(address, ResponsesHandler)

    def remember_reasoning(self, call_id: str, reasoning: str) -> None:
        if not call_id or not reasoning:
            return
        with self.reasoning_lock:
            previous = self.reasoning_cache.pop(call_id, "")
            self.reasoning_cache_bytes -= len(previous.encode("utf-8"))
            self.reasoning_cache[call_id] = reasoning
            self.reasoning_cache_bytes += len(reasoning.encode("utf-8"))
            while len(self.reasoning_cache) > 128 or self.reasoning_cache_bytes > 32 * 1024 * 1024:
                _, removed = self.reasoning_cache.popitem(last=False)
                self.reasoning_cache_bytes -= len(removed.encode("utf-8"))

    def recall_reasoning(self, call_id: str) -> str:
        with self.reasoning_lock:
            return self.reasoning_cache.get(call_id, "")


class ResponsesHandler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    server_version = "ResponsesChatBridge/1.0"

    def log_message(self, fmt: str, *args: Any) -> None:
        # Deliberately exclude arbitrary URLs, request bodies, and credentials.
        sys.stderr.write("%s %s %s\n" % (time.strftime("%Y-%m-%dT%H:%M:%S%z"), self.command, urlsplit(self.path).path))

    def send_json(self, status: int, payload: dict) -> None:
        raw = json.dumps(payload, ensure_ascii=False).encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", "application/json; charset=utf-8")
        self.send_header("Content-Length", str(len(raw)))
        self.end_headers()
        self.wfile.write(raw)

    def error_json(self, status: int, message: str, code: str = "invalid_request_error") -> None:
        self.send_json(status, {"error": {"type": code, "code": code, "message": message}})

    def do_GET(self) -> None:
        if usage_endpoint(self):
            return
        route = urlsplit(self.path).path.rstrip("/")
        if route in ("/health", "/v1/health"):
            self.send_json(200, {"status": "ok", "model": self.server.config["name"], "stateless": True})
        elif route in ("/models", "/v1/models"):
            self.send_json(200, {"object": "list", "data": [{"id": self.server.config["name"], "object": "model", "owned_by": "catalog"}]})
        else:
            self.error_json(404, "Unknown route.")

    def do_POST(self) -> None:
        if usage_endpoint(self):
            return
        if urlsplit(self.path).path.rstrip("/") not in ("/responses", "/v1/responses"):
            self.error_json(404, "Only /v1/responses is supported.")
            return
        try:
            size = int(self.headers.get("Content-Length", "0"))
            if size <= 0 or size > 100 * 1024 * 1024:
                raise BadRequest("A JSON request body of at most 100 MiB is required.")
            body = json.loads(self.rfile.read(size))
            if not isinstance(body, dict):
                raise BadRequest("The request must be a JSON object.")
            upstream, tool_types = convert_request(body, self.server.config["name"], self.server.recall_reasoning)
            if "max_tokens" not in upstream and self.server.config.get("max_output") is not None:
                upstream["max_tokens"] = self.server.config["max_output"]
        except (ValueError, TypeError, KeyError, IndexError):
            # BadRequest messages are controlled strings, never body excerpts.
            exc = sys.exc_info()[1]
            self.error_json(400, str(exc) if isinstance(exc, BadRequest) else "Invalid JSON request.")
            return
        try:
            with requests.Session() as session:
                if self.server.config.get("proxy") == "never":
                    session.trust_env = False
                headers = {"Content-Type": "application/json", "Accept": "text/event-stream" if upstream["stream"] else "application/json"}
                if self.server.config.get("api_key"):
                    headers["Authorization"] = "Bearer " + self.server.config["api_key"]
                # No retry adapter and no redirects: a generation POST is never replayed.
                with metered_post(self.server, session, self.server.config["url"], json=upstream, headers=headers, stream=upstream["stream"], timeout=(15, self.server.config["timeout"]), allow_redirects=False) as result:
                    if result.status_code != 200:
                        raw = result.content
                        self.send_response(result.status_code)
                        self.send_header("Content-Type", result.headers.get("Content-Type", "application/json"))
                        self.send_header("Content-Length", str(len(raw)))
                        self.end_headers()
                        self.wfile.write(raw)
                        return
                    if upstream["stream"]:
                        self.stream_result(result, tool_types)
                    else:
                        self.nonstream_result(result.json(), tool_types)
        except (BrokenPipeError, ConnectionResetError):
            self.close_connection = True
        except requests.RequestException:
            self.error_json(502, "Upstream connection failed or timed out; the POST was not retried.", "upstream_connection_error")
        except (ValueError, TypeError, KeyError):
            self.error_json(502, "Upstream returned an invalid Chat Completions response.", "upstream_protocol_error")

    def nonstream_result(self, result: dict, tool_types: dict) -> None:
        choice = result["choices"][0]
        finish = choice.get("finish_reason")
        if finish not in ("stop", "tool_calls", "length", "content_filter"):
            raise ValueError("Missing or unsupported finish reason.")
        status = "incomplete" if finish in ("length", "content_filter") else "completed"
        message = choice["message"]
        output = []
        reasoning = message.get("reasoning_content") or ""
        if reasoning:
            output.append({"id": new_id("rs_"), "type": "reasoning", "summary": [{"type": "summary_text", "text": reasoning}], "content": [{"type": "reasoning_text", "text": reasoning}], "status": status})
        if message.get("content"):
            output.append({"id": new_id("msg_"), "type": "message", "status": status, "role": "assistant", "content": [{"type": "output_text", "text": message["content"], "annotations": []}]})
        if message.get("refusal"):
            output.append({"id": new_id("msg_"), "type": "message", "status": status, "role": "assistant", "content": [{"type": "refusal", "refusal": message["refusal"]}]})
        for call in message.get("tool_calls") or []:
            self.server.remember_reasoning(call.get("id"), reasoning)
            output.append(tool_item({"call_id": call.get("id"), "name": call["function"].get("name"), "arguments": call["function"].get("arguments", "")}, tool_types, status))
        incomplete = {"reason": "max_output_tokens" if finish == "length" else "content_filter"} if status == "incomplete" else None
        self.send_json(200, response_object(new_id("resp_"), self.server.config["name"], int(time.time()), status, output, convert_usage(result.get("usage")), incomplete=incomplete))

    def stream_result(self, upstream: requests.Response, tool_types: dict) -> None:
        self.send_response(200)
        self.send_header("Content-Type", "text/event-stream; charset=utf-8")
        self.send_header("Cache-Control", "no-cache")
        self.send_header("Connection", "close")
        self.send_header("X-Accel-Buffering", "no")
        self.end_headers()
        self.close_connection = True
        sequence = 0
        response_id = new_id("resp_")
        created = int(time.time())
        output = []
        message = None
        reasoning_item = None
        message_index = None
        reasoning_index = None
        usage = None
        finish = None
        calls = {}
        events = queue.Queue(maxsize=256)
        stop = threading.Event()

        def emit(kind: str, **payload: Any) -> None:
            nonlocal sequence
            payload = {"type": kind, "sequence_number": sequence, **payload}
            sequence += 1
            raw = "event: " + kind + "\ndata: " + json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + "\n\n"
            self.wfile.write(raw.encode("utf-8"))
            self.wfile.flush()

        def put_event(event: tuple) -> None:
            while not stop.is_set():
                try:
                    events.put(event, timeout=0.5)
                    return
                except queue.Full:
                    pass

        def read_stream() -> None:
            try:
                # chunk_size=1 avoids requests buffering a quiet, small SSE stream.
                data_lines = []
                for raw in upstream.iter_lines(chunk_size=1, decode_unicode=False):
                    if stop.is_set():
                        return
                    line = raw.decode("utf-8")
                    if not line:
                        if data_lines:
                            data = "\n".join(data_lines)
                            data_lines = []
                            if data == "[DONE]":
                                put_event(("end", None))
                                return
                            put_event(("data", json.loads(data)))
                    elif line.startswith("data:"):
                        data_lines.append(line[5:].lstrip(" "))
                if data_lines:
                    data = "\n".join(data_lines)
                    if data == "[DONE]":
                        put_event(("end", None))
                        return
                    put_event(("data", json.loads(data)))
                put_event(("end_of_file", None))
            except Exception:
                put_event(("error", None))

        try:
            initial = response_object(response_id, self.server.config["name"], created, "in_progress", [])
            emit("response.created", response=initial)
            emit("response.in_progress", response=initial)
            threading.Thread(target=read_stream, name="upstream-sse-reader", daemon=True).start()
            while True:
                try:
                    kind, payload = events.get(timeout=10)
                except queue.Empty:
                    self.wfile.write(b": keepalive\n\n")
                    self.wfile.flush()
                    continue
                if kind in ("error", "end_of_file"):
                    raise ValueError("Upstream stream failed or was interrupted.")
                if kind == "end":
                    break
                if payload.get("error"):
                    raise ValueError("Upstream sent an error inside the stream.")
                if payload.get("usage"):
                    usage = convert_usage(payload["usage"])
                choices = payload.get("choices") or []
                if not choices:
                    continue
                choice = choices[0]
                if choice.get("finish_reason") is not None:
                    finish = choice["finish_reason"]
                delta = choice.get("delta") or {}
                if delta.get("refusal"):
                    raise ValueError("Upstream refused this generation.")
                if delta.get("reasoning_content"):
                    if not isinstance(delta["reasoning_content"], str):
                        raise ValueError("Unexpected upstream reasoning type.")
                    if reasoning_item is None:
                        reasoning_index = len(output)
                        reasoning_item = {"id": new_id("rs_"), "type": "reasoning", "summary": [], "status": "in_progress"}
                        output.append(reasoning_item)
                        emit("response.output_item.added", output_index=reasoning_index, item=dict(reasoning_item))
                        part = {"type": "summary_text", "text": ""}
                        reasoning_item["summary"].append(part)
                        emit("response.reasoning_summary_part.added", item_id=reasoning_item["id"], output_index=reasoning_index, summary_index=0, part=dict(part))
                    reasoning_item["summary"][0]["text"] += delta["reasoning_content"]
                    emit("response.reasoning_summary_text.delta", item_id=reasoning_item["id"], output_index=reasoning_index, summary_index=0, delta=delta["reasoning_content"])
                if delta.get("content"):
                    if not isinstance(delta["content"], str):
                        raise ValueError("Unexpected upstream content type.")
                    if message is None:
                        message_index = len(output)
                        message = {"id": new_id("msg_"), "type": "message", "status": "in_progress", "role": "assistant", "content": []}
                        output.append(message)
                        emit("response.output_item.added", output_index=message_index, item=dict(message))
                        part = {"type": "output_text", "text": "", "annotations": []}
                        message["content"].append(part)
                        emit("response.content_part.added", item_id=message["id"], output_index=message_index, content_index=0, part=dict(part))
                    message["content"][0]["text"] += delta["content"]
                    emit("response.output_text.delta", item_id=message["id"], output_index=message_index, content_index=0, delta=delta["content"], logprobs=[])
                for part in delta.get("tool_calls") or []:
                    index = part.get("index", 0)
                    call = calls.setdefault(index, {"call_id": "", "name": "", "arguments": ""})
                    if part.get("id"):
                        call["call_id"] = part["id"]
                    fn = part.get("function") or {}
                    call["name"] += fn.get("name") or ""
                    call["arguments"] += fn.get("arguments") or ""
            if finish not in ("stop", "tool_calls", "length", "content_filter"):
                raise ValueError("Upstream stream ended without a supported finish reason.")
            status = "incomplete" if finish in ("length", "content_filter") else "completed"
            # Validate all complete custom wrappers before emitting tool completion.
            converted_calls = [tool_item(calls[index], tool_types, status) for index in sorted(calls)]
            if reasoning_item is not None:
                reasoning_item["status"] = status
                part = reasoning_item["summary"][0]
                # Preserve the provider's raw reasoning for full-history replay;
                # the summary events also make it visible to existing clients.
                reasoning_item["content"] = [{"type": "reasoning_text", "text": part["text"]}]
                emit("response.reasoning_summary_text.done", item_id=reasoning_item["id"], output_index=reasoning_index, summary_index=0, text=part["text"])
                emit("response.reasoning_summary_part.done", item_id=reasoning_item["id"], output_index=reasoning_index, summary_index=0, part=part)
                emit("response.output_item.done", output_index=reasoning_index, item=reasoning_item)
                for item in converted_calls:
                    self.server.remember_reasoning(item["call_id"], part["text"])
            if message is not None:
                message["status"] = status
                part = message["content"][0]
                emit("response.output_text.done", item_id=message["id"], output_index=message_index, content_index=0, text=part["text"], logprobs=[])
                emit("response.content_part.done", item_id=message["id"], output_index=message_index, content_index=0, part=part)
                emit("response.output_item.done", output_index=message_index, item=message)
            for item in converted_calls:
                index = len(output)
                output.append(item)
                custom = item["type"] == "custom_tool_call"
                field = "input" if custom else "arguments"
                added = dict(item, status="in_progress", **{field: ""})
                emit("response.output_item.added", output_index=index, item=added)
                event = "response.custom_tool_call_input" if custom else "response.function_call_arguments"
                emit(event + ".delta", item_id=item["id"], output_index=index, delta=item[field])
                emit(event + ".done", item_id=item["id"], output_index=index, **{field: item[field]})
                emit("response.output_item.done", output_index=index, item=item)
            incomplete = {"reason": "max_output_tokens" if finish == "length" else "content_filter"} if status == "incomplete" else None
            final = response_object(response_id, self.server.config["name"], created, status, output, usage, incomplete=incomplete)
            emit("response.incomplete" if status == "incomplete" else "response.completed", response=final)
        except (BrokenPipeError, ConnectionResetError):
            pass
        except Exception:
            error = {"code": "upstream_protocol_error", "message": "Upstream stream failed, was interrupted, or contained invalid tool arguments; the POST was not retried."}
            try:
                emit("response.failed", response=response_object(response_id, self.server.config["name"], created, "failed", output, usage, error=error))
            except (BrokenPipeError, ConnectionResetError):
                pass
        finally:
            stop.set()
            upstream.close()


def responses_main(argv=None) -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--catalog", type=Path,
                        default=Path(__file__).resolve().parents[1] / "configs" / "op_catalog.json")
    parser.add_argument("--model", default="glm-5.3-bigmodel", help="Upstream catalog model key.")
    parser.add_argument("--upstream", default=None,
                        help="OpenAI-compatible base URL; when set, --catalog/--model are not read.")
    parser.add_argument("--name", default=None, help="Model name returned to the client (direct mode).")
    parser.add_argument("--api-key", default=os.environ.get("PROXY_API_KEY") or os.environ.get("MODEL_PROXY_API_KEY", ""))
    parser.add_argument("--max-output", type=int, default=None)
    parser.add_argument("--host", default="127.0.0.1")
    parser.add_argument("--port", type=int, default=18008)
    parser.add_argument("--timeout", type=float, default=900, help="Upstream read timeout in seconds.")
    args = parser.parse_args(argv)
    try:
        if args.upstream:
            base = args.upstream.rstrip("/")
            config = {"name": args.name or args.model, "api_key": args.api_key, "proxy": "never",
                      "url": base + "/chat/completions", "timeout": args.timeout,
                      "max_output": args.max_output}
        else:
            catalog = json.loads(args.catalog.read_text(encoding="utf-8"))
            selected = catalog["models"][args.model]
            base = selected["openai_base"].rstrip("/")
            config = {"name": selected["name"], "api_key": selected.get("api_key", ""), "proxy": selected.get("proxy"), "url": base + "/chat/completions", "timeout": args.timeout, "max_output": selected.get("max_output")}
        if urlsplit(base).scheme not in ("http", "https"):
            raise ValueError("Invalid upstream URL scheme.")
    except (OSError, ValueError, KeyError, TypeError):
        parser.error("Cannot load the requested model configuration from the catalog.")
    server = BridgeServer((args.host, args.port), config)
    start_parent_watch()
    print("Responses bridge listening on %s:%d; model=%s; stateless full-history requests only" % (args.host, args.port, config["name"]), file=sys.stderr, flush=True)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.server_close()


def _id(prefix: str) -> str:
    return prefix + uuid.uuid4().hex


def _text_of(content: Any) -> str:
    if isinstance(content, str):
        return content
    if content is None:
        return ""
    if isinstance(content, list):
        parts = []
        for block in content:
            if isinstance(block, str):
                parts.append(block)
            elif isinstance(block, dict) and block.get("type") == "text":
                parts.append(str(block.get("text", "")))
            elif isinstance(block, dict) and block.get("type") == "tool_result":
                parts.append(_text_of(block.get("content")))
        return "\n".join(p for p in parts if p)
    return json.dumps(content, ensure_ascii=False)


def _image_part(block: dict) -> dict | None:
    source = block.get("source") or {}
    if source.get("type") == "base64":
        media = source.get("media_type") or "image/png"
        data = source.get("data") or ""
        return {"type": "image_url", "image_url": {"url": f"data:{media};base64,{data}"}}
    if source.get("type") == "url" and source.get("url"):
        return {"type": "image_url", "image_url": {"url": source["url"]}}
    return None


def _user_message(content: Any) -> list[dict]:
    """Convert one Anthropic user message into OpenAI messages.

    tool_result blocks become role=tool messages; text/image blocks become a
    following role=user message.  Order matters: OpenAI requires tool results
    immediately after the assistant tool_calls message.
    """
    if isinstance(content, str):
        return [{"role": "user", "content": content}]
    if not isinstance(content, list):
        return [{"role": "user", "content": _text_of(content)}]

    tool_messages: list[dict] = []
    user_parts: list[dict] = []
    for block in content:
        if not isinstance(block, dict):
            user_parts.append({"type": "text", "text": str(block)})
            continue
        kind = block.get("type")
        if kind == "tool_result":
            text = _text_of(block.get("content"))
            if block.get("is_error"):
                text = "Error: " + text
            tool_messages.append({
                "role": "tool",
                "tool_call_id": block.get("tool_use_id") or "",
                "content": text,
            })
        elif kind == "text":
            user_parts.append({"type": "text", "text": str(block.get("text", ""))})
        elif kind == "image":
            part = _image_part(block)
            if part:
                user_parts.append(part)
        # thinking / redacted_thinking and other beta blocks are ignored.
    messages = tool_messages
    if user_parts:
        if len(user_parts) == 1 and user_parts[0].get("type") == "text":
            messages.append({"role": "user", "content": user_parts[0]["text"]})
        else:
            messages.append({"role": "user", "content": user_parts})
    return messages


def _assistant_message(content: Any) -> dict:
    if isinstance(content, str):
        return {"role": "assistant", "content": content}
    if not isinstance(content, list):
        return {"role": "assistant", "content": _text_of(content)}

    texts: list[str] = []
    tool_calls: list[dict] = []
    for block in content:
        if not isinstance(block, dict):
            texts.append(str(block))
        elif block.get("type") == "text":
            texts.append(str(block.get("text", "")))
        elif block.get("type") == "tool_use":
            tool_calls.append({
                "id": block.get("id") or _id("call_"),
                "type": "function",
                "function": {
                    "name": block.get("name") or "",
                    "arguments": json.dumps(block.get("input") or {}, ensure_ascii=False),
                },
            })
    message: dict[str, Any] = {"role": "assistant"}
    text = "\n".join(t for t in texts if t)
    if text:
        message["content"] = text
    else:
        message["content"] = None
    if tool_calls:
        message["tool_calls"] = tool_calls
    return message


def anthropic_convert_request(body: dict, max_tokens_cap: int | None) -> dict:
    messages: list[dict] = []
    system = body.get("system")
    if system:
        messages.append({"role": "system", "content": _text_of(system)})

    for item in body.get("messages") or []:
        if not isinstance(item, dict):
            continue
        role = item.get("role")
        content = item.get("content")
        if role == "assistant":
            messages.append(_assistant_message(content))
        elif role in ("user", "system"):
            messages.extend(_user_message(content) if role == "user" else
                            [{"role": "system", "content": _text_of(content)}])
        else:
            messages.append({"role": role or "user", "content": _text_of(content)})

    upstream: dict[str, Any] = {
        "model": body.get("model"),
        "messages": messages,
        "stream": bool(body.get("stream")),
    }
    max_tokens = body.get("max_tokens")
    if max_tokens_cap:
        max_tokens = min(int(max_tokens or max_tokens_cap), max_tokens_cap)
    if max_tokens:
        upstream["max_tokens"] = max_tokens
    for key in ("temperature", "top_p", "stop", "seed"):
        if body.get(key) is not None:
            upstream[key] = body[key]

    tools = []
    for tool in body.get("tools") or []:
        if not isinstance(tool, dict):
            continue
        name = tool.get("name")
        if not name:
            continue
        tools.append({
            "type": "function",
            "function": {
                "name": name,
                "description": tool.get("description") or "",
                "parameters": tool.get("input_schema") or {"type": "object", "properties": {}},
            },
        })
    if tools:
        upstream["tools"] = tools
        choice = body.get("tool_choice")
        if isinstance(choice, dict):
            kind = choice.get("type")
            if kind == "tool" and choice.get("name"):
                upstream["tool_choice"] = {
                    "type": "function",
                    "function": {"name": choice["name"]},
                }
            elif kind == "any":
                upstream["tool_choice"] = "required"
            elif kind == "none":
                upstream["tool_choice"] = "none"
            else:
                upstream["tool_choice"] = "auto"
        elif choice:
            upstream["tool_choice"] = choice

    if upstream["stream"]:
        upstream["stream_options"] = {"include_usage": True}
    return upstream


def _usage(usage: dict | None, fallback_output: int = 0) -> dict:
    usage = usage or {}
    prompt = int(usage.get("prompt_tokens") or 0)
    completion = int(usage.get("completion_tokens") or fallback_output)
    return {"input_tokens": prompt, "output_tokens": completion}


def _stop_reason(finish: str | None, has_tool: bool) -> str:
    if has_tool or finish == "tool_calls":
        return "tool_use"
    if finish == "length":
        return "max_tokens"
    if finish == "content_filter":
        return "end_turn"
    return "end_turn"


def anthropic_convert_response(result: dict, model: str) -> dict:
    choice = (result.get("choices") or [{}])[0]
    message = choice.get("message") or {}
    content: list[dict] = []
    text = message.get("content")
    if text:
        content.append({"type": "text", "text": text})
    for call in message.get("tool_calls") or []:
        raw = (call.get("function") or {}).get("arguments") or "{}"
        try:
            parsed = json.loads(raw)
        except (TypeError, ValueError):
            parsed = {"raw": raw}
        content.append({
            "type": "tool_use",
            "id": call.get("id") or _id("toolu_"),
            "name": (call.get("function") or {}).get("name") or "",
            "input": parsed if isinstance(parsed, dict) else {"value": parsed},
        })
    if not content:
        content.append({"type": "text", "text": ""})
    return {
        "id": _id("msg_"),
        "type": "message",
        "role": "assistant",
        "model": model,
        "content": content,
        "stop_reason": _stop_reason(choice.get("finish_reason"), bool(message.get("tool_calls"))),
        "stop_sequence": None,
        "usage": _usage(result.get("usage")),
    }


class Handler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    server_version = "anthropic-proxy/1.0"

    @property
    def cfg(self) -> argparse.Namespace:
        return self.server.cfg  # type: ignore[attr-defined]

    def log_message(self, fmt: str, *args: Any) -> None:
        sys.stderr.write("[%s] %s\n" % (time.strftime("%H:%M:%S"), fmt % args))

    def _json(self, status: int, payload: dict) -> None:
        raw = json.dumps(payload, ensure_ascii=False).encode()
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(raw)))
        self.end_headers()
        self.wfile.write(raw)

    def _error(self, status: int, kind: str, message: str) -> None:
        self._json(status, {"type": "error", "error": {"type": kind, "message": message}})

    def do_GET(self) -> None:
        if usage_endpoint(self):
            return
        route = urlsplit(self.path).path.rstrip("/")
        if route in ("/health", "/v1/health"):
            self._json(200, {"status": "ok", "upstream": self.cfg.upstream})
        elif route in ("/models", "/v1/models"):
            self._json(200, {
                "object": "list",
                "data": [{"id": self.cfg.model or "model", "object": "model", "owned_by": "proxy"}],
            })
        else:
            self._error(404, "not_found_error", "Unknown route.")

    def do_POST(self) -> None:
        if usage_endpoint(self):
            return
        if urlsplit(self.path).path.rstrip("/") not in ("/v1/messages", "/messages"):
            self._error(404, "not_found_error", "Only /v1/messages is supported.")
            return
        try:
            size = int(self.headers.get("Content-Length", "0"))
            if size <= 0 or size > 100 * 1024 * 1024:
                raise ValueError("A JSON request body of at most 100 MiB is required.")
            body = json.loads(self.rfile.read(size))
            if not isinstance(body, dict):
                raise ValueError("The request must be a JSON object.")
            upstream_body = anthropic_convert_request(body, self.cfg.max_tokens)
        except (ValueError, TypeError, KeyError) as exc:
            self._error(400, "invalid_request_error", str(exc) or "Invalid request.")
            return

        url = self.cfg.upstream.rstrip("/") + "/chat/completions"
        headers = {"Content-Type": "application/json"}
        if self.cfg.api_key:
            headers["Authorization"] = "Bearer " + self.cfg.api_key
        try:
            with requests.Session() as session:
                session.trust_env = False
                with metered_post(self.server, session,
                    url, json=upstream_body, headers=headers,
                    stream=upstream_body["stream"],
                    timeout=(15, self.cfg.timeout), allow_redirects=False,
                ) as result:
                    if result.status_code != 200:
                        raw = result.content[:2000]
                        try:
                            detail = json.loads(raw).get("error", raw.decode("utf-8", "replace"))
                        except Exception:
                            detail = raw.decode("utf-8", "replace")
                        self._error(502, "upstream_error", str(detail))
                        return
                    if upstream_body["stream"]:
                        self._stream(result, body.get("model") or "")
                    else:
                        self._json(200, anthropic_convert_response(result.json(), body.get("model") or ""))
        except requests.RequestException as exc:
            self._error(502, "upstream_error", f"Upstream connection failed: {exc.__class__.__name__}")

    def _stream(self, result: requests.Response, model: str) -> None:
        self.send_response(200)
        self.send_header("Content-Type", "text/event-stream; charset=utf-8")
        self.send_header("Cache-Control", "no-cache")
        self.send_header("Connection", "close")
        self.send_header("X-Accel-Buffering", "no")
        self.end_headers()
        self.close_connection = True

        def event(kind: str, payload: dict) -> None:
            data = json.dumps(payload, ensure_ascii=False)
            self.wfile.write(f"event: {kind}\ndata: {data}\n\n".encode())
            self.wfile.flush()

        started = False
        text_started = False
        text_index = 0
        next_index = 1
        calls: dict[int, dict] = {}
        finish: str | None = None
        usage: dict | None = None

        def start_message(prompt_tokens: int = 0) -> None:
            nonlocal started
            if started:
                return
            started = True
            event("message_start", {
                "type": "message_start",
                "message": {
                    "id": _id("msg_"),
                    "type": "message",
                    "role": "assistant",
                    "model": model,
                    "content": [],
                    "stop_reason": None,
                    "stop_sequence": None,
                    "usage": {"input_tokens": prompt_tokens, "output_tokens": 0},
                },
            })

        try:
            for line in result.iter_lines(decode_unicode=True):
                if not line or not line.startswith("data:"):
                    continue
                data = line[5:].strip()
                if data == "[DONE]":
                    break
                chunk = json.loads(data)
                if chunk.get("usage"):
                    usage = chunk["usage"]
                choices = chunk.get("choices") or []
                if not choices:
                    continue
                choice = choices[0]
                delta = choice.get("delta") or {}
                if not started:
                    start_message(int((usage or {}).get("prompt_tokens") or 0))
                text = delta.get("content")
                if text:
                    if not text_started:
                        text_started = True
                        event("content_block_start", {
                            "type": "content_block_start",
                            "index": text_index,
                            "content_block": {"type": "text", "text": ""},
                        })
                    event("content_block_delta", {
                        "type": "content_block_delta",
                        "index": text_index,
                        "delta": {"type": "text_delta", "text": text},
                    })
                for call in delta.get("tool_calls") or []:
                    index = int(call.get("index") or 0)
                    slot = calls.setdefault(index, {"id": "", "name": "", "arguments": ""})
                    if call.get("id"):
                        slot["id"] = call["id"]
                    function = call.get("function") or {}
                    if function.get("name"):
                        slot["name"] = function["name"]
                    if function.get("arguments"):
                        slot["arguments"] += function["arguments"]
                if choice.get("finish_reason"):
                    finish = choice["finish_reason"]

            if not started:
                start_message(int((usage or {}).get("prompt_tokens") or 0))
            if text_started:
                event("content_block_stop", {"type": "content_block_stop", "index": text_index})
            for _, slot in sorted(calls.items()):
                event("content_block_start", {
                    "type": "content_block_start",
                    "index": next_index,
                    "content_block": {
                        "type": "tool_use",
                        "id": slot["id"] or _id("toolu_"),
                        "name": slot["name"],
                        "input": {},
                    },
                })
                event("content_block_delta", {
                    "type": "content_block_delta",
                    "index": next_index,
                    "delta": {"type": "input_json_delta", "partial_json": slot["arguments"] or "{}"},
                })
                event("content_block_stop", {"type": "content_block_stop", "index": next_index})
                next_index += 1
            event("message_delta", {
                "type": "message_delta",
                "delta": {"stop_reason": _stop_reason(finish, bool(calls)), "stop_sequence": None},
                "usage": _usage(usage),
            })
            event("message_stop", {"type": "message_stop"})
        except (BrokenPipeError, ConnectionResetError):
            self.close_connection = True
        except Exception as exc:  # noqa: BLE001 - report a protocol error to the client
            try:
                event("error", {
                    "type": "error",
                    "error": {"type": "api_error", "message": f"Proxy stream error: {exc.__class__.__name__}"},
                })
            except (BrokenPipeError, ConnectionResetError):
                self.close_connection = True


def anthropic_main(argv=None) -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--listen", "--host", dest="listen", default="127.0.0.1")
    parser.add_argument("--port", type=int, default=18008)
    parser.add_argument("--upstream", required=True, help="OpenAI-compatible base, e.g. http://host:8008/v1")
    parser.add_argument("--api-key", default=os.environ.get("PROXY_API_KEY") or os.environ.get("ANTHROPIC_PROXY_API_KEY", ""))
    parser.add_argument("--max-tokens", type=int, default=16384,
                        help="Clamp Anthropic max_tokens to this value (0 disables).")
    parser.add_argument("--timeout", type=int, default=3600)
    parser.add_argument("--model", default="", help="Optional model id returned by GET /v1/models.")
    args = parser.parse_args(argv)

    server = ThreadingHTTPServer((args.listen, args.port), Handler)
    server.cfg = args  # type: ignore[attr-defined]
    server.usage_meter = TransportMeter()
    start_parent_watch()
    print(f"anthropic proxy listening on http://{args.listen}:{args.port} -> {args.upstream}", flush=True)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.server_close()

def main() -> None:
    """Select the protocol implementation and start one local server."""
    if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"):
        print("usage: proxy.py --protocol {anthropic,responses,passthrough} [options]")
        print("  --protocol anthropic   expose POST /v1/messages")
        print("  --protocol responses   expose POST /v1/responses")
        print("  --protocol passthrough preserve native HTTP protocol and observe provider usage")
        print("Run with --protocol <name> --help for protocol-specific options.")
        return
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("--protocol", choices=("anthropic", "responses", "passthrough"), required=True)
    parser.add_argument("--parent-pid", type=int, default=0)
    args, rest = parser.parse_known_args()
    if args.parent_pid > 0:
        os.environ["PROXY_PARENT_PID"] = str(args.parent_pid)
    if args.protocol == "passthrough":
        passthrough_main(rest)
    elif args.protocol == "anthropic":
        anthropic_main(rest)
    else:
        responses_main(rest)


if __name__ == "__main__":
    main()