workbuddy2api 2.0.6__py3-none-any.whl → 2.0.8.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,14 +13,17 @@ Usage with uv:
13
13
  uv run --with workbuddy2api python -m codebuddy_proxy --host 0.0.0.0 --port 8787
14
14
  """
15
15
 
16
- import argparse
17
- import asyncio
18
- import base64
19
- import hashlib
20
- import io
21
- import importlib.metadata
22
- import importlib.resources
23
- import json
16
+ import argparse
17
+ import asyncio
18
+ import copy
19
+ from contextlib import asynccontextmanager, nullcontext
20
+ from contextvars import ContextVar
21
+ import base64
22
+ import hashlib
23
+ import io
24
+ import importlib.metadata
25
+ import importlib.resources
26
+ import json
24
27
  import logging
25
28
  import logging.handlers
26
29
  import os
@@ -28,22 +31,32 @@ import pathlib
28
31
  import platform
29
32
  import sys
30
33
  import time
34
+ import tempfile
31
35
  import uuid
36
+ from contextlib import AsyncExitStack
32
37
  from typing import Any, Optional
33
38
 
34
39
  import httpx
35
40
  from fastapi import FastAPI, Request, HTTPException
41
+ from codebuddy_proxy.tool_pairing import ToolPairingError
36
42
  from fastapi.responses import JSONResponse, StreamingResponse
37
43
  import uvicorn
38
- from codebuddy_proxy.dsml_parser import DSMLStreamBuffer, parse_all_tool_calls, remove_all_tool_call_markers
39
-
40
- from codebuddy_proxy.backend_profile import (
41
- BackendProfile,
42
- ResolvedBackend,
43
- DOMESTIC_PROFILE,
44
- resolve_backend_from_args,
45
- )
46
- from codebuddy_proxy.codebuddy_client_demo import CodeBuddyClient, CodeBuddyError
44
+ from codebuddy_proxy.dsml_parser import DSMLStreamBuffer, parse_all_tool_calls, remove_all_tool_call_markers
45
+
46
+ from codebuddy_proxy.backend_profile import (
47
+ BackendProfile,
48
+ ResolvedBackend,
49
+ DOMESTIC_PROFILE,
50
+ GLOBAL_PROFILE,
51
+ resolve_backend,
52
+ resolve_backend_from_args,
53
+ )
54
+ from codebuddy_proxy.codebuddy_client_demo import CodeBuddyClient, CodeBuddyError
55
+ from daily_checkin.workbuddy_api import create_legacy_router
56
+ from codebuddy_proxy.upstream_request import ChatRequestPreparer
57
+ from codebuddy_proxy.protocol_response import format_nonstream_response
58
+ from codebuddy_proxy.process_diagnostics import ProcessDiagnostics
59
+ from codebuddy_proxy.admin_api import create_admin_router
47
60
 
48
61
  # 尝试导入高级功能模块(可选)
49
62
  try:
@@ -87,6 +100,17 @@ except ImportError:
87
100
  # 日志配置
88
101
  # ============================================================================
89
102
 
103
+ class ObservedTimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
104
+ """Make otherwise-suppressed Python logging handler failures observable."""
105
+
106
+ def handleError(self, record: logging.LogRecord) -> None:
107
+ monitor = globals().get("process_diagnostics")
108
+ exc = sys.exc_info()[1]
109
+ if monitor is not None and exc is not None:
110
+ monitor.log_failure("file_logger", exc)
111
+ super().handleError(record)
112
+
113
+
90
114
  def setup_logging(log_dir: pathlib.Path) -> logging.Logger:
91
115
  """配置滚动日志:按天分片,保留30天。"""
92
116
  log_dir.mkdir(parents=True, exist_ok=True)
@@ -97,7 +121,7 @@ def setup_logging(log_dir: pathlib.Path) -> logging.Logger:
97
121
  logger.propagate = False
98
122
  logger.handlers.clear()
99
123
 
100
- handler = logging.handlers.TimedRotatingFileHandler(
124
+ handler = ObservedTimedRotatingFileHandler(
101
125
  log_file, when='midnight', interval=1, backupCount=30, encoding='utf-8'
102
126
  )
103
127
  handler.suffix = "%Y-%m-%d"
@@ -118,7 +142,7 @@ def setup_json_logging(log_file: pathlib.Path) -> logging.Logger:
118
142
  logger.propagate = False
119
143
  logger.handlers.clear()
120
144
 
121
- handler = logging.handlers.TimedRotatingFileHandler(
145
+ handler = ObservedTimedRotatingFileHandler(
122
146
  log_file, when="midnight", interval=1, backupCount=30, encoding="utf-8"
123
147
  )
124
148
  handler.suffix = "%Y-%m-%d"
@@ -162,8 +186,12 @@ class ProxyState:
162
186
  verbose_llm: bool = False,
163
187
  logger: logging.Logger | None = None,
164
188
  json_logger: logging.Logger | None = None,
189
+ account_manager=None,
165
190
  ):
166
- self.client = client
191
+ self._client = client
192
+ self.account_manager = account_manager
193
+ self.account_runtime = account_manager
194
+ self.managed_auth = account_manager is not None
167
195
  self.mock_dir = mock_dir
168
196
  self.log_file = log_file
169
197
  self.enable_desensitize = enable_desensitize
@@ -173,13 +201,47 @@ class ProxyState:
173
201
  self.json_logger = json_logger
174
202
  self.runtime_info = get_runtime_info()
175
203
  self.started_at = time.time()
204
+ checkins = getattr(account_manager, "checkins", None)
205
+ if checkins is not None:
206
+ checkins.configure_logging(self.log_checkin)
207
+
208
+ def log_checkin(self, event: str, **fields) -> None:
209
+ """Send safe check-in events to existing files and the process console."""
210
+ self.write_log(event, **fields)
211
+ summary = json.dumps(fields, ensure_ascii=False)
212
+ if self.logger is not None:
213
+ self.logger.info("%s: %s", event, summary)
214
+ try:
215
+ print(f"[Checkin] {event} {summary}", flush=True)
216
+ except (OSError, UnicodeError):
217
+ pass
218
+
219
+ @property
220
+ def client(self):
221
+ if self.account_manager is not None:
222
+ return self.account_manager.client or self._client
223
+ return self._client
224
+
225
+ @client.setter
226
+ def client(self, value):
227
+ self._client = value
228
+
229
+ def for_request(self):
230
+ """Pin the account before any await, including refresh and billing retries."""
231
+ snapshot = copy.copy(self)
232
+ snapshot._client = self.client
233
+ snapshot.account_manager = None
234
+ return snapshot
176
235
 
177
236
  def ensure_auth(self) -> None:
178
237
  """确保已认证;认证失败(token 过期/网络错误/登录未完成)返回结构化 401 而非 500。"""
179
238
  if self.mock_dir is not None:
180
239
  return
181
240
  try:
182
- self.client.ensure_authenticated()
241
+ if self.managed_auth:
242
+ self.client.ensure_authenticated(open_browser=False, allow_login=False)
243
+ else:
244
+ self.client.ensure_authenticated()
183
245
  except HTTPException:
184
246
  raise # 已是结构化异常,原样透传(如 503 proxy not initialized)
185
247
  except Exception as exc:
@@ -187,9 +249,9 @@ class ProxyState:
187
249
  status_code=401,
188
250
  detail={
189
251
  "error": {
190
- "message": "认证失败:token 无效或已过期,请重新登录(--login)",
252
+ "message": "认证失败:请在 /admin 关联或切换账号,或重新登录(--login)",
191
253
  "type": "authentication_error",
192
- "details": str(exc)[:200],
254
+ "details": type(exc).__name__ if self.managed_auth else str(exc)[:200],
193
255
  }
194
256
  },
195
257
  )
@@ -205,8 +267,10 @@ class ProxyState:
205
267
  **kwargs,
206
268
  }
207
269
  self.json_logger.info(json.dumps(record, ensure_ascii=False))
208
- except Exception:
209
- pass
270
+ except Exception as exc:
271
+ monitor = globals().get("process_diagnostics")
272
+ if monitor is not None:
273
+ monitor.log_failure("jsonl_write", exc)
210
274
 
211
275
  def write_body_log(self, event: str, body: bytes, **kwargs) -> None:
212
276
  if self.json_logger is None:
@@ -222,18 +286,98 @@ class ProxyState:
222
286
  **kwargs
223
287
  }
224
288
  self.json_logger.info(json.dumps(record, ensure_ascii=False))
225
- except Exception:
226
- pass
289
+ except Exception as exc:
290
+ monitor = globals().get("process_diagnostics")
291
+ if monitor is not None:
292
+ monitor.log_failure("jsonl_body_write", exc)
227
293
 
228
294
 
229
295
  # ============================================================================
230
296
  # FastAPI 应用
231
297
  # ============================================================================
232
298
 
233
- app = FastAPI(title="CodeBuddy Proxy (FastAPI)", version="2.0")
299
+ app = FastAPI(title="CodeBuddy Proxy (FastAPI)", version="2.0.8.dev1")
234
300
 
235
301
  # 全局状态(在 main() 中初始化)
236
302
  proxy_state: ProxyState | None = None
303
+ process_diagnostics: ProcessDiagnostics | None = None
304
+ _request_state: ContextVar[Any] = ContextVar("account_request_state", default=None)
305
+
306
+
307
+ @app.middleware("http")
308
+ async def pin_account_at_request_entry(request: Request, call_next):
309
+ # Capture before body parsing or other awaits. Each downstream task also
310
+ # inherits this context, including streaming responses.
311
+ if proxy_state is None or not request.url.path.startswith("/v1/"):
312
+ return await call_next(request)
313
+ state = proxy_state.for_request() if isinstance(proxy_state, ProxyState) else proxy_state
314
+ token = _request_state.set(state)
315
+ try:
316
+ return await call_next(request)
317
+ finally:
318
+ _request_state.reset(token)
319
+
320
+
321
+ async def install_process_asyncio_diagnostics() -> None:
322
+ """Capture unhandled background task errors in the active Uvicorn loop."""
323
+ if process_diagnostics is not None:
324
+ process_diagnostics.install_asyncio(asyncio.get_running_loop())
325
+ process_diagnostics.install_signal_observers()
326
+
327
+
328
+ app.router.add_event_handler("startup", install_process_asyncio_diagnostics)
329
+
330
+
331
+ async def start_account_checkin() -> None:
332
+ if isinstance(proxy_state, ProxyState) and proxy_state.account_manager is not None:
333
+ await proxy_state.account_manager.checkins.start()
334
+
335
+
336
+ app.router.add_event_handler("startup", start_account_checkin)
337
+
338
+ # Trae automation is opt-in because its local login is independent of WorkBuddy.
339
+ _trae_checkin_service = None
340
+
341
+
342
+ async def start_trae_checkin() -> None:
343
+ global _trae_checkin_service
344
+ native = os.environ.get("DAILYCHECKIN_TRAE_NATIVE") == "1"
345
+ creds = os.environ.get("DAILYCHECKIN_TRAE_CREDS") or None
346
+ if not native and not creds:
347
+ return
348
+ try:
349
+ from daily_checkin.trae_scheduler import create_service
350
+ service = await asyncio.to_thread(
351
+ create_service, native=native, creds=creds,
352
+ device_id_source=os.environ.get("DAILYCHECKIN_TRAE_DEVICE_ID_SOURCE", "get-device-id"))
353
+ if isinstance(proxy_state, ProxyState):
354
+ service.configure_logging(proxy_state.log_checkin)
355
+ await service.start()
356
+ _trae_checkin_service = service
357
+ except Exception:
358
+ logging.getLogger(__name__).warning(
359
+ "Trae automatic check-in could not start; check the configured account and device source")
360
+
361
+
362
+ async def close_trae_checkin() -> None:
363
+ global _trae_checkin_service
364
+ service, _trae_checkin_service = _trae_checkin_service, None
365
+ if service is not None:
366
+ await service.close()
367
+
368
+
369
+ app.router.add_event_handler("startup", start_trae_checkin)
370
+ app.router.add_event_handler("shutdown", close_trae_checkin)
371
+
372
+
373
+
374
+ async def close_account_manager() -> None:
375
+ if isinstance(proxy_state, ProxyState) and proxy_state.account_manager is not None:
376
+ await proxy_state.account_manager.checkins.close()
377
+ proxy_state.account_manager.close()
378
+
379
+
380
+ app.router.add_event_handler("shutdown", close_account_manager)
237
381
 
238
382
 
239
383
  def get_state() -> ProxyState:
@@ -242,6 +386,24 @@ def get_state() -> ProxyState:
242
386
  return proxy_state
243
387
 
244
388
 
389
+ def get_request_state():
390
+ pinned = _request_state.get()
391
+ if pinned is not None:
392
+ return pinned
393
+ state = get_state()
394
+ return state.for_request() if isinstance(state, ProxyState) else state
395
+
396
+
397
+ def get_account_manager():
398
+ manager = getattr(get_state(), "account_manager", None)
399
+ if manager is None:
400
+ raise HTTPException(status_code=503, detail="Account management is unavailable in this mode")
401
+ return manager
402
+
403
+
404
+ app.include_router(create_admin_router(get_account_manager))
405
+
406
+
245
407
  # ============================================================================
246
408
  # 辅助函数
247
409
  # ============================================================================
@@ -306,29 +468,29 @@ def diagnostic(event: str, **kwargs) -> None:
306
468
  # 远程配置缓存
307
469
  # ============================================================================
308
470
 
309
- class RemoteConfigCache:
310
- """远程配置缓存(带 TTL)- 匹配 VS Code 插件的逻辑"""
311
-
312
- def __init__(
313
- self,
314
- client: CodeBuddyClient | str | None = None,
315
- ttl: int = 300,
316
- *,
317
- url: str | None = None,
318
- ):
319
- # ``url=`` and a positional string were accepted by older callers.
320
- # Convert them once to a client, while all actual requests below use
321
- # the proxy state's resolved client so the base URL cannot diverge.
322
- if url is not None:
323
- if client is not None:
324
- raise TypeError("RemoteConfigCache 不能同时指定 client 和 url")
325
- client = url
326
- if isinstance(client, str):
327
- client = CodeBuddyClient(endpoint=client)
328
- self.client = client or CodeBuddyClient()
329
- self.ttl = ttl
330
- self._cache: Optional[dict[str, Any]] = None
331
- self._last_fetch: float = 0
471
+ class RemoteConfigCache:
472
+ """远程配置缓存(带 TTL)- 匹配 VS Code 插件的逻辑"""
473
+
474
+ def __init__(
475
+ self,
476
+ client: CodeBuddyClient | str | None = None,
477
+ ttl: int = 300,
478
+ *,
479
+ url: str | None = None,
480
+ ):
481
+ # ``url=`` and a positional string were accepted by older callers.
482
+ # Convert them once to a client, while all actual requests below use
483
+ # the proxy state's resolved client so the base URL cannot diverge.
484
+ if url is not None:
485
+ if client is not None:
486
+ raise TypeError("RemoteConfigCache 不能同时指定 client 和 url")
487
+ client = url
488
+ if isinstance(client, str):
489
+ client = CodeBuddyClient(endpoint=client)
490
+ self.client = client or CodeBuddyClient()
491
+ self.ttl = ttl
492
+ self._cache: Optional[dict[str, Any]] = None
493
+ self._last_fetch: float = 0
332
494
 
333
495
  async def get_config(self) -> dict[str, Any]:
334
496
  """获取配置(带缓存)
@@ -361,11 +523,11 @@ class RemoteConfigCache:
361
523
  diagnostic("remote_config_skipped", reason="no enterprise id", account_type=account.get("type", "unknown"))
362
524
  return {}
363
525
 
364
- # 企业版用户:调用动态 API
365
- try:
366
- # 构造 URL(匹配插件逻辑)
367
- path = state.client.enterprise_models_path(str(enterprise_id))
368
- full_url = state.client.build_url(path)
526
+ # 企业版用户:调用动态 API
527
+ try:
528
+ # 构造 URL(匹配插件逻辑)
529
+ path = state.client.enterprise_models_path(str(enterprise_id))
530
+ full_url = state.client.build_url(path)
369
531
 
370
532
  diagnostic("remote_config_fetch_attempt", url=full_url, enterprise_id=enterprise_id)
371
533
 
@@ -477,62 +639,62 @@ def build_model_list_static() -> list[dict[str, Any]]:
477
639
 
478
640
 
479
641
 
480
- def _profile_for_models(
481
- backend: BackendProfile | ResolvedBackend | None = None,
482
- ) -> BackendProfile:
483
- if isinstance(backend, ResolvedBackend):
484
- return backend.profile
485
- if isinstance(backend, BackendProfile):
486
- return backend
487
- if proxy_state is not None:
488
- return proxy_state.client.profile
489
- return DOMESTIC_PROFILE
490
-
491
-
492
- def _local_diagnostic(event: str, **kwargs: Any) -> None:
493
- """Emit diagnostics when the app is initialized, but stay unit-testable."""
494
-
495
- if proxy_state is not None and proxy_state.logger:
496
- diagnostic(event, **kwargs)
497
-
498
-
499
- def load_models_from_local_config(
500
- backend: BackendProfile | ResolvedBackend | None = None,
501
- ) -> list[dict[str, Any]]:
502
- """Load and normalize the selected profile's package model resource.
503
-
504
- A missing or malformed resource is an error. In particular, global mode
505
- must never silently expose the domestic model catalog.
506
- """
507
-
508
- profile = _profile_for_models(backend)
509
- resource_name = profile.models_resource
510
- try:
511
- resource = importlib.resources.files("codebuddy_proxy").joinpath(resource_name)
512
- with resource.open("r", encoding="utf-8") as stream:
513
- config_data = json.load(stream)
514
- models = config_data.get("models", [])
515
- if not isinstance(models, list):
516
- raise ValueError("models 字段必须是数组")
517
- normalized_models = [normalize_model_format(model) for model in models]
518
- except Exception as exc:
519
- _local_diagnostic(
520
- "local_config_error",
521
- backend=profile.key,
522
- resource=resource_name,
523
- error=str(exc),
524
- )
525
- raise CodeBuddyError(
526
- f"无法加载 backend={profile.key} 的模型资源 {resource_name}: {exc}"
527
- ) from exc
528
-
529
- _local_diagnostic(
530
- "local_config_loaded",
531
- backend=profile.key,
532
- resource=resource_name,
533
- models_count=len(normalized_models),
534
- )
535
- return normalized_models
642
+ def _profile_for_models(
643
+ backend: BackendProfile | ResolvedBackend | None = None,
644
+ ) -> BackendProfile:
645
+ if isinstance(backend, ResolvedBackend):
646
+ return backend.profile
647
+ if isinstance(backend, BackendProfile):
648
+ return backend
649
+ if proxy_state is not None:
650
+ return proxy_state.client.profile
651
+ return DOMESTIC_PROFILE
652
+
653
+
654
+ def _local_diagnostic(event: str, **kwargs: Any) -> None:
655
+ """Emit diagnostics when the app is initialized, but stay unit-testable."""
656
+
657
+ if proxy_state is not None and proxy_state.logger:
658
+ diagnostic(event, **kwargs)
659
+
660
+
661
+ def load_models_from_local_config(
662
+ backend: BackendProfile | ResolvedBackend | None = None,
663
+ ) -> list[dict[str, Any]]:
664
+ """Load and normalize the selected profile's package model resource.
665
+
666
+ A missing or malformed resource is an error. In particular, global mode
667
+ must never silently expose the domestic model catalog.
668
+ """
669
+
670
+ profile = _profile_for_models(backend)
671
+ resource_name = profile.models_resource
672
+ try:
673
+ resource = importlib.resources.files("codebuddy_proxy").joinpath(resource_name)
674
+ with resource.open("r", encoding="utf-8") as stream:
675
+ config_data = json.load(stream)
676
+ models = config_data.get("models", [])
677
+ if not isinstance(models, list):
678
+ raise ValueError("models 字段必须是数组")
679
+ normalized_models = [normalize_model_format(model) for model in models]
680
+ except Exception as exc:
681
+ _local_diagnostic(
682
+ "local_config_error",
683
+ backend=profile.key,
684
+ resource=resource_name,
685
+ error=str(exc),
686
+ )
687
+ raise CodeBuddyError(
688
+ f"无法加载 backend={profile.key} 的模型资源 {resource_name}: {exc}"
689
+ ) from exc
690
+
691
+ _local_diagnostic(
692
+ "local_config_loaded",
693
+ backend=profile.key,
694
+ resource=resource_name,
695
+ models_count=len(normalized_models),
696
+ )
697
+ return normalized_models
536
698
 
537
699
 
538
700
  def normalize_model_format(remote_model: dict[str, Any]) -> dict[str, Any]:
@@ -791,7 +953,8 @@ def log_upstream_response(protocol: str, text: str, **stats) -> None:
791
953
  common["content_preview"] = text[:200] if text else ""
792
954
 
793
955
  diagnostic("response", **common)
794
- state.write_log("stream_completed" if stats.get("stream") else "response",
956
+ event = f"stream_{stats.get('outcome', 'completed')}" if stats.get("stream") else "response"
957
+ state.write_log(event,
795
958
  **{k: v for k, v in common.items()
796
959
  if k not in ("content_preview",)})
797
960
 
@@ -805,14 +968,36 @@ async def health():
805
968
  state = get_state()
806
969
  auth = {} if state.mock_dir is not None else (state.client.session.get("auth") or {})
807
970
  expires = int(auth.get("expiresAt") or 0)
808
- return {
809
- "status": "ok",
810
- "authenticated": bool(auth.get("accessToken")),
811
- "token_valid": not expires or expires > int(time.time() * 1000),
812
- "uptime_seconds": int(time.time() - state.started_at),
813
- "backend": state.client.profile.key,
814
- "upstream_endpoint": state.client.endpoint,
815
- }
971
+ return {
972
+ "status": "ok",
973
+ "authenticated": bool(auth.get("accessToken")),
974
+ "token_valid": not expires or expires > int(time.time() * 1000),
975
+ "uptime_seconds": int(time.time() - state.started_at),
976
+ "backend": state.client.profile.key,
977
+ "upstream_endpoint": state.client.endpoint,
978
+ }
979
+
980
+
981
+ # Compatibility routes are implemented and maintained by DailyCheckin.
982
+ app.include_router(create_legacy_router(get_request_state, diagnostic))
983
+
984
+
985
+ # ============================================================================
986
+
987
+ @app.api_route("/v1/checkin-status", methods=["GET", "POST"])
988
+ async def checkin_status():
989
+ """Accept GET/POST locally; the real upstream billing status endpoint requires POST."""
990
+ return await forward_billing(
991
+ get_request_state(), "POST", "/v2/billing/meter/checkin-activity-status", diagnostic
992
+ )
993
+
994
+
995
+ @app.post("/v1/checkin")
996
+ async def checkin():
997
+ """POST daily check-in: preserve the upstream HTTP status and raw body."""
998
+ return await forward_billing(
999
+ get_request_state(), "POST", "/v2/billing/meter/daily-checkin", diagnostic
1000
+ )
816
1001
 
817
1002
 
818
1003
  # ============================================================================
@@ -820,18 +1005,18 @@ async def health():
820
1005
  # ============================================================================
821
1006
 
822
1007
  @app.get("/v1/models")
823
- async def list_models():
824
- state = get_state()
825
- # 始终从当前 backend 的包内资源读取;不访问远程动态配置。
826
- data = load_models_from_local_config(state.client.backend)
827
-
828
- # 记录模型列表请求
829
- diagnostic(
830
- "models_list_request",
831
- backend=state.client.profile.key,
832
- resource=state.client.profile.models_resource,
833
- models_count=len(data),
834
- source="local_config"
1008
+ async def list_models():
1009
+ state = get_state()
1010
+ # 始终从当前 backend 的包内资源读取;不访问远程动态配置。
1011
+ data = load_models_from_local_config(state.client.backend)
1012
+
1013
+ # 记录模型列表请求
1014
+ diagnostic(
1015
+ "models_list_request",
1016
+ backend=state.client.profile.key,
1017
+ resource=state.client.profile.models_resource,
1018
+ models_count=len(data),
1019
+ source="local_config"
835
1020
  )
836
1021
 
837
1022
  # 转换为 Codex 格式
@@ -881,7 +1066,7 @@ async def parse_request_body(request: Request) -> Any:
881
1066
 
882
1067
  @app.post("/v1/chat/completions")
883
1068
  async def chat_completions(request: Request):
884
- state = get_state()
1069
+ state = get_request_state()
885
1070
  body = await parse_request_body(request)
886
1071
 
887
1072
  log_client_request("POST", "/v1/chat/completions", body)
@@ -896,13 +1081,18 @@ async def chat_completions(request: Request):
896
1081
 
897
1082
  @app.post("/v1/responses")
898
1083
  async def create_response(request: Request):
899
- state = get_state()
1084
+ state = get_request_state()
900
1085
  body = await parse_request_body(request)
901
1086
 
902
1087
  log_client_request("POST", "/v1/responses", body)
903
1088
 
904
1089
  # 转换 Responses → Chat
905
- chat_body = responses_request_to_chat(body)
1090
+ try:
1091
+ chat_body = responses_request_to_chat(body)
1092
+ except ToolPairingError as exc:
1093
+ diagnostic("tool_pairing_rejected", protocol="responses", reason=exc.reason,
1094
+ call_count=len(exc.call_ids))
1095
+ raise HTTPException(status_code=422, detail=exc.as_detail()) from exc
906
1096
 
907
1097
  # 消息压缩优化(如果启用)
908
1098
  if state.enable_optimize_context and HAS_PROJECTION:
@@ -954,13 +1144,18 @@ async def create_response(request: Request):
954
1144
 
955
1145
  @app.post("/v1/messages")
956
1146
  async def create_message(request: Request):
957
- state = get_state()
1147
+ state = get_request_state()
958
1148
  body = await parse_request_body(request)
959
1149
 
960
1150
  log_client_request("POST", "/v1/messages", body)
961
1151
 
962
1152
  # 转换 Anthropic → Chat
963
- chat_body = anthropic_to_chat(body)
1153
+ try:
1154
+ chat_body = anthropic_to_chat(body)
1155
+ except ToolPairingError as exc:
1156
+ diagnostic("tool_pairing_rejected", protocol="anthropic", reason=exc.reason,
1157
+ call_count=len(exc.call_ids))
1158
+ raise HTTPException(status_code=422, detail=exc.as_detail()) from exc
964
1159
  diagnostic("request", protocol="anthropic", **body_summary(chat_body))
965
1160
 
966
1161
  return await forward_chat(chat_body, "anthropic", original=body)
@@ -988,58 +1183,176 @@ def _normalize_tool_choice(tool_choice: Any) -> Any:
988
1183
  return tool_choice
989
1184
 
990
1185
 
1186
+ def _retry_after_header(response) -> dict[str, str]:
1187
+ retry_after = getattr(response, "headers", {}).get("Retry-After")
1188
+ return {"Retry-After": retry_after} if retry_after is not None else {}
1189
+
1190
+
991
1191
  async def forward_chat(
992
1192
  body: dict[str, Any],
993
1193
  protocol: str,
994
1194
  original: dict[str, Any] | None = None
995
1195
  ) -> StreamingResponse | JSONResponse:
996
- """转发 chat 请求到 CodeBuddy 上游,支持流式和非流式。"""
997
- state = get_state()
998
- state.ensure_auth()
1196
+ """Forward with bounded account failover before sending a client response."""
1197
+ state = get_request_state()
1198
+ manager = getattr(state, "account_runtime", None)
1199
+ attempted: set[str] = set()
1200
+ max_attempts = 1
1201
+ if manager is not None:
1202
+ max_attempts = max(1, len(manager.snapshot()["accounts"]))
1203
+ for attempt in range(max_attempts):
1204
+ response = await _forward_chat_once(body, protocol, original, state)
1205
+ if response.status_code != 429:
1206
+ return response
1207
+ retry_state = await rate_limited_account(
1208
+ state, response.headers.get("Retry-After"), attempted, model=body.get("model"),
1209
+ )
1210
+ if retry_state is None or attempt + 1 == max_attempts:
1211
+ return response
1212
+ state = retry_state
1213
+ return response
1214
+
1215
+
1216
+ async def rate_limited_account(state, retry_after, attempted, *, model):
1217
+ """Record the original identity; late failures cannot override manual switches."""
1218
+ manager = getattr(state, "account_runtime", None)
1219
+ if manager is None:
1220
+ return None
1221
+ try:
1222
+ account_id = manager.account_id_for_client(state.client)
1223
+ if account_id is not None:
1224
+ attempted.add(account_id)
1225
+ candidate = await asyncio.to_thread(
1226
+ manager.handle_rate_limit, state.client, retry_after,
1227
+ model=model, excluded_account_ids=set(attempted),
1228
+ )
1229
+ next_id = manager.account_id_for_client(candidate) if candidate is not None else None
1230
+ state.write_log(
1231
+ "account_rate_limited", account_id=account_id, model=model, switched_to=next_id,
1232
+ )
1233
+ if candidate is None or next_id is None or next_id in attempted:
1234
+ return None
1235
+ retry_state = copy.copy(state)
1236
+ retry_state._client = candidate
1237
+ retry_state.account_manager = None
1238
+ return retry_state
1239
+ except Exception as exc:
1240
+ # Persistence or selection failures must not hide the provider's 429.
1241
+ state.write_log("account_rate_limit_record_failed", error_type=type(exc).__name__)
1242
+ return None
1243
+
1244
+
1245
+ async def _forward_chat_once(body, protocol, original, state):
1246
+ """One account attempt; any returned stream has already passed HTTP preflight."""
1247
+ if isinstance(state, ProxyState) and state.managed_auth:
1248
+ await asyncio.to_thread(state.ensure_auth)
1249
+ else:
1250
+ state.ensure_auth()
999
1251
 
1000
1252
  diagnostic("upstream_request", protocol=protocol, **body_summary(body))
1001
1253
 
1002
- stream = bool(body.get("stream"))
1003
- upstream_body = dict(body)
1004
-
1005
- # 归一化 tool_choice:object 形式 → 函数名字符串(上游只接受 string)
1006
- if "tool_choice" in upstream_body:
1007
- upstream_body["tool_choice"] = _normalize_tool_choice(upstream_body["tool_choice"])
1008
-
1009
- # 应用脱敏处理
1010
- if state.enable_desensitize:
1011
- upstream_body = desensitize_body(upstream_body, compact_harness=True)
1012
-
1013
- # 始终以流式方式请求上游(聚合或转发)
1014
- upstream_body["stream"] = True
1015
- upstream_body.setdefault("stream_options", {"include_usage": True})
1016
-
1017
-
1018
- url = state.client.build_url(state.client.profile.chat_path)
1019
- headers = {
1020
- "User-Agent": "Mozilla/5.0 (compatible; Genie-IDE/1.0)",
1021
- **state.client.auth_headers(),
1022
- "Content-Type": "application/json",
1023
- "Accept": "text/event-stream",
1024
- }
1025
-
1026
- # 🔍 调试:输出实际发送的IDE识别headers
1254
+ prepared = ChatRequestPreparer(
1255
+ client=state.client,
1256
+ enable_desensitize=state.enable_desensitize,
1257
+ desensitize_body=desensitize_body,
1258
+ normalize_tool_choice=_normalize_tool_choice,
1259
+ diagnostic=diagnostic,
1260
+ ).prepare(body, protocol)
1261
+ stream, upstream_body, url, headers = (
1262
+ prepared.stream, prepared.body, prepared.url, prepared.headers,
1263
+ )
1264
+
1265
+ # Keep the original diagnostics after gateway-specific header injection.
1027
1266
  if state.logger:
1028
- ide_headers = {k: v for k, v in headers.items()
1029
- if k.startswith("X-IDE-") or k == "X-Product-Version" or k == "X-Machine-Id"}
1267
+ ide_headers = {k: v for k, v in headers.items()
1268
+ if k.startswith("X-IDE-") or k == "X-Product-Version" or k == "X-Machine-Id"}
1030
1269
  diagnostic("upstream_ide_headers", **ide_headers)
1031
-
1270
+
1032
1271
  if stream:
1033
- # 流式:直接转发
1272
+ # 上游响应头必须在客户端 StreamingResponse 开始之前读取,否则 400 会被包装成 200。
1273
+ upstream_client = httpx.AsyncClient(timeout=httpx.Timeout(30.0, read=300.0))
1274
+ preflight_request_id = uuid.uuid4().hex
1275
+
1276
+ async def open_preflight_response():
1277
+ # Build a fresh request for each connect attempt; a request that
1278
+ # received headers (including 4xx) must never be replayed.
1279
+ upstream_request = upstream_client.build_request(
1280
+ "POST", url, headers=headers, json=upstream_body
1281
+ )
1282
+ return await upstream_client.send(upstream_request, stream=True)
1283
+
1284
+ try:
1285
+ upstream_response = await retry_upstream_connection(
1286
+ open_preflight_response, url, protocol, preflight_request_id,
1287
+ )
1288
+ except httpx.HTTPError as exc:
1289
+ await upstream_client.aclose()
1290
+ status = 504 if isinstance(exc, httpx.TimeoutException) else 502
1291
+ diagnostic("upstream_connection_error", protocol=protocol, status=status, error=str(exc))
1292
+ return JSONResponse(
1293
+ status_code=status,
1294
+ content={"error": {
1295
+ "type": "upstream_error",
1296
+ "message": f"Upstream connection failed: {exc}",
1297
+ "code": status,
1298
+ }},
1299
+ )
1300
+ except BaseException:
1301
+ await upstream_client.aclose()
1302
+ raise
1303
+
1304
+ if upstream_response.status_code != 200:
1305
+ try:
1306
+ error_body = await upstream_response.aread()
1307
+ error_text = error_body.decode("utf-8", errors="replace")
1308
+ diagnostic("upstream_error", protocol=protocol,
1309
+ status=upstream_response.status_code, detail=error_text[:500])
1310
+ try:
1311
+ error_json = json.loads(error_text)
1312
+ except (ValueError, TypeError):
1313
+ error_json = {}
1314
+ upstream_code = error_json.get("code") if isinstance(error_json, dict) else None
1315
+ error_message = (
1316
+ error_json.get("msg") or error_json.get("message") or error_text
1317
+ if isinstance(error_json, dict) else error_text
1318
+ )
1319
+ error_info = {
1320
+ "type": "upstream_error",
1321
+ "message": str(error_message)[:1000],
1322
+ "code": upstream_code or upstream_response.status_code,
1323
+ }
1324
+ error_payload = (
1325
+ {"type": "error", "error": error_info}
1326
+ if protocol == "anthropic" else {"error": error_info}
1327
+ )
1328
+ return JSONResponse(
1329
+ status_code=upstream_response.status_code, content=error_payload,
1330
+ headers=_retry_after_header(upstream_response),
1331
+ )
1332
+ finally:
1333
+ await upstream_response.aclose()
1334
+ await upstream_client.aclose()
1335
+
1034
1336
  return StreamingResponse(
1035
- stream_upstream(url, headers, upstream_body, protocol, original),
1337
+ stream_upstream(
1338
+ url, headers, upstream_body, protocol, original,
1339
+ prepared_upstream=(upstream_client, upstream_response),
1340
+ request_id=preflight_request_id,
1341
+ ),
1036
1342
  media_type="text/event-stream",
1037
- headers={"Cache-Control": "no-cache", "Connection": "close"}
1343
+ headers={"Cache-Control": "no-cache", "Connection": "close"},
1038
1344
  )
1039
- else:
1040
- # 非流式:聚合后返回
1345
+
1346
+ # 非流式:聚合后返回
1347
+ try:
1041
1348
  collected = await collect_upstream(url, headers, upstream_body, protocol)
1042
- return JSONResponse(content=convert_nonstream(collected, protocol, original))
1349
+ except HTTPException as exc:
1350
+ if exc.status_code != 429:
1351
+ raise
1352
+ return JSONResponse(
1353
+ status_code=429, content={"detail": exc.detail}, headers=exc.headers,
1354
+ )
1355
+ return JSONResponse(content=convert_nonstream(collected, protocol, original))
1043
1356
 
1044
1357
 
1045
1358
  # ============================================================================
@@ -1065,12 +1378,99 @@ def _build_openai_flush_chunk(residual: str, last_chunk_id: str,
1065
1378
  }
1066
1379
 
1067
1380
 
1381
+ def exception_details(exc: BaseException) -> dict[str, Any]:
1382
+ """Keep bounded cause/group details, including native socket error codes."""
1383
+ chain: list[dict[str, Any]] = []
1384
+ seen: set[int] = set()
1385
+
1386
+ def visit(error: BaseException, parent: int | None = None, relation: str = "root") -> None:
1387
+ if id(error) in seen or len(chain) >= 16:
1388
+ return
1389
+ seen.add(id(error))
1390
+ index = len(chain)
1391
+ item: dict[str, Any] = {
1392
+ "type": type(error).__name__, "message": str(error)[:1000],
1393
+ "parent": parent, "relation": relation,
1394
+ }
1395
+ for key in ("errno", "winerror"):
1396
+ value = getattr(error, key, None)
1397
+ if isinstance(value, int):
1398
+ item[key] = value
1399
+ chain.append(item)
1400
+ cause = error.__cause__
1401
+ if cause is not None:
1402
+ visit(cause, index, "cause")
1403
+ # HTTPX/HTTPCore can suppress a context while mapping transport errors.
1404
+ # It still contains the original socket error and is needed for diagnostics.
1405
+ if error.__context__ is not None:
1406
+ relation = "suppressed_context" if error.__suppress_context__ else "context"
1407
+ visit(error.__context__, index, relation)
1408
+ # getattr also supports AnyIO's exception-group backport on Python 3.10.
1409
+ for child in getattr(error, "exceptions", ()):
1410
+ if isinstance(child, BaseException):
1411
+ visit(child, index, "group_member")
1412
+
1413
+ visit(exc)
1414
+ return {"error_type": type(exc).__name__, "exception_chain": chain}
1415
+
1416
+
1417
+ async def retry_upstream_connection(open_response, url, protocol, request_id):
1418
+ """Retry only failures to establish a connection, before any HTTP response."""
1419
+ state = get_state()
1420
+ target = httpx.URL(url)
1421
+ max_attempts = 3
1422
+ for attempt in range(1, max_attempts + 1):
1423
+ try:
1424
+ return await open_response()
1425
+ except (httpx.ConnectError, httpx.ConnectTimeout) as exc:
1426
+ retrying = attempt < max_attempts
1427
+ delay = 0.5 * 2 ** (attempt - 1) if retrying else 0
1428
+ event = "upstream_connect_retry" if retrying else "upstream_connect_failed"
1429
+ details = {
1430
+ "protocol": protocol, "request_id": request_id,
1431
+ "upstream_host": target.host,
1432
+ "upstream_port": target.port or (443 if target.scheme == "https" else 80),
1433
+ "attempt": attempt, "max_attempts": max_attempts,
1434
+ "retry_delay": delay, "error": str(exc), **exception_details(exc),
1435
+ }
1436
+ diagnostic(event, **details)
1437
+ state.write_log(event, **details)
1438
+ if not retrying:
1439
+ raise
1440
+ await asyncio.sleep(delay)
1441
+
1442
+
1443
+ @asynccontextmanager
1444
+ async def open_upstream_stream(client, url, headers, body, protocol, request_id):
1445
+ """Open the stream with bounded connect retries; never replay a response."""
1446
+ async with AsyncExitStack() as stack:
1447
+ response = await retry_upstream_connection(
1448
+ lambda: stack.enter_async_context(
1449
+ client.stream("POST", url, headers=headers, json=body)
1450
+ ),
1451
+ url, protocol, request_id,
1452
+ )
1453
+ yield response
1454
+
1455
+
1456
+ @asynccontextmanager
1457
+ async def _prepared_upstream_response(client: httpx.AsyncClient, response: httpx.Response):
1458
+ """Take ownership of the already-open HTTP response and client."""
1459
+ try:
1460
+ yield response
1461
+ finally:
1462
+ await response.aclose()
1463
+ await client.aclose()
1464
+
1465
+
1068
1466
  async def stream_upstream(
1069
1467
  url: str,
1070
1468
  headers: dict[str, str],
1071
1469
  body: dict[str, Any],
1072
1470
  protocol: str,
1073
- original: dict[str, Any] | None
1471
+ original: dict[str, Any] | None,
1472
+ prepared_upstream: tuple[httpx.AsyncClient, httpx.Response] | None = None,
1473
+ request_id: str | None = None,
1074
1474
  ):
1075
1475
  """异步流式转发上游响应到客户端。
1076
1476
 
@@ -1081,10 +1481,13 @@ async def stream_upstream(
1081
1481
  """
1082
1482
  state = get_state()
1083
1483
  stream_start_time = time.time()
1484
+ request_id = request_id or uuid.uuid4().hex
1485
+ outcome = "cancelled"
1084
1486
 
1085
1487
  # 【日志】流开始
1086
- state.write_log("stream_started", protocol=protocol, timestamp=stream_start_time)
1087
- diagnostic("stream_started", protocol=protocol)
1488
+ state.write_log("stream_started", protocol=protocol, timestamp=stream_start_time,
1489
+ request_id=request_id)
1490
+ diagnostic("stream_started", protocol=protocol, request_id=request_id)
1088
1491
 
1089
1492
 
1090
1493
  response_id = "resp_" + uuid.uuid4().hex
@@ -1106,7 +1509,7 @@ async def stream_upstream(
1106
1509
  native_tool_name_by_index: dict[int, str] = {}
1107
1510
 
1108
1511
  # 【修复】跟踪上游实际状态码,避免finally块硬编码status=200导致日志误导
1109
- actual_status = 200
1512
+ actual_status = None
1110
1513
  response_text = ""
1111
1514
  response_text_started = False
1112
1515
  chunk_count = 0
@@ -1120,13 +1523,17 @@ async def stream_upstream(
1120
1523
  last_chunk_created: int = 0
1121
1524
  last_chunk_model: str = ""
1122
1525
  try:
1123
- # 异步HTTP客户端:timeout=None 依赖TCP超时
1124
- # 使用合理的超时配置:连接超时30s,读取超时300s
1526
+ # Existing direct stream_upstream callers remain supported. The HTTP
1527
+ # endpoint supplies a pre-opened response to preserve upstream status.
1125
1528
  timeout_config = httpx.Timeout(30.0, read=300.0)
1126
- async with httpx.AsyncClient(timeout=timeout_config) as client:
1127
- async with client.stream("POST", url, headers=headers, json=body) as resp:
1529
+ async with (nullcontext() if prepared_upstream else httpx.AsyncClient(timeout=timeout_config)) as client:
1530
+ async with (
1531
+ _prepared_upstream_response(*prepared_upstream)
1532
+ if prepared_upstream else open_upstream_stream(client, url, headers, body, protocol, request_id)
1533
+ ) as resp:
1534
+ actual_status = resp.status_code
1128
1535
  if resp.status_code != 200:
1129
- actual_status = resp.status_code # 【修复】记录实际状态码
1536
+ outcome = "failed"
1130
1537
  error_body = await resp.aread()
1131
1538
  error_text = error_body.decode("utf-8", "replace")
1132
1539
 
@@ -1168,6 +1575,7 @@ async def stream_upstream(
1168
1575
 
1169
1576
  return
1170
1577
 
1578
+ outcome = "incomplete"
1171
1579
  diagnostic("upstream_response", protocol=protocol, status=resp.status_code)
1172
1580
 
1173
1581
  # 【配置】超时策略 - 双重超时机制
@@ -1242,7 +1650,31 @@ async def stream_upstream(
1242
1650
  chunk = json.loads(data)
1243
1651
  except json.JSONDecodeError:
1244
1652
  continue
1245
-
1653
+
1654
+ if not isinstance(chunk, dict):
1655
+ continue
1656
+ if chunk.get("error"):
1657
+ upstream_error = chunk["error"]
1658
+ if isinstance(upstream_error, dict):
1659
+ message = str(upstream_error.get("message") or upstream_error)
1660
+ else:
1661
+ message = str(upstream_error)
1662
+ diagnostic("upstream_stream_error_event", protocol=protocol,
1663
+ chunks=chunk_count)
1664
+ if protocol == "responses" and responses_state:
1665
+ for event_name, event_data in responses_state.fail(
1666
+ "upstream_stream_error", message
1667
+ ):
1668
+ yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1669
+ elif protocol == "anthropic":
1670
+ payload = {"type": "error", "error": {
1671
+ "type": "api_error", "message": message
1672
+ }}
1673
+ yield f"event: error\ndata: {json.dumps(payload, ensure_ascii=False)}\n\n".encode()
1674
+ else:
1675
+ yield f"data: {json.dumps({'error': upstream_error}, ensure_ascii=False)}\n\n".encode()
1676
+ return
1677
+
1246
1678
  chunk_count += 1
1247
1679
 
1248
1680
  # 【修复 C3】记录最后一个上游 chunk 的元数据,供流结束 flush 复用
@@ -1425,6 +1857,20 @@ async def stream_upstream(
1425
1857
  for event_name, event_data in events:
1426
1858
  yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1427
1859
 
1860
+ # 未看到上游 [DONE]:不能把断流/超时伪装成成功完成。
1861
+ if not done_seen:
1862
+ diagnostic("upstream_stream_incomplete", protocol=protocol, chunks=chunk_count)
1863
+ if protocol == "responses" and responses_state:
1864
+ for event_name, event_data in responses_state.fail(
1865
+ "upstream_stream_incomplete", "Upstream stream ended without [DONE]"
1866
+ ):
1867
+ yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1868
+ elif protocol == "anthropic":
1869
+ yield b'event: error\ndata: {"type":"error","error":{"type":"api_error","message":"Upstream stream ended without [DONE]"}}\n\n'
1870
+ else:
1871
+ yield b'data: {"error":{"type":"upstream_error","message":"Upstream stream ended without [DONE]"}}\n\n'
1872
+ return
1873
+
1428
1874
  # 发送结束事件
1429
1875
  if protocol == "responses" and responses_state:
1430
1876
  # 【修复】流结束前强制刷新 DSML 缓冲区残留内容
@@ -1467,53 +1913,51 @@ async def stream_upstream(
1467
1913
  )
1468
1914
  yield f"data: {json.dumps(final_chunk, ensure_ascii=False)}\n\n".encode()
1469
1915
  yield b"data: [DONE]\n\n"
1470
-
1916
+
1917
+ outcome = "completed" if done_seen else "incomplete"
1918
+
1471
1919
  except httpx.TimeoutException as exc:
1920
+ outcome = "failed"
1472
1921
  # 【日志】超时
1922
+ details = {"request_id": request_id, **exception_details(exc)}
1473
1923
  diagnostic("stream_timeout", protocol=protocol, chunks=chunk_count,
1474
- elapsed=round(time.time() - stream_start_time, 2), error=str(exc))
1475
- state.write_log("stream_timeout", protocol=protocol, chunks=chunk_count, error=str(exc))
1924
+ elapsed=round(time.time() - stream_start_time, 2), error=str(exc), **details)
1925
+ state.write_log("stream_timeout", protocol=protocol, chunks=chunk_count,
1926
+ error=str(exc), **details)
1476
1927
 
1477
- # 【防御性编程】确保发出完整的事件序列
1928
+ error_msg = f"stream timeout after {chunk_count} chunks"
1478
1929
  if protocol == "responses" and responses_state:
1479
- for event_name, event_data in responses_state.finish():
1930
+ for event_name, event_data in responses_state.fail("upstream_timeout", error_msg):
1480
1931
  yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1481
- elif protocol == "anthropic" and anthropic_state:
1482
- for event_name, event_data in anthropic_state.finish():
1483
- yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1484
-
1485
- # 发送错误事件
1486
- error_chunk = {
1487
- "error": {
1488
- "message": f"stream timeout after {chunk_count} chunks",
1489
- "type": "timeout_error"
1490
- }
1491
- }
1492
- yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n".encode()
1932
+ elif protocol == "anthropic":
1933
+ payload = {"type": "error", "error": {"type": "api_error", "message": error_msg}}
1934
+ yield f"event: error\ndata: {json.dumps(payload)}\n\n".encode()
1935
+ else:
1936
+ yield f"data: {json.dumps({'error': {'type': 'timeout_error', 'message': error_msg}})}\n\n".encode()
1493
1937
 
1494
1938
  except Exception as exc:
1939
+ outcome = "failed"
1495
1940
  # 【日志】其他错误
1941
+ details = {"request_id": request_id, **exception_details(exc)}
1496
1942
  diagnostic("stream_error", protocol=protocol, chunks=chunk_count,
1497
- elapsed=round(time.time() - stream_start_time, 2), error=str(exc))
1498
- state.write_log("stream_error", protocol=protocol, chunks=chunk_count, error=str(exc))
1943
+ elapsed=round(time.time() - stream_start_time, 2), error=str(exc), **details)
1944
+ state.write_log("stream_error", protocol=protocol, chunks=chunk_count,
1945
+ error=str(exc), **details)
1499
1946
 
1500
- # 【防御性编程】确保发出完整的事件序列
1947
+ error_msg = f"Upstream stream error: {exc}"
1501
1948
  if protocol == "responses" and responses_state:
1502
- for event_name, event_data in responses_state.finish():
1503
- yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1504
- elif protocol == "anthropic" and anthropic_state:
1505
- for event_name, event_data in anthropic_state.finish():
1949
+ for event_name, event_data in responses_state.fail("upstream_stream_error", error_msg):
1506
1950
  yield f"event: {event_name}\ndata: {json.dumps(event_data, ensure_ascii=False)}\n\n".encode()
1507
-
1508
- # 发送错误事件
1509
- error_chunk = {
1510
- "error": {
1511
- "message": f"stream error: {exc}",
1512
- "type": "internal_error"
1513
- }
1514
- }
1515
- yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n".encode()
1951
+ elif protocol == "anthropic":
1952
+ payload = {"type": "error", "error": {"type": "api_error", "message": error_msg}}
1953
+ yield f"event: error\ndata: {json.dumps(payload)}\n\n".encode()
1954
+ else:
1955
+ yield f"data: {json.dumps({'error': {'type': 'upstream_error', 'message': error_msg}})}\n\n".encode()
1516
1956
 
1957
+ except (asyncio.CancelledError, GeneratorExit):
1958
+ outcome = "cancelled"
1959
+ raise
1960
+
1517
1961
  finally:
1518
1962
  # 【日志】流完成
1519
1963
  if state.verbose_llm:
@@ -1530,7 +1974,8 @@ async def stream_upstream(
1530
1974
 
1531
1975
  log_upstream_response(protocol, logged_text, stream=True,
1532
1976
  chunk_count=chunk_count, duration=stream_duration,
1533
- upstream_done=done_seen)
1977
+ upstream_done=done_seen, outcome=outcome,
1978
+ request_id=request_id, status=actual_status)
1534
1979
 
1535
1980
 
1536
1981
  # ============================================================================
@@ -1545,6 +1990,7 @@ async def collect_upstream(
1545
1990
  ) -> dict[str, Any]:
1546
1991
  """聚合上游流式响应为单个 JSON 对象(非流式场景)。"""
1547
1992
  state = get_state()
1993
+ request_id = uuid.uuid4().hex
1548
1994
 
1549
1995
  usage = None
1550
1996
  finish_reason = None
@@ -1557,12 +2003,13 @@ async def collect_upstream(
1557
2003
  # 使用合理的超时配置:连接超时30s,读取超时300s
1558
2004
  timeout_config = httpx.Timeout(30.0, read=300.0)
1559
2005
  async with httpx.AsyncClient(timeout=timeout_config) as client:
1560
- async with client.stream("POST", url, headers=headers, json=body) as resp:
2006
+ async with open_upstream_stream(client, url, headers, body, protocol, request_id) as resp:
1561
2007
  if resp.status_code != 200:
1562
2008
  error_body = await resp.aread()
1563
2009
  raise HTTPException(
1564
2010
  status_code=resp.status_code,
1565
- detail={"error": {"message": error_body.decode("utf-8", "replace")[:500], "type": "upstream_error"}}
2011
+ detail={"error": {"message": error_body.decode("utf-8", "replace")[:500], "type": "upstream_error"}},
2012
+ headers=_retry_after_header(resp),
1566
2013
  )
1567
2014
 
1568
2015
  async for line in resp.aiter_lines():
@@ -1628,7 +2075,10 @@ async def collect_upstream(
1628
2075
  tool_calls_dict[idx]["function"]["arguments"] += tc["function"]["arguments"]
1629
2076
 
1630
2077
  except httpx.HTTPError as exc:
1631
- diagnostic("upstream_error", protocol=protocol, error=str(exc))
2078
+ details = {"protocol": protocol, "request_id": request_id,
2079
+ "error": str(exc), **exception_details(exc)}
2080
+ diagnostic("upstream_error", **details)
2081
+ state.write_log("upstream_error", **details)
1632
2082
  raise HTTPException(status_code=502, detail={"error": {"message": f"upstream error: {exc}", "type": "upstream_error"}})
1633
2083
 
1634
2084
  # 转换为 list 并过滤掉无效的 tool_calls(name 为空的)
@@ -1647,7 +2097,7 @@ async def collect_upstream(
1647
2097
  # collect_upstream 没有保存原始响应,只记录聚合后的内容
1648
2098
  pass
1649
2099
 
1650
- log_upstream_response(protocol, content, stream=False)
2100
+ log_upstream_response(protocol, content, stream=False, request_id=request_id)
1651
2101
  return {
1652
2102
  "id": "chatcmpl-" + uuid.uuid4().hex,
1653
2103
  "object": "chat.completion",
@@ -1671,85 +2121,15 @@ async def collect_upstream(
1671
2121
  # ============================================================================
1672
2122
 
1673
2123
  def convert_nonstream(data: dict[str, Any], protocol: str, original: dict[str, Any] | None) -> dict[str, Any]:
1674
- """将聚合的 OpenAI 格式转换为目标协议格式。"""
1675
- if protocol == "openai":
1676
- return data
1677
-
1678
- choice = (data.get("choices") or [{}])[0]
1679
- message = choice.get("message") or {}
1680
- content = message.get("content", "")
1681
-
1682
- if protocol == "anthropic":
1683
- content_blocks = []
1684
- if content:
1685
- content_blocks.append({"type": "text", "text": content})
1686
- for call in message.get("tool_calls") or []:
1687
- fn = call.get("function") or {}
1688
- try:
1689
- arguments = json.loads(fn.get("arguments", "{}"))
1690
- except json.JSONDecodeError:
1691
- arguments = fn.get("arguments", "")
1692
- content_blocks.append({
1693
- "type": "tool_use",
1694
- "id": call.get("id", ""),
1695
- "name": fn.get("name", ""),
1696
- "input": arguments
1697
- })
1698
- return {
1699
- "id": "msg_" + uuid.uuid4().hex,
1700
- "type": "message",
1701
- "role": "assistant",
1702
- "model": (original or {}).get("model", data.get("model", "default")),
1703
- "content": content_blocks,
1704
- "stop_reason": "tool_use" if message.get("tool_calls") else "end_turn",
1705
- "stop_sequence": None,
1706
- "usage": {
1707
- "input_tokens": (data.get("usage") or {}).get("prompt_tokens", 0),
1708
- "output_tokens": (data.get("usage") or {}).get("completion_tokens", 0)
1709
- }
1710
- }
1711
-
1712
- elif protocol == "responses":
1713
- # 构建 content 数组(文本 + 工具调用)
1714
- content_parts = []
1715
- if content:
1716
- content_parts.append({"type": "output_text", "text": content})
1717
-
1718
- # 处理工具调用
1719
- for call in message.get("tool_calls") or []:
1720
- fn = call.get("function") or {}
1721
- try:
1722
- arguments = json.loads(fn.get("arguments", "{}"))
1723
- except json.JSONDecodeError:
1724
- arguments = fn.get("arguments", "")
1725
-
1726
- content_parts.append({
1727
- "type": "function_call",
1728
- "id": call.get("id", ""),
1729
- "name": fn.get("name", ""),
1730
- "arguments": arguments
1731
- })
1732
-
1733
- return {
1734
- "id": "resp_" + uuid.uuid4().hex,
1735
- "object": "response",
1736
- "created_at": now_s(),
1737
- "status": "completed",
1738
- "output": [{
1739
- "type": "message",
1740
- "role": "assistant",
1741
- "content": content_parts
1742
- }]
1743
- }
1744
-
1745
- return data
2124
+ """Convert a collected response via the protocol's registered formatter."""
2125
+ return format_nonstream_response(data, protocol, original, now_s)
1746
2126
 
1747
2127
 
1748
- # ============================================================================
1749
- # 启动
1750
- # ============================================================================
1751
-
1752
- def run_http_server(*, host: str, port: int, windows_loop: str = "selector") -> None:
2128
+ # ============================================================================
2129
+ # 启动
2130
+ # ============================================================================
2131
+
2132
+ def run_http_server(*, host: str, port: int, windows_loop: str = "selector") -> None:
1753
2133
  """Avoid Windows Proactor's noisy WinError 64 accept failures.
1754
2134
 
1755
2135
  Newer Uvicorn versions choose a Proactor loop explicitly, ignoring the
@@ -1765,95 +2145,175 @@ def run_http_server(*, host: str, port: int, windows_loop: str = "selector") ->
1765
2145
  uvicorn.run(app, **options)
1766
2146
 
1767
2147
 
1768
- def build_parser() -> argparse.ArgumentParser:
1769
- parser = argparse.ArgumentParser(description="CodeBuddy local API proxy")
1770
- parser.add_argument("--host", default=os.getenv("CODEBUDDY_PROXY_HOST", "127.0.0.1"),
1771
- help="监听地址")
1772
- parser.add_argument("--port", type=int, default=int(os.getenv("CODEBUDDY_PROXY_PORT", "8787")),
1773
- help="监听端口")
1774
- parser.add_argument(
2148
+ def build_parser() -> argparse.ArgumentParser:
2149
+ parser = argparse.ArgumentParser(description="CodeBuddy local API proxy")
2150
+ parser.add_argument("--host", default=os.getenv("CODEBUDDY_PROXY_HOST", "127.0.0.1"),
2151
+ help="监听地址")
2152
+ parser.add_argument("--port", type=int, default=int(os.getenv("CODEBUDDY_PROXY_PORT", "8787")),
2153
+ help="监听端口")
2154
+ parser.add_argument(
1775
2155
  "--windows-loop",
1776
2156
  choices=("selector", "proactor"),
1777
2157
  default="selector",
1778
2158
  help="仅 Windows 生效:默认 selector 避免 WinError 64;可选 proactor 保留原行为",
1779
2159
  )
1780
- parser.add_argument(
1781
- "--global",
1782
- dest="global_mode",
1783
- action="store_true",
1784
- help="使用国际版 CodeBuddy(默认使用国内版)",
1785
- )
1786
- parser.add_argument(
1787
- "--endpoint",
1788
- default=None,
1789
- help="CodeBuddy 后端地址;显式值优先于 profile 默认值和 CODEBUDDY_ENDPOINT",
1790
- )
1791
- parser.add_argument("--session-file", type=pathlib.Path,
1792
- help="会话文件路径(默认按 backend profile 隔离)")
1793
- parser.add_argument("--mock-dir", type=pathlib.Path,
1794
- help="只使用指定目录中的真实响应 fixture,不访问 CodeBuddy 后端")
1795
- default_log_file = pathlib.Path(
1796
- os.getenv(
1797
- "CODEBUDDY_PROXY_LOG_FILE",
1798
- str(pathlib.Path.home() / ".workbuddy2api" / "codebuddy-proxy.jsonl"),
1799
- )
1800
- ).expanduser()
1801
- parser.add_argument(
1802
- "--log-file",
1803
- type=pathlib.Path,
1804
- default=default_log_file,
1805
- help="记录完整请求/响应的 JSONL 文件(默认 ~/.workbuddy2api/codebuddy-proxy.jsonl)",
1806
- )
1807
- parser.add_argument("--desensitize", action="store_true",
1808
- help="启用脱敏处理,对 system 消息中的敏感词插入零宽空格(缓解审核误拦)")
1809
- parser.add_argument("--optimize-context", action="store_true",
1810
- help="启用消息压缩优化(仅 /v1/responses),大幅减少 token 使用(适用于 Codex CLI 等长上下文场景)")
1811
- parser.add_argument("--login", action="store_true",
1812
- help="启动时执行浏览器登录/账户查询")
1813
- parser.add_argument("--no-browser", action="store_true",
1814
- help="登录时不自动打开浏览器")
1815
- parser.add_argument("--verbose-llm", action="store_true",
1816
- help="log full LLM request/response content (default: summary only, saves 98%% space)")
1817
- parser.add_argument(
1818
- "--static-models",
1819
- action="store_true",
1820
- help="兼容旧参数;/v1/models 当前始终使用 profile 对应的本地模型资源",
1821
- )
1822
- parser.add_argument(
1823
- "--config-cache-ttl",
1824
- type=int,
1825
- default=int(os.getenv("CODEBUDDY_CONFIG_CACHE_TTL", "300")),
1826
- help="兼容旧参数;当前 /v1/models 不启用远程动态模型",
1827
- )
1828
- return parser
1829
-
1830
-
1831
- def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
1832
- args = build_parser().parse_args(argv)
1833
- args.log_file = args.log_file.expanduser()
1834
- return args
1835
-
1836
-
1837
- def main():
1838
- global proxy_state
1839
-
1840
- args = parse_args()
1841
- try:
1842
- resolved_backend = resolve_backend_from_args(args)
1843
- client = CodeBuddyClient(backend=resolved_backend)
1844
- except (CodeBuddyError, ValueError) as exc:
1845
- print(f"错误: {exc}", file=sys.stderr)
1846
- return 1
2160
+ parser.add_argument(
2161
+ "--global",
2162
+ dest="global_mode",
2163
+ action="store_true",
2164
+ help="使用国际版 CodeBuddy(默认使用国内版)",
2165
+ )
2166
+ parser.add_argument(
2167
+ "--endpoint",
2168
+ default=None,
2169
+ help="CodeBuddy 后端地址;显式值优先于 profile 默认值和 CODEBUDDY_ENDPOINT",
2170
+ )
2171
+ parser.add_argument("--session-file", type=pathlib.Path,
2172
+ help="会话文件路径(默认按 backend profile 隔离)")
2173
+ parser.add_argument(
2174
+ "--accounts-dir", type=pathlib.Path,
2175
+ default=pathlib.Path.home() / ".workbuddy2api" / "accounts",
2176
+ help="独立账号库目录;管理页位于 /admin",
2177
+ )
2178
+ parser.add_argument("--mock-dir", type=pathlib.Path,
2179
+ help="只使用指定目录中的真实响应 fixture,不访问 CodeBuddy 后端")
2180
+ default_log_file = pathlib.Path(
2181
+ os.getenv(
2182
+ "CODEBUDDY_PROXY_LOG_FILE",
2183
+ str(pathlib.Path.home() / ".workbuddy2api" / "codebuddy-proxy.jsonl"),
2184
+ )
2185
+ ).expanduser()
2186
+ parser.add_argument(
2187
+ "--log-file",
2188
+ type=pathlib.Path,
2189
+ default=default_log_file,
2190
+ help="记录完整请求/响应的 JSONL 文件(默认 ~/.workbuddy2api/codebuddy-proxy.jsonl)",
2191
+ )
2192
+ parser.add_argument("--desensitize", action="store_true",
2193
+ help="启用脱敏处理,对 system 消息中的敏感词插入零宽空格(缓解审核误拦)")
2194
+ parser.add_argument("--optimize-context", action="store_true",
2195
+ help="启用消息压缩优化(仅 /v1/responses),大幅减少 token 使用(适用于 Codex CLI 等长上下文场景)")
2196
+ parser.add_argument("--login", action="store_true",
2197
+ help="启动时执行浏览器登录/账户查询")
2198
+ parser.add_argument("--no-browser", action="store_true",
2199
+ help="登录时不自动打开浏览器")
2200
+ parser.add_argument("--verbose-llm", action="store_true",
2201
+ help="log full LLM request/response content (default: summary only, saves 98%% space)")
2202
+ parser.add_argument(
2203
+ "--static-models",
2204
+ action="store_true",
2205
+ help="兼容旧参数;/v1/models 当前始终使用 profile 对应的本地模型资源",
2206
+ )
2207
+ parser.add_argument(
2208
+ "--config-cache-ttl",
2209
+ type=int,
2210
+ default=int(os.getenv("CODEBUDDY_CONFIG_CACHE_TTL", "300")),
2211
+ help="兼容旧参数;当前 /v1/models 不启用远程动态模型",
2212
+ )
2213
+ return parser
2214
+
2215
+
2216
+ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
2217
+ args = build_parser().parse_args(argv)
2218
+ args.log_file = args.log_file.expanduser()
2219
+ return args
2220
+
2221
+
2222
+ def initialize_account_manager(args, resolved_backend):
2223
+ """Import legacy identities once and restore a persisted active account.
2224
+
2225
+ Reading legacy files never refreshes or rewrites them. A forced login
2226
+ always uses a temporary session, so --login cannot overwrite an old user.
2227
+ """
2228
+ from codebuddy_proxy.account_store import AccountStore
2229
+ from codebuddy_proxy.account_manager import AccountManager
2230
+
2231
+ store = AccountStore(args.accounts_dir)
2232
+ initial_import = not store.index_file.exists()
2233
+ explicit = bool(args.global_mode or args.endpoint is not None
2234
+ or args.session_file is not None
2235
+ or os.getenv("CODEBUDDY_ENDPOINT"))
2236
+ candidates = [resolved_backend]
2237
+ for profile in (DOMESTIC_PROFILE, GLOBAL_PROFILE):
2238
+ candidate = resolve_backend(profile=profile, environment={})
2239
+ if candidate not in candidates:
2240
+ candidates.append(candidate)
2241
+ selected_uid = None
2242
+ for candidate in candidates:
2243
+ # Migration is one-time. Re-reading a legacy file after its account
2244
+ # was deleted must not silently associate that identity again.
2245
+ if not initial_import and not (args.session_file is not None
2246
+ and candidate is resolved_backend):
2247
+ continue
2248
+ try:
2249
+ old = CodeBuddyClient(backend=candidate)
2250
+ except CodeBuddyError:
2251
+ if args.session_file is not None and candidate is resolved_backend:
2252
+ raise
2253
+ print(f"Skipped incompatible legacy session for {candidate.profile.key}", file=sys.stderr)
2254
+ continue
2255
+ account = old.session.get("account") or {}
2256
+ auth = old.session.get("auth") or {}
2257
+ if not isinstance(account, dict) or not isinstance(auth, dict):
2258
+ continue
2259
+ uid = account.get("uid")
2260
+ if not uid or not (auth.get("accessToken") or auth.get("refreshToken")):
2261
+ continue
2262
+ if candidate is resolved_backend:
2263
+ selected_uid = str(uid).strip()
2264
+ known = any(
2265
+ row["backend"] == old.profile.key and row["endpoint"] == old.endpoint
2266
+ and str(row["uid"]) == str(uid) for row in store.list_accounts()
2267
+ )
2268
+ if not known:
2269
+ store.import_client(old)
2270
+ if args.login:
2271
+ with tempfile.TemporaryDirectory(prefix="login-", dir=store.root) as temporary:
2272
+ login_backend = resolve_backend(
2273
+ profile=resolved_backend.profile, endpoint=resolved_backend.endpoint,
2274
+ session_file=pathlib.Path(temporary) / "session.json", environment={},
2275
+ )
2276
+ login_client = CodeBuddyClient(backend=login_backend)
2277
+ login_client.login(open_browser=not args.no_browser)
2278
+ store.import_client(login_client, activate=True)
2279
+ elif explicit:
2280
+ matching = [row for row in store.list_accounts()
2281
+ if row["backend"] == resolved_backend.profile.key
2282
+ and row["endpoint"] == resolved_backend.endpoint
2283
+ and (args.session_file is None or str(row["uid"]) == selected_uid)]
2284
+ if matching:
2285
+ chosen = next((row for row in matching if row["active"]), matching[0])
2286
+ store.set_active(chosen["id"])
2287
+ else:
2288
+ store.clear_active()
2289
+ manager = AccountManager(store)
2290
+ # The empty fallback preserves the requested profile for health/models.
2291
+ # Managed requests cannot start browser login or write to a legacy file.
2292
+ fallback = CodeBuddyClient(backend=resolve_backend(
2293
+ profile=resolved_backend.profile, endpoint=resolved_backend.endpoint,
2294
+ session_file=store.root / ".unlinked-session.json", environment={},
2295
+ ))
2296
+ return manager, manager.client or fallback
2297
+
2298
+
2299
+ def _run_proxy(args: argparse.Namespace) -> int:
2300
+ global proxy_state
2301
+ try:
2302
+ resolved_backend = resolve_backend_from_args(args)
2303
+ manager = None
2304
+ if args.mock_dir is not None:
2305
+ client = CodeBuddyClient(backend=resolved_backend)
2306
+ else:
2307
+ manager, client = initialize_account_manager(args, resolved_backend)
2308
+ except (CodeBuddyError, ValueError) as exc:
2309
+ print(f"错误: {exc}", file=sys.stderr)
2310
+ return 1
1847
2311
 
1848
2312
  # 设置日志
1849
2313
  log_dir = args.log_file.parent if args.log_file else pathlib.Path("logs")
1850
2314
  logger = setup_logging(log_dir)
1851
2315
  json_logger = setup_json_logging(args.log_file)
1852
2316
 
1853
- # 处理登录
1854
- if args.login:
1855
- client.login(open_browser=not args.no_browser)
1856
-
1857
2317
  # 创建全局状态
1858
2318
  proxy_state = ProxyState(
1859
2319
  client=client,
@@ -1864,23 +2324,24 @@ def main():
1864
2324
  verbose_llm=args.verbose_llm,
1865
2325
  logger=logger,
1866
2326
  json_logger=json_logger,
2327
+ account_manager=manager,
2328
+ )
2329
+ proxy_state.write_log(
2330
+ "startup",
2331
+ host=args.host,
2332
+ port=args.port,
2333
+ backend=client.profile.key,
2334
+ upstream_endpoint=client.endpoint,
2335
+ session_file=str(client.session_file),
2336
+ models_resource=client.profile.models_resource,
2337
+ )
2338
+ logger.info(
2339
+ "Backend profile=%s endpoint=%s session_file=%s models_resource=%s",
2340
+ client.profile.key,
2341
+ client.endpoint,
2342
+ client.session_file,
2343
+ client.profile.models_resource,
1867
2344
  )
1868
- proxy_state.write_log(
1869
- "startup",
1870
- host=args.host,
1871
- port=args.port,
1872
- backend=client.profile.key,
1873
- upstream_endpoint=client.endpoint,
1874
- session_file=str(client.session_file),
1875
- models_resource=client.profile.models_resource,
1876
- )
1877
- logger.info(
1878
- "Backend profile=%s endpoint=%s session_file=%s models_resource=%s",
1879
- client.profile.key,
1880
- client.endpoint,
1881
- client.session_file,
1882
- client.profile.models_resource,
1883
- )
1884
2345
  logger.info(
1885
2346
  "Runtime: app_version=%s system_version=%s python_version=%s machine=%s",
1886
2347
  proxy_state.runtime_info["app_version"],
@@ -1889,29 +2350,48 @@ def main():
1889
2350
  proxy_state.runtime_info["machine"],
1890
2351
  )
1891
2352
 
1892
- # 保留远程缓存类以兼容旧调用方,但 /v1/models 明确使用包内资源。
1893
- global remote_config_cache
1894
- remote_config_cache = None
1895
- logger.info(
1896
- "Using local model resource: backend=%s resource=%s",
1897
- client.profile.key,
1898
- client.profile.models_resource,
1899
- )
1900
- print(
1901
- f"[Local Models] backend={client.profile.key}, "
1902
- f"resource={client.profile.models_resource}"
1903
- )
2353
+ # 保留远程缓存类以兼容旧调用方,但 /v1/models 明确使用包内资源。
2354
+ global remote_config_cache
2355
+ remote_config_cache = None
2356
+ logger.info(
2357
+ "Using local model resource: backend=%s resource=%s",
2358
+ client.profile.key,
2359
+ client.profile.models_resource,
2360
+ )
2361
+ print(
2362
+ f"[Local Models] backend={client.profile.key}, "
2363
+ f"resource={client.profile.models_resource}"
2364
+ )
1904
2365
 
1905
2366
  # 启动信息输出到 stdout
1906
2367
  print(f"CodeBuddy proxy listening on http://{args.host}:{args.port}")
1907
2368
  print("Endpoints: /v1/models /v1/chat/completions /v1/responses /v1/messages /health")
2369
+ if manager is not None:
2370
+ print(f"Account manager: http://127.0.0.1:{args.port}/admin (local access only)")
1908
2371
 
1909
2372
  # 同时记录到日志
1910
2373
  logger.info(f"CodeBuddy proxy listening on http://{args.host}:{args.port}")
1911
2374
  logger.info("Endpoints: /v1/models /v1/chat/completions /v1/responses /v1/messages /health")
1912
2375
 
1913
2376
  # 启动 uvicorn
1914
- run_http_server(host=args.host, port=args.port, windows_loop=args.windows_loop)
2377
+ try:
2378
+ run_http_server(host=args.host, port=args.port, windows_loop=args.windows_loop)
2379
+ finally:
2380
+ if manager is not None:
2381
+ manager.close()
2382
+ return 0
2383
+
2384
+
2385
+ def main() -> int:
2386
+ global process_diagnostics
2387
+ args = parse_args()
2388
+ monitor = ProcessDiagnostics(args.log_file.parent)
2389
+ process_diagnostics = monitor
2390
+ try:
2391
+ return monitor.run(lambda: _run_proxy(args))
2392
+ finally:
2393
+ process_diagnostics = None
2394
+
1915
2395
 
1916
- if __name__ == "__main__":
1917
- raise SystemExit(main())
2396
+ if __name__ == "__main__":
2397
+ raise SystemExit(main())