forked from Blendman974/faceclaw
feat: configure an independent OpenAI translation model
This commit is contained in:
5 files changed
+52
-9
No files matched your search
@@ -4,6 +4,7 @@ import {
|
|||||||
resolveTranslateLanguage,
|
resolveTranslateLanguage,
|
||||||
resolveTranslateProvider,
|
resolveTranslateProvider,
|
||||||
translateLanguageSetting,
|
translateLanguageSetting,
|
||||||
|
translateModelSetting,
|
||||||
translateProviderSetting,
|
translateProviderSetting,
|
||||||
translateShowOriginalSetting,
|
translateShowOriginalSetting,
|
||||||
} from "./translate-settings";
|
} from "./translate-settings";
|
||||||
@@ -11,7 +12,7 @@ import { MIC_ICON } from "../transcribe/transcribe-app";
|
|||||||
import { transcriptTimestamp } from "../transcribe/transcribe";
|
import { transcriptTimestamp } from "../transcribe/transcribe";
|
||||||
import { type ContinuousCaptureOptions } from "../../ui/layers";
|
import { type ContinuousCaptureOptions } from "../../ui/layers";
|
||||||
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
||||||
import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
import { enumSettingMenuItem, textSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||||
import {
|
import {
|
||||||
createInProcessWindow,
|
createInProcessWindow,
|
||||||
YieldAtRootLayer,
|
YieldAtRootLayer,
|
||||||
@@ -28,8 +29,8 @@ const TRAY_ICON_ID = "translate";
|
|||||||
const NO_PROVIDER_PROBLEM =
|
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).";
|
"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
|
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: 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: 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 one in Settings > Assistant, or download the on-phone model.";
|
||||||
|
|
||||||
export type TranslateAppOptions = InProcessAppOptions & {
|
export type TranslateAppOptions = InProcessAppOptions & {
|
||||||
/** Begin (or retarget) continuous mic capture, kept on while the window is open. */
|
/** 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", [
|
openSettingsSubMenu(ctx, "Translate settings", [
|
||||||
enumSettingMenuItem(translateLanguageSetting, { onChange: startCapture }),
|
enumSettingMenuItem(translateLanguageSetting, { onChange: startCapture }),
|
||||||
enumSettingMenuItem(translateProviderSetting, { onChange: startCapture }),
|
enumSettingMenuItem(translateProviderSetting, { onChange: startCapture }),
|
||||||
|
textSettingMenuItem(translateModelSetting),
|
||||||
toggleSettingMenuItem(translateShowOriginalSetting),
|
toggleSettingMenuItem(translateShowOriginalSetting),
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Streamed translation against the assistant's configured model (Settings >
|
* Streamed translation against Translate's OpenAI override, or the assistant's
|
||||||
* Assistant). 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 English; see
|
||||||
* TranslationModel for how stretches are batched and given context.
|
* TranslationModel for how stretches are batched and given context.
|
||||||
*/
|
*/
|
||||||
import { streamAnthropicMessage } from "../../native/anthropic";
|
import { streamAnthropicMessage } from "../../native/anthropic";
|
||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting,
|
assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting,
|
||||||
} from "../../ui/dashboard-settings";
|
} from "../../ui/dashboard-settings";
|
||||||
import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts";
|
import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts";
|
||||||
|
import { translateModelSetting } from "./translate-settings";
|
||||||
import { type TranslationTurn } from "./translation-model";
|
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.
|
* for whichever API key is set, else the on-phone model.
|
||||||
*/
|
*/
|
||||||
export function resolveTranslationModel(): ResolvedAssistantModel | null {
|
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();
|
const selection = assistantModelSetting.get();
|
||||||
return resolveAssistantModel(selection === "hermes" ? "auto" : selection, {
|
return resolveAssistantModel(selection === "hermes" ? "auto" : selection, {
|
||||||
anthropic: anthropicApiKeySetting.get(),
|
anthropic: anthropicApiKeySetting.get(),
|
||||||
|
|||||||
@@ -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
|
* The transcription providers that recognize any spoken language, in picker
|
||||||
@@ -106,3 +106,14 @@ export const translateShowOriginalSetting = new ConfigSettingBoolean({
|
|||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
description: "Show what was said, dimmed, above each translation.",
|
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.",
|
||||||
|
});
|
||||||
@@ -2,7 +2,7 @@ const test = require('node:test');
|
|||||||
const assert = require('node:assert/strict');
|
const assert = require('node:assert/strict');
|
||||||
const { loader } = require('./helpers/load-typescript.cjs');
|
const { loader } = require('./helpers/load-typescript.cjs');
|
||||||
|
|
||||||
function sharedEnv() {
|
function sharedEnv({ translationModel = '', openAiKey = '', assistantModel = 'openai-compatible' } = {}) {
|
||||||
const compatible = [], unexpected = [];
|
const compatible = [], unexpected = [];
|
||||||
const localLlm = {
|
const localLlm = {
|
||||||
streamLocalModel: () => unexpected.push('local'),
|
streamLocalModel: () => unexpected.push('local'),
|
||||||
@@ -12,11 +12,11 @@ function sharedEnv() {
|
|||||||
};
|
};
|
||||||
const settings = {
|
const settings = {
|
||||||
anthropicApiKeySetting: { get: () => '' },
|
anthropicApiKeySetting: { get: () => '' },
|
||||||
assistantModelSetting: { get: () => 'openai-compatible' },
|
assistantModelSetting: { get: () => assistantModel },
|
||||||
assistantOpenAiCompatibleUrlSetting: { get: () => 'https://provider.example/api/v1' },
|
assistantOpenAiCompatibleUrlSetting: { get: () => 'https://provider.example/api/v1' },
|
||||||
assistantOpenAiCompatibleKeySetting: { get: () => 'compatible-key' },
|
assistantOpenAiCompatibleKeySetting: { get: () => 'compatible-key' },
|
||||||
assistantOpenAiCompatibleModelSetting: { get: () => 'compatible-model' },
|
assistantOpenAiCompatibleModelSetting: { get: () => 'compatible-model' },
|
||||||
openAiApiKeySetting: { get: () => '' },
|
openAiApiKeySetting: { get: () => openAiKey },
|
||||||
};
|
};
|
||||||
const fail = provider => () => { unexpected.push(provider); };
|
const fail = provider => () => { unexpected.push(provider); };
|
||||||
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
|
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
|
||||||
@@ -31,6 +31,7 @@ function sharedEnv() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
'../../ui/dashboard-settings': settings,
|
'../../ui/dashboard-settings': settings,
|
||||||
|
'./translate-settings': { translateModelSetting: { get: () => translationModel } },
|
||||||
'../../prompts': {
|
'../../prompts': {
|
||||||
CALCULATOR_REWRITE_SYSTEM_PROMPT: 'calculator system',
|
CALCULATOR_REWRITE_SYSTEM_PROMPT: 'calculator system',
|
||||||
buildCalculatorRewriteUserMessage: spoken => `rewrite: ${spoken}`,
|
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(answers, ['Hello']);
|
||||||
assert.deepEqual(errors, []);
|
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');
|
||||||
|
});
|
||||||
@@ -23,6 +23,7 @@ function settingsHarness({ main = 'onboard', setUp = [] } = {}) {
|
|||||||
'../../ui/dashboard-settings': {
|
'../../ui/dashboard-settings': {
|
||||||
ConfigSettingEnum: Setting,
|
ConfigSettingEnum: Setting,
|
||||||
ConfigSettingBoolean: Setting,
|
ConfigSettingBoolean: Setting,
|
||||||
|
ConfigSettingString: Setting,
|
||||||
voiceProviderSetting: {
|
voiceProviderSetting: {
|
||||||
get: () => main,
|
get: () => main,
|
||||||
isDisabled: provider => !available.has(provider),
|
isDisabled: provider => !available.has(provider),
|
||||||
|
|||||||
Reference in new issue
Block a user