Repository navigation
Expand file tree
/
Copy pathconverter.py
More file actions
962 lines (816 loc) · 40.8 KB
/
Copy pathconverter.py
File metadata and controls
962 lines (816 loc) · 40.8 KB
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
#!/usr/bin/env python3
"""
codebuddy2openai — 把 CodeBuddy / WorkBuddy 的订阅暴露成标准 OpenAI 兼容 API。
原理(直连后端,原生 function calling):
- 读取本机已登录的 CodeBuddy 桌面端凭据(auth 文件里的 token / uid / enterpriseId)。
- 直接转发到 CodeBuddy 后端 `https://copilot.tencent.com/v2/chat/completions`。
该后端本身就是标准 OpenAI chat/completions 协议(含原生 tools / tool_calls / SSE 流式)。
- 转换器只做两件事:①注入鉴权 header(Authorization / X-User-Id 等)
②在本地 /v1/* 与后端 /v2/* 之间做路径映射与透传(含 Anthropic / Chat / Responses 三种协议)。
- token 过期时自动调 `/v2/plugin/auth/token/refresh` 刷新,并回写 auth 文件。
跨平台:自动定位 auth 目录(macOS / Windows / Linux)。
依赖:fastapi + uvicorn + httpx(pip install fastapi "uvicorn[standard]" httpx)。
用法:
python3 converter.py # 默认 127.0.0.1:8787
python3 converter.py --port 9000
python3 converter.py --api-key mysecret # 启用客户端鉴权
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import threading
import time
from pathlib import Path
from typing import Optional
import httpx
from fastapi import FastAPI, Header, HTTPException, Request
from fastapi.responses import JSONResponse, StreamingResponse
import uvicorn
try:
from desensitize import desensitize_body
except ImportError: # 模块缺失时降级为不脱敏
def desensitize_body(body, roles=("system",), desensitize_harness_user=False,
desensitize_tools=False, compact_harness=False,
strip_tool_metadata=False):
return body
from responses_adapter import (
responses_request_to_chat,
ResponsesStreamConverter,
)
from responses_projection import project_responses_chat_body
from anthropic_adapter import (
anthropic_request_to_chat,
AnthropicStreamConverter,
)
# ---------------------------------------------------------------------------
# 常量
# ---------------------------------------------------------------------------
BACKEND = "https://copilot.tencent.com"
DEFAULT_DOMAIN = "www.codebuddy.cn"
USER_AGENT = "codebuddy2openai/2.0"
# ---------------------------------------------------------------------------
# 平台相关:定位 auth 目录
# ---------------------------------------------------------------------------
def auth_dirs() -> list[Path]:
env_dir = os.environ.get("CODEBUDDY_AUTH_DIR")
if env_dir:
return [Path(env_dir)]
home = Path.home()
plat = sys.platform
if plat == "darwin":
return [home / "Library" / "Application Support" / "CodeBuddyExtension" / "Data" / "Public" / "auth"]
if plat == "win32":
local = Path(os.environ.get("LOCALAPPDATA", home / "AppData" / "Local"))
return [local / "CodeBuddyExtension" / "Data" / "Public" / "auth"]
xdg = Path(os.environ.get("XDG_DATA_HOME", home / ".local" / "share"))
return [xdg / "CodeBuddyExtension" / "Data" / "Public" / "auth"]
def find_auth_file(explicit_file: str | None = None) -> Path | None:
# 1. 命令行显式指定优先
if explicit_file:
p = Path(explicit_file).expanduser()
if p.is_file():
return p
return None
# 2. 环境变量显式指定
env_file = os.environ.get("CODEBUDDY_AUTH_FILE")
if env_file:
p = Path(env_file).expanduser()
if p.is_file():
return p
return None
# 3. 兼容旧逻辑:从目录里找第一个 *.info
for d in auth_dirs():
d = d.expanduser()
if d.is_file() and d.suffix == ".info":
return d
if d.is_dir():
for f in sorted(d.glob("*.info")):
return f
return None
# ---------------------------------------------------------------------------
# Auth 凭据管理(读 + 自动刷新 + 回写)
# ---------------------------------------------------------------------------
class CredentialManager:
"""从 auth 文件读取凭据;token 临近过期时自动刷新并回写。"""
def __init__(self, path: Path):
self.path = path
self._lock = threading.Lock()
self._cached: dict | None = None
self._mtime: float = 0.0
def _read_raw(self) -> dict:
with open(self.path, "r", encoding="utf-8") as f:
return json.load(f)
def _load_if_stale(self):
"""若文件 mtime 变了(外部刷新过),重新加载缓存。"""
try:
mt = self.path.stat().st_mtime
except OSError:
return
if self._cached is None or mt != self._mtime:
self._cached = self._read_raw()
self._mtime = mt
def _session(self) -> dict:
self._load_if_stale()
if self._cached is None:
raise RuntimeError(f"无法读取 auth 文件:{self.path}")
return self._cached
def _is_expired(self) -> bool:
s = self._session()
expires_at = (s.get("auth") or {}).get("expiresAt") or 0
# 提前 60s 判定过期
return time.time() * 1000 >= (expires_at - 60_000)
def _refresh(self):
"""调后端刷新 token,写回 auth 文件与缓存。"""
s = self._session()
auth = s.get("auth") or {}
headers = self._build_headers_from(auth, s.get("account") or {})
headers["X-Refresh-Token"] = auth.get("refreshToken", "")
headers["X-Auth-Refresh-Source"] = "plugin"
url = f"{BACKEND}/v2/plugin/auth/token/refresh"
try:
with httpx.Client(timeout=15) as c:
r = c.post(url, headers=headers, json={})
data = r.json()
except Exception as e:
raise RuntimeError(f"刷新 token 网络失败:{e}")
if data.get("code") != 0 or not data.get("data"):
raise RuntimeError(f"刷新 token 失败:{data.get('msg', data)}")
new_auth = data["data"]
# 继承部分字段
new_auth["domain"] = new_auth.get("domain") or auth.get("domain")
new_auth["lastRefreshTime"] = int(time.time() * 1000)
# 计算 expiresAt(若后端没直接给)
if not new_auth.get("expiresAt") and new_auth.get("expiresIn"):
new_auth["expiresAt"] = int(time.time() * 1000) + new_auth["expiresIn"] * 1000
if not new_auth.get("refreshExpiresAt") and new_auth.get("refreshExpiresIn"):
new_auth["refreshExpiresAt"] = int(time.time() * 1000) + new_auth["refreshExpiresIn"] * 1000
s["auth"] = new_auth
# 原子写回
tmp = self.path.with_suffix(self.path.suffix + ".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(s, f, ensure_ascii=False, indent=2)
os.replace(tmp, self.path)
self._cached = s
self._mtime = self.path.stat().st_mtime
def _build_headers_from(self, auth: dict, account: dict) -> dict:
domain = auth.get("domain") or DEFAULT_DOMAIN
h = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {auth.get('accessToken','')}",
"X-User-Id": account.get("uid", ""),
"X-Enterprise-Id": account.get("enterpriseId", ""),
"X-Tenant-Id": account.get("enterpriseId", ""),
"X-Domain": domain,
"User-Agent": USER_AGENT,
}
return h
def get_headers(self) -> dict:
"""返回带最新 token 的后端请求 header;必要时先刷新。"""
with self._lock:
if self._is_expired():
self._refresh()
s = self._session()
return self._build_headers_from(s.get("auth") or {}, s.get("account") or {})
def summary(self) -> dict:
s = self._session()
auth = s.get("auth") or {}
acct = s.get("account") or {}
exp = auth.get("expiresAt", 0)
return {
"uid": acct.get("uid"),
"nickname": acct.get("nickname"),
"enterpriseName": acct.get("enterpriseName"),
"token_expires_at": exp,
"token_expired": self._is_expired(),
}
# ---------------------------------------------------------------------------
# 模型列表
# ---------------------------------------------------------------------------
DEFAULT_MODELS = [
"glm-5.2", "glm-5.1", "glm-5v-turbo",
"kimi-k2.7", "kimi-k2.6", "kimi-k2.5",
"deepseek-v4-pro", "deepseek-v4-flash",
"minimax-m3-pay", "hy3-preview-agent", "auto",
]
# 后端请求体里出现过的额外字段(透传时若客户端给了就保留)
PASSTHROUGH_BODY_KEYS = {
"model", "messages", "tools", "tool_choice", "temperature",
"max_tokens", "max_completion_tokens", "top_p", "stream",
"stream_options", "stop", "presence_penalty", "frequency_penalty",
"n", "response_format", "seed", "user", "reasoning_effort",
"verbosity", "reasoning_summary",
}
# ---------------------------------------------------------------------------
# FastAPI 应用
# ---------------------------------------------------------------------------
app = FastAPI(title="codebuddy2openai", version="2.0")
CONFIG: dict = {"api_key": "", "cred": None, "log_path": None,
"desensitize": False, "no_compact": False} # cred: CredentialManager | None
# ---------------------------------------------------------------------------
# 日志(写文件)
# ---------------------------------------------------------------------------
_LOG_LOCK = threading.Lock()
def _log(msg: str):
"""写一行日志到 CONFIG['log_path'] 指定的文件(追加,带时间戳)。未设置则丢弃。"""
path = CONFIG.get("log_path")
if not path:
return
line = f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}\n"
try:
with _LOG_LOCK:
with open(path, "a", encoding="utf-8") as f:
f.write(line)
except OSError:
pass # 日志失败不应影响主流程
def _truncate(s: str, n: int = 80) -> str:
s = str(s).replace("\n", " ").strip()
return s[:n] + ("…" if len(s) > n else "")
def _check_auth(authorization: Optional[str], x_api_key: Optional[str]):
key = CONFIG["api_key"]
if not key:
return
token = ""
if authorization and authorization.startswith("Bearer "):
token = authorization[7:].strip()
if not token and x_api_key:
token = x_api_key
if token != key:
raise HTTPException(status_code=401, detail={"error": {"message": "invalid api key", "type": "auth_error"}})
def _cred() -> CredentialManager:
if CONFIG["cred"] is None:
raise HTTPException(status_code=503, detail={"error": {"message": "未找到登录凭据,请先在桌面端登录 CodeBuddy/WorkBuddy", "type": "auth_error"}})
return CONFIG["cred"]
@app.get("/health")
def health():
cred = CONFIG["cred"]
info: dict = {"status": "ok", "platform": sys.platform, "python": sys.version.split()[0],
"auth_file": str(find_auth_file() or "(未找到)"), "mode": "direct-proxy (native function calling)"}
if cred is not None:
try:
info["credential"] = cred.summary()
except Exception as e:
info["credential_error"] = str(e)
return info
@app.get("/v1/models")
def list_models(authorization: Optional[str] = Header(default=None),
x_api_key: Optional[str] = Header(default=None, alias="X-Api-Key")):
_check_auth(authorization, x_api_key)
data = [{"id": m, "object": "model", "created": 1700000000, "owned_by": "codebuddy"}
for m in DEFAULT_MODELS]
return {"object": "list", "data": data}
@app.post("/v1/chat/completions")
async def chat_completions(request: Request,
authorization: Optional[str] = Header(default=None),
x_api_key: Optional[str] = Header(default=None, alias="X-Api-Key")):
_check_auth(authorization, x_api_key)
cred = _cred()
try:
payload = await request.json()
except Exception as e:
raise HTTPException(status_code=400, detail={"error": {"message": f"bad json: {e}", "type": "invalid_request_error"}})
messages = payload.get("messages") or []
if not messages:
raise HTTPException(status_code=400, detail={"error": {"message": "messages is required", "type": "invalid_request_error"}})
# 构造后端 body:只透传已知的合法字段
client_wants_stream = bool(payload.get("stream"))
body = {k: payload[k] for k in PASSTHROUGH_BODY_KEYS if k in payload}
body.setdefault("model", "auto")
# 后端只支持流式:始终以 stream=True 调后端,非流式由转换器聚合
body["stream"] = True
if "stream_options" not in body:
body["stream_options"] = {"include_usage": True}
# 可选:脱敏。缓解客户端合规模板(如 Codex CLI / ZCode 注入的说明文字)被后端误判为敏感词。
# 处理 system / developer 消息、Codex 注入的上下文 user 消息,以及 tools 的 description。
if CONFIG.get("desensitize"):
body = desensitize_body(body, roles=("system", "developer"),
desensitize_harness_user=True,
desensitize_tools=True,
compact_harness=not CONFIG.get("no_compact"),
strip_tool_metadata=True)
# 日志:请求摘要
model_name = payload.get("model", "auto")
tool_names = [t.get("function", {}).get("name") for t in (payload.get("tools") or [])
if isinstance(t, dict)]
last_user = _last_user_text(messages)
rid = os.urandom(4).hex()
_log(f"[{rid}] ▶ REQUEST {model_name} | stream={client_wants_stream} | msgs={len(messages)}"
+ (f" | tools={tool_names}" if tool_names else "")
+ (f" | last_user={_truncate(last_user, 60)!r}" if last_user else ""))
# 完整请求体(发往后端的实际内容;若启用脱敏,这里已是脱敏后)
_log(f"[{rid}] ── REQUEST BODY (发往后端) ──\n{json.dumps(body, ensure_ascii=False, indent=2)}")
headers = cred.get_headers()
url = f"{BACKEND}/v2/chat/completions"
t0 = time.time()
if client_wants_stream:
return StreamingResponse(
_stream_upstream(url, headers, body, model_name, t0, rid),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
# 非流式:后端只支持流式,这里把后端 SSE 聚合成单个 chat.completion 响应
try:
async with httpx.AsyncClient(timeout=300) as c:
async with c.stream("POST", url, headers=headers, json=body) as r:
if r.status_code != 200:
raw = await r.aread()
_log(f"[{rid}] ✗ HTTP {r.status_code} | {model_name} | {_truncate(raw.decode('utf-8','replace'),200)}")
_log(f"[{rid}] ── ERROR BODY ──\n{raw.decode('utf-8','replace')}")
raise HTTPException(status_code=r.status_code, detail=_safe_err_raw(raw, r.status_code))
collected = await _collect_stream(r)
except HTTPException:
raise
except httpx.HTTPError as e:
_log(f"[{rid}] ✗ 网络错误 | {model_name} | {e}")
raise HTTPException(status_code=502, detail={"error": {"message": f"upstream error: {e}", "type": "upstream_error"}})
_log_finish(model_name, t0, collected, rid)
return JSONResponse(content=collected)
def _last_user_text(messages: list) -> str:
"""取最后一条 user 消息的文本,用于日志预览。"""
for m in reversed(messages):
if m.get("role") != "user":
continue
content = m.get("content", "")
if isinstance(content, list):
for blk in content:
if isinstance(blk, dict) and blk.get("type") == "text":
return str(blk.get("text", ""))
return ""
return str(content)
return ""
def _log_finish(model_name: str, t0: float, result: dict, rid: str = ""):
"""记录一次完成的请求:耗时 / finish_reason / usage / 工具调用 / 审核拦截 + 完整响应。"""
elapsed = time.time() - t0
prefix = f"[{rid}] " if rid else ""
choice = (result.get("choices") or [{}])[0]
finish = choice.get("finish_reason")
msg = choice.get("message") or {}
tcs = msg.get("tool_calls") or []
usage = result.get("usage") or {}
tag = ""
if finish == "content-filter":
tag = " ⚠️内容审核拦截"
tc_names = [t.get("function", {}).get("name") for t in tcs]
_log(f"{prefix}◀ RESPONSE {model_name} | {elapsed:.1f}s | finish={finish}{tag}"
+ (f" | tool_calls={tc_names}" if tc_names else "")
+ f" | tokens={usage.get('total_tokens', '?')}")
# 完整响应体
_log(f"{prefix}── RESPONSE BODY ──\n{json.dumps(result, ensure_ascii=False, indent=2)}")
async def _collect_stream(response: httpx.Response) -> dict:
"""消费后端的 OpenAI SSE 流,聚合成单个非流式 chat.completion 对象。
合并所有 chunk 的 delta(content / tool_calls),并取 usage / finish_reason。
"""
content_parts: list[str] = []
# tool_calls: index -> {id, name, arguments(分片拼接)}
tool_calls: dict[int, dict] = {}
model: str | None = None
finish_reason: str | None = None
usage: dict | None = None
async for line in response.aiter_lines():
line = line.strip()
if not line or not line.startswith("data:"):
continue
data = line[5:].strip()
if data == "[DONE]":
break
try:
chunk = json.loads(data)
except json.JSONDecodeError:
continue
model = chunk.get("model") or model
if chunk.get("usage"):
usage = chunk["usage"]
for choice in chunk.get("choices") or []:
if choice.get("finish_reason"):
finish_reason = choice["finish_reason"]
delta = choice.get("delta") or {}
if delta.get("content"):
content_parts.append(delta["content"])
for tc in delta.get("tool_calls") or []:
idx = tc.get("index", 0)
slot = tool_calls.setdefault(idx, {"id": None, "name": None, "arguments": ""})
if tc.get("id"):
slot["id"] = tc["id"]
fn = tc.get("function") or {}
if fn.get("name"):
slot["name"] = fn["name"]
if fn.get("arguments"):
slot["arguments"] += fn["arguments"]
tcs = None
if tool_calls:
tcs = [
{"id": v["id"], "type": "function",
"function": {"name": v["name"], "arguments": v["arguments"]}}
for _, v in sorted(tool_calls.items())
]
finish_reason = finish_reason or "tool_calls"
message = {"role": "assistant", "content": "".join(content_parts) or None}
if tcs:
message["tool_calls"] = tcs
return {
"id": "chatcmpl-" + os.urandom(12).hex(),
"object": "chat.completion",
"created": int(time.time()),
"model": model or "unknown",
"choices": [{"index": 0, "message": message,
"finish_reason": finish_reason or "stop"}],
"usage": usage or {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
}
def _safe_err_raw(raw: bytes, status: int) -> dict:
try:
return json.loads(raw.decode("utf-8", "replace"))
except Exception:
return {"error": {"message": raw.decode("utf-8", "replace")[:500], "type": "upstream_error", "code": status}}
async def _stream_upstream(url: str, headers: dict, body: dict,
model_name: str = "?", t0: float = 0.0, rid: str = ""):
"""把后端 SSE 原样转发给客户端(后端已是标准 OpenAI SSE,含 tool_calls)。
同时轻量解析流,统计 finish_reason / tool_calls / usage 用于日志,不阻塞转发。
完整原始 SSE 累积后落盘到日志(调试用)。
"""
finish_reason = None
tool_names: list[str] = []
usage: dict = {}
saw_filter = False
buf = b""
raw_parts: list[bytes] = [] # 累积完整原始 SSE
prefix = f"[{rid}] " if rid else ""
def _feed(chunk: bytes):
nonlocal finish_reason, saw_filter, buf
# 行缓冲解析:把累计的 chunk 按 data: 行切出来统计
buf += chunk
while b"\n" in buf:
line, buf = buf.split(b"\n", 1)
line = line.strip()
if not line.startswith(b"data:"):
continue
data = line[5:].strip()
if data == b"[DONE]":
continue
try:
obj = json.loads(data)
except Exception:
continue
if obj.get("usage"):
usage.update(obj["usage"])
for ch in obj.get("choices") or []:
if ch.get("finish_reason"):
finish_reason = ch["finish_reason"]
for tc in (ch.get("delta") or {}).get("tool_calls") or []:
nm = (tc.get("function") or {}).get("name")
if nm:
tool_names.append(nm)
# 内容审核拦截常以 content-filter 或特殊中文文案返回
try:
text_repr = data.decode("utf-8", "replace")
except Exception:
text_repr = ""
if "content-filter" in text_repr or "敏感" in text_repr or "审核" in text_repr:
saw_filter = True
try:
async with httpx.AsyncClient(timeout=None) as c:
async with c.stream("POST", url, headers=headers, json=body) as r:
if r.status_code != 200:
err = await r.aread()
_log(f"{prefix}✗ HTTP {r.status_code} | {model_name} | {_truncate(err.decode('utf-8','replace'),200)}")
_log(f"{prefix}── ERROR BODY ──\n{err.decode('utf-8','replace')}")
yield _err_event(err, r.status_code)
return
async for chunk in r.aiter_bytes():
if chunk:
raw_parts.append(chunk)
_feed(chunk)
yield chunk
except httpx.HTTPError as e:
_log(f"{prefix}✗ 网络错误 | {model_name} | {e}")
yield _err_event(str(e).encode(), 502)
# 流结束:输出完成日志
elapsed = time.time() - t0 if t0 else 0
tag = " ⚠️内容审核拦截" if (saw_filter or finish_reason == "content-filter") else ""
_log(f"{prefix}◀ RESPONSE {model_name} | {elapsed:.1f}s | stream finish={finish_reason}{tag}"
+ (f" | tool_calls={tool_names}" if tool_names else "")
+ f" | tokens={usage.get('total_tokens', '?')}")
# 完整原始 SSE(后端返回的全部内容)
_log(f"{prefix}── RESPONSE RAW SSE ──\n{b''.join(raw_parts).decode('utf-8','replace')}")
def _safe_err(r: httpx.Response) -> dict:
try:
return {"error": r.json()}
except Exception:
return {"error": {"message": r.text[:500], "type": "upstream_error", "code": r.status_code}}
def _err_event(msg: bytes, status: int) -> bytes:
# 以 OpenAI SSE 错误 chunk 形式返回
import json as _json, time as _time
chunk = {
"error": {"message": msg.decode("utf-8", "replace")[:500], "type": "upstream_error", "code": status},
}
return f"data: {_json.dumps(chunk, ensure_ascii=False)}\n\n".encode("utf-8")
def _looks_like_content_filter_text(text: str) -> bool:
text = (text or "").lower()
return (
"content-filter" in text
or "content_filter" in text
or "敏感内容" in text
or "内容审核" in text
or "无法响应您的请求" in text
)
def _chat_body_desensitize(body: dict, *, force_compact: bool = False) -> dict:
if not CONFIG.get("desensitize"):
return body
return desensitize_body(
body,
roles=("system", "developer"),
desensitize_harness_user=True,
desensitize_tools=True,
compact_harness=(force_compact or not CONFIG.get("no_compact")),
strip_tool_metadata=True,
)
async def _post_backend_once(url: str, headers: dict, body: dict) -> tuple[int, bytes]:
async with httpx.AsyncClient(timeout=120) as c:
async with c.stream("POST", url, headers=headers, json=body) as r:
chunks: list[bytes] = []
async for chunk in r.aiter_bytes():
if chunk:
chunks.append(chunk)
return r.status_code, b"".join(chunks)
async def _post_backend_with_filter_retry(url: str, headers: dict, body: dict,
rid: str = "", model_name: str = "?") -> tuple[int, bytes, dict]:
prefix = f"[{rid}] " if rid else ""
status, raw = await _post_backend_once(url, headers, body)
text = raw.decode("utf-8", "replace")
if status == 200 and _looks_like_content_filter_text(text) and CONFIG.get("desensitize") and CONFIG.get("no_compact"):
retry_body = _chat_body_desensitize(body, force_compact=True)
_log(f"{prefix}↻ RESPONSES {model_name} | content filter detected, retry with compact harness")
_log(f"{prefix}── RESPONSES RETRY CHAT BODY ──\n{json.dumps(retry_body, ensure_ascii=False, indent=2)}")
retry_status, retry_raw = await _post_backend_once(url, headers, retry_body)
retry_text = retry_raw.decode("utf-8", "replace")
if retry_status == 200 and not _looks_like_content_filter_text(retry_text):
return retry_status, retry_raw, retry_body
return status, raw, body
# ---------------------------------------------------------------------------
# Responses API 端点(Codex CLI 兼容)
# ---------------------------------------------------------------------------
@app.post("/v1/responses")
async def create_response(request: Request,
authorization: Optional[str] = Header(default=None),
x_api_key: Optional[str] = Header(default=None, alias="X-Api-Key")):
"""OpenAI Responses API 兼容端点。
Codex CLI 使用 Responses API(wire_api = "responses")而非 Chat Completions。
本端点接收 Responses 格式请求,转换为 Chat 格式发往后端,再将后端的 Chat SSE
转换为 Responses 语义事件流返回。
"""
_check_auth(authorization, x_api_key)
cred = _cred()
try:
payload = await request.json()
except Exception as e:
raise HTTPException(status_code=400, detail={"error": {"message": f"bad json: {e}", "type": "invalid_request_error"}})
# 转换请求:Responses → Chat
try:
chat_body = responses_request_to_chat(payload)
except Exception as e:
raise HTTPException(status_code=400, detail={"error": {"message": f"request conversion error: {e}", "type": "invalid_request_error"}})
chat_body, projection_stats = project_responses_chat_body(chat_body)
chat_body.setdefault("model", "auto")
chat_body["stream"] = True
if "stream_options" not in chat_body:
chat_body["stream_options"] = {"include_usage": True}
chat_body = _chat_body_desensitize(chat_body)
client_wants_stream = payload.get("stream", True) # Codex CLI 默认 stream
model_name = payload.get("model", "auto")
rid = os.urandom(4).hex()
_log(f"[{rid}] ▶ RESPONSES {model_name} | stream={client_wants_stream} | input_items={len(payload.get('input', []))}")
_log(
f"[{rid}] ── RESPONSES PROJECTION ── "
f"mode={projection_stats.get('mode')} "
f"| msgs {projection_stats.get('original_messages')}→{projection_stats.get('projected_messages')} "
f"| chars {projection_stats.get('original_message_chars')}→{projection_stats.get('projected_message_chars')} "
f"| tools {projection_stats.get('original_tools')}→{projection_stats.get('projected_tools')} "
f"| tool_chars {projection_stats.get('original_tool_chars')}→{projection_stats.get('projected_tool_chars')} "
f"| summarized_history={projection_stats.get('summarized_history_messages', 0)} "
f"| dropped_harness={projection_stats.get('dropped_harness_messages', 0)} "
f"| anchor_user={projection_stats.get('anchor_user_preserved', False)}"
)
_log(f"[{rid}] ── RESPONSES → CHAT BODY ──\n{json.dumps(chat_body, ensure_ascii=False, indent=2)}")
headers = cred.get_headers()
url = f"{BACKEND}/v2/chat/completions"
t0 = time.time()
if client_wants_stream:
return StreamingResponse(
_stream_responses(url, headers, chat_body, model_name, t0, rid),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
# 非流式:聚合后端 SSE → 非流式 Response 对象
try:
status_code, raw, final_body = await _post_backend_with_filter_retry(url, headers, chat_body, rid, model_name)
if status_code != 200:
_log(f"[{rid}] ✗ HTTP {status_code} | {model_name} | {_truncate(raw.decode('utf-8','replace'),200)}")
raise HTTPException(status_code=status_code, detail=_safe_err_raw(raw, status_code))
converter = ResponsesStreamConverter(model=model_name)
for line in raw.decode("utf-8", "replace").splitlines():
converter.feed_line(line)
chat_body = final_body
except HTTPException:
raise
except httpx.HTTPError as e:
_log(f"[{rid}] ✗ 网络错误 | {model_name} | {e}")
raise HTTPException(status_code=502, detail={"error": {"message": f"upstream error: {e}", "type": "upstream_error"}})
result = converter.get_nonstream_response()
elapsed = time.time() - t0
_log(f"[{rid}] ◀ RESPONSES {model_name} | {elapsed:.1f}s")
_log(f"[{rid}] ── RESPONSE OBJ ──\n{json.dumps(result, ensure_ascii=False, indent=2)}")
return JSONResponse(content=result)
async def _stream_responses(url: str, headers: dict, body: dict,
model_name: str = "?", t0: float = 0.0, rid: str = ""):
"""消费后端 Chat SSE,实时转换为 Responses API 事件流输出。"""
converter = ResponsesStreamConverter(model=model_name)
prefix = f"[{rid}] " if rid else ""
try:
status_code, raw, _ = await _post_backend_with_filter_retry(url, headers, body, rid, model_name)
if status_code != 200:
_log(f"{prefix}✗ HTTP {status_code} | {model_name} | {_truncate(raw.decode('utf-8','replace'),200)}")
error_evt = {"type": "error", "error": {"message": raw.decode('utf-8','replace')[:500], "code": status_code}}
yield f"data: {json.dumps(error_evt, ensure_ascii=False)}\n\n".encode("utf-8")
return
raw_sse_lines = []
for line in raw.decode("utf-8", "replace").splitlines():
if line.strip():
raw_sse_lines.append(line)
events = converter.feed_line(line)
if events:
yield events.encode("utf-8")
except httpx.HTTPError as e:
_log(f"{prefix}✗ 网络错误 | {model_name} | {e}")
error_evt = {"type": "error", "error": {"message": str(e)[:500], "code": 502}}
yield f"data: {json.dumps(error_evt, ensure_ascii=False)}\n\n".encode("utf-8")
return
# 发送收尾事件
finish_events = converter.finish()
if finish_events:
yield finish_events.encode("utf-8")
elapsed = time.time() - t0 if t0 else 0
_log(f"{prefix}◀ RESPONSES {model_name} | {elapsed:.1f}s | stream done")
_log(f"{prefix}── RESPONSES RAW SSE ──\n" + "\n".join(raw_sse_lines[-30:]))
# ---------------------------------------------------------------------------
# Anthropic Messages API 端点(Claude Code / CC Switch 兼容)
# ---------------------------------------------------------------------------
@app.post("/v1/messages")
async def create_message(request: Request,
authorization: Optional[str] = Header(default=None),
x_api_key: Optional[str] = Header(default=None, alias="X-Api-Key")):
"""Anthropic Messages API 兼容端点。
Claude Code / CC Switch 使用 Anthropic Messages API(POST /v1/messages)。
本端点接收 Anthropic 格式请求,转换为 Chat 格式发往后端,再将后端的 Chat SSE
转换为 Anthropic SSE 事件流返回。
"""
_check_auth(authorization, x_api_key)
cred = _cred()
try:
payload = await request.json()
except Exception as e:
raise HTTPException(status_code=400, detail={"error": {"message": f"bad json: {e}", "type": "invalid_request_error"}})
# 将 Anthropic 格式消息、工具规范在进入后端前统一转换为 OpenAI Chat 格式。
messages = payload.get("messages") or []
if not messages:
raise HTTPException(status_code=400, detail={"error": {"message": "messages is required", "type": "invalid_request_error"}})
try:
chat_body = anthropic_request_to_chat(payload)
except Exception as e:
raise HTTPException(status_code=400, detail={"error": {"message": f"request conversion error: {e}", "type": "invalid_request_error"}})
chat_body.setdefault("model", "auto")
chat_body["stream"] = True
if "stream_options" not in chat_body:
chat_body["stream_options"] = {"include_usage": True}
if CONFIG.get("desensitize"):
chat_body = desensitize_body(chat_body, roles=("system", "developer"),
desensitize_harness_user=True,
desensitize_tools=True,
compact_harness=not CONFIG.get("no_compact"),
strip_tool_metadata=True)
model_name = payload.get("model", "auto")
chat_messages = chat_body.get("messages", [])
rid = os.urandom(4).hex()
_log(f"[{rid}] ▶ ANTHROPIC {model_name} | msgs={len(chat_messages)} | anthropic_msgs={len(messages)}")
_log(f"[{rid}] ── ANTHROPIC → CHAT BODY ──\n{json.dumps(chat_body, ensure_ascii=False, indent=2)}")
headers = cred.get_headers()
url = f"{BACKEND}/v2/chat/completions"
t0 = time.time()
return StreamingResponse(
_stream_anthropic(url, headers, chat_body, model_name, t0, rid),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
async def _stream_anthropic(url: str, headers: dict, body: dict,
model_name: str = "?", t0: float = 0.0, rid: str = ""):
"""消费后端 OpenAI Chat SSE,实时转换为 Anthropic Messages SSE 事件流。"""
converter = AnthropicStreamConverter(model=model_name)
prefix = f"[{rid}] " if rid else ""
try:
async with httpx.AsyncClient(timeout=None) as c:
async with c.stream("POST", url, headers=headers, json=body) as r:
if r.status_code != 200:
err = await r.aread()
_log(f"{prefix}✗ HTTP {r.status_code} | {model_name} | {_truncate(err.decode('utf-8','replace'),200)}")
error_evt = {"type": "error", "error": {"message": err.decode('utf-8','replace')[:500], "type": "api_error", "code": r.status_code}}
yield f"event: error\ndata: {json.dumps(error_evt, ensure_ascii=False)}\n\n".encode("utf-8")
return
async for line in r.aiter_lines():
events = converter.feed_line(line)
if events:
yield events.encode("utf-8")
except httpx.HTTPError as e:
_log(f"{prefix}✗ 网络错误 | {model_name} | {e}")
error_evt = {"type": "error", "error": {"message": str(e)[:500], "type": "api_error", "code": 502}}
yield f"event: error\ndata: {json.dumps(error_evt, ensure_ascii=False)}\n\n".encode("utf-8")
return
finish_events = converter.finish()
if finish_events:
yield finish_events.encode("utf-8")
elapsed = time.time() - t0 if t0 else 0
_log(f"{prefix}◀ ANTHROPIC {model_name} | {elapsed:.1f}s | stream done")
@app.post("/v1/messages/count_tokens")
async def count_tokens(request: Request,
authorization: Optional[str] = Header(default=None),
x_api_key: Optional[str] = Header(default=None, alias="X-Api-Key")):
"""Anthropic token 计数端点(stub)。
Claude Code 可能在发送消息前调用此端点。
返回一个简单估算值,不做实际 token 计数。
"""
_check_auth(authorization, x_api_key)
return {"input_tokens": 0}
# ---------------------------------------------------------------------------
# 启动
# ---------------------------------------------------------------------------
def preflight(auth_file: str | None = None) -> bool:
af = find_auth_file(auth_file)
sys.stderr.write("==== 预检 ====\n")
sys.stderr.write(f"平台 : {sys.platform}\n")
sys.stderr.write(f"Python : {sys.version.split()[0]}\n")
sys.stderr.write(f"后端 : {BACKEND} (直连,原生 function calling)\n")
sys.stderr.write(f"登录文件 : {af or '(未找到)'}\n")
if auth_dirs():
sys.stderr.write(f"已查目录 : {', '.join(str(d) for d in auth_dirs())}\n")
ok = True
if af is None:
sys.stderr.write("\n[警告] 未找到登录文件。请在桌面端完成登录(CodeBuddy/WorkBuddy)。\n")
ok = False
else:
try:
cm = CredentialManager(af)
info = cm.summary()
sys.stderr.write(f"账号 : {info.get('nickname')} / {info.get('enterpriseName')}\n")
sys.stderr.write(f"token过期 : {'是(将自动刷新)' if info['token_expired'] else '否'}\n")
except Exception as e:
sys.stderr.write(f"[警告] 读取凭据失败:{e}\n")
ok = False
sys.stderr.write("================\n")
return ok
def main():
ap = argparse.ArgumentParser(description="CodeBuddy -> OpenAI 兼容转换器(直连后端)")
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8787)
ap.add_argument("--api-key", default=os.environ.get("CODEBUDDY2OPENAI_KEY", ""),
help="可选:要求客户端携带的 API key(默认不校验)")
ap.add_argument("--log", default=None, metavar="PATH",
help="开启日志并写到该文件(如 --log converter.log 或 --log /tmp/cb.log)。"
"不传则不记日志。")
ap.add_argument("--desensitize", action="store_true",
help="启用脱敏:对 system 消息里的合规模板敏感词(DoS/exploit/credential 等)"
"插入零宽空格,缓解被后端内容审核误拦。默认关闭。")
ap.add_argument("--no-compact", action="store_true",
help="配合 --desensitize 使用:跳过 system/harness 压缩,仅做零宽脱敏。"
"保留原始 system prompt 完整内容(如 Claude Code 的行为指令),"
"但审核误拦风险略高于默认压缩模式。")
ap.add_argument("--skip-check", action="store_true", help="跳过启动预检")
ap.add_argument("--auth-file", default=os.environ.get("CODEBUDDY_AUTH_FILE", ""),
help="显式指定 CodeBuddy/WorkBuddy 登录态 .info 文件路径")
args = ap.parse_args()
CONFIG["api_key"] = args.api_key
CONFIG["desensitize"] = args.desensitize
CONFIG["no_compact"] = args.no_compact
# --log 直接指定文件路径即开启;不传则不记
CONFIG["log_path"] = args.log if args.log else os.environ.get("CODEBUDDY2OPENAI_LOG")
af = find_auth_file(args.auth_file)
CONFIG["cred"] = CredentialManager(af) if af else None
if not args.skip_check:
preflight(args.auth_file)
sys.stderr.write(f"\n✅ 监听 http://{args.host}:{args.port}(直连后端,原生 function calling)\n")
sys.stderr.write(" GET /v1/models\n")
sys.stderr.write(" POST /v1/chat/completions (原生 tools/tool_calls,支持流式)\n")
sys.stderr.write(" POST /v1/responses (Responses API,Codex CLI 兼容)\n")
sys.stderr.write(" POST /v1/messages (Anthropic API,Claude Code / CC Switch 兼容)\n")
sys.stderr.write(" GET /health\n")
if args.api_key:
sys.stderr.write(" 鉴权已启用(API key 已设置)\n")
if CONFIG["log_path"]:
sys.stderr.write(f" 日志 : {CONFIG['log_path']}\n")
if args.desensitize:
mode = "零宽脱敏 + 保留全文" if args.no_compact else "零宽脱敏 + 压缩摘要"
sys.stderr.write(f" 脱敏 : 已启用({mode})\n")
sys.stderr.write("按 Ctrl+C 退出。\n\n")
# 启动时写一条标记
_log(f"==== converter 启动 ====")
uvicorn.run(app, host=args.host, port=args.port, log_level="warning")
if __name__ == "__main__":
main()