feat: choose translation output language independently

This commit is contained in:
Blendman974 committed 2026-11-08 00:56:36 +01:00
1 parent 69bb92da7b
commit 26e570f0e1
8 files changed
+81 -27

No files matched your search

+8 -7
View File
@@ -1,6 +1,6 @@
/**
* Streamed translation against Translate's OpenAI override, or the assistant's
* configured model when no override is set. Each call translates one stretch of speech into English; see
* configured model when no override is set. Each call translates one stretch of speech into the selected output language; see
* TranslationModel for how stretches are batched and given context.
*/
import { streamAnthropicMessage } from "../../native/anthropic";
@@ -13,8 +13,8 @@ import {
anthropicApiKeySetting, assistantModelSetting, assistantOpenAiCompatibleKeySetting,
assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting,
} from "../../ui/dashboard-settings";
import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts";
import { translateModelSetting } from "./translate-settings";
import { buildTranslateSystemPrompt, buildTranslateUserMessage } from "../../prompts";
import { translateModelSetting, translateTargetLanguageSetting, spokenLanguageName } from "./translate-settings";
import { type TranslationTurn } from "./translation-model";
/**
@@ -50,13 +50,14 @@ export type TranslationRequest = {
};
export function streamTranslation(model: ResolvedAssistantModel, request: TranslationRequest): LlmStreamHandle {
const targetLanguage = spokenLanguageName(translateTargetLanguageSetting.get());
const options: LlmStreamOptions = {
apiKey: model.apiKey,
model: model.model,
system: TRANSLATE_SYSTEM_PROMPT,
system: buildTranslateSystemPrompt(targetLanguage),
effort: model.effort,
maxTokens: 1024,
messages: [{ role: "user", content: buildTranslateUserMessage(request.speech, request.context, request.language) }],
messages: [{ role: "user", content: buildTranslateUserMessage(request.speech, request.context, request.language, targetLanguage) }],
onTextDelta: (_delta, textSoFar) => request.onProgress(cleanTranslation(textSoFar)),
onDone: (result) => {
if (result.stopReason === "refusal") request.onError("The model declined to translate this.");
@@ -76,7 +77,7 @@ export function streamTranslation(model: ResolvedAssistantModel, request: Transl
}
}
/** The context turns are tagged <english>; a model may echo the tag around its reply. */
/** The context turns are tagged <translation>; a model may echo the tag around its reply. */
function cleanTranslation(text: string): string {
return text.replace(/^\s*<english>/i, "").replace(/<\/english>\s*$/i, "").trim();
return text.replace(/^\s*<(?:translation|english)>/i, "").replace(/<\/(?:translation|english)>\s*$/i, "").trim();
}