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。

feat: 提问卡录音中提交自动停止并等待完整回答

zcinta05146ae780dc6cce
diff --git a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
index 48c5977202..f4b80f7573 100644
--- a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
@@ -11,12 +11,13 @@ import { AskUserQuestionPrompt } from '../components/new-chat/AskUserQuestionPro
import type { VoiceQuestionBridge } from '../components/new-chat/voiceQuestionSelection';
const start = vi.fn(() => true);
-const stop = vi.fn(async () => undefined);
+const stop = vi.fn(async () => true);
+let finalTranscript = '完整回答';
// 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: () => {
+ useAskUserDictation: (target: { setText: (text: string) => void }) => {
const [state, setState] = useState<'idle' | 'listening'>('idle');
return {
state,
@@ -29,8 +30,10 @@ vi.mock('../components/new-chat/useAskUserDictation', () => ({
return true;
},
stop: async () => {
- await stop();
+ const ok = await stop();
+ if (ok) target.setText(finalTranscript);
setState('idle');
+ return ok;
},
};
},
@@ -38,7 +41,8 @@ vi.mock('../components/new-chat/useAskUserDictation', () => ({
beforeEach(async () => {
start.mockClear();
- stop.mockClear();
+ stop.mockReset().mockResolvedValue(true);
+ finalTranscript = '完整回答';
await i18n.changeLanguage('zh-CN');
});
@@ -221,7 +225,7 @@ describe('AskUserQuestion voice entry', () => {
}
});
- it('blocks question navigation and submit while dictation owns the answer', async () => {
+ it('blocks skipping while dictation owns the answer', async () => {
const { view } = renderAsk([
{ question: '第一题?', options: [{ label: '甲' }, { label: '乙' }] },
{ question: '第二题?', options: [{ label: '丙' }, { label: '丁' }] },
@@ -231,14 +235,66 @@ describe('AskUserQuestion voice entry', () => {
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);
});
+
+ // #6785: the old card disabled submit until the user stopped the mic.
+ // Unit: real card actions must wait for the final transcript, submit once,
+ // and leave failures editable. Existing cases only tested the voice entry.
+ it.each(['click', 'enter'] as const)('stops and submits the final answer once via %s', async (action) => {
+ let finish!: (ok: boolean) => void;
+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));
+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ const submit = view.getByText('提交').closest('button')!;
+ expect(submit.disabled).toBe(false);
+ if (action === 'click') fireEvent.click(submit);
+ else fireEvent.keyDown(view.getByPlaceholderText('请输入回答…'), { key: 'Enter' });
+ fireEvent.click(submit);
+ expect(stop).toHaveBeenCalledOnce();
+ expect(onAnswer).not.toHaveBeenCalled();
+ await act(async () => finish(true));
+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '补充一句': '完整回答' });
+ });
+
+ it('keeps the answer editable instead of submitting after a failed stop', async () => {
+ stop.mockResolvedValueOnce(false);
+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);
+ fireEvent.change(view.getByPlaceholderText('请输入回答…'), { target: { value: '保留文字' } });
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ await act(async () => fireEvent.click(view.getByText('提交')));
+ expect(onAnswer).not.toHaveBeenCalled();
+ expect((view.getByPlaceholderText('请输入回答…') as HTMLTextAreaElement).value).toBe('保留文字');
+ });
+
+ it('does not submit an empty finalized transcript', async () => {
+ finalTranscript = '';
+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ await act(async () => fireEvent.click(view.getByText('提交')));
+ expect(stop).toHaveBeenCalledOnce();
+ expect(onAnswer).not.toHaveBeenCalled();
+ expect(view.getByText('提交').closest('button')!.disabled).toBe(true);
+ });
+
+ it('does not submit a late result after leaving the card', async () => {
+ let finish!: (ok: boolean) => void;
+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));
+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ fireEvent.click(view.getByText('提交'));
+ view.unmount();
+ await act(async () => finish(true));
+ expect(onAnswer).not.toHaveBeenCalled();
+ });
+
+ it('encodes checked options together with the final voice answer', async () => {
+ const { view, onAnswer } = renderAsk([{ question: '选哪些?', multiSelect: true, options: [{ label: '甲' }, { label: '乙' }] }]);
+ fireEvent.click(view.getByText('甲'));
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ await act(async () => fireEvent.click(view.getByText('提交')));
+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '选哪些?': JSON.stringify(['甲', '完整回答']) });
+ });
});
describe('companion voice mode integration', () => {
diff --git a/apps/desktop/src/renderer/__tests__/useAskUserDictation.test.tsx b/apps/desktop/src/renderer/__tests__/useAskUserDictation.test.tsx
index a9b9d9899e..f00a279d9e 100644
--- a/apps/desktop/src/renderer/__tests__/useAskUserDictation.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/useAskUserDictation.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
-import { renderHook, waitFor } from '@testing-library/react';
+import { act, renderHook, waitFor } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { VoiceInputRendererEvent } from '@cindy/voice-input-core';
@@ -39,7 +39,7 @@ describe('useAskUserDictation', () => {
vi.unstubAllGlobals();
});
- it('writes the submitted transcript into the answer and applies an unedited refinement', async () => {
+ it.each(['done', 'error'] as const)('keeps the final text and reports terminal %s to the submitter', async (terminal) => {
let onEvent: ((event: VoiceInputRendererEvent) => void) | undefined;
let text = '已有';
const setText = vi.fn((value: string) => { text = value; });
@@ -71,32 +71,45 @@ describe('useAskUserDictation', () => {
focus: () => undefined,
}));
- expect(hook.result.current.start()).toBe(true);
+ act(() => expect(hook.result.current.start()).toBe(true));
await waitFor(() => expect(onEvent).toBeTypeOf('function'));
- onEvent?.({
+ act(() => onEvent?.({
type: 'draft',
runId: 'run-1',
text: '补充',
source: 'partial',
segment: { id: 'seg-1', text: '补充', source: 'partial', status: 'partial', updatedAt: 1 },
- } as unknown as VoiceInputRendererEvent);
+ } as unknown as VoiceInputRendererEvent));
await waitFor(() => expect(hook.result.current.liveText).toBe('已有补充'));
- onEvent?.({
+ act(() => onEvent?.({
type: 'submitted',
runId: 'run-1',
text: '补充',
segment: { id: 'seg-1', text: '补充', basedOnText: '补充', source: 'stable', status: 'final', updatedAt: 2 },
- } as unknown as VoiceInputRendererEvent);
+ } as unknown as VoiceInputRendererEvent));
await waitFor(() => expect(setText).toHaveBeenCalledWith('已有补充'));
- onEvent?.({
+ // #6785, unit: an IPC acknowledgement alone is not a completed answer.
+ // The final refinement may arrive later; submit must await the terminal event.
+ let completed = false;
+ let stopping!: Promise<boolean>;
+ await act(async () => {
+ stopping = hook.result.current.stop().then((ok) => { completed = true; return ok; });
+ });
+ expect(completed).toBe(false);
+
+ act(() => onEvent?.({
type: 'refined',
runId: 'run-1',
text: '补充说明',
segment: { id: 'seg-1', text: '补充说明', basedOnText: '补充', source: 'stable', status: 'final', updatedAt: 3 },
range: { segmentIds: ['seg-1'], start: 2, end: 4 },
- } as unknown as VoiceInputRendererEvent);
+ } as unknown as VoiceInputRendererEvent));
await waitFor(() => expect(text).toBe('已有补充说明'));
+ expect(completed).toBe(false);
+ act(() => onEvent?.({ type: 'state', runId: 'run-1', state: terminal } as VoiceInputRendererEvent));
+ await expect(stopping).resolves.toBe(terminal === 'done');
+ expect(text).toBe('已有补充说明');
});
});
diff --git a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
index 62abc0f2bb..c60d8a9961 100644
--- a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
@@ -241,6 +241,15 @@ function AskUserQuestionForm({
});
const dictationRef = useRef(dictation);
dictationRef.current = dictation;
+ const [voiceSubmitting, setVoiceSubmitting] = useState(false);
+ const voiceSubmitRef = useRef(false);
+ const mountedRef = useRef(true);
+ useEffect(() => {
+ mountedRef.current = true;
+ return () => { mountedRef.current = false; };
+ }, []);
+ const voiceSubmitLabel = t(dictation.state === 'refining'
+ ? 'voiceInputOverlay.status.refining' : 'voiceInputOverlay.status.submitting');
const pendingVoiceStartRef = useRef(false);
const voiceShortcutAtRef = useRef(0);
// A shortcut press emits `start` then, on release, `tap`. The flag lives in a
@@ -434,16 +443,35 @@ function AskUserQuestionForm({
}, [markStarted]);
// ── Multi-select: Next button → encode as JSON array string ──
- const handleNext = useCallback(() => {
- if (selectedLabels.size === 0 && !customInput.trim()) return;
- // Maintain UI visual order: filter options by selected state, then append custom text
- const parts: string[] = options.filter((o) => selectedLabels.has(o.label)).map((o) => o.label);
- if (customInput.trim()) {
- // Custom text is an additional selected item
- parts.push(customInput.trim());
+ const submitWithDictation = useCallback(async (encode: (text: string) => string | null) => {
+ if (voiceSubmitRef.current || isAnimating) return;
+ if (!dictationRef.current.isBusy) {
+ const answer = encode(answerTextRef.current);
+ if (answer !== null) advance(answer);
+ return;
+ }
+ voiceSubmitRef.current = true;
+ setVoiceSubmitting(true);
+ try {
+ const ok = await dictationRef.current.stop();
+ if (!ok || !mountedRef.current) return;
+ const answer = encode(answerTextRef.current);
+ if (answer !== null) advance(answer);
+ } finally {
+ voiceSubmitRef.current = false;
+ if (mountedRef.current) setVoiceSubmitting(false);
}
- advance(JSON.stringify(parts));
- }, [selectedLabels, customInput, advance]);
+ }, [advance, isAnimating]);
+
+ const handleNext = useCallback(() => {
+ void submitWithDictation((text) => {
+ if (selectedLabels.size === 0 && !text.trim()) return null;
+ // Keep option order, followed by the finalized free-text answer.
+ const parts = options.filter((o) => selectedLabels.has(o.label)).map((o) => o.label);
+ if (text.trim()) parts.push(text.trim());
+ return JSON.stringify(parts);
+ });
+ }, [options, selectedLabels, submitWithDictation]);
// ── Back ──
const handleBack = useCallback(() => {
@@ -480,6 +508,7 @@ function AskUserQuestionForm({
const beginVoiceInput = useCallback(() => {
if (voiceBridge) return;
+ if (voiceSubmitRef.current) return;
const session = dictationRef.current;
if (session.isBusy) {
void session.stop();
@@ -563,25 +592,24 @@ function AskUserQuestionForm({
// ── Custom input submit (single-select mode) ──
const handleCustomSubmit = useCallback(() => {
- const trimmed = customInput.trim();
- if (!trimmed) return;
if (isMultiSelect) {
// In multi-select, custom text is part of the selection — click Next to submit
return;
}
- advance(trimmed);
+ void submitWithDictation((text) => text.trim() || null);
// advance restores the next page when navigating. On the final page keep
// this field retryable until the host confirms and removes the card.
- }, [customInput, isMultiSelect, advance]);
+ }, [isMultiSelect, submitWithDictation]);
// ── Direct free-text answer when no option rows render ──
// Multi-select keeps the documented JSON-array encoding (`["text"]`), otherwise
// the answer cannot be parsed back when the user returns to edit this question.
const submitDirectAnswer = useCallback(() => {
- const trimmed = customInput.trim();
- if (!trimmed) return;
- advance(isMultiSelect ? JSON.stringify([trimmed]) : trimmed);
- }, [customInput, isMultiSelect, advance]);
+ void submitWithDictation((text) => {
+ const trimmed = text.trim();
+ return trimmed ? (isMultiSelect ? JSON.stringify([trimmed]) : trimmed) : null;
+ });
+ }, [isMultiSelect, submitWithDictation]);
const voiceHandler = useRef<(text: string) => void>(() => {});
const voiceTarget = useMemo<VoiceQuestionTarget>(() => {
@@ -789,13 +817,13 @@ function AskUserQuestionForm({
}
}}
disabled={
- dictation.isBusy ||
+ voiceSubmitting ||
(isMultiSelect
- ? selectedLabels.size === 0 && !customInput.trim()
- : existingAnswer === undefined)
+ ? !dictation.isBusy && selectedLabels.size === 0 && !customInput.trim()
+ : dictation.isBusy || existingAnswer === undefined)
}
>
- {isLastQuestion ? t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit') : t('chat.askUserQuestion.next')}
+ {voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit') : t('chat.askUserQuestion.next')}
</Button>
)}
</>
@@ -847,10 +875,6 @@ function AskUserQuestionForm({
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
e.preventDefault();
- if (dictation.isBusy) {
- void dictation.stop();
- return;
- }
if (isMultiSelect) handleNext();
else handleCustomSubmit();
}
@@ -878,27 +902,31 @@ function AskUserQuestionForm({
size="md"
label={voiceButtonLabel}
busy={dictation.isBusy}
+ disabled={voiceSubmitting}
onClick={beginVoiceInput}
/>)}
{!isMultiSelect && (
- <Tip text={isLastQuestion ? null : t('chat.askUserQuestion.next')}>
+ <Tip text={voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? null : t('chat.askUserQuestion.next')}>
<button
type="button"
onClick={handleCustomSubmit}
- disabled={!customInput.trim() || dictation.isBusy}
+ disabled={voiceSubmitting || (!dictation.isBusy && !customInput.trim())}
+ aria-busy={voiceSubmitting}
data-testid={isLastQuestion ? undefined : 'ask-user-custom-next'}
- aria-label={isLastQuestion ? undefined : t('chat.askUserQuestion.next')}
+ aria-label={voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? undefined : t('chat.askUserQuestion.next')}
className={cn(
'shrink-0 rounded-[9999px] text-13 font-medium',
isLastQuestion
? 'px-[16px] py-[6px]'
: 'flex h-8 w-8 items-center justify-center',
- customInput.trim()
+ customInput.trim() || dictation.isBusy
? 'bg-[var(--ask-send-bg)] text-[var(--ask-send-text)]'
: 'bg-[var(--ask-send-disabled-bg)] text-[var(--ask-send-disabled-text)]',
)}
>
- {isLastQuestion ? (
+ {voiceSubmitting ? (
+ <span role="status" aria-label={voiceSubmitLabel}>{isLastQuestion ? voiceSubmitLabel : '…'}</span>
+ ) : isLastQuestion ? (
t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit')
) : (
<ArrowRight size={15} strokeWidth={2} aria-hidden="true" />
@@ -1063,10 +1091,6 @@ function AskUserQuestionForm({
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
e.preventDefault();
- if (dictation.isBusy) {
- void dictation.stop();
- return;
- }
submitDirectAnswer();
}
if (e.key === 'Escape') {
@@ -1088,24 +1112,28 @@ function AskUserQuestionForm({
size="md"
label={voiceButtonLabel}
busy={dictation.isBusy}
+ disabled={voiceSubmitting}
onClick={beginVoiceInput}
/>)}
- <Tip text={isLastQuestion ? null : t('chat.askUserQuestion.next')}>
+ <Tip text={voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? null : t('chat.askUserQuestion.next')}>
<button
type="button"
onClick={submitDirectAnswer}
- disabled={!customInput.trim() || dictation.isBusy}
+ disabled={voiceSubmitting || (!dictation.isBusy && !customInput.trim())}
+ aria-busy={voiceSubmitting}
data-testid={isLastQuestion ? undefined : 'ask-user-custom-next'}
- aria-label={isLastQuestion ? undefined : t('chat.askUserQuestion.next')}
+ aria-label={voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? undefined : t('chat.askUserQuestion.next')}
className={cn(
'h-10 rounded-[9999px] text-14 font-medium transition-colors',
isLastQuestion ? 'px-4' : 'flex w-10 items-center justify-center',
- customInput.trim()
+ customInput.trim() || dictation.isBusy
? 'bg-[var(--ask-send-bg)] text-[var(--ask-send-text)]'
: 'bg-[var(--ask-send-disabled-bg)] text-[var(--ask-send-disabled-text)]',
)}
>
- {isLastQuestion ? (
+ {voiceSubmitting ? (
+ <span role="status" aria-label={voiceSubmitLabel}>{isLastQuestion ? voiceSubmitLabel : '…'}</span>
+ ) : isLastQuestion ? (
t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit')
) : (
<ArrowRight size={17} strokeWidth={2} aria-hidden="true" />
@@ -1125,9 +1153,11 @@ function AskUserVoiceButton({
busy,
onClick,
size = 'sm',
+ disabled = false,
}: {
label: string;
busy: boolean;
+ disabled?: boolean;
onClick: () => void;
/**
* md matches the answer row's send button (32px) so the two share one center
@@ -1141,6 +1171,7 @@ function AskUserVoiceButton({
data-testid="ask-user-voice"
aria-label={label}
aria-pressed={busy}
+ disabled={disabled}
onMouseDown={(event) => {
// Keep the caret in the answer field so dictation replaces the selection
// the user was editing, instead of jumping after the button takes focus.
diff --git a/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts b/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts
index 063989f930..a8ae2cdd6a 100644
--- a/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts
+++ b/apps/desktop/src/renderer/components/new-chat/useAskUserDictation.ts
@@ -67,7 +67,7 @@ export function useAskUserDictation(
liveText: string | null;
lastError: string | null;
start: () => boolean;
- stop: () => Promise<void>;
+ stop: () => Promise<boolean>;
} {
const { t } = useTranslation();
const [state, setState] = useState<VoiceInputState>('idle');
@@ -81,7 +81,9 @@ export function useAskUserDictation(
const engineRef = useRef<WebMicAudioEngine | null>(null);
const anchorRef = useRef<Anchor | null>(null);
const submittedRef = useRef<SubmittedSpan | null>(null);
- const stopInFlightRef = useRef<Promise<void> | null>(null);
+ const stopInFlightRef = useRef<Promise<boolean> | null>(null);
+ const completionRef = useRef<{ promise: Promise<boolean>; resolve: (ok: boolean) => void } | null>(null);
+ const draftTextRef = useRef<string | null>(null);
const startReadyRef = useRef<Promise<void>>(Promise.resolve());
const mutedRef = useRef(false);
const pendingMuteRef = useRef<Promise<void> | null>(null);
@@ -89,8 +91,15 @@ export function useAskUserDictation(
optionsRef.current = options;
const setVoiceState = useCallback((next: VoiceInputState) => {
+ if (next === 'error' && !submittedRef.current && draftTextRef.current !== null) {
+ targetRef.current.setText(draftTextRef.current);
+ draftTextRef.current = null;
+ }
stateRef.current = next;
setState(next);
+ if (next === 'done' || next === 'error' || next === 'idle') {
+ completionRef.current?.resolve(next === 'done');
+ }
}, []);
const formatError = useCallback((message: string, code?: VoiceInputErrorCode) => {
@@ -162,7 +171,9 @@ export function useAskUserDictation(
if (event.type === 'draft') {
const anchor = anchorRef.current;
if (!anchor) return;
- setLiveText(spliceText(anchor.text, anchor.range, event.text).value);
+ const value = spliceText(anchor.text, anchor.range, event.text).value;
+ draftTextRef.current = value;
+ setLiveText(value);
return;
}
if (event.type === 'submitted') {
@@ -249,6 +260,10 @@ export function useAskUserDictation(
const selection = targetRef.current.getSelection();
anchorRef.current = { text: targetRef.current.getText(), range: selection };
submittedRef.current = null;
+ draftTextRef.current = null;
+ let resolveCompletion!: (ok: boolean) => void;
+ const completion = new Promise<boolean>((resolve) => { resolveCompletion = resolve; });
+ completionRef.current = { promise: completion, resolve: resolveCompletion };
runIdRef.current = null;
setLastError(null);
setLiveText(null);
@@ -380,33 +395,44 @@ export function useAskUserDictation(
const stop = useCallback(async () => {
if (stopInFlightRef.current) return stopInFlightRef.current;
const stopping = (async () => {
- await startReadyRef.current;
- const settings = getVoiceInputSettings();
- if (settings.playInteractionSound) playVoiceInputEndCue();
- await stopEngine();
- if (!runIdRef.current) {
- await restoreMute();
- targetRef.current.focus();
- return;
- }
try {
- const result = await window.electronAPI.voiceInput.stop();
- if (!result.ok) reportError(formatError(result.error));
+ // Capture may already be live while the service is connecting. Stop it
+ // before awaiting startup so post-press speech is not included.
+ await stopEngine();
+ await startReadyRef.current;
+ const completion = completionRef.current;
+ const settings = getVoiceInputSettings();
+ if (settings.playInteractionSound) playVoiceInputEndCue();
+ // Also release an engine whose startup finished after the first stop.
+ await stopEngine();
+ if (runIdRef.current) {
+ if (stateRef.current === 'listening') setVoiceState('submitting');
+ const result = await window.electronAPI.voiceInput.stop();
+ if (!result.ok) {
+ await fail(formatError(result.error));
+ return false;
+ }
+ }
+ // IPC acknowledgement and final transcript events are separate. Only
+ // the terminal event proves that setText has received the final text.
+ return completion ? await completion.promise : false;
} catch (error) {
- reportError(formatError(error instanceof Error ? error.message : String(error)));
+ await fail(formatError(error instanceof Error ? error.message : String(error)));
+ return false;
} finally {
await restoreMute();
+ targetRef.current.focus();
}
- targetRef.current.focus();
})();
stopInFlightRef.current = stopping.finally(() => {
stopInFlightRef.current = null;
});
return stopInFlightRef.current;
- }, [formatError, reportError, restoreMute, stopEngine]);
+ }, [fail, formatError, restoreMute, setVoiceState, stopEngine]);
useEffect(() => () => {
attemptRef.current += 1;
+ completionRef.current?.resolve(false);
const runId = runIdRef.current;
runIdRef.current = null;
void stopEngine();
diff --git a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
index fc2537bc51..a926a77c5d 100644
--- a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
+++ b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
@@ -13,6 +13,10 @@ import { ChatFilePathContext, type ChatFilePathContextValue } from '@/session/ch
import { QuestionReferenceText } from '@/session/QuestionReferenceText';
import { _clearRemotePathVerdictCache } from '@/session/remotePathVerdict';
import { i18n } from '@/i18n';
+import { AskCardAuthContext } from '@/session/askCardAuth';
+
+const voiceStarter = vi.hoisted(() => ({ start: vi.fn() }));
+vi.mock('@/session/askCardVoiceStart', () => ({ startAskCardDictation: voiceStarter.start }));
const evidence = vi.hoisted(() => ({ openURL: vi.fn() }));
// As in LegendList, setters are inert outside a list item. The composer card
@@ -120,6 +124,67 @@ async function type(id: string, value: string) {
});
}
+// #6785, unit: real card + hook; only native/cloud startup is faked.
+// Existing cases never submitted while listening. The final text arrives at stop.
+it.each([
+ { multiSelect: false, connecting: false },
+ { multiSelect: true, connecting: false },
+ { multiSelect: false, connecting: true },
+])('waits for voice finalization and submits once (%o)', async ({ multiSelect, connecting }) => {
+ let finish!: () => void;
+ const stop = vi.fn();
+ voiceStarter.start.mockImplementation(async (input) => {
+ const run = { stop: () => stop(), cancel: vi.fn(async () => undefined) };
+ let ready: (() => void) | undefined;
+ stop.mockImplementation(() => new Promise<void>((resolve) => { finish = () => { input.setText('完整回答'); ready?.(); resolve(); }; }));
+ input.onRunCreated(run);
+ input.onListening();
+ return connecting ? new Promise((resolve) => { ready = () => resolve(run); }) : run;
+ });
+ remoteSessionStore.setPendingInteractions('s1', [{ request: {
+ kind: 'ask_user_question', requestId, questions: [{ question: 'Answer?', multiSelect }],
+ } }]);
+ await act(async () => root.render(<AskCardAuthContext.Provider value={{
+ getAccessToken: async () => null, refreshAccessToken: async () => null, apiFetch: async () => null,
+ }}><Harness /></AskCardAuthContext.Provider>));
+ await click('interaction.ask.voice');
+ expect(host.querySelector<HTMLButtonElement>('[data-testid="interaction.ask.submitButton"]')!.disabled).toBe(false);
+ await click('interaction.ask.submitButton');
+ await click('interaction.ask.submitButton');
+ expect(stop).toHaveBeenCalledOnce();
+ expect(resolveInteraction).not.toHaveBeenCalled();
+ await act(async () => finish());
+ expect(resolveInteraction).toHaveBeenCalledOnce();
+ expect(JSON.stringify(resolveInteraction.mock.calls[0])).toContain('完整回答');
+});
+
+it.each(['stop', 'live'] as const)('retains text and permits manual correction after a %s failure', async (failure) => {
+ let fail!: () => void;
+ voiceStarter.start.mockImplementation(async (input) => {
+ const run = { stop: async () => { input.setText('保留文字'); throw new Error('识别失败'); }, cancel: async () => undefined };
+ fail = () => { input.setText('保留文字'); input.onError('识别失败'); };
+ input.onRunCreated(run);
+ input.onListening();
+ return run;
+ });
+ remoteSessionStore.setPendingInteractions('s1', [{ request: {
+ kind: 'ask_user_question', requestId, questions: [{ question: 'Answer?' }],
+ } }]);
+ await act(async () => root.render(<AskCardAuthContext.Provider value={{
+ getAccessToken: async () => null, refreshAccessToken: async () => null, apiFetch: async () => null,
+ }}><Harness /></AskCardAuthContext.Provider>));
+ await click('interaction.ask.voice');
+ if (failure === 'stop') await click('interaction.ask.submitButton');
+ else await act(async () => fail());
+ expect(resolveInteraction).not.toHaveBeenCalled();
+ expect(host.querySelector<HTMLInputElement>('[data-testid="interaction.ask.textInput"]')!.value).toBe('保留文字');
+ expect(host.querySelector('[data-testid="interaction.ask.voiceError"]')!.textContent).toBe('识别失败');
+ await type('interaction.ask.textInput', '修改后的回答');
+ await click('interaction.ask.submitButton');
+ expect(resolveInteraction).toHaveBeenCalledOnce();
+ expect(JSON.stringify(resolveInteraction.mock.calls[0])).toContain('修改后的回答');
+});
+
// Unit regression for PR #6306 review: a signed-out microphone tap used to
// render hardcoded Chinese. Existing receipt tests never tapped this entry.
// Exercise the real card and hook in each supported locale, without auth.
diff --git a/apps/mobile/src/session/InteractionPanel.tsx b/apps/mobile/src/session/InteractionPanel.tsx
index f70157139e..0e8cb82450 100644
--- a/apps/mobile/src/session/InteractionPanel.tsx
+++ b/apps/mobile/src/session/InteractionPanel.tsx
@@ -990,13 +990,14 @@ type AskUserQuestionCardProps = {
touchLayout: InteractionTouchLayout;
};
-function AskCardVoiceButton({ listening, onPress }: { listening: boolean; onPress: () => void }) {
+function AskCardVoiceButton({ listening, disabled, onPress }: { listening: boolean; disabled?: boolean; onPress: () => void }) {
const { t } = useTranslation();
const { colors } = useTheme();
return (
<Pressable
accessibilityLabel={listening ? t('session.common.voiceStopRecording') : t('session.screen.voiceStartInput')}
accessibilityRole="button"
+ disabled={disabled}
hitSlop={8}
onPress={onPress}
style={{ alignItems: 'center', height: 44, justifyContent: 'center', width: 44 }}
@@ -1059,6 +1060,13 @@ function AskUserQuestionForm({
return selection;
},
});
+ const [voiceSubmitting, setVoiceSubmitting] = useState(false);
+ const voiceSubmitRef = useRef(false);
+ const mountedRef = useRef(true);
+ useEffect(() => {
+ mountedRef.current = true;
+ return () => { mountedRef.current = false; };
+ }, []);
const beginVoice = () => {
if (optionsLengthRef.current > 0 && !showCustomInputRef.current) setShowCustomInput(true);
voice.toggle();
@@ -1173,14 +1181,25 @@ function AskUserQuestionForm({
});
};
- const submitMulti = () => {
- if (!canSubmitMulti) return;
- advance(encodeMultiSelectAnswer(options, selectedLabels, customInput));
- };
-
- const submitSingle = () => {
- if (!canSubmitSingle || singleAnswer === undefined) return;
- advance(singleAnswer);
+ const submitAnswer = async () => {
+ if (busy || voiceSubmitRef.current) return;
+ voiceSubmitRef.current = true;
+ setVoiceSubmitting(voice.listening);
+ try {
+ if (voice.listening && !await voice.stop()) return;
+ if (!mountedRef.current) return;
+ const text = answerTextRef.current;
+ if (isMulti) {
+ if (selectedLabels.size === 0 && !text.trim()) return;
+ advance(encodeMultiSelectAnswer(options, selectedLabels, text));
+ } else {
+ const answer = customModeActive ? text.trim() : existingAnswer;
+ if (answer?.trim()) advance(answer);
+ }
+ } finally {
+ voiceSubmitRef.current = false;
+ if (mountedRef.current) setVoiceSubmitting(false);
+ }
};
const toggleLabel = (label: string) => {
@@ -1288,7 +1307,7 @@ function AskUserQuestionForm({
testID="interaction.ask.customInput"
value={customInput}
/>
- <AskCardVoiceButton listening={voice.listening} onPress={beginVoice} />
+ <AskCardVoiceButton listening={voice.listening} disabled={voice.stopping} onPress={beginVoice} />
</View>
) : (
<View style={[styles.customInputRow, { gap: touchLayout.actionGap }]}>
@@ -1315,7 +1334,7 @@ function AskUserQuestionForm({
>
<Text style={styles.optionCustom}>{t('interaction.panel.customAnswerButtonText')}</Text>
</InteractionTouchButton>
- <AskCardVoiceButton listening={voice.listening} onPress={beginVoice} />
+ <AskCardVoiceButton listening={voice.listening} disabled={voice.stopping} onPress={beginVoice} />
</View>
)}
</View>
@@ -1342,7 +1361,7 @@ function AskUserQuestionForm({
testID="interaction.ask.textInput"
value={customInput}
/>
- <AskCardVoiceButton listening={voice.listening} onPress={beginVoice} />
+ <AskCardVoiceButton listening={voice.listening} disabled={voice.stopping} onPress={beginVoice} />
</View>
)}
@@ -1392,10 +1411,10 @@ function AskUserQuestionForm({
<ResolveButton
accessibilityLabel={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}
+ disabled={voiceSubmitting}
+ invalidReason={!voice.listening && !canSubmitMulti ? t('interaction.panel.answerRequired') : null}
+ label={voiceSubmitting ? t('session.presentation.composer.voice.submitting') : isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submit') : t('interaction.panel.next')}
+ onPress={() => { void submitAnswer(); }}
requestId={requestId}
touchStyle={resolveButtonLayoutStyle(touchLayout, 'primary', companion)}
testID="interaction.ask.submitButton"
@@ -1405,10 +1424,10 @@ function AskUserQuestionForm({
<ResolveButton
accessibilityLabel={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}
+ disabled={voiceSubmitting}
+ invalidReason={!voice.listening && !canSubmitSingle ? t('interaction.panel.answerRequired') : null}
+ label={voiceSubmitting ? t('session.presentation.composer.voice.submitting') : isLast ? t(resumeOnSubmit ? 'interaction.panel.sendAndContinue' : 'interaction.panel.submit') : t('interaction.panel.next')}
+ onPress={() => { void submitAnswer(); }}
requestId={requestId}
touchStyle={resolveButtonLayoutStyle(touchLayout, 'primary', companion)}
testID="interaction.ask.submitButton"
diff --git a/apps/mobile/src/session/askCardVoiceStart.ts b/apps/mobile/src/session/askCardVoiceStart.ts
index 4f5f2e11eb..eb14168139 100644
--- a/apps/mobile/src/session/askCardVoiceStart.ts
+++ b/apps/mobile/src/session/askCardVoiceStart.ts
@@ -48,6 +48,7 @@ export async function startAskCardDictation(input: {
setText: (value: string) => void;
getSelection: () => MobileVoiceSelection;
onListening: () => void;
+ onRunCreated?: (run: { stop: () => Promise<void>; cancel: () => Promise<void> }) => void;
onError: (message: string) => void;
}): Promise<{ stop: () => Promise<void>; cancel: () => Promise<void> }> {
if (!isMobileRealtimeAudioAvailable()) {
@@ -111,17 +112,27 @@ export async function startAskCardDictation(input: {
onError: input.onError,
onReadyForEndCue: credential.settings?.playInteractionSound ? playMobileVoiceInputEndCue : undefined,
});
- await session.start();
+ const run = {
+ stop: () => session.stop().then(() => undefined),
+ cancel: () => session.cancel(),
+ };
+ if (input.signal?.aborted) throw new AskCardVoiceCancelledError();
+ // Publish the handle before awaiting the handshake. stop() closes native
+ // capture immediately and still flushes audio recorded before the press.
+ input.onRunCreated?.(run);
+ try {
+ await session.start();
+ } catch (error) {
+ await session.cancel().catch(() => undefined);
+ throw error;
+ }
// 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(),
- };
+ return run;
}
function waitUntilActive(
diff --git a/apps/mobile/src/session/useAskCardVoice.ts b/apps/mobile/src/session/useAskCardVoice.ts
index 4689081dae..867c56b855 100644
--- a/apps/mobile/src/session/useAskCardVoice.ts
+++ b/apps/mobile/src/session/useAskCardVoice.ts
@@ -23,15 +23,20 @@ export function useAskCardVoice(input: {
getSelection: () => MobileVoiceSelection;
}): {
listening: boolean;
+ stopping: boolean;
error: string | null;
toggle: () => void;
+ stop: () => Promise<boolean>;
} {
const auth = useAskCardAuth();
const [listening, setListening] = useState(false);
+ const [stopping, setStopping] = useState(false);
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 stoppingRef = useRef<Promise<boolean> | null>(null);
+ const failedRef = useRef(false);
+ const mountedRef = useRef(true);
const startAbortRef = useRef<AbortController | null>(null);
const inputRef = useRef(input);
inputRef.current = input;
@@ -39,30 +44,46 @@ export function useAskCardVoice(input: {
authRef.current = auth;
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;
+ if (stoppingRef.current) return stoppingRef.current;
const run = runRef.current;
- runRef.current = null;
- setListening(false);
- if (!run) return;
- const stopping = run.stop().catch(() => undefined).finally(() => {
- if (stoppingRef.current === stopping) stoppingRef.current = null;
+ const abort = startAbortRef.current;
+ if (!run) {
+ startingRef.current = false;
+ abort?.abort();
+ setListening(false);
+ return Promise.resolve(false);
+ }
+ setStopping(true);
+ const finishing = run.stop().then(() => !failedRef.current && !abort?.signal.aborted).catch((error: unknown) => {
+ if (mountedRef.current) setError(error instanceof Error ? error.message : String(error));
+ return false;
+ }).finally(() => {
+ if (runRef.current === run) runRef.current = null;
+ if (stoppingRef.current === finishing) stoppingRef.current = null;
+ startingRef.current = false;
+ abort?.abort();
+ if (mountedRef.current) {
+ setListening(false);
+ setStopping(false);
+ }
});
- stoppingRef.current = stopping;
+ stoppingRef.current = finishing;
+ return finishing;
}, []);
// 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);
+ useEffect(() => {
+ mountedRef.current = true;
+ return () => {
+ mountedRef.current = false;
+ startingRef.current = false;
+ startAbortRef.current?.abort();
+ startAbortRef.current = null;
+ const run = runRef.current;
+ runRef.current = null;
+ void run?.cancel().catch(() => undefined);
+ };
}, []);
const toggle = useCallback(() => {
@@ -80,13 +101,15 @@ export function useAskCardVoice(input: {
return;
}
startingRef.current = true;
+ failedRef.current = false;
+ setListening(true);
setError(null);
const abort = new AbortController();
startAbortRef.current = abort;
void (async () => {
try {
const { startAskCardDictation } = await import('@/session/askCardVoiceStart');
- if (!startingRef.current) return;
+ if (abort.signal.aborted) return;
const run = await startAskCardDictation({
deviceId,
signal: abort.signal,
@@ -94,37 +117,45 @@ export function useAskCardVoice(input: {
refreshAccessToken: () => currentAuth.refreshAccessToken(),
apiFetch: currentAuth.apiFetch,
getText: () => inputRef.current.getText(),
- setText: (value) => inputRef.current.setText(value),
+ setText: (value) => { if (!abort.signal.aborted) inputRef.current.setText(value); },
getSelection: () => inputRef.current.getSelection(),
onListening: () => {
- if (startingRef.current || runRef.current) setListening(true);
+ if (!abort.signal.aborted && !stoppingRef.current) setListening(true);
},
+ onRunCreated: (run) => { if (!abort.signal.aborted) runRef.current = run; },
onError: (message) => {
+ if (abort.signal.aborted) return;
+ failedRef.current = true;
startingRef.current = false;
- runRef.current = null;
- setListening(false);
setError(message);
+ if (!stoppingRef.current) {
+ const failedRun = runRef.current;
+ runRef.current = null;
+ abort.abort();
+ setListening(false);
+ void failedRun?.cancel().catch(() => undefined);
+ }
},
});
- if (!startingRef.current) {
+ if (abort.signal.aborted) {
await run.cancel();
return;
}
runRef.current = run;
startingRef.current = false;
- setListening(true);
+ if (!stoppingRef.current) setListening(true);
} catch (error) {
+ if (abort.signal.aborted) return;
startingRef.current = false;
+ runRef.current = null;
setListening(false);
// 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]);
- return { listening, error, toggle };
+ return { listening, stopping, error, toggle, stop };
}