Skip to content

fix(ai): AI inline suggestions respect UI language setting#277

Open
zerx-lab wants to merge 2 commits into
mainfrom
fix/issue-276-ai-suggestions-language
Open

fix(ai): AI inline suggestions respect UI language setting#277
zerx-lab wants to merge 2 commits into
mainfrom
fix/issue-276-ai-suggestions-language

Conversation

@zerx-lab

Copy link
Copy Markdown
Owner

Fixes #276

问题点(确定性描述)

prompt_suggestions::dispatch at app/src/ai/agent_providers/active_ai/mod.rs:153 rendered the system prompt template with an empty context (context! {}):

// before
let system = render("prompt_suggestions_system.j2", context! {});

The template (app/src/ai/agent_providers/prompts/active_ai/prompt_suggestions_system.j2:4-6) had only the ambiguous instruction:

Output: ONLY one JSON object, no markdown / commentary. Match the user's
language (English / 中文 / …). Strictly valid JSON. No trailing commas,
no comments.

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, no language variable
  • prompt_suggestions_system.j2:4-6: ambiguous Match the user's language (English / 中文 / …) — no actual language value
  • prompt_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 / Language enum confirmed present; LanguageSettings::as_ref(app) pattern confirmed usable with &AppContext

规模声明

  • 修改文件数:2
  • 修改行数:14 insertions, 9 deletions
  • 影响面 grep 命中数:1mod.rs:153 是唯一调用点)
  • 不涉及公共 API、鉴权、并发、新依赖、DB schema

修改文件清单

文件 改动说明
app/src/ai/agent_providers/active_ai/mod.rs prompt_suggestions 模块添加 Language/LanguageSettings import;将空 context! {} 替换为携带 language 字符串的 context(Language::SystemLanguage::English 均映射为 "English" 与 app 的 i18n fallback 行为一致)
app/src/ai/agent_providers/prompts/active_ai/prompt_suggestions_system.j2 将模糊指令 Match 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:135appearance_page.rs:969 一致
  • context! { key => value } — 与 mod.rs 其余所有 render() 调用一致
  • match 覆盖 Language 的全部 4 个 variant(SystemEnglishSimplifiedChineseJapanese

影响面

唯一调用方:prompt_suggestions::dispatch (mod.rs:147-172)。其他 dispatch 函数(nld_predictrelevant_filesworkflow_metadatanext_command)的 system prompt 不包含语言指令,不受影响。


Generated by Claude Code

…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

@zerx-lab zerx-lab left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(详见 inline comments 和下方总结 comment)


Generated by Claude Code

) -> 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 {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Language::System 表示"跟随操作系统语言",实际值可以是任意语言(法语、德语等)。将其 hard-code 到 "English" 会造成一个潜在回归:系统语言为简体中文或日文、但未显式切换 UI 语言的用户,修复后会从之前"偶然得到中文建议"变成"被强制给到英文建议"。

PR body 提到"与 app 的 i18n fallback 行为一致",但没有贴出具体逻辑。如果 LanguageSettingsLanguage::System 下确实向用户显示英文 UI,此处对齐是合理的;否则建议在旁边加一行注释说明这是已知 tradeoff,例如:

// Language::System: app falls back to English for unsupported system locales
Language::System | Language::English => "English",

Generated by Claude Code

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确认这是一个真实回归,已在 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}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Review 总结

整体判断:Comment(不阻塞合并,但有两点建议作者确认或说明)


阻塞问题

无。


建议

1. Language::System 映射到 "English" 的合理性需要确认(见 inline comment on mod.rs:154

Language::System 代表"跟随 OS 系统语言",实际值未知。将其 hard-code 为 "English" 会导致:系统语言为简体中文或日文、但未显式设置 UI 语言的用户,修复后从"偶然得到中文建议"变为"被强制收到英文建议"。若 app 本身对 Language::System 已有明确的 English fallback(即 UI 对这类用户也显示英文),此处对齐是合理的;否则建议加一行注释说明这是已知 tradeoff。

2. 全英文 few-shot 示例对弱模型的兼容性(见 inline comment on prompt_suggestions_system.j2:58

对于 SimplifiedChinese / Japanese 用户,修复后的模板同时携带 Respond in Simplified Chinese 指令和全英文示例。对强模型没问题,但弱/本地模型以 few-shot 语言为优先信号,可能仍输出英文 query。这是静态模板的固有限制,不要求修改,建议在描述中说明"非英文用户的改善效果取决于所用模型的指令跟随能力"。


看起来不错的地方

  • 修改极为克制(14 insertions / 9 deletions,仅两个文件),不引入新依赖。
  • match 覆盖了 Language 的全部 4 个 variant,编译器保证枚举穷举。
  • context! { language => language } 模式与 mod.rs 中其余 render() 调用完全一致。
  • 将空 context! {} 替换为带语言变量的 context,是对根因(信号缺失 + 中文示例偏置)的正确定位和修复。

— 由 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI Inline Suggestions ignore UI language setting

2 participants