The open-source AI voice studio. Clone, dictate, create.
Address CodeRabbit findings on the custom-endpoint PR
Moroya Sakamotobbe518dd5612
diff --git a/app/src/components/ServerTab/CapturesPage.tsx b/app/src/components/ServerTab/CapturesPage.tsxindex b26e3649..b56ca30d 100644--- a/app/src/components/ServerTab/CapturesPage.tsx+++ b/app/src/components/ServerTab/CapturesPage.tsx@@ -143,7 +143,10 @@ export function CapturesPage() {const toggleToTalkKeys = settings?.chord_toggle_to_talk_keys ?? defaultChordKeys('toggle');const customLlmEndpointSaved = settings?.custom_llm_endpoint ?? '';const customLlmModelSaved = settings?.custom_llm_model ?? '';- const customLlmApiKeySaved = settings?.custom_llm_api_key ?? '';+ // The server no longer returns the API key value; it reports whether one+ // is stored via ``custom_llm_api_key_configured``. Anything sitting in+ // React state below is a freshly-typed key that hasn't been sent yet.+ const customLlmApiKeyConfigured = Boolean(settings?.custom_llm_api_key_configured);const customLlmActive = Boolean(customLlmEndpointSaved && customLlmModelSaved);const [chordEditor, setChordEditor] = useState<'push' | 'toggle' | null>(null);@@ -153,7 +156,10 @@ export function CapturesPage() {// re-render on every keystroke while the user types out a URL or token.const [customLlmEndpointDraft, setCustomLlmEndpointDraft] = useState(customLlmEndpointSaved);const [customLlmModelDraft, setCustomLlmModelDraft] = useState(customLlmModelSaved);- const [customLlmApiKeyDraft, setCustomLlmApiKeyDraft] = useState(customLlmApiKeySaved);+ // Write-only field: the input is blank on load even when a key is stored,+ // and clears itself after a successful save so the raw value never lingers+ // in React state where a browser cache or dev-tools inspection could grab it.+ const [customLlmApiKeyDraft, setCustomLlmApiKeyDraft] = useState('');useEffect(() => {setCustomLlmEndpointDraft(customLlmEndpointSaved);@@ -161,9 +167,6 @@ export function CapturesPage() {useEffect(() => {setCustomLlmModelDraft(customLlmModelSaved);}, [customLlmModelSaved]);- useEffect(() => {- setCustomLlmApiKeyDraft(customLlmApiKeySaved);- }, [customLlmApiKeySaved]);const commitCustomLlmEndpoint = useCallback(() => {const next = customLlmEndpointDraft.trim();@@ -178,14 +181,18 @@ export function CapturesPage() {}}, [customLlmModelDraft, customLlmModelSaved, update]);const commitCustomLlmApiKey = useCallback(() => {- // The API key can legitimately contain leading/trailing punctuation for- // some providers, so we only trim when the whole field is whitespace.const trimmed = customLlmApiKeyDraft.trim();- const next = trimmed ? customLlmApiKeyDraft : '';- if (next !== customLlmApiKeySaved) {- update({ custom_llm_api_key: next || null });- }- }, [customLlmApiKeyDraft, customLlmApiKeySaved, update]);+ if (!trimmed) return;+ // Fire-and-forget the key, then wipe it from state so nothing else in+ // the tree can read it back. The server flag flips to `configured=true`+ // on the next settings query.+ update({ custom_llm_api_key: customLlmApiKeyDraft });+ setCustomLlmApiKeyDraft('');+ }, [customLlmApiKeyDraft, update]);+ const clearCustomLlmApiKey = useCallback(() => {+ setCustomLlmApiKeyDraft('');+ update({ custom_llm_api_key: null });+ }, [update]);useEffect(() => {fetch(`${serverUrl}/health/filesystem`)@@ -501,6 +508,9 @@ export function CapturesPage() {description={t('settings.captures.refinement.customEndpoint.description')}><div className="space-y-2 max-w-lg">+ <label htmlFor="customLlmEndpoint" className="block text-xs font-medium text-muted-foreground">+ {t('settings.captures.refinement.customEndpoint.endpointLabel')}+ </label><Inputid="customLlmEndpoint"type="url"@@ -511,7 +521,11 @@ export function CapturesPage() {disabled={!autoRefine}autoComplete="off"spellCheck={false}+ aria-label={t('settings.captures.refinement.customEndpoint.endpointLabel')}/>+ <label htmlFor="customLlmModel" className="block text-xs font-medium text-muted-foreground pt-1">+ {t('settings.captures.refinement.customEndpoint.modelLabel')}+ </label><Inputid="customLlmModel"type="text"@@ -522,18 +536,45 @@ export function CapturesPage() {disabled={!autoRefine || !customLlmEndpointDraft}autoComplete="off"spellCheck={false}+ aria-label={t('settings.captures.refinement.customEndpoint.modelLabel')}/>- <Input- id="customLlmApiKey"- type="password"- placeholder={t('settings.captures.refinement.customEndpoint.apiKeyPlaceholder')}- value={customLlmApiKeyDraft}- onChange={(e) => setCustomLlmApiKeyDraft(e.target.value)}- onBlur={commitCustomLlmApiKey}- disabled={!autoRefine || !customLlmEndpointDraft}- autoComplete="off"- spellCheck={false}- />+ <label htmlFor="customLlmApiKey" className="block text-xs font-medium text-muted-foreground pt-1">+ {t('settings.captures.refinement.customEndpoint.apiKeyLabel')}+ {customLlmApiKeyConfigured ? (+ <span className="ml-2 text-[10px] uppercase tracking-wide text-emerald-600">+ {t('settings.captures.refinement.customEndpoint.apiKeyConfigured')}+ </span>+ ) : null}+ </label>+ <div className="flex gap-2">+ <Input+ id="customLlmApiKey"+ type="password"+ placeholder={+ customLlmApiKeyConfigured+ ? t('settings.captures.refinement.customEndpoint.apiKeyPlaceholderReplace')+ : t('settings.captures.refinement.customEndpoint.apiKeyPlaceholder')+ }+ value={customLlmApiKeyDraft}+ onChange={(e) => setCustomLlmApiKeyDraft(e.target.value)}+ onBlur={commitCustomLlmApiKey}+ disabled={!autoRefine || !customLlmEndpointDraft}+ autoComplete="new-password"+ spellCheck={false}+ aria-label={t('settings.captures.refinement.customEndpoint.apiKeyLabel')}+ className="flex-1"+ />+ {customLlmApiKeyConfigured && (+ <button+ type="button"+ onClick={clearCustomLlmApiKey}+ className="text-xs text-muted-foreground hover:text-destructive px-2 py-1 border border-border rounded-md"+ aria-label={t('settings.captures.refinement.customEndpoint.apiKeyClear')}+ >+ {t('settings.captures.refinement.customEndpoint.apiKeyClear')}+ </button>+ )}+ </div><p className="text-xs text-muted-foreground">{customLlmActive? t('settings.captures.refinement.customEndpoint.statusRemote', {@@ -639,8 +680,14 @@ export function CapturesPage() {<li className="flex gap-2.5"><Lock className="h-4 w-4 shrink-0 mt-0.5 text-accent" /><span className="leading-relaxed">- <span className="text-foreground font-medium">{t('settings.captures.sidebar.local.title')}</span>{' '}- {t('settings.captures.sidebar.local.body')}+ <span className="text-foreground font-medium">+ {customLlmActive+ ? t('settings.captures.sidebar.remote.title')+ : t('settings.captures.sidebar.local.title')}+ </span>{' '}+ {customLlmActive+ ? t('settings.captures.sidebar.remote.body', { endpoint: customLlmEndpointSaved })+ : t('settings.captures.sidebar.local.body')}</span></li><li className="flex gap-2.5">diff --git a/app/src/i18n/locales/en/translation.json b/app/src/i18n/locales/en/translation.jsonindex 3c0bab96..f8973fda 100644--- a/app/src/i18n/locales/en/translation.json+++ b/app/src/i18n/locales/en/translation.json@@ -986,11 +986,17 @@"customEndpoint": {"title": "Custom LLM endpoint (advanced)","description": "Delegate refinement and personality rewrites to any OpenAI-compatible server — llama.cpp, vLLM, LM Studio, LocalAI, Ollama's shim, or the OpenAI API itself. Leave blank to use the built-in Qwen3.",+ "endpointLabel": "Endpoint URL","endpointPlaceholder": "http://localhost:8090/v1",+ "modelLabel": "Model name","modelPlaceholder": "Model name (e.g. qwen3-4b)",+ "apiKeyLabel": "API key","apiKeyPlaceholder": "API key (optional)",+ "apiKeyPlaceholderReplace": "Enter a new key to replace the stored one",+ "apiKeyConfigured": "Stored",+ "apiKeyClear": "Clear","statusBuiltin": "Using the built-in Qwen3 model selected above.",- "statusRemote": "Sending every refinement to {{endpoint}}."+ "statusRemote": "Sending every refinement and personality rewrite to {{endpoint}}."}},"playback": {@@ -1029,6 +1035,10 @@"title": "Fully local.","body": "Whisper and the refinement LLM run on your hardware. No cloud, no accounts, your voice never leaves the machine."},+ "remote": {+ "title": "Refinement runs remotely.",+ "body": "Whisper still transcribes on your hardware, but refined transcripts and personality rewrites are sent to {{endpoint}}. Voice audio itself never leaves the machine."+ },"playAs": {"title": "Play as any voice.","body": "Transcripts can be read back in any profile you've cloned."diff --git a/app/src/i18n/locales/ja/translation.json b/app/src/i18n/locales/ja/translation.jsonindex 3171d585..f6640e86 100644--- a/app/src/i18n/locales/ja/translation.json+++ b/app/src/i18n/locales/ja/translation.json@@ -981,11 +981,17 @@"customEndpoint": {"title": "カスタム LLM エンドポイント (上級)","description": "リファインとパーソナリティの書き換えを、任意の OpenAI 互換サーバー (llama.cpp、vLLM、LM Studio、LocalAI、Ollama の互換 API、OpenAI API 自体など) に委譲します。空欄のままなら内蔵の Qwen3 を使います。",+ "endpointLabel": "エンドポイント URL","endpointPlaceholder": "http://localhost:8090/v1",+ "modelLabel": "モデル名","modelPlaceholder": "モデル名 (例: qwen3-4b)",+ "apiKeyLabel": "API キー","apiKeyPlaceholder": "API キー (任意)",+ "apiKeyPlaceholderReplace": "上書きする新しいキーを入力",+ "apiKeyConfigured": "保存済",+ "apiKeyClear": "クリア","statusBuiltin": "上で選んだ内蔵 Qwen3 モデルを使用しています",- "statusRemote": "リファインを {{endpoint}} に送信しています"+ "statusRemote": "リファインとパーソナリティの書き換えを {{endpoint}} に送信しています"}},"playback": {@@ -1024,6 +1030,10 @@"title": "完全にローカル。","body": "Whisper と整形用 LLM はあなたのハードウェア上で動作します。クラウドもアカウントも不要で、声がマシンの外に出ることはありません。"},+ "remote": {+ "title": "整形処理はリモート実行中。",+ "body": "Whisper はハードウェア上で書き起こしを続けますが、整形された文字起こしとパーソナリティ書き換えは {{endpoint}} に送られます。音声そのものはマシンから出ません。"+ },"playAs": {"title": "どのボイスでも再生。","body": "クローンしたどのプロファイルでも文字起こしを読み上げできます。"diff --git a/app/src/lib/api/types.ts b/app/src/lib/api/types.tsindex 0357a6a5..ba2d7df7 100644--- a/app/src/lib/api/types.ts+++ b/app/src/lib/api/types.ts@@ -225,10 +225,22 @@ export interface CaptureSettings {*/custom_llm_endpoint: string | null;custom_llm_model: string | null;- custom_llm_api_key: string | null;+ /**+ * Whether a custom LLM API key is currently stored on the server. The raw+ * key value never rides the response — this flag replaces it — so the+ * settings UI can show a "Configured" indicator without letting the+ * frontend rehydrate the secret into state or leak it to a browser cache.+ * Writes still go through ``CaptureSettingsUpdate.custom_llm_api_key``.+ */+ custom_llm_api_key_configured: boolean;}-export type CaptureSettingsUpdate = Partial<CaptureSettings>;+export type CaptureSettingsUpdate = Partial<+ Omit<CaptureSettings, 'custom_llm_api_key_configured'>+> & {+ /** Write-only: setting this to a non-empty string stores it, ``null`` clears it. */+ custom_llm_api_key?: string | null;+};/*** One row in the dictation readiness checklist. ``model_name`` is thediff --git a/backend/backends/openai_compat_backend.py b/backend/backends/openai_compat_backend.pyindex 75f12045..b60cd457 100644--- a/backend/backends/openai_compat_backend.py+++ b/backend/backends/openai_compat_backend.py@@ -185,4 +185,13 @@ def _extract_content(data: dict, url: str) -> str:content = first.get("text")if content is None:raise ValueError(f"No content in response from {url}: {data!r}")+ if not isinstance(content, str):+ # Vision-capable and tool-use servers sometimes return the content as+ # an array of typed parts instead of a bare string. Refinement /+ # personality are text-only paths, so surface a clear error instead+ # of tripping ``AttributeError`` on ``.strip()`` and burying the+ # shape mismatch.+ raise ValueError(+ f"Unexpected content type {type(content).__name__} in response from {url}: {data!r}"+ )return content.strip()diff --git a/backend/models.py b/backend/models.pyindex fd7bbbc4..dad76967 100644--- a/backend/models.py+++ b/backend/models.py@@ -2,8 +2,8 @@Pydantic models for request/response validation."""-from pydantic import BaseModel, Field-from typing import Optional, List+from pydantic import BaseModel, Field, model_validator+from typing import Any, Optional, Listfrom datetime import datetimefrom .utils.capture_chords import (@@ -266,7 +266,36 @@ class CaptureSettingsResponse(BaseModel):)custom_llm_endpoint: Optional[str] = Nonecustom_llm_model: Optional[str] = None- custom_llm_api_key: Optional[str] = None+ # Provider credential — write-only. The response reports whether one is+ # configured but never echoes the value, so the settings API can't be+ # used to exfiltrate stored keys and the frontend can't rehydrate them+ # into React state where a browser cache could pick them up.+ custom_llm_api_key_configured: bool = False++ @model_validator(mode="before")+ @classmethod+ def _mask_custom_llm_api_key(cls, data: Any) -> Any:+ """Replace the raw ``custom_llm_api_key`` with a boolean ``_configured`` flag.++ Runs before field validation so the stored credential is never+ materialised on a Response instance, even in memory. Accepts both+ SQLAlchemy ORM rows (``from_attributes=True`` path) and plain dicts+ so tests that construct the model directly get the same treatment.+ """+ if isinstance(data, dict):+ raw_key = data.pop("custom_llm_api_key", None)+ data.setdefault("custom_llm_api_key_configured", bool(raw_key))+ return data+ # ORM row — build a snapshot dict, drop the secret, and hand the+ # rest to Pydantic. ``from_attributes`` won't reach the raw+ # attribute anymore because the returned dict wins.+ columns = getattr(getattr(data, "__table__", None), "columns", None)+ if columns is None:+ return data+ snapshot = {c.name: getattr(data, c.name, None) for c in columns}+ raw_key = snapshot.pop("custom_llm_api_key", None)+ snapshot["custom_llm_api_key_configured"] = bool(raw_key)+ return snapshotclass Config:from_attributes = Truediff --git a/backend/tests/test_openai_compat_integration.py b/backend/tests/test_openai_compat_integration.pyindex d5ebbc58..f39fc600 100644--- a/backend/tests/test_openai_compat_integration.py+++ b/backend/tests/test_openai_compat_integration.py@@ -110,26 +110,40 @@ async def test_backend_generates_non_empty_response():async def test_backend_respects_examples_history():- """Few-shot examples reach the endpoint as proper user/assistant turns."""+ """Few-shot examples reach the endpoint as proper user/assistant turns.++ Uses an arbitrary mapping (``ping → PONG``, ``foo → BAR``) so a passing+ assertion actually proves the examples were serialised into the+ request. A non-empty translation would still pass if ``examples``+ were silently dropped; the model has to have seen the pattern to+ return the mapped token on a novel input.+ """backend = OpenAICompatLLMBackend(endpoint=_endpoint(),model=_model(),api_key=_api_key(),)reply = await backend.generate(- prompt="hola",+ prompt="baz",system=(- "You are a translation bot. Reply with the single-word English "- "translation of the user's message and nothing else."+ "You are a lookup function. Respond with exactly one uppercase word "+ "matching the mapping shown in the examples and nothing else."),- max_tokens=16,+ max_tokens=8,temperature=0.0,examples=[- ("bonjour", "hello"),- ("guten tag", "hello"),+ ("ping", "PONG"),+ ("foo", "BAR"),],)- assert reply.strip(), "Endpoint returned an empty translation"+ stripped = reply.strip().upper()+ # The model may add punctuation; check that the mapped-shape token+ # ("QUX" is the natural next entry after PONG/BAR) leads the reply.+ assert stripped, "Endpoint returned an empty response to a few-shot prompt"+ assert stripped.startswith(("QUX", "BAZ")), (+ f"Reply {reply!r} doesn't look like it followed the example mapping — "+ "the ``examples`` field may have been dropped in transit."+ )async def test_dispatch_returns_openai_compat_when_config_set():