coding-proxy 0.1.4a7__py3-none-any.whl → 0.2.0__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.
- coding/proxy/cli/__init__.py +18 -4
- coding/proxy/config/config.default.yaml +211 -2
- coding/proxy/config/routing.py +25 -5
- coding/proxy/config/schema.py +13 -0
- coding/proxy/config/vendors.py +50 -0
- coding/proxy/logging/db.py +12 -8
- coding/proxy/logging/stats.py +55 -3
- coding/proxy/routing/executor.py +5 -0
- coding/proxy/routing/model_mapper.py +5 -0
- coding/proxy/routing/quota_guard.py +9 -2
- coding/proxy/routing/session_manager.py +5 -0
- coding/proxy/server/app.py +2 -5
- coding/proxy/server/factory.py +53 -0
- coding/proxy/vendors/alibaba.py +27 -0
- coding/proxy/vendors/doubao.py +27 -0
- coding/proxy/vendors/kimi.py +27 -0
- coding/proxy/vendors/minimax.py +27 -0
- coding/proxy/vendors/native_anthropic.py +270 -0
- coding/proxy/vendors/xiaomi.py +27 -0
- coding/proxy/vendors/zhipu.py +6 -236
- {coding_proxy-0.1.4a7.dist-info → coding_proxy-0.2.0.dist-info}/METADATA +1 -1
- {coding_proxy-0.1.4a7.dist-info → coding_proxy-0.2.0.dist-info}/RECORD +25 -19
- {coding_proxy-0.1.4a7.dist-info → coding_proxy-0.2.0.dist-info}/WHEEL +0 -0
- {coding_proxy-0.1.4a7.dist-info → coding_proxy-0.2.0.dist-info}/entry_points.txt +0 -0
- {coding_proxy-0.1.4a7.dist-info → coding_proxy-0.2.0.dist-info}/licenses/LICENSE +0 -0
coding/proxy/cli/__init__.py
CHANGED
|
@@ -150,7 +150,9 @@ def usage(
|
|
|
150
150
|
),
|
|
151
151
|
total: bool = typer.Option(False, "--total", "-t", help="统计全部历史记录"),
|
|
152
152
|
vendor: str | None = typer.Option(None, "--vendor", "-v", help="过滤供应商"),
|
|
153
|
-
model: str | None = typer.Option(
|
|
153
|
+
model: str | None = typer.Option(
|
|
154
|
+
None, "--model", help="过滤实际服务模型(model_served),逗号分隔可指定多个"
|
|
155
|
+
),
|
|
154
156
|
db_path: str | None = typer.Option(None, "--db", help="数据库路径"),
|
|
155
157
|
) -> None:
|
|
156
158
|
"""查看 Token 使用统计.
|
|
@@ -166,15 +168,27 @@ def usage(
|
|
|
166
168
|
period, count = _resolve_period(days=days, week=week, month=month, total=total)
|
|
167
169
|
cfg = load_config(Path(db_path) if db_path else None)
|
|
168
170
|
token_logger = TokenLogger(cfg.db_path)
|
|
169
|
-
|
|
171
|
+
# 解析逗号分隔的多 vendor(如 "anthropic,zhipu" → ["anthropic", "zhipu"])
|
|
172
|
+
vendor_filter: str | list[str] | None = None
|
|
173
|
+
if vendor:
|
|
174
|
+
parts = [v.strip() for v in vendor.split(",") if v.strip()]
|
|
175
|
+
vendor_filter = parts[0] if len(parts) == 1 else parts
|
|
176
|
+
# 解析逗号分隔的多 model(如 "glm-5,glm-5.1" → ["glm-5", "glm-5.1"])
|
|
177
|
+
model_filter: str | list[str] | None = None
|
|
178
|
+
if model:
|
|
179
|
+
parts = [m.strip() for m in model.split(",") if m.strip()]
|
|
180
|
+
model_filter = parts[0] if len(parts) == 1 else parts
|
|
181
|
+
asyncio.run(
|
|
182
|
+
_run_usage(token_logger, period, count, vendor_filter, model_filter, cfg)
|
|
183
|
+
)
|
|
170
184
|
|
|
171
185
|
|
|
172
186
|
async def _run_usage(
|
|
173
187
|
token_logger: TokenLogger,
|
|
174
188
|
period: TimePeriod,
|
|
175
189
|
count: int,
|
|
176
|
-
vendor: str | None,
|
|
177
|
-
model: str | None,
|
|
190
|
+
vendor: str | list[str] | None,
|
|
191
|
+
model: str | list[str] | None,
|
|
178
192
|
cfg: ProxyConfig,
|
|
179
193
|
) -> None:
|
|
180
194
|
from ..pricing import PricingTable
|
|
@@ -26,12 +26,12 @@ logging:
|
|
|
26
26
|
# 示例 3:将 zhipu 提升为首选,anthropic 作为后备
|
|
27
27
|
# tiers: ["zhipu", "anthropic"]
|
|
28
28
|
|
|
29
|
-
tiers: ["
|
|
29
|
+
tiers: ["zhipu", "anthropic", "copilot", "antigravity"]
|
|
30
30
|
|
|
31
31
|
# === 供应商定义(vendors 列表) ===
|
|
32
32
|
#
|
|
33
33
|
# 每个 vendor 通过 vendor 字段指定供应商类型:
|
|
34
|
-
# anthropic |
|
|
34
|
+
# anthropic | zhipu | minimax | alibaba | xiaomi | kimi | doubao | copilot | antigravity
|
|
35
35
|
#
|
|
36
36
|
# 优先级规则:
|
|
37
37
|
# - 若配置了 tiers,以其指定的顺序为准
|
|
@@ -101,6 +101,7 @@ vendors:
|
|
|
101
101
|
|
|
102
102
|
# Vendor 3: 智谱 GLM(终端兜底,无 circuit_breaker)
|
|
103
103
|
- vendor: zhipu
|
|
104
|
+
enabled: true
|
|
104
105
|
base_url: "https://open.bigmodel.cn/api/anthropic"
|
|
105
106
|
api_key: "${ZHIPU_API_KEY}"
|
|
106
107
|
timeout_ms: 3000000
|
|
@@ -116,6 +117,91 @@ vendors:
|
|
|
116
117
|
threshold_percent: 95.0
|
|
117
118
|
probe_interval_seconds: 300
|
|
118
119
|
|
|
120
|
+
# Vendor 4: MiniMax(默认禁用,需手动启用并添加到 tiers)
|
|
121
|
+
- vendor: minimax
|
|
122
|
+
enabled: false
|
|
123
|
+
base_url: "https://api.minimaxi.com/anthropic"
|
|
124
|
+
api_key: "${MINIMAX_API_KEY}"
|
|
125
|
+
timeout_ms: 3000000
|
|
126
|
+
circuit_breaker:
|
|
127
|
+
failure_threshold: 3
|
|
128
|
+
recovery_timeout_seconds: 300
|
|
129
|
+
success_threshold: 2
|
|
130
|
+
quota_guard:
|
|
131
|
+
enabled: false
|
|
132
|
+
token_budget: 1000000000
|
|
133
|
+
window_hours: 24.0
|
|
134
|
+
threshold_percent: 95.0
|
|
135
|
+
probe_interval_seconds: 300
|
|
136
|
+
|
|
137
|
+
# Vendor 5: 阿里 Qwen(默认禁用)
|
|
138
|
+
- vendor: alibaba
|
|
139
|
+
enabled: false
|
|
140
|
+
base_url: "https://coding-intl.dashscope.aliyuncs.com/apps/anthropic"
|
|
141
|
+
api_key: "${ALIBABA_API_KEY}"
|
|
142
|
+
timeout_ms: 3000000
|
|
143
|
+
circuit_breaker:
|
|
144
|
+
failure_threshold: 3
|
|
145
|
+
recovery_timeout_seconds: 300
|
|
146
|
+
success_threshold: 2
|
|
147
|
+
quota_guard:
|
|
148
|
+
enabled: false
|
|
149
|
+
token_budget: 1000000000
|
|
150
|
+
window_hours: 24.0
|
|
151
|
+
threshold_percent: 95.0
|
|
152
|
+
probe_interval_seconds: 300
|
|
153
|
+
|
|
154
|
+
# Vendor 6: 小米 MiMo(默认禁用)
|
|
155
|
+
- vendor: xiaomi
|
|
156
|
+
enabled: false
|
|
157
|
+
base_url: "https://token-plan-cn.xiaomimimo.com/anthropic"
|
|
158
|
+
api_key: "${XIAOMI_API_KEY}"
|
|
159
|
+
timeout_ms: 3000000
|
|
160
|
+
circuit_breaker:
|
|
161
|
+
failure_threshold: 3
|
|
162
|
+
recovery_timeout_seconds: 300
|
|
163
|
+
success_threshold: 2
|
|
164
|
+
quota_guard:
|
|
165
|
+
enabled: false
|
|
166
|
+
token_budget: 1000000000
|
|
167
|
+
window_hours: 24.0
|
|
168
|
+
threshold_percent: 95.0
|
|
169
|
+
probe_interval_seconds: 300
|
|
170
|
+
|
|
171
|
+
# Vendor 7: Kimi(默认禁用)
|
|
172
|
+
- vendor: kimi
|
|
173
|
+
enabled: false
|
|
174
|
+
base_url: "https://api.kimi.com/coding/"
|
|
175
|
+
api_key: "${KIMI_API_KEY}"
|
|
176
|
+
timeout_ms: 3000000
|
|
177
|
+
circuit_breaker:
|
|
178
|
+
failure_threshold: 3
|
|
179
|
+
recovery_timeout_seconds: 300
|
|
180
|
+
success_threshold: 2
|
|
181
|
+
quota_guard:
|
|
182
|
+
enabled: false
|
|
183
|
+
token_budget: 1000000000
|
|
184
|
+
window_hours: 24.0
|
|
185
|
+
threshold_percent: 95.0
|
|
186
|
+
probe_interval_seconds: 300
|
|
187
|
+
|
|
188
|
+
# Vendor 8: 豆包 Doubao(默认禁用)
|
|
189
|
+
- vendor: doubao
|
|
190
|
+
enabled: false
|
|
191
|
+
base_url: "https://ark.cn-beijing.volces.com/api/coding"
|
|
192
|
+
api_key: "${ARK_API_KEY}"
|
|
193
|
+
timeout_ms: 3000000
|
|
194
|
+
circuit_breaker:
|
|
195
|
+
failure_threshold: 3
|
|
196
|
+
recovery_timeout_seconds: 300
|
|
197
|
+
success_threshold: 2
|
|
198
|
+
quota_guard:
|
|
199
|
+
enabled: false
|
|
200
|
+
token_budget: 1000000000
|
|
201
|
+
window_hours: 24.0
|
|
202
|
+
threshold_percent: 95.0
|
|
203
|
+
probe_interval_seconds: 300
|
|
204
|
+
|
|
119
205
|
# === 故障转移触发条件 ===
|
|
120
206
|
failover:
|
|
121
207
|
status_codes: [429, 403, 503, 500, 529]
|
|
@@ -166,6 +252,71 @@ model_mapping:
|
|
|
166
252
|
vendors: ["zhipu"]
|
|
167
253
|
target: "glm-4.5-air"
|
|
168
254
|
is_regex: true
|
|
255
|
+
# MiniMax 模型映射(原生 Anthropic 兼容端点)
|
|
256
|
+
- pattern: "claude-sonnet-.*"
|
|
257
|
+
vendors: ["minimax"]
|
|
258
|
+
target: "minimax-m2.7"
|
|
259
|
+
is_regex: true
|
|
260
|
+
- pattern: "claude-opus-.*"
|
|
261
|
+
vendors: ["minimax"]
|
|
262
|
+
target: "minimax-m2.7"
|
|
263
|
+
is_regex: true
|
|
264
|
+
- pattern: "claude-haiku-.*"
|
|
265
|
+
vendors: ["minimax"]
|
|
266
|
+
target: "minimax-m2.7"
|
|
267
|
+
is_regex: true
|
|
268
|
+
# 阿里 Qwen 模型映射(原生 Anthropic 兼容端点)
|
|
269
|
+
- pattern: "claude-sonnet-.*"
|
|
270
|
+
vendors: ["alibaba"]
|
|
271
|
+
target: "qwen3.6-plus"
|
|
272
|
+
is_regex: true
|
|
273
|
+
- pattern: "claude-opus-.*"
|
|
274
|
+
vendors: ["alibaba"]
|
|
275
|
+
target: "qwen3.6-plus"
|
|
276
|
+
is_regex: true
|
|
277
|
+
- pattern: "claude-haiku-.*"
|
|
278
|
+
vendors: ["alibaba"]
|
|
279
|
+
target: "qwen3.6-plus"
|
|
280
|
+
is_regex: true
|
|
281
|
+
# 小米 MiMo 模型映射(原生 Anthropic 兼容端点)
|
|
282
|
+
- pattern: "claude-sonnet-.*"
|
|
283
|
+
vendors: ["xiaomi"]
|
|
284
|
+
target: "mimo-v2-pro"
|
|
285
|
+
is_regex: true
|
|
286
|
+
- pattern: "claude-opus-.*"
|
|
287
|
+
vendors: ["xiaomi"]
|
|
288
|
+
target: "mimo-v2-pro"
|
|
289
|
+
is_regex: true
|
|
290
|
+
- pattern: "claude-haiku-.*"
|
|
291
|
+
vendors: ["xiaomi"]
|
|
292
|
+
target: "mimo-v2-pro"
|
|
293
|
+
is_regex: true
|
|
294
|
+
# Kimi 模型映射(原生 Anthropic 兼容端点)
|
|
295
|
+
- pattern: "claude-sonnet-.*"
|
|
296
|
+
vendors: ["kimi"]
|
|
297
|
+
target: "kimi-k2.5"
|
|
298
|
+
is_regex: true
|
|
299
|
+
- pattern: "claude-opus-.*"
|
|
300
|
+
vendors: ["kimi"]
|
|
301
|
+
target: "kimi-k2.5"
|
|
302
|
+
is_regex: true
|
|
303
|
+
- pattern: "claude-haiku-.*"
|
|
304
|
+
vendors: ["kimi"]
|
|
305
|
+
target: "kimi-k2.5"
|
|
306
|
+
is_regex: true
|
|
307
|
+
# 豆包 Doubao 模型映射(三个不同模型对应不同 Claude 模型槽位)
|
|
308
|
+
- pattern: "claude-opus-.*"
|
|
309
|
+
vendors: ["doubao"]
|
|
310
|
+
target: "doubao-seed-2.0-code"
|
|
311
|
+
is_regex: true
|
|
312
|
+
- pattern: "claude-sonnet-.*"
|
|
313
|
+
vendors: ["doubao"]
|
|
314
|
+
target: "doubao-seed-2.0-pro"
|
|
315
|
+
is_regex: true
|
|
316
|
+
- pattern: "claude-haiku-.*"
|
|
317
|
+
vendors: ["doubao"]
|
|
318
|
+
target: "doubao-seed-2.0-lite"
|
|
319
|
+
is_regex: true
|
|
169
320
|
|
|
170
321
|
# === 模型定价(USD / 1M tokens) ===
|
|
171
322
|
# vendor: 供应商名称(对应 `usage` 统计表中的"供应商"列)
|
|
@@ -271,6 +422,64 @@ pricing:
|
|
|
271
422
|
input_cost_per_mtok: ¥4.00
|
|
272
423
|
output_cost_per_mtok: ¥18.00
|
|
273
424
|
cache_read_cost_per_mtok: ¥1.00
|
|
425
|
+
# ── MiniMax ──
|
|
426
|
+
- vendor: minimax
|
|
427
|
+
model: minimax-m2.7
|
|
428
|
+
input_cost_per_mtok: ¥2.10
|
|
429
|
+
output_cost_per_mtok: ¥8.40
|
|
430
|
+
cache_write_cost_per_mtok: ¥2.625
|
|
431
|
+
cache_read_cost_per_mtok: ¥0.42
|
|
432
|
+
- vendor: minimax
|
|
433
|
+
model: minimax-m2.5
|
|
434
|
+
input_cost_per_mtok: ¥2.10
|
|
435
|
+
output_cost_per_mtok: ¥8.40
|
|
436
|
+
cache_write_cost_per_mtok: ¥2.625
|
|
437
|
+
cache_read_cost_per_mtok: ¥0.21
|
|
438
|
+
# ── 阿里 Qwen ──
|
|
439
|
+
- vendor: alibaba
|
|
440
|
+
model: qwen3.6-plus
|
|
441
|
+
input_cost_per_mtok: $0.276
|
|
442
|
+
output_cost_per_mtok: $1.651
|
|
443
|
+
# ── 小米 MiMo ──
|
|
444
|
+
- vendor: xiaomi
|
|
445
|
+
model: mimo-v2-pro
|
|
446
|
+
input_cost_per_mtok: ¥7.00
|
|
447
|
+
output_cost_per_mtok: ¥21.00
|
|
448
|
+
cache_read_cost_per_mtok: ¥1.40
|
|
449
|
+
- vendor: xiaomi
|
|
450
|
+
model: mimo-v2-omni
|
|
451
|
+
input_cost_per_mtok: ¥2.80
|
|
452
|
+
output_cost_per_mtok: ¥14.00
|
|
453
|
+
cache_read_cost_per_mtok: ¥0.56
|
|
454
|
+
- vendor: xiaomi
|
|
455
|
+
model: mimo-v2-flash
|
|
456
|
+
input_cost_per_mtok: ¥0.70
|
|
457
|
+
output_cost_per_mtok: ¥2.10
|
|
458
|
+
cache_read_cost_per_mtok: ¥0.07
|
|
459
|
+
# ── Kimi ──
|
|
460
|
+
- vendor: kimi
|
|
461
|
+
model: kimi-k2.5
|
|
462
|
+
input_cost_per_mtok: $0.60
|
|
463
|
+
output_cost_per_mtok: $3.00
|
|
464
|
+
# ── 豆包 Doubao ──
|
|
465
|
+
- vendor: doubao
|
|
466
|
+
model: doubao-seed-2.0-code
|
|
467
|
+
input_cost_per_mtok: ¥3.20
|
|
468
|
+
output_cost_per_mtok: ¥16.00
|
|
469
|
+
cache_write_cost_per_mtok: ¥0.64
|
|
470
|
+
cache_read_cost_per_mtok: ¥0.017
|
|
471
|
+
- vendor: doubao
|
|
472
|
+
model: doubao-seed-2.0-pro
|
|
473
|
+
input_cost_per_mtok: ¥3.20
|
|
474
|
+
output_cost_per_mtok: ¥16.00
|
|
475
|
+
cache_write_cost_per_mtok: ¥0.64
|
|
476
|
+
cache_read_cost_per_mtok: ¥0.017
|
|
477
|
+
- vendor: doubao
|
|
478
|
+
model: doubao-seed-2.0-lite
|
|
479
|
+
input_cost_per_mtok: ¥0.60
|
|
480
|
+
output_cost_per_mtok: ¥3.60
|
|
481
|
+
cache_write_cost_per_mtok: ¥0.12
|
|
482
|
+
cache_read_cost_per_mtok: ¥0.017
|
|
274
483
|
|
|
275
484
|
database:
|
|
276
485
|
path: "~/.coding-proxy/usage.db"
|
coding/proxy/config/routing.py
CHANGED
|
@@ -59,19 +59,36 @@ _ANTIGRAVITY_FIELDS: frozenset[str] = frozenset(
|
|
|
59
59
|
"model_endpoint",
|
|
60
60
|
}
|
|
61
61
|
)
|
|
62
|
-
|
|
62
|
+
_NATIVE_ANTHROPIC_FIELDS: frozenset[str] = frozenset(
|
|
63
63
|
{
|
|
64
64
|
"api_key",
|
|
65
65
|
}
|
|
66
66
|
)
|
|
67
|
+
# 向后兼容别名
|
|
68
|
+
_ZHIPU_FIELDS = _NATIVE_ANTHROPIC_FIELDS
|
|
67
69
|
|
|
68
70
|
_VENDOR_EXCLUSIVE_FIELDS: dict[str, frozenset[str]] = {
|
|
69
71
|
"copilot": _COPILOT_FIELDS,
|
|
70
72
|
"antigravity": _ANTIGRAVITY_FIELDS,
|
|
71
|
-
"zhipu":
|
|
73
|
+
"zhipu": _NATIVE_ANTHROPIC_FIELDS,
|
|
74
|
+
"minimax": _NATIVE_ANTHROPIC_FIELDS,
|
|
75
|
+
"kimi": _NATIVE_ANTHROPIC_FIELDS,
|
|
76
|
+
"doubao": _NATIVE_ANTHROPIC_FIELDS,
|
|
77
|
+
"xiaomi": _NATIVE_ANTHROPIC_FIELDS,
|
|
78
|
+
"alibaba": _NATIVE_ANTHROPIC_FIELDS,
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
VendorType = Literal[
|
|
81
|
+
VendorType = Literal[
|
|
82
|
+
"anthropic",
|
|
83
|
+
"copilot",
|
|
84
|
+
"antigravity",
|
|
85
|
+
"zhipu",
|
|
86
|
+
"minimax",
|
|
87
|
+
"kimi",
|
|
88
|
+
"doubao",
|
|
89
|
+
"xiaomi",
|
|
90
|
+
"alibaba",
|
|
91
|
+
]
|
|
75
92
|
|
|
76
93
|
|
|
77
94
|
class ModelMappingRule(BaseModel):
|
|
@@ -253,10 +270,10 @@ class VendorConfig(BaseModel):
|
|
|
253
270
|
description="[antigravity] Antigravity 模型端点路径",
|
|
254
271
|
)
|
|
255
272
|
|
|
256
|
-
# ──
|
|
273
|
+
# ── 原生 Anthropic 兼容供应商共用字段 ────────────────────────────
|
|
257
274
|
api_key: str = Field(
|
|
258
275
|
default="",
|
|
259
|
-
description="[zhipu]
|
|
276
|
+
description="[zhipu/minimax/kimi/doubao/xiaomi/alibaba] 原生 Anthropic 兼容端点 API Key",
|
|
260
277
|
)
|
|
261
278
|
|
|
262
279
|
# ── 弹性配置 ──────────────────────────────────────────────
|
|
@@ -278,6 +295,8 @@ class VendorConfig(BaseModel):
|
|
|
278
295
|
if vendor_type == self.vendor:
|
|
279
296
|
continue
|
|
280
297
|
for field_name in fields:
|
|
298
|
+
if field_name in exclusive:
|
|
299
|
+
continue
|
|
281
300
|
value = getattr(self, field_name, None)
|
|
282
301
|
if value and value != getattr(
|
|
283
302
|
VendorConfig.model_fields[field_name], "default", None
|
|
@@ -307,6 +326,7 @@ __all__ = [
|
|
|
307
326
|
"_COPILOT_FIELDS",
|
|
308
327
|
"_ANTIGRAVITY_FIELDS",
|
|
309
328
|
"_ZHIPU_FIELDS",
|
|
329
|
+
"_NATIVE_ANTHROPIC_FIELDS",
|
|
310
330
|
"_VENDOR_EXCLUSIVE_FIELDS",
|
|
311
331
|
"_BACKEND_EXCLUSIVE_FIELDS",
|
|
312
332
|
]
|
coding/proxy/config/schema.py
CHANGED
|
@@ -30,6 +30,7 @@ from .routing import ( # noqa: F401
|
|
|
30
30
|
_ANTIGRAVITY_FIELDS,
|
|
31
31
|
_BACKEND_EXCLUSIVE_FIELDS, # 向后兼容别名
|
|
32
32
|
_COPILOT_FIELDS,
|
|
33
|
+
_NATIVE_ANTHROPIC_FIELDS,
|
|
33
34
|
_VENDOR_EXCLUSIVE_FIELDS,
|
|
34
35
|
_ZHIPU_FIELDS,
|
|
35
36
|
BackendType, # 向后兼容别名
|
|
@@ -43,9 +44,14 @@ from .routing import ( # noqa: F401
|
|
|
43
44
|
# ── 子模块 re-export ────────────────────────────────────────────
|
|
44
45
|
from .server import DatabaseConfig, LoggingConfig, ServerConfig # noqa: F401
|
|
45
46
|
from .vendors import ( # noqa: F401
|
|
47
|
+
AlibabaConfig,
|
|
46
48
|
AnthropicConfig,
|
|
47
49
|
AntigravityConfig,
|
|
48
50
|
CopilotConfig,
|
|
51
|
+
DoubaoConfig,
|
|
52
|
+
KimiConfig,
|
|
53
|
+
MinimaxConfig,
|
|
54
|
+
XiaomiConfig,
|
|
49
55
|
ZhipuConfig,
|
|
50
56
|
)
|
|
51
57
|
|
|
@@ -303,8 +309,15 @@ __all__ = [
|
|
|
303
309
|
"_COPILOT_FIELDS",
|
|
304
310
|
"_ANTIGRAVITY_FIELDS",
|
|
305
311
|
"_ZHIPU_FIELDS",
|
|
312
|
+
"_NATIVE_ANTHROPIC_FIELDS",
|
|
306
313
|
"_VENDOR_EXCLUSIVE_FIELDS",
|
|
307
314
|
"_BACKEND_EXCLUSIVE_FIELDS",
|
|
308
315
|
# auth
|
|
309
316
|
"AuthConfig",
|
|
317
|
+
# new native anthropic vendor configs
|
|
318
|
+
"MinimaxConfig",
|
|
319
|
+
"KimiConfig",
|
|
320
|
+
"DoubaoConfig",
|
|
321
|
+
"XiaomiConfig",
|
|
322
|
+
"AlibabaConfig",
|
|
310
323
|
]
|
coding/proxy/config/vendors.py
CHANGED
|
@@ -49,9 +49,59 @@ class ZhipuConfig(BaseModel):
|
|
|
49
49
|
timeout_ms: int = 3000000
|
|
50
50
|
|
|
51
51
|
|
|
52
|
+
class MinimaxConfig(BaseModel):
|
|
53
|
+
"""MiniMax 供应商配置(原生 Anthropic 兼容端点)."""
|
|
54
|
+
|
|
55
|
+
enabled: bool = True
|
|
56
|
+
base_url: str = "https://api.minimaxi.com/anthropic"
|
|
57
|
+
api_key: str = ""
|
|
58
|
+
timeout_ms: int = 3000000
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class KimiConfig(BaseModel):
|
|
62
|
+
"""Kimi 供应商配置(原生 Anthropic 兼容端点)."""
|
|
63
|
+
|
|
64
|
+
enabled: bool = True
|
|
65
|
+
base_url: str = "https://api.kimi.com/coding/"
|
|
66
|
+
api_key: str = ""
|
|
67
|
+
timeout_ms: int = 3000000
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class DoubaoConfig(BaseModel):
|
|
71
|
+
"""豆包 Doubao 供应商配置(原生 Anthropic 兼容端点)."""
|
|
72
|
+
|
|
73
|
+
enabled: bool = True
|
|
74
|
+
base_url: str = "https://ark.cn-beijing.volces.com/api/coding"
|
|
75
|
+
api_key: str = ""
|
|
76
|
+
timeout_ms: int = 3000000
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class XiaomiConfig(BaseModel):
|
|
80
|
+
"""小米 MiMo 供应商配置(原生 Anthropic 兼容端点)."""
|
|
81
|
+
|
|
82
|
+
enabled: bool = True
|
|
83
|
+
base_url: str = "https://token-plan-cn.xiaomimimo.com/anthropic"
|
|
84
|
+
api_key: str = ""
|
|
85
|
+
timeout_ms: int = 3000000
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class AlibabaConfig(BaseModel):
|
|
89
|
+
"""阿里 Qwen 供应商配置(原生 Anthropic 兼容端点)."""
|
|
90
|
+
|
|
91
|
+
enabled: bool = True
|
|
92
|
+
base_url: str = "https://coding-intl.dashscope.aliyuncs.com/apps/anthropic"
|
|
93
|
+
api_key: str = ""
|
|
94
|
+
timeout_ms: int = 3000000
|
|
95
|
+
|
|
96
|
+
|
|
52
97
|
__all__ = [
|
|
53
98
|
"AnthropicConfig",
|
|
54
99
|
"CopilotConfig",
|
|
55
100
|
"AntigravityConfig",
|
|
56
101
|
"ZhipuConfig",
|
|
102
|
+
"MinimaxConfig",
|
|
103
|
+
"KimiConfig",
|
|
104
|
+
"DoubaoConfig",
|
|
105
|
+
"XiaomiConfig",
|
|
106
|
+
"AlibabaConfig",
|
|
57
107
|
]
|
coding/proxy/logging/db.py
CHANGED
|
@@ -374,8 +374,8 @@ class TokenLogger:
|
|
|
374
374
|
*,
|
|
375
375
|
period: TimePeriod = TimePeriod.DAY,
|
|
376
376
|
count: int = 7,
|
|
377
|
-
vendor: str | None = None,
|
|
378
|
-
model: str | None = None,
|
|
377
|
+
vendor: str | list[str] | None = None,
|
|
378
|
+
model: str | list[str] | None = None,
|
|
379
379
|
) -> list[dict]:
|
|
380
380
|
"""按指定时间维度聚合 Token 使用统计.
|
|
381
381
|
|
|
@@ -383,8 +383,8 @@ class TokenLogger:
|
|
|
383
383
|
period: 时间维度(日/周/月/全量)。
|
|
384
384
|
count: ``period`` 的数量。仅用于计算起始时间边界,
|
|
385
385
|
``TOTAL`` 维度下忽略此参数。
|
|
386
|
-
vendor:
|
|
387
|
-
model:
|
|
386
|
+
vendor: 过滤供应商,支持单个字符串或字符串列表(多 vendor 过滤)。
|
|
387
|
+
model: 过滤实际服务模型(model_served),支持单个字符串或字符串列表。
|
|
388
388
|
"""
|
|
389
389
|
if not self._db:
|
|
390
390
|
return []
|
|
@@ -411,11 +411,15 @@ class TokenLogger:
|
|
|
411
411
|
params.append(start_iso)
|
|
412
412
|
|
|
413
413
|
if vendor:
|
|
414
|
-
|
|
415
|
-
|
|
414
|
+
vendors = [vendor] if isinstance(vendor, str) else vendor
|
|
415
|
+
placeholders = ",".join("?" * len(vendors))
|
|
416
|
+
sql += f" AND vendor IN ({placeholders})"
|
|
417
|
+
params.extend(vendors)
|
|
416
418
|
if model:
|
|
417
|
-
|
|
418
|
-
|
|
419
|
+
models = [model] if isinstance(model, str) else model
|
|
420
|
+
placeholders = ",".join("?" * len(models))
|
|
421
|
+
sql += f" AND model_served IN ({placeholders})"
|
|
422
|
+
params.extend(models)
|
|
419
423
|
|
|
420
424
|
sql += f" GROUP BY {group_clause} ORDER BY {order_clause}"
|
|
421
425
|
|
coding/proxy/logging/stats.py
CHANGED
|
@@ -95,8 +95,8 @@ _PERIOD_DATE_LABELS: dict[TimePeriod, str] = {
|
|
|
95
95
|
async def show_usage(
|
|
96
96
|
logger: TokenLogger,
|
|
97
97
|
*,
|
|
98
|
-
vendor: str | None = None,
|
|
99
|
-
model: str | None = None,
|
|
98
|
+
vendor: str | list[str] | None = None,
|
|
99
|
+
model: str | list[str] | None = None,
|
|
100
100
|
pricing_table: PricingTable | None = None,
|
|
101
101
|
period: TimePeriod = TimePeriod.DAY,
|
|
102
102
|
count: int = 7,
|
|
@@ -126,6 +126,15 @@ async def show_usage(
|
|
|
126
126
|
table.add_column("Cost", justify="right", style="bold green")
|
|
127
127
|
table.add_column("平均耗时(ms)", justify="right")
|
|
128
128
|
|
|
129
|
+
# ── 汇总累计变量 ──────────────────────────────────────────
|
|
130
|
+
sum_requests = 0
|
|
131
|
+
sum_input = 0
|
|
132
|
+
sum_output = 0
|
|
133
|
+
sum_cache_creation = 0
|
|
134
|
+
sum_cache_read = 0
|
|
135
|
+
weighted_duration_sum = 0.0 # Σ(avg_duration_ms × total_requests)
|
|
136
|
+
cost_totals: dict = {} # currency → float
|
|
137
|
+
|
|
129
138
|
for row in rows:
|
|
130
139
|
total_input = row.get("total_input", 0) or 0
|
|
131
140
|
total_output = row.get("total_output", 0) or 0
|
|
@@ -137,6 +146,7 @@ async def show_usage(
|
|
|
137
146
|
|
|
138
147
|
vendor_name = str(row.get("vendor", ""))
|
|
139
148
|
model_served = str(row.get("model_served", ""))
|
|
149
|
+
cost_value = None
|
|
140
150
|
if pricing_table is not None:
|
|
141
151
|
cost_value = pricing_table.compute_cost(
|
|
142
152
|
vendor_name,
|
|
@@ -150,13 +160,27 @@ async def show_usage(
|
|
|
150
160
|
else:
|
|
151
161
|
cost_str = "-"
|
|
152
162
|
|
|
163
|
+
# 累加汇总
|
|
164
|
+
total_requests_row = row.get("total_requests", 0) or 0
|
|
165
|
+
sum_requests += total_requests_row
|
|
166
|
+
sum_input += total_input
|
|
167
|
+
sum_output += total_output
|
|
168
|
+
sum_cache_creation += total_cache_creation
|
|
169
|
+
sum_cache_read += total_cache_read
|
|
170
|
+
weighted_duration_sum += (
|
|
171
|
+
row.get("avg_duration_ms", 0) or 0
|
|
172
|
+
) * total_requests_row
|
|
173
|
+
if cost_value is not None:
|
|
174
|
+
cur = cost_value.currency
|
|
175
|
+
cost_totals[cur] = cost_totals.get(cur, 0.0) + cost_value.amount
|
|
176
|
+
|
|
153
177
|
date_value = row.get("date") or ""
|
|
154
178
|
table.add_row(
|
|
155
179
|
str(date_value),
|
|
156
180
|
vendor_name,
|
|
157
181
|
_format_model_display(row.get("model_requested")),
|
|
158
182
|
model_served,
|
|
159
|
-
str(
|
|
183
|
+
str(total_requests_row),
|
|
160
184
|
_format_tokens(total_input),
|
|
161
185
|
_format_tokens(total_output),
|
|
162
186
|
_format_tokens(total_cache_creation),
|
|
@@ -166,6 +190,34 @@ async def show_usage(
|
|
|
166
190
|
str(int(row.get("avg_duration_ms", 0) or 0)),
|
|
167
191
|
)
|
|
168
192
|
|
|
193
|
+
# ── 汇总行 ───────────────────────────────────────────────
|
|
194
|
+
table.add_section()
|
|
195
|
+
|
|
196
|
+
sum_tokens = sum_input + sum_output + sum_cache_creation + sum_cache_read
|
|
197
|
+
avg_duration = int(weighted_duration_sum / sum_requests) if sum_requests else 0
|
|
198
|
+
|
|
199
|
+
if cost_totals:
|
|
200
|
+
total_cost_str = " + ".join(
|
|
201
|
+
f"{cur.symbol}{amt:.4f}" for cur, amt in cost_totals.items()
|
|
202
|
+
)
|
|
203
|
+
else:
|
|
204
|
+
total_cost_str = "-"
|
|
205
|
+
|
|
206
|
+
table.add_row(
|
|
207
|
+
"[bold]总计[/bold]",
|
|
208
|
+
"",
|
|
209
|
+
"",
|
|
210
|
+
"",
|
|
211
|
+
f"[bold]{sum_requests}[/bold]",
|
|
212
|
+
f"[bold]{_format_tokens(sum_input)}[/bold]",
|
|
213
|
+
f"[bold]{_format_tokens(sum_output)}[/bold]",
|
|
214
|
+
f"[bold]{_format_tokens(sum_cache_creation)}[/bold]",
|
|
215
|
+
f"[bold]{_format_tokens(sum_cache_read)}[/bold]",
|
|
216
|
+
f"[bold]{_format_tokens(sum_tokens)}[/bold]",
|
|
217
|
+
f"[bold]{total_cost_str}[/bold]",
|
|
218
|
+
f"[bold]{avg_duration}[/bold]",
|
|
219
|
+
)
|
|
220
|
+
|
|
169
221
|
console.print(table)
|
|
170
222
|
|
|
171
223
|
# 故障转移来源汇总(使用与主查询相同的时间范围)
|
coding/proxy/routing/executor.py
CHANGED
|
@@ -168,6 +168,11 @@ def _log_vendor_response_error(
|
|
|
168
168
|
_VENDOR_PROTOCOL_LABEL_MAP: dict[str, str] = {
|
|
169
169
|
"anthropic": "Anthropic",
|
|
170
170
|
"zhipu": "Anthropic",
|
|
171
|
+
"minimax": "Anthropic",
|
|
172
|
+
"kimi": "Anthropic",
|
|
173
|
+
"doubao": "Anthropic",
|
|
174
|
+
"xiaomi": "Anthropic",
|
|
175
|
+
"alibaba": "Anthropic",
|
|
171
176
|
"copilot": "OpenAI",
|
|
172
177
|
"antigravity": "Gemini",
|
|
173
178
|
}
|
|
@@ -129,7 +129,7 @@ class QuotaGuard:
|
|
|
129
129
|
int(self._effective_probe_interval),
|
|
130
130
|
)
|
|
131
131
|
|
|
132
|
-
def load_baseline(self, total_tokens: int) -> None:
|
|
132
|
+
def load_baseline(self, total_tokens: int, vendor: str | None = None) -> None:
|
|
133
133
|
"""从数据库加载窗口历史用量基线."""
|
|
134
134
|
if not self._enabled or total_tokens <= 0:
|
|
135
135
|
return
|
|
@@ -137,7 +137,14 @@ class QuotaGuard:
|
|
|
137
137
|
midpoint = time.monotonic() - self._window / 2
|
|
138
138
|
self._entries.append((midpoint, total_tokens))
|
|
139
139
|
self._total += total_tokens
|
|
140
|
-
|
|
140
|
+
if vendor:
|
|
141
|
+
logger.info(
|
|
142
|
+
"Quota guard [%s]: loaded baseline %d tokens",
|
|
143
|
+
vendor,
|
|
144
|
+
total_tokens,
|
|
145
|
+
)
|
|
146
|
+
else:
|
|
147
|
+
logger.info("Quota guard: loaded baseline %d tokens", total_tokens)
|
|
141
148
|
|
|
142
149
|
def reset(self) -> None:
|
|
143
150
|
"""手动重置为 WITHIN_QUOTA 状态."""
|
|
@@ -39,6 +39,11 @@ class RouteSessionManager:
|
|
|
39
39
|
"copilot": "openai_chat_completions",
|
|
40
40
|
"antigravity": "gemini_generate_content",
|
|
41
41
|
"zhipu": "anthropic_messages",
|
|
42
|
+
"minimax": "anthropic_messages",
|
|
43
|
+
"kimi": "anthropic_messages",
|
|
44
|
+
"doubao": "anthropic_messages",
|
|
45
|
+
"xiaomi": "anthropic_messages",
|
|
46
|
+
"alibaba": "anthropic_messages",
|
|
42
47
|
"anthropic": "anthropic_messages",
|
|
43
48
|
}.get(tier.name, "unknown")
|
|
44
49
|
compat_trace = CompatibilityTrace(
|
coding/proxy/server/app.py
CHANGED
|
@@ -60,17 +60,14 @@ async def lifespan(app: FastAPI):
|
|
|
60
60
|
tier.quota_guard.window_hours,
|
|
61
61
|
vendor=tier.name,
|
|
62
62
|
)
|
|
63
|
-
tier.quota_guard.load_baseline(total)
|
|
63
|
+
tier.quota_guard.load_baseline(total, vendor=tier.name)
|
|
64
64
|
if tier.weekly_quota_guard and tier.weekly_quota_guard.enabled:
|
|
65
65
|
total = await token_logger.query_window_total(
|
|
66
66
|
tier.weekly_quota_guard.window_hours,
|
|
67
67
|
vendor=tier.name,
|
|
68
68
|
)
|
|
69
|
-
tier.weekly_quota_guard.load_baseline(total)
|
|
69
|
+
tier.weekly_quota_guard.load_baseline(total, vendor=tier.name)
|
|
70
70
|
|
|
71
|
-
logger.info(
|
|
72
|
-
"coding-proxy started: host=%s port=%d", config.server.host, config.server.port
|
|
73
|
-
)
|
|
74
71
|
yield
|
|
75
72
|
await router.close()
|
|
76
73
|
await compat_session_store.close()
|