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(clipboard): preserve dictation history when clipboard delivery fails (#1979)
Joshuaf03e027e9104
diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.jsindex 9a22c99b3..359b4178a 100644--- a/src/hooks/useAudioRecording.js+++ b/src/hooks/useAudioRecording.js@@ -535,6 +535,53 @@ export const useAudioRecording = (toast, options = {}) => {const isStreaming = result.source?.includes("streaming");const { autoPasteEnabled, keepTranscriptionInClipboard } = getSettings();+ const persistencePromise = audioManagerRef.current+ .saveTranscription(result.text, result.rawText ?? result.text, {+ clientTranscriptionId: result.clientTranscriptionId,+ })+ .then(+ (persisted) => {+ if (!persisted) {+ logger.error(+ "Failed to persist transcription",+ {+ clientTranscriptionId: result.clientTranscriptionId,+ source: result.source,+ },+ "audio"+ );+ }+ return persisted;+ },+ (error) => {+ logger.error(+ "Failed to persist transcription",+ {+ clientTranscriptionId: result.clientTranscriptionId,+ error: error?.message,+ source: result.source,+ },+ "audio"+ );+ return false;+ }+ );++ const keepInClipboard = async (delivery) => {+ try {+ const clipboardResult = await window.electronAPI.writeClipboard(result.text);+ if (clipboardResult?.success === false) {+ throw new Error("clipboard-write-failed");+ }+ } catch (error) {+ logger.warn(+ "Failed to keep transcription in clipboard",+ { delivery, error: error?.message },+ "clipboard"+ );+ }+ };+if (autoPasteEnabled && !result.assistantConversation) {const pasteStart = performance.now();let pasteSucceeded = true;@@ -551,7 +598,7 @@ export const useAudioRecording = (toast, options = {}) => {if (!pasteSucceeded) {window.electronAPI?.hideDictationPreview?.();if (keepTranscriptionInClipboard) {- await navigator.clipboard.writeText(result.text);+ await keepInClipboard("selection-edit-fallback");}const detailKey =SELECTION_EDIT_DETAIL_KEY_BY_CODE[replacement?.code] || "unavailable";@@ -588,13 +635,9 @@ export const useAudioRecording = (toast, options = {}) => {if (result.cleanupFailure) recordCleanupFailure(result.cleanupFailure);}} else if (keepTranscriptionInClipboard && !result.assistantConversation) {- await navigator.clipboard.writeText(result.text);+ await keepInClipboard("clipboard-only");}- audioManagerRef.current.saveTranscription(result.text, result.rawText ?? result.text, {- clientTranscriptionId: result.clientTranscriptionId,- });-if (result.source === "openai" && getSettings().useLocalWhisper) {toast({title: t("hooks.audioRecording.fallback.title"),@@ -618,6 +661,8 @@ export const useAudioRecording = (toast, options = {}) => {if (audioManagerRef.current.shouldUseStreaming()) {audioManagerRef.current.warmupStreamingConnection();}++ await persistencePromise;}},onTranslationFallback: ({ reason }) => {diff --git a/test/helpers/useAudioRecordingCleanupFallback.test.js b/test/helpers/useAudioRecordingCleanupFallback.test.jsindex e6d59d3e4..39702dae4 100644--- a/test/helpers/useAudioRecordingCleanupFallback.test.js+++ b/test/helpers/useAudioRecordingCleanupFallback.test.js@@ -24,7 +24,7 @@ export default class FakeAudioManager {this.pasteCalls += 1;return globalThis.__cleanupFallbackPasteOutcome?.pasted === true;}- saveTranscription() {}+ async saveTranscription() { return true; }cleanup() {}}`;diff --git a/test/helpers/useAudioRecordingClipboardPersistence.test.js b/test/helpers/useAudioRecordingClipboardPersistence.test.jsnew file mode 100644index 000000000..43df62ae0--- /dev/null+++ b/test/helpers/useAudioRecordingClipboardPersistence.test.js@@ -0,0 +1,327 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const React = require("react");+const { createRoot } = require("react-dom/client");+const {+ createRendererServer,+ installBrowserGlobals,+ installHookDom,+} = require("../lib/rendererTestHarness");++const FAKE_AUDIO_MANAGER_SOURCE = `+export default class FakeAudioManager {+ constructor() {+ this.voiceAgentRequested = false;+ this.translationRequested = false;+ globalThis.__clipboardPersistenceAudioManager = this;+ }+ getState() {+ return {};+ }+ setCallbacks(callbacks) {+ this.callbacks = callbacks;+ }+ complete(result) {+ return this.callbacks.onTranscriptionComplete(result);+ }+ saveTranscription(...args) {+ return globalThis.__saveClipboardPersistenceTranscription(...args);+ }+ shouldUseStreaming() {+ return false;+ }+ cleanup() {}+}+`;++const SETTINGS_STORE_SOURCE = `+export const getSettings = () => globalThis.__clipboardPersistenceSettings;+`;++const POLICY_STORE_SOURCE = `+export const usePolicyStore = {+ getState: () => ({}),+ subscribe: () => () => {},+};+`;++const LOGGER_SOURCE = `+const record = (level, message, meta, scope) => {+ globalThis.__clipboardPersistenceLogs.push({ level, message, meta, scope });+};+export default {+ trace: (message, meta, scope) => record("trace", message, meta, scope),+ debug: (message, meta, scope) => record("debug", message, meta, scope),+ info: (message, meta, scope) => record("info", message, meta, scope),+ warn: (message, meta, scope) => record("warn", message, meta, scope),+ error: (message, meta, scope) => record("error", message, meta, scope),+ fatal: (message, meta, scope) => record("fatal", message, meta, scope),+ logReasoning: (message, meta) => record("debug", message, meta, "reasoning"),+};+`;++const TRANSLATION_SOURCE = `+const translate = (key) => key;+export const useTranslation = () => ({ t: translate });+`;++const NOOP = () => {};++async function mountCompletionHarness(+ t,+ {+ settings,+ writeClipboard = async () => ({ success: true }),+ replaceSelectedText = async () => ({ success: true }),+ saveTranscription = async () => true,+ }+) {+ let root = null;+ t.after(async () => {+ if (root) await React.act(async () => root.unmount());+ });++ const navigatorWrites = [];+ const originalNavigator = Object.getOwnPropertyDescriptor(globalThis, "navigator");+ Object.defineProperty(globalThis, "navigator", {+ value: {+ clipboard: {+ async writeText(text) {+ navigatorWrites.push(text);+ throw new Error("renderer clipboard should not be used");+ },+ },+ },+ configurable: true,+ writable: true,+ });+ t.after(() => {+ if (originalNavigator) Object.defineProperty(globalThis, "navigator", originalNavigator);+ else delete globalThis.navigator;+ });++ const bridgeWrites = [];+ const saves = [];+ const noopDispose = () => () => {};+ installBrowserGlobals(t, {+ window: {+ electronAPI: {+ onToggleDictation: noopDispose,+ onToggleVoiceAgent: noopDispose,+ onToggleTranslation: noopDispose,+ onStartDictation: noopDispose,+ onPrepareDictation: noopDispose,+ onCancelDictationPreparation: noopDispose,+ onStopDictation: noopDispose,+ dictationLifecycleStateChanged: NOOP,+ completeDictationPreview: NOOP,+ hideDictationPreview: NOOP,+ setScreenContextEnabled: NOOP,+ async writeClipboard(text) {+ bridgeWrites.push(text);+ return writeClipboard(text);+ },+ replaceSelectedText,+ },+ },+ });+ const container = installHookDom(t);++ globalThis.__clipboardPersistenceSettings = {+ autoPasteEnabled: false,+ keepTranscriptionInClipboard: true,+ showTranscriptionPreview: false,+ snippets: [],+ useLocalWhisper: false,+ pauseMediaOnDictation: false,+ voiceAgentScreenContext: false,+ ...settings,+ };+ globalThis.__clipboardPersistenceLogs = [];+ globalThis.__saveClipboardPersistenceTranscription = async (...args) => {+ saves.push(args);+ return saveTranscription(...args);+ };++ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-audio-recording-clipboard-persistence-",+ noExternal: ["react-i18next"],+ mockModules: {+ "/helpers/audioManager": FAKE_AUDIO_MANAGER_SOURCE,+ "/stores/settingsStore": SETTINGS_STORE_SOURCE,+ "/stores/policyStore": POLICY_STORE_SOURCE,+ "/utils/logger": LOGGER_SOURCE,+ "react-i18next": TRANSLATION_SOURCE,+ },+ });+ const { useAudioRecording } = await vite.ssrLoadModule("/hooks/useAudioRecording.js");++ function Harness() {+ useAudioRecording(NOOP, { onDemoEvent: NOOP });+ return null;+ }++ root = createRoot(container);+ await React.act(async () => {+ root.render(React.createElement(Harness));+ });++ return {+ bridgeWrites,+ complete: async (result) => {+ await React.act(async () => {+ await globalThis.__clipboardPersistenceAudioManager.complete(result);+ });+ },+ logs: globalThis.__clipboardPersistenceLogs,+ navigatorWrites,+ saves,+ };+}++test("clipboard-only rejection cannot cancel non-preview transcription persistence", async (t) => {+ const harness = await mountCompletionHarness(t, {+ writeClipboard: async () => {+ throw new Error("main-process clipboard rejected");+ },+ });++ await harness.complete({+ success: true,+ text: "Final non-preview text",+ rawText: "Raw non-preview text",+ clientTranscriptionId: "client-non-preview",+ source: "openai",+ });++ assert.deepEqual(harness.saves, [+ [+ "Final non-preview text",+ "Raw non-preview text",+ { clientTranscriptionId: "client-non-preview" },+ ],+ ]);+ assert.ok(+ harness.logs.some(+ ({ level, message, meta, scope }) =>+ level === "warn" &&+ message === "Failed to keep transcription in clipboard" &&+ meta.delivery === "clipboard-only" &&+ scope === "clipboard"+ )+ );+ assert.deepEqual(harness.navigatorWrites, []);+});++test("clipboard-only unsuccessful bridge response is logged without cancelling persistence", async (t) => {+ const harness = await mountCompletionHarness(t, {+ writeClipboard: async () => ({ success: false }),+ });++ await harness.complete({+ success: true,+ text: "Text with an unsuccessful clipboard response",+ rawText: "Raw text with an unsuccessful clipboard response",+ clientTranscriptionId: "client-clipboard-unsuccessful",+ source: "openai",+ });++ assert.deepEqual(harness.saves, [+ [+ "Text with an unsuccessful clipboard response",+ "Raw text with an unsuccessful clipboard response",+ { clientTranscriptionId: "client-clipboard-unsuccessful" },+ ],+ ]);+ assert.ok(+ harness.logs.some(+ ({ level, message, meta, scope }) =>+ level === "warn" &&+ message === "Failed to keep transcription in clipboard" &&+ meta.delivery === "clipboard-only" &&+ scope === "clipboard"+ )+ );+ assert.deepEqual(harness.navigatorWrites, []);+});++test("clipboard-only delivery uses the main-process bridge for a streaming-capable result", async (t) => {+ const harness = await mountCompletionHarness(t, {+ settings: { showTranscriptionPreview: true },+ });++ await harness.complete({+ success: true,+ text: "Final streaming text",+ rawText: "Raw streaming text",+ clientTranscriptionId: "client-streaming",+ source: "deepgram-streaming",+ });++ assert.deepEqual(harness.bridgeWrites, ["Final streaming text"]);+ assert.deepEqual(harness.saves, [+ ["Final streaming text", "Raw streaming text", { clientTranscriptionId: "client-streaming" }],+ ]);+ assert.deepEqual(harness.navigatorWrites, []);+});++test("failed selection-edit clipboard rejection cannot cancel persistence", async (t) => {+ const harness = await mountCompletionHarness(t, {+ settings: { autoPasteEnabled: true },+ replaceSelectedText: async () => ({ success: false, code: "paste_failed" }),+ writeClipboard: async () => {+ throw new Error("main-process clipboard rejected");+ },+ });++ await harness.complete({+ success: true,+ text: "Edited selection text",+ rawText: "Raw selection instruction",+ clientTranscriptionId: "client-selection",+ source: "openai",+ selectionEdit: { sessionId: "selection-session" },+ });++ assert.deepEqual(harness.bridgeWrites, ["Edited selection text"]);+ assert.deepEqual(harness.saves, [+ [+ "Edited selection text",+ "Raw selection instruction",+ { clientTranscriptionId: "client-selection" },+ ],+ ]);+ assert.ok(+ harness.logs.some(+ ({ level, message, meta, scope }) =>+ level === "warn" &&+ message === "Failed to keep transcription in clipboard" &&+ meta.delivery === "selection-edit-fallback" &&+ scope === "clipboard"+ )+ );+ assert.deepEqual(harness.navigatorWrites, []);+});++test("a false persistence result is logged instead of silently ignored", async (t) => {+ const harness = await mountCompletionHarness(t, {+ saveTranscription: async () => false,+ });++ await harness.complete({+ success: true,+ text: "Text with a storage failure",+ rawText: "Raw text with a storage failure",+ clientTranscriptionId: "client-storage-failure",+ source: "openai",+ });++ assert.equal(harness.saves.length, 1);+ assert.ok(+ harness.logs.some(+ ({ level, message, scope }) =>+ level === "error" && message === "Failed to persist transcription" && scope === "audio"+ )+ );+ assert.deepEqual(harness.navigatorWrites, []);+});