coding-proxy 0.5.2a1__py3-none-any.whl → 0.5.2a2__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,10 @@ session_policies:
704
704
  title_vendor_bindings:
705
705
  - prefix: "# 目标"
706
706
  vendor: "zhipu"
707
+ # Session 标题豁免前缀:user 输入(经噪声剥离清洗后)以任一前缀开头则跳过,
708
+ # 继续向后查找 title 候选。用于过滤注入式 Prompt(如示例语言指令),
709
+ # 避免其被误用作 Session 标题。大小写敏感 startswith。
710
+ # ⚠️ 自定义 yaml 中该字段为整体替换(loader 的 list 合并约定,非追加合并);
711
+ # 写空列表即关闭豁免。加载时会自动 strip + 去空 + 去重。
712
+ title_exempt_prefixes:
713
+ - "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,34 @@ 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
+ "以任一前缀开头时,跳过该输入、继续向后查找合适的 title 候选。"
89
+ "用于过滤注入式 Prompt(如示例语言指令),避免其被误用作 Session 标题。"
90
+ "大小写敏感的 startswith 匹配,与 title_vendor_bindings 语义一致。"
91
+ "加载时会自动 strip + 去空 + 去重;空字符串前缀会被丢弃"
92
+ "(防空串 startswith 恒真导致全量误豁免)。"
93
+ ),
94
+ )
95
+
96
+ @field_validator("title_exempt_prefixes", mode="after")
97
+ @classmethod
98
+ def _normalize_exempt_prefixes(cls, value: list[str]) -> list[str]:
99
+ """归一化豁免前缀:strip + 去空 + 去重保序.
100
+
101
+ 空字符串前缀必须丢弃——``"".startswith`` 恒真会豁免一切用户输入,
102
+ 导致 Level 1 标题提取永久失效。
103
+ """
104
+ seen: set[str] = set()
105
+ result: list[str] = []
106
+ for item in value:
107
+ if not isinstance(item, str):
108
+ continue
109
+ cleaned = item.strip()
110
+ if not cleaned or cleaned in seen:
111
+ continue
112
+ seen.add(cleaned)
113
+ result.append(cleaned)
114
+ 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,15 +209,24 @@ 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
219
  任意级别命中即返回,确保 Dashboard 尽可能展示有辨识度的标题。
220
+
221
+ ``exempt_prefixes`` 仅作用于 Level 1(用户直接输入的 TEXT);Level 2/3/4
222
+ 为非用户直接输入的回退来源,不参与豁免。默认 ``None`` 时行为与历史一致。
201
223
  """
202
224
  messages = request.messages
225
+ # Level 1 需要豁免前缀;单独调用以传入参数,避免污染 L2/L3 的统一签名。
226
+ title = _extract_title_from_user_text(messages, exempt_prefixes)
227
+ if title:
228
+ return title[:_SESSION_TITLE_MAX_LEN]
203
229
  for extractor in (
204
- _extract_title_from_user_text,
205
230
  _extract_title_from_tool_results,
206
231
  _extract_title_from_images,
207
232
  ):
@@ -614,6 +639,7 @@ class _RouteExecutor:
614
639
  reauth_coordinator: Any | None = None,
615
640
  session_policy_resolver: SessionPolicyResolver | None = None,
616
641
  title_vendor_bindings: list[TitleVendorBinding] | None = None,
642
+ title_exempt_prefixes: list[str] | None = None,
617
643
  ) -> None:
618
644
  self._router = router
619
645
  self._tiers = tiers
@@ -622,6 +648,13 @@ class _RouteExecutor:
622
648
  self._reauth_coordinator = reauth_coordinator
623
649
  self._policy_resolver = session_policy_resolver or SessionPolicyResolver()
624
650
  self._title_vendor_bindings = title_vendor_bindings or []
651
+ # 豁免前缀名单:构造期二次归一化(strip + 去空),双重防御配置层 field_validator。
652
+ # 即便绕过配置层直接构造 executor,也能避免空串 startswith 恒真与纯空白失配。
653
+ self._title_exempt_prefixes: list[str] = [
654
+ stripped
655
+ for p in (title_exempt_prefixes or [])
656
+ if isinstance(p, str) and (stripped := p.strip())
657
+ ]
625
658
  self._validate_title_vendor_bindings()
626
659
 
627
660
  # Tier 名称 → OAuth provider 名称的映射
@@ -676,6 +709,17 @@ class _RouteExecutor:
676
709
  seen.add(tier.name)
677
710
  return ordered
678
711
 
712
+ def _extract_session_title(self, request: CanonicalRequest) -> str:
713
+ """封装模块级标题提取,注入实例持有的豁免前缀名单.
714
+
715
+ 将 ``self._title_exempt_prefixes`` 透传给模块级 ``_extract_session_title``,
716
+ 使 Level 1 能跳过以豁免前缀开头的注入式 Prompt。4 处标题提取调用点统一
717
+ 经此方法,避免重复透传(零透传污染)。
718
+ """
719
+ return _extract_session_title(
720
+ request, exempt_prefixes=self._title_exempt_prefixes
721
+ )
722
+
679
723
  def _apply_title_based_policy(self, session_key: str, title: str) -> None:
680
724
  """根据 Session 标题前缀自动绑定供应商.
681
725
 
@@ -790,7 +834,7 @@ class _RouteExecutor:
790
834
  canonical_request.trace_id,
791
835
  )
792
836
  if is_new_session:
793
- title = _extract_session_title(canonical_request)
837
+ title = self._extract_session_title(canonical_request)
794
838
  if title:
795
839
  await self._recorder.set_session_title(
796
840
  canonical_request.session_key, title
@@ -798,7 +842,7 @@ class _RouteExecutor:
798
842
  self._apply_title_based_policy(canonical_request.session_key, title)
799
843
  else:
800
844
  # 延迟标题补写: 若 session 尚无标题,尝试从当前请求中提取并回写。
801
- title = _extract_session_title(canonical_request)
845
+ title = self._extract_session_title(canonical_request)
802
846
  if title:
803
847
  await self._recorder.update_empty_session_title(
804
848
  canonical_request.session_key, title
@@ -977,7 +1021,7 @@ class _RouteExecutor:
977
1021
  canonical_request.trace_id,
978
1022
  )
979
1023
  if is_new_session:
980
- title = _extract_session_title(canonical_request)
1024
+ title = self._extract_session_title(canonical_request)
981
1025
  if title:
982
1026
  await self._recorder.set_session_title(
983
1027
  canonical_request.session_key, title
@@ -985,7 +1029,7 @@ class _RouteExecutor:
985
1029
  self._apply_title_based_policy(canonical_request.session_key, title)
986
1030
  else:
987
1031
  # 延迟标题补写: 若 session 尚无标题,尝试从当前请求中提取并回写。
988
- title = _extract_session_title(canonical_request)
1032
+ title = self._extract_session_title(canonical_request)
989
1033
  if title:
990
1034
  await self._recorder.update_empty_session_title(
991
1035
  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.2a2
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=HVfHHvqJqBQ89Oq582ScT-VYz6QzwYhLyfQFt24xwU4,23481
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=bjJqszszWirnrKiiA46lckzsY-nqbFIh1xnrju0S0j0,4073
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=Nexuq1xJ3MKYeJHDTXGD731bu4-vnOcPMUyH2Ah1ZUA,58929
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.2a2.dist-info/METADATA,sha256=EVtjI9q0EXp90rCMWE5HRHaC4jlW0Q56Xwnu0R0sJ3w,11346
97
+ coding_proxy-0.5.2a2.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
98
+ coding_proxy-0.5.2a2.dist-info/entry_points.txt,sha256=moIVzt5ho0Wk9B47LOo2SEAbhzuDDHWi-EfM30U0XBg,54
99
+ coding_proxy-0.5.2a2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
100
+ coding_proxy-0.5.2a2.dist-info/RECORD,,