Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion desktop-tauri/src-tauri/server/src/server/config/title_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,21 @@ pub fn active() -> Option<TitleRoutes> {
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<TitleRule> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };
}

Expand All @@ -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) {
Expand Down