Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(cleanup): Preserve raw text when cleanup repeats itself (#2300)
Chad Pihae22cb9563c68
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex f5a81072c..5bb50841f 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -1,5 +1,6 @@import ReasoningService from "../services/ReasoningService";import logger from "../utils/logger";+import { assertValidCleanupOutput } from "../utils/cleanupOutput";import { isAzureOpenAIEndpoint } from "../utils/urlUtils";import { withSessionRefresh } from "../lib/auth";import { getBaseLanguageCode, getLanguageLabel } from "../utils/languageSupport";@@ -2947,13 +2948,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);async runTranslationChain({ text, settings, agentName, route, cleanup }) {const runCleanup = async (currentText) => {if (cleanup.mode === "cloudReason") {+ const customPrompt = this.getCustomPrompt();const reasonResult = await withSessionRefresh(async () => {const res = await window.electronAPI.cloudReason(currentText, {agentName,promptMode: "cleanup",purpose: "cleanup",customDictionary: getDictionaryHintWords(settings),- customPrompt: this.getCustomPrompt(),+ customPrompt,language: this.getCleanupLanguage(settings),locale: settings.uiLanguage || "en",...(cleanup.meta || {}),@@ -2965,6 +2967,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}return res;});+ if (!customPrompt && hasTextContent(reasonResult.text)) {+ assertValidCleanupOutput(currentText, reasonResult.text);+ }return reasonResult.success && reasonResult.text ? reasonResult.text : null;}const cleanupModel = cleanup.model;@@ -3430,13 +3435,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor););if (hasTextContent(reasoned)) processedText = reasoned;} else if (route.kind === "cleanup" && cleanupCloudMode === "openwhispr") {+ const customPrompt = this.getCustomPrompt();const reasonResult = await withSessionRefresh(async () => {const res = await window.electronAPI.cloudReason(processedText, {agentName,promptMode: "cleanup",purpose: "cleanup",customDictionary: getDictionaryHintWords(settings),- customPrompt: this.getCustomPrompt(),+ customPrompt,language: this.getCleanupLanguage(settings),locale: settings.uiLanguage || "en",streamingFallbackReason,@@ -3459,6 +3465,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);// Cloud cleanup can return success with empty text; keep the raw transcription instead of wiping it.if (reasonResult.success && hasTextContent(reasonResult.text)) {+ if (!customPrompt) assertValidCleanupOutput(processedText, reasonResult.text);processedText = reasonResult.text;}} else if (route.kind === "cleanup") {@@ -5276,13 +5283,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);"streaming");} else if (route.kind === "cleanup" && cleanupCloudMode === "openwhispr") {+ const customPrompt = this.getCustomPrompt();const reasonResult = await withSessionRefresh(async () => {const res = await window.electronAPI.cloudReason(finalText, {agentName,promptMode: "cleanup",purpose: "cleanup",customDictionary: getDictionaryHintWords(stSettings),- customPrompt: this.getCustomPrompt(),+ customPrompt,language: this.getCleanupLanguage(stSettings),locale: stSettings.uiLanguage || "en",sttProvider: this.getStreamingProviderName(),@@ -5302,10 +5310,11 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);return res;});+ usedCloudReasoning = true;if (reasonResult.success && hasTextContent(reasonResult.text)) {+ if (!customPrompt) assertValidCleanupOutput(finalText, reasonResult.text);finalText = reasonResult.text;}- usedCloudReasoning = true;logger.info("Streaming reasoning complete",diff --git a/src/helpers/translationChain.js b/src/helpers/translationChain.jsindex e26b2e6b4..a3b32165d 100644--- a/src/helpers/translationChain.js+++ b/src/helpers/translationChain.js@@ -46,6 +46,10 @@ export async function executeTranslationChain({// returned empty text.if (cleanupIsCloud) usedCloudReasoning = true;} catch (cleanupError) {+ // Rejected output still came from a successful cloud call.+ if (cleanupIsCloud && cleanupError?.code === "CLEANUP_OUTPUT_INVALID") {+ usedCloudReasoning = true;+ }if (onCleanupError) onCleanupError(cleanupError);}}diff --git a/src/locales/ar/translation.json b/src/locales/ar/translation.jsonindex d9efab275..89ed6e37b 100644--- a/src/locales/ar/translation.json+++ b/src/locales/ar/translation.json@@ -48,6 +48,7 @@"streamingOnlyProvider": "هذا المزود يدعم التفريغ المباشر فقط. اختر مزودًا آخر لرفع الملفات وإعادة المحاولة.","providerKeyMissing": "لم يتم تعيين مفتاح API لمزود التفريغ الخاص بك. أضفه في الإعدادات ← تحويل الكلام إلى نص (Speech to Text).","cleanupTruncated": "نفدت مساحة الإخراج لدى نموذج الذكاء الاصطناعي قبل أن يُكمل تنقيح إملائك.",+ "cleanupDuplicated": "كرر تنقيح الذكاء الاصطناعي النص. تم الاحتفاظ بالنص الأصلي.","cleanupEmptyReply": "أرجع نموذج الذكاء الاصطناعي ردًا فارغًا بدلًا من الإملاء المنقَّح."},"translationFallback": {diff --git a/src/locales/de/translation.json b/src/locales/de/translation.jsonindex 184bfa47b..5141eb50e 100644--- a/src/locales/de/translation.json+++ b/src/locales/de/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "Dieser Anbieter unterstützt nur Live-Transkription. Wähle für Datei-Uploads und Wiederholungen einen anderen Anbieter.","providerKeyMissing": "Für Ihren Transkriptionsanbieter ist kein API-Schlüssel hinterlegt. Fügen Sie ihn unter Einstellungen → Sprache zu Text hinzu.","cleanupTruncated": "Dem KI-Modell ging der Platz aus, bevor es dein Diktat fertig bereinigt hatte.",+ "cleanupDuplicated": "Die KI-Bereinigung hat das Transkript wiederholt. Der Originaltext wurde beibehalten.","cleanupEmptyReply": "Das KI-Modell lieferte eine leere Antwort statt des bereinigten Diktats."},"translationFallback": {diff --git a/src/locales/en/translation.json b/src/locales/en/translation.jsonindex 471dc5634..ae051e337 100644--- a/src/locales/en/translation.json+++ b/src/locales/en/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "This provider only supports live transcription. Choose another provider for file uploads and retries.","providerKeyMissing": "No API key is set for your transcription provider. Add it in Settings → Speech to Text.","cleanupTruncated": "The AI model ran out of room before it finished cleaning up your dictation.",+ "cleanupDuplicated": "AI cleanup repeated the transcript. The original text was kept.","cleanupEmptyReply": "The AI model returned an empty reply instead of the cleaned-up dictation."},"translationFallback": {diff --git a/src/locales/es/translation.json b/src/locales/es/translation.jsonindex da0e21799..bcf7e1778 100644--- a/src/locales/es/translation.json+++ b/src/locales/es/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "Este proveedor solo admite transcripción en directo. Elige otro proveedor para subir archivos y reintentos.","providerKeyMissing": "No hay ninguna clave de API configurada para tu proveedor de transcripción. Añádela en Ajustes → Voz a texto.","cleanupTruncated": "El modelo de IA se quedó sin espacio antes de terminar de limpiar tu dictado.",+ "cleanupDuplicated": "La limpieza con IA repitió la transcripción. Se conservó el texto original.","cleanupEmptyReply": "El modelo de IA devolvió una respuesta vacía en lugar del dictado limpio."},"translationFallback": {diff --git a/src/locales/fr/translation.json b/src/locales/fr/translation.jsonindex 9d5f91ac2..6b802c424 100644--- a/src/locales/fr/translation.json+++ b/src/locales/fr/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "Ce fournisseur ne prend en charge que la transcription en direct. Choisissez un autre fournisseur pour les imports de fichiers et les nouvelles tentatives.","providerKeyMissing": "Aucune clé API n'est définie pour votre fournisseur de transcription. Ajoutez-la dans Réglages → Parole en texte.","cleanupTruncated": "Le modèle d'IA a manqué de place avant de terminer le nettoyage de votre dictée.",+ "cleanupDuplicated": "Le nettoyage par IA a répété la transcription. Le texte original a été conservé.","cleanupEmptyReply": "Le modèle d'IA a renvoyé une réponse vide au lieu de la dictée nettoyée."},"translationFallback": {diff --git a/src/locales/it/translation.json b/src/locales/it/translation.jsonindex 7e844effe..2534d2093 100644--- a/src/locales/it/translation.json+++ b/src/locales/it/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "Questo provider supporta solo la trascrizione dal vivo. Scegli un altro provider per i caricamenti di file e i nuovi tentativi.","providerKeyMissing": "Nessuna chiave API impostata per il tuo provider di trascrizione. Aggiungila in Impostazioni → Da voce a testo.","cleanupTruncated": "Il modello IA ha esaurito lo spazio prima di finire la pulizia della dettatura.",+ "cleanupDuplicated": "La pulizia con IA ha ripetuto la trascrizione. Il testo originale è stato conservato.","cleanupEmptyReply": "Il modello IA ha restituito una risposta vuota invece della dettatura pulita."},"translationFallback": {diff --git a/src/locales/ja/translation.json b/src/locales/ja/translation.jsonindex ceb0f8432..6fc47fef0 100644--- a/src/locales/ja/translation.json+++ b/src/locales/ja/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "このプロバイダーはライブ文字起こしのみに対応しています。ファイルのアップロードや再実行には別のプロバイダーを選択してください。","providerKeyMissing": "文字起こしプロバイダーのAPIキーが設定されていません。設定 → 音声テキスト変換 から追加してください。","cleanupTruncated": "AI モデルの出力が上限に達し、ディクテーションのクリーンアップが完了しませんでした。",+ "cleanupDuplicated": "AI クリーンアップが文字起こしを繰り返しました。元のテキストを保持しました。","cleanupEmptyReply": "AI モデルが空の応答を返したため、クリーンアップされたテキストがありません。"},"translationFallback": {diff --git a/src/locales/pt/translation.json b/src/locales/pt/translation.jsonindex acf3b2c4d..7c3f2eb8e 100644--- a/src/locales/pt/translation.json+++ b/src/locales/pt/translation.json@@ -899,6 +899,7 @@"streamingOnlyProvider": "Este provedor só suporta transcrição ao vivo. Escolha outro provedor para uploads de arquivos e novas tentativas.","providerKeyMissing": "Nenhuma chave de API definida para seu provedor de transcrição. Adicione-a em Configurações → Fala para texto.","cleanupTruncated": "O modelo de IA ficou sem espaço antes de terminar a limpeza do seu ditado.",+ "cleanupDuplicated": "A limpeza por IA repetiu a transcrição. O texto original foi mantido.","cleanupEmptyReply": "O modelo de IA devolveu uma resposta vazia em vez do ditado limpo."},"translationFallback": {diff --git a/src/locales/ru/translation.json b/src/locales/ru/translation.jsonindex 421818a97..f8d81b5ab 100644--- a/src/locales/ru/translation.json+++ b/src/locales/ru/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "Этот провайдер поддерживает только транскрипцию в реальном времени. Для загрузки файлов и повторных попыток выберите другого провайдера.","providerKeyMissing": "Для вашего провайдера транскрипции не задан API-ключ. Добавьте его в Настройках → Речь в текст.","cleanupTruncated": "У ИИ-модели закончилось место, и она не завершила очистку диктовки.",+ "cleanupDuplicated": "При обработке ИИ повторил расшифровку. Исходный текст сохранён.","cleanupEmptyReply": "ИИ-модель вернула пустой ответ вместо очищенной диктовки."},"translationFallback": {diff --git a/src/locales/zh-CN/translation.json b/src/locales/zh-CN/translation.jsonindex 936a69c0b..e2954a673 100644--- a/src/locales/zh-CN/translation.json+++ b/src/locales/zh-CN/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "该服务商仅支持实时转录。请为文件上传和重试选择其他服务商。","providerKeyMissing": "尚未为您的转录服务商设置 API 密钥。请在“设置 → 语音转文字”中添加。","cleanupTruncated": "AI 模型的输出长度达到上限,未能完成听写清理。",+ "cleanupDuplicated": "AI 清理重复了转录内容。已保留原始文本。","cleanupEmptyReply": "AI 模型返回了空响应,没有生成清理后的文本。"},"translationFallback": {diff --git a/src/locales/zh-TW/translation.json b/src/locales/zh-TW/translation.jsonindex 53ce5b758..fee4c7ace 100644--- a/src/locales/zh-TW/translation.json+++ b/src/locales/zh-TW/translation.json@@ -920,6 +920,7 @@"streamingOnlyProvider": "此服務商僅支援即時轉錄。請為檔案上傳與重試選擇其他服務商。","providerKeyMissing": "尚未為您的轉錄服務商設定 API 金鑰。請在「設定 → 語音轉文字」中新增。","cleanupTruncated": "AI 模型的輸出長度達到上限,未能完成聽寫清理。",+ "cleanupDuplicated": "AI 清理重複了轉錄內容。已保留原始文字。","cleanupEmptyReply": "AI 模型傳回空白回應,沒有產生清理後的文字。"},"translationFallback": {diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.tsindex 5746733f1..e3bb5428c 100644--- a/src/services/ReasoningService.ts+++ b/src/services/ReasoningService.ts@@ -11,6 +11,7 @@ import { SecureCache } from "../utils/SecureCache";import { withRetry, createApiRetryStrategy, httpError } from "../utils/retry";import { API_ENDPOINTS, TOKEN_LIMITS, buildApiUrl, ensureV1Suffix } from "../config/constants";import logger from "../utils/logger";+import { assertValidCleanupOutput } from "../utils/cleanupOutput";import { getSettings, isCloudCleanupMode } from "../stores/settingsStore";import { wrapCleanupTranscript } from "../config/prompts";import { stripThinkingTags } from "../helpers/stripThinking.js";@@ -461,6 +462,11 @@ class ReasoningService extends BaseReasoningService {({ model, config } = managed);const trimmedModel = model?.trim?.() || "";const settings = getSettings();+ const validateCleanup =+ config.inferenceScope === "dictationCleanup" &&+ !config.systemPrompt &&+ !config.requiresAgent &&+ !settings.customPrompts.cleanup;const isImplicitCleanup =config.provider === undefined && config.baseUrl === undefined && config.lanUrl === undefined;const implicitProvider =@@ -518,6 +524,8 @@ class ReasoningService extends BaseReasoningService {ctx: this.providerContext,});+ if (validateCleanup) assertValidCleanupOutput(text, result);+logger.logReasoning("PROVIDER_SUCCESS", {provider: providerId,model: trimmedModel,diff --git a/src/utils/cleanupOutput.ts b/src/utils/cleanupOutput.tsnew file mode 100644index 000000000..f593c3ffa--- /dev/null+++ b/src/utils/cleanupOutput.ts@@ -0,0 +1,59 @@+import logger from "./logger";++const wordSegmenter = new Intl.Segmenter("und", { granularity: "word" });+const cleanupLabel =+ /^(?:Cleaned transcript:|\*\*Cleaned transcript:\*\*|\*\*Cleaned transcript\*\*:)$/i;++function comparisonTokens(text: string): string[] {+ // Keep contractions as one word when punctuation is discarded.+ const normalized = text+ .normalize("NFKC")+ .toLowerCase()+ .replace(/['’]/gu, "")+ .replace(/\p{P}/gu, " ");+ return Array.from(wordSegmenter.segment(normalized))+ .filter((segment) => segment.isWordLike)+ .map((segment) => segment.segment);+}++export function assertValidCleanupOutput(rawText: string, output: string): void {+ // Collapse stutters ("the the") so they don't read as saying a word twice.+ const originalTokens = comparisonTokens(rawText).filter(+ (token, index, all) => token !== all[index - 1]+ );+ const tokens: string[] = [];+ for (const line of output.split(/\r?\n/u)) {+ if (cleanupLabel.test(line.trim())) continue;+ for (const token of comparisonTokens(line)) tokens.push(token);+ }++ const halfLength = tokens.length / 2;+ if (!Number.isInteger(halfLength) || halfLength < 6) return;+ const copy = tokens.slice(0, halfLength);+ if (!copy.every((token, index) => token === tokens[index + halfLength])) return;+ // A speaker who really said it twice said most of its words twice: each word+ // in the copy uses up two of its occurrences in the raw transcript.+ const rawCounts = new Map<string, number>();+ for (const token of originalTokens) rawCounts.set(token, (rawCounts.get(token) ?? 0) + 1);+ let saidTwice = 0;+ for (const token of copy) {+ const remaining = rawCounts.get(token) ?? 0;+ if (remaining < 2) continue;+ rawCounts.set(token, remaining - 2);+ saidTwice++;+ }+ if (saidTwice * 2 > halfLength) return;++ logger.logReasoning("CLEANUP_OUTPUT_REJECTED", {+ reason: "duplicated_transcript",+ inputLength: rawText.length,+ outputLength: output.length,+ });+ throw Object.assign(+ new Error("AI cleanup repeated the transcript. The original text was kept."),+ {+ code: "CLEANUP_OUTPUT_INVALID",+ messageKey: "hooks.audioRecording.errorDescriptions.cleanupDuplicated",+ }+ );+}diff --git a/test/helpers/audioManagerCleanupOutput.test.js b/test/helpers/audioManagerCleanupOutput.test.jsnew file mode 100644index 000000000..72626b5f3--- /dev/null+++ b/test/helpers/audioManagerCleanupOutput.test.js@@ -0,0 +1,148 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");++const RAW = "um so can you uh send me the report by friday";+const CLEAN = "Can you send me the report by Friday?";+const DUPLICATE = `**Cleaned transcript:**\n${CLEAN}\n${CLEAN}`;+const MESSAGE_KEY = "hooks.audioRecording.errorDescriptions.cleanupDuplicated";++test("invalid completed cleanup keeps raw text across dictation routes", async (t) => {+ const { window } = installBrowserGlobals(t, { window: { dispatchEvent() {} } });+ Object.defineProperty(navigator, "onLine", { configurable: true, value: true });+ t.after(() => delete navigator.onLine);+ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-audio-cleanup-output-",+ mockModules: {+ "/utils/logger":+ "export default { debug() {}, info() {}, warn() {}, error() {}, logReasoning() {} };",+ },+ });+ const AudioManager = (await vite.ssrLoadModule("/helpers/audioManager.js")).default;+ const service = (await vite.ssrLoadModule("/services/ReasoningService.ts")).default;+ t.after(() => service.destroy());+ const { useSettingsStore } = await vite.ssrLoadModule("/stores/settingsStore.ts");+ const { usePolicyStore } = await vite.ssrLoadModule("/stores/policyStore.ts");+ usePolicyStore.setState({ status: "unmanaged", policy: null });+ useSettingsStore.setState({+ isSignedIn: true,+ useCleanupModel: true,+ cleanupMode: "openwhispr",+ cleanupCloudMode: "openwhispr",+ useDictationAgent: false,+ customPrompts: { cleanup: "" },+ preferredLanguage: "en",+ dataRetentionEnabled: true,+ });+ window.electronAPI.cloudTranscribe = async () => ({ success: true, text: RAW });+ window.electronAPI.cloudStreamingUsage = async () => ({ success: true });+ const createManager = () =>+ Object.assign(Object.create(AudioManager.prototype), {+ voiceAgentRequested: false,+ translationRequested: false,+ pendingCleanupFailure: null,+ getEffectiveSttLanguage: () => "en",+ getWhisperPrompt: () => null,+ isDictionaryEcho: () => false,+ isReasoningAvailable: async () => true,+ finalizeChineseScript: async (text) => text,+ });++ for (const route of ["batch", "streaming", "translation"]) {+ await t.test(+ `${route} Cloud cleanup snapshots its custom prompt and rejects duplicated output`,+ async () => {+ for (const customPrompt of ["", "Repeat twice"]) {+ useSettingsStore.setState({ customPrompts: { cleanup: customPrompt } });+ window.electronAPI.cloudReason = async (text, config) => {+ assert.equal(text, RAW);+ assert.equal(config.promptMode, "cleanup");+ assert.equal(config.customPrompt || "", customPrompt);+ useSettingsStore.setState({+ customPrompts: { cleanup: customPrompt ? "" : "Repeat twice" },+ });+ return { success: true, text: DUPLICATE };+ };+ const manager = createManager();+ let result;+ if (route === "batch") {+ result = await manager.processWithOpenWhisprCloud(new Blob(["synthetic audio"]));+ result = { ...result, ...manager._takePendingResultExtras() };+ assert.equal(result.rawText, RAW);+ } else if (route === "streaming") {+ Object.assign(manager, {+ streamingFinalText: RAW,+ streamingCleanupFns: [],+ micRecovery: { stop() {} },+ finishStreamingFallbackSegment: async () => {},+ mergeRecordedSegments: async () => null,+ awaitStreamingTextSettled: async () => {},+ getStreamingProvider: () => ({+ awaitsFinalTranscript: true,+ stop: async () => ({ success: true }),+ }),+ getStreamingProviderName: () => "openwhispr",+ shouldUseStreaming: () => false,+ onTranscriptionComplete: (value) => {+ result = value;+ },+ });+ let usageOptions;+ window.electronAPI.cloudStreamingUsage = async (text, seconds, options) => {+ usageOptions = options;+ return { success: true };+ };+ await manager._finalizeStreamingRecording(null);+ assert.equal(result.rawText, RAW);+ await new Promise(setImmediate);+ // The successful /api/reason call already logged this dictation.+ assert.equal(usageOptions?.sendLogs, false);+ } else {+ let translationInput;+ manager.processWithReasoningModel = async (text) => {+ translationInput = text;+ return "Traduction du texte original.";+ };+ result = await manager.runTranslationChain({+ text: RAW,+ settings: { translationSourceLanguage: "en", translationTargetLanguage: "fr" },+ route: {+ cleanupReachable: true,+ model: "translate-model",+ config: { systemPrompt: "Translate" },+ },+ cleanup: { mode: "cloudReason" },+ });+ assert.equal(translationInput, customPrompt ? DUPLICATE : RAW);+ assert.equal(result.text, "Traduction du texte original.");+ result = { ...result, ...manager._takePendingResultExtras() };+ }+ if (route !== "translation") assert.equal(result.text, customPrompt ? DUPLICATE : RAW);+ assert.equal(result.cleanupFailure?.messageKey, customPrompt ? undefined : MESSAGE_KEY);+ }+ }+ );+ }++ await t.test("local cleanup failure flows through raw fallback and persistence", async () => {+ useSettingsStore.setState({+ cleanupMode: "local",+ cleanupProvider: "local",+ cleanupModel: "test-model",+ customPrompts: { cleanup: "" },+ });+ window.electronAPI.processLocalReasoning = async () => ({ success: true, text: DUPLICATE });+ const manager = createManager();+ const result = await manager.processTranscriptionCore(RAW, "local");+ assert.equal(result, RAW);+ assert.equal(manager._takePendingResultExtras().cleanupFailure.messageKey, MESSAGE_KEY);+ let saved;+ window.electronAPI.recordAnalyticsEvent = async () => {};+ window.electronAPI.saveTranscription = async (text, rawText) => {+ saved = { text, rawText };+ return { success: true };+ };+ await manager.saveTranscription(result, RAW);+ assert.deepEqual(saved, { text: RAW, rawText: RAW });+ });+});diff --git a/test/helpers/translationChain.test.js b/test/helpers/translationChain.test.jsindex 31b83c654..dd2decc7d 100644--- a/test/helpers/translationChain.test.js+++ b/test/helpers/translationChain.test.js@@ -279,6 +279,23 @@ test("usedCloudReasoning: cloud cleanup that throws does not set it", async () =assert.equal(result.usedCloudReasoning, false);});+test("usedCloudReasoning: cloud cleanup rejected after a successful call sets it true", async () => {+ const { executeTranslationChain } = await load();++ const result = await executeTranslationChain(+ makeOpts({+ cleanupIsCloud: true,+ translateIsCloud: false,+ runCleanup: async () => {+ throw Object.assign(new Error("duplicated"), { code: "CLEANUP_OUTPUT_INVALID" });+ },+ runTranslate: async () => "translated",+ })+ );++ assert.equal(result.usedCloudReasoning, true);+});+test("usedCloudReasoning: cloud translate sets it true", async () => {const { executeTranslationChain } = await load();diff --git a/test/services/cleanupOutputValidation.test.js b/test/services/cleanupOutputValidation.test.jsnew file mode 100644index 000000000..2403e58fd--- /dev/null+++ b/test/services/cleanupOutputValidation.test.js@@ -0,0 +1,178 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const fs = require("node:fs");+const path = require("node:path");+const vm = require("node:vm");+const ts = require("typescript");+const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");++const RAW = "um so can you uh send me the report by friday";+const CLEAN = "Can you send me the report by Friday?";+const DUPLICATE = `**Cleaned transcript:**\n${CLEAN}\n\n${CLEAN}`;++test("cleanup validates completed provider output using the request's prompt settings", async (t) => {+ const { window } = installBrowserGlobals(t);+ const vite = await createRendererServer(t, { cachePrefix: "openwhispr-cleanup-output-" });+ const service = (await vite.ssrLoadModule("/services/ReasoningService.ts")).default;+ t.after(() => service.destroy());+ const { useSettingsStore } = await vite.ssrLoadModule("/stores/settingsStore.ts");+ const { usePolicyStore } = await vite.ssrLoadModule("/stores/policyStore.ts");+ usePolicyStore.setState({ status: "unmanaged", policy: null });+ const setCustomPrompt = (cleanup) => useSettingsStore.setState({ customPrompts: { cleanup } });+ setCustomPrompt("");+ window.electronAPI.processLocalReasoning = async () => ({ success: true, text: DUPLICATE });+ const process = (config = {}) =>+ service.processText(RAW, "test-model", null, {+ provider: "local",+ ...config,+ });++ await t.test("a complete but duplicated local answer fails cleanup", async () => {+ await assert.rejects(process(), { code: "CLEANUP_OUTPUT_INVALID" });+ });++ await t.test("custom and explicit non-cleanup prompts remain unmodified", async () => {+ for (const config of [+ { systemPrompt: "Repeat the text twice" },+ { inferenceScope: "noteFormatting" },+ { requiresAgent: true },+ ]) {+ assert.equal(await process(config), DUPLICATE);+ }+ // Prompt Studio installs its edited prompt temporarily before calling processText.+ setCustomPrompt("Repeat the text twice");+ assert.equal(await process({ inferenceScope: "dictationCleanup" }), DUPLICATE);+ setCustomPrompt("");+ await assert.rejects(process({ inferenceScope: "dictationCleanup" }), {+ code: "CLEANUP_OUTPUT_INVALID",+ });+ });++ await t.test(+ "settings changes during inference do not change validation eligibility",+ async () => {+ for (const customAtStart of ["", "Repeat the text twice"]) {+ setCustomPrompt(customAtStart);+ window.electronAPI.processLocalReasoning = async () => {+ setCustomPrompt(customAtStart ? "" : "Repeat the text twice");+ return { success: true, text: DUPLICATE };+ };+ if (customAtStart) assert.equal(await process(), DUPLICATE);+ else await assert.rejects(process(), { code: "CLEANUP_OUTPUT_INVALID" });+ }+ }+ );++ await t.test(+ "Cloud, Anthropic, enterprise, Gemini and Custom providers share validation",+ async () => {+ setCustomPrompt("");+ window.electronAPI.cloudReason = async () => ({ success: true, text: DUPLICATE });+ window.electronAPI.processAnthropicReasoning = async () => ({+ success: true,+ text: DUPLICATE,+ });+ window.electronAPI.processEnterpriseReasoning = async () => ({+ success: true,+ text: DUPLICATE,+ });+ window.electronAPI.getGeminiKey = async () => "synthetic-key";+ const originalFetch = globalThis.fetch;+ t.after(() => {+ globalThis.fetch = originalFetch;+ });+ globalThis.fetch = async () =>+ new Response(+ JSON.stringify({+ choices: [{ message: { content: DUPLICATE }, finish_reason: "stop" }],+ candidates: [{ content: { parts: [{ text: DUPLICATE }] }, finishReason: "STOP" }],+ }),+ { headers: { "Content-Type": "application/json" } }+ );+ for (const config of [+ { provider: "openwhispr" },+ { provider: "anthropic" },+ { provider: "bedrock" },+ { provider: "gemini" },+ {+ provider: "custom",+ baseUrl: "https://cleanup.example.test/v1/chat/completions",+ customApiKey: "synthetic-key",+ },+ ]) {+ await assert.rejects(process(config), { code: "CLEANUP_OUTPUT_INVALID" }, config.provider);+ }+ }+ );++ await t.test("history retry keeps the raw row and reports rejected cleanup", async () => {+ setCustomPrompt("");+ useSettingsStore.setState({+ cleanupMode: "local",+ cleanupProvider: "local",+ cleanupModel: "test-model",+ });+ window.electronAPI.processLocalReasoning = async () => ({ success: true, text: DUPLICATE });+ const row = { id: 123, text: RAW, raw_text: RAW };+ window.electronAPI.retryTranscription = async () => ({ success: true, transcription: row });+ window.electronAPI.updateTranscriptionText = async () =>+ assert.fail("rejected cleanup must not overwrite the row");+ const source = fs.readFileSync(+ path.resolve(__dirname, "../../src/components/ControlPanel.tsx"),+ "utf8"+ );+ const parsed = ts.createSourceFile(+ "ControlPanel.tsx",+ source,+ ts.ScriptTarget.Latest,+ true,+ ts.ScriptKind.TSX+ );+ let callback;+ const visit = (node) => {+ if (ts.isVariableDeclaration(node) && node.name.getText(parsed) === "retryTranscription") {+ callback = node.initializer.arguments[0].getText(parsed);+ }+ ts.forEachChild(node, visit);+ };+ visit(parsed);+ assert.ok(callback, "the history retry callback must be present");+ // Execute the real callback without mounting the whole application shell.+ const code = ts.transpileModule(`const retry = ${callback};`, {+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },+ }).outputText;+ const settingsModule = await vite.ssrLoadModule("/stores/settingsStore.ts");+ const toasts = [];+ let displayedRow;+ const retry = vm.runInNewContext(`${code}\nretry`, {+ require: (id) => {+ if (id === "../services/ReasoningService") return { __esModule: true, default: service };+ if (id === "../stores/settingsStore") return settingsModule;+ throw new Error(`Unexpected history retry import: ${id}`);+ },+ window,+ getSettings: settingsModule.getSettings,+ getManagedTranscriptionResolution: () => null,+ isTranscriptionContextAllowed: () => true,+ usePolicyStore,+ useCleanupModel: true,+ getAgentName: () => "OpenWhispr",+ hasTextContent: (text) => typeof text === "string" && text.trim().length > 0,+ applyChineseScript: async (text) => text,+ resolveChineseScriptTarget: () => null,+ updateInStore: (value) => {+ displayedRow = value;+ },+ toast: (value) => toasts.push(value),+ t: (key) => key,+ });+ await retry(row.id);+ assert.strictEqual(displayedRow, row);+ assert.ok(+ toasts.some(+ (toast) => toast.description === "hooks.audioRecording.errorDescriptions.cleanupDuplicated"+ ),+ JSON.stringify(toasts)+ );+ });+});diff --git a/test/utils/cleanupOutput.test.js b/test/utils/cleanupOutput.test.jsnew file mode 100644index 000000000..2385880b3--- /dev/null+++ b/test/utils/cleanupOutput.test.js@@ -0,0 +1,86 @@+const test = require("node:test");+const assert = require("node:assert/strict");++const RAW = "um so can you uh send me the report by friday";+const CLEAN = "Can you send me the report by Friday?";+const LONG_RAW =+ "the team wants to ship the release next week but the team also wants the tests to pass first so the release might slip to the week after";+const LONG_CLEAN =+ "The team wants to ship the release next week, but the team also wants the tests to pass first, so the release might slip to the week after.";++// #2225: these synthetic cases cover duplication. CUS-227's original raw/final+// pair and configuration are still missing, so example substitution is unverified.+test("cleanup rejects whole-output duplication without requiring a match to raw speech", async () => {+ const { assertValidCleanupOutput } = await import("../../src/utils/cleanupOutput.ts");+ for (const output of [+ `${CLEAN} ${CLEAN}`,+ `Cleaned transcript:\n${CLEAN}\n\n${CLEAN}`,+ `**Cleaned transcript:**\n${CLEAN}\n**Cleaned transcript:**\n${CLEAN}`,+ `**Cleaned transcript**:\n${CLEAN}\n${CLEAN}`,+ `Can you send me\nCleaned transcript:\nthe report by Friday? ${CLEAN}`,+ `${CLEAN}\n${CLEAN}\nCleaned transcript:`,+ "CAN YOU SEND ME THE REPORT BY FRIDAY!\ncan you send me the report by Friday?",+ "Can you send me the report by Friday?\nCan you send me the report by Friday?",+ "Please send the updated report tomorrow. Please send the updated report tomorrow.",+ "请你明天把修改后的报告发送给项目负责人。请你明天把修改后的报告发送给项目负责人。",+ "Envoyez le rapport complet à Marie demain. Envoyez le rapport complet à Marie demain.",+ ]) {+ assert.throws(+ () => assertValidCleanupOutput(RAW, output),+ {+ code: "CLEANUP_OUTPUT_INVALID",+ messageKey: "hooks.audioRecording.errorDescriptions.cleanupDuplicated",+ },+ output+ );+ }+ // Filler-heavy speech can be as long as its cleanup said twice, and longer or+ // stuttered speech repeats words without being said twice.+ for (const [raw, output] of [+ ["um so uh basically can you uh like send me the report by friday um yeah", CLEAN],+ [LONG_RAW, LONG_CLEAN],+ [+ "I I I think we we should uh we should move the the meeting to to friday because because the the client is is out on on thursday",+ "I think we should move the meeting to Friday because the client is out on Thursday.",+ ],+ ]) {+ assert.throws(() => assertValidCleanupOutput(raw, `${output} ${output}`), {+ code: "CLEANUP_OUTPUT_INVALID",+ });+ }+});++test("cleanup leaves legitimate, ambiguous, and out-of-scope output alone", async () => {+ const { assertValidCleanupOutput } = await import("../../src/utils/cleanupOutput.ts");+ for (const [raw, output] of [+ [RAW, CLEAN],+ [RAW, "Please send the report tomorrow. Please send the report tomorrow."],+ [RAW, "I'm sorry, I can't. I'm sorry, I can't."],+ [RAW, `${CLEAN} Can you send me the report by Monday?`],+ [RAW, `Introduction. ${CLEAN} ${CLEAN}`],+ [RAW, `${CLEAN} ${CLEAN} Additional details.`],+ [RAW, `**Cleaned transcript:**\n${CLEAN}`],+ [RAW, `**${CLEAN}**`],+ [RAW, "What's the capital of France?"],+ [RAW, ""],+ [RAW, " \n "],+ [`${CLEAN} ${CLEAN}`, `${CLEAN.toUpperCase()}\n${CLEAN}`],+ ["Send it Thursday no wait Friday", "Send it Friday."],+ ["déjà vu élève", "Déjà vu, élève."],+ [+ "um please send the report by friday please send the report by friday",+ "Please send the report by Friday. Please send the report by Friday.",+ ],+ [+ "im gonna send the report to marie tomorrow im gonna send the report to marie tomorrow",+ "I'm going to send the report to Marie tomorrow. I'm going to send the report to Marie tomorrow.",+ ],+ [`${LONG_RAW} ${LONG_RAW}`, `${LONG_CLEAN} ${LONG_CLEAN}`],+ [+ "were gonna ship it on friday for sure um were gonna ship it on friday for sure",+ "We're going to ship it on Friday for sure. We're going to ship it on Friday for sure.",+ ],+ ]) {+ assert.doesNotThrow(() => assertValidCleanupOutput(raw, output), output);+ }+});