-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(core): apply output language to side queries #4636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { readFile } from 'node:fs/promises'; | ||
| import type { | ||
| Content, | ||
| GenerateContentConfig, | ||
|
|
@@ -123,6 +124,50 @@ function applyThinkingDefault( | |
| }; | ||
| } | ||
|
|
||
| async function getOutputLanguageInstruction( | ||
| config: Config, | ||
| ): Promise<string | undefined> { | ||
| const outputLanguageFilePath = config.getOutputLanguageFilePath?.(); | ||
| if (!outputLanguageFilePath) return undefined; | ||
|
|
||
| try { | ||
| const preference = (await readFile(outputLanguageFilePath, 'utf8')).trim(); | ||
| if (!preference) return undefined; | ||
|
|
||
| return [ | ||
| 'Follow the user-visible output language preference below for this side query.', | ||
| preference, | ||
| ].join('\n\n'); | ||
| } catch { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The empty Compare with — qwen3.7-max via Qwen Code /review |
||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function appendSystemInstruction( | ||
| systemInstruction: string | Part | Part[] | Content | undefined, | ||
| outputLanguageInstruction: string | undefined, | ||
| ): string | Part | Part[] | Content | undefined { | ||
| if (!outputLanguageInstruction) return systemInstruction; | ||
| if (systemInstruction === undefined) return outputLanguageInstruction; | ||
| if (typeof systemInstruction === 'string') { | ||
| return `${systemInstruction}\n\n${outputLanguageInstruction}`; | ||
| } | ||
| if (Array.isArray(systemInstruction)) { | ||
| return [...systemInstruction, { text: outputLanguageInstruction }]; | ||
| } | ||
| if ( | ||
| typeof systemInstruction === 'object' && | ||
| 'parts' in systemInstruction && | ||
| Array.isArray(systemInstruction.parts) | ||
| ) { | ||
| return { | ||
| ...systemInstruction, | ||
| parts: [...systemInstruction.parts, { text: outputLanguageInstruction }], | ||
| }; | ||
| } | ||
| return [systemInstruction as Part, { text: outputLanguageInstruction }]; | ||
| } | ||
|
|
||
| function isJsonOptions<TResponse>( | ||
| options: SideQueryTextOptions | SideQueryJsonOptions<TResponse>, | ||
| ): options is SideQueryJsonOptions<TResponse> { | ||
|
|
@@ -147,14 +192,18 @@ export async function runSideQuery<TResponse>( | |
| const model = resolveDefaultModel(config, options.model); | ||
| const promptId = options.promptId ?? buildDefaultPromptId(options.purpose); | ||
| const requestConfig = applyThinkingDefault(options.config); | ||
| const systemInstruction = appendSystemInstruction( | ||
| options.systemInstruction, | ||
| await getOutputLanguageInstruction(config), | ||
| ); | ||
|
|
||
| if (isJsonOptions(options)) { | ||
| const response = (await config.getBaseLlmClient().generateJson({ | ||
| contents: options.contents, | ||
| schema: options.schema, | ||
| abortSignal: options.abortSignal, | ||
| model, | ||
| systemInstruction: options.systemInstruction, | ||
| systemInstruction, | ||
| promptId, | ||
| config: requestConfig, | ||
| ...(options.maxAttempts !== undefined && { | ||
|
|
@@ -178,7 +227,7 @@ export async function runSideQuery<TResponse>( | |
| const result = await config.getBaseLlmClient().generateText({ | ||
| contents: options.contents, | ||
| model, | ||
| systemInstruction: options.systemInstruction, | ||
| systemInstruction, | ||
| abortSignal: options.abortSignal, | ||
| promptId, | ||
| config: requestConfig, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion]
getOutputLanguageInstructionreads the file on everyrunSideQuerycall. This is a hot path (13+ callers: chatCompressionService, ArenaManager, sessionRecap, toolUseSummary, etc.), and the file content is effectively static within a session (only changed via/languagecommand).Consider caching the result — either in the Config constructor (read once at startup) or as a module-level cache — to make this a synchronous lookup and avoid repeated disk I/O.
— qwen3.7-max via Qwen Code /review