coding-proxy 0.5.1a7__py3-none-any.whl → 0.5.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.
- coding/proxy/config/config.default.yaml +55 -34
- coding/proxy/config/session_policy.py +35 -1
- coding/proxy/model/__init__.py +2 -0
- coding/proxy/model/vendor.py +15 -0
- coding/proxy/routing/executor.py +78 -14
- coding/proxy/routing/quota_guard.py +31 -7
- coding/proxy/routing/retry.py +13 -4
- coding/proxy/routing/router.py +2 -0
- coding/proxy/server/app.py +1 -0
- coding/proxy/server/dashboard.py +355 -31
- coding/proxy/server/routes.py +48 -0
- coding/proxy/vendors/antigravity.py +5 -2
- coding/proxy/vendors/base.py +2 -1
- coding/proxy/vendors/copilot.py +2 -1
- coding/proxy/vendors/zhipu.py +43 -35
- {coding_proxy-0.5.1a7.dist-info → coding_proxy-0.5.2.dist-info}/METADATA +5 -6
- {coding_proxy-0.5.1a7.dist-info → coding_proxy-0.5.2.dist-info}/RECORD +20 -20
- {coding_proxy-0.5.1a7.dist-info → coding_proxy-0.5.2.dist-info}/WHEEL +1 -1
- {coding_proxy-0.5.1a7.dist-info → coding_proxy-0.5.2.dist-info}/entry_points.txt +0 -0
- {coding_proxy-0.5.1a7.dist-info → coding_proxy-0.5.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -228,51 +228,51 @@ failover:
|
|
|
228
228
|
# === 模型映射 ===
|
|
229
229
|
model_mapping:
|
|
230
230
|
# GitHub Copilot 可用模型(通过 /api/copilot/models 诊断获取)
|
|
231
|
-
- pattern: "claude-sonnet-.*"
|
|
232
|
-
vendors: ["copilot"]
|
|
233
|
-
target: "claude-sonnet-4.6"
|
|
234
|
-
is_regex: true
|
|
235
231
|
- pattern: "claude-opus-.*"
|
|
236
232
|
vendors: ["copilot"]
|
|
237
233
|
target: "claude-opus-4.6"
|
|
238
234
|
is_regex: true
|
|
235
|
+
- pattern: "claude-sonnet-.*"
|
|
236
|
+
vendors: ["copilot"]
|
|
237
|
+
target: "claude-sonnet-4.6"
|
|
238
|
+
is_regex: true
|
|
239
239
|
- pattern: "claude-haiku-.*"
|
|
240
240
|
vendors: ["copilot"]
|
|
241
241
|
target: "claude-haiku-4.5"
|
|
242
242
|
is_regex: true
|
|
243
243
|
# Google Antigravity 模型映射
|
|
244
|
-
- pattern: "claude-sonnet-.*"
|
|
245
|
-
vendors: ["antigravity"]
|
|
246
|
-
target: "Claude Sonnet 4.6"
|
|
247
|
-
is_regex: true
|
|
248
244
|
- pattern: "claude-opus-.*"
|
|
249
245
|
vendors: ["antigravity"]
|
|
250
246
|
target: "Claude Opus 4.6 Thinking"
|
|
251
247
|
is_regex: true
|
|
248
|
+
- pattern: "claude-sonnet-.*"
|
|
249
|
+
vendors: ["antigravity"]
|
|
250
|
+
target: "Claude Sonnet 4.6"
|
|
251
|
+
is_regex: true
|
|
252
252
|
- pattern: "claude-haiku-.*"
|
|
253
253
|
vendors: ["antigravity"]
|
|
254
254
|
target: "Gemini 3 Flash"
|
|
255
255
|
is_regex: true
|
|
256
256
|
# 智谱 GLM 映射(官方原生 Anthropic 兼容端点)
|
|
257
257
|
# glm-5v-turbo 完整承载 Claude Code 满血能力(tool calling / thinking / 多模态)
|
|
258
|
-
- pattern: "claude-
|
|
258
|
+
- pattern: "claude-opus-.*"
|
|
259
259
|
vendors: ["zhipu"]
|
|
260
|
-
target: "glm-
|
|
260
|
+
target: "glm-5.2"
|
|
261
261
|
is_regex: true
|
|
262
|
-
- pattern: "claude-
|
|
262
|
+
- pattern: "claude-sonnet-.*"
|
|
263
263
|
vendors: ["zhipu"]
|
|
264
264
|
target: "glm-5.1"
|
|
265
265
|
is_regex: true
|
|
266
266
|
- pattern: "claude-haiku-.*"
|
|
267
267
|
vendors: ["zhipu"]
|
|
268
|
-
target: "glm-
|
|
268
|
+
target: "glm-5.1"
|
|
269
269
|
is_regex: true
|
|
270
270
|
# MiniMax 模型映射(原生 Anthropic 兼容端点)
|
|
271
|
-
- pattern: "claude-
|
|
271
|
+
- pattern: "claude-opus-.*"
|
|
272
272
|
vendors: ["minimax"]
|
|
273
273
|
target: "minimax-m2.7"
|
|
274
274
|
is_regex: true
|
|
275
|
-
- pattern: "claude-
|
|
275
|
+
- pattern: "claude-sonnet-.*"
|
|
276
276
|
vendors: ["minimax"]
|
|
277
277
|
target: "minimax-m2.7"
|
|
278
278
|
is_regex: true
|
|
@@ -281,11 +281,11 @@ model_mapping:
|
|
|
281
281
|
target: "minimax-m2.7"
|
|
282
282
|
is_regex: true
|
|
283
283
|
# 阿里 Qwen 模型映射(原生 Anthropic 兼容端点)
|
|
284
|
-
- pattern: "claude-
|
|
284
|
+
- pattern: "claude-opus-.*"
|
|
285
285
|
vendors: ["alibaba"]
|
|
286
286
|
target: "qwen3.6-plus"
|
|
287
287
|
is_regex: true
|
|
288
|
-
- pattern: "claude-
|
|
288
|
+
- pattern: "claude-sonnet-.*"
|
|
289
289
|
vendors: ["alibaba"]
|
|
290
290
|
target: "qwen3.6-plus"
|
|
291
291
|
is_regex: true
|
|
@@ -294,11 +294,11 @@ model_mapping:
|
|
|
294
294
|
target: "qwen3.6-plus"
|
|
295
295
|
is_regex: true
|
|
296
296
|
# 小米 MiMo 模型映射(原生 Anthropic 兼容端点)
|
|
297
|
-
- pattern: "claude-
|
|
297
|
+
- pattern: "claude-opus-.*"
|
|
298
298
|
vendors: ["xiaomi"]
|
|
299
299
|
target: "mimo-v2-pro"
|
|
300
300
|
is_regex: true
|
|
301
|
-
- pattern: "claude-
|
|
301
|
+
- pattern: "claude-sonnet-.*"
|
|
302
302
|
vendors: ["xiaomi"]
|
|
303
303
|
target: "mimo-v2-pro"
|
|
304
304
|
is_regex: true
|
|
@@ -307,11 +307,11 @@ model_mapping:
|
|
|
307
307
|
target: "mimo-v2-pro"
|
|
308
308
|
is_regex: true
|
|
309
309
|
# Kimi 模型映射(原生 Anthropic 兼容端点)
|
|
310
|
-
- pattern: "claude-
|
|
310
|
+
- pattern: "claude-opus-.*"
|
|
311
311
|
vendors: ["kimi"]
|
|
312
312
|
target: "kimi-k2.5"
|
|
313
313
|
is_regex: true
|
|
314
|
-
- pattern: "claude-
|
|
314
|
+
- pattern: "claude-sonnet-.*"
|
|
315
315
|
vendors: ["kimi"]
|
|
316
316
|
target: "kimi-k2.5"
|
|
317
317
|
is_regex: true
|
|
@@ -339,6 +339,12 @@ model_mapping:
|
|
|
339
339
|
# 未配置定价的模型在统计中 Cost 列显示 "-"
|
|
340
340
|
pricing:
|
|
341
341
|
# ── Anthropic Claude ──
|
|
342
|
+
- vendor: anthropic
|
|
343
|
+
model: claude-fable-5
|
|
344
|
+
input_cost_per_mtok: $10.0
|
|
345
|
+
output_cost_per_mtok: $50.0
|
|
346
|
+
cache_write_cost_per_mtok: $12.50
|
|
347
|
+
cache_read_cost_per_mtok: $1.00
|
|
342
348
|
- vendor: anthropic
|
|
343
349
|
model: claude-opus-4-8
|
|
344
350
|
input_cost_per_mtok: $5.0
|
|
@@ -527,20 +533,10 @@ pricing:
|
|
|
527
533
|
cache_read_cost_per_mtok: $0.05
|
|
528
534
|
# ── 智谱 GLM ──
|
|
529
535
|
- vendor: zhipu
|
|
530
|
-
model: glm-
|
|
531
|
-
input_cost_per_mtok: ¥
|
|
532
|
-
output_cost_per_mtok: ¥
|
|
533
|
-
cache_read_cost_per_mtok: ¥
|
|
534
|
-
- vendor: zhipu
|
|
535
|
-
model: glm-4.7 # 待区分长短上下文定价
|
|
536
|
-
input_cost_per_mtok: ¥2.00
|
|
537
|
-
output_cost_per_mtok: ¥8.00
|
|
538
|
-
cache_read_cost_per_mtok: ¥0.40
|
|
539
|
-
- vendor: zhipu
|
|
540
|
-
model: glm-5v-turbo # 待区分长短上下文定价
|
|
541
|
-
input_cost_per_mtok: ¥5.00
|
|
542
|
-
output_cost_per_mtok: ¥22.00
|
|
543
|
-
cache_read_cost_per_mtok: ¥1.20
|
|
536
|
+
model: glm-5.2 # 待区分长短上下文定价
|
|
537
|
+
input_cost_per_mtok: ¥8.00
|
|
538
|
+
output_cost_per_mtok: ¥28.00
|
|
539
|
+
cache_read_cost_per_mtok: ¥2.00
|
|
544
540
|
- vendor: zhipu
|
|
545
541
|
model: glm-5.1 # 待区分长短上下文定价
|
|
546
542
|
input_cost_per_mtok: ¥6.00
|
|
@@ -556,6 +552,21 @@ pricing:
|
|
|
556
552
|
input_cost_per_mtok: ¥4.00
|
|
557
553
|
output_cost_per_mtok: ¥18.00
|
|
558
554
|
cache_read_cost_per_mtok: ¥1.00
|
|
555
|
+
- vendor: zhipu
|
|
556
|
+
model: glm-5v-turbo # 待区分长短上下文定价
|
|
557
|
+
input_cost_per_mtok: ¥5.00
|
|
558
|
+
output_cost_per_mtok: ¥22.00
|
|
559
|
+
cache_read_cost_per_mtok: ¥1.20
|
|
560
|
+
- vendor: zhipu
|
|
561
|
+
model: glm-4.7 # 待区分长短上下文定价
|
|
562
|
+
input_cost_per_mtok: ¥2.00
|
|
563
|
+
output_cost_per_mtok: ¥8.00
|
|
564
|
+
cache_read_cost_per_mtok: ¥0.40
|
|
565
|
+
- vendor: zhipu
|
|
566
|
+
model: glm-4.5-air # 待区分长短上下文定价
|
|
567
|
+
input_cost_per_mtok: ¥0.80
|
|
568
|
+
output_cost_per_mtok: ¥2.00
|
|
569
|
+
cache_read_cost_per_mtok: ¥0.16
|
|
559
570
|
# ── MiniMax ──
|
|
560
571
|
- vendor: minimax
|
|
561
572
|
model: minimax-m2.7
|
|
@@ -693,3 +704,13 @@ session_policies:
|
|
|
693
704
|
title_vendor_bindings:
|
|
694
705
|
- prefix: "# 目标"
|
|
695
706
|
vendor: "zhipu"
|
|
707
|
+
# Session 标题豁免前缀:标题提取每一层(user 文本 / 工具结果 / 图片 / 元数据兜底)
|
|
708
|
+
# 产出的候选以任一前缀开头则跳过,继续向下一层回退。既用于过滤注入式 Prompt
|
|
709
|
+
# (如示例语言指令),也可豁免无信息量的合成兜底标题。大小写敏感 startswith。
|
|
710
|
+
# 例:添加 "[Session]" 可让 [Session] <model> 这类兜底标题不再写入,
|
|
711
|
+
# session 标题保持空,待后续真实输入经延迟补写回填。
|
|
712
|
+
# ⚠️ 自定义 yaml 中该字段为整体替换(loader 的 list 合并约定,非追加合并);
|
|
713
|
+
# 写空列表即关闭豁免。加载时会自动 strip + 去空 + 去重。
|
|
714
|
+
title_exempt_prefixes:
|
|
715
|
+
- "Write the title in the"
|
|
716
|
+
- "[Session]"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from pydantic import BaseModel, Field
|
|
5
|
+
from pydantic import BaseModel, Field, field_validator
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class SessionPolicyMatch(BaseModel):
|
|
@@ -81,3 +81,37 @@ class SessionPoliciesConfig(BaseModel):
|
|
|
81
81
|
"匹配规则按列表顺序求值,首次匹配生效。"
|
|
82
82
|
),
|
|
83
83
|
)
|
|
84
|
+
title_exempt_prefixes: list[str] = Field(
|
|
85
|
+
default_factory=list,
|
|
86
|
+
description=(
|
|
87
|
+
"Session 标题豁免前缀名单。标题提取的每一层(user TEXT 噪声剥离、"
|
|
88
|
+
"TOOL_RESULT 摘要、IMAGE 计数、元数据兜底)产出的候选,若以任一前缀"
|
|
89
|
+
"开头,则视为豁免、继续向下一层回退;全部层级均被豁免时返回空串,"
|
|
90
|
+
"调用方据此跳过写库,session 标题保持空待后续真实输入回填。"
|
|
91
|
+
"既用于过滤注入式 Prompt(如示例语言指令),也可豁免无信息量的合成"
|
|
92
|
+
"兜底标题(如 [Session]、[Tool call]、[Tool output])。"
|
|
93
|
+
"大小写敏感的 startswith 匹配,与 title_vendor_bindings 语义一致。"
|
|
94
|
+
"加载时会自动 strip + 去空 + 去重;空字符串前缀会被丢弃"
|
|
95
|
+
"(防空串 startswith 恒真导致全量误豁免)。"
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
@field_validator("title_exempt_prefixes", mode="after")
|
|
100
|
+
@classmethod
|
|
101
|
+
def _normalize_exempt_prefixes(cls, value: list[str]) -> list[str]:
|
|
102
|
+
"""归一化豁免前缀:strip + 去空 + 去重保序.
|
|
103
|
+
|
|
104
|
+
空字符串前缀必须丢弃——``"".startswith`` 恒真会豁免一切用户输入,
|
|
105
|
+
导致 Level 1 标题提取永久失效。
|
|
106
|
+
"""
|
|
107
|
+
seen: set[str] = set()
|
|
108
|
+
result: list[str] = []
|
|
109
|
+
for item in value:
|
|
110
|
+
if not isinstance(item, str):
|
|
111
|
+
continue
|
|
112
|
+
cleaned = item.strip()
|
|
113
|
+
if not cleaned or cleaned in seen:
|
|
114
|
+
continue
|
|
115
|
+
seen.add(cleaned)
|
|
116
|
+
result.append(cleaned)
|
|
117
|
+
return result
|
coding/proxy/model/__init__.py
CHANGED
|
@@ -25,6 +25,7 @@ from .vendor import ( # noqa: F401
|
|
|
25
25
|
UsageInfo,
|
|
26
26
|
VendorCapabilities,
|
|
27
27
|
VendorResponse,
|
|
28
|
+
decode_error_body,
|
|
28
29
|
decode_json_body,
|
|
29
30
|
extract_error_message,
|
|
30
31
|
sanitize_headers_for_synthetic_response,
|
|
@@ -83,6 +84,7 @@ __all__ = [
|
|
|
83
84
|
"CopilotModelCatalog",
|
|
84
85
|
"RequestCapabilities",
|
|
85
86
|
"UsageInfo",
|
|
87
|
+
"decode_error_body",
|
|
86
88
|
"decode_json_body",
|
|
87
89
|
"extract_error_message",
|
|
88
90
|
"sanitize_headers_for_synthetic_response",
|
coding/proxy/model/vendor.py
CHANGED
|
@@ -75,6 +75,20 @@ def extract_error_message(
|
|
|
75
75
|
return text[:500] if text else None
|
|
76
76
|
|
|
77
77
|
|
|
78
|
+
def decode_error_body(raw: bytes, limit: int = 500) -> str:
|
|
79
|
+
"""将上游错误响应体(bytes)安全解码为可读文本,供日志展示.
|
|
80
|
+
|
|
81
|
+
直接以 ``%s`` 格式化 ``bytes`` 会走 ``repr()``,导致非 ASCII 的 UTF-8
|
|
82
|
+
字节被转义为 ``\\xe6\\x82\\xa8`` 之类的不可读序列(中文乱码)。本函数:
|
|
83
|
+
|
|
84
|
+
- 使用 ``errors="replace"`` 容忍非法字节,非法字节降级为 ``�`` 而非抛异常,
|
|
85
|
+
确保日志路径绝对健壮;
|
|
86
|
+
- 先整体解码再按字符截断,避免在多字节 UTF-8 边界切断产生乱码
|
|
87
|
+
(因此 ``limit`` 语义为「字符数」而非「字节数」)。
|
|
88
|
+
"""
|
|
89
|
+
return raw.decode("utf-8", errors="replace")[:limit]
|
|
90
|
+
|
|
91
|
+
|
|
78
92
|
# ═══════════════════════════════════════════════════════════════
|
|
79
93
|
# 供应商核心数据类型
|
|
80
94
|
# ═══════════════════════════════════════════════════════════════
|
|
@@ -229,6 +243,7 @@ __all__ = [
|
|
|
229
243
|
"CopilotMisdirectedRequest",
|
|
230
244
|
"CopilotModelCatalog",
|
|
231
245
|
# 工具函数
|
|
246
|
+
"decode_error_body",
|
|
232
247
|
"decode_json_body",
|
|
233
248
|
"extract_error_message",
|
|
234
249
|
"sanitize_headers_for_synthetic_response",
|
coding/proxy/routing/executor.py
CHANGED
|
@@ -148,14 +148,30 @@ def _sanitize_user_text(raw: str) -> str:
|
|
|
148
148
|
# ─────────────────────────────────────────────────────────────────
|
|
149
149
|
|
|
150
150
|
|
|
151
|
-
def _extract_title_from_user_text(
|
|
152
|
-
|
|
151
|
+
def _extract_title_from_user_text(
|
|
152
|
+
messages: list[CanonicalMessagePart],
|
|
153
|
+
exempt_prefixes: list[str] | None = None,
|
|
154
|
+
) -> str:
|
|
155
|
+
"""Level 1: 从 user TEXT 部分提取经噪声剥离后的首条非空文本.
|
|
156
|
+
|
|
157
|
+
``exempt_prefixes`` 非空时,清洗后文本以任一前缀开头则跳过该输入、继续向后
|
|
158
|
+
查找——用于过滤注入式 Prompt,避免其被误用作 Session 标题。匹配作用于
|
|
159
|
+
``_sanitize_user_text`` 清洗后的纯文本(而非带 XML 标签的原始 raw)。
|
|
160
|
+
"""
|
|
153
161
|
for part in messages:
|
|
154
162
|
if part.role != "user" or part.type != CanonicalPartType.TEXT:
|
|
155
163
|
continue
|
|
156
164
|
cleaned = _sanitize_user_text(part.text)
|
|
157
|
-
if cleaned:
|
|
158
|
-
|
|
165
|
+
if not cleaned:
|
|
166
|
+
continue
|
|
167
|
+
if exempt_prefixes and any(cleaned.startswith(p) for p in exempt_prefixes):
|
|
168
|
+
logger.debug(
|
|
169
|
+
"Title candidate skipped by exempt prefix %r (snippet=%r)",
|
|
170
|
+
next(p for p in exempt_prefixes if cleaned.startswith(p)),
|
|
171
|
+
cleaned[:60],
|
|
172
|
+
)
|
|
173
|
+
continue
|
|
174
|
+
return cleaned[:_SESSION_TITLE_MAX_LEN]
|
|
159
175
|
return ""
|
|
160
176
|
|
|
161
177
|
|
|
@@ -193,23 +209,52 @@ def _extract_title_from_metadata(request: CanonicalRequest) -> str:
|
|
|
193
209
|
return ""
|
|
194
210
|
|
|
195
211
|
|
|
196
|
-
def _extract_session_title(
|
|
212
|
+
def _extract_session_title(
|
|
213
|
+
request: CanonicalRequest,
|
|
214
|
+
exempt_prefixes: list[str] | None = None,
|
|
215
|
+
) -> str:
|
|
197
216
|
"""从规范化请求中提取 session 标题 — 多层级回退策略。
|
|
198
217
|
|
|
199
218
|
依次尝试: user TEXT 噪声剥离 → TOOL_RESULT 摘要 → IMAGE 计数 → 元数据兜底。
|
|
200
|
-
|
|
219
|
+
任一层级产出候选且未被豁免即返回,确保 Dashboard 尽可能展示有辨识度的标题。
|
|
220
|
+
|
|
221
|
+
``exempt_prefixes`` 对所有层级统一生效:任一层产出的候选标题若以任一前缀
|
|
222
|
+
开头,则视为豁免、继续向下一层回退。Level 1 内部仍保留「逐条消息跳过」
|
|
223
|
+
语义(清洗后文本以豁免前缀开头则跳过该条、继续查找下一条 user TEXT);
|
|
224
|
+
本函数在编排层对每层最终候选再做一次 ``_is_exempt`` 拦截,使 Level 2/3/4
|
|
225
|
+
的合成标题(如 ``[Tool output]``、``[N Image(s)]``、``[Tool call]``、
|
|
226
|
+
``[Session]``)也能被用户显式豁免。全部层级均被豁免时返回空串,调用方
|
|
227
|
+
``if title:`` 据此跳过写库,session 标题保持空,待后续请求经
|
|
228
|
+
``update_empty_session_title`` 回填有意义标题。默认 ``None`` 时行为与历史一致。
|
|
201
229
|
"""
|
|
230
|
+
# 防御性过滤空串前缀:"" startswith 恒真会误豁免一切。入参虽经配置层与
|
|
231
|
+
# 构造器两层归一化,模块级直接调用(如测试)仍可能传入 [""],此处兜底。
|
|
232
|
+
prefixes = [p for p in (exempt_prefixes or []) if p]
|
|
233
|
+
|
|
234
|
+
def _is_exempt(candidate: str) -> bool:
|
|
235
|
+
"""候选标题是否命中任一豁免前缀(大小写敏感 startswith)。"""
|
|
236
|
+
return bool(prefixes) and any(candidate.startswith(p) for p in prefixes)
|
|
237
|
+
|
|
202
238
|
messages = request.messages
|
|
239
|
+
# Level 1: 内部已对每条 user TEXT 做豁免跳过;外层 _is_exempt 对其最终候选
|
|
240
|
+
# 再做一次拦截(正常路径下冗余,作为 _sanitize_user_text 一致性兜底)。
|
|
241
|
+
title = _extract_title_from_user_text(messages, exempt_prefixes=prefixes)
|
|
242
|
+
if title and not _is_exempt(title):
|
|
243
|
+
return title[:_SESSION_TITLE_MAX_LEN]
|
|
244
|
+
# Level 2 / Level 3: 候选为合成标题,统一经 _is_exempt 拦截后回退。
|
|
203
245
|
for extractor in (
|
|
204
|
-
_extract_title_from_user_text,
|
|
205
246
|
_extract_title_from_tool_results,
|
|
206
247
|
_extract_title_from_images,
|
|
207
248
|
):
|
|
208
249
|
title = extractor(messages)
|
|
209
|
-
if title:
|
|
250
|
+
if title and not _is_exempt(title):
|
|
210
251
|
return title[:_SESSION_TITLE_MAX_LEN]
|
|
211
|
-
# Level 4
|
|
212
|
-
|
|
252
|
+
# Level 4: 签名不同(依赖 request 元数据)。被豁免则落到末尾返回空串,
|
|
253
|
+
# 使调用方 if title: 跳过写库、保留空标题待后续回填。
|
|
254
|
+
title = _extract_title_from_metadata(request)
|
|
255
|
+
if title and not _is_exempt(title):
|
|
256
|
+
return title[:_SESSION_TITLE_MAX_LEN]
|
|
257
|
+
return ""
|
|
213
258
|
|
|
214
259
|
|
|
215
260
|
def _build_semantic_rejection_diagnostic(body: dict[str, Any]) -> str:
|
|
@@ -614,6 +659,7 @@ class _RouteExecutor:
|
|
|
614
659
|
reauth_coordinator: Any | None = None,
|
|
615
660
|
session_policy_resolver: SessionPolicyResolver | None = None,
|
|
616
661
|
title_vendor_bindings: list[TitleVendorBinding] | None = None,
|
|
662
|
+
title_exempt_prefixes: list[str] | None = None,
|
|
617
663
|
) -> None:
|
|
618
664
|
self._router = router
|
|
619
665
|
self._tiers = tiers
|
|
@@ -622,6 +668,13 @@ class _RouteExecutor:
|
|
|
622
668
|
self._reauth_coordinator = reauth_coordinator
|
|
623
669
|
self._policy_resolver = session_policy_resolver or SessionPolicyResolver()
|
|
624
670
|
self._title_vendor_bindings = title_vendor_bindings or []
|
|
671
|
+
# 豁免前缀名单:构造期二次归一化(strip + 去空),双重防御配置层 field_validator。
|
|
672
|
+
# 即便绕过配置层直接构造 executor,也能避免空串 startswith 恒真与纯空白失配。
|
|
673
|
+
self._title_exempt_prefixes: list[str] = [
|
|
674
|
+
stripped
|
|
675
|
+
for p in (title_exempt_prefixes or [])
|
|
676
|
+
if isinstance(p, str) and (stripped := p.strip())
|
|
677
|
+
]
|
|
625
678
|
self._validate_title_vendor_bindings()
|
|
626
679
|
|
|
627
680
|
# Tier 名称 → OAuth provider 名称的映射
|
|
@@ -676,6 +729,17 @@ class _RouteExecutor:
|
|
|
676
729
|
seen.add(tier.name)
|
|
677
730
|
return ordered
|
|
678
731
|
|
|
732
|
+
def _extract_session_title(self, request: CanonicalRequest) -> str:
|
|
733
|
+
"""封装模块级标题提取,注入实例持有的豁免前缀名单.
|
|
734
|
+
|
|
735
|
+
将 ``self._title_exempt_prefixes`` 透传给模块级 ``_extract_session_title``,
|
|
736
|
+
使 Level 1 能跳过以豁免前缀开头的注入式 Prompt。4 处标题提取调用点统一
|
|
737
|
+
经此方法,避免重复透传(零透传污染)。
|
|
738
|
+
"""
|
|
739
|
+
return _extract_session_title(
|
|
740
|
+
request, exempt_prefixes=self._title_exempt_prefixes
|
|
741
|
+
)
|
|
742
|
+
|
|
679
743
|
def _apply_title_based_policy(self, session_key: str, title: str) -> None:
|
|
680
744
|
"""根据 Session 标题前缀自动绑定供应商.
|
|
681
745
|
|
|
@@ -790,7 +854,7 @@ class _RouteExecutor:
|
|
|
790
854
|
canonical_request.trace_id,
|
|
791
855
|
)
|
|
792
856
|
if is_new_session:
|
|
793
|
-
title = _extract_session_title(canonical_request)
|
|
857
|
+
title = self._extract_session_title(canonical_request)
|
|
794
858
|
if title:
|
|
795
859
|
await self._recorder.set_session_title(
|
|
796
860
|
canonical_request.session_key, title
|
|
@@ -798,7 +862,7 @@ class _RouteExecutor:
|
|
|
798
862
|
self._apply_title_based_policy(canonical_request.session_key, title)
|
|
799
863
|
else:
|
|
800
864
|
# 延迟标题补写: 若 session 尚无标题,尝试从当前请求中提取并回写。
|
|
801
|
-
title = _extract_session_title(canonical_request)
|
|
865
|
+
title = self._extract_session_title(canonical_request)
|
|
802
866
|
if title:
|
|
803
867
|
await self._recorder.update_empty_session_title(
|
|
804
868
|
canonical_request.session_key, title
|
|
@@ -977,7 +1041,7 @@ class _RouteExecutor:
|
|
|
977
1041
|
canonical_request.trace_id,
|
|
978
1042
|
)
|
|
979
1043
|
if is_new_session:
|
|
980
|
-
title = _extract_session_title(canonical_request)
|
|
1044
|
+
title = self._extract_session_title(canonical_request)
|
|
981
1045
|
if title:
|
|
982
1046
|
await self._recorder.set_session_title(
|
|
983
1047
|
canonical_request.session_key, title
|
|
@@ -985,7 +1049,7 @@ class _RouteExecutor:
|
|
|
985
1049
|
self._apply_title_based_policy(canonical_request.session_key, title)
|
|
986
1050
|
else:
|
|
987
1051
|
# 延迟标题补写: 若 session 尚无标题,尝试从当前请求中提取并回写。
|
|
988
|
-
title = _extract_session_title(canonical_request)
|
|
1052
|
+
title = self._extract_session_title(canonical_request)
|
|
989
1053
|
if title:
|
|
990
1054
|
await self._recorder.update_empty_session_title(
|
|
991
1055
|
canonical_request.session_key, title
|
|
@@ -175,15 +175,39 @@ class QuotaGuard:
|
|
|
175
175
|
)
|
|
176
176
|
|
|
177
177
|
def reset(self) -> None:
|
|
178
|
-
"""
|
|
178
|
+
"""手动复位配额守卫(保留滑动窗口用量计数).
|
|
179
|
+
|
|
180
|
+
- 用量未超阈值 / 守卫非 EXCEEDED → 状态机回到 WITHIN_QUOTA;
|
|
181
|
+
- 用量确已超阈值 → 保持 QUOTA_EXCEEDED:不伪造用量,也不走
|
|
182
|
+
WITHIN_QUOTA → QUOTA_EXCEEDED 回环(回环会把 ``_last_probe`` 刷成
|
|
183
|
+
当前时刻,令探测恢复凭空推迟一个 probe_interval,反复复位即饿死探测)。
|
|
184
|
+
|
|
185
|
+
两种分支均清除 cap 错误卡死标志并还原探测间隔。``_entries`` / ``_total``
|
|
186
|
+
记录的是真实用量,清零会使 ``usage_percent`` 永久归零(基线仅在进程
|
|
187
|
+
启动时回填),任何情况下不予触碰。
|
|
188
|
+
"""
|
|
179
189
|
with self._lock:
|
|
180
|
-
self.
|
|
181
|
-
self.
|
|
182
|
-
|
|
183
|
-
logger.info(
|
|
184
|
-
"Quota guard [%s]: manually reset to WITHIN_QUOTA",
|
|
185
|
-
self._window_label,
|
|
190
|
+
self._expire()
|
|
191
|
+
over = self._budget > 0 and self._total >= int(
|
|
192
|
+
self._budget * self._threshold
|
|
186
193
|
)
|
|
194
|
+
if over and self._state == QuotaState.QUOTA_EXCEEDED:
|
|
195
|
+
self._cap_error_active = False
|
|
196
|
+
self._effective_probe_interval = self._probe_interval
|
|
197
|
+
logger.info(
|
|
198
|
+
"Quota guard [%s]: manually reset (usage %d tokens over "
|
|
199
|
+
"threshold, EXCEEDED state kept, probe interval restored)",
|
|
200
|
+
self._window_label,
|
|
201
|
+
self._total,
|
|
202
|
+
)
|
|
203
|
+
else:
|
|
204
|
+
self._transition_to(QuotaState.WITHIN_QUOTA)
|
|
205
|
+
logger.info(
|
|
206
|
+
"Quota guard [%s]: manually reset to WITHIN_QUOTA "
|
|
207
|
+
"(usage %d tokens preserved)",
|
|
208
|
+
self._window_label,
|
|
209
|
+
self._total,
|
|
210
|
+
)
|
|
187
211
|
|
|
188
212
|
def get_info(self) -> dict:
|
|
189
213
|
"""获取配额守卫状态信息."""
|
coding/proxy/routing/retry.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""传输层重试策略 — 指数退避与
|
|
1
|
+
"""传输层重试策略 — 指数退避与 Equal Jitter.
|
|
2
2
|
|
|
3
3
|
与 Circuit Breaker 正交互:
|
|
4
4
|
- Retry 处理瞬态网络抖动(秒级恢复)
|
|
@@ -68,15 +68,24 @@ def is_retryable_status(status_code: int) -> bool:
|
|
|
68
68
|
|
|
69
69
|
|
|
70
70
|
def calculate_delay(attempt: int, cfg: RetryConfig) -> float:
|
|
71
|
-
"""计算第 N 次重试的延迟(毫秒),含指数退避和
|
|
71
|
+
"""计算第 N 次重试的延迟(毫秒),含指数退避和 Equal Jitter.
|
|
72
|
+
|
|
73
|
+
Equal Jitter 策略: temp = min(initial * backoff^attempt, max);
|
|
74
|
+
delay = temp/2 + random(0, temp/2),落在 [temp/2, temp]。
|
|
75
|
+
相较 Full Jitter (random(0, temp)),Equal Jitter 保留一半固定基线,
|
|
76
|
+
使相邻重试的延迟区间仅边界相切,呈现单调非递减的指数形态,
|
|
77
|
+
同时保留抖动以防惊群。
|
|
78
|
+
|
|
79
|
+
契约边界:单调非递减依赖 ``backoff_multiplier >= 2.0`` 且未触及
|
|
80
|
+
``max_delay_ms`` 封顶;封顶后各 attempt 区间退化为同一 [max/2, max]
|
|
81
|
+
(当前 Zhipu ``max_retries=4`` 触及不到该边界)。
|
|
72
82
|
|
|
73
|
-
Full Jitter 策略: delay = random(0, min(initial * backoff^attempt, max))
|
|
74
83
|
参考: AWS "Exponential Backoff And Jitter" (Marc Brooker, 2015)
|
|
75
84
|
"""
|
|
76
85
|
delay = cfg.initial_delay_ms * (cfg.backoff_multiplier**attempt)
|
|
77
86
|
delay = min(delay, cfg.max_delay_ms)
|
|
78
87
|
|
|
79
88
|
if cfg.jitter:
|
|
80
|
-
delay = random.uniform(0, delay)
|
|
89
|
+
delay = delay / 2 + random.uniform(0, delay / 2)
|
|
81
90
|
|
|
82
91
|
return delay
|
coding/proxy/routing/router.py
CHANGED
|
@@ -40,6 +40,7 @@ class RequestRouter:
|
|
|
40
40
|
compat_session_store: CompatSessionStore | None = None,
|
|
41
41
|
session_policy_resolver: SessionPolicyResolver | None = None,
|
|
42
42
|
title_vendor_bindings: list[TitleVendorBinding] | None = None,
|
|
43
|
+
title_exempt_prefixes: list[str] | None = None,
|
|
43
44
|
) -> None:
|
|
44
45
|
if not tiers:
|
|
45
46
|
raise ValueError("至少需要一个供应商层级")
|
|
@@ -59,6 +60,7 @@ class RequestRouter:
|
|
|
59
60
|
reauth_coordinator=reauth_coordinator,
|
|
60
61
|
session_policy_resolver=session_policy_resolver,
|
|
61
62
|
title_vendor_bindings=title_vendor_bindings,
|
|
63
|
+
title_exempt_prefixes=title_exempt_prefixes,
|
|
62
64
|
)
|
|
63
65
|
|
|
64
66
|
def set_pricing_table(self, table: PricingTable) -> None:
|
coding/proxy/server/app.py
CHANGED
|
@@ -162,6 +162,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
|
|
|
162
162
|
compat_session_store,
|
|
163
163
|
session_policy_resolver=SessionPolicyResolver(config.session_policies.policies),
|
|
164
164
|
title_vendor_bindings=config.session_policies.title_vendor_bindings,
|
|
165
|
+
title_exempt_prefixes=config.session_policies.title_exempt_prefixes,
|
|
165
166
|
)
|
|
166
167
|
|
|
167
168
|
app = FastAPI(title="coding-proxy", version=__version__, lifespan=lifespan)
|