coding-proxy 0.1.1__py3-none-any.whl → 0.1.2__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.
@@ -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"
@@ -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 透传 — 旁路直通 Anthropic,不经过路由链.
136
+ """Token 计数 API 透传 — 使用主供应商转发.
137
137
 
138
- 仅当 Anthropic 主供应商启用时可用;其他供应商不支持此协议。
138
+ 支持所有提供 Anthropic 兼容端点的供应商(anthropic, zhipu 等)。
139
139
  """
140
- from .factory import _find_anthropic_vendor
140
+ from .factory import _find_count_tokens_vendor
141
141
 
142
- anthropic_vendor = _find_anthropic_vendor(router)
143
- if anthropic_vendor is None:
142
+ target_vendor = _find_count_tokens_vendor(router)
143
+ if target_vendor is None:
144
144
  return Response(
145
- content=b'{"error":{"type":"not_found","message":"count_tokens requires anthropic vendor"}}',
146
- status_code=404,
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 anthropic_vendor._prepare_request(
152
+ prepared_body, prepared_headers = await target_vendor._prepare_request(
153
153
  body, headers
154
154
  )
155
155
 
156
- client = anthropic_vendor._get_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}"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coding-proxy
3
- Version: 0.1.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
@@ -16,7 +16,7 @@ coding/proxy/compat/canonical.py,sha256=-zcuEwZ402xeH3C545RmuYRkT2HDuvFloyFydDv8
16
16
  coding/proxy/compat/session_store.py,sha256=B9IFjjQJnHMg1244m__jG9gnqGWi26-JyEtwopEri6Q,5244
17
17
  coding/proxy/config/__init__.py,sha256=hzgU5noJGecjj13UY38cC_p6jpWO3GO7okSW-A2XkJ0,127
18
18
  coding/proxy/config/auth_schema.py,sha256=LYrJQU_fgW-6AoQdjXt4-MgPJjXjv9HrghlCcZwotnA,696
19
- coding/proxy/config/config.default.yaml,sha256=yGHjDHXg9sW33cLC8Dnixu5jKrUN7nBSJKANDGAbte0,9625
19
+ coding/proxy/config/config.default.yaml,sha256=_y16VK8mxGBh2WRwMc32t39FW9NEwo7QzEwAWILkB6M,9625
20
20
  coding/proxy/config/loader.py,sha256=1J_RBJgjuC8RwB2mwewLMS7vy9WEhUqttZG2igeDm-w,8984
21
21
  coding/proxy/config/resiliency.py,sha256=seSPzoXBTH4RV7thLiUfivFH9nT3otaurB9xRdK8lPk,1204
22
22
  coding/proxy/config/routing.py,sha256=1jX96Uhs15PogZF67kIvKrgpWb01m7EP85XKzz7Ov38,11255
@@ -42,22 +42,22 @@ coding/proxy/model/vendor.py,sha256=Yg4AYN7tgjHaGWRGEccCs3AdO8vBc-KxIelnqRowpyo,
42
42
  coding/proxy/routing/__init__.py,sha256=2Tpc6MQ5Oy_w-YlEWQVDSgcSgiHp3OwKOABIjj5kfHA,1595
43
43
  coding/proxy/routing/circuit_breaker.py,sha256=GzGxnk1MYfszQ654B72IWe0dVgaTxhYqyQv6jZA20e8,6028
44
44
  coding/proxy/routing/error_classifier.py,sha256=wbBMoScg7628qCnS5DP9o91joDuBXIRW52aN7UbhgV4,1944
45
- coding/proxy/routing/executor.py,sha256=QrDVAr9klZCJzH5kdCwkXJNn_5pC7EhiwwHlGgkaZ-U,22639
45
+ coding/proxy/routing/executor.py,sha256=uFLjBTgEZfv3oBwCm1u4bjOIv9rwFwzI8E7rHI7b8e4,22926
46
46
  coding/proxy/routing/model_mapper.py,sha256=PPyZ8ZvLSG8ujGmZL3bMPAzdTNu-z8woNTg6cXSZ-Js,3454
47
47
  coding/proxy/routing/quota_guard.py,sha256=VBJSgS1Wjw8RBwyfCdegWrBXGTbVmmU3bA3Tf81O8ns,6558
48
48
  coding/proxy/routing/rate_limit.py,sha256=i2cCqtbmXrP6wjRUTWXLP4DdYwVj0C4o59QdYGkPQj0,5122
49
49
  coding/proxy/routing/retry.py,sha256=KEYVToBkBOtXdqY7He70RrtVWFqIpnOQf2VVksdLrlk,2386
50
- coding/proxy/routing/router.py,sha256=ZNQEUh1oIGA8_GG9oMJuyu8jwM0momJXDwnhSd9OfCI,3025
50
+ coding/proxy/routing/router.py,sha256=bXaglxuT-5qYzrSMk7ZE4mpQynP4Z6F2pm9hnCbdG90,3440
51
51
  coding/proxy/routing/session_manager.py,sha256=r3ach8ZxnuZEWWikPB0wl-oYzT__MkAvK3Frh4j_xr4,2730
52
52
  coding/proxy/routing/tier.py,sha256=Sm--jA3WEcTO8nDs1aw15ilggg5ly_evWvs4gaD87Bo,6931
53
53
  coding/proxy/routing/usage_parser.py,sha256=j4G0ArFduQ2D4Yeuad94DVlwdc-JvSh7SJLVzc-hLcs,8480
54
54
  coding/proxy/routing/usage_recorder.py,sha256=pSs2r79fOrrq2cWJP-P5KKFnnXf62-3gYLaxAwndFi0,6272
55
55
  coding/proxy/server/__init__.py,sha256=KeH7mEu36v9v27m3VgeSxSeFz9sLTJrxS_EVATJ7Vks,20
56
56
  coding/proxy/server/app.py,sha256=hxCKb8FVEOYCvC84agUtwECajtqLqNgkTIEpEySJaGs,5613
57
- coding/proxy/server/factory.py,sha256=qymARkk3-3aTYJjiCWpJaUvLPByv7fJ3OMF1oWyHMxc,7019
57
+ coding/proxy/server/factory.py,sha256=TUAgAl2s528b5DpelzuJvSSJKzDTVdi6e8p2QrqihDo,7759
58
58
  coding/proxy/server/request_normalizer.py,sha256=vN9ibmDw7oJy0WNPZkP3lWeOzogQoPQShwC-CSXmnX4,5443
59
59
  coding/proxy/server/responses.py,sha256=i0ugnLRNOdRYGHEWxkwsxR35ChmdMQsSaD8AjRluTn4,2167
60
- coding/proxy/server/routes.py,sha256=xZYR5ImoVHX5yzHjKe980ZCgKqdKYdo5bjsFD1O4Tdw,11756
60
+ coding/proxy/server/routes.py,sha256=QeEsYczjvPfVhOrED2qe1V6CpwVBZbQHs8dP0OGdGXQ,11734
61
61
  coding/proxy/streaming/__init__.py,sha256=0al5TC-zJt8Bz0CibTmd8WPza-Cs-qcuZWvT2fKPqHI,23
62
62
  coding/proxy/streaming/anthropic_compat.py,sha256=cycYiJ-glN88OmfLTR7FroHRethONlEcK0PT78KqGbA,21202
63
63
  coding/proxy/vendors/__init__.py,sha256=fehSpQ2-kUlZY6N8cNMGVJYMXspQmi3V4e0FgC4Ay4k,1011
@@ -71,8 +71,8 @@ coding/proxy/vendors/copilot_urls.py,sha256=YxaR6zmPdrySzsotJx6PjNIu2JIGG6N1D8kZ
71
71
  coding/proxy/vendors/mixins.py,sha256=eZd_vlsPCKUNOEWSxWTDDb5i24GUIE3_JZDsj5LMr08,2583
72
72
  coding/proxy/vendors/token_manager.py,sha256=s10t4Com0jNnKGkPyJ_HpG5SjHrCEJvfArEOAaPKA_k,4189
73
73
  coding/proxy/vendors/zhipu.py,sha256=R4SOI9hKYnPk7sifSswsJpaEyWH6x3Ijo5ZoZSzFw7s,9623
74
- coding_proxy-0.1.1.dist-info/METADATA,sha256=_k6UOzwaJ-P4zV7WNBBzx_5NVyKCT5iy3WSyPKFQDpY,10780
75
- coding_proxy-0.1.1.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
76
- coding_proxy-0.1.1.dist-info/entry_points.txt,sha256=moIVzt5ho0Wk9B47LOo2SEAbhzuDDHWi-EfM30U0XBg,54
77
- coding_proxy-0.1.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
78
- coding_proxy-0.1.1.dist-info/RECORD,,
74
+ coding_proxy-0.1.2.dist-info/METADATA,sha256=OofDx3dxVy0KDwpnKR8aVopuYECwooXepIs3bzHafyA,10780
75
+ coding_proxy-0.1.2.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
76
+ coding_proxy-0.1.2.dist-info/entry_points.txt,sha256=moIVzt5ho0Wk9B47LOo2SEAbhzuDDHWi-EfM30U0XBg,54
77
+ coding_proxy-0.1.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
78
+ coding_proxy-0.1.2.dist-info/RECORD,,