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(desktop): 修复新建对话语音输入无法停止 (#1663)

Dash50f5efdffc23
diff --git a/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts b/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts
index 11cfde4d71..10f4bfd1da 100644
--- a/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts
+++ b/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts
@@ -63,6 +63,15 @@ describe('ChatInput voice button anchor contract', () => {
expect(slotDecisionBlock).not.toContain('voiceInput.isListening');
expect(slotDecisionBlock).toContain('voiceInput.isBusy');
});
+
+ it('keeps the stop controls enabled while voice input owns the editor lock', () => {
+ // The surrounding voice lifecycle lock must not disable the button or
+ // shortcut that is needed to end the active recording.
+ expect(chatInputSource).toContain('const disabledRef = useRef(composerEditorLocked);');
+ expect(chatInputSource).toContain('disabledRef.current = composerEditorLocked;');
+ expect(chatInputSource).toContain('disabled={composerEditorLocked || !editor}');
+ expect(chatInputSource).not.toContain('disabled={composerMutationLocked || !editor}');
+ });
});
function extractBetween(sourceBlock: string, startNeedle: string, endNeedle: string): string {
diff --git a/apps/desktop/src/renderer/__tests__/voiceInputEditorEditability.test.ts b/apps/desktop/src/renderer/__tests__/voiceInputEditorEditability.test.ts
new file mode 100644
index 0000000000..a5caecb725
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/voiceInputEditorEditability.test.ts
@@ -0,0 +1,58 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+
+import { describe, expect, it } from 'vitest';
+
+const chatInputSource = readFileSync(
+ resolve(__dirname, '..', 'components', 'new-chat', 'ChatInput.tsx'),
+ 'utf8',
+).replace(/\r\n?/g, '\n');
+
+describe('ChatInput voice lifecycle locks', () => {
+ it('keeps the editor read-only for the entire voice lifecycle', () => {
+ expect(chatInputSource).toContain(
+ 'const composerMutationLocked = composerEditorLocked || voiceInput.isBusy;',
+ );
+ expect(chatInputSource).toContain('editor?.setEditable(!composerMutationLocked);');
+ expect(chatInputSource).toContain('if (composerMutationLockedRef.current) return true;');
+ expect(chatInputSource).toContain('active={voiceInput.isBusy}');
+ });
+
+ it('keeps attachments locked while leaving permission mode available', () => {
+ const extraDirsStart = chatInputSource.indexOf('<ExtraDirsButton');
+ expect(extraDirsStart).toBeGreaterThanOrEqual(0);
+ const extraDirsEnd = chatInputSource.indexOf('/>', extraDirsStart);
+ expect(extraDirsEnd).toBeGreaterThan(extraDirsStart);
+ const extraDirsBlock = chatInputSource.slice(extraDirsStart, extraDirsEnd);
+ expect(extraDirsBlock).toContain('disabled={composerMutationLocked}');
+
+ const permissionStart = chatInputSource.indexOf('<PermissionSelector');
+ expect(permissionStart).toBeGreaterThanOrEqual(0);
+ const permissionEnd = chatInputSource.indexOf('/>', permissionStart);
+ expect(permissionEnd).toBeGreaterThan(permissionStart);
+ const permissionBlock = chatInputSource.slice(permissionStart, permissionEnd);
+ expect(permissionBlock).toContain('disabled={composerEditorLocked}');
+ expect(permissionBlock).not.toContain('disabled={composerMutationLocked}');
+ });
+
+ it('lets top-level permission surfaces consume Escape before voice cancellation', () => {
+ const shortcutHandlerStart = chatInputSource.indexOf(
+ 'const handleKeyDown = (event: KeyboardEvent) => {',
+ );
+ expect(shortcutHandlerStart).toBeGreaterThanOrEqual(0);
+ const shortcutHandlerEnd = chatInputSource.indexOf(
+ 'const enterIntent = resolveComposerEnterIntent(',
+ shortcutHandlerStart,
+ );
+ expect(shortcutHandlerEnd).toBeGreaterThan(shortcutHandlerStart);
+ const shortcutHandlerBlock = chatInputSource.slice(shortcutHandlerStart, shortcutHandlerEnd);
+
+ expect(shortcutHandlerBlock).toContain('[role="alertdialog"]');
+ expect(shortcutHandlerBlock).toContain('[data-morph-side]');
+ const voiceCancelStart = shortcutHandlerBlock.indexOf('voiceInputCancelRef.current()');
+ expect(voiceCancelStart).toBeGreaterThanOrEqual(0);
+ expect(shortcutHandlerBlock.indexOf('[data-morph-side]')).toBeLessThan(
+ voiceCancelStart,
+ );
+ });
+});
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 308ec94c86..336a731fe6 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -2482,7 +2482,10 @@ export function ChatInput({
const voiceInputCanStopAndSendRef = useRef(false);
const composerCanSubmitRef = useRef(false);
const handleVoiceInputStartRef = useRef(handleVoiceInputStart);
- const disabledRef = useRef(composerMutationLocked);
+ // The voice lifecycle locks surrounding composer mutations while listening,
+ // but that must not disable the shortcut that stops the active recording.
+ // Keep this ref on the external composer lock only (disabled / send preflight).
+ const disabledRef = useRef(composerEditorLocked);
const disableAutofocusRef = useRef(disableAutofocus);
const focusOnStorageKeyChangeRef = useRef(focusOnStorageKeyChange);
const latestStorageKeyRef = useRef<string | undefined>(storageKey);
@@ -2512,11 +2515,11 @@ export function ChatInput({
voiceInputStopRef.current = handleVoiceInputStopWithRefinement;
voiceInputCancelRef.current = voiceInput.cancel;
handleVoiceInputStartRef.current = handleVoiceInputStart;
- disabledRef.current = composerMutationLocked;
+ disabledRef.current = composerEditorLocked;
disableAutofocusRef.current = disableAutofocus;
focusOnStorageKeyChangeRef.current = focusOnStorageKeyChange;
}, [
- composerMutationLocked,
+ composerEditorLocked,
disableAutofocus,
focusOnStorageKeyChange,
handleVoiceInputStart,
@@ -2548,6 +2551,15 @@ export function ChatInput({
const handleKeyDown = (event: KeyboardEvent) => {
const currentState = voiceInputStateRef.current;
+ // 权限菜单 / 确认框是当前顶层交互面,应先消费 Esc;它们的 capture handler
+ // 注册在 document 或组件层,晚于本 window capture handler 执行。
+ if (
+ event.key === 'Escape' &&
+ event.target instanceof Element &&
+ event.target.closest('[role="alertdialog"], [data-morph-side]')
+ ) {
+ return;
+ }
const platform = window.electronAPI?.platform;
if (event.key === 'Escape' && !event.repeat && !event.isComposing) {
if (
@@ -5904,7 +5916,7 @@ export function ChatInput({
onPermissionModeChange={handlePermissionModeChange}
vendorKey={vendorKey}
deviceId={deviceLinkDeviceId}
- disabled={composerMutationLocked}
+ disabled={composerEditorLocked}
dense={effectiveDenseToolbar}
iconOnly={useUltraCompactToolbar}
visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
@@ -6011,7 +6023,9 @@ export function ChatInput({
)}
<VoiceInputButton
state={voiceInput.state}
- disabled={composerMutationLocked || !editor}
+ // The surrounding controls stay locked during voice input, but
+ // this control must remain enabled so the recording can stop.
+ disabled={composerEditorLocked || !editor}
shortcutLabel={voiceInputShortcutLabel}
onStart={handleVoiceInputStart}
onStop={handleVoiceInputPlainStop}