fix(ai): AI inline suggestions respect UI language setting#277
fix(ai): AI inline suggestions respect UI language setting#277zerx-lab wants to merge 2 commits into
Conversation
…276) Root cause: prompt_suggestions::dispatch at app/src/ai/agent_providers/active_ai/mod.rs:153 rendered prompt_suggestions_system.j2 with an empty context (`context! {}`), so the template had no language signal. The template's instruction was the ambiguous "Match the user's language (English / 中文 / …)" and all 5 few-shot example queries were in Chinese, biasing the model to always output Chinese. Changes: - mod.rs: read LanguageSettings from AppContext and pass a `language` string ("English" / "Simplified Chinese" / "Japanese") to the template context; Language::System defaults to "English" matching the app's i18n fallback - prompt_suggestions_system.j2: replace ambiguous instruction with explicit "Respond in {{ language }}."; translate all 5 Chinese few-shot query strings to English so the model is no longer biased by the example language Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V2PG7rHXKzsE998uPS1iEP
| ) -> Option<RenderedRequest> { | ||
| let cfg = resolve_active_ai_oneshot(app, terminal_view_id)?; | ||
| let system = render("prompt_suggestions_system.j2", context! {}); | ||
| let language = match *LanguageSettings::as_ref(app).language { |
There was a problem hiding this comment.
Question: Language::System 表示"跟随操作系统语言",实际值可以是任意语言(法语、德语等)。将其 hard-code 到 "English" 会造成一个潜在回归:系统语言为简体中文或日文、但未显式切换 UI 语言的用户,修复后会从之前"偶然得到中文建议"变成"被强制给到英文建议"。
PR body 提到"与 app 的 i18n fallback 行为一致",但没有贴出具体逻辑。如果 LanguageSettings 在 Language::System 下确实向用户显示英文 UI,此处对齐是合理的;否则建议在旁边加一行注释说明这是已知 tradeoff,例如:
// Language::System: app falls back to English for unsupported system locales
Language::System | Language::English => "English",Generated by Claude Code
There was a problem hiding this comment.
确认这是一个真实回归,已在 9c1d94f 中修复。
Language::System 下改为通过 crate::i18n::current_languages() 读取运行时已激活的 locale,再映射到对应语言名;系统语言为中文或日文的用户不会被强制切到英文。不支持的 locale 仍 fallback 到 English,与 app 自身 i18n 行为一致。
Generated by Claude Code
| -rw-r--r-- … error.log | ||
| -rw-r--r-- … access.log | ||
| → {"kind":"simple","query":"查看 nginx error.log 最近 50 行","should_plan_task":false} | ||
| → {"kind":"simple","query":"Show the last 50 lines of nginx error.log","should_plan_task":false} |
There was a problem hiding this comment.
Question: 5 条 few-shot 示例现在全部为英文。对于 SimplifiedChinese / Japanese 用户,模板会同时携带:
- 明确指令:
Respond in Simplified Chinese(来自{{ language }}) - 全英文的示例 query
对强模型(GPT-4 / Claude 系)显式指令优先,问题不大。但 Zap 支持本地/弱模型接入,这类模型往往将 few-shot 示例语言作为最强信号,仍可能输出英文 query。
这是静态模板的固有限制,不要求修改,但建议在 PR 描述或 commit message 里说明"对非英文用户的改善效果取决于所用模型的指令跟随能力",避免 CJK 用户反馈"仍然没修好"。
Generated by Claude Code
Review 总结整体判断:Comment(不阻塞合并,但有两点建议作者确认或说明) 阻塞问题无。 建议1.
2. 全英文 few-shot 示例对弱模型的兼容性(见 inline comment on 对于 看起来不错的地方
— 由 Claude Routine 自动生成;如需复评请 @ 维护者人工触发。 Generated by Claude Code |
…lish Language::System follows the OS locale; users with a Chinese or Japanese system locale (without an explicit UI language override) would have received a regression — English AI suggestions — from the initial fix. Read crate::i18n::current_languages() to get the actual active locale and map it to the correct prompt-language string, matching the app's own i18n resolution behaviour (zh-* → Simplified Chinese, ja → Japanese, else English). Addresses review comment on #277. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V2PG7rHXKzsE998uPS1iEP
Fixes #276
问题点(确定性描述)
prompt_suggestions::dispatchatapp/src/ai/agent_providers/active_ai/mod.rs:153rendered the system prompt template with an empty context (context! {}):The template (
app/src/ai/agent_providers/prompts/active_ai/prompt_suggestions_system.j2:4-6) had only the ambiguous instruction:With no concrete language signal and all 5 few-shot example queries in Chinese (lines 59, 69, 74, 83, 87), the model consistently output Chinese suggestions regardless of the user's UI language setting.
复现证据
mod.rs:153:render("prompt_suggestions_system.j2", context! {})— empty context, nolanguagevariableprompt_suggestions_system.j2:4-6: ambiguousMatch the user's language (English / 中文 / …)— no actual language valueprompt_suggestions_system.j2:59,69,74,83,87: all 5 query examples are Chinese ("查看 nginx error.log 最近 50 行","解决 src/auth.ts 的合并冲突","修复 test_login 的断言失败","查看 redis-cache 退出日志并重启","先 rebase 远端 main 再重推")app/src/settings/language.rs:70-81:LanguageSettings/Languageenum confirmed present;LanguageSettings::as_ref(app)pattern confirmed usable with&AppContext规模声明
mod.rs:153是唯一调用点)修改文件清单
app/src/ai/agent_providers/active_ai/mod.rsprompt_suggestions模块添加Language/LanguageSettingsimport;将空context! {}替换为携带language字符串的 context(Language::System和Language::English均映射为"English"与 app 的 i18n fallback 行为一致)app/src/ai/agent_providers/prompts/active_ai/prompt_suggestions_system.j2Match the user's language (English / 中文 / …)替换为Respond in {{ language }}.;将全部 5 条 few-shot example query 从中文翻译为英文,消除对英文用户的语言偏置验证
cargo check在此 Linux 沙箱中因 macOS 专用 git 依赖core-foundation-rs(403 代理拦截)无法运行,属预存环境限制与本次改动无关。代码变更使用的 API 模式均经代码库内确认:LanguageSettings::as_ref(app).language— 与settings/init.rs:135、appearance_page.rs:969一致context! { key => value }— 与mod.rs其余所有render()调用一致match覆盖Language的全部 4 个 variant(System、English、SimplifiedChinese、Japanese)影响面
唯一调用方:
prompt_suggestions::dispatch(mod.rs:147-172)。其他 dispatch 函数(nld_predict、relevant_files、workflow_metadata、next_command)的 system prompt 不包含语言指令,不受影响。Generated by Claude Code