Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(voice): defer composer microphone preparation until long press (#7263)
DavidShenXD55e974dcd970
diff --git a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsxindex 87d9f057b2..0bf2083abb 100644--- a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx+++ b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx@@ -238,6 +238,8 @@ it('locks editor and attachments for the whole voice run, but keeps permissions// User request 2026-10-24: empty composer hints must follow hover/hold/click,// and native selection must not take over a recording. Existing voice tests// only cover editability; minimal inputs below drive real DOM handlers (unit/jsdom).+// 2026-10-26 Bluetooth regression: preparation must also wait for the hold+// threshold; even a microphone opened without recording changes headset audio.it('shows voice guidance only in an empty composer and distinguishes holding from clicking', async () => {const view = renderComposer();await waitFor(() => expect(h.editor).not.toBeNull());@@ -261,9 +263,13 @@ it('shows voice guidance only in an empty composer and distinguishes holding fro// mousedown path inert while the real composer handler still receives it.Object.defineProperty(document, 'elementFromPoint', { configurable: true, value: () => null });fireEvent.mouseDown(h.editor!.view.dom, { button: 0, detail: 1, clientX: 10, clientY: 10 });- expect(h.prepare).toHaveBeenCalledOnce();+ expect(h.prepare).not.toHaveBeenCalled();+ expect(h.listening).toBe(false);+ await act(async () => { await vi.advanceTimersByTimeAsync(599); });+ expect(h.prepare).not.toHaveBeenCalled();expect(h.listening).toBe(false);- await act(async () => { await vi.advanceTimersByTimeAsync(600); });+ await act(async () => { await vi.advanceTimersByTimeAsync(1); });+ expect(h.prepare).toHaveBeenCalledOnce();view.rerender(<ChatInput {...view.props} />);expect(screen.getByTestId('composer-voice-hint').textContent).toBe('newChat.chatInput.voiceInput.speakRelease');for (const type of ['selectstart', 'dragstart', 'mousemove']) {diff --git a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.tsindex 9ce80b1979..09401b5e0a 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts@@ -1,6 +1,10 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { prewarmVoiceInputAudio } from '../audioContextPool';+import {+ COMPOSER_LONG_PRESS_VOICE_INPUT_MS,+ createComposerLongPressVoiceGesture,+} from '../composerLongPressGesture';import {disposeKeepAliveVoiceInputMicrophone,isMicrophoneDeviceUnavailableError,@@ -115,6 +119,39 @@ describe('prewarmVoiceInputMicrophoneWithAutomaticFallback', () => {vi.restoreAllMocks();});+ // User report 2026-10-26: short clicks switch Bluetooth headphones into mic+ // mode. Existing preparation tests only checked eventual track cleanup.+ // Unit: run the real gesture + preparation with getUserMedia as the device+ // boundary; cancelling before the hold threshold must never open that device.+ it.each(['release', 'move', 'dispose'] as const)(+ 'does not open the microphone when a pending composer hold ends via %s',+ async (cancel) => {+ vi.useFakeTimers();+ const start = vi.fn();+ const gesture = createComposerLongPressVoiceGesture({+ getState: () => 'idle',+ prepare: () => prepareVoiceInputMicrophone({+ workletUrl: 'https://app.local/pcm16k-worklet.js',+ }),+ start,+ stop: vi.fn(),+ });+ try {+ gesture.press({ x: 0, y: 0 });+ await vi.advanceTimersByTimeAsync(COMPOSER_LONG_PRESS_VOICE_INPUT_MS - 1);+ expect(getUserMedia).not.toHaveBeenCalled();+ if (cancel === 'move') gesture.move({ x: 20, y: 0 });+ else gesture[cancel]();+ await vi.advanceTimersByTimeAsync(1);+ expect(getUserMedia).not.toHaveBeenCalled();+ expect(start).not.toHaveBeenCalled();+ } finally {+ gesture.dispose();+ vi.useRealTimers();+ }+ },+ );+it('prepares without activating PCM and closes a cancelled press', async () => {const release = prepareVoiceInputMicrophone({ workletUrl: 'https://app.local/pcm16k-worklet.js' });await vi.waitFor(() => expect(AudioWorkletNode).toHaveBeenCalledOnce());diff --git a/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.tsindex 7d917c313d..f1b2e99d33 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts@@ -59,18 +59,24 @@ describe('createComposerLongPressVoiceGesture', () => {vi.useRealTimers();});- it('按住不动到时长才开始录音,松开结束', async () => {+ // Regression: a short composer click opened the Bluetooth microphone before+ // recording, interrupting other apps' audio (user report, 2026-10-26).+ // Unit: exercise the production gesture at 599/600ms. Previously these cases+ // only guarded start/stop, allowing prepare to acquire the device too early.+ it('按住不动到时长才准备麦克风并开始录音,松开结束', async () => {const { gesture, start, stop, getState, prepare, releasePreparation } = setup();gesture.press(origin);- expect(prepare).toHaveBeenCalledOnce();+ expect(prepare).not.toHaveBeenCalled();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS - 1);await settle();+ expect(prepare).not.toHaveBeenCalled();expect(start).not.toHaveBeenCalled();expect(releasePreparation).not.toHaveBeenCalled();vi.advanceTimersByTime(1);await settle();+ expect(prepare).toHaveBeenCalledOnce();expect(start).toHaveBeenCalledOnce();expect(releasePreparation).toHaveBeenCalledOnce();expect(getState()).toBe('listening');@@ -83,29 +89,31 @@ describe('createComposerLongPressVoiceGesture', () => {});it('没到时长就松开是普通点击,不录音', async () => {- const { gesture, start, stop, releasePreparation } = setup();+ const { gesture, start, stop, prepare, releasePreparation } = setup();gesture.press(origin);vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS / 2);gesture.release();- expect(releasePreparation).toHaveBeenCalledOnce();+ expect(releasePreparation).not.toHaveBeenCalled();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS);await settle();+ expect(prepare).not.toHaveBeenCalled();expect(start).not.toHaveBeenCalled();expect(stop).not.toHaveBeenCalled();});it('等待期间拖动超出容差视为选字,放弃长按', async () => {- const { gesture, start, releasePreparation } = setup();+ const { gesture, start, prepare, releasePreparation } = setup();gesture.press(origin);gesture.move({ x: origin.x + COMPOSER_LONG_PRESS_MOVE_TOLERANCE_PX, y: origin.y });gesture.move({ x: origin.x + COMPOSER_LONG_PRESS_MOVE_TOLERANCE_PX + 1, y: origin.y });- expect(releasePreparation).toHaveBeenCalledOnce();+ expect(releasePreparation).not.toHaveBeenCalled();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS);await settle();+ expect(prepare).not.toHaveBeenCalled();expect(start).not.toHaveBeenCalled();});diff --git a/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts b/apps/desktop/src/renderer/voice-input/composerLongPressGesture.tsindex 0634eaf233..752c4458cc 100644--- a/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts+++ b/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts@@ -11,7 +11,7 @@ export type ComposerLongPressVoiceGestureOptions = {holdMs?: number;moveTolerancePx?: number;getState: () => VoiceInputState;- /** Prepare without recording. Cleanup cancels a short press or hands off after start. */+ /** Prepare only after the hold is recognized; cleanup hands off after start. */prepare?: () => (() => void) | void;/*** 开始录音。返回 `false` 表示这次没有占用录音(已在录音、被取消等),@@ -125,6 +125,13 @@ export function createComposerLongPressVoiceGesture(}holding = true;options.onHoldStart?.();+ // Opening a Bluetooth microphone can interrupt playback in other apps even+ // without recording PCM. Ordinary clicks must never prepare the device.+ try {+ releasePreparation = options.prepare?.();+ } catch {+ // Preparation is optional; the normal start path can still recover.+ }const generation = ++startGeneration;const currentStart = Promise.resolve().then(() => options.start())@@ -158,11 +165,6 @@ export function createComposerLongPressVoiceGesture(origin: point,timer: setTimeout(beginVoiceInput, holdMs),};- try {- releasePreparation = options.prepare?.();- } catch {- // Preparation is optional; the normal start path can still recover.- }},move(point) {if (!pending) return;