Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix: 按 review 收紧提问卡语音的生命周期与并发
zcinta051491c003ba0f15
diff --git a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsxindex 53d1046150..1d484bd60d 100644--- a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx+++ b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx@@ -1,6 +1,6 @@// @vitest-environment jsdom-import { createElement } from 'react';+import { createElement, useState } from 'react';import { cleanup, fireEvent, render, waitFor, within } from '@testing-library/react';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';@@ -11,15 +11,27 @@ import { AskUserQuestionPrompt } from '../components/new-chat/AskUserQuestionProconst start = vi.fn(() => true);const stop = vi.fn(async () => undefined);+// Stateful stand-in for the dictation hook: starting flips the card into the+// busy state, which is what disables question navigation.vi.mock('../components/new-chat/useAskUserDictation', () => ({- useAskUserDictation: () => ({- state: 'idle',- isBusy: false,- liveText: null,- lastError: null,- start,- stop,- }),+ useAskUserDictation: () => {+ const [state, setState] = useState<'idle' | 'listening'>('idle');+ return {+ state,+ isBusy: state !== 'idle',+ liveText: null,+ lastError: null,+ start: () => {+ start();+ setState('listening');+ return true;+ },+ stop: async () => {+ await stop();+ setState('idle');+ },+ };+ },}));beforeEach(async () => {@@ -117,4 +129,55 @@ describe('AskUserQuestion voice entry', () => {window.electronAPI = previous;}});++ it('does not stop the recording on the tap that ends the press which started it', async () => {+ const previous = window.electronAPI;+ let trigger: ((payload?: { id?: string; phase?: 'start' | 'tap' | 'end' }) => void) | undefined;+ window.electronAPI = {+ voiceInput: {+ onGlobalShortcutTrigger: (callback: typeof trigger) => {+ trigger = callback;+ return () => { trigger = undefined; };+ },+ claimGlobalShortcutTrigger: () => {},+ getDataSnapshot: () => ({ settings: { shortcut: null } }),+ },+ } as unknown as typeof window.electronAPI;+ try {+ renderAsk([{ question: '选哪个?', options: [{ label: '甲' }, { label: '乙' }] }]);+ trigger?.({ id: 'press-1', phase: 'start' });+ await waitFor(() => expect(start).toHaveBeenCalledOnce());++ // Same press released quickly: `tap` must not cancel what `start` began.+ trigger?.({ id: 'press-1', phase: 'tap' });+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(stop).not.toHaveBeenCalled();++ // A separate press while listening stops it.+ await new Promise((resolve) => setTimeout(resolve, 300));+ trigger?.({ id: 'press-2', phase: 'tap' });+ await waitFor(() => expect(stop).toHaveBeenCalledOnce());+ } finally {+ window.electronAPI = previous;+ }+ });++ it('blocks question navigation and submit while dictation owns the answer', async () => {+ const { view } = renderAsk([+ { question: '第一题?', options: [{ label: '甲' }, { label: '乙' }] },+ { question: '第二题?', options: [{ label: '丙' }, { label: '丁' }] },+ ]);++ fireEvent.click(view.getByTestId('ask-user-voice'));+ await waitFor(() => expect(start).toHaveBeenCalledOnce());++ // Switching questions mid-dictation would write the rest into another answer.+ for (const label of ['跳过', '下一题', '提交']) {+ const button = view.queryByText(label);+ if (button?.closest('button')) {+ expect((button.closest('button') as HTMLButtonElement).disabled).toBe(true);+ }+ }+ expect((view.getByText('跳过').closest('button') as HTMLButtonElement).disabled).toBe(true);+ });});diff --git a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsxindex c1a2b61771..694744a0a6 100644--- a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx+++ b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx@@ -483,6 +483,11 @@ function AskUserQuestionForm({}, [showCustomInput]);useEffect(() => {+ // A global-shortcut press emits `start` and, on release, `tap` (quick) or+ // `end` (held). The tap of the press that just started dictation must not+ // stop it again — otherwise a normal short press starts and immediately+ // ends the recording.+ let startedByThisPress = false;const claim = (phase?: 'start' | 'tap' | 'end') => {if (viewerState === 'minimized') return;const now = performance.now();@@ -490,10 +495,20 @@ function AskUserQuestionForm({voiceShortcutAtRef.current = now;const session = dictationRef.current;if (session.state === 'submitting' || session.state === 'refining') return;- if (phase === 'end' || phase === 'tap') {+ if (phase === 'tap') {+ if (startedByThisPress) {+ startedByThisPress = false;+ return;+ }+ if (session.isBusy) void session.stop();+ return;+ }+ if (phase === 'end') {+ startedByThisPress = false;if (session.isBusy) void session.stop();return;}+ startedByThisPress = !session.isBusy;beginVoiceInput();};const onKeyDown = (event: KeyboardEvent) => {@@ -555,6 +570,10 @@ function AskUserQuestionForm({// question. The answer inputs handle their own keys.if (shouldCardShortcutYield(e, e.key === 'Escape' ? 'dismiss' : 'character', cardRef.current)) return;+ // Dictation owns the answer field until it stops: switching questions+ // would write the rest of the transcript into a different answer.+ if (dictationRef.current.isBusy) return;+// If custom input is focused in single-select mode, handle Escape onlyif (showCustomInput && !isMultiSelect) {if (e.key === 'Escape') {@@ -641,6 +660,7 @@ function AskUserQuestionForm({size="lg"compacttype="button"+ disabled={dictation.isBusy}onClick={handleBack}><span className="flex items-center gap-1.5">@@ -656,6 +676,7 @@ function AskUserQuestionForm({size="lg"compacttype="button"+ disabled={dictation.isBusy}onClick={handleSkip}>{t('chat.askUserQuestion.skip')}@@ -677,9 +698,10 @@ function AskUserQuestionForm({}}}disabled={- isMultiSelect+ dictation.isBusy ||+ (isMultiSelect? selectedLabels.size === 0 && !customInput.trim()- : existingAnswer === undefined+ : existingAnswer === undefined)}>{isLastQuestion ? t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit') : t('chat.askUserQuestion.next')}@@ -769,7 +791,7 @@ function AskUserQuestionForm({<buttontype="button"onClick={handleCustomSubmit}- disabled={!customInput.trim()}+ disabled={!customInput.trim() || dictation.isBusy}data-testid={isLastQuestion ? undefined : 'ask-user-custom-next'}aria-label={isLastQuestion ? undefined : t('chat.askUserQuestion.next')}className={cn(@@ -875,6 +897,7 @@ function AskUserQuestionForm({{idx > 0 && <div className="h-px bg-[var(--ask-option-divider)]" />}<buttontype="button"+ disabled={dictation.isBusy}className={cn('flex w-full items-center justify-between px-[16px] py-[14px] text-left','transition-colors hover:bg-[var(--ask-option-hover)]',@@ -976,7 +999,7 @@ function AskUserQuestionForm({<buttontype="button"onClick={submitDirectAnswer}- disabled={!customInput.trim()}+ disabled={!customInput.trim() || dictation.isBusy}data-testid={isLastQuestion ? undefined : 'ask-user-custom-next'}aria-label={isLastQuestion ? undefined : t('chat.askUserQuestion.next')}className={cn(diff --git a/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts b/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.tsindex 2649fe99f6..063989f930 100644--- a/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts+++ b/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts@@ -84,6 +84,7 @@ export function useAskUserDictation(const stopInFlightRef = useRef<Promise<void> | null>(null);const startReadyRef = useRef<Promise<void>>(Promise.resolve());const mutedRef = useRef(false);+ const pendingMuteRef = useRef<Promise<void> | null>(null);targetRef.current = target;optionsRef.current = options;@@ -104,6 +105,10 @@ export function useAskUserDictation(}, []);const restoreMute = useCallback(async () => {+ // The mute IPC is fire-and-forget at start. Waiting for it here keeps a+ // fast failure from skipping the restore and leaving the system muted.+ const pendingMute = pendingMuteRef.current;+ if (pendingMute) await pendingMute;if (!mutedRef.current || !supportsSystemAudioMute()) return;mutedRef.current = false;try {@@ -201,12 +206,17 @@ export function useAskUserDictation(if (event.type === 'state') {setVoiceState(event.state);if (event.state === 'done' || event.state === 'error') {- runIdRef.current = null;anchorRef.current = null;setLiveText(null);if (event.state === 'error') {- /* The error event carries the message. */+ // The controller has given up before the closing `error` event+ // arrives, so release the microphone now. Keep runIdRef so that+ // error event still passes the ownership check below.+ void stopEngine();+ void restoreMute();+ return;}+ runIdRef.current = null;}return;}@@ -217,10 +227,11 @@ export function useAskUserDictation(setVoiceState('error');setLiveText(null);runIdRef.current = null;+ void stopEngine();void restoreMute();}});- }, [formatError, reportError, restoreMute, setVoiceState, t]);+ }, [formatError, reportError, restoreMute, setVoiceState, stopEngine, t]);const start = useCallback((): boolean => {const api = window.electronAPI?.voiceInput;@@ -255,9 +266,12 @@ export function useAskUserDictation(void playVoiceInputStartCue();}if (settings.muteSystemAudio && supportsSystemAudioMute()) {- void api.muteSystemAudio().then((result) => {+ const muteRequest = api.muteSystemAudio().then((result) => {if (result.ok) mutedRef.current = true;- }).catch(() => undefined);+ }).catch(() => undefined).finally(() => {+ if (pendingMuteRef.current === muteRequest) pendingMuteRef.current = null;+ });+ pendingMuteRef.current = muteRequest;}let guards: Awaited<ReturnType<typeof resolveVoiceInputStartGuards>>;diff --git a/apps/mobile/src/session/InteractionPanel.tsx b/apps/mobile/src/session/InteractionPanel.tsxindex f52ed9d902..f70157139e 100644--- a/apps/mobile/src/session/InteractionPanel.tsx+++ b/apps/mobile/src/session/InteractionPanel.tsx@@ -1224,7 +1224,7 @@ function AskUserQuestionForm({<InteractionTouchButtonaccessibilityLabel={t('interaction.panel.selectAnswer', { label: option.label })}accessibilityHint={busy ? t('interaction.panel.submittingHint') : undefined}- disabled={busy}+ disabled={busy || voice.listening}key={option.label}onPress={() => toggleLabel(option.label)}selected={selected}@@ -1318,7 +1318,6 @@ function AskUserQuestionForm({<AskCardVoiceButton listening={voice.listening} onPress={beginVoice} /></View>)}- {voice.error ? <Text testID="interaction.ask.voiceError">{voice.error}</Text> : null}</View>) : (<View style={[@@ -1347,6 +1346,9 @@ function AskUserQuestionForm({</View>)}+ {/* Shown for both layouts: the option-less card has a microphone too. */}+ {voice.error ? <Text testID="interaction.ask.voiceError">{voice.error}</Text> : null}+<InteractionActions touchLayout={touchLayout}>{item.retainedDraft ? (<ResolveButton busy={busy} label={t('interaction.panel.closeAnswer')}@@ -1358,7 +1360,7 @@ function AskUserQuestionForm({{currentIndex > 0 ? (<InteractionTouchButtonaccessibilityLabel={t('interaction.panel.previous')}- disabled={busy}+ disabled={busy || voice.listening}onPress={() => {const answer = isMulti? encodeMultiSelectAnswer(options, selectedLabels, customInput)@@ -1378,6 +1380,7 @@ function AskUserQuestionForm({<ResolveButtonaccessibilityLabel={t('interaction.panel.skipAccessibility')}busy={busy}+ disabled={voice.listening}label={t('interaction.panel.skip')}onPress={() => advance('')}requestId={requestId}@@ -1389,6 +1392,7 @@ function AskUserQuestionForm({<ResolveButtonaccessibilityLabel={isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submitAnswerAccessibility') : t('interaction.panel.next')}busy={busy}+ disabled={voice.listening}invalidReason={!canSubmitMulti ? t('interaction.panel.answerRequired') : null}label={isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submit') : t('interaction.panel.next')}onPress={submitMulti}@@ -1401,6 +1405,7 @@ function AskUserQuestionForm({<ResolveButtonaccessibilityLabel={isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submitAnswerAccessibility') : t('interaction.panel.next')}busy={busy}+ disabled={voice.listening}invalidReason={!canSubmitSingle ? t('interaction.panel.answerRequired') : null}label={isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submit') : t('interaction.panel.next')}onPress={submitSingle}@@ -1969,6 +1974,7 @@ function ResolveButton({armed,busy,confirmLabel,+ disabled,invalidReason,label,onPress,@@ -1981,6 +1987,8 @@ function ResolveButton({armed?: boolean;busy: boolean;confirmLabel?: string;+ /** Blocks the press without pretending to submit (e.g. while dictating). */+ disabled?: boolean;invalidReason?: string | null;label: string;onPress(): void;@@ -2005,7 +2013,7 @@ function ResolveButton({: variant === 'secondary'? styles.secondaryButton: styles.inlineButton;- const disabledButtonStyle = variant === 'primary' && presentation.disabled+ const disabledButtonStyle = variant === 'primary' && (disabled || presentation.disabled)? styles.primaryButtonDisabled: null;const textStyle = variant === 'primary'@@ -2013,7 +2021,7 @@ function ResolveButton({: variant === 'secondary'? styles.secondaryText: styles.inlineButtonText;- const disabledTextStyle = variant === 'primary' && presentation.disabled+ const disabledTextStyle = variant === 'primary' && (disabled || presentation.disabled)? styles.primaryTextDisabled: null;@@ -2022,7 +2030,7 @@ function ResolveButton({accessibilityLabel={accessibilityLabel}accessibilityHint={presentation.disabledReason ?? undefined}busy={busy}- disabled={presentation.disabled}+ disabled={Boolean(disabled) || presentation.disabled}hitSlop={companion ? COMPANION_BUTTON_HIT_SLOP : undefined}onPress={onPress}style={[buttonStyle, disabledButtonStyle, touchStyle, stack?.stacked && STACKED_COMPANION_BUTTON]}diff --git a/apps/mobile/src/session/askCardVoiceStart.ts b/apps/mobile/src/session/askCardVoiceStart.tsindex 10b3a83713..6491db0eb1 100644--- a/apps/mobile/src/session/askCardVoiceStart.ts+++ b/apps/mobile/src/session/askCardVoiceStart.ts@@ -23,8 +23,21 @@ import { resolveMobileVoiceRecordingPermission } from '@/session/mobileVoiceStartype AppStateListener = (state: string) => void;+/** Startup aborted because the user stopped or the card went away. */+export class AskCardVoiceCancelledError extends Error {+ constructor() {+ super('语音输入已取消。');+ this.name = 'AskCardVoiceCancelledError';+ }+}++export function isAskCardVoiceCancelled(error: unknown): boolean {+ return error instanceof AskCardVoiceCancelledError;+}+export async function startAskCardDictation(input: {deviceId: string;+ signal?: AbortSignal;getAccessToken: () => Promise<string | null>;refreshAccessToken: () => Promise<string | null>;apiFetch: AskCardAuth['apiFetch'];@@ -46,17 +59,25 @@ export async function startAskCardDictation(input: {return () => subscription.remove();});const abort = new AbortController();- const permission = await resolveMobileVoiceRecordingPermission({- getPermission: getRecordingPermissionsAsync,- requestPermission: requestRecordingPermissionsAsync,- isRequestCurrent: () => !abort.signal.aborted,- isAppActive,- subscribeToAppState,- signal: abort.signal,- waitForAppActive: () => waitUntilActive(isAppActive, subscribeToAppState, abort.signal),- });- if (permission === 'denied') throw new Error(mobileVoiceMicPermissionError());- if (permission !== 'granted') throw new Error('语音输入已取消。');+ const forwardAbort = () => abort.abort();+ if (input.signal?.aborted) throw new AskCardVoiceCancelledError();+ input.signal?.addEventListener('abort', forwardAbort, { once: true });+ try {+ const permission = await resolveMobileVoiceRecordingPermission({+ getPermission: getRecordingPermissionsAsync,+ requestPermission: requestRecordingPermissionsAsync,+ isRequestCurrent: () => !abort.signal.aborted,+ isAppActive,+ subscribeToAppState,+ signal: abort.signal,+ waitForAppActive: () => waitUntilActive(isAppActive, subscribeToAppState, abort.signal),+ });+ if (abort.signal.aborted) throw new AskCardVoiceCancelledError();+ if (permission === 'denied') throw new Error(mobileVoiceMicPermissionError());+ if (permission !== 'granted') throw new AskCardVoiceCancelledError();+ } finally {+ input.signal?.removeEventListener('abort', forwardAbort);+ }const credential = createMobileCindyVoiceCredential(input.deviceId);const voiceContext = new MobileCindyVoiceRunContext(@@ -90,6 +111,12 @@ export async function startAskCardDictation(input: {onReadyForEndCue: credential.settings?.playInteractionSound ? playMobileVoiceInputEndCue : undefined,});await session.start();+ // The user may have stopped while the voice service was connecting: the+ // microphone is now open, so release it right away instead of leaving it on.+ if (input.signal?.aborted) {+ await session.cancel().catch(() => undefined);+ throw new AskCardVoiceCancelledError();+ }return {stop: () => session.stop().then(() => undefined),cancel: () => session.cancel(),@@ -110,7 +137,7 @@ function waitUntilActive(});signal.addEventListener('abort', () => {unsubscribe();- reject(new Error('语音输入已取消。'));+ reject(new AskCardVoiceCancelledError());}, { once: true });});}diff --git a/apps/mobile/src/session/useAskCardVoice.ts b/apps/mobile/src/session/useAskCardVoice.tsindex 24976f2452..10444ce444 100644--- a/apps/mobile/src/session/useAskCardVoice.ts+++ b/apps/mobile/src/session/useAskCardVoice.ts@@ -5,7 +5,7 @@* dictation session against the answer string, and only loads the native audio* modules when the user actually taps the microphone.*/-import { useCallback, useRef, useState } from 'react';+import { useCallback, useEffect, useRef, useState } from 'react';import { useAskCardAuth } from '@/session/askCardAuth';import type { MobileVoiceSelection } from '@/session/mobileVoiceInput';@@ -30,6 +30,7 @@ export function useAskCardVoice(input: {const [error, setError] = useState<string | null>(null);const runRef = useRef<VoiceRun | null>(null);const startingRef = useRef(false);+ const startAbortRef = useRef<AbortController | null>(null);const inputRef = useRef(input);inputRef.current = input;const authRef = useRef(auth);@@ -37,6 +38,10 @@ export function useAskCardVoice(input: {const stop = useCallback(() => {startingRef.current = false;+ // Abort the startup so the microphone closes now instead of waiting for a+ // slow voice-service handshake to finish.+ startAbortRef.current?.abort();+ startAbortRef.current = null;const run = runRef.current;runRef.current = null;setListening(false);@@ -44,6 +49,17 @@ export function useAskCardVoice(input: {void run.stop().catch(() => undefined);}, []);+ // The card unmounts when the answer is submitted, skipped or the request+ // disappears — the recording must not outlive it.+ useEffect(() => () => {+ startingRef.current = false;+ startAbortRef.current?.abort();+ startAbortRef.current = null;+ const run = runRef.current;+ runRef.current = null;+ void run?.cancel().catch(() => undefined);+ }, []);+const toggle = useCallback(() => {if (runRef.current || startingRef.current) {stop();@@ -57,12 +73,15 @@ export function useAskCardVoice(input: {}startingRef.current = true;setError(null);+ const abort = new AbortController();+ startAbortRef.current = abort;void (async () => {try {const { startAskCardDictation } = await import('@/session/askCardVoiceStart');if (!startingRef.current) return;const run = await startAskCardDictation({deviceId,+ signal: abort.signal,getAccessToken: () => currentAuth.getAccessToken(),refreshAccessToken: () => currentAuth.refreshAccessToken(),apiFetch: currentAuth.apiFetch,@@ -89,7 +108,12 @@ export function useAskCardVoice(input: {} catch (error) {startingRef.current = false;setListening(false);- setError(error instanceof Error ? error.message : String(error));+ // Stopping during startup is a normal outcome, not a failure.+ if (!(error instanceof Error && error.name === 'AskCardVoiceCancelledError')) {+ setError(error instanceof Error ? error.message : String(error));+ }+ } finally {+ if (startAbortRef.current === abort) startAbortRef.current = null;}})();}, [stop]);