Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(voice-input): 快速启动不再因组件挂载重新点亮麦克风
Leng Jingxu2b6e29972d15
diff --git a/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts b/apps/desktop/src/renderer/hooks/useVoiceInputSettings.tsindex 9458aefb17..0d0dedf2c2 100644--- a/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts+++ b/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts@@ -31,6 +31,7 @@ import {type VoiceInputSettingsPatch,} from '../../shared/voiceInputData';import type { VoiceInputShortcut } from '@/voice-input/shortcut';+import { noteVoiceInputUse } from '@/voice-input/WebMicAudioEngine';import { findComposerVoiceInputConflict } from '@/voice-input/composerVoiceInputConflict';import { getComposerSendShortcutPreference } from './useComposerSendShortcutPreference';@@ -260,7 +261,14 @@ export function useVoiceInputSettings(): {);const setFastActivationEnabled = useCallback(- (fastActivationEnabled: boolean) => updateSettings({ fastActivationEnabled }),+ (fastActivationEnabled: boolean) => {+ // Flipping this switch on is an explicit statement of intent, so it counts+ // as use and lets the immediate prewarm open the microphone. Without it the+ // setting would stay inert until the first dictation, which is not what the+ // user just asked for.+ if (fastActivationEnabled) noteVoiceInputUse();+ return updateSettings({ fastActivationEnabled });+ },[updateSettings],);diff --git a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.tsindex b3eb9cf052..4a3a2d0c81 100644--- a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts+++ b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts@@ -203,6 +203,17 @@ let keepAliveIdleDisposeTimer: number | undefined;// (component mount, unrelated settings changes) clear and re-arm the timer, and// must land on the original deadline instead of restarting the countdown.let keepAliveIdleDeadlineAt: number | undefined;+// Last time the user actually dictated, on the same monotonic clock. Mounting a+// ChatInput or the overlay is *not* use: prewarm may only (re)open the capture+// device while this timestamp is still inside the idle window, otherwise every+// task switch would light up a fresh 30-minute window and the OS privacy+// indicator would never go out on a machine the user is merely working in —+// contradicting the promise in settings.voiceInput.fastActivation.hint.+let lastVoiceInputUseAt: number | undefined;+// A recording claim that never reached activate() is a leak: the session stays+// isBusy() forever, so neither the idle window nor "setting turned off" is+// allowed to touch it and the microphone can never be reclaimed.+const STALE_RECORDING_RESERVATION_MS = 60 * 1000;function isSpecificMicrophoneDeviceId(deviceId?: string): deviceId is string {return Boolean(deviceId && deviceId !== 'default');@@ -460,6 +471,9 @@ class KeepAliveMicSession {// session, and a flag would let whichever finishes first clear the other's// claim — handing the still-live session to a replacing prewarm.private recordingReservations = 0;+ // When the current claim was taken. Only used to detect a claim that never+ // reached activate() and never got released.+ private reservationClaimedAt: number | undefined;constructor(options: KeepAliveSessionOptions) {this.options = options;@@ -673,11 +687,28 @@ class KeepAliveMicSession {* stop(), or on any startup path that fails after claiming.*/reserveForRecording(): void {+ if (this.recordingReservations === 0) this.reservationClaimedAt = keepAliveMonotonicNow();this.recordingReservations += 1;}releaseRecordingReservation(): void {this.recordingReservations = Math.max(0, this.recordingReservations - 1);+ if (this.recordingReservations === 0) this.reservationClaimedAt = undefined;+ }++ /**+ * A claim that has outlived any plausible startup.+ *+ * start() takes the claim before its first await and stop() drops it, but a+ * startup that dies in an unforeseen way would keep it forever: the session+ * then reads as busy, so neither the idle window nor "fast activation turned+ * off" may dispose it — the microphone stays open with no path back. Callers+ * use this to reclaim such a session instead of trusting the claim blindly.+ */+ hasStaleRecordingReservation(): boolean {+ if (this.active || this.recordingReservations === 0) return false;+ if (this.reservationClaimedAt === undefined) return false;+ return keepAliveMonotonicNow() - this.reservationClaimedAt > STALE_RECORDING_RESERVATION_MS;}/** Someone other than the caller still needs this session live. */@@ -898,9 +929,23 @@ export async function prewarmVoiceInputMicrophone(options: WebMicAudioEngineOpti// track the user is speaking into when the device/profile changed. stop()// already owns both re-arming the idle window and honouring the new config.if (keepAliveSession?.isBusy()) {- if (keepAliveSession.key !== keepAliveKey(normalized)) {- keepAliveSession.markStaleForReplacement();+ if (keepAliveSession.hasStaleRecordingReservation()) {+ // Self-heal a leaked claim (see hasStaleRecordingReservation): without this+ // the session is busy forever and no later prewarm can ever reclaim it.+ await disposeKeepAliveVoiceInputMicrophone('stale_recording_reservation');+ } else {+ if (keepAliveSession.key !== keepAliveKey(normalized)) {+ keepAliveSession.markStaleForReplacement();+ }+ return;}+ }+ if (!isWithinVoiceInputUseWindow()) {+ // Nobody asked to dictate inside the retention window, so mounting a+ // ChatInput must not open (or re-open) the capture device. Whatever idle+ // session is still around is reclaimed here — the audio context and the+ // worklet stay warm, which is the part that needs no microphone.+ await disposeKeepAliveVoiceInputMicrophone('idle_timeout_after_use_window');return;}let session: KeepAliveMicSession;@@ -989,6 +1034,29 @@ function keepAliveMonotonicNow(): number {: Date.now();}+/**+ * Record that the user actually dictated.+ *+ * Called by an engine once capture really starts, and by the settings switch+ * when the user turns fast activation on (that click is an explicit statement of+ * intent). It is the only thing that re-opens the door for mount-time prewarm+ * after the idle window has released the device; mounting components never does.+ */+export function noteVoiceInputUse(): void {+ lastVoiceInputUseAt = keepAliveMonotonicNow();+}++/**+ * Is the user still inside the window where a warm microphone is justified?+ *+ * Deliberately in-memory: a restart drops the timestamp, so a fresh launch+ * never lights the privacy indicator before the user has asked for dictation.+ */+function isWithinVoiceInputUseWindow(): boolean {+ if (lastVoiceInputUseAt === undefined) return false;+ return keepAliveMonotonicNow() - lastVoiceInputUseAt <= KEEP_ALIVE_MIC_IDLE_TTL_MS;+}+/*** Arm the bounded idle window that releases the warm microphone.*@@ -1363,7 +1431,10 @@ export class WebMicAudioEngine {await disposeKeepAliveVoiceInputMicrophone('devicechange_after_recording');} else {// Real use just ended — this is the only event allowed to restart the- // full 30-minute window the settings copy promises.+ // full 30-minute window the settings copy promises. The use timestamp+ // moves with it, so "last use" and the idle deadline (which starts at+ // the *end* of a recording) never disagree about when the window ends.+ noteVoiceInputUse();scheduleKeepAliveIdleDispose(session, 'idle_timeout_after_recording', { refresh: true });}}@@ -1646,6 +1717,11 @@ export class WebMicAudioEngine {private startWatchdog(): void {this.clearWatchdog();+ // Reached by both capture paths (keep-alive and direct) once the device is+ // actually delivering, so this is where a real dictation starts counting as+ // "use" — the only event that lets a later mount prewarm the microphone+ // again after the idle window has released it.+ noteVoiceInputUse();this.watchdogId = window.setInterval(() => this.checkAudioHealth(), AUDIO_FRAME_WATCHDOG_INTERVAL_MS);}diff --git a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.tsindex 469b9d2a3c..898b1146b9 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts@@ -107,6 +107,7 @@ describe('keep-alive microphone idle window', () => {});it('releases the warm microphone once the idle window elapses', async () => {+ mod.noteVoiceInputUse();await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });expect(track.stopped).toBe(false);@@ -156,7 +157,67 @@ describe('keep-alive microphone idle window', () => {expect(track.stopped).toBe(true);});+ it('does not open the microphone for a mount-time prewarm before any dictation', async () => {+ // Mounting a ChatInput or the overlay is bookkeeping, not use. The AudioContext+ // and worklet may warm up, but the capture device — and the OS privacy+ // indicator — must stay off until the user asks to dictate.+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });++ expect(getUserMedia).not.toHaveBeenCalled();+ });++ it('does not relight the microphone from a later mount after the idle release', async () => {+ mod.noteVoiceInputUse();+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });+ await vi.advanceTimersByTimeAsync(IDLE_TTL_MS);+ expect(track.stopped).toBe(true);+ getUserMedia.mockClear();++ // Past the window the user stopped dictating: remounting the ChatInput (a new+ // task, a settings change) must not start a fresh 30-minute window, otherwise+ // the indicator never goes out while the user just keeps working.+ await vi.advanceTimersByTimeAsync(60 * 1000);+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });++ expect(getUserMedia).not.toHaveBeenCalled();+ });++ it('reopens the warm microphone from a later mount inside the use window', async () => {+ mod.noteVoiceInputUse();+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });+ expect(getUserMedia).toHaveBeenCalledTimes(1);++ // A devicechange released the warm session, but the user dictated moments+ // ago: the next mount may warm the new configuration right away.+ await mod.disposeKeepAliveVoiceInputMicrophone('devicechange');+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });++ expect(getUserMedia).toHaveBeenCalledTimes(2);+ });++ it('reclaims a session whose recording claim leaked', async () => {+ mod.noteVoiceInputUse();+ // Startup hangs on the device handshake and its owner never stops: the claim+ // outlives any plausible startup, so the session would read as busy forever+ // and neither the idle window nor a settings change could reclaim it.+ getUserMedia.mockImplementationOnce(() => new Promise(() => undefined));+ const engine = new mod.WebMicAudioEngine({+ workletUrl: WORKLET_URL,+ keepAlive: true,+ onInterrupted: vi.fn(),+ });+ void engine.start().catch(() => undefined);+ await vi.advanceTimersByTimeAsync(0);++ await vi.advanceTimersByTimeAsync(2 * 60 * 1000);+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });++ // The leaked session is reclaimed and a fresh warm-up owns the device again.+ expect(getUserMedia).toHaveBeenCalledTimes(2);+ });+it('does not extend the idle window when prewarm re-asserts keep-alive intent', async () => {+ mod.noteVoiceInputUse();await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });// A ChatInput mounting (or an unrelated voice setting changing) 20 minutes@@ -172,6 +233,7 @@ describe('keep-alive microphone idle window', () => {});it('restarts the full window after real dictation ends', async () => {+ mod.noteVoiceInputUse();await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });await vi.advanceTimersByTimeAsync(20 * 60 * 1000);@@ -193,6 +255,7 @@ describe('keep-alive microphone idle window', () => {});it('releases the microphone when the machine suspends or the screen locks', async () => {+ mod.noteVoiceInputUse();await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });expect(powerCallback).toBeDefined();@@ -208,6 +271,7 @@ describe('keep-alive microphone idle window', () => {resolveStream = resolve;}));+ mod.noteVoiceInputUse();const prewarming = mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });// Let start() reach the pending getUserMedia await.await vi.advanceTimersByTimeAsync(0);@@ -318,6 +382,7 @@ describe('keep-alive microphone idle window', () => {resolveStream = resolve;}));+ mod.noteVoiceInputUse();const engine = new mod.WebMicAudioEngine({workletUrl: WORKLET_URL,keepAlive: true,@@ -344,6 +409,7 @@ describe('keep-alive microphone idle window', () => {resolveStream = resolve;}));+ mod.noteVoiceInputUse();const first = mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });await vi.advanceTimersByTimeAsync(0);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 09401b5e0a..ca704cbb3b 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@@ -8,6 +8,7 @@ import {import {disposeKeepAliveVoiceInputMicrophone,isMicrophoneDeviceUnavailableError,+ noteVoiceInputUse,prewarmVoiceInputMicrophoneWithAutomaticFallback,prepareVoiceInputMicrophone,WebMicAudioEngine,@@ -61,6 +62,10 @@ describe('prewarmVoiceInputMicrophoneWithAutomaticFallback', () => {let deviceChangeListener: (() => void) | undefined;beforeEach(() => {+ // Every case here exercises prewarming for a user who is dictating, so put+ // the process inside the use window first — mounting alone never opens the+ // device (see WebMicAudioEngine.keepAliveIdle.test.ts).+ noteVoiceInputUse();track = createFakeTrack();getUserMedia = vi.fn(async () => ({getAudioTracks: () => [track],diff --git a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts b/apps/desktop/src/renderer/voice-input/useVoiceInput.tsindex 70da517928..02df7d70b2 100644--- a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts+++ b/apps/desktop/src/renderer/voice-input/useVoiceInput.ts@@ -1206,9 +1206,11 @@ export function useVoiceInput(// Microphone keep-alive is deliberately a separate effect: unlike the prewarm// above it opens a real capture device and turns on the OS privacy indicator,- // so it must not re-run for settings unrelated to the microphone. Language and- // refinement changes used to re-assert keep-alive intent here, which kept the- // device warm for reasons the user never asked for.+ // so it must not re-run for settings unrelated to the microphone. It only+ // opens the device while the user is inside the use window, so a remount+ // cannot relight the indicator on its own (see WebMicAudioEngine). Language+ // and refinement changes used to re-assert keep-alive intent here, which kept+ // the device warm for reasons the user never asked for.useEffect(() => {if (disabledRef.current) return;if (!voiceInputSettings.fastActivationEnabled) {