Skip to content

Commit b3c372b

Browse files
committed
fix: match generated text to the user's language
1 parent 69ea269 commit b3c372b

12 files changed

Lines changed: 239 additions & 65 deletions

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ version with its date and start a fresh empty `[Unreleased]` above it.
1111

1212
## [Unreleased]
1313

14+
### Fixed
15+
16+
- Auto-generated conversation titles, inline edits, and refined instructions
17+
now follow the language of the user's request, falling back to the UI locale
18+
when the language is unclear. Proper nouns, product names, file names, and
19+
code identifiers are preserved.
20+
1421
## [1.0.20] - 2026-11-26
1522

1623
### Fixed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getLocale } from '../../i18n/i18n';
2+
3+
export function buildGeneratedTextLanguageRule(): string {
4+
return `**Language**: Write the generated text in the same language as the user's request. If the request's language is unclear, use the UI locale (\`${getLocale()}\`). Keep proper nouns, product names, file names, paths, code identifiers, and code snippets unchanged; do not translate or rename them.`;
5+
}

‎src/qoder/prompt/inline-edit.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
InlineEditResult,
66
} from '../../core/types/services';
77
import { appendContextFiles } from './context/prompt-context';
8+
import { buildGeneratedTextLanguageRule } from './generated-text-language';
89

910
export function parseInlineEditResponse(responseText: string): InlineEditResult {
1011
const replacementMatch = responseText.match(/<replacement>([\s\S]*?)<\/replacement>/);
@@ -88,6 +89,8 @@ You are **Qoderian**, an expert editor and writing assistant embedded in Obsidia
8889
3. **Silent Execution**: Use tools (Read, WebSearch) silently. Your final output must be ONLY the result.
8990
4. **No Fluff**: No pleasantries, no "Here is the text", no "I have updated...". Just the content.
9091
92+
${buildGeneratedTextLanguageRule()}
93+
9194
## Input Format
9295
9396
User messages have the instruction first, followed by XML context tags:
@@ -174,6 +177,8 @@ CORRECT: "This is a guide about..."
174177
175178
If the request is ambiguous, ask a clarifying question. Keep questions concise and specific.
176179
180+
The examples below illustrate formatting only. Always follow the language rule above for the generated text.
181+
177182
## Examples
178183
179184
### Selection Mode

‎src/qoder/prompt/instruction-refine.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildGeneratedTextLanguageRule } from './generated-text-language';
2+
13
export function buildRefineSystemPrompt(existingInstructions: string): string {
24
const existingSection = existingInstructions.trim()
35
? `\n\nEXISTING INSTRUCTIONS (already in the user's system prompt):
@@ -21,7 +23,7 @@ When refining the new instruction:
2123
2. **Check Context**: Does this conflict with existing instructions?
2224
- *No Conflict*: Add as new.
2325
- *Conflict*: Propose a **merged instruction** that resolves the contradiction (or ask if unsure).
24-
3. **Refine**: Draft a clear, positive instruction (e.g., "Do X" instead of "Don't do Y").
26+
3. **Refine**: Draft a clear, positive instruction.
2527
4. **Format**: Return *only* the Markdown snippet wrapped in \`<instruction>\` tags.
2628
2729
**Guidelines**:
@@ -31,12 +33,16 @@ When refining the new instruction:
3133
- **No Header**: Do NOT include a top-level header like \`# Custom Instructions\`.
3234
- **Conflict Handling**: If the new rule directly contradicts an existing one, rewrite the *new* one to override specific cases or ask for clarification.
3335
36+
${buildGeneratedTextLanguageRule()}
37+
3438
**Output Format**:
3539
- **Success**: \`<instruction>...markdown content...</instruction>\`
3640
- **Ambiguity**: Plain text question.
3741
3842
${existingSection}
3943
44+
The examples below illustrate formatting only. Always follow the language rule above for the generated text.
45+
4046
**Examples**:
4147
4248
Input: "typescript for code"

‎src/qoder/prompt/title-generation.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
import { buildGeneratedTextLanguageRule } from './generated-text-language';
2+
13
const MAX_TITLE_INPUT_LENGTH = 500;
24
const MAX_TITLE_LENGTH = 50;
35

4-
export const TITLE_GENERATION_SYSTEM_PROMPT = `You are a specialist in summarizing user intent.
6+
export function getTitleGenerationSystemPrompt(): string {
7+
return `You are a specialist in summarizing user intent.
58
69
**Task**: Generate a **concise, descriptive title** (max 50 chars) summarizing the user's task/request.
710
811
**Rules**:
912
1. **Format**: Sentence case. No periods/quotes.
10-
2. **Structure**: Start with a **strong verb** (e.g., Create, Fix, Debug, Explain, Analyze).
13+
2. **Structure**: Start with a **strong verb** appropriate to the output language.
1114
3. **Forbidden**: "Conversation with...", "Help me...", "Question about...", "I need...".
12-
4. **Tech Context**: Detect and include the primary language/framework if code is present (e.g., "Debug Python script", "Refactor React hook").
15+
4. **Tech Context**: When code is present, include the primary language or framework, preserving product names and code identifiers exactly.
16+
17+
${buildGeneratedTextLanguageRule()}
1318
1419
**Output**: Return ONLY the raw title text.`;
20+
}
1521

1622
export function buildTitleGenerationPrompt(userMessage: string): string {
1723
const truncated = userMessage.length > MAX_TITLE_INPUT_LENGTH

‎src/qoder/services/qoder-title-generation-service.ts‎

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type {
44
} from '../../core/types/services';
55
import { toQoderRuntimeModelId } from '../models/model-selection';
66
import { qoderModelConfig } from '../models/qoder-model-config';
7-
import { TITLE_GENERATION_SYSTEM_PROMPT } from '../prompt/title-generation';
7+
import {
8+
buildTitleGenerationPrompt,
9+
getTitleGenerationSystemPrompt,
10+
parseTitleGenerationResponse,
11+
} from '../prompt/title-generation';
812
import type { QoderHostContext } from '../qoder-host-context';
913
import { runColdStartQuery } from '../runtime/qoder-cold-start-query';
1014

@@ -32,21 +36,20 @@ export class QoderTitleGenerationService {
3236
const abortController = new AbortController();
3337
this.activeGenerations.set(conversationId, abortController);
3438

35-
const truncatedUser = this.truncateText(userMessage, 500);
36-
const prompt = `User's request:\n"""\n${truncatedUser}\n"""\n\nGenerate a title for this conversation:`;
39+
const prompt = buildTitleGenerationPrompt(userMessage);
3740

3841
try {
3942
const result = await runColdStartQuery({
4043
plugin: this.plugin,
41-
systemPrompt: TITLE_GENERATION_SYSTEM_PROMPT,
44+
systemPrompt: getTitleGenerationSystemPrompt(),
4245
tools: [],
4346
model: this.resolveTitleModel(),
4447
thinking: { disabled: true },
4548
persistSession: false,
4649
abortController,
4750
}, prompt);
4851

49-
const title = this.parseTitle(result.text);
52+
const title = parseTitleGenerationResponse(result.text);
5053
if (title) {
5154
await this.safeCallback(callback, conversationId, { success: true, title });
5255
} else {
@@ -82,32 +85,6 @@ export class QoderTitleGenerationService {
8285
return 'auto';
8386
}
8487

85-
private truncateText(text: string, maxLength: number): string {
86-
if (text.length <= maxLength) return text;
87-
return text.substring(0, maxLength) + '...';
88-
}
89-
90-
private parseTitle(responseText: string): string | null {
91-
const trimmed = responseText.trim();
92-
if (!trimmed) return null;
93-
94-
let title = trimmed;
95-
if (
96-
(title.startsWith('"') && title.endsWith('"')) ||
97-
(title.startsWith("'") && title.endsWith("'"))
98-
) {
99-
title = title.slice(1, -1);
100-
}
101-
102-
title = title.replace(/[.!?:;,]+$/, '');
103-
104-
if (title.length > 50) {
105-
title = title.substring(0, 47) + '...';
106-
}
107-
108-
return title || null;
109-
}
110-
11188
private async safeCallback(
11289
callback: TitleGenerationCallback,
11390
conversationId: string,

‎tests/unit/qoder/prompt/inline-edit.test.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ jest.mock('@/core/time/date', () => ({
22
getTodayDate: () => 'Mocked Date',
33
}));
44

5+
import { getLocale, setLocale } from '@/i18n/i18n';
56
import { getInlineEditSystemPrompt } from '@/qoder/prompt/inline-edit';
67

78
describe('getInlineEditSystemPrompt', () => {
9+
let previousLocale: ReturnType<typeof getLocale>;
10+
11+
beforeEach(() => {
12+
previousLocale = getLocale();
13+
setLocale('en');
14+
});
15+
16+
afterEach(() => {
17+
setLocale(previousLocale);
18+
});
19+
820
it('should include inline edit critical output rules', () => {
921
const prompt = getInlineEditSystemPrompt();
1022
expect(prompt).toContain('ABSOLUTE RULE');
@@ -17,16 +29,25 @@ describe('getInlineEditSystemPrompt', () => {
1729
expect(prompt).toContain('read-only');
1830
});
1931

20-
it('should include example scenarios', () => {
32+
it('should use the shared language rule while preserving formatting examples', () => {
2133
const prompt = getInlineEditSystemPrompt();
22-
expect(prompt).toContain('translate to French');
23-
expect(prompt).toContain('Bonjour le monde');
24-
expect(prompt).toContain('asking for clarification');
34+
35+
expect(prompt).toContain("same language as the user's request");
36+
expect(prompt).toContain('UI locale (`en`)');
37+
expect(prompt).toContain('proper nouns, product names, file names, paths, code identifiers, and code snippets unchanged');
38+
expect(prompt).toContain('The examples below illustrate formatting only');
39+
expect(prompt).toContain('<replacement>Bonjour le monde</replacement>');
40+
expect(prompt.indexOf('**Language**')).toBeLessThan(prompt.indexOf('## Examples'));
41+
});
42+
43+
it('should fall back to the current UI locale', () => {
44+
setLocale('zh-CN');
45+
46+
expect(getInlineEditSystemPrompt()).toContain('UI locale (`zh-CN`)');
2547
});
2648

2749
it('should include date from utils', () => {
2850
const prompt = getInlineEditSystemPrompt();
2951
expect(prompt).toContain('Mocked Date');
3052
});
31-
3253
});

‎tests/unit/qoder/prompt/instruction-refine.test.ts‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
import { getLocale, setLocale } from '@/i18n/i18n';
12
import { buildRefineSystemPrompt } from '@/qoder/prompt/instruction-refine';
23

34
describe('buildRefineSystemPrompt', () => {
5+
let previousLocale: ReturnType<typeof getLocale>;
6+
7+
beforeEach(() => {
8+
previousLocale = getLocale();
9+
setLocale('en');
10+
});
11+
12+
afterEach(() => {
13+
setLocale(previousLocale);
14+
});
15+
416
describe('without existing instructions', () => {
517
it('should return base prompt when existing instructions is empty', () => {
618
const result = buildRefineSystemPrompt('');
@@ -10,7 +22,6 @@ describe('buildRefineSystemPrompt', () => {
1022
expect(result).toContain('**Process**');
1123
expect(result).toContain('**Guidelines**');
1224
expect(result).toContain('**Output Format**');
13-
expect(result).toContain('**Examples**');
1425
});
1526

1627
it('should not include existing instructions section when empty', () => {
@@ -93,29 +104,22 @@ describe('buildRefineSystemPrompt', () => {
93104
expect(result).toContain('**Ambiguity**');
94105
});
95106

96-
it('should include examples', () => {
107+
it('should use the shared language rule while preserving formatting examples', () => {
97108
const result = buildRefineSystemPrompt('');
98109

110+
expect(result).toContain("same language as the user's request");
111+
expect(result).toContain('UI locale (`en`)');
112+
expect(result).toContain('proper nouns, product names, file names, paths, code identifiers, and code snippets unchanged');
113+
expect(result).toContain('The examples below illustrate formatting only');
99114
expect(result).toContain('Input: "typescript for code"');
100-
expect(result).toContain('Input: "be concise"');
101-
expect(result).toContain('Input: "organize coding style rules"');
102-
expect(result).toContain('Input: "use that thing from before"');
115+
expect(result).not.toContain('"Do X" instead of "Don\'t do Y"');
116+
expect(result.indexOf('**Language**')).toBeLessThan(result.indexOf('**Examples**'));
103117
});
104118

105-
it('should show example outputs with proper tag wrapping', () => {
106-
const result = buildRefineSystemPrompt('');
107-
108-
expect(result).toContain('Output: <instruction>');
109-
expect(result).toContain('**Code Language**');
110-
expect(result).toContain('**Conciseness**');
111-
expect(result).toContain('## Coding Standards');
112-
});
113-
114-
it('should include ambiguity handling example', () => {
115-
const result = buildRefineSystemPrompt('');
119+
it('should fall back to the current UI locale', () => {
120+
setLocale('zh-CN');
116121

117-
expect(result).toContain('I\'m not sure what you\'re referring to');
118-
expect(result).toContain('Could you please clarify');
122+
expect(buildRefineSystemPrompt('')).toContain('UI locale (`zh-CN`)');
119123
});
120124
});
121125

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
1-
import { TITLE_GENERATION_SYSTEM_PROMPT } from '@/qoder/prompt/title-generation';
1+
import { getLocale, setLocale } from '@/i18n/i18n';
2+
import {
3+
buildTitleGenerationPrompt,
4+
getTitleGenerationSystemPrompt,
5+
parseTitleGenerationResponse,
6+
} from '@/qoder/prompt/title-generation';
27

38
describe('titleGeneration', () => {
9+
let previousLocale: ReturnType<typeof getLocale>;
10+
11+
beforeEach(() => {
12+
previousLocale = getLocale();
13+
setLocale('en');
14+
});
15+
16+
afterEach(() => {
17+
setLocale(previousLocale);
18+
});
19+
420
it('exports a non-empty system prompt string', () => {
5-
expect(typeof TITLE_GENERATION_SYSTEM_PROMPT).toBe('string');
6-
expect(TITLE_GENERATION_SYSTEM_PROMPT.length).toBeGreaterThan(0);
21+
const prompt = getTitleGenerationSystemPrompt();
22+
expect(typeof prompt).toBe('string');
23+
expect(prompt.length).toBeGreaterThan(0);
724
});
825

926
it('includes the max character constraint', () => {
10-
expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('max 50 chars');
27+
expect(getTitleGenerationSystemPrompt()).toContain('max 50 chars');
1128
});
1229

13-
it('instructs to start with a strong verb', () => {
14-
expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('strong verb');
30+
it('instructs to start with a strong verb in the output language', () => {
31+
expect(getTitleGenerationSystemPrompt()).toContain('strong verb');
32+
expect(getTitleGenerationSystemPrompt()).toContain('output language');
1533
});
1634

1735
it('instructs to return only the raw title text', () => {
18-
expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('ONLY the raw title text');
36+
expect(getTitleGenerationSystemPrompt()).toContain('ONLY the raw title text');
37+
});
38+
39+
it('uses one language rule without English verb or title examples', () => {
40+
const prompt = getTitleGenerationSystemPrompt();
41+
42+
expect(prompt).toContain("same language as the user's request");
43+
expect(prompt).toContain('UI locale (`en`)');
44+
expect(prompt).toContain('proper nouns, product names, file names, paths, code identifiers, and code snippets unchanged');
45+
expect(prompt).not.toContain('Create, Fix, Debug, Explain, Analyze');
46+
expect(prompt).not.toContain('Debug Python script');
47+
expect(prompt).not.toContain('Refactor React hook');
48+
});
49+
50+
it('falls back to the current UI locale', () => {
51+
setLocale('zh-CN');
52+
53+
expect(getTitleGenerationSystemPrompt()).toContain('UI locale (`zh-CN`)');
54+
});
55+
56+
it.each([
57+
['Chinese', '请修复 useReducer 的状态更新'],
58+
['English', 'Fix useReducer state updates'],
59+
['mixed code identifiers', '请修复 Qoderian 中 notes/demo.md 的 useReducer'],
60+
])('preserves %s request text and identifiers', (_language, request) => {
61+
expect(buildTitleGenerationPrompt(request)).toContain(request);
62+
});
63+
64+
it('preserves a Chinese title and its code identifiers', () => {
65+
expect(parseTitleGenerationResponse('修复 Qoderian 的 useReducer')).toBe('修复 Qoderian 的 useReducer');
1966
});
2067
});

0 commit comments

Comments
 (0)