Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix: 补上复审指出的三处语音竞态

zcinta0514479f2e91c4ab
diff --git a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
index 1d484bd60d..403fa926df 100644
--- a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
@@ -148,14 +148,16 @@ describe('AskUserQuestion voice entry', () => {
trigger?.({ id: 'press-1', phase: 'start' });
await waitFor(() => expect(start).toHaveBeenCalledOnce());
- // Same press released quickly: `tap` must not cancel what `start` began.
+ // Short press: held past the 250ms dedupe window, still released quickly.
+ // The release must not cancel the recording this same press started.
+ await new Promise((resolve) => setTimeout(resolve, 300));
trigger?.({ id: 'press-1', phase: 'tap' });
await new Promise((resolve) => setTimeout(resolve, 0));
expect(stop).not.toHaveBeenCalled();
- // A separate press while listening stops it.
+ // A new press while listening stops it.
await new Promise((resolve) => setTimeout(resolve, 300));
- trigger?.({ id: 'press-2', phase: 'tap' });
+ trigger?.({ id: 'press-2', phase: 'start' });
await waitFor(() => expect(stop).toHaveBeenCalledOnce());
} finally {
window.electronAPI = previous;
diff --git a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
index 694744a0a6..01300f20b8 100644
--- a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
@@ -236,6 +236,11 @@ function AskUserQuestionForm({
dictationRef.current = dictation;
const pendingVoiceStartRef = useRef(false);
const voiceShortcutAtRef = useRef(0);
+ // A shortcut press emits `start` then, on release, `tap`. The flag lives in a
+ // ref because opening the free-text row re-creates the effect that owns the
+ // listener — an effect-local flag would be lost and the tap would stop the
+ // recording the same press just started.
+ const voiceShortcutStartedPressRef = useRef(false);
const answerValue = dictation.liveText ?? customInput;
const voiceShortcutLabel = formatVoiceInputShortcut(getVoiceInputSettings().shortcut);
const voiceButtonLabel = dictation.isBusy
@@ -483,33 +488,27 @@ 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;
+ // Minimized hides the microphone, so only let the shortcut stop a run
+ // that is already recording.
+ if (viewerState === 'minimized' && !dictationRef.current.isBusy) return;
const now = performance.now();
if (now - voiceShortcutAtRef.current < 250) return;
voiceShortcutAtRef.current = now;
const session = dictationRef.current;
if (session.state === 'submitting' || session.state === 'refining') return;
- if (phase === 'tap') {
- if (startedByThisPress) {
- startedByThisPress = false;
- return;
- }
- if (session.isBusy) void session.stop();
+ if (phase === 'start') {
+ // A press while listening means stop; its release must not act again.
+ voiceShortcutStartedPressRef.current = !session.isBusy;
+ beginVoiceInput();
return;
}
- if (phase === 'end') {
- startedByThisPress = false;
- if (session.isBusy) void session.stop();
+ // `tap` / `end` are the release of a press.
+ if (voiceShortcutStartedPressRef.current) {
+ voiceShortcutStartedPressRef.current = false;
return;
}
- startedByThisPress = !session.isBusy;
- beginVoiceInput();
+ if (session.isBusy) void session.stop();
};
const onKeyDown = (event: KeyboardEvent) => {
if (event.repeat || event.isComposing) return;
@@ -764,6 +763,9 @@ function AskUserQuestionForm({
else handleCustomSubmit();
}
if (e.key === 'Escape') {
+ // Closing the row mid-dictation would leave the rest of the
+ // transcript writing into a hidden field.
+ if (dictation.isBusy) return;
e.preventDefault();
setShowCustomInput(false);
setAnswerText('');
@@ -976,6 +978,7 @@ function AskUserQuestionForm({
submitDirectAnswer();
}
if (e.key === 'Escape') {
+ if (dictation.isBusy) return;
e.preventDefault();
handleSkip();
}
diff --git a/apps/mobile/src/session/useAskCardVoice.ts b/apps/mobile/src/session/useAskCardVoice.ts
index 10444ce444..5770644b13 100644
--- a/apps/mobile/src/session/useAskCardVoice.ts
+++ b/apps/mobile/src/session/useAskCardVoice.ts
@@ -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 stoppingRef = useRef<Promise<void> | null>(null);
const startAbortRef = useRef<AbortController | null>(null);
const inputRef = useRef(input);
inputRef.current = input;
@@ -46,7 +47,10 @@ export function useAskCardVoice(input: {
runRef.current = null;
setListening(false);
if (!run) return;
- void run.stop().catch(() => undefined);
+ const stopping = run.stop().catch(() => undefined).finally(() => {
+ if (stoppingRef.current === stopping) stoppingRef.current = null;
+ });
+ stoppingRef.current = stopping;
}, []);
// The card unmounts when the answer is submitted, skipped or the request
@@ -61,6 +65,9 @@ export function useAskCardVoice(input: {
}, []);
const toggle = useCallback(() => {
+ // A session that is still closing must finish first; starting a new one
+ // now would race the microphone teardown.
+ if (stoppingRef.current) return;
if (runRef.current || startingRef.current) {
stop();
return;