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): 语音录音时停止按钮改到语音键左边,消除位置跳变 (#374)

Dash14867addeb9a
diff --git a/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts b/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts
new file mode 100644
index 0000000000..11cfde4d71
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/voiceInputButtonAnchor.test.ts
@@ -0,0 +1,74 @@
+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');
+
+/**
+ * composer 右下按钮组右对齐,所以「语音按钮与最右主槽之间夹着几个按钮」直接决定语音按钮
+ * 自己的屏幕位置。次槽 Stop 只在 streaming 时出现,若它夹在语音与 Send 之间,语音按钮就会
+ * 随 streaming / 草稿 / 录音状态整格横跳,而它让出的位置正好被 Stop 占据 —— 用户点完语音想
+ * 「原地再点一下停止录音」会误停正在跑的任务。
+ * 约束:次槽 Stop 一律在语音按钮左边,语音按钮永远是主槽的左邻。
+ */
+describe('ChatInput voice button anchor contract', () => {
+ // 从次槽 Stop 起、到主槽 sendButtonRef 止,即右下按钮组里语音按钮所在的区间。
+ const buttonGroupBlock = extractBetween(
+ chatInputSource,
+ '{showSecondaryStop && (',
+ '<span ref={sendButtonRef}',
+ );
+
+ it('renders the secondary stop button to the left of the voice input button', () => {
+ const secondaryStopIndex = buttonGroupBlock.indexOf('<SendButton');
+ const voiceButtonIndex = buttonGroupBlock.indexOf('<VoiceInputButton');
+
+ expect(secondaryStopIndex).toBeGreaterThanOrEqual(0);
+ expect(voiceButtonIndex).toBeGreaterThan(secondaryStopIndex);
+ });
+
+ it('keeps the voice input button as the immediate left neighbour of the main send slot', () => {
+ const afterVoiceButton = buttonGroupBlock.slice(
+ buttonGroupBlock.indexOf('<VoiceInputButton'),
+ );
+
+ // 语音按钮与主槽之间不得再插入任何 Send/Stop 控件。
+ expect(afterVoiceButton).not.toContain('<SendButton');
+ });
+
+ it('does not fork the secondary stop placement on listening state', () => {
+ // 只在录音态挪 Stop 是不够的:录音结束且草稿非空时 Stop 会跳回右边,语音按钮同样
+ // 左移一格,只是把误点风险推迟到「刚点完停止录音」那一刻。
+ expect(chatInputSource).toContain(
+ 'const showSecondaryStop =\n showStopButton && (canSend || voiceInput.isBusy) && !sendDispatchInFlight;',
+ );
+ expect(chatInputSource).toContain(
+ 'const mainSlotIsStop =\n showStopButton && (sendDispatchInFlight || (!canSend && !voiceInput.isBusy));',
+ );
+ });
+
+ it('keeps the slot assignment stable across the whole voice lifecycle', () => {
+ // 停止录音 → submitting/refining → 草稿落地之间 canSend 仍为 false;若按 isListening
+ // 判定,这一段会退化成 streaming idle,Stop 弹回主槽再弹走,肉眼可见闪一下。
+ // isBusy = listening || submitting || refining,覆盖整段生命周期。
+ const slotDecisionBlock = extractBetween(
+ chatInputSource,
+ 'const mainSlotIsStop =',
+ 'useEffect(() => {\n voiceInputCanStopAndSendRef.current = !sendButtonDisabled;',
+ );
+
+ expect(slotDecisionBlock).not.toContain('voiceInput.isListening');
+ expect(slotDecisionBlock).toContain('voiceInput.isBusy');
+ });
+});
+
+function extractBetween(sourceBlock: string, startNeedle: string, endNeedle: string): string {
+ const start = sourceBlock.indexOf(startNeedle);
+ const end = sourceBlock.indexOf(endNeedle, start);
+ expect(start).toBeGreaterThanOrEqual(0);
+ expect(end).toBeGreaterThan(start);
+ return sourceBlock.slice(start, end);
+}
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 92c829b5c8..bcd7ecb013 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -4814,6 +4814,34 @@ export function ChatInput({
voiceInput.state === 'submitting' ||
voiceInput.state === 'refining',
);
+ // Send / Stop 双槽语义 (voice busy = voiceInput.isBusy = listening|submitting|refining,
+ // 判定为何用 isBusy 而不是 isListening 见下方第三段):
+ // - 主槽 (最右, 永远占位, sendButtonRef 钉在这里):
+ // · 发送瞬间 (inflight=true) → Stop (Send 原位被替换, 不抖左侧 layout)
+ // · streaming idle (无内容 且 无 voice busy) → Stop (取代 Send 占主槽)
+ // · 其它 (idle / streaming+canSend / voice busy) → Send
+ // - 次槽 (语音按钮左边, 即本组最左; 仅 streaming+(canSend||voice busy)+!inflight 时出现): Stop
+ // 设计意图: Send 是最显眼的主行动按钮, 应当永远在最右; streaming idle 时由 Stop 顶替
+ // Send 主槽 (原位替换, 不抖); streaming 中用户输入下一条要送入 PendingQueue 时, Send 回
+ // 到主槽, Stop 退到次槽. sendDispatchInFlight 锁次槽, 避免 send 瞬间主槽 Send→Stop 切换
+ // 的同帧再多出一个 Stop 把模型选择推一下又复位的 bug.
+ //
+ // 次槽必须在语音按钮**左边**, 不能夹在语音与 Send 之间 (2026-07-25): 本组右对齐, 语音
+ // 按钮永远紧邻主槽左侧, 于是它的右边缘恒等于「容器右 - 主槽宽 - gap」, 与是否 streaming /
+ // 是否有草稿 / 是否录音全部无关 —— 录音时计时胶囊只向左生长, 原来的麦克风命中点始终留在
+ // 按钮内, 用户可以"原地再点一下"停止录音. 若把 Stop 塞进语音与 Send 之间, 语音按钮会随
+ // Stop 的出现/消失整格横跳, 而它让出的位置正好被 Stop 占据, 原地再点一下就会误停任务.
+ //
+ // 槽位判定用 isBusy 而不是 isListening (2026-07-25): 停止录音的瞬间 state 就离开
+ // listening 进入 submitting/refining, 但转写文本要等润色完才落进草稿, 中间这一小段
+ // canSend 仍是 false —— 若按 isListening 判定, 这一帧会退化成"streaming idle", Stop 弹
+ // 回主槽、Send 消失, 等草稿落地再弹回来, 肉眼可见闪一下. isBusy 覆盖整个语音生命周期
+ // (listening + submitting + refining), 让槽位从开录到润色结束保持不变; 润色期间主槽是
+ // 禁用态 Send (见 sendButtonDisabled), 停止任务的能力由次槽 Stop 承担.
+ const mainSlotIsStop =
+ showStopButton && (sendDispatchInFlight || (!canSend && !voiceInput.isBusy));
+ const showSecondaryStop =
+ showStopButton && (canSend || voiceInput.isBusy) && !sendDispatchInFlight;
useEffect(() => {
voiceInputCanStopAndSendRef.current = !sendButtonDisabled;
composerCanSubmitRef.current = !sendButtonDisabled;
@@ -5370,6 +5398,16 @@ export function ChatInput({
: 'flex items-center gap-2'
}
>
+ {/* 次槽 Stop 必须在语音按钮左边 —— 见 showSecondaryStop 定义处的
+ 双槽语义与"语音按钮位置守恒"说明, 不要挪到语音与 Send 之间。 */}
+ {showSecondaryStop && (
+ <SendButton
+ disabled={false}
+ onClick={onStop ?? (() => {})}
+ isStreaming
+ visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
+ />
+ )}
<VoiceInputButton
state={voiceInput.state}
disabled={!!disabled || !editor}
@@ -5384,87 +5422,55 @@ export function ChatInput({
visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
className={isCreateAgentVariant && !useNarrowToolbar ? 'ml-[7px]' : undefined}
/>
- {/* Send / Stop 双槽语义:
- - 主槽 (最右, 永远占位, sendButtonRef 钉在这里):
- · 发送瞬间 (inflight=true) → Stop (Send 原位被替换, 不抖左侧 layout)
- · streaming idle (无内容 / 无 voice) → Stop (取代 Send 占主槽)
- · 其它 (idle / streaming+canSend / voice listening) → Send
- - 次槽 (主槽左边, 仅 streaming+(canSend||listening)+!inflight 时出现): Stop
- 设计意图: Send 是最显眼的主行动按钮, 应当永远在最右; streaming
- idle 时由 Stop 顶替 Send 主槽 (原位替换, 不抖); streaming 中用户输入
- 下一条要送入 PendingQueue 时, Send 回到主槽, Stop 退到次槽 (Send 在
- Stop 右边). dispatchSendInFlight 锁次槽, 避免 send 瞬间主槽
- Send→Stop 切换的同帧再多出一个 Stop 把模型选择推一下又复位的 bug. */}
- {(() => {
- const mainSlotIsStop =
- showStopButton &&
- (sendDispatchInFlight || (!canSend && !voiceInput.isListening));
- const showSecondaryStop =
- showStopButton &&
- (canSend || voiceInput.isListening) &&
- !sendDispatchInFlight;
- return (
- <>
- {showSecondaryStop && (
+ <span ref={sendButtonRef} className="inline-flex rounded-full">
+ {mainSlotIsStop ? (
+ <SendButton
+ disabled={false}
+ onClick={onStop ?? (() => {})}
+ isStreaming
+ visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
+ />
+ ) : (
+ <Tip
+ text={
+ voiceReleaseToSendActive
+ ? t('newChat.chatInput.voiceInput.releaseToSend')
+ : voiceInput.isListening && !sendButtonDisabled
+ ? `${t('newChat.chatInput.voiceInput.finishAndSend')} · Enter`
+ : showStopButton
+ ? t('newChat.sendButton.queueTooltip', {
+ shortcut: steerShortcutLabel,
+ })
+ : !sendButtonDisabled
+ ? `${t('newChat.sendButton.send')} · Enter`
+ : selectedSourceDisconnected
+ ? t('newChat.sourceDisconnected.sendBlocked')
+ : null
+ }
+ side="top"
+ forceOpen={voiceReleaseToSendActive}
+ >
+ {/* Tip 的 trigger 放在稳定 wrapper 上,而不是 button 本身。
+ disabled button 不会可靠地产生 hover/focus 事件;曾经因此让
+ running 时“排队/快捷键插话”提示完全不出现。 */}
+ <span className="inline-flex rounded-full">
<SendButton
- disabled={false}
- onClick={onStop ?? (() => {})}
- isStreaming
+ disabled={sendButtonDisabled}
+ highlighted={voiceReleaseToSendActive}
visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
+ ariaLabel={
+ showStopButton
+ ? t('newChat.sendButton.queue')
+ : t('newChat.sendButton.send')
+ }
+ onClick={() => {
+ void handleClickSend();
+ }}
/>
- )}
- <span ref={sendButtonRef} className="inline-flex rounded-full">
- {mainSlotIsStop ? (
- <SendButton
- disabled={false}
- onClick={onStop ?? (() => {})}
- isStreaming
- visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
- />
- ) : (
- <Tip
- text={
- voiceReleaseToSendActive
- ? t('newChat.chatInput.voiceInput.releaseToSend')
- : voiceInput.isListening && !sendButtonDisabled
- ? `${t('newChat.chatInput.voiceInput.finishAndSend')} · Enter`
- : showStopButton
- ? t('newChat.sendButton.queueTooltip', {
- shortcut: steerShortcutLabel,
- })
- : !sendButtonDisabled
- ? `${t('newChat.sendButton.send')} · Enter`
- : selectedSourceDisconnected
- ? t('newChat.sourceDisconnected.sendBlocked')
- : null
- }
- side="top"
- forceOpen={voiceReleaseToSendActive}
- >
- {/* Tip 的 trigger 放在稳定 wrapper 上,而不是 button 本身。
- disabled button 不会可靠地产生 hover/focus 事件;曾经因此让
- running 时“排队/快捷键插话”提示完全不出现。 */}
- <span className="inline-flex rounded-full">
- <SendButton
- disabled={sendButtonDisabled}
- highlighted={voiceReleaseToSendActive}
- visualVariant={isCreateAgentVariant ? 'create-agent' : 'default'}
- ariaLabel={
- showStopButton
- ? t('newChat.sendButton.queue')
- : t('newChat.sendButton.send')
- }
- onClick={() => {
- void handleClickSend();
- }}
- />
- </span>
- </Tip>
- )}
</span>
- </>
- );
- })()}
+ </Tip>
+ )}
+ </span>
</div>
</div>
</div>