coding-proxy 0.1.1__tar.gz → 0.1.2__tar.gz
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.
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/CHANGELOG.md +17 -1
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/PKG-INFO +1 -1
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/pyproject.toml +1 -1
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/executor.py +4 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/router.py +9 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/factory.py +22 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/routes.py +9 -9
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_app_routes.py +171 -14
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_router_executor.py +2 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/uv.lock +1 -1
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/.github/workflows/ci.yml +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/.github/workflows/coverage.yml +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/.github/workflows/promote.yml +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/.github/workflows/release.yml +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/.gitignore +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/AGENTS.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/CLAUDE.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/LICENSE +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/README.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/docs/ci-cd.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/docs/framework.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/docs/user-guide.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/docs/zh-CN/README.md +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/__main__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/providers/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/providers/base.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/providers/github.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/providers/google.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/runtime.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/auth/store.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/cli/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/cli/auth_commands.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/compat/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/compat/canonical.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/compat/session_store.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/auth_schema.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/config.default.yaml +22 -22
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/loader.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/resiliency.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/routing.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/schema.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/server.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/config/vendors.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/anthropic_to_gemini.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/anthropic_to_openai.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/gemini_sse_adapter.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/gemini_to_anthropic.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/convert/openai_to_anthropic.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/logging/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/logging/db.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/logging/stats.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/auth.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/compat.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/constants.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/pricing.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/token.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/model/vendor.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/pricing.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/circuit_breaker.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/error_classifier.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/model_mapper.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/quota_guard.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/rate_limit.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/retry.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/session_manager.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/tier.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/usage_parser.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/routing/usage_recorder.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/app.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/request_normalizer.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/server/responses.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/streaming/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/streaming/anthropic_compat.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/anthropic.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/antigravity.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/base.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/copilot.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/copilot_models.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/copilot_token_manager.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/copilot_urls.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/mixins.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/token_manager.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/src/coding/proxy/vendors/zhipu.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/__init__.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_antigravity.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_auto_login.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_circuit_breaker.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_cli_usage.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_compat.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_config_init.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_config_loader.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_convert_request.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_convert_response.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_convert_sse.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_copilot.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_copilot_convert_request.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_copilot_convert_response.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_copilot_models.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_copilot_urls.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_currency.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_error_classifier.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_mixins.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_auth.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_compat.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_constants.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_mapper.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_pricing.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_token.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_model_vendor.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_parse_usage.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_pricing.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_quota_guard.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_rate_limit.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_request_normalizer.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_router_chain.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_runtime_reauth.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_schema.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_streaming_anthropic_compat.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_tier.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_tiers_config.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_token_logger.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_token_manager.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_types.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_vendor_streaming.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_vendors.py +0 -0
- {coding_proxy-0.1.1 → coding_proxy-0.1.2}/tests/test_zhipu.py +0 -0
|
@@ -4,7 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
-
## [v0.1.
|
|
7
|
+
## [v0.1.2](https://github.com/ThreeFish-AI/coding-proxy/releases/tag/v0.1.2) — 2026-04-06
|
|
8
|
+
|
|
9
|
+
> [!IMPORTANT]
|
|
10
|
+
>
|
|
11
|
+
> **🔓 count_tokens 终于不再"偏心" Anthropic 了!**
|
|
12
|
+
>
|
|
13
|
+
> 全面拥抱多供应商泛化透传。配合全局活跃 Vendor 状态追踪机制,智能跟随 Vendor 当前移位,熔断降级?无缝切换,零感知!
|
|
14
|
+
|
|
15
|
+
### ✨ 核心亮点
|
|
16
|
+
|
|
17
|
+
- **全局活跃 Vendor 状态追踪**:🧠 Router 新增活跃 Vendor 属性,Executor 在每次流式/非流式请求成功后自动写入当前活跃供应商名称。精准锁定"此刻谁在干活",完美适配熔断降级等动态切换场景;
|
|
18
|
+
|
|
19
|
+
### 🔧 更多特性
|
|
20
|
+
|
|
21
|
+
- 🔧 **CI 三合一修复**:一次性根治 ruff lint 的 F821/F401 导入幽灵、formatter 行长规范对齐,CI 绿灯常亮!
|
|
22
|
+
|
|
23
|
+
## [v0.1.1](https://github.com/ThreeFish-AI/coding-proxy/releases/tag/v0.1.1) — 2026-04-05
|
|
8
24
|
|
|
9
25
|
> [!IMPORTANT]
|
|
10
26
|
>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: coding-proxy
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A High-Availability, Transparent, and Smart Multi-Vendor Proxy for Claude Code. Support Claude Plans, GitHub Copilot, Google Antigravity, ZAI/GLM.
|
|
5
5
|
Project-URL: Source Code, https://github.com/ThreeFish-AI/coding-proxy
|
|
6
6
|
Project-URL: User Guide, https://github.com/ThreeFish-AI/coding-proxy/blob/master/docs/user-guide.md
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "coding-proxy"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.2"
|
|
4
4
|
description = "A High-Availability, Transparent, and Smart Multi-Vendor Proxy for Claude Code. Support Claude Plans, GitHub Copilot, Google Antigravity, ZAI/GLM."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -154,11 +154,13 @@ class _RouteExecutor:
|
|
|
154
154
|
|
|
155
155
|
def __init__(
|
|
156
156
|
self,
|
|
157
|
+
router: Any, # RequestRouter 引用,用于写入活跃供应商状态
|
|
157
158
|
tiers: list[VendorTier],
|
|
158
159
|
usage_recorder: UsageRecorder,
|
|
159
160
|
session_manager: RouteSessionManager,
|
|
160
161
|
reauth_coordinator: Any | None = None,
|
|
161
162
|
) -> None:
|
|
163
|
+
self._router = router
|
|
162
164
|
self._tiers = tiers
|
|
163
165
|
self._recorder = usage_recorder
|
|
164
166
|
self._session_mgr = session_manager
|
|
@@ -256,6 +258,7 @@ class _RouteExecutor:
|
|
|
256
258
|
request_id=info.request_id,
|
|
257
259
|
),
|
|
258
260
|
)
|
|
261
|
+
self._router._active_vendor_name = tier.name # 更新活跃供应商
|
|
259
262
|
return
|
|
260
263
|
|
|
261
264
|
except TokenAcquireError as exc:
|
|
@@ -367,6 +370,7 @@ class _RouteExecutor:
|
|
|
367
370
|
usage=resp.usage,
|
|
368
371
|
),
|
|
369
372
|
)
|
|
373
|
+
self._router._active_vendor_name = tier.name # 更新活跃供应商
|
|
370
374
|
return resp
|
|
371
375
|
|
|
372
376
|
# 非流式的 semantic rejection 和 failover 判断(从响应对象而非异常中提取)
|
|
@@ -40,11 +40,15 @@ class RequestRouter:
|
|
|
40
40
|
if not tiers:
|
|
41
41
|
raise ValueError("至少需要一个供应商层级")
|
|
42
42
|
self._tiers = tiers
|
|
43
|
+
self._active_vendor_name: str | None = (
|
|
44
|
+
None # 当前活跃供应商名称(由 Executor 成功时写入)
|
|
45
|
+
)
|
|
43
46
|
|
|
44
47
|
# 正交分解的子组件
|
|
45
48
|
self._recorder = UsageRecorder(token_logger=token_logger)
|
|
46
49
|
self._session_mgr = RouteSessionManager(compat_session_store)
|
|
47
50
|
self._executor = _RouteExecutor(
|
|
51
|
+
router=self, # 传入 router 引用,用于写入活跃供应商状态
|
|
48
52
|
tiers=tiers,
|
|
49
53
|
usage_recorder=self._recorder,
|
|
50
54
|
session_manager=self._session_mgr,
|
|
@@ -59,6 +63,11 @@ class RequestRouter:
|
|
|
59
63
|
def tiers(self) -> list[VendorTier]:
|
|
60
64
|
return self._tiers
|
|
61
65
|
|
|
66
|
+
@property
|
|
67
|
+
def active_vendor_name(self) -> str | None:
|
|
68
|
+
"""当前活跃供应商名称(由 Executor 在成功响应时写入)."""
|
|
69
|
+
return self._active_vendor_name
|
|
70
|
+
|
|
62
71
|
# ── 公开路由接口(委托给 _RouteExecutor)───────────────
|
|
63
72
|
|
|
64
73
|
async def route_stream(
|
|
@@ -34,6 +34,7 @@ from ..routing.quota_guard import QuotaGuard
|
|
|
34
34
|
from ..routing.tier import VendorTier
|
|
35
35
|
from ..vendors.anthropic import AnthropicVendor
|
|
36
36
|
from ..vendors.antigravity import AntigravityVendor
|
|
37
|
+
from ..vendors.base import BaseVendor
|
|
37
38
|
from ..vendors.copilot import CopilotVendor
|
|
38
39
|
from ..vendors.zhipu import ZhipuVendor
|
|
39
40
|
|
|
@@ -51,6 +52,27 @@ def _find_anthropic_vendor(router: Any) -> AnthropicVendor | None:
|
|
|
51
52
|
return None
|
|
52
53
|
|
|
53
54
|
|
|
55
|
+
def _find_count_tokens_vendor(router: Any) -> BaseVendor | None:
|
|
56
|
+
"""查找当前实际在用的供应商(通过全局活跃状态).
|
|
57
|
+
|
|
58
|
+
读取 Executor 在成功响应时写入的活跃供应商名称,
|
|
59
|
+
按名称匹配返回对应的 vendor 对象。
|
|
60
|
+
无活跃记录时回退到 tiers[0](冷启动场景)。
|
|
61
|
+
"""
|
|
62
|
+
if not router.tiers:
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
# 优先使用全局活跃状态
|
|
66
|
+
active_name = router.active_vendor_name
|
|
67
|
+
if active_name:
|
|
68
|
+
for tier in router.tiers:
|
|
69
|
+
if tier.name == active_name:
|
|
70
|
+
return tier.vendor
|
|
71
|
+
|
|
72
|
+
# 冷启动(无任何成功请求):回退到首个供应商
|
|
73
|
+
return router.tiers[0].vendor
|
|
74
|
+
|
|
75
|
+
|
|
54
76
|
def _find_copilot_vendor(router: Any) -> CopilotVendor | None:
|
|
55
77
|
"""从路由链中查找 Copilot 供应商实例(用于诊断与模型探测)."""
|
|
56
78
|
for tier in router.tiers:
|
|
@@ -133,27 +133,27 @@ def register_core_routes(app: Any, router: Any) -> None:
|
|
|
133
133
|
|
|
134
134
|
@app.post("/v1/messages/count_tokens")
|
|
135
135
|
async def count_tokens(request: Request) -> Response:
|
|
136
|
-
"""Token 计数 API 透传 —
|
|
136
|
+
"""Token 计数 API 透传 — 使用主供应商转发.
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
支持所有提供 Anthropic 兼容端点的供应商(anthropic, zhipu 等)。
|
|
139
139
|
"""
|
|
140
|
-
from .factory import
|
|
140
|
+
from .factory import _find_count_tokens_vendor
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
if
|
|
142
|
+
target_vendor = _find_count_tokens_vendor(router)
|
|
143
|
+
if target_vendor is None:
|
|
144
144
|
return Response(
|
|
145
|
-
content=b'{"error":{"type":"
|
|
146
|
-
status_code=
|
|
145
|
+
content=b'{"error":{"type":"not_implemented","message":"no available vendor for count_tokens"}}',
|
|
146
|
+
status_code=501,
|
|
147
147
|
media_type="application/json",
|
|
148
148
|
)
|
|
149
149
|
|
|
150
150
|
body = await request.json()
|
|
151
151
|
headers = dict(request.headers)
|
|
152
|
-
prepared_body, prepared_headers = await
|
|
152
|
+
prepared_body, prepared_headers = await target_vendor._prepare_request(
|
|
153
153
|
body, headers
|
|
154
154
|
)
|
|
155
155
|
|
|
156
|
-
client =
|
|
156
|
+
client = target_vendor._get_client()
|
|
157
157
|
url = "/v1/messages/count_tokens"
|
|
158
158
|
if request.query_params:
|
|
159
159
|
url = f"{url}?{request.query_params}"
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
|
-
from unittest.mock import AsyncMock, MagicMock, patch
|
|
6
|
+
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
|
|
7
7
|
|
|
8
8
|
import httpx
|
|
9
9
|
from fastapi.testclient import TestClient
|
|
@@ -45,19 +45,26 @@ def test_get_root_returns_200():
|
|
|
45
45
|
# ── count_tokens 透传 ────────────────────────────────────────
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
def
|
|
49
|
-
"""
|
|
50
|
-
with _make_app(primary_enabled=
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
def test_count_tokens_no_vendor_returns_501():
|
|
49
|
+
"""无可用供应商时 count_tokens 返回 501 Not Implemented"""
|
|
50
|
+
with _make_app(primary_enabled=True) as client:
|
|
51
|
+
# Mock tiers 属性返回空列表(防御性编程覆盖)
|
|
52
|
+
with patch.object(
|
|
53
|
+
type(client.app.state.router),
|
|
54
|
+
"tiers",
|
|
55
|
+
new_callable=PropertyMock,
|
|
56
|
+
return_value=[],
|
|
57
|
+
):
|
|
58
|
+
resp = client.post(
|
|
59
|
+
"/v1/messages/count_tokens",
|
|
60
|
+
json={
|
|
61
|
+
"model": "claude-sonnet-4-20250514",
|
|
62
|
+
"messages": [{"role": "user", "content": "Hi"}],
|
|
63
|
+
},
|
|
64
|
+
)
|
|
65
|
+
assert resp.status_code == 501
|
|
66
|
+
data = resp.json()
|
|
67
|
+
assert data["error"]["type"] == "not_implemented"
|
|
61
68
|
|
|
62
69
|
|
|
63
70
|
def test_count_tokens_proxies_to_anthropic():
|
|
@@ -129,6 +136,156 @@ def test_count_tokens_upstream_error_passthrough():
|
|
|
129
136
|
assert resp.json()["error"]["type"] == "rate_limit_error"
|
|
130
137
|
|
|
131
138
|
|
|
139
|
+
def test_count_tokens_with_zhipu_primary():
|
|
140
|
+
"""zhipu 作为主供应商时,count_tokens 使用 ZhipuVendor 转发."""
|
|
141
|
+
config = ProxyConfig(
|
|
142
|
+
tiers=[
|
|
143
|
+
{"vendor": "zhipu", "enabled": True, "api_key": "sk-zhipu-test"},
|
|
144
|
+
],
|
|
145
|
+
database={"path": "/tmp/test-count-tokens-zhipu.db"},
|
|
146
|
+
)
|
|
147
|
+
app = create_app(config)
|
|
148
|
+
|
|
149
|
+
mock_response = MagicMock()
|
|
150
|
+
mock_response.content = b'{"input_tokens": 128}'
|
|
151
|
+
mock_response.status_code = 200
|
|
152
|
+
|
|
153
|
+
with TestClient(app) as client:
|
|
154
|
+
with patch.object(
|
|
155
|
+
httpx.AsyncClient,
|
|
156
|
+
"post",
|
|
157
|
+
new_callable=AsyncMock,
|
|
158
|
+
return_value=mock_response,
|
|
159
|
+
):
|
|
160
|
+
resp = client.post(
|
|
161
|
+
"/v1/messages/count_tokens?beta=true",
|
|
162
|
+
json={
|
|
163
|
+
"model": "claude-sonnet-4-20250514",
|
|
164
|
+
"messages": [{"role": "user", "content": "Hello"}],
|
|
165
|
+
},
|
|
166
|
+
)
|
|
167
|
+
assert resp.status_code == 200
|
|
168
|
+
assert resp.json()["input_tokens"] == 128
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_count_tokens_zhipu_upstream_error_passthrough():
|
|
172
|
+
"""zhipu 作为主供应商时,上游错误原样透传."""
|
|
173
|
+
config = ProxyConfig(
|
|
174
|
+
tiers=[
|
|
175
|
+
{"vendor": "zhipu", "enabled": True, "api_key": "sk-zhipu-test"},
|
|
176
|
+
],
|
|
177
|
+
database={"path": "/tmp/test-count-tokens-zhipu-error.db"},
|
|
178
|
+
)
|
|
179
|
+
app = create_app(config)
|
|
180
|
+
|
|
181
|
+
mock_response = MagicMock()
|
|
182
|
+
mock_response.content = (
|
|
183
|
+
b'{"error":{"type":"authentication_error","message":"invalid api key"}}'
|
|
184
|
+
)
|
|
185
|
+
mock_response.status_code = 403
|
|
186
|
+
|
|
187
|
+
with TestClient(app) as client:
|
|
188
|
+
with patch.object(
|
|
189
|
+
httpx.AsyncClient,
|
|
190
|
+
"post",
|
|
191
|
+
new_callable=AsyncMock,
|
|
192
|
+
return_value=mock_response,
|
|
193
|
+
):
|
|
194
|
+
resp = client.post(
|
|
195
|
+
"/v1/messages/count_tokens?beta=true",
|
|
196
|
+
json={
|
|
197
|
+
"model": "claude-sonnet-4-20250514",
|
|
198
|
+
"messages": [{"role": "user", "content": "Hello"}],
|
|
199
|
+
},
|
|
200
|
+
)
|
|
201
|
+
assert resp.status_code == 403
|
|
202
|
+
assert resp.json()["error"]["type"] == "authentication_error"
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_count_tokens_uses_active_vendor_from_global_state():
|
|
206
|
+
"""count_tokens 使用全局活跃状态标记的供应商(而非 tiers[0] 或门控判断).
|
|
207
|
+
|
|
208
|
+
模拟场景:zhipu 因熔断器开启降级到 anthropic,
|
|
209
|
+
Executor 成功后将 active_vendor_name 设为 "anthropic",
|
|
210
|
+
count_tokens 应跟随使用 anthropic。
|
|
211
|
+
"""
|
|
212
|
+
config = ProxyConfig(
|
|
213
|
+
tiers=[
|
|
214
|
+
{
|
|
215
|
+
"vendor": "zhipu",
|
|
216
|
+
"enabled": True,
|
|
217
|
+
"api_key": "sk-zhipu-test",
|
|
218
|
+
"circuit_breaker": {"failure_threshold": 3},
|
|
219
|
+
},
|
|
220
|
+
{"vendor": "anthropic", "enabled": True, "api_key": "sk-ant-test"},
|
|
221
|
+
],
|
|
222
|
+
database={"path": "/tmp/test-count-tokens-active-vendor.db"},
|
|
223
|
+
)
|
|
224
|
+
app = create_app(config)
|
|
225
|
+
|
|
226
|
+
# 模拟 Executor 已将活跃供应商切换为 anthropic(如 zhipu CB OPEN 后降级)
|
|
227
|
+
app.state.router._active_vendor_name = "anthropic"
|
|
228
|
+
|
|
229
|
+
mock_response = MagicMock()
|
|
230
|
+
mock_response.content = b'{"input_tokens": 55}'
|
|
231
|
+
mock_response.status_code = 200
|
|
232
|
+
|
|
233
|
+
with TestClient(app) as client:
|
|
234
|
+
with patch.object(
|
|
235
|
+
httpx.AsyncClient,
|
|
236
|
+
"post",
|
|
237
|
+
new_callable=AsyncMock,
|
|
238
|
+
return_value=mock_response,
|
|
239
|
+
) as mock_post:
|
|
240
|
+
resp = client.post(
|
|
241
|
+
"/v1/messages/count_tokens",
|
|
242
|
+
json={
|
|
243
|
+
"model": "claude-sonnet-4-20250514",
|
|
244
|
+
"messages": [{"role": "user", "content": "Hi"}],
|
|
245
|
+
},
|
|
246
|
+
)
|
|
247
|
+
assert resp.status_code == 200
|
|
248
|
+
assert resp.json()["input_tokens"] == 55
|
|
249
|
+
# 验证 httpx.post 被调用(使用了 anthropic vendor 的 client)
|
|
250
|
+
assert mock_post.called
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def test_count_tokens_falls_back_to_tiers0_on_cold_start():
|
|
254
|
+
"""冷启动(无任何成功请求)时,count_tokens 回退到 tiers[0]."""
|
|
255
|
+
config = ProxyConfig(
|
|
256
|
+
tiers=[
|
|
257
|
+
{"vendor": "zhipu", "enabled": True, "api_key": "sk-zhipu-test"},
|
|
258
|
+
{"vendor": "anthropic", "enabled": True, "api_key": "sk-ant-test"},
|
|
259
|
+
],
|
|
260
|
+
database={"path": "/tmp/test-count-tokens-cold-start.db"},
|
|
261
|
+
)
|
|
262
|
+
app = create_app(config)
|
|
263
|
+
|
|
264
|
+
# 确认无活跃供应商记录(冷启动)
|
|
265
|
+
assert app.state.router.active_vendor_name is None
|
|
266
|
+
|
|
267
|
+
mock_response = MagicMock()
|
|
268
|
+
mock_response.content = b'{"input_tokens": 88}'
|
|
269
|
+
mock_response.status_code = 200
|
|
270
|
+
|
|
271
|
+
with TestClient(app) as client:
|
|
272
|
+
with patch.object(
|
|
273
|
+
httpx.AsyncClient,
|
|
274
|
+
"post",
|
|
275
|
+
new_callable=AsyncMock,
|
|
276
|
+
return_value=mock_response,
|
|
277
|
+
):
|
|
278
|
+
resp = client.post(
|
|
279
|
+
"/v1/messages/count_tokens",
|
|
280
|
+
json={
|
|
281
|
+
"model": "claude-sonnet-4-20250514",
|
|
282
|
+
"messages": [{"role": "user", "content": "Hi"}],
|
|
283
|
+
},
|
|
284
|
+
)
|
|
285
|
+
assert resp.status_code == 200
|
|
286
|
+
assert resp.json()["input_tokens"] == 88
|
|
287
|
+
|
|
288
|
+
|
|
132
289
|
def test_status_exposes_vendor_diagnostics():
|
|
133
290
|
"""状态接口暴露供应商诊断信息,便于排查凭证交换异常."""
|
|
134
291
|
config = ProxyConfig(
|
|
@@ -107,7 +107,9 @@ def _executor(tiers: list[VendorTier] | None = None, **kwargs) -> _RouteExecutor
|
|
|
107
107
|
tiers = [_make_tier()]
|
|
108
108
|
recorder = kwargs.pop("recorder", UsageRecorder())
|
|
109
109
|
session_mgr = kwargs.pop("session_mgr", RouteSessionManager())
|
|
110
|
+
router = kwargs.pop("router", MagicMock())
|
|
110
111
|
return _RouteExecutor(
|
|
112
|
+
router=router,
|
|
111
113
|
tiers=tiers,
|
|
112
114
|
usage_recorder=recorder,
|
|
113
115
|
session_manager=session_mgr,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -6,6 +6,28 @@ server:
|
|
|
6
6
|
host: "127.0.0.1"
|
|
7
7
|
port: 8046
|
|
8
8
|
|
|
9
|
+
logging:
|
|
10
|
+
level: "INFO"
|
|
11
|
+
|
|
12
|
+
# === 降级链路优先级(可选) ===
|
|
13
|
+
#
|
|
14
|
+
# 显式指定故障转移时的供应商尝试顺序。
|
|
15
|
+
# 未配置时回退到 vendors 列表的原始顺序。
|
|
16
|
+
#
|
|
17
|
+
# 引用的 vendor 名称必须在上方 vendors 中定义且 enabled=true。
|
|
18
|
+
# 可用于在不改变 vendors 定义顺序的情况下灵活调整降级策略。
|
|
19
|
+
#
|
|
20
|
+
# 示例 1:完整四层降级链路(默认行为等效)
|
|
21
|
+
# tiers: ["anthropic", "copilot", "antigravity", "zhipu"]
|
|
22
|
+
#
|
|
23
|
+
# 示例 2:跳过中间层,仅保留首尾两级
|
|
24
|
+
# tiers: ["anthropic", "zhipu"]
|
|
25
|
+
#
|
|
26
|
+
# 示例 3:将 zhipu 提升为首选,anthropic 作为后备
|
|
27
|
+
# tiers: ["zhipu", "anthropic"]
|
|
28
|
+
|
|
29
|
+
tiers: ["anthropic", "copilot", "antigravity", "zhipu"]
|
|
30
|
+
|
|
9
31
|
# === 供应商定义(vendors 列表) ===
|
|
10
32
|
#
|
|
11
33
|
# 每个 vendor 通过 vendor 字段指定供应商类型:
|
|
@@ -94,25 +116,6 @@ vendors:
|
|
|
94
116
|
threshold_percent: 95.0
|
|
95
117
|
probe_interval_seconds: 300
|
|
96
118
|
|
|
97
|
-
# === 降级链路优先级(可选) ===
|
|
98
|
-
#
|
|
99
|
-
# 显式指定故障转移时的供应商尝试顺序。
|
|
100
|
-
# 未配置时回退到 vendors 列表的原始顺序。
|
|
101
|
-
#
|
|
102
|
-
# 引用的 vendor 名称必须在上方 vendors 中定义且 enabled=true。
|
|
103
|
-
# 可用于在不改变 vendors 定义顺序的情况下灵活调整降级策略。
|
|
104
|
-
#
|
|
105
|
-
# 示例 1:完整四层降级链路(默认行为等效)
|
|
106
|
-
# tiers: ["anthropic", "copilot", "antigravity", "zhipu"]
|
|
107
|
-
#
|
|
108
|
-
# 示例 2:跳过中间层,仅保留首尾两级
|
|
109
|
-
# tiers: ["anthropic", "zhipu"]
|
|
110
|
-
#
|
|
111
|
-
# 示例 3:将 zhipu 提升为首选,anthropic 作为后备
|
|
112
|
-
# tiers: ["zhipu", "anthropic"]
|
|
113
|
-
|
|
114
|
-
tiers: ["anthropic", "copilot", "antigravity", "zhipu"]
|
|
115
|
-
|
|
116
119
|
# === 故障转移触发条件 ===
|
|
117
120
|
failover:
|
|
118
121
|
status_codes: [429, 403, 503, 500]
|
|
@@ -278,6 +281,3 @@ auth:
|
|
|
278
281
|
# google_client_id: ""
|
|
279
282
|
# google_client_secret: ""
|
|
280
283
|
token_store_path: "~/.coding-proxy/tokens.json"
|
|
281
|
-
|
|
282
|
-
logging:
|
|
283
|
-
level: "INFO"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|