Skip to content

feat(ai): make lookup language explicit and deterministic (#286) - #287

Merged
yicheng47 merged 1 commit into
mainfrom
feat/286-explicit-lookup-language
Jun 30, 2026
Merged

feat(ai): make lookup language explicit and deterministic (#286)#287
yicheng47 merged 1 commit into
mainfrom
feat/286-explicit-lookup-language

Conversation

@yicheng47

Copy link
Copy Markdown
Owner

Closes #286.

Problem

Look Up's main definition/context did not deterministically follow the configured Lookup language:

  1. English emitted no language directive — the "Respond entirely in {X}" instruction was only built when language != "en", so a Chinese selection with Lookup language = English drifted back to Chinese. (Core bug.)
  2. Source-language lookup was implicit — "Chinese definition for a Chinese book" only happened as a side effect of defaults/bug feat: frontend static #1, so it read as a glitch rather than a choice.

Changes

  • src-tauri/src/commands/ai.rs
    • Add LOOKUP_LANGUAGE_SELECTION sentinel + main_language_clause() helper.
    • lookup_system_prompt now always emits a "Respond entirely in {clause}" directive for both definition and context (drops the language != "en" guard). The brief-translation gloss stays an independent add-on, allowed alongside "Same as selection".
    • explain_system_prompt uses the shared language_name map, handles "selection" ("the same language as the selected passage"), and emits the English directive explicitly.
    • lookup_language falls back to "selection" (instead of the app language) in both ai_lookup and ai_explain.
  • src/components/settings/ToolsSettings.tsx — prepend a Same as selection option to the Lookup-language dropdown only (gloss/explain/translate keep concrete languages); default the dropdown to lookup_language || "selection"; widen the select to fit the label.
  • i18n — add settings.tools.sameAsSelection ("Same as selection" / "跟随所选内容").

No setting keys renamed, no migration: "selection" is a value the existing lookup_language key can hold. Existing installs that never set lookup_language shift from app-language to source-language definitions — intentional and now self-documenting in Settings.

Tests / checks

  • cargo test — full suite green; new prompt regressions: lookup_english_emits_explicit_english_directive, lookup_selection_uses_source_language, lookup_selection_allows_gloss, explain_selection_uses_source_language (plus updated existing tests).
  • npm run lint — 0 errors (pre-existing warnings only); tsc --noEmit — clean.
  • Manual smoke test of the Settings UI passed.

🤖 Generated with Claude Code

Look Up's definition/context now always honors the configured lookup
language (English included), fixing the case where an English setting
drifted to the selection's language. Adds a "Same as selection" option
that mirrors the selected text's language — the new default for installs
that never set lookup_language.

- ai.rs: add LOOKUP_LANGUAGE_SELECTION sentinel + main_language_clause;
  always emit "Respond entirely in {lang}" for definition and context
  (drop the language != "en" guard); handle "selection" in explain;
  default lookup_language to "selection" in ai_lookup and ai_explain
- ToolsSettings: add "Same as selection" to the lookup dropdown only,
  default the select to selection, and widen it to fit the label
- i18n: add settings.tools.sameAsSelection (en/zh)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@yicheng47

Copy link
Copy Markdown
Owner Author

Review findings:

No remaining must-fix issues found. The PR matches issue #286: lookup/explain prompts now emit explicit response-language directives including English, the selection sentinel is handled without leaking as a literal target language, backend defaults align with the new source-language mode, and the Tools settings dropdown exposes “Same as selection” only for Lookup while keeping gloss/explain/translate targets concrete.

Verification:

  • gh pr diff 287 reviewed.
  • gh pr checks 287 passed: Frontend, Backend.
  • Local checks previously rerun on the same diff: cargo test lookup, cargo test explain_prompt, pnpm exec tsc --noEmit, pnpm run lint (0 errors, existing warnings), git diff --check.

@yicheng47
yicheng47 merged commit 1efc349 into main Jun 30, 2026
2 checks passed
@yicheng47
yicheng47 deleted the feat/286-explicit-lookup-language branch June 30, 2026 13:58
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.

bug: make lookup language behavior explicit for source-language lookups

1 participant