coding-proxy 0.5.2a1__py3-none-any.whl → 0.5.2a3__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.
@@ -704,3 +704,12 @@ session_policies:
704
704
  title_vendor_bindings:
705
705
  - prefix: "# 目标"
706
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 language"
@@ -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
@@ -148,14 +148,30 @@ def _sanitize_user_text(raw: str) -> str:
148
148
  # ─────────────────────────────────────────────────────────────────
149
149
 
150
150
 
151
- def _extract_title_from_user_text(messages: list[CanonicalMessagePart]) -> str:
152
- """Level 1: 从 user TEXT 部分提取经噪声剥离后的首条非空文本."""
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
- return cleaned[:_SESSION_TITLE_MAX_LEN]
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(request: CanonicalRequest) -> str:
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
- 任意级别命中即返回,确保 Dashboard 尽可能展示有辨识度的标题。
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 依赖 request 元数据,签名不同
212
- return _extract_title_from_metadata(request)[:_SESSION_TITLE_MAX_LEN]
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
@@ -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:
@@ -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)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coding-proxy
3
- Version: 0.5.2a1
3
+ Version: 0.5.2a3
4
4
  Summary: A High-Availability, Transparent, and Smart Multi-Vendor Proxy for Claude Code. Support Claude Plans, GitHub Copilot, Google Antigravity, ZAI/GLM, MiniMax, Qwen, Xiaomi, Kimi, Doubao...
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
@@ -17,13 +17,13 @@ coding/proxy/compat/canonical.py,sha256=-zcuEwZ402xeH3C545RmuYRkT2HDuvFloyFydDv8
17
17
  coding/proxy/compat/session_store.py,sha256=B9IFjjQJnHMg1244m__jG9gnqGWi26-JyEtwopEri6Q,5244
18
18
  coding/proxy/config/__init__.py,sha256=hzgU5noJGecjj13UY38cC_p6jpWO3GO7okSW-A2XkJ0,127
19
19
  coding/proxy/config/auth_schema.py,sha256=LYrJQU_fgW-6AoQdjXt4-MgPJjXjv9HrghlCcZwotnA,696
20
- coding/proxy/config/config.default.yaml,sha256=7UCdYS-yVM1kF697LpYsjoJMXKwkFTausz7OmmhyiYc,22946
20
+ coding/proxy/config/config.default.yaml,sha256=20cIVpZBfUvQeIFltXxQ7TlFSB2voZuYxha4iPX5tzk,23708
21
21
  coding/proxy/config/loader.py,sha256=1J_RBJgjuC8RwB2mwewLMS7vy9WEhUqttZG2igeDm-w,8984
22
22
  coding/proxy/config/resiliency.py,sha256=GnzY-LoyfFqXFM1l6xEru418v-cKlv97-HM0noGPdks,1308
23
23
  coding/proxy/config/routing.py,sha256=MWTUq6QaYDmoXEARN9EBAptC6TSGK2yezNiaVk3kaXY,12196
24
24
  coding/proxy/config/schema.py,sha256=q1mlpnSMoVneDqzCSBR88xCgKG6aVu11ikdbMzWM27I,13542
25
25
  coding/proxy/config/server.py,sha256=4yY7i3WYOjtyZtZqHMtkjqHZ2W6D_ffEwVIfzxRar90,1002
26
- coding/proxy/config/session_policy.py,sha256=Oo4e1x4OpMXDnzILFmWW2rRXpwtnQffw-Nt1tF5bVCg,2634
26
+ coding/proxy/config/session_policy.py,sha256=0aV6bXKAyS5CkiVH3z4sBNksghaioyOwk3Zql3Gu10k,4356
27
27
  coding/proxy/config/vendors.py,sha256=9_Pgj9U6zOZVUV3UlA9P8BlKSDlj34g4p0lCy_bdimE,3472
28
28
  coding/proxy/convert/__init__.py,sha256=0D3Q9TdY_AWCaMMf9f_i3jn0RIiWmqlcIAA79Ki8uJc,458
29
29
  coding/proxy/convert/anthropic_to_gemini.py,sha256=9mRQ0CRAGJyWQADNlfMqoCJ_vY8ZZBGWaS_R8Vbe_l0,13201
@@ -56,19 +56,19 @@ coding/proxy/native_api/extractors/openai.py,sha256=bPdkcR_2g7zb-F8P7CGumVqv_chR
56
56
  coding/proxy/routing/__init__.py,sha256=2Tpc6MQ5Oy_w-YlEWQVDSgcSgiHp3OwKOABIjj5kfHA,1595
57
57
  coding/proxy/routing/circuit_breaker.py,sha256=hOyoY_RWB5dTVbipyuiWfAelnva3cZRLbXrSmSn8KTs,8096
58
58
  coding/proxy/routing/error_classifier.py,sha256=SoJsExbfzduBOC04tKv9RdQqkzA-tQxbPapU5duNJqA,4204
59
- coding/proxy/routing/executor.py,sha256=rJm3VPx0djZsB8LsyMq3cFsIZnDEsssrhEWL95JX-U0,56730
59
+ coding/proxy/routing/executor.py,sha256=PCj4yvMccGZu99ZrOjJ1aeiAmapeYUBVBCqG0ZmKCt0,60473
60
60
  coding/proxy/routing/model_mapper.py,sha256=yc5PW7c9x6wfs7wGdbm-G5pFwg-kPTim6A2mKZ3sQgc,2803
61
61
  coding/proxy/routing/quota_guard.py,sha256=uoQSr2siv6aIBVylvXG_PsfdpY_i4QJxc-WHz4MGDa4,7791
62
62
  coding/proxy/routing/rate_limit.py,sha256=i2cCqtbmXrP6wjRUTWXLP4DdYwVj0C4o59QdYGkPQj0,5122
63
63
  coding/proxy/routing/retry.py,sha256=iRtuMSwXgGi_FPRi5mopTicdbEs_xrrKCXs83QjCz1c,2912
64
- coding/proxy/routing/router.py,sha256=_ryu455wv08D90nU1UdriEkwYLveE9mUQSdrgy0xO9o,6198
64
+ coding/proxy/routing/router.py,sha256=qqI3-cqdwArnBr0d8L0rLlkisu4HRsxhs-PwZAY8TiA,6311
65
65
  coding/proxy/routing/session_manager.py,sha256=GXeqLmHBlbcK49vNV_fDZnW8pVumeTJkiDB3ijKIsow,3145
66
66
  coding/proxy/routing/session_policy.py,sha256=U46jidS3y7MgfcIQtOa8k0SBnkdRO0Jybe3ph4q-br4,4725
67
67
  coding/proxy/routing/tier.py,sha256=OBrJiMxfLCxXvZfARD5oNmuhpYASKnyrW9sOCHLdQ1g,7777
68
68
  coding/proxy/routing/usage_parser.py,sha256=ZwPuMGtXAwdIOKrXLpdtA4nCmc83ViF6kw5JnivRC6s,10426
69
69
  coding/proxy/routing/usage_recorder.py,sha256=PHUHotCt3VaKkdJizMpPGsUOAj-f5qz9JKeiLEnFNgA,8779
70
70
  coding/proxy/server/__init__.py,sha256=KeH7mEu36v9v27m3VgeSxSeFz9sLTJrxS_EVATJ7Vks,20
71
- coding/proxy/server/app.py,sha256=mOjVW2IlY0gZULtBUVegE_N9p_BZjYSqUpiX2Tn0ito,7044
71
+ coding/proxy/server/app.py,sha256=3MYE8-Vsxpt1SVqTKReMKNREBsh_LHKIFq1B4jhfeL8,7121
72
72
  coding/proxy/server/dashboard.py,sha256=DRfa4T4nbKGqVgryf44dGQewh8Umh8lkiRrEu1Emr_s,96638
73
73
  coding/proxy/server/factory.py,sha256=L0d0AA0fpkV-gynM85u1l5dXY1OQ5A_p_-PV0XlFGQs,10272
74
74
  coding/proxy/server/responses.py,sha256=i0ugnLRNOdRYGHEWxkwsxR35ChmdMQsSaD8AjRluTn4,2167
@@ -93,8 +93,8 @@ coding/proxy/vendors/native_anthropic.py,sha256=SxtM71PDci0gqLqiwCrFnT410SnSoD7F
93
93
  coding/proxy/vendors/token_manager.py,sha256=s10t4Com0jNnKGkPyJ_HpG5SjHrCEJvfArEOAaPKA_k,4189
94
94
  coding/proxy/vendors/xiaomi.py,sha256=E-GcmJBZh7GOtDFonxZmlf0hKRhrlrXzL0IxHFRYcRo,860
95
95
  coding/proxy/vendors/zhipu.py,sha256=GaLaWssa4c1lcukQL4nOtzji8jxEUJFrZtFgOkXTKaM,11127
96
- coding_proxy-0.5.2a1.dist-info/METADATA,sha256=hRB6Z5C5NV42tOZjYi-xMgyHWpqHKYHvKz9tZavmQ1Q,11346
97
- coding_proxy-0.5.2a1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
98
- coding_proxy-0.5.2a1.dist-info/entry_points.txt,sha256=moIVzt5ho0Wk9B47LOo2SEAbhzuDDHWi-EfM30U0XBg,54
99
- coding_proxy-0.5.2a1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
100
- coding_proxy-0.5.2a1.dist-info/RECORD,,
96
+ coding_proxy-0.5.2a3.dist-info/METADATA,sha256=AQWHMsoXar2_gP8DdsaNVnJNS31RpuK6TNtNxVNdXR4,11346
97
+ coding_proxy-0.5.2a3.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
98
+ coding_proxy-0.5.2a3.dist-info/entry_points.txt,sha256=moIVzt5ho0Wk9B47LOo2SEAbhzuDDHWi-EfM30U0XBg,54
99
+ coding_proxy-0.5.2a3.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
100
+ coding_proxy-0.5.2a3.dist-info/RECORD,,