diff --git a/app/apps/translate/translate-app.ts b/app/apps/translate/translate-app.ts index dd0800ab..6dbc2935 100644 --- a/app/apps/translate/translate-app.ts +++ b/app/apps/translate/translate-app.ts @@ -4,6 +4,7 @@ import { resolveTranslateLanguage, resolveTranslateProvider, translateLanguageSetting, + translateModelSetting, translateProviderSetting, translateShowOriginalSetting, } from "./translate-settings"; @@ -11,7 +12,7 @@ import { MIC_ICON } from "../transcribe/transcribe-app"; import { transcriptTimestamp } from "../transcribe/transcribe"; import { type ContinuousCaptureOptions } from "../../ui/layers"; import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel"; -import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings"; +import { enumSettingMenuItem, textSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings"; import { createInProcessWindow, YieldAtRootLayer, @@ -28,8 +29,8 @@ const TRAY_ICON_ID = "translate"; const NO_PROVIDER_PROBLEM = "Translate needs a transcription provider that understands any language: add an ElevenLabs, OpenAI, or Soniox API key (Settings > API Keys), or a Whisper server URL (Settings > Voice)."; const NO_MODEL_PROBLEM = global.isIOS - ? "Translate needs a language model: configure a cloud or OpenAI-compatible model in Settings > Assistant." - : "Translate needs a language model: configure one in Settings > Assistant, or download the on-phone model."; + ? "Translate needs a language model: set an OpenAI model in Translate settings and add its API key, or configure a cloud or OpenAI-compatible model in Settings > Assistant." + : "Translate needs a language model: set an OpenAI model in Translate settings and add its API key, or configure one in Settings > Assistant, or download the on-phone model."; export type TranslateAppOptions = InProcessAppOptions & { /** Begin (or retarget) continuous mic capture, kept on while the window is open. */ @@ -84,6 +85,7 @@ export function createTranslateAppWindow(options: TranslateAppOptions): InProces openSettingsSubMenu(ctx, "Translate settings", [ enumSettingMenuItem(translateLanguageSetting, { onChange: startCapture }), enumSettingMenuItem(translateProviderSetting, { onChange: startCapture }), + textSettingMenuItem(translateModelSetting), toggleSettingMenuItem(translateShowOriginalSetting), ]); }, diff --git a/app/apps/translate/translate-llm.ts b/app/apps/translate/translate-llm.ts index e3447d38..7364f873 100644 --- a/app/apps/translate/translate-llm.ts +++ b/app/apps/translate/translate-llm.ts @@ -1,6 +1,6 @@ /** - * Streamed translation against the assistant's configured model (Settings > - * Assistant). Each call translates one stretch of speech into English; see + * 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 * TranslationModel for how stretches are batched and given context. */ import { streamAnthropicMessage } from "../../native/anthropic"; @@ -14,6 +14,7 @@ import { assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting, } from "../../ui/dashboard-settings"; import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts"; +import { translateModelSetting } from "./translate-settings"; import { type TranslationTurn } from "./translation-model"; /** @@ -23,6 +24,11 @@ import { type TranslationTurn } from "./translation-model"; * for whichever API key is set, else the on-phone model. */ export function resolveTranslationModel(): ResolvedAssistantModel | null { + const translationModel = translateModelSetting.get().trim(); + if (translationModel) { + const apiKey = openAiApiKeySetting.get().trim(); + return apiKey ? { selection: "auto", provider: "openai", model: translationModel, apiKey } : null; + } const selection = assistantModelSetting.get(); return resolveAssistantModel(selection === "hermes" ? "auto" : selection, { anthropic: anthropicApiKeySetting.get(), diff --git a/app/apps/translate/translate-settings.ts b/app/apps/translate/translate-settings.ts index 802419e1..e6be4f8c 100644 --- a/app/apps/translate/translate-settings.ts +++ b/app/apps/translate/translate-settings.ts @@ -1,4 +1,4 @@ -import { ConfigSettingBoolean, ConfigSettingEnum, voiceProviderSetting, type VoiceProvider } from "../../ui/dashboard-settings"; +import { ConfigSettingBoolean, ConfigSettingEnum, ConfigSettingString, voiceProviderSetting, type VoiceProvider } from "../../ui/dashboard-settings"; /** * The transcription providers that recognize any spoken language, in picker @@ -106,3 +106,14 @@ export const translateShowOriginalSetting = new ConfigSettingBoolean({ defaultValue: false, description: "Show what was said, dimmed, above each translation.", }); + +/** Empty preserves the existing Assistant-based routing. */ +export const translateModelSetting = new ConfigSettingString({ + id: "translate-model", + label: "Translation model (OpenAI)", + storageKey: "translate.model", + defaultValue: "", + inputKind: "text", + normalize: (value) => value.trim(), + description: "OpenAI model ID used only by Translate, for example gpt-4o-mini. Uses the OpenAI key from Settings > API Keys. Leave empty to use the Assistant model. Changes apply to subsequent translation requests; Assistant settings are unchanged.", +}); diff --git a/tests/openai-compatible-shared-features.test.cjs b/tests/openai-compatible-shared-features.test.cjs index 385b7c70..a0644aa6 100644 --- a/tests/openai-compatible-shared-features.test.cjs +++ b/tests/openai-compatible-shared-features.test.cjs @@ -2,7 +2,7 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); -function sharedEnv() { +function sharedEnv({ translationModel = '', openAiKey = '', assistantModel = 'openai-compatible' } = {}) { const compatible = [], unexpected = []; const localLlm = { streamLocalModel: () => unexpected.push('local'), @@ -12,11 +12,11 @@ function sharedEnv() { }; const settings = { anthropicApiKeySetting: { get: () => '' }, - assistantModelSetting: { get: () => 'openai-compatible' }, + assistantModelSetting: { get: () => assistantModel }, assistantOpenAiCompatibleUrlSetting: { get: () => 'https://provider.example/api/v1' }, assistantOpenAiCompatibleKeySetting: { get: () => 'compatible-key' }, assistantOpenAiCompatibleModelSetting: { get: () => 'compatible-model' }, - openAiApiKeySetting: { get: () => '' }, + openAiApiKeySetting: { get: () => openAiKey }, }; const fail = provider => () => { unexpected.push(provider); }; const load = loader({ global: { isIOS: false, isAndroid: true } }, { @@ -31,6 +31,7 @@ function sharedEnv() { }, }, '../../ui/dashboard-settings': settings, + './translate-settings': { translateModelSetting: { get: () => translationModel } }, '../../prompts': { CALCULATOR_REWRITE_SYSTEM_PROMPT: 'calculator system', buildCalculatorRewriteUserMessage: spoken => `rewrite: ${spoken}`, @@ -84,3 +85,25 @@ test('Translate resolves and streams through the compatible provider without cha assert.deepEqual(answers, ['Hello']); assert.deepEqual(errors, []); }); + +test('Translate overrides the model without changing Assistant configuration', () => { + const env = sharedEnv({ translationModel: ' gpt-4o-mini ', openAiKey: 'openai-key', assistantModel: 'terra' }); + const { resolveTranslationModel } = env.load('app/apps/translate/translate-llm.ts'); + const model = resolveTranslationModel(); + assert.equal(model.provider, 'openai'); + assert.equal(model.model, 'gpt-4o-mini'); + assert.equal(model.apiKey, 'openai-key'); + assert.equal(model.effort, undefined, 'do not send Assistant reasoning options to the custom model'); +}); + +test('Translate override requires the OpenAI key and does not silently fall back', () => { + const env = sharedEnv({ translationModel: 'gpt-4o-mini' }); + assert.equal(env.load('app/apps/translate/translate-llm.ts').resolveTranslationModel(), null); +}); + +test('Whitespace-only Translate override preserves Assistant routing', () => { + const env = sharedEnv({ translationModel: ' ' }); + const model = env.load('app/apps/translate/translate-llm.ts').resolveTranslationModel(); + assert.equal(model.provider, 'openai-compatible'); + assert.equal(model.model, 'compatible-model'); +}); diff --git a/tests/translate.test.cjs b/tests/translate.test.cjs index c9446b15..da983e63 100644 --- a/tests/translate.test.cjs +++ b/tests/translate.test.cjs @@ -23,6 +23,7 @@ function settingsHarness({ main = 'onboard', setUp = [] } = {}) { '../../ui/dashboard-settings': { ConfigSettingEnum: Setting, ConfigSettingBoolean: Setting, + ConfigSettingString: Setting, voiceProviderSetting: { get: () => main, isDisabled: provider => !available.has(provider),