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: 锁住提问卡语音收尾导航并保留按钮加载样式

zcinta051436fe49ad5d6f
diff --git a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
index f4b80f7573..2227c0b45b 100644
--- a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx
@@ -13,6 +13,7 @@ import type { VoiceQuestionBridge } from '../components/new-chat/voiceQuestionSe
const start = vi.fn(() => true);
const stop = vi.fn(async () => true);
let finalTranscript = '完整回答';
+let idleBeforeStop = false;
// Stateful stand-in for the dictation hook: starting flips the card into the
// busy state, which is what disables question navigation.
@@ -30,6 +31,7 @@ vi.mock('../components/new-chat/useAskUserDictation', () => ({
return true;
},
stop: async () => {
+ if (idleBeforeStop) setState('idle');
const ok = await stop();
if (ok) target.setText(finalTranscript);
setState('idle');
@@ -43,6 +45,7 @@ beforeEach(async () => {
start.mockClear();
stop.mockReset().mockResolvedValue(true);
finalTranscript = '完整回答';
+ idleBeforeStop = false;
await i18n.changeLanguage('zh-CN');
});
@@ -257,6 +260,24 @@ describe('AskUserQuestion voice entry', () => {
expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '补充一句': '完整回答' });
});
+ it('keeps navigation locked after done until stop finishes restoring audio', async () => {
+ idleBeforeStop = true;
+ let finish!: (ok: boolean) => void;
+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));
+ const { view, onAnswer } = renderAsk([{ question: '选哪些?', multiSelect: true, options: [{ label: '甲' }, { label: '乙' }] }]);
+ fireEvent.click(view.getByTestId('ask-user-voice'));
+ fireEvent.click(view.getByText('提交'));
+ const submit = view.getByRole('button', { name: '提交' });
+ expect(submit.getAttribute('aria-busy')).toBe('true');
+ expect((view.getByText('跳过').closest('button') as HTMLButtonElement).disabled).toBe(true);
+ fireEvent.click(view.getByText('乙'));
+ fireEvent.keyDown(window, { key: 'Escape' });
+ fireEvent.keyDown(window, { key: '2' });
+ expect(onAnswer).not.toHaveBeenCalled();
+ await act(async () => finish(true));
+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '选哪些?': JSON.stringify(['完整回答']) });
+ });
+
it('keeps the answer editable instead of submitting after a failed stop', async () => {
stop.mockResolvedValueOnce(false);
const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);
diff --git a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
index c60d8a9961..bf5b482ed2 100644
--- a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx
@@ -243,6 +243,7 @@ function AskUserQuestionForm({
dictationRef.current = dictation;
const [voiceSubmitting, setVoiceSubmitting] = useState(false);
const voiceSubmitRef = useRef(false);
+ const voiceLocked = dictation.isBusy || voiceSubmitting;
const mountedRef = useRef(true);
useEffect(() => {
mountedRef.current = true;
@@ -425,6 +426,7 @@ function AskUserQuestionForm({
// ── Single-select: click option -> advance ──
const handleSingleSelect = useCallback(
(label: string) => {
+ if (voiceSubmitRef.current) return;
markStarted();
advance(label);
},
@@ -433,6 +435,7 @@ function AskUserQuestionForm({
// ── Multi-select: toggle checkbox ──
const handleToggle = useCallback((label: string) => {
+ if (voiceSubmitRef.current) return;
markStarted();
setSelectedLabels((prev) => {
const next = new Set(prev);
@@ -475,7 +478,7 @@ function AskUserQuestionForm({
// ── Back ──
const handleBack = useCallback(() => {
- if (currentIndex === 0) return;
+ if (voiceSubmitRef.current || currentIndex === 0) return;
++voiceRevision.current;
setVoiceFeedback('hint');
snapshotButtons();
@@ -497,11 +500,13 @@ function AskUserQuestionForm({
// ── Skip ──
const handleSkip = useCallback(() => {
+ if (voiceSubmitRef.current) return;
advance('');
}, [advance]);
// ── Custom input toggle ──
const handleCustomOptionClick = useCallback(() => {
+ if (voiceSubmitRef.current) return;
setShowCustomInput(true);
setTimeout(() => inputRef.current?.focus(), 0);
}, []);
@@ -621,7 +626,7 @@ function AskUserQuestionForm({
}, [voiceBridge, currentIndex, voiceRevision.current, selectedLabels, customInput, showCustomInput, voiceFeedback]);
useLayoutEffect(() => {
voiceHandler.current = (text) => {
- if (isAnimating || referencePreviewOpen || !currentQ || dictationRef.current.isBusy) return;
+ if (isAnimating || referencePreviewOpen || !currentQ || dictationRef.current.isBusy || voiceSubmitRef.current) return;
onViewerStateChange('expanded');
const result = parseVoiceQuestionSelection(text, options.map((option) => option.label));
if (result.kind === 'cancel') {
@@ -660,12 +665,12 @@ function AskUserQuestionForm({
};
});
useLayoutEffect(() => {
- if (!voiceBridge || isAnimating || referencePreviewOpen || dictation.isBusy) return;
+ if (!voiceBridge || isAnimating || referencePreviewOpen || voiceLocked) return;
voiceBridge.current = voiceTarget;
return () => {
if (voiceBridge.current === voiceTarget) voiceBridge.current = null;
};
- }, [voiceBridge, voiceTarget, isAnimating, referencePreviewOpen, dictation.isBusy]);
+ }, [voiceBridge, voiceTarget, isAnimating, referencePreviewOpen, voiceLocked]);
const voiceNotice = voiceBridge ? (
<div className="flex flex-wrap items-center gap-2 text-13 leading-5 text-[var(--text-secondary)]">
@@ -691,7 +696,7 @@ function AskUserQuestionForm({
// 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 (dictationRef.current.isBusy || voiceSubmitRef.current) return;
// If custom input is focused in single-select mode, handle Escape only
if (showCustomInput && !isMultiSelect) {
@@ -764,7 +769,7 @@ function AskUserQuestionForm({
<div className="flex gap-2.5">
{pending.delivery === 'async' && startedRef.current && (
<Button variant="secondary" palette="confirmation" size="lg" compact
- type="button" onClick={() => onAnswer(requestId, {})}>
+ type="button" disabled={voiceLocked} onClick={() => { if (!voiceSubmitRef.current) onAnswer(requestId, {}); }}>
{t('chat.askUserQuestion.close')}
</Button>
)}
@@ -779,7 +784,7 @@ function AskUserQuestionForm({
size="lg"
compact
type="button"
- disabled={dictation.isBusy}
+ disabled={voiceLocked}
onClick={handleBack}
>
<span className="flex items-center gap-1.5">
@@ -795,7 +800,7 @@ function AskUserQuestionForm({
size="lg"
compact
type="button"
- disabled={dictation.isBusy}
+ disabled={voiceLocked}
onClick={handleSkip}
>
{t('chat.askUserQuestion.skip')}
@@ -809,7 +814,9 @@ function AskUserQuestionForm({
size="lg"
compact
type="button"
+ loading={voiceSubmitting}
onClick={() => {
+ if (voiceSubmitRef.current) return;
if (isMultiSelect) {
handleNext();
} else {
@@ -823,7 +830,7 @@ function AskUserQuestionForm({
: dictation.isBusy || existingAnswer === undefined)
}
>
- {voiceSubmitting ? voiceSubmitLabel : isLastQuestion ? t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit') : t('chat.askUserQuestion.next')}
+ {isLastQuestion ? t(pending.resumeOnSubmit ? 'chat.askUserQuestion.sendAndContinue' : 'chat.askUserQuestion.submit') : t('chat.askUserQuestion.next')}
</Button>
)}
</>
@@ -869,7 +876,7 @@ function AskUserQuestionForm({
<AutoGrowingAnswerTextarea
textareaRef={inputRef}
value={answerValue}
- readOnly={dictation.isBusy}
+ readOnly={voiceLocked}
onCompositionStart={markStarted}
onChange={(e) => { markStarted(); setAnswerText(e.target.value); }}
onKeyDown={(e) => {
@@ -881,7 +888,7 @@ function AskUserQuestionForm({
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;
+ if (voiceSubmitRef.current || dictation.isBusy) return;
e.preventDefault();
setShowCustomInput(false);
setAnswerText('');
@@ -944,6 +951,7 @@ function AskUserQuestionForm({
'flex min-w-0 flex-1 items-center justify-between py-[6px] text-left',
'transition-colors hover:bg-[var(--ask-option-hover)]',
)}
+ disabled={voiceLocked}
onClick={handleCustomOptionClick}
>
<div className="flex items-center gap-3">
@@ -958,7 +966,7 @@ function AskUserQuestionForm({
{options.length + 1}
</div>
</button>
- {!voiceBridge && (<AskUserVoiceButton label={voiceButtonLabel} busy={dictation.isBusy} onClick={beginVoiceInput} />)}
+ {!voiceBridge && (<AskUserVoiceButton label={voiceButtonLabel} busy={dictation.isBusy} disabled={voiceSubmitting} onClick={beginVoiceInput} />)}
</div>
)}
</div>
@@ -1019,7 +1027,7 @@ function AskUserQuestionForm({
{idx > 0 && <div className="h-px bg-[var(--ask-option-divider)]" />}
<button
type="button"
- disabled={dictation.isBusy}
+ disabled={voiceLocked}
className={cn(
'flex w-full items-center justify-between px-[16px] py-[14px] text-left',
'transition-colors hover:bg-[var(--ask-option-hover)]',
@@ -1085,7 +1093,7 @@ function AskUserQuestionForm({
<AutoGrowingAnswerTextarea
textareaRef={inputRef}
value={answerValue}
- readOnly={dictation.isBusy}
+ readOnly={voiceLocked}
onCompositionStart={markStarted}
onChange={(e) => { markStarted(); setAnswerText(e.target.value); }}
onKeyDown={(e) => {
@@ -1094,7 +1102,7 @@ function AskUserQuestionForm({
submitDirectAnswer();
}
if (e.key === 'Escape') {
- if (dictation.isBusy) return;
+ if (voiceSubmitRef.current || dictation.isBusy) return;
e.preventDefault();
handleSkip();
}