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(assistant): don't start panel thinking when a streaming follow-up is cancelled (#2693)
Chad Pihab8cad16e9328
diff --git a/src/App.jsx b/src/App.jsxindex 5f0067e71..03036da29 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -7,7 +7,7 @@ import { formatHotkeyListLabel } from "./utils/hotkeys";import { useWindowDrag } from "./hooks/useWindowDrag";import { useLinuxPillInteractivity } from "./hooks/useLinuxPillInteractivity";import { useAudioRecording } from "./hooks/useAudioRecording";-import { useAssistantPanel } from "./hooks/useAssistantPanel";+import { useAssistantFollowUpThinking, useAssistantPanel } from "./hooks/useAssistantPanel";import { useOnboardingAssistantDemo } from "./hooks/useOnboardingAssistantDemo";import { useLiveTranscriptPanel } from "./hooks/useLiveTranscriptPanel";import { useMainWindowSizeOwner } from "./hooks/useMainWindowSizeOwner";@@ -207,18 +207,7 @@ export default function App() {}}, [assistantOpenRef, setWindowInteractivity]);- const {- isRecording,- isProcessing,- isAssistantVoice,- isPreparing,- isStopping,- micCaptureStatus,- toggleListening,- cancelRecording,- cancelProcessing,- getAudioLevel,- } = useAudioRecording(toast, {+ const recording = useAudioRecording(toast, {onToggle: handleDictationToggle,onDemoEvent: publishOnboardingDemoEvent,onAssistantCommand: assistant.handleCommand,@@ -237,6 +226,18 @@ export default function App() {},assistantOpenRef,});+ const {+ isRecording,+ isProcessing,+ isAssistantVoice,+ isPreparing,+ isStopping,+ micCaptureStatus,+ toggleListening,+ cancelRecording,+ cancelProcessing,+ getAudioLevel,+ } = recording;const isVisuallyProcessing = isProcessing || isPreparing || isStopping;useLayoutEffect(() => {@@ -281,12 +282,7 @@ export default function App() {);}, [mainWindowHorizontalDirection, panelStartPosition, voiceDirectionLocked]);- const { beginThinking: beginAssistantThinking } = assistant;- useEffect(() => {- if (isAssistantVoice && isProcessing && assistantOpenRef.current) {- beginAssistantThinking();- }- }, [isAssistantVoice, isProcessing, assistantOpenRef, beginAssistantThinking]);+ useAssistantFollowUpThinking(recording, assistant);// While the Agent panel is open, plain dictation renders on the// opposite-edge companion pill — neither its recording nor its processingdiff --git a/src/helpers/voicePillPresentation.js b/src/helpers/voicePillPresentation.jsindex 5b289289e..2ba7b2548 100644--- a/src/helpers/voicePillPresentation.js+++ b/src/helpers/voicePillPresentation.js@@ -423,3 +423,16 @@ export function resolveAssistantThinkingTransition(panelOpen) {thinking: true,};}++/**+ * A streaming cancel reports processing while it tears the stream down, but+ * nothing will answer it, so it must not start the open panel's thinking.+ */+export function shouldBeginAssistantFollowUpThinking({+ isAssistantVoice,+ isProcessing,+ isCancelling,+ panelOpen,+}) {+ return Boolean(isAssistantVoice && isProcessing && !isCancelling && panelOpen);+}diff --git a/src/hooks/useAssistantPanel.js b/src/hooks/useAssistantPanel.jsindex bd02fa840..5f02ea93c 100644--- a/src/hooks/useAssistantPanel.js+++ b/src/hooks/useAssistantPanel.js@@ -2,6 +2,7 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "reactimport {getAssistantFooterTransitionTimeline,resolveAssistantThinkingTransition,+ shouldBeginAssistantFollowUpThinking,} from "../helpers/voicePillPresentation";import {closeAssistantSessionState,@@ -338,3 +339,27 @@ export function useAssistantPanel({noteDictationError,};}++/**+ * Starts the open panel's thinking when a voice-assistant follow-up spoken into+ * it is processed. `recording` is the `useAudioRecording` result and+ * `assistant` the `useAssistantPanel` result.+ */+export function useAssistantFollowUpThinking(recording, assistant) {+ const { isAssistantVoice, isProcessing, cancellingRef } = recording;+ const { openRef, beginThinking } = assistant;+ // A layout effect reads the cancel flag in the processing commit itself; a+ // passive one runs after a paint, when the teardown may already have cleared it.+ useLayoutEffect(() => {+ if (+ shouldBeginAssistantFollowUpThinking({+ isAssistantVoice,+ isProcessing,+ isCancelling: cancellingRef.current,+ panelOpen: openRef.current,+ })+ ) {+ beginThinking();+ }+ }, [isAssistantVoice, isProcessing, cancellingRef, openRef, beginThinking]);+}diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.jsindex 57886cb1e..f0bf7083c 100644--- a/src/hooks/useAudioRecording.js+++ b/src/hooks/useAudioRecording.js@@ -70,6 +70,7 @@ export const useAudioRecording = (toast, options = {}) => {const dictationErrorGenerationRef = useRef(0);const wasRecordingRef = useRef(false);const pastesInFlightRef = useRef(0);+ const cancellingRef = useRef(false);const wasMicUnavailableRef = useRef(false);const demoKindRef = useRef("dictation");const onDemoEventRef = useRef(options.onDemoEvent);@@ -1113,7 +1114,16 @@ export const useAudioRecording = (toast, options = {}) => {// A streaming start in its mic-open phase is not yet `isStreaming`;// only the streaming cancel knows how to abandon it.if (state.isStreaming || state.isStreamingStartInProgress) {- return await audioManagerRef.current.cancelStreamingRecording();+ // The streaming cancel reports processing while it tears the stream+ // down; flagged first so the follow-up thinking effect doesn't read+ // that as a command. A ref, so clearing it can't re-run that effect+ // while a pending paste still reports processing.+ cancellingRef.current = true;+ try {+ return await audioManagerRef.current.cancelStreamingRecording();+ } finally {+ cancellingRef.current = false;+ }}return audioManagerRef.current.cancelRecording();}@@ -1171,6 +1181,7 @@ export const useAudioRecording = (toast, options = {}) => {isProcessing: isProcessing || (isPasting && !isRecording),isStreaming,isAssistantVoice,+ cancellingRef,isPreparing,isStopping,micCaptureStatus,diff --git a/test/helpers/useAudioRecordingStreamingCancel.test.js b/test/helpers/useAudioRecordingStreamingCancel.test.jsnew file mode 100644index 000000000..7d8437c0c--- /dev/null+++ b/test/helpers/useAudioRecordingStreamingCancel.test.js@@ -0,0 +1,350 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const React = require("react");+const { flushSync } = require("react-dom");+const { createRoot } = require("react-dom/client");+const {+ createRendererServer,+ installBrowserGlobals,+ installHookDom,+} = require("../lib/rendererTestHarness");++// A streaming voice-assistant recording in progress. Its stop and cancel+// report state the way the real AudioManager does: a stop stays processing+// until the transcript is handled, and a cancel reports a transient+// processing state while it tears the stream down, then idle even when the+// teardown fails.+const FAKE_AUDIO_MANAGER_SOURCE = `+export default class FakeAudioManager {+ constructor() {+ this.voiceAgentRequested = true;+ this.translationRequested = false;+ this.sttConfig = { success: true };+ this.callbacks = {};+ this.state = { isRecording: true, isStreaming: true };+ this.cancelError = null;+ globalThis.__streamingCancelAudioManager = this;+ }+ getState() {+ return this.state;+ }+ setCallbacks(callbacks) {+ this.callbacks = callbacks;+ }+ setVoiceAgentRequested(value) {+ this.voiceAgentRequested = value;+ }+ setAssistantSelectionContext() {}+ setTranslationRequested(value) {+ this.translationRequested = value;+ }+ shouldUseStreaming() {+ return true;+ }+ isSttConfigStale() {+ return false;+ }+ prepareMicCapture() {}+ cancelPreparedMicCapture() {}+ cleanup() {}+ getRecordingAudioLevel() {+ return null;+ }+ report(state) {+ this.callbacks.onStateChange?.(state);+ }+ warmupStreamingConnection() {}+ async saveTranscription() {+ return true;+ }+ // Held open the way a paste waiting on held modifier keys stays pending.+ safePaste() {+ return new Promise((resolve) => {+ this.finishPaste = () => resolve({ pasted: true });+ });+ }+ async stopStreamingRecording() {+ this.report({ isRecording: false, isProcessing: true, isStreaming: false });+ return true;+ }+ async cancelStreamingRecording() {+ this.report({ isRecording: false, isProcessing: true, isStreaming: false });+ try {+ // Held open the way a real provider disconnect keeps the cancel pending.+ await new Promise((resolve) => {+ this.finishCancel = resolve;+ });+ if (this.cancelError) throw this.cancelError;+ return true;+ } finally {+ this.state = { isRecording: false, isStreaming: false };+ this.report({ isRecording: false, isProcessing: false, isStreaming: false });+ }+ }+}+`;++// Renders the recording hook, the assistant panel and the follow-up wiring+// App.jsx uses, with the panel already open.+async function renderOpenPanelWithStreamingFollowUp(t) {+ // Registered before the DOM helpers so it runs while window/document exist.+ let root = null;+ t.after(async () => {+ if (root) await React.act(async () => root.unmount());+ });++ const noopDispose = () => () => {};+ installBrowserGlobals(t, {+ window: {+ electronAPI: {+ onToggleDictation: noopDispose,+ onToggleVoiceAgent: noopDispose,+ onToggleTranslation: noopDispose,+ onStartDictation: noopDispose,+ onPrepareDictation: noopDispose,+ onCancelDictationPreparation: noopDispose,+ onStopDictation: noopDispose,+ },+ },+ });+ const container = installHookDom(t);+ globalThis.requestAnimationFrame = (callback) => {+ callback();+ return 1;+ };+ t.after(() => {+ delete globalThis.__streamingCancelAudioManager;+ });++ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-audio-recording-streaming-cancel-",+ mockModules: {+ "/helpers/audioManager": FAKE_AUDIO_MANAGER_SOURCE,+ },+ });+ const { useAudioRecording } = await vite.ssrLoadModule("/hooks/useAudioRecording.js");+ const { useAssistantFollowUpThinking, useAssistantPanel } = await vite.ssrLoadModule(+ "/hooks/useAssistantPanel.js"+ );++ const api = {};+ const panel = {};+ // Stable, or every render would rebuild the AudioManager in the mount effect.+ const toast = () => {};+ const options = { onDemoEvent: () => {} };+ const panelOptions = {+ requestMainWindowSize: async () => {},+ dictationErrorActionCount: 0,+ recordingControlsRef: { current: null },+ };+ function Harness() {+ const assistant = useAssistantPanel(panelOptions);+ const recording = useAudioRecording(toast, options);+ useAssistantFollowUpThinking(recording, assistant);+ // As App.jsx does, so closing the panel cancels the follow-up.+ React.useLayoutEffect(() => {+ panelOptions.recordingControlsRef.current = recording;+ });+ Object.assign(api, recording);+ Object.assign(panel, assistant);+ return null;+ }++ root = createRoot(container);+ await React.act(async () => {+ root.render(React.createElement(Harness));+ });+ await React.act(async () => {+ await panel.openPanel();+ });+ const manager = globalThis.__streamingCancelAudioManager;+ await startFollowUp(manager);+ assert.equal(panel.openRef.current, true);+ assert.equal(api.isRecording, true);+ assert.equal(api.isAssistantVoice, true);+ assert.equal(panel.thinking, false);++ return { api, panel, manager };+}++async function startFollowUp(manager) {+ manager.state = { isRecording: true, isStreaming: true };+ await React.act(async () => {+ manager.report({ isRecording: true, isProcessing: false, isStreaming: true });+ });+}++test("cancelling a streaming voice-assistant follow-up never starts the open panel's thinking", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);++ let cancelled;+ await React.act(async () => {+ // A discrete event (Esc, the panel's close button) flushes its updates+ // before any microtask, so the cancel flag must land in the same batch as+ // the teardown's processing report.+ flushSync(() => {+ cancelled = api.cancelRecording();+ });+ });+ assert.equal(api.isProcessing, true);+ assert.equal(api.cancellingRef.current, true);+ assert.equal(panel.thinking, false);++ await React.act(async () => {+ manager.finishCancel();+ await cancelled;+ });++ assert.equal(panel.thinking, false);+ assert.equal(api.isProcessing, false);+ assert.equal(api.cancellingRef.current, false);+});++test("cancelling a streaming follow-up while its stream is still starting never starts thinking", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);+ manager.state = { isRecording: true, isStreaming: false, isStreamingStartInProgress: true };++ let cancelled;+ await React.act(async () => {+ cancelled = api.cancelRecording();+ });+ assert.equal(api.isProcessing, true);+ assert.equal(panel.thinking, false);++ await React.act(async () => {+ manager.finishCancel();+ await cancelled;+ });+ assert.equal(panel.thinking, false);+});++test("stopping a streaming voice-assistant follow-up still starts the open panel's thinking", async (t) => {+ const { api, panel } = await renderOpenPanelWithStreamingFollowUp(t);++ await React.act(async () => {+ await api.stopRecording();+ });++ assert.equal(panel.thinking, true);+ assert.equal(api.isProcessing, true);+ assert.equal(api.cancellingRef.current, false);+});++test("the next follow-up still thinks after a cancelled one", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);++ let cancelled;+ await React.act(async () => {+ cancelled = api.cancelRecording();+ });+ await React.act(async () => {+ manager.finishCancel();+ await cancelled;+ });++ await startFollowUp(manager);+ await React.act(async () => {+ await api.stopRecording();+ });++ assert.equal(panel.thinking, true);+});++test("a streaming cancel that fails still lets the next follow-up think", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);+ manager.cancelError = new Error("preview cleanup failed");++ let cancelled;+ await React.act(async () => {+ cancelled = api.cancelRecording();+ });+ await React.act(async () => {+ manager.finishCancel();+ await assert.rejects(cancelled, /preview cleanup failed/);+ });+ assert.equal(api.cancellingRef.current, false);+ assert.equal(panel.thinking, false);++ manager.cancelError = null;+ await startFollowUp(manager);+ await React.act(async () => {+ await api.stopRecording();+ });++ assert.equal(panel.thinking, true);+});++test("closing the panel mid follow-up cancels it and the panel stays closed", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);++ await React.act(async () => {+ flushSync(() => {+ panel.handleClose();+ });+ });+ assert.equal(api.isProcessing, true);+ assert.equal(panel.closing, true);+ assert.equal(panel.thinking, false);++ await React.act(async () => {+ manager.finishCancel();+ // Past the panel's content-fade fallback.+ await new Promise((resolve) => setTimeout(resolve, 300));+ });+ assert.equal(api.isProcessing, false);+ assert.equal(panel.openRef.current, false);+ assert.equal(panel.thinking, false);+});++test("a paste still in flight when a follow-up is cancelled never starts thinking", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);++ // The previous dictation's paste is still waiting when the follow-up is cancelled.+ let completed;+ await React.act(async () => {+ completed = manager.callbacks.onTranscriptionComplete({+ success: true,+ text: "earlier dictation",+ source: "deepgram-streaming",+ });+ });+ assert.equal(typeof manager.finishPaste, "function");++ let cancelled;+ await React.act(async () => {+ cancelled = api.cancelRecording();+ });+ await React.act(async () => {+ manager.finishCancel();+ await cancelled;+ });+ // The pending paste still reports processing once the cancel is done.+ assert.equal(api.isProcessing, true);+ assert.equal(panel.thinking, false);++ await React.act(async () => {+ manager.finishPaste();+ await completed;+ });+ assert.equal(api.isProcessing, false);+ assert.equal(panel.thinking, false);+});++test("a cancel whose teardown ends before the processing commit's passive effects never starts thinking", async (t) => {+ const { api, panel, manager } = await renderOpenPanelWithStreamingFollowUp(t);+ // Outside act, as the cancel hotkey's IPC runs: React yields to paint after+ // the processing commit, before its passive effects.+ globalThis.IS_REACT_ACT_ENVIRONMENT = false;++ const cancelled = api.cancelRecording();+ await Promise.resolve();+ await new Promise((resolve) => setImmediate(resolve));+ assert.equal(api.isProcessing, true);+ manager.finishCancel();+ await cancelled;+ await new Promise((resolve) => setTimeout(resolve, 20));+ globalThis.IS_REACT_ACT_ENVIRONMENT = true;++ assert.equal(api.isProcessing, false);+ assert.equal(panel.thinking, false);+});diff --git a/test/helpers/voicePillPresentation.test.js b/test/helpers/voicePillPresentation.test.jsindex 49aac7ac9..9ad8acf2b 100644--- a/test/helpers/voicePillPresentation.test.js+++ b/test/helpers/voicePillPresentation.test.js@@ -821,3 +821,24 @@ test("activity handed back at close intent stays visible through the content fadtrue);});++test("an open panel starts thinking only for a voice-assistant command being processed, never a cancel", async () => {+ const { shouldBeginAssistantFollowUpThinking } = await load();+ const followUp = {+ isAssistantVoice: true,+ isProcessing: true,+ isCancelling: false,+ panelOpen: true,+ };++ assert.equal(shouldBeginAssistantFollowUpThinking(followUp), true);+ // A streaming cancel reports processing while it tears the stream down;+ // nothing will ever answer it, so the panel must not start thinking.+ assert.equal(shouldBeginAssistantFollowUpThinking({ ...followUp, isCancelling: true }), false);+ assert.equal(shouldBeginAssistantFollowUpThinking({ ...followUp, isProcessing: false }), false);+ assert.equal(+ shouldBeginAssistantFollowUpThinking({ ...followUp, isAssistantVoice: false }),+ false+ );+ assert.equal(shouldBeginAssistantFollowUpThinking({ ...followUp, panelOpen: false }), false);+});