coding-proxy 0.5.0__py3-none-any.whl → 0.5.1__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.
@@ -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-sonnet-.*"
258
+ - pattern: "claude-opus-.*"
259
259
  vendors: ["zhipu"]
260
- target: "glm-5v-turbo"
260
+ target: "glm-5.2"
261
261
  is_regex: true
262
- - pattern: "claude-opus-.*"
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-4.5-air"
268
+ target: "glm-5.1"
269
269
  is_regex: true
270
270
  # MiniMax 模型映射(原生 Anthropic 兼容端点)
271
- - pattern: "claude-sonnet-.*"
271
+ - pattern: "claude-opus-.*"
272
272
  vendors: ["minimax"]
273
273
  target: "minimax-m2.7"
274
274
  is_regex: true
275
- - pattern: "claude-opus-.*"
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-sonnet-.*"
284
+ - pattern: "claude-opus-.*"
285
285
  vendors: ["alibaba"]
286
286
  target: "qwen3.6-plus"
287
287
  is_regex: true
288
- - pattern: "claude-opus-.*"
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-sonnet-.*"
297
+ - pattern: "claude-opus-.*"
298
298
  vendors: ["xiaomi"]
299
299
  target: "mimo-v2-pro"
300
300
  is_regex: true
301
- - pattern: "claude-opus-.*"
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-sonnet-.*"
310
+ - pattern: "claude-opus-.*"
311
311
  vendors: ["kimi"]
312
312
  target: "kimi-k2.5"
313
313
  is_regex: true
314
- - pattern: "claude-opus-.*"
314
+ - pattern: "claude-sonnet-.*"
315
315
  vendors: ["kimi"]
316
316
  target: "kimi-k2.5"
317
317
  is_regex: true
@@ -339,6 +339,18 @@ 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
348
+ - vendor: anthropic
349
+ model: claude-opus-4-8
350
+ input_cost_per_mtok: $5.0
351
+ output_cost_per_mtok: $25.0
352
+ cache_write_cost_per_mtok: $6.25
353
+ cache_read_cost_per_mtok: $0.50
342
354
  - vendor: anthropic
343
355
  model: claude-opus-4-7
344
356
  input_cost_per_mtok: $5.0
@@ -521,20 +533,10 @@ pricing:
521
533
  cache_read_cost_per_mtok: $0.05
522
534
  # ── 智谱 GLM ──
523
535
  - vendor: zhipu
524
- model: glm-4.5-air # 待区分长短上下文定价
525
- input_cost_per_mtok: ¥0.80
526
- output_cost_per_mtok: ¥2.00
527
- cache_read_cost_per_mtok: ¥0.16
528
- - vendor: zhipu
529
- model: glm-4.7 # 待区分长短上下文定价
530
- input_cost_per_mtok: ¥2.00
531
- output_cost_per_mtok: ¥8.00
532
- cache_read_cost_per_mtok: ¥0.40
533
- - vendor: zhipu
534
- model: glm-5v-turbo # 待区分长短上下文定价
535
- input_cost_per_mtok: ¥5.00
536
- output_cost_per_mtok: ¥22.00
537
- cache_read_cost_per_mtok: ¥1.20
536
+ model: glm-5.2 # 待区分长短上下文定价
537
+ input_cost_per_mtok: ¥6.00
538
+ output_cost_per_mtok: ¥24.00
539
+ cache_read_cost_per_mtok: ¥1.30
538
540
  - vendor: zhipu
539
541
  model: glm-5.1 # 待区分长短上下文定价
540
542
  input_cost_per_mtok: ¥6.00
@@ -550,6 +552,21 @@ pricing:
550
552
  input_cost_per_mtok: ¥4.00
551
553
  output_cost_per_mtok: ¥18.00
552
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
553
570
  # ── MiniMax ──
554
571
  - vendor: minimax
555
572
  model: minimax-m2.7
@@ -679,4 +696,11 @@ native_api:
679
696
  # tiers: ["copilot", "anthropic", "zhipu"]
680
697
  #
681
698
  # 未配置时(默认),所有 Session 使用全局 tiers 顺序。
682
- session_policies: []
699
+ session_policies:
700
+ policies: []
701
+ # 标题前缀 → 供应商自动绑定。
702
+ # 当 Session 标题以指定前缀开头时,自动将该 Session 绑定到对应供应商。
703
+ # 匹配规则按列表顺序求值,首次匹配生效。
704
+ title_vendor_bindings:
705
+ - prefix: "# 目标"
706
+ vendor: "zhipu"
@@ -44,7 +44,7 @@ from .routing import ( # noqa: F401
44
44
 
45
45
  # ── 子模块 re-export ────────────────────────────────────────────
46
46
  from .server import DatabaseConfig, LoggingConfig, ServerConfig # noqa: F401
47
- from .session_policy import SessionPoliciesConfig # noqa: F401
47
+ from .session_policy import SessionPoliciesConfig, TitleVendorBinding # noqa: F401
48
48
  from .vendors import ( # noqa: F401
49
49
  AlibabaConfig,
50
50
  AnthropicConfig,
@@ -350,4 +350,5 @@ __all__ = [
350
350
  "NativeApiConfig",
351
351
  # session policy
352
352
  "SessionPoliciesConfig",
353
+ "TitleVendorBinding",
353
354
  ]
@@ -50,6 +50,22 @@ class SessionPolicy(BaseModel):
50
50
  )
51
51
 
52
52
 
53
+ class TitleVendorBinding(BaseModel):
54
+ """标题前缀 → 供应商自动绑定规则."""
55
+
56
+ prefix: str = Field(
57
+ min_length=1,
58
+ description=(
59
+ "标题前缀匹配模式(大小写敏感的 startswith 匹配)。"
60
+ "禁止空字符串——空前缀会匹配所有标题,导致全量误绑定。"
61
+ ),
62
+ )
63
+ vendor: str = Field(
64
+ min_length=1,
65
+ description="匹配后绑定的目标供应商名称",
66
+ )
67
+
68
+
53
69
  class SessionPoliciesConfig(BaseModel):
54
70
  """顶层 Session 策略配置容器."""
55
71
 
@@ -57,3 +73,11 @@ class SessionPoliciesConfig(BaseModel):
57
73
  default_factory=list,
58
74
  description="Session 路由策略列表,按定义顺序求值,首次匹配生效",
59
75
  )
76
+ title_vendor_bindings: list[TitleVendorBinding] = Field(
77
+ default_factory=list,
78
+ description=(
79
+ "标题前缀 → 供应商自动绑定规则。"
80
+ "当 Session 标题以指定前缀开头时,自动绑定到对应供应商。"
81
+ "匹配规则按列表顺序求值,首次匹配生效。"
82
+ ),
83
+ )
@@ -52,6 +52,117 @@ def get_transition_channel(
52
52
  # ── 共享辅助函数 ──────────────────────────────────────────────
53
53
 
54
54
 
55
+ def _dump_message_digest(
56
+ messages: list[dict[str, Any]],
57
+ *,
58
+ max_messages: int = 10,
59
+ label: str = "",
60
+ ) -> None:
61
+ """输出前 N 条消息的结构摘要(role + content_type_counts),用于过渡管线诊断.
62
+
63
+ 仅在 DEBUG 级别输出,且仅在消息数 > 0 时才输出,避免噪声。
64
+ """
65
+ if not messages or not logger.isEnabledFor(logging.DEBUG):
66
+ return
67
+ parts: list[str] = [f"[{label}]" if label else ""]
68
+ limit = min(len(messages), max_messages)
69
+ for idx in range(limit):
70
+ msg = messages[idx]
71
+ role = msg.get("role", "?") if isinstance(msg, dict) else "?"
72
+ content = msg.get("content") if isinstance(msg, dict) else None
73
+ if isinstance(content, list):
74
+ type_counts: dict[str, int] = {}
75
+ for b in content:
76
+ if isinstance(b, dict):
77
+ t = b.get("type", "?")
78
+ type_counts[t] = type_counts.get(t, 0) + 1
79
+ else:
80
+ type_counts["raw"] = type_counts.get("raw", 0) + 1
81
+ counts_str = ",".join(f"{t}:{c}" for t, c in sorted(type_counts.items()))
82
+ elif isinstance(content, str):
83
+ counts_str = f"str({len(content)})"
84
+ else:
85
+ counts_str = "empty"
86
+ parts.append(f"{idx}:{role}[{counts_str}]")
87
+ if len(messages) > max_messages:
88
+ parts.append(f"...+{len(messages) - max_messages}more")
89
+ logger.debug("Transition digest %s", " ".join(parts))
90
+
91
+
92
+ def _validate_anthropic_pairing(
93
+ messages: list[dict[str, Any]],
94
+ *,
95
+ context: str = "",
96
+ ) -> list[str]:
97
+ """独立的 Anthropic tool_use/tool_result 配对自检(过渡管线末端执行).
98
+
99
+ 与 ``_enforce_pairing_sanity_pass`` 不同,此函数:
100
+ - 不修改消息列表(纯检测)
101
+ - 针对每个 assistant + tool_use,精确记录下一条 user 消息中匹配/缺失的 ID
102
+ - 发现不一致时输出 WARNING 级别日志含 message index 与具体 ID
103
+
104
+ Returns:
105
+ 检测到的问题描述列表(空列表表示全部通过)。
106
+ """
107
+ issues: list[str] = []
108
+ for i, msg in enumerate(messages):
109
+ if not isinstance(msg, dict) or msg.get("role") != "assistant":
110
+ continue
111
+ content = msg.get("content")
112
+ if not isinstance(content, list):
113
+ continue
114
+ tool_use_ids = [
115
+ b["id"]
116
+ for b in content
117
+ if isinstance(b, dict) and b.get("type") == "tool_use" and b.get("id")
118
+ ]
119
+ if not tool_use_ids:
120
+ continue
121
+
122
+ next_idx = i + 1
123
+ if next_idx >= len(messages):
124
+ issues.append(f"messages[{i}]: assistant with tool_uses at end of list")
125
+ continue
126
+
127
+ next_msg = messages[next_idx]
128
+ if not isinstance(next_msg, dict) or next_msg.get("role") != "user":
129
+ issues.append(
130
+ f"messages[{i}]: next messages[{next_idx}] is not user "
131
+ f"(role={next_msg.get('role') if isinstance(next_msg, dict) else '?'})"
132
+ )
133
+ continue
134
+
135
+ user_content = next_msg.get("content")
136
+ if not isinstance(user_content, list):
137
+ user_content = []
138
+
139
+ result_ids = {
140
+ b["tool_use_id"]
141
+ for b in user_content
142
+ if isinstance(b, dict)
143
+ and b.get("type") == "tool_result"
144
+ and isinstance(b.get("tool_use_id"), str)
145
+ }
146
+
147
+ missing = [uid for uid in tool_use_ids if uid not in result_ids]
148
+ if missing:
149
+ issue = (
150
+ f"messages[{i}]: {len(missing)}/{len(tool_use_ids)} tool_use(s) "
151
+ f"without tool_result in messages[{next_idx}]: {missing[:5]}"
152
+ )
153
+ issues.append(issue)
154
+
155
+ if issues:
156
+ prefix = f"[{context}] " if context else ""
157
+ logger.warning(
158
+ "Anthropic pairing validation: %s%d issue(s) found: %s",
159
+ prefix,
160
+ len(issues),
161
+ "; ".join(issues),
162
+ )
163
+ return issues
164
+
165
+
55
166
  def strip_thinking_blocks(body: dict[str, Any]) -> int:
56
167
  """从 assistant 消息中移除 thinking/redacted_thinking 块(就地).
57
168
 
@@ -678,6 +789,7 @@ def prepare_zhipu_to_anthropic(
678
789
  2. 改写 ``srvtoolu_*`` ID 与 ``server_tool_use`` 类型为标准 Anthropic 形式
679
790
  3. 强制 tool_use/tool_result 配对(单遍正向扫描)
680
791
  4. 剥离 thinking blocks(signature 无效)
792
+ 5. 独立的 Anthropic 兼容性自检(纯检测,不修改,定位 enforce/sanity 未覆盖的边界 case)
681
793
 
682
794
  所有变换均为幂等操作,安全地在已清理的请求体上重复执行。
683
795
 
@@ -686,6 +798,10 @@ def prepare_zhipu_to_anthropic(
686
798
  """
687
799
  prepared = copy.deepcopy(body)
688
800
  adaptations: list[str] = []
801
+ msgs = prepared.get("messages", [])
802
+
803
+ # ── 过渡管线诊断:变换前快照 ──
804
+ _dump_message_digest(msgs, label="zhipu→anthropic.before")
689
805
 
690
806
  # Step 1: 剥离 zhipu 私有 content block 类型(如 server_tool_use_delta)
691
807
  removed_vendor_blocks = _remove_vendor_blocks(prepared, _ZHIPU_VENDOR_BLOCK_TYPES)
@@ -696,16 +812,24 @@ def prepare_zhipu_to_anthropic(
696
812
  rewritten, _ = _rewrite_srvtoolu_ids(prepared)
697
813
  if rewritten:
698
814
  adaptations.append(f"rewritten_{rewritten}_srvtoolu_ids")
815
+ _dump_message_digest(msgs, label="zhipu→anthropic.after_rewrite")
699
816
 
700
817
  # Step 3: 强制 tool_use/tool_result 配对
701
- pairing_fixes = enforce_anthropic_tool_pairing(prepared.get("messages", []))
818
+ pairing_fixes = enforce_anthropic_tool_pairing(msgs)
702
819
  if pairing_fixes:
703
820
  adaptations.extend(pairing_fixes)
821
+ _dump_message_digest(msgs, label="zhipu→anthropic.after_enforce")
704
822
 
705
823
  # Step 4: 剥离 thinking blocks(zhipu signature 无效)
706
824
  stripped = strip_thinking_blocks(prepared)
707
825
  if stripped:
708
826
  adaptations.append(f"stripped_{stripped}_thinking_blocks")
827
+ _dump_message_digest(msgs, label="zhipu→anthropic.after_strip")
828
+
829
+ # Step 5: 独立的 Anthropic 兼容性自检(纯检测,不修改)
830
+ validation_issues = _validate_anthropic_pairing(msgs, context="zhipu→anthropic")
831
+ if validation_issues:
832
+ adaptations.append("anthropic_pairing_validation_issues")
709
833
 
710
834
  return prepared, adaptations
711
835
 
@@ -335,6 +335,20 @@ class TokenLogger:
335
335
  )
336
336
  await self._db.commit()
337
337
 
338
+ async def update_empty_session_title(self, session_key: str, title: str) -> None:
339
+ """为标题为空的 session 补写标题(幂等,仅覆盖空标题行).
340
+
341
+ 使用 ``AND title = ''`` 条件确保不覆盖已有标题,
342
+ 即使行已存在但标题为空也会被更新。
343
+ """
344
+ if not self._db or not title or not session_key:
345
+ return
346
+ await self._db.execute(
347
+ "UPDATE session_meta SET title = ? WHERE session_key = ? AND title = ''",
348
+ (title, session_key),
349
+ )
350
+ await self._db.commit()
351
+
338
352
  async def get_session_titles(self, session_keys: list[str]) -> dict[str, str]:
339
353
  """批量查询 session 标题."""
340
354
  if not self._db or not session_keys: