From 595d6199c1ecc6ea8baa818b37e2db5f517ca362 Mon Sep 17 00:00:00 2001 From: ScriptSage Date: Thu, 26 Nov 2026 00:28:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(title-routes):=20=E5=BD=92=E4=B8=80?= =?UTF-8?q?=E5=8C=96=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=A0=87=E8=AF=86=EF=BC=8C?= =?UTF-8?q?=E6=A0=87=E9=A2=98=E8=B7=AF=E7=94=B1=E4=B8=8D=E5=86=8D=E5=9B=A0?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E5=A4=B1=E9=85=8D=E8=80=8C=E6=B0=B8?= =?UTF-8?q?=E4=B8=8D=E5=91=BD=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit title_routing::client_for_request 拿 pipeline::request_source 的 behavior_client(core::client::classify() 的归类名,如 "Codex" / "ZCode" / "Claude Code")与规则里保存的配置 id("codex" / "zcode" / "claude-code")做严格相等比较,两套词汇永远不相等: 1. match_rule 的 rule.clients 比对恒不命中 → 直接返回 None; 2. 即使规则匹配成功,client == CLIENT_CODEX 与 match client 分支 也全部失配,Codex 头部判据与 ZCode / Claude Code 提示词判据 都不可达。 结果是标题生成路由对所有客户端都是死代码:titleRoute 恒为 null、 [Title] 改写日志零条。对照:压缩路由可用,因为它的门卫写死的是 classify 风格的大写 "ZCode"。 修复:新增 canonical_client()(小写 + 空白/下划线折叠为连字符), match_rule 与 client_for_request 的分支判断统一走归一形态。保存侧 validate / client_label 保持严格认小写 id,落库契约与存量数据零迁移; 运行态匹配对大小写与分隔符写法容错,后续新增客户端只要 classify 输出与配置 id 只差大小写/分隔符即自动命中,无需逐客户端加分支。 --- .../server/src/server/config/title_routes.rs | 12 +++++++++++- .../server/src/server/core/title_routing/mod.rs | 12 ++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/desktop-tauri/src-tauri/server/src/server/config/title_routes.rs b/desktop-tauri/src-tauri/server/src/server/config/title_routes.rs index f468c7d03..a815d5179 100644 --- a/desktop-tauri/src-tauri/server/src/server/config/title_routes.rs +++ b/desktop-tauri/src-tauri/server/src/server/config/title_routes.rs @@ -217,11 +217,21 @@ pub fn active() -> Option { Some(settings.clone()) } +/// 客户端标识归一:`core::client::classify()` 的行为归类名("Codex" / +/// "ZCode" / "Claude Code")与本页规则里保存的配置 id("codex" / "zcode" / +/// "claude-code")是两套词汇——前者是 UA 归类值,后者是面板勾选与落库 id。 +/// 比较前把两侧都折叠成小写并把空白、下划线折成连字符;保存侧 +/// `validate` / `client_label` 仍严格认小写 id,只有运行态匹配放宽。 +pub fn canonical_client(client: &str) -> String { + client.trim().to_lowercase().replace([' ', '_'], "-") +} + /// 这次请求该走哪条规则:按列表顺序取第一条命中的 —— 顺序即优先级。 pub fn match_rule(settings: &TitleRoutes, client: &str) -> Option { + let wanted = canonical_client(client); settings.rules.iter() .filter(|rule| effective(rule)) - .find(|rule| rule.clients.iter().any(|id| id == client)) + .find(|rule| rule.clients.iter().any(|id| canonical_client(id) == wanted)) .cloned() } diff --git a/desktop-tauri/src-tauri/server/src/server/core/title_routing/mod.rs b/desktop-tauri/src-tauri/server/src/server/core/title_routing/mod.rs index 08709e904..fed23dc49 100644 --- a/desktop-tauri/src-tauri/server/src/server/core/title_routing/mod.rs +++ b/desktop-tauri/src-tauri/server/src/server/core/title_routing/mod.rs @@ -160,7 +160,9 @@ fn claude_title(messages: &[Value]) -> bool { /// 判定这次请求属于哪家客户端的标题生成。 /// /// `client` 是行为客户端标识(`pipeline::request_source` 的 `behavior_client`), -/// 与压缩路由同一取值来源;只有配置里勾了这家客户端才继续判。 +/// 与压缩路由同一取值来源;只有配置里勾了这家客户端才继续判。注意它取的是 +/// `core::client::classify()` 的归类名(如 "Codex"),与规则里保存的配置 id +/// (如 "codex")大小写可能不同,比较一律先过 `title_routes::canonical_client`。 /// `headers` 给 Codex 的显式标记用;提示词类判据不看它。 pub fn client_for_request( client: &str, @@ -169,12 +171,14 @@ pub fn client_for_request( responses: bool, ) -> Option<&'static str> { let settings = title_routes::active()?; + // 行为归类名 → 配置 id 的归一形态;分支判断与 CLIENTS 回查都用它。 + let normalized = title_routes::canonical_client(client); // 这家客户端得有一条真会命中的规则(自己开着、勾了它、配了模型)才值得往下判 —— // 提示词判据要遍历消息结构,不便宜。 if title_routes::match_rule(&settings, client).is_none() { return None; } // Codex 的判据是请求头,不需要看消息结构,先判它(省一次遍历)。 - if client == CLIENT_CODEX { + if normalized == CLIENT_CODEX { return if codex_title(headers, body) { Some(CLIENT_CODEX) } else { None }; } @@ -188,14 +192,14 @@ pub fn client_for_request( } else if has_tools(body) { return None; } - let matched = match client { + let matched = match normalized.as_str() { CLIENT_ZCODE => zcode_title(messages), CLIENT_CLAUDE_CODE => claude_title(messages), _ => false, }; if !matched { return None; } // 返回静态 id:调用方会把它带进改写痕迹与日志,不能是配置里的临时字符串。 - title_routes::CLIENTS.iter().find(|(id, _)| *id == client).map(|(id, _)| *id) + title_routes::CLIENTS.iter().find(|(id, _)| *id == normalized).map(|(id, _)| *id) } pub fn mark_routed(body: &mut Value, source: &str, target: &str, client: &str) {