fix: restore Buddy2api original server.py with HEAD method on /health + utf-8 coding declaration
Browse files
server.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
"""
|
| 2 |
WorkBuddy → OpenAI-compatible reverse proxy.
|
| 3 |
|
|
@@ -13,6 +14,7 @@ Usage:
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import asyncio
|
|
|
|
| 16 |
import json
|
| 17 |
import logging
|
| 18 |
import os
|
|
@@ -51,10 +53,10 @@ try:
|
|
| 51 |
except ImportError:
|
| 52 |
pass
|
| 53 |
|
| 54 |
-
TOKEN_FILE = BASE_DIR / "data" / "token.json"
|
| 55 |
|
| 56 |
PROXY_PORT = int(os.getenv("PROXY_PORT", "19090"))
|
| 57 |
-
PROXY_API_KEY = os.getenv("PROXY_API_KEY", "
|
| 58 |
WB_API_BASE = os.getenv("WB_API_BASE", "https://copilot.tencent.com")
|
| 59 |
CDP_URL = os.getenv("CDP_URL", "http://127.0.0.1:9222")
|
| 60 |
|
|
@@ -98,8 +100,15 @@ HEADERS_TEMPLATE = {
|
|
| 98 |
"User-Agent": f"CodeBuddyIDE/{WB_VERSION} coding-copilot/{WB_VERSION}",
|
| 99 |
}
|
| 100 |
|
| 101 |
-
REASONING_MODELS = {"deepseek-r1", "deepseek-r1-0528-lkeap", "hunyuan-2.0-thinking-ioa"}
|
| 102 |
DEFAULT_TIMEOUT = int(os.getenv("WB_TIMEOUT", "120"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
REASONING_TIMEOUT = int(os.getenv("WB_REASONING_TIMEOUT", "300"))
|
| 104 |
|
| 105 |
|
|
@@ -191,8 +200,9 @@ class TokenManager:
|
|
| 191 |
try:
|
| 192 |
payload = jwt.decode(self.access_token, options={"verify_signature": False})
|
| 193 |
return time.time() > (payload.get("exp", 0) - 300)
|
| 194 |
-
except Exception:
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
def _log_token_info(self):
|
| 198 |
try:
|
|
@@ -228,7 +238,7 @@ class TokenManager:
|
|
| 228 |
}
|
| 229 |
if self.department_info:
|
| 230 |
headers["X-Department-Info"] = self.department_info
|
| 231 |
-
async with httpx.AsyncClient(
|
| 232 |
resp = await client.post(
|
| 233 |
f"{WB_API_BASE}/v2/plugin/auth/token/refresh",
|
| 234 |
headers=headers,
|
|
@@ -249,6 +259,7 @@ class TokenManager:
|
|
| 249 |
await self._extract_from_cdp()
|
| 250 |
|
| 251 |
async def _extract_from_cdp(self):
|
|
|
|
| 252 |
log.info(f"Extracting token from WorkBuddy via CDP ({CDP_URL})...")
|
| 253 |
try:
|
| 254 |
async with httpx.AsyncClient() as client:
|
|
@@ -376,40 +387,39 @@ def resolve_model(model: str) -> str:
|
|
| 376 |
MODELS = [
|
| 377 |
# DeepSeek
|
| 378 |
{"id": "deepseek-r1", "name": "DeepSeek-R1"},
|
|
|
|
| 379 |
{"id": "deepseek-v3", "name": "DeepSeek-V3"},
|
| 380 |
{"id": "deepseek-v3.2", "name": "DeepSeek-V3.2"},
|
| 381 |
{"id": "deepseek-v3-1", "name": "DeepSeek-V3.1"},
|
| 382 |
{"id": "deepseek-v3-0324", "name": "DeepSeek-V3-0324"},
|
| 383 |
-
{"id": "deepseek-v3-1-volc", "name": "DeepSeek-V3-1-Terminus"},
|
| 384 |
{"id": "deepseek-v3-0324-lkeap", "name": "DeepSeek-V3-0324-LKEAP"},
|
| 385 |
-
{"id": "deepseek-
|
| 386 |
-
{"id": "deepseek-
|
| 387 |
-
|
| 388 |
-
# Claude
|
| 389 |
-
{"id": "claude-4.5", "name": "Claude-Sonnet-4.5"},
|
| 390 |
-
{"id": "claude-opus-4.5", "name": "Claude-Opus-4.5"},
|
| 391 |
-
{"id": "claude-opus-4.6", "name": "Claude-Opus-4.6"},
|
| 392 |
-
{"id": "claude-opus-4.6-1m", "name": "Claude-Opus-4.6 (1M context)"},
|
| 393 |
-
{"id": "claude-sonnet-4.6", "name": "Claude-Sonnet-4.6"},
|
| 394 |
-
{"id": "claude-sonnet-4.6-1m", "name": "Claude-Sonnet-4.6 (1M context)"},
|
| 395 |
{"id": "claude-haiku-4.5", "name": "Claude-Haiku-4.5"},
|
| 396 |
-
|
| 397 |
-
{"id": "
|
| 398 |
-
{"id": "gemini-3.1-flash-lite", "name": "Gemini-3.1-Flash-Lite"},
|
| 399 |
# GLM
|
| 400 |
{"id": "glm-4.6", "name": "GLM-4.6"},
|
| 401 |
{"id": "glm-4.7", "name": "GLM-4.7"},
|
| 402 |
-
{"id": "glm-
|
| 403 |
-
{"id": "glm-5.
|
| 404 |
-
{"id": "glm-5.
|
| 405 |
{"id": "glm-5v-turbo", "name": "GLM-5v-Turbo"},
|
| 406 |
{"id": "glm-5v-turbo-ioa", "name": "GLM-5v-Turbo-IOA"},
|
| 407 |
# Hunyuan
|
| 408 |
-
{"id": "hunyuan-2.0-instruct", "name": "Hunyuan-2.0
|
| 409 |
-
{"id": "hunyuan-2.0-
|
| 410 |
-
{"id": "hunyuan-2.0-thinking-ioa", "name": "Hunyuan-2.0-Thinking"},
|
|
|
|
| 411 |
# Kimi
|
| 412 |
-
{"id": "kimi-k2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
# Default
|
| 414 |
{"id": "codewise-default-model-v2", "name": "Default (Codewise)"},
|
| 415 |
]
|
|
@@ -425,7 +435,6 @@ http_pool: httpx.AsyncClient | None = None
|
|
| 425 |
async def lifespan(_app: FastAPI):
|
| 426 |
global http_pool
|
| 427 |
http_pool = httpx.AsyncClient(
|
| 428 |
-
verify=False,
|
| 429 |
timeout=httpx.Timeout(DEFAULT_TIMEOUT, connect=10),
|
| 430 |
limits=httpx.Limits(max_connections=50, max_keepalive_connections=10),
|
| 431 |
)
|
|
@@ -437,7 +446,8 @@ async def lifespan(_app: FastAPI):
|
|
| 437 |
|
| 438 |
app = FastAPI(title="WorkBuddy Proxy", lifespan=lifespan)
|
| 439 |
|
| 440 |
-
#
|
|
|
|
| 441 |
app.add_middleware(
|
| 442 |
CORSMiddleware,
|
| 443 |
allow_origins=["*"],
|
|
@@ -452,7 +462,7 @@ def _verify_api_key(request: Request):
|
|
| 452 |
key = auth.replace("Bearer ", "").strip()
|
| 453 |
if not key:
|
| 454 |
key = (request.headers.get("X-API-Key") or "").strip()
|
| 455 |
-
if key
|
| 456 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 457 |
|
| 458 |
|
|
@@ -517,12 +527,12 @@ def _timeout_for(model: str) -> float:
|
|
| 517 |
|
| 518 |
|
| 519 |
async def _upstream_stream(url: str, headers: dict, body: dict, timeout: float):
|
| 520 |
-
"""Open a streaming connection to upstream;
|
| 521 |
try:
|
| 522 |
-
req = http_pool.build_request("POST", url, headers=headers, json=body)
|
| 523 |
resp = await http_pool.send(req, stream=True)
|
| 524 |
return resp
|
| 525 |
-
except httpx.TimeoutException:
|
| 526 |
return None
|
| 527 |
|
| 528 |
|
|
@@ -542,6 +552,10 @@ async def chat_completions(request: Request):
|
|
| 542 |
wb_body["stream"] = True
|
| 543 |
wb_body["model"] = model
|
| 544 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
access_token = await token_mgr.get_token()
|
| 546 |
if not access_token:
|
| 547 |
raise HTTPException(status_code=503, detail="No valid WorkBuddy token")
|
|
@@ -583,7 +597,6 @@ async def _stream_response(
|
|
| 583 |
|
| 584 |
try:
|
| 585 |
if resp.status_code == 401:
|
| 586 |
-
await resp.aclose()
|
| 587 |
log.warning(f"[{model}] Got 401, refreshing token...")
|
| 588 |
await token_mgr.refresh()
|
| 589 |
if attempt < max_attempts:
|
|
@@ -594,27 +607,59 @@ async def _stream_response(
|
|
| 594 |
|
| 595 |
if resp.status_code != 200:
|
| 596 |
error_body = await resp.aread()
|
| 597 |
-
|
| 598 |
-
|
|
|
|
| 599 |
yield "data: [DONE]\n\n"
|
| 600 |
return
|
| 601 |
|
| 602 |
done_sent = False
|
|
|
|
| 603 |
async for line in resp.aiter_lines():
|
| 604 |
-
if line.startswith("data: "):
|
| 605 |
-
if line
|
| 606 |
-
done_sent = True
|
| 607 |
-
else:
|
| 608 |
has_content = True
|
| 609 |
yield line + "\n\n"
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
elapsed = time.monotonic() - t_start
|
| 615 |
|
| 616 |
-
if not has_content and attempt < max_attempts:
|
| 617 |
-
await resp.aclose()
|
| 618 |
log.warning(f"[{model}] Empty response, retrying... ({elapsed:.1f}s)")
|
| 619 |
await asyncio.sleep(1)
|
| 620 |
continue
|
|
@@ -628,7 +673,6 @@ async def _stream_response(
|
|
| 628 |
except httpx.ReadTimeout:
|
| 629 |
log.error(f"[{model}] Read timeout during stream (attempt {attempt})")
|
| 630 |
if attempt < max_attempts:
|
| 631 |
-
await resp.aclose()
|
| 632 |
continue
|
| 633 |
yield 'data: {"error":"upstream timeout"}\n\n'
|
| 634 |
yield "data: [DONE]\n\n"
|
|
@@ -650,6 +694,7 @@ async def _non_stream_response(
|
|
| 650 |
headers = _build_headers(access_token)
|
| 651 |
|
| 652 |
collected_content = ""
|
|
|
|
| 653 |
tool_calls_map: dict[int, dict] = {}
|
| 654 |
finish_reason = "stop"
|
| 655 |
resp_model = model
|
|
@@ -664,7 +709,6 @@ async def _non_stream_response(
|
|
| 664 |
|
| 665 |
try:
|
| 666 |
if resp.status_code == 401:
|
| 667 |
-
await resp.aclose()
|
| 668 |
log.warning(f"[{model}] Got 401, refreshing token...")
|
| 669 |
await token_mgr.refresh()
|
| 670 |
if attempt < max_attempts:
|
|
@@ -674,7 +718,7 @@ async def _non_stream_response(
|
|
| 674 |
if resp.status_code != 200:
|
| 675 |
error_body = await resp.aread()
|
| 676 |
raise HTTPException(status_code=resp.status_code,
|
| 677 |
-
detail=error_body.decode())
|
| 678 |
|
| 679 |
async for line in resp.aiter_lines():
|
| 680 |
text = line.removeprefix("data: ").strip()
|
|
@@ -686,6 +730,9 @@ async def _non_stream_response(
|
|
| 686 |
delta = choice.get("delta", {})
|
| 687 |
|
| 688 |
collected_content += delta.get("content") or ""
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
for tc in delta.get("tool_calls") or []:
|
| 691 |
idx = tc.get("index", 0)
|
|
@@ -714,10 +761,15 @@ async def _non_stream_response(
|
|
| 714 |
except (json.JSONDecodeError, IndexError, KeyError):
|
| 715 |
pass
|
| 716 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
finally:
|
| 718 |
await resp.aclose()
|
| 719 |
|
| 720 |
-
if not collected_content and not tool_calls_map and attempt < max_attempts:
|
| 721 |
log.warning(f"[{model}] Empty response, retrying...")
|
| 722 |
await asyncio.sleep(1)
|
| 723 |
continue
|
|
@@ -728,6 +780,8 @@ async def _non_stream_response(
|
|
| 728 |
log.info(f"[{model}] non-stream {elapsed:.1f}s prompt={prompt_t} completion={compl_t}")
|
| 729 |
|
| 730 |
message: dict = {"role": "assistant", "content": collected_content or None}
|
|
|
|
|
|
|
| 731 |
if tool_calls_map:
|
| 732 |
message["tool_calls"] = [tool_calls_map[i] for i in sorted(tool_calls_map)]
|
| 733 |
|
|
@@ -756,8 +810,14 @@ async def health():
|
|
| 756 |
|
| 757 |
|
| 758 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
log.info(f"Starting WorkBuddy proxy on port {PROXY_PORT}")
|
| 760 |
log.info(f"WB version: {WB_VERSION}")
|
| 761 |
-
log.info(f"API key: {PROXY_API_KEY}")
|
| 762 |
log.info(f"Upstream: {WB_API_BASE}")
|
| 763 |
uvicorn.run(app, host="0.0.0.0", port=PROXY_PORT, log_level="info")
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
WorkBuddy → OpenAI-compatible reverse proxy.
|
| 4 |
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
import asyncio
|
| 17 |
+
import hmac
|
| 18 |
import json
|
| 19 |
import logging
|
| 20 |
import os
|
|
|
|
| 53 |
except ImportError:
|
| 54 |
pass
|
| 55 |
|
| 56 |
+
TOKEN_FILE = Path(os.getenv("TOKEN_FILE_PATH", str(BASE_DIR / "data" / "token.json")))
|
| 57 |
|
| 58 |
PROXY_PORT = int(os.getenv("PROXY_PORT", "19090"))
|
| 59 |
+
PROXY_API_KEY = os.getenv("PROXY_API_KEY", "").strip()
|
| 60 |
WB_API_BASE = os.getenv("WB_API_BASE", "https://copilot.tencent.com")
|
| 61 |
CDP_URL = os.getenv("CDP_URL", "http://127.0.0.1:9222")
|
| 62 |
|
|
|
|
| 100 |
"User-Agent": f"CodeBuddyIDE/{WB_VERSION} coding-copilot/{WB_VERSION}",
|
| 101 |
}
|
| 102 |
|
| 103 |
+
REASONING_MODELS = {"deepseek-r1", "deepseek-r1-0528-lkeap", "hunyuan-2.0-thinking-ioa", "hunyuan-2.0-thinking", "hy3-preview", "kimi-k2.6", "kimi-k2.6-ioa", "kimi-k2.7", "deepseek-v4-pro", "glm-5.2"}
|
| 104 |
DEFAULT_TIMEOUT = int(os.getenv("WB_TIMEOUT", "120"))
|
| 105 |
+
|
| 106 |
+
# WorkBuddy prepends "Thought: Xms\n" to each reasoning chunk — strip it
|
| 107 |
+
_THOUGHT_RE = re.compile(r"^Thought:\s*\d+ms\s*\n", re.MULTILINE)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _clean_reasoning(text: str) -> str:
|
| 111 |
+
return _THOUGHT_RE.sub("", text)
|
| 112 |
REASONING_TIMEOUT = int(os.getenv("WB_REASONING_TIMEOUT", "300"))
|
| 113 |
|
| 114 |
|
|
|
|
| 200 |
try:
|
| 201 |
payload = jwt.decode(self.access_token, options={"verify_signature": False})
|
| 202 |
return time.time() > (payload.get("exp", 0) - 300)
|
| 203 |
+
except Exception as e:
|
| 204 |
+
log.warning(f"Failed to decode token, treating as expired: {e}")
|
| 205 |
+
return True
|
| 206 |
|
| 207 |
def _log_token_info(self):
|
| 208 |
try:
|
|
|
|
| 238 |
}
|
| 239 |
if self.department_info:
|
| 240 |
headers["X-Department-Info"] = self.department_info
|
| 241 |
+
async with httpx.AsyncClient() as client:
|
| 242 |
resp = await client.post(
|
| 243 |
f"{WB_API_BASE}/v2/plugin/auth/token/refresh",
|
| 244 |
headers=headers,
|
|
|
|
| 259 |
await self._extract_from_cdp()
|
| 260 |
|
| 261 |
async def _extract_from_cdp(self):
|
| 262 |
+
"""Extract token via CDP (deprecated: WorkBuddy 5.1.4+ no longer exposes ipcRenderer)."""
|
| 263 |
log.info(f"Extracting token from WorkBuddy via CDP ({CDP_URL})...")
|
| 264 |
try:
|
| 265 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 387 |
MODELS = [
|
| 388 |
# DeepSeek
|
| 389 |
{"id": "deepseek-r1", "name": "DeepSeek-R1"},
|
| 390 |
+
{"id": "deepseek-r1-0528-lkeap", "name": "DeepSeek-R1-LKEAP"},
|
| 391 |
{"id": "deepseek-v3", "name": "DeepSeek-V3"},
|
| 392 |
{"id": "deepseek-v3.2", "name": "DeepSeek-V3.2"},
|
| 393 |
{"id": "deepseek-v3-1", "name": "DeepSeek-V3.1"},
|
| 394 |
{"id": "deepseek-v3-0324", "name": "DeepSeek-V3-0324"},
|
|
|
|
| 395 |
{"id": "deepseek-v3-0324-lkeap", "name": "DeepSeek-V3-0324-LKEAP"},
|
| 396 |
+
{"id": "deepseek-v3-1-volc", "name": "DeepSeek-V3.1-Volc"},
|
| 397 |
+
{"id": "deepseek-v4-pro", "name": "DeepSeek-V4-Pro"},
|
| 398 |
+
{"id": "deepseek-v4-flash", "name": "DeepSeek-V4-Flash"},
|
| 399 |
+
# Claude (haiku only — opus/sonnet require enterprise auth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
{"id": "claude-haiku-4.5", "name": "Claude-Haiku-4.5"},
|
| 401 |
+
{"id": "claude-4.5-haiku", "name": "Claude-4.5-Haiku"},
|
| 402 |
+
{"id": "claude-4.5-haiku-thinking", "name": "Claude-4.5-Haiku-Thinking"},
|
|
|
|
| 403 |
# GLM
|
| 404 |
{"id": "glm-4.6", "name": "GLM-4.6"},
|
| 405 |
{"id": "glm-4.7", "name": "GLM-4.7"},
|
| 406 |
+
{"id": "glm-5.0", "name": "GLM-5.0"},
|
| 407 |
+
{"id": "glm-5.1", "name": "GLM-5.1"},
|
| 408 |
+
{"id": "glm-5.2", "name": "GLM-5.2"},
|
| 409 |
{"id": "glm-5v-turbo", "name": "GLM-5v-Turbo"},
|
| 410 |
{"id": "glm-5v-turbo-ioa", "name": "GLM-5v-Turbo-IOA"},
|
| 411 |
# Hunyuan
|
| 412 |
+
{"id": "hunyuan-2.0-instruct", "name": "Hunyuan-2.0"},
|
| 413 |
+
{"id": "hunyuan-2.0-thinking", "name": "Hunyuan-2.0-Thinking"},
|
| 414 |
+
{"id": "hunyuan-2.0-thinking-ioa", "name": "Hunyuan-2.0-Thinking-IOA"},
|
| 415 |
+
{"id": "hy3-preview", "name": "Hunyuan-3-Preview"},
|
| 416 |
# Kimi
|
| 417 |
+
{"id": "kimi-k2.6", "name": "Kimi-K2.6"},
|
| 418 |
+
{"id": "kimi-k2.6-ioa", "name": "Kimi-K2.6-IOA"},
|
| 419 |
+
{"id": "kimi-k2.7", "name": "Kimi-K2.7"},
|
| 420 |
+
# MiniMax
|
| 421 |
+
{"id": "minimax-m3", "name": "MiniMax-M3"},
|
| 422 |
+
{"id": "minimax-m3-ioa", "name": "MiniMax-M3-IOA"},
|
| 423 |
# Default
|
| 424 |
{"id": "codewise-default-model-v2", "name": "Default (Codewise)"},
|
| 425 |
]
|
|
|
|
| 435 |
async def lifespan(_app: FastAPI):
|
| 436 |
global http_pool
|
| 437 |
http_pool = httpx.AsyncClient(
|
|
|
|
| 438 |
timeout=httpx.Timeout(DEFAULT_TIMEOUT, connect=10),
|
| 439 |
limits=httpx.Limits(max_connections=50, max_keepalive_connections=10),
|
| 440 |
)
|
|
|
|
| 446 |
|
| 447 |
app = FastAPI(title="WorkBuddy Proxy", lifespan=lifespan)
|
| 448 |
|
| 449 |
+
# CORS: allow all origins since this is a public-facing API proxy.
|
| 450 |
+
# Auth is enforced via API key, not origin.
|
| 451 |
app.add_middleware(
|
| 452 |
CORSMiddleware,
|
| 453 |
allow_origins=["*"],
|
|
|
|
| 462 |
key = auth.replace("Bearer ", "").strip()
|
| 463 |
if not key:
|
| 464 |
key = (request.headers.get("X-API-Key") or "").strip()
|
| 465 |
+
if not hmac.compare_digest(key, PROXY_API_KEY):
|
| 466 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 467 |
|
| 468 |
|
|
|
|
| 527 |
|
| 528 |
|
| 529 |
async def _upstream_stream(url: str, headers: dict, body: dict, timeout: float):
|
| 530 |
+
"""Open a streaming connection to upstream; returns resp or None on failure."""
|
| 531 |
try:
|
| 532 |
+
req = http_pool.build_request("POST", url, headers=headers, json=body, timeout=timeout)
|
| 533 |
resp = await http_pool.send(req, stream=True)
|
| 534 |
return resp
|
| 535 |
+
except (httpx.TimeoutException, httpx.ConnectError, httpx.NetworkError, httpx.RemoteProtocolError):
|
| 536 |
return None
|
| 537 |
|
| 538 |
|
|
|
|
| 552 |
wb_body["stream"] = True
|
| 553 |
wb_body["model"] = model
|
| 554 |
|
| 555 |
+
# Auto-inject reasoning for thinking models
|
| 556 |
+
if model in REASONING_MODELS:
|
| 557 |
+
wb_body.setdefault("reasoning", {"effort": "medium"})
|
| 558 |
+
|
| 559 |
access_token = await token_mgr.get_token()
|
| 560 |
if not access_token:
|
| 561 |
raise HTTPException(status_code=503, detail="No valid WorkBuddy token")
|
|
|
|
| 597 |
|
| 598 |
try:
|
| 599 |
if resp.status_code == 401:
|
|
|
|
| 600 |
log.warning(f"[{model}] Got 401, refreshing token...")
|
| 601 |
await token_mgr.refresh()
|
| 602 |
if attempt < max_attempts:
|
|
|
|
| 607 |
|
| 608 |
if resp.status_code != 200:
|
| 609 |
error_body = await resp.aread()
|
| 610 |
+
error_text = error_body.decode("utf-8", errors="replace")
|
| 611 |
+
log.error(f"[{model}] Upstream {resp.status_code}: {error_text[:200]}")
|
| 612 |
+
yield f"data: {json.dumps({'error': error_text})}\n\n"
|
| 613 |
yield "data: [DONE]\n\n"
|
| 614 |
return
|
| 615 |
|
| 616 |
done_sent = False
|
| 617 |
+
reasoning_buf = ""
|
| 618 |
async for line in resp.aiter_lines():
|
| 619 |
+
if not line.startswith("data: "):
|
| 620 |
+
if line.strip():
|
|
|
|
|
|
|
| 621 |
has_content = True
|
| 622 |
yield line + "\n\n"
|
| 623 |
+
continue
|
| 624 |
+
if line == "data: [DONE]":
|
| 625 |
+
done_sent = True
|
| 626 |
+
if reasoning_buf:
|
| 627 |
+
buf_chunk = {"choices": [{"index": 0, "delta": {"reasoning_content": reasoning_buf}}]}
|
| 628 |
+
yield f"data: {json.dumps(buf_chunk)}\n\n"
|
| 629 |
+
yield line + "\n\n"
|
| 630 |
+
continue
|
| 631 |
+
|
| 632 |
+
has_content = True
|
| 633 |
+
try:
|
| 634 |
+
chunk = json.loads(line.removeprefix("data: "))
|
| 635 |
+
choice = chunk.get("choices", [{}])[0]
|
| 636 |
+
delta = choice.get("delta", {})
|
| 637 |
+
rc = delta.get("reasoning_content")
|
| 638 |
+
has_content_field = bool(delta.get("content") or delta.get("tool_calls"))
|
| 639 |
+
|
| 640 |
+
if rc is not None:
|
| 641 |
+
cleaned = _clean_reasoning(rc)
|
| 642 |
+
if has_content_field and reasoning_buf:
|
| 643 |
+
buf_chunk = {"choices": [{"index": 0, "delta": {"reasoning_content": reasoning_buf}}]}
|
| 644 |
+
yield f"data: {json.dumps(buf_chunk)}\n\n"
|
| 645 |
+
reasoning_buf = ""
|
| 646 |
+
reasoning_buf += cleaned
|
| 647 |
+
if not has_content_field:
|
| 648 |
+
continue
|
| 649 |
+
delta.pop("reasoning_content", None)
|
| 650 |
+
|
| 651 |
+
if reasoning_buf:
|
| 652 |
+
buf_chunk = {"choices": [{"index": 0, "delta": {"reasoning_content": reasoning_buf}}]}
|
| 653 |
+
yield f"data: {json.dumps(buf_chunk)}\n\n"
|
| 654 |
+
reasoning_buf = ""
|
| 655 |
+
|
| 656 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
| 657 |
+
except (json.JSONDecodeError, KeyError):
|
| 658 |
+
yield line + "\n\n"
|
| 659 |
|
| 660 |
elapsed = time.monotonic() - t_start
|
| 661 |
|
| 662 |
+
if not has_content and not reasoning_buf and attempt < max_attempts:
|
|
|
|
| 663 |
log.warning(f"[{model}] Empty response, retrying... ({elapsed:.1f}s)")
|
| 664 |
await asyncio.sleep(1)
|
| 665 |
continue
|
|
|
|
| 673 |
except httpx.ReadTimeout:
|
| 674 |
log.error(f"[{model}] Read timeout during stream (attempt {attempt})")
|
| 675 |
if attempt < max_attempts:
|
|
|
|
| 676 |
continue
|
| 677 |
yield 'data: {"error":"upstream timeout"}\n\n'
|
| 678 |
yield "data: [DONE]\n\n"
|
|
|
|
| 694 |
headers = _build_headers(access_token)
|
| 695 |
|
| 696 |
collected_content = ""
|
| 697 |
+
collected_reasoning = ""
|
| 698 |
tool_calls_map: dict[int, dict] = {}
|
| 699 |
finish_reason = "stop"
|
| 700 |
resp_model = model
|
|
|
|
| 709 |
|
| 710 |
try:
|
| 711 |
if resp.status_code == 401:
|
|
|
|
| 712 |
log.warning(f"[{model}] Got 401, refreshing token...")
|
| 713 |
await token_mgr.refresh()
|
| 714 |
if attempt < max_attempts:
|
|
|
|
| 718 |
if resp.status_code != 200:
|
| 719 |
error_body = await resp.aread()
|
| 720 |
raise HTTPException(status_code=resp.status_code,
|
| 721 |
+
detail=error_body.decode("utf-8", errors="replace"))
|
| 722 |
|
| 723 |
async for line in resp.aiter_lines():
|
| 724 |
text = line.removeprefix("data: ").strip()
|
|
|
|
| 730 |
delta = choice.get("delta", {})
|
| 731 |
|
| 732 |
collected_content += delta.get("content") or ""
|
| 733 |
+
rc = delta.get("reasoning_content")
|
| 734 |
+
if rc:
|
| 735 |
+
collected_reasoning += _clean_reasoning(rc)
|
| 736 |
|
| 737 |
for tc in delta.get("tool_calls") or []:
|
| 738 |
idx = tc.get("index", 0)
|
|
|
|
| 761 |
except (json.JSONDecodeError, IndexError, KeyError):
|
| 762 |
pass
|
| 763 |
|
| 764 |
+
except httpx.ReadTimeout:
|
| 765 |
+
log.error(f"[{model}] Read timeout during non-stream (attempt {attempt})")
|
| 766 |
+
if attempt < max_attempts:
|
| 767 |
+
continue
|
| 768 |
+
raise HTTPException(status_code=504, detail="Upstream read timeout")
|
| 769 |
finally:
|
| 770 |
await resp.aclose()
|
| 771 |
|
| 772 |
+
if not collected_content and not collected_reasoning and not tool_calls_map and attempt < max_attempts:
|
| 773 |
log.warning(f"[{model}] Empty response, retrying...")
|
| 774 |
await asyncio.sleep(1)
|
| 775 |
continue
|
|
|
|
| 780 |
log.info(f"[{model}] non-stream {elapsed:.1f}s prompt={prompt_t} completion={compl_t}")
|
| 781 |
|
| 782 |
message: dict = {"role": "assistant", "content": collected_content or None}
|
| 783 |
+
if collected_reasoning:
|
| 784 |
+
message["reasoning_content"] = collected_reasoning
|
| 785 |
if tool_calls_map:
|
| 786 |
message["tool_calls"] = [tool_calls_map[i] for i in sorted(tool_calls_map)]
|
| 787 |
|
|
|
|
| 810 |
|
| 811 |
|
| 812 |
if __name__ == "__main__":
|
| 813 |
+
if not PROXY_API_KEY:
|
| 814 |
+
log.error("PROXY_API_KEY is not set. Set it via env var or .env file.")
|
| 815 |
+
log.error("Example: PROXY_API_KEY=$(openssl rand -hex 32) python server.py")
|
| 816 |
+
sys.exit(1)
|
| 817 |
+
if len(PROXY_API_KEY) < 16:
|
| 818 |
+
log.warning("PROXY_API_KEY is shorter than 16 chars — consider using a stronger key")
|
| 819 |
log.info(f"Starting WorkBuddy proxy on port {PROXY_PORT}")
|
| 820 |
log.info(f"WB version: {WB_VERSION}")
|
| 821 |
+
log.info(f"API key: {PROXY_API_KEY[:4]}***{PROXY_API_KEY[-4:] if len(PROXY_API_KEY) > 8 else ''}")
|
| 822 |
log.info(f"Upstream: {WB_API_BASE}")
|
| 823 |
uvicorn.run(app, host="0.0.0.0", port=PROXY_PORT, log_level="info")
|