Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
feat(voice): 默认开启输入框长按语音并提前准备麦克风
Dash98ce549cfb50
diff --git a/apps/desktop/scripts/fixtures/companion-composer/ui-page.mjs b/apps/desktop/scripts/fixtures/companion-composer/ui-page.mjsindex eb02ff1997..18a7bcacc4 100644--- a/apps/desktop/scripts/fixtures/companion-composer/ui-page.mjs+++ b/apps/desktop/scripts/fixtures/companion-composer/ui-page.mjs@@ -24,9 +24,13 @@ export default {'src/renderer/components/new-chat/NewGoalDialog': `export const NewGoalDialog = () => null;`,'src/renderer/components/new-chat/AtMentionPanel': `export const AtMentionPanel = () => null;`,'src/renderer/components/new-chat/SlashCommandPalette': `export const SlashCommandPalette = () => null;`,- 'src/renderer/voice-input/useVoiceInput': `import { useState } from 'react'; export function useVoiceInput(editor) { const [state, setState] = useState('idle'); return { state, isListening: state === 'listening', isBusy: state !== 'idle', draftText: '', start: () => { setState('listening'); return true; }, stop: async () => { setState('idle'); editor?.commands.setContent('<p>Dictated message</p>'); }, cancel: async () => setState('idle') }; }`,+ 'src/renderer/voice-input/useVoiceInput': `import { useState, useRef } from 'react'; export function useVoiceInput(editor) { const [state, setState] = useState('idle'); const current = useRef(state); const update = value => { current.current = value; setState(value); }; return { state, getState: () => current.current, isListening: state === 'listening', isBusy: state !== 'idle', draftText: '', start: () => { update('listening'); return true; }, stop: async () => { update('idle'); editor?.commands.setContent('<p>Dictated message</p>'); }, cancel: async () => update('idle') }; }`,},cases: {+ 'voice-hold': {+ about: 'Empty composer hints, native mouse selection during hold, and button hold/click release semantics',+ viewport: { width: 360, height: 400 },+ },default: {about:'Empty/text/attachment action switch, stable target, keyboard send, call return and stop/queue',@@ -40,6 +44,97 @@ export default {await page.locator('[contenteditable="true"]').waitFor();},async check(page, ui) {+ if (ui.case === 'voice-hold') {+ const editor = page.locator('.ProseMirror');+ const hint = page.getByTestId('composer-voice-hint');+ const hintText = async (key) => {+ await hint.waitFor();+ assert.equal(await hint.textContent(), await ui.t(`newChat.chatInput.voiceInput.${key}`));+ };+ const card = page.locator('[data-split-group-composer-drop-target]');+ const cardBox = await card.boundingBox();+ // Start in the card's padding, outside the single text line.+ await page.mouse.move(cardBox.x + 4, cardBox.y + 4);+ await hintText('holdComposer');+ await ui.capture('hover');+ await page.mouse.down();+ await page.waitForFunction(() => document.querySelector('[data-voice-holding="true"]'));+ await hintText('speakRelease');+ await ui.capture('hold-empty');+ await page.mouse.up();+ await page.waitForFunction(() => document.querySelector('.ProseMirror')?.textContent === 'Dictated message');+ assert.equal(await hint.count(), 0);++ // The gap above the toolbar is also part of the input surface.+ const editorBox = await editor.boundingBox();+ await page.mouse.move(cardBox.x + cardBox.width / 2, editorBox.y + editorBox.height + 5);+ await page.mouse.down();+ await page.waitForFunction(() => document.querySelector('[data-voice-holding="true"]'));+ await page.mouse.up();+ await page.locator('.ProseMirror[contenteditable="true"]').waitFor();++ const rect = await editor.boundingBox();+ const middle = await editor.evaluate((node) => {+ const text = node.querySelector('p').firstChild;+ const range = document.createRange();+ range.setStart(text, 8);+ range.setEnd(text, 9);+ const bounds = range.getBoundingClientRect();+ return { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height / 2 };+ });+ await page.mouse.move(middle.x, middle.y);+ await page.mouse.down();+ await page.waitForFunction(() => document.querySelector('[data-voice-holding="true"]'));+ await page.mouse.move(rect.x + rect.width - 8, rect.y + rect.height / 2, { steps: 12 });+ assert.equal(await page.evaluate(() => window.getSelection()?.toString()), '');+ await page.mouse.move(rect.x + 2, middle.y, { steps: 12 });+ assert.equal(await page.evaluate(() => window.getSelection()?.toString()), '');+ // Exercise selection changes that escape move/selectstart cancellation+ // (an already-running native selection gesture). The editor must retain+ // the insertion point, not adopt the expanded DOM selection.+ const insertionPoint = await editor.evaluate((node) => node.editor.state.selection.from);+ await editor.evaluate((node) => {+ const text = node.querySelector('p').firstChild;+ document.getSelection().setBaseAndExtent(text, 2, text, 5);+ });+ await page.waitForFunction(() => document.getSelection()?.isCollapsed);+ assert.equal(await editor.evaluate((node) => node.editor.state.selection.from), insertionPoint);+ assert.equal(await hint.count(), 0);+ await page.mouse.up();+ await page.locator('.ProseMirror[contenteditable="true"]').waitFor();+ assert.equal(await page.evaluate(() => window.getSelection()?.toString()), '');+ // A queued native selection change can arrive after pointerup/mouseup.+ // It must still be rejected until a new user interaction begins.+ await editor.evaluate((node) => {+ const text = node.querySelector('p').firstChild;+ document.getSelection().setBaseAndExtent(text, 2, text, 5);+ });+ await page.waitForFunction(() => document.getSelection()?.isCollapsed);+ assert.equal(await editor.evaluate((node) => node.editor.state.selection.empty), true);+ // Releasing restores ordinary text selection.+ const releasedText = await editor.locator('p').boundingBox();+ await page.mouse.move(releasedText.x + 5, releasedText.y + releasedText.height / 2);+ await page.mouse.down();+ await page.mouse.move(releasedText.x + 90, releasedText.y + releasedText.height / 2, { steps: 4 });+ await page.mouse.up();+ assert.notEqual(await page.evaluate(() => window.getSelection()?.toString()), '');+ await editor.fill('');+ const mic = page.locator('button[aria-pressed]');+ await mic.click();+ await hintText('speakPressAgain');+ await ui.capture('click-empty');+ await mic.click();+ await page.locator('.ProseMirror[contenteditable="true"]').waitFor();+ await editor.fill('');+ await mic.hover();+ await page.mouse.down();+ await page.getByRole('button', { name: await ui.t('newChat.chatInput.voiceInput.releaseToStop'), exact: true }).waitFor();+ await hintText('speakRelease');+ await ui.capture('button-hold-empty');+ await page.mouse.up();+ await page.locator('.ProseMirror[contenteditable="true"]').waitFor();+ return;+ }// Screenshot harness defaults to reduced motion; opt in to verify real transitions.await page.emulateMedia({ reducedMotion: 'no-preference' });const editor = page.locator('[contenteditable]');diff --git a/apps/desktop/src/main/voice-input/__tests__/composerLongPressSetting.test.ts b/apps/desktop/src/main/voice-input/__tests__/composerLongPressSetting.test.tsindex a018665d6b..fd218dfa5e 100644--- a/apps/desktop/src/main/voice-input/__tests__/composerLongPressSetting.test.ts+++ b/apps/desktop/src/main/voice-input/__tests__/composerLongPressSetting.test.ts@@ -65,9 +65,9 @@ afterEach(() => {});describe('长按输入框语音输入开关 —— 只持久化用户 override', () => {- it('默认关闭;无关设置保存不会把默认值记成用户选择', () => {+ it('默认开启;无关设置保存不会把默认值记成用户选择', () => {writeSettingsFile({ dictionaryEntries: [] });- expect(voiceInputDataStore.getSettings().composerLongPressEnabled).toBe(false);+ expect(voiceInputDataStore.getSettings().composerLongPressEnabled).toBe(true);voiceInputDataStore.updateSettings({ language: 'zh-CN' });expect(readPersistedSettings().composerLongPressEnabledOverride).toBeUndefined();@@ -91,20 +91,22 @@ describe('长按输入框语音输入开关 —— 只持久化用户 override',voiceInputDataStore.updateSettings({ composerLongPressEnabled: false });expect(readPersistedSettings().composerLongPressEnabledOverride).toBe(false);expect(readPersistedSettings().composerLongPressEnabled).toBeUndefined();+ resetStoreCaches();+ expect(voiceInputDataStore.getSettings().composerLongPressEnabled).toBe(false);});it('传 null 恢复默认:删除 override,重新跟随版本默认值', () => {writeSettingsFile({ dictionaryEntries: [] });voiceInputDataStore.updateSettings({ composerLongPressEnabled: true });const next = voiceInputDataStore.updateSettings({ composerLongPressEnabled: null });- expect(next.composerLongPressEnabled).toBe(false);+ expect(next.composerLongPressEnabled).toBe(true);expect(readPersistedSettings().composerLongPressEnabledOverride).toBeUndefined();expect(readPersistedSettings().composerLongPressEnabled).toBeUndefined();});it('配置里残留的有效值不会被当成用户选择', () => {// 有效值只是写盘时的投影;没有 override 就一律跟随默认值。- writeSettingsFile({ dictionaryEntries: [], composerLongPressEnabled: true });- expect(voiceInputDataStore.getSettings().composerLongPressEnabled).toBe(false);+ writeSettingsFile({ dictionaryEntries: [], composerLongPressEnabled: false });+ expect(voiceInputDataStore.getSettings().composerLongPressEnabled).toBe(true);});});diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsxindex 976b4a398b..088bb0398f 100644--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx@@ -3460,6 +3460,7 @@ export function ChatInput({const voiceShortcutRef = useRef(voiceInputSettings.shortcut);const voiceInputStateRef = useRef(voiceInput.state);const voiceInputGetStateRef = useRef(voiceInput.getState);+ const voiceInputPrepareRef = useRef(voiceInput.prepare);const voiceInputStopRef = useRef(handleVoiceInputStopWithRefinement);voiceInputBusyRef.current = voiceInput.isBusy;voiceDraftTextRef.current = voiceInput.draftText;@@ -3506,6 +3507,7 @@ export function ChatInput({useEffect(() => {voiceInputStateRef.current = voiceInput.state;voiceInputGetStateRef.current = voiceInput.getState;+ voiceInputPrepareRef.current = voiceInput.prepare;voiceInputStopRef.current = handleVoiceInputStopWithRefinement;voiceInputCancelRef.current = voiceInput.cancel;handleVoiceInputStartRef.current = handleVoiceInputStart;@@ -3520,6 +3522,7 @@ export function ChatInput({handleVoiceInputStopWithRefinement,voiceInput.cancel,voiceInput.getState,+ voiceInput.prepare,voiceInput.state,]);@@ -3717,11 +3720,21 @@ export function ChatInput({};}, []);- // 长按输入框语音输入(语音设置里打开,默认关闭):输入框不加任何提示,按住鼠标+ // 长按输入框语音输入默认开启,手动配置可关闭。按住鼠标// 左键不动片刻即开始录音,松开结束。按下由输入框的 onMouseDown 接入(要读// detail 才能排除双击);移动/松开挂在 window 上。真正开始录音后再指针捕获,// 拖出窗口松开也能停;等待期间不捕获,免得打断选字。const composerLongPressVoiceInputEnabled = voiceInputSettings.composerLongPressEnabled;+ const [composerVoiceHovered, setComposerVoiceHovered] = useState(false);+ const [composerVoiceHoverHintReady, setComposerVoiceHoverHintReady] = useState(false);+ useEffect(() => {+ setComposerVoiceHoverHintReady(false);+ if (!composerVoiceHovered) return;+ const timer = setTimeout(() => setComposerVoiceHoverHintReady(true), 500);+ return () => clearTimeout(timer);+ }, [composerVoiceHovered]);+ const [composerVoiceHolding, setComposerVoiceHolding] = useState(false);+ const [voiceButtonHolding, setVoiceButtonHolding] = useState(false);const composerLongPressGestureRef = useRef<ComposerLongPressVoiceGesture | null>(null);const armComposerLongPressCaptureRef = useRef<((pointerId: number, target: Element) => void) | null>(null,@@ -3729,6 +3742,26 @@ export function ChatInput({useEffect(() => {if (!composerLongPressVoiceInputEnabled) return;let capture: { pointerId: number; target: Element } | null = null;+ let selectionLockTarget: HTMLElement | null = null;+ const clearSelectionLock = () => {+ selectionLockTarget?.removeAttribute('data-voice-holding');+ selectionLockTarget = null;+ };+ // pointerup is followed by mouseup/click and asynchronous selectionchange.+ // Keep protecting this gesture until the next user input, not a timeout+ // that could expire before Chromium finishes dispatching those events.+ const handleNextInput = () => {+ if (!gesture.isHolding()) clearSelectionLock();+ };+ const preventReleaseClick = (event: MouseEvent) => {+ if (!selectionLockTarget || gesture.isHolding()) return;+ if (event.detail === 0) {+ clearSelectionLock();+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ };const handleLostPointerCapture = (event: Event) => {if (!(event instanceof PointerEvent)) return;@@ -3752,9 +3785,13 @@ export function ChatInput({const gesture = createComposerLongPressVoiceGesture({getState: () => voiceInputGetStateRef.current(),+ prepare: () => voiceInputPrepareRef.current?.(),start: () => handleVoiceInputStartRef.current(),stop: () => voiceInputStopRef.current(),onHoldStart: () => {+ setComposerVoiceHolding(true);+ selectionLockTarget = editorRef.current?.view.dom ?? null;+ selectionLockTarget?.setAttribute('data-voice-holding', 'true');if (!capture) return;try {capture.target.setPointerCapture(capture.pointerId);@@ -3763,7 +3800,13 @@ export function ChatInput({capture = null;}},- onHoldEnd: releaseCapture,+ onHoldEnd: () => {+ setComposerVoiceHolding(false);+ // Restore hit-testing for the next press immediately; only the+ // selectionchange/click guard needs to outlive this release.+ selectionLockTarget?.removeAttribute('data-voice-holding');+ releaseCapture();+ },});composerLongPressGestureRef.current = gesture;armComposerLongPressCaptureRef.current = (pointerId, target) => {@@ -3781,6 +3824,7 @@ export function ChatInput({};const handleLeftButtonRelease = (event: PointerEvent | MouseEvent) => {if (event.button !== 0) return;+ if (selectionLockTarget) event.preventDefault();gesture.release();// 没到时长就松开时不会走 onHoldEnd,但仍要清掉按下时记下的 pointerId。releaseCapture();@@ -3788,13 +3832,50 @@ export function ChatInput({const handleAbort = () => {gesture.release();releaseCapture();+ clearSelectionLock();+ };+ // pointermove.preventDefault() does not suppress Chromium's compatibility+ // mouse events or native selection/drag defaults after mousedown.+ const preventHeldSelection = (event: Event) => {+ if (selectionLockTarget) event.preventDefault();+ };+ // mousedown has already started Chromium's selection gesture before the+ // hold timer fires. Cancelling later move/selectstart events alone cannot+ // guarantee it stops. Keep the DOM selection at the editor's current+ // insertion range before ProseMirror observes native selection changes.+ // Read the current state each time so streamed dictation can move it.+ const preserveHeldSelection = (event: Event) => {+ if (!selectionLockTarget) return;+ const current = editorRef.current;+ if (!current || current.isDestroyed) return;+ const selection = current.view.dom.ownerDocument.getSelection();+ if (!selection?.anchorNode || !current.view.dom.contains(selection.anchorNode)) return;+ const anchor = current.view.domAtPos(current.state.selection.anchor);+ const head = current.view.domAtPos(current.state.selection.head);+ if (+ selection.anchorNode === anchor.node && selection.anchorOffset === anchor.offset &&+ selection.focusNode === head.node && selection.focusOffset === head.offset+ ) return;+ event.stopImmediatePropagation();+ selection.setBaseAndExtent(anchor.node, anchor.offset, head.node, head.offset);+ };+ const handleDragStart = (event: Event) => {+ if (gesture.isHolding()) event.preventDefault();+ else handleAbort();};window.addEventListener('pointermove', handlePointerMove, true);+ window.addEventListener('pointerdown', handleNextInput, true);+ window.addEventListener('mousedown', handleNextInput, true);+ window.addEventListener('keydown', handleNextInput, true);+ window.addEventListener('click', preventReleaseClick, true);+ window.addEventListener('mousemove', preventHeldSelection, true);+ window.addEventListener('selectstart', preventHeldSelection, true);+ document.addEventListener('selectionchange', preserveHeldSelection, true);window.addEventListener('pointerup', handleLeftButtonRelease, true);window.addEventListener('mouseup', handleLeftButtonRelease, true);window.addEventListener('pointercancel', handleAbort, true);// 按在已选中的文字上会进入原生拖拽,之后不再有 move / up。- window.addEventListener('dragstart', handleAbort, true);+ window.addEventListener('dragstart', handleDragStart, true);window.addEventListener('blur', handleAbort);return () => {armComposerLongPressCaptureRef.current = null;@@ -3803,17 +3884,31 @@ export function ChatInput({}gesture.dispose();releaseCapture();+ clearSelectionLock();window.removeEventListener('pointermove', handlePointerMove, true);+ window.removeEventListener('pointerdown', handleNextInput, true);+ window.removeEventListener('mousedown', handleNextInput, true);+ window.removeEventListener('keydown', handleNextInput, true);+ window.removeEventListener('click', preventReleaseClick, true);+ window.removeEventListener('mousemove', preventHeldSelection, true);+ window.removeEventListener('selectstart', preventHeldSelection, true);+ document.removeEventListener('selectionchange', preserveHeldSelection, true);window.removeEventListener('pointerup', handleLeftButtonRelease, true);window.removeEventListener('mouseup', handleLeftButtonRelease, true);window.removeEventListener('pointercancel', handleAbort, true);- window.removeEventListener('dragstart', handleAbort, true);+ window.removeEventListener('dragstart', handleDragStart, true);window.removeEventListener('blur', handleAbort);};- }, [composerLongPressVoiceInputEnabled]);+ }, [composerLongPressVoiceInputEnabled, storageKey]);const handleComposerLongPressPointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>) => {if (event.button !== 0) return;+ const current = editorRef.current;+ if (!current || current.isDestroyed || !(event.target instanceof Element)) return;+ if (+ !current.view.dom.contains(event.target) &&+ !isComposerBlankPointerTarget(event.target, event.currentTarget, current.view.dom, event)+ ) return;armComposerLongPressCaptureRef.current?.(event.pointerId, event.currentTarget);}, []);@@ -3828,7 +3923,11 @@ export function ChatInput({if (!editorInstance || editorInstance.isDestroyed) return;const editorElement = editorInstance.view.dom;const target = event.target;- if (!(target instanceof Element) || !editorElement.contains(target)) return;+ if (!(target instanceof Element)) return;+ if (+ !editorElement.contains(target) &&+ !isComposerBlankPointerTarget(target, event.currentTarget, editorElement, event)+ ) return;// 提及 / 粘贴文本 / 引用这类 chip 自带点击与拖拽行为,不在上面起手。const atom = target.closest('[contenteditable="false"]');if (atom && atom !== editorElement && editorElement.contains(atom)) return;@@ -8899,10 +8998,24 @@ export function ChatInput({});// 推荐 overlay 的可见判据:开关开启 + 有推荐词 + 输入框空 + 无附件/浏览器评论/语音草稿 + 输入框未锁定。// composerMutationLocked 涵盖 disabled、sendDispatchInFlight、当前输入框所属语音及远程只读/锁定状态。- const showRecommendationOverlay = shouldShowComposerPromptRecommendation({- ...recommendationComposerState,- prompt: recommendedPrompt,- });+ const voiceHintEmpty =+ !!editor && !editor.isDestroyed && composerDocIsEmpty(editor.state.doc) && !hasVoiceDraftText;+ const voiceHint = voiceHintEmpty+ ? voiceBusyOnCurrentComposer && voiceInput.state === 'listening'+ ? t(+ composerVoiceHolding || voiceButtonHolding+ ? 'newChat.chatInput.voiceInput.speakRelease'+ : 'newChat.chatInput.voiceInput.speakPressAgain',+ )+ : composerVoiceHovered && composerVoiceHoverHintReady && composerLongPressVoiceInputEnabled && !composerTypingLocked+ ? t('newChat.chatInput.voiceInput.holdComposer')+ : null+ : null;+ const showRecommendationOverlay =+ !voiceHint && shouldShowComposerPromptRecommendation({+ ...recommendationComposerState,+ prompt: recommendedPrompt,+ });// handleKeyDown 的稳定闭包只按真实可见性接受 Tab,避免隐藏推荐被误填入。showRecommendationRef.current = showRecommendationOverlay;// 首页建议悬停预览:输入框锁定(发送中 / 语音占用 / 禁用)时不预览,免得遮住进行中的状态。@@ -8911,7 +9024,7 @@ export function ChatInput({const [dismissedPreviewPrompt, setDismissedPreviewPrompt] = useState<string | null>(null);if (!previewPrompt && dismissedPreviewPrompt !== null) setDismissedPreviewPrompt(null);const showPromptPreview =- !!previewPrompt && previewPrompt !== dismissedPreviewPrompt && !composerMutationLocked;+ !!previewPrompt && previewPrompt !== dismissedPreviewPrompt && !composerMutationLocked && !voiceHint;promptPreviewInputGuardRef.current = () => {if (!showPromptPreview) return false;setDismissedPreviewPrompt(previewPrompt ?? null);@@ -9176,12 +9289,22 @@ export function ChatInput({],)}data-split-group-composer-drop-target+ onPointerDown={handleComposerLongPressPointerDown}+ onPointerOver={(event) => {+ const editorDom = editor && !editor.isDestroyed ? editor.view.dom : null;+ setComposerVoiceHovered(+ (event.target instanceof Element && !!editorDom?.contains(event.target)) ||+ isComposerBlankPointerTarget(event.target, event.currentTarget, editorDom, event),+ );+ }}+ onPointerLeave={() => setComposerVoiceHovered(false)}// 卡片里的空白(文字行下方的空隙、工具栏两组按钮之间的空档、四周// padding)没有元素承接点击:浏览器默认会把焦点从 contenteditable 撤到// <body>,正在输入的光标凭空消失;而点空白又该能进入输入态。所以先// preventDefault 掉这次默认的焦点转移,再按需补一次不带坐标的 focus ——// 「进入输入态」归空白区,「定位插入点」只归点击文字那一行。onMouseDown={(event) => {+ handleComposerLongPressMouseDown(event);if (event.button !== 0) return;if (!isComposerBlankPointerTarget(@@ -9435,10 +9558,9 @@ export function ChatInput({className="relative w-full"// 推荐词生效时由 CSS 关掉原生 placeholder,避免两行字叠在一起。data-recommendation-active={showRecommendationOverlay ? 'true' : undefined}+ data-voice-hint-active={voiceHint ? 'true' : undefined}// 建议预览生效时由 CSS 隐去编辑器正文。data-prompt-preview-active={showPromptPreview ? 'true' : undefined}- onPointerDown={handleComposerLongPressPointerDown}- onMouseDown={handleComposerLongPressMouseDown}><EditorContenteditor={editor}@@ -9452,6 +9574,14 @@ export function ChatInput({voiceBusyOnCurrentComposer && voiceInput.draftText ? 'true' : undefined}/>+ {voiceHint && (+ <div+ data-testid="composer-voice-hint"+ className="pointer-events-none absolute inset-x-0 top-0 py-[3px] pr-[11px] text-15 leading-[1.467] font-normal text-[var(--chat-input-placeholder-subtle)]"+ >+ {voiceHint}+ </div>+ )}{/* 建议预览:排版与原生 placeholder 对齐,py-[3px] 同下方推荐词 overlay 的说明。 */}{showPromptPreview && (<div@@ -9927,6 +10057,8 @@ export function ChatInput({}shortcutLabel={voiceInputShortcutLabel}onStart={handleVoiceInputStart}+ onHoldChange={setVoiceButtonHolding}+ emptyComposer={voiceHintEmpty}onStop={handleVoiceInputPlainStop}onStopAndSend={handleClickSend}sendTargetRef={sendButtonRef}@@ -10169,6 +10301,8 @@ export function ChatInput({function VoiceInputButton({state,+ onHoldChange,+ emptyComposer,disabled,shortcutLabel,onStart,@@ -10181,6 +10315,8 @@ function VoiceInputButton({className,}: {state: import('@cindy/voice-input-core').VoiceInputState;+ onHoldChange: (holding: boolean) => void;+ emptyComposer: boolean;disabled: boolean;shortcutLabel: string;onStart: () => boolean | Promise<boolean | void>;@@ -10195,6 +10331,10 @@ function VoiceInputButton({}) {const { t } = useTranslation();const [longPressActive, setLongPressActive] = useState(false);+ useEffect(() => {+ onHoldChange(longPressActive);+ return () => onHoldChange(false);+ }, [longPressActive, onHoldChange]);const timerRef = useRef<number | null>(null);const pointerIdRef = useRef<number | null>(null);const lastPointerPointRef = useRef<{ clientX: number; clientY: number } | null>(null);@@ -10259,8 +10399,16 @@ function VoiceInputButton({} else if (activeRecording) {label = t('newChat.chatInput.voiceInput.stop');}+ const hintLabel =+ emptyComposer && activeRecording+ ? t(+ longPressActive+ ? 'newChat.chatInput.voiceInput.speakRelease'+ : 'newChat.chatInput.voiceInput.speakPressAgain',+ )+ : label;const tooltipText =- shortcutLabel && !longPressActive && !busy ? `${label} · ${shortcutLabel}` : label;+ shortcutLabel && !activeRecording && !busy ? `${hintLabel} · ${shortcutLabel}` : hintLabel;const controlledTooltipOpen = busy? showProcessing: longPressActive@@ -10388,6 +10536,9 @@ function VoiceInputButton({aria-pressed={activeRecording}onPointerDown={(event) => {if (event.button !== 0) return;+ // A fresh press is never the synthetic click from the previous hold.+ // Chromium may omit that click when the recording pill changes size.+ clearSuppressNextClick();event.preventDefault();event.currentTarget.blur();if (disabledOrBusy || listening) return;diff --git a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsxindex f44528feb7..87d9f057b2 100644--- a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx+++ b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx@@ -35,6 +35,7 @@ const h = vi.hoisted(() => ({ t: (key: string) => key, confirm: vi.fn(), editor:draftSource: null as UseVoiceInputResult['draftSource'],draftRange: null as UseVoiceInputResult['draftRange'],cancel: vi.fn().mockResolvedValue(undefined),+ prepare: vi.fn(),realModelSelector: false,listAgentSkills: vi.fn(),localProviders: [] as ProviderView[],@@ -88,7 +89,7 @@ vi.mock('@/voice-input/useVoiceInput', () => ({ useVoiceInput: (editor: Editor |if (editor && editor !== h.editor) editor.on('transaction', ({ transaction }) => { if (transaction.docChanged) h.documentChanged(); });h.editor = editor;const state = h.voiceState ?? (h.listening ? 'listening' : 'idle');- return { state, isListening: state === 'listening', isBusy: state === 'listening' || state === 'submitting' || state === 'refining', draftText: h.draftText, draftSource: h.draftSource, draftRange: h.draftRange, start: vi.fn(), stop: h.stop, cancel: h.cancel };+ return { state, prepare: h.prepare, getState: () => h.voiceState ?? (h.listening ? 'listening' : 'idle'), isListening: state === 'listening', isBusy: state === 'listening' || state === 'submitting' || state === 'refining', draftText: h.draftText, draftSource: h.draftSource, draftRange: h.draftRange, start: () => { h.listening = true; return true; }, stop: h.stop, cancel: h.cancel };} }));vi.mock('@/hooks/useProviders', () => ({ useProviders: () => ({ providers: h.localProviders, providerOrder: [], loading: false }) }));vi.mock('@/hooks/useDeviceProviders', () => ({ useDeviceProviders: () => ({ providers: [], loading: false, unsupported: false }) }));@@ -139,6 +140,7 @@ beforeEach(() => {h.draftSource = null;h.draftRange = null;h.stop.mockClear();+ h.prepare.mockClear();h.realModelSelector = false;h.cancel.mockReset().mockResolvedValue(undefined);h.atPanel = null;@@ -233,6 +235,75 @@ it('locks editor and attachments for the whole voice run, but keeps permissions}});+// User request 2026-10-24: empty composer hints must follow hover/hold/click,+// and native selection must not take over a recording. Existing voice tests+// only cover editability; minimal inputs below drive real DOM handlers (unit/jsdom).+it('shows voice guidance only in an empty composer and distinguishes holding from clicking', async () => {+ const view = renderComposer();+ await waitFor(() => expect(h.editor).not.toBeNull());+ const area = view.container.querySelector('[data-recommendation-active]')+ ?? h.editor!.view.dom.parentElement!.parentElement!;+ vi.useFakeTimers();+ try {+ fireEvent.pointerEnter(area);+ await act(async () => { await vi.advanceTimersByTimeAsync(499); });+ expect(screen.queryByTestId('composer-voice-hint')).toBeNull();+ await act(async () => { await vi.advanceTimersByTimeAsync(1); });+ expect(screen.getByTestId('composer-voice-hint').textContent).toBe('newChat.chatInput.voiceInput.holdComposer');+ fireEvent.pointerLeave(area);+ expect(screen.queryByTestId('composer-voice-hint')).toBeNull();+ fireEvent.pointerEnter(area);+ await act(async () => { await vi.advanceTimersByTimeAsync(250); });+ fireEvent.pointerLeave(area);+ await act(async () => { await vi.advanceTimersByTimeAsync(500); });+ expect(screen.queryByTestId('composer-voice-hint')).toBeNull();+ // jsdom has no hit-testing; returning null leaves ProseMirror's ordinary+ // mousedown path inert while the real composer handler still receives it.+ Object.defineProperty(document, 'elementFromPoint', { configurable: true, value: () => null });+ fireEvent.mouseDown(h.editor!.view.dom, { button: 0, detail: 1, clientX: 10, clientY: 10 });+ expect(h.prepare).toHaveBeenCalledOnce();+ expect(h.listening).toBe(false);+ await act(async () => { await vi.advanceTimersByTimeAsync(600); });+ view.rerender(<ChatInput {...view.props} />);+ expect(screen.getByTestId('composer-voice-hint').textContent).toBe('newChat.chatInput.voiceInput.speakRelease');+ for (const type of ['selectstart', 'dragstart', 'mousemove']) {+ const event = new Event(type, { bubbles: true, cancelable: true });+ h.editor!.view.dom.dispatchEvent(event);+ expect(event.defaultPrevented, type).toBe(true);+ }+ await act(async () => { fireEvent.mouseUp(window, { button: 0 }); });+ expect(h.stop).toHaveBeenCalledOnce();+ const selection = new Event('selectstart', { bubbles: true, cancelable: true });+ h.editor!.view.dom.dispatchEvent(selection);+ expect(selection.defaultPrevented).toBe(true);+ const releaseClick = new MouseEvent('click', { bubbles: true, cancelable: true, detail: 1 });+ h.editor!.view.dom.dispatchEvent(releaseClick);+ expect(releaseClick.defaultPrevented).toBe(true);+ fireEvent.pointerDown(document.body, { button: 0 });+ const nextSelection = new Event('selectstart', { bubbles: true, cancelable: true });+ h.editor!.view.dom.dispatchEvent(nextSelection);+ expect(nextSelection.defaultPrevented).toBe(false);+ } finally {+ Reflect.deleteProperty(document, 'elementFromPoint');+ vi.useRealTimers();+ }++ h.listening = false;+ view.rerender(<ChatInput {...view.props} />);+ fireEvent.click(screen.getByRole('button', { name: 'newChat.chatInput.voiceInput.start' }));+ view.rerender(<ChatInput {...view.props} />);+ expect(screen.getByTestId('composer-voice-hint').textContent).toBe('newChat.chatInput.voiceInput.speakPressAgain');+ h.draftText = 'hello';+ view.rerender(<ChatInput {...view.props} />);+ expect(screen.queryByTestId('composer-voice-hint')).toBeNull();+ h.draftText = '';+ h.listening = false;+ await act(async () => { h.editor!.commands.setContent('<p>Existing text</p>'); });+ view.rerender(<ChatInput {...view.props} />);+ fireEvent.pointerEnter(area);+ expect(screen.queryByTestId('composer-voice-hint')).toBeNull();+});+// #374 / voiceInputButtonAnchor: the real toolbar must keep the voice control// immediately beside the main action while listening, submitting, refining,// and returning to idle. A source-text guard could miss a misplaced rendereddiff --git a/apps/desktop/src/renderer/components/settings/VoiceInputSection.settings-search.ts b/apps/desktop/src/renderer/components/settings/VoiceInputSection.settings-search.tsindex 14e48c60ca..8d13b1fbdd 100644--- a/apps/desktop/src/renderer/components/settings/VoiceInputSection.settings-search.ts+++ b/apps/desktop/src/renderer/components/settings/VoiceInputSection.settings-search.ts@@ -16,7 +16,6 @@ export default {{ id: 'settings.voiceInput.history.label', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-settings-voiceInput-history-label', titleKey: 'settings.voiceInput.history.label', sectionKey: 'settings.tabs.voiceInput' },{ id: 'settings.voiceInput.sections.usageData', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-voice-usage-data', titleKey: 'settings.voiceInput.sections.usageData', sectionKey: 'settings.tabs.voiceInput' },{ id: 'settings.voiceInput.fastActivation.label', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-settings-voiceInput-fastActivation-label', titleKey: 'settings.voiceInput.fastActivation.label', sectionKey: 'settings.tabs.voiceInput', descriptionKey: 'settings.voiceInput.fastActivation.hint' },- { id: 'settings.voiceInput.composerLongPress.label', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-settings-voiceInput-composerLongPress-label', titleKey: 'settings.voiceInput.composerLongPress.label', sectionKey: 'settings.tabs.voiceInput', descriptionKey: 'settings.voiceInput.composerLongPress.hint' },{ id: 'settings.voiceInput.speakerFilter.label', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-settings-voiceInput-speakerFilter-label', titleKey: 'settings.voiceInput.speakerFilter.label', sectionKey: 'settings.tabs.voiceInput', descriptionKey: 'settings.voiceInput.speakerFilter.hint', isVisible: ({ platform }) => platform === 'darwin' || platform === 'win32' },{ id: 'settings.voiceInput.shortcut.label', fallbackTargetId: 'settings-panel-voice-input', tab: 'voice-input', targetId: 'settings-search-settings-voiceInput-shortcut-label', titleKey: 'settings.voiceInput.shortcut.label', sectionKey: 'settings.tabs.voiceInput', descriptionKey: 'settings.voiceInput.shortcut.hint' },diff --git a/apps/desktop/src/renderer/components/settings/VoiceInputSection.tsx b/apps/desktop/src/renderer/components/settings/VoiceInputSection.tsxindex 910d758cdb..56f200d9f0 100644--- a/apps/desktop/src/renderer/components/settings/VoiceInputSection.tsx+++ b/apps/desktop/src/renderer/components/settings/VoiceInputSection.tsx@@ -1038,8 +1038,6 @@ export function VoiceInputSection() {setMuteSystemAudio,setPlayInteractionSound,setFastActivationEnabled,- setComposerLongPressEnabled,- resetComposerLongPressEnabled,setRefinementEnabled,setRefinementInstructions,setAutoDictionaryEnabled,@@ -2695,36 +2693,6 @@ export function VoiceInputSection() {/></div>- <div className="flex items-center justify-between gap-5 border-t border-[var(--settings-theme-card-border)] pt-4">- <div className="flex min-w-0 flex-col gap-1">- <p id="settings-search-settings-voiceInput-composerLongPress-label"- className="text-13 font-medium text-[var(--settings-section-title)]"- style={{ letterSpacing: '0.12px' }}- >- {t('settings.voiceInput.composerLongPress.label')}- </p>- <p className="text-12 leading-[1.4] text-[var(--text-secondary)]">- {t('settings.voiceInput.composerLongPress.hint')}- </p>- </div>-- <div className="flex shrink-0 items-center gap-2">- <DefaultOverrideControls- isCustomized={settings.composerLongPressEnabledOverride != null}- onReset={() => {- void resetComposerLongPressEnabled().then((ok) => {- if (ok) toast.success(t('settings.defaults.restored'));- });- }}- />- <Switch- checked={settings.composerLongPressEnabled}- onCheckedChange={setComposerLongPressEnabled}- aria-label={t('settings.voiceInput.composerLongPress.ariaLabel')}- />- </div>- </div>-<div className="flex items-center justify-between gap-5 border-t border-[var(--settings-theme-card-border)] pt-4"><div className="flex min-w-0 flex-col gap-1"><p id="settings-search-settings-voiceInput-interactionSound-label"diff --git a/apps/desktop/src/renderer/hooks/__tests__/useVoiceInputSettings.overrideReset.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useVoiceInputSettings.overrideReset.test.tsindex b02f29d468..e5ed6f8b23 100644--- a/apps/desktop/src/renderer/hooks/__tests__/useVoiceInputSettings.overrideReset.test.ts+++ b/apps/desktop/src/renderer/hooks/__tests__/useVoiceInputSettings.overrideReset.test.ts@@ -62,7 +62,7 @@ describe('voice input override restore defaults', () => {expect(patch).toEqual({ composerLongPressEnabled: null });const next = { ...settings };delete next.composerLongPressEnabledOverride;- next.composerLongPressEnabled = false;+ next.composerLongPressEnabled = getDefaultVoiceInputSettings('darwin').composerLongPressEnabled;return next;});@@ -74,7 +74,7 @@ describe('voice input override restore defaults', () => {expect(ok).toBe(true);expect(updateSettings).toHaveBeenCalledTimes(1);expect(updateSettings).toHaveBeenCalledWith({ composerLongPressEnabled: null });- expect(result.current.settings.composerLongPressEnabled).toBe(false);+ expect(result.current.settings.composerLongPressEnabled).toBe(true);expect(result.current.settings.composerLongPressEnabledOverride).toBeUndefined();});diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.jsonindex 1bc434871e..11b9193578 100644--- a/apps/desktop/src/renderer/i18n/locales/en/common.json+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json@@ -11179,6 +11179,9 @@"start": "Start voice input","stop": "Stop voice input","releaseToStop": "Recording. Release to stop.",+ "holdComposer": "Hold the input box to dictate",+ "speakRelease": "Start speaking. Release to finish.",+ "speakPressAgain": "Start speaking. Press again to finish.","releaseToSend": "Release to send","finishAndSend": "Finish voice input and send","refining": "Refining",diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.jsonindex 48222ea6ca..e2d2642288 100644--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json@@ -11144,6 +11144,9 @@"start": "音声入力を開始","stop": "音声入力を停止","releaseToStop": "音声入力中、離すと終了",+ "holdComposer": "入力欄を長押しして音声入力",+ "speakRelease": "話してください。離すと終了",+ "speakPressAgain": "話してください。もう一度押すと終了","releaseToSend": "離すと送信","finishAndSend": "音声入力を終了して送信","refining": "整えています",diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.jsonindex 72ef531752..f603620879 100644--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json@@ -11144,6 +11144,9 @@"start": "음성 입력 시작","stop": "음성 입력 중지","releaseToStop": "음성 입력 중, 놓으면 종료",+ "holdComposer": "입력란을 길게 눌러 음성 입력",+ "speakRelease": "말씀하세요. 놓으면 종료",+ "speakPressAgain": "말씀하세요. 다시 누르면 종료","releaseToSend": "놓으면 전송","finishAndSend": "음성 입력을 끝내고 전송","refining": "다듬는 중",diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.jsonindex 793d1ea4ca..5e461dec39 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json@@ -11144,6 +11144,9 @@"start": "开始语音输入","stop": "停止语音输入","releaseToStop": "语音输入中,松开结束",+ "holdComposer": "长按输入框,语音输入",+ "speakRelease": "开始说话,松开结束",+ "speakPressAgain": "开始说话,再次按下结束","releaseToSend": "松开发送","finishAndSend": "结束语音输入并发送","refining": "正在润色",diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.jsonindex 5ca5557bd5..24eb90eda3 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json@@ -11144,6 +11144,9 @@"start": "開始語音輸入","stop": "停止語音輸入","releaseToStop": "語音輸入中,鬆開結束",+ "holdComposer": "長按輸入框,語音輸入",+ "speakRelease": "開始說話,鬆開結束",+ "speakPressAgain": "開始說話,再按一下結束","releaseToSend": "鬆開傳送","finishAndSend": "結束語音輸入並傳送","refining": "正在潤色",diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.cssindex 277cb89ee5..b037de5dc5 100644--- a/apps/desktop/src/renderer/styles/globals.css+++ b/apps/desktop/src/renderer/styles/globals.css@@ -339,10 +339,21 @@ html:lang(ko) {content: none;}/* 输入框推荐提示词生效时,推荐词由 ChatInput 的 overlay 承载,关掉原生 placeholder。 */+[data-voice-hint-active='true'] .ProseMirror > p.is-editor-empty:first-child::before,+[data-voice-hint-active='true'] .ProseMirror > p.is-empty:first-child:only-child::before,[data-recommendation-active='true'] .ProseMirror > p.is-editor-empty:first-child::before,[data-recommendation-active='true'] .ProseMirror > p.is-empty:first-child:only-child::before {content: none;}+.ProseMirror[data-voice-holding='true'],+.ProseMirror[data-voice-holding='true'] * {+ -webkit-user-select: none;+ user-select: none;+}+/* 空框用完整语音提示替代语音光标,避免图标压住提示首字。 */+[data-voice-hint-active='true'] [data-voice-caret] {+ visibility: hidden;+}/* 首页建议悬停预览:预览由 ChatInput 的 overlay 承载,暂时隐去正文/placeholder;预览不改变编辑器高度,见 ChatInput 的 promptPreviewLines。 */[data-prompt-preview-active='true'] .ProseMirror {diff --git a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.tsindex f05be36aed..b3eb9cf052 100644--- a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts+++ b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts@@ -21,6 +21,8 @@ export type WebMicAudioEngineOptions = {audioProcessing?: boolean | AudioProcessingConfig;latencyMs?: number;keepAlive?: boolean;+ /** Borrow a prepared session for this recording only, including device fallback. */+ releaseAfterUse?: boolean;onStateChange?: (event: string, details?: Record<string, unknown>) => void;onInterrupted?: (message: string) => void;};@@ -853,6 +855,40 @@ async function getOrCreateKeepAliveSession(return keepAliveSessionPromise;}+/** Prepare an inactive microphone for one press; never activate PCM or replace another owner's device. */+export function prepareVoiceInputMicrophone(options: WebMicAudioEngineOptions): (() => void) | undefined {+ const normalized = normalizeKeepAliveOptions(options);+ // A failed startup can leave its settling promise without a session. Let+ // normal recording recover it; preparation must own a session synchronously.+ if (keepAliveSessionPromise && !keepAliveSession) return;+ if (keepAliveSession && (+ keepAliveSession.isBusy() || keepAliveSession.isStaleAfterDeviceChange() ||+ keepAliveSession.key !== keepAliveKey(normalized)+ )) return;+ // With no replacement needed, getOrCreate publishes the session before its+ // first await. Retain that identity so a late cancellation cannot close a+ // different session, including while getUserMedia is still pending.+ const ready = getOrCreateKeepAliveSession(normalized);+ const session = keepAliveSession;+ void ready.then((prepared) => {+ if (options.keepAlive && !prepared.isBusy()) {+ scheduleKeepAliveIdleDispose(prepared, 'idle_timeout_after_prewarm', { refresh: false });+ }+ }).catch(() => undefined);+ let released = false;+ return () => {+ if (released) return;+ released = true;+ if (options.keepAlive || !session || keepAliveSession !== session) return;+ if (session.isBusy()) {+ // Recording adopted it. Let its normal stop close the temporary device.+ session.markStaleForReplacement();+ } else {+ void disposeKeepAliveVoiceInputMicrophone('press_preparation_cancelled').catch(() => undefined);+ }+ };+}+export async function prewarmVoiceInputMicrophone(options: WebMicAudioEngineOptions): Promise<void> {const normalized = normalizeKeepAliveOptions(options);// Prewarm can land mid-dictation: another ChatInput mounts, or a settings@@ -1019,6 +1055,7 @@ export class WebMicAudioEngine {private readonly audioProcessing: boolean | AudioProcessingConfig;private readonly latencyMs?: number;private readonly keepAlive: boolean;+ private readonly releaseAfterUse: boolean;private readonly onStateChange?: (event: string, details?: Record<string, unknown>) => void;private readonly onInterrupted?: (message: string) => void;private keepAliveSession?: KeepAliveMicSession;@@ -1053,6 +1090,7 @@ export class WebMicAudioEngine {this.audioProcessing = options.audioProcessing ?? true;this.latencyMs = options.latencyMs;this.keepAlive = options.keepAlive ?? false;+ this.releaseAfterUse = options.releaseAfterUse ?? false;this.onStateChange = options.onStateChange;this.onInterrupted = options.onInterrupted;}@@ -1309,6 +1347,7 @@ export class WebMicAudioEngine {this.clearWatchdog();if (this.usingKeepAliveSession) {const session = this.keepAliveSession;+ if (this.releaseAfterUse) session?.markStaleForReplacement();// Drop the claim taken in startKeepAlive, then only tear the session down// if nobody else still holds one. deactivate() clears the shared PCM// callback, sends setActive=false and detaches the output path — doingdiff --git a/apps/desktop/src/renderer/voice-input/__tests__/VoiceInputSection.overrideReset.test.tsx b/apps/desktop/src/renderer/voice-input/__tests__/VoiceInputSection.overrideReset.test.tsxindex 2fe0c28eb1..f91992e3b4 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/VoiceInputSection.overrideReset.test.tsx+++ b/apps/desktop/src/renderer/voice-input/__tests__/VoiceInputSection.overrideReset.test.tsx@@ -90,8 +90,6 @@ it('keeps custom ASR URL validation inline and associated through repeated edits});const cases = [- { field: 'composerLongPressEnabled', override: 'composerLongPressEnabledOverride',- label: 'settings.voiceInput.composerLongPress.ariaLabel' },{ field: 'dictionarySyncEnabled', override: 'dictionarySyncEnabledOverride',label: 'settings.voiceInput.refinement.dictionary.deviceSync.ariaLabel' },] as const;@@ -103,7 +101,8 @@ const cases = [* clicked this component, so removing its controls or disconnecting their props escaped coverage.* Minimal input: one explicit override opposite to its default (including false for dictionary sync).* Unit/jsdom: a separate file is needed because recordingGate is a node-only source guard; no- * VoiceInputSection component harness exists to extend. Both controls share this single fixture.+ * VoiceInputSection component harness exists to extend. The 2026-10-24 product decision hides+ * long-press dictation even with an explicit override; dictionary sync keeps its restore control.* Measured alone: 3.25s wall / 2.19s Vitest (1.11s collect, 550ms jsdom, 281ms tests).*/describe('VoiceInputSection restore defaults', () => {@@ -130,9 +129,8 @@ describe('VoiceInputSection restore defaults', () => {expect(row().queryByText('settings.defaults.customizedBadge')).toBeNull();expect(toast.success).toHaveBeenCalledExactlyOnceWith('settings.defaults.restored');- const other = cases.find(candidate => candidate.field !== field)!;- const otherToggle = screen.getByRole('switch', { name: other.label });- expect(otherToggle.getAttribute('aria-checked')).toBe(String(!defaults[other.field]));- expect(within(otherToggle.parentElement!.parentElement!).getByRole('button', { name: restoreLabel })).toBeTruthy();+ // Long-press dictation is now a hidden override, even when explicitly configured.+ expect(screen.queryByRole('switch', { name: 'settings.voiceInput.composerLongPress.ariaLabel' })).toBeNull();+ expect(settings.composerLongPressEnabledOverride).toBe(false);});});diff --git a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.tsindex af004f6b28..9ce80b1979 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts@@ -5,6 +5,7 @@ import {disposeKeepAliveVoiceInputMicrophone,isMicrophoneDeviceUnavailableError,prewarmVoiceInputMicrophoneWithAutomaticFallback,+ prepareVoiceInputMicrophone,WebMicAudioEngine,} from '../WebMicAudioEngine';@@ -114,6 +115,75 @@ describe('prewarmVoiceInputMicrophoneWithAutomaticFallback', () => {vi.restoreAllMocks();});+ it('prepares without activating PCM and closes a cancelled press', async () => {+ const release = prepareVoiceInputMicrophone({ workletUrl: 'https://app.local/pcm16k-worklet.js' });+ await vi.waitFor(() => expect(AudioWorkletNode).toHaveBeenCalledOnce());+ const node = vi.mocked(AudioWorkletNode).mock.results[0].value;+ expect(node.port.postMessage).toHaveBeenCalledWith({ type: 'setActive', active: false, reset: true });+ expect(node.port.postMessage).not.toHaveBeenCalledWith(expect.objectContaining({ active: true }));+ release?.();+ await vi.waitFor(() => expect(track.stopped).toBe(true));+ });++ it('reuses press preparation for capture and closes it after recording', async () => {+ const options = { workletUrl: 'https://app.local/pcm16k-worklet.js' };+ const release = prepareVoiceInputMicrophone(options);+ const engine = new WebMicAudioEngine({ ...options, keepAlive: true });+ const started = engine.start();+ release?.();+ await started;+ expect(getUserMedia).toHaveBeenCalledOnce();+ expect(track.stopped).toBe(false);+ expect(prepareVoiceInputMicrophone(options)).toBeUndefined();+ await engine.stop();+ expect(track.stopped).toBe(true);+ });++ it('closes a late microphone when the press was already cancelled', async () => {+ let finish!: (stream: unknown) => void;+ getUserMedia.mockImplementation(() => new Promise((resolve) => { finish = resolve; }));+ const release = prepareVoiceInputMicrophone({ workletUrl: 'https://app.local/pcm16k-worklet.js' });+ await vi.waitFor(() => expect(getUserMedia).toHaveBeenCalledOnce());+ release?.();+ finish({ getAudioTracks: () => [track], getTracks: () => [track] });+ await vi.waitFor(() => expect(track.stopped).toBe(true));+ expect(AudioWorkletNode).not.toHaveBeenCalled();+ });++ it('closes a temporary replacement device even when preparation could not open the selected one', async () => {+ const release = prepareVoiceInputMicrophone({+ workletUrl: 'https://app.local/pcm16k-worklet.js', deviceId: 'missing-device',+ });+ await vi.waitFor(() => expect(enumerateDevices).toHaveBeenCalledOnce());+ release?.();+ const engine = new WebMicAudioEngine({+ workletUrl: 'https://app.local/pcm16k-worklet.js', keepAlive: true, releaseAfterUse: true,+ });+ await engine.start();+ expect(track.stopped).toBe(false);+ await engine.stop();+ expect(track.stopped).toBe(true);+ });++ it('does not reopen a failed device after the next preparation was cancelled', async () => {+ let rejectDevice!: (error: Error) => void;+ getUserMedia.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectDevice = reject; }));+ const options = { workletUrl: 'https://app.local/pcm16k-worklet.js' };+ const release = prepareVoiceInputMicrophone(options);+ await vi.waitFor(() => expect(getUserMedia).toHaveBeenCalledOnce());+ rejectDevice(new DOMException('Device unavailable', 'NotReadableError'));+ await new Promise((resolve) => setTimeout(resolve, 0));+ release?.();+ prepareVoiceInputMicrophone(options)?.();+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(getUserMedia).toHaveBeenCalledOnce();+ const engine = new WebMicAudioEngine({ ...options, keepAlive: true, releaseAfterUse: true });+ await engine.start();+ expect(getUserMedia).toHaveBeenCalledTimes(2);+ await engine.stop();+ expect(track.stopped).toBe(true);+ });+it('requests microphone permission only once when keep-alive capture is denied', async () => {getUserMedia.mockRejectedValue(new DOMException('Permission denied', 'NotAllowedError'));const engine = new WebMicAudioEngine({diff --git a/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.tsindex 298fc7b73d..7d917c313d 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/composerLongPressGesture.test.ts@@ -29,13 +29,18 @@ function setup(initialState: VoiceInputState = 'idle') {const stop = vi.fn(async () => {state = 'done';});+ const releasePreparation = vi.fn();+ const prepare = vi.fn(() => releasePreparation);const gesture = createComposerLongPressVoiceGesture({getState: () => state,+ prepare,start,stop,});return {gesture,+ prepare,+ releasePreparation,start,stop,getState: () => state,@@ -55,16 +60,19 @@ describe('createComposerLongPressVoiceGesture', () => {});it('按住不动到时长才开始录音,松开结束', async () => {- const { gesture, start, stop, getState } = setup();+ const { gesture, start, stop, getState, prepare, releasePreparation } = setup();gesture.press(origin);+ expect(prepare).toHaveBeenCalledOnce();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS - 1);await settle();expect(start).not.toHaveBeenCalled();+ expect(releasePreparation).not.toHaveBeenCalled();vi.advanceTimersByTime(1);await settle();expect(start).toHaveBeenCalledOnce();+ expect(releasePreparation).toHaveBeenCalledOnce();expect(getState()).toBe('listening');expect(gesture.isHolding()).toBe(true);@@ -75,11 +83,12 @@ describe('createComposerLongPressVoiceGesture', () => {});it('没到时长就松开是普通点击,不录音', async () => {- const { gesture, start, stop } = setup();+ const { gesture, start, stop, releasePreparation } = setup();gesture.press(origin);vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS / 2);gesture.release();+ expect(releasePreparation).toHaveBeenCalledOnce();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS);await settle();@@ -88,11 +97,12 @@ describe('createComposerLongPressVoiceGesture', () => {});it('等待期间拖动超出容差视为选字,放弃长按', async () => {- const { gesture, start } = setup();+ const { gesture, start, releasePreparation } = setup();gesture.press(origin);gesture.move({ x: origin.x + COMPOSER_LONG_PRESS_MOVE_TOLERANCE_PX, y: origin.y });gesture.move({ x: origin.x + COMPOSER_LONG_PRESS_MOVE_TOLERANCE_PX + 1, y: origin.y });+ expect(releasePreparation).toHaveBeenCalledOnce();vi.advanceTimersByTime(COMPOSER_LONG_PRESS_VOICE_INPUT_MS);await settle();diff --git a/apps/desktop/src/renderer/voice-input/__tests__/useVoiceInput.startup.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/useVoiceInput.startup.test.tsindex 720fb17237..60da41b906 100644--- a/apps/desktop/src/renderer/voice-input/__tests__/useVoiceInput.startup.test.ts+++ b/apps/desktop/src/renderer/voice-input/__tests__/useVoiceInput.startup.test.ts@@ -18,7 +18,9 @@ const mocks = vi.hoisted(() => ({startEngine: vi.fn<() => Promise<void>>(),stopEngine: vi.fn(),drainEngine: vi.fn(),- engines: [] as Array<{ emit: (chunk: PcmChunk) => void }>,+ engines: [] as Array<{ emit: (chunk: PcmChunk) => void; keepAlive: boolean }>,+ prepareMicrophone: vi.fn(),+ releasePreparation: vi.fn(),promptExpired: vi.fn(),}));vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: mocks.translate }) }));@@ -49,9 +51,10 @@ vi.mock('../audioContextPool', () => ({vi.mock('../WebMicAudioEngine', () => ({WebMicAudioEngine: class {emit = (_chunk: PcmChunk) => {};- constructor() {+ constructor(readonly options: { keepAlive: boolean }) {mocks.engines.push(this);}+ get keepAlive() { return this.options.keepAlive; }onPcm16k(callback: (chunk: PcmChunk) => void) {this.emit = callback;}@@ -74,6 +77,7 @@ vi.mock('../WebMicAudioEngine', () => ({disposeKeepAliveVoiceInputMicrophone: vi.fn().mockResolvedValue(undefined),prewarmVoiceInputBenchmarkFixture: vi.fn().mockResolvedValue(undefined),prewarmVoiceInputMicrophoneWithAutomaticFallback: vi.fn().mockResolvedValue(undefined),+ prepareVoiceInputMicrophone: mocks.prepareMicrophone,}));import { useVoiceInput } from '../useVoiceInput';@@ -150,6 +154,8 @@ beforeEach(() => {mocks.startEngine.mockReset().mockResolvedValue(undefined);mocks.stopEngine.mockReset();mocks.drainEngine.mockReset();+ mocks.releasePreparation.mockReset();+ mocks.prepareMicrophone.mockReset().mockReturnValue(mocks.releasePreparation);});afterEach(() => {cleanup();@@ -158,6 +164,31 @@ afterEach(() => {});describe('local voice capture independent of startup checks and cloud', () => {+ it('prepares only authorized devices without claiming recording or starting ASR', async () => {+ const h = mount();+ h.result.current.prepare();+ expect(mocks.prepareMicrophone).not.toHaveBeenCalled();+ h.api.getMicrophonePermissionCached.mockReturnValue({ ok: true, status: 'granted' });+ const release = h.result.current.prepare();+ expect(mocks.prepareMicrophone).toHaveBeenCalledOnce();+ expect(h.result.current.state).toBe('idle');+ expect(h.api.start).not.toHaveBeenCalled();+ expect(mocks.engines).toHaveLength(0);+ await act(async () => { h.result.current.start(); });+ expect(mocks.engines[0].keepAlive).toBe(true);+ release?.();+ expect(mocks.releasePreparation).toHaveBeenCalledOnce();+ });++ it('releases unused press preparation when the composer unmounts', () => {+ const h = mount();+ h.api.getMicrophonePermissionCached.mockReturnValue({ ok: true, status: 'granted' });+ h.result.current.prepare();+ h.unmount();+ expect(mocks.releasePreparation).toHaveBeenCalledOnce();+ expect(h.api.start).not.toHaveBeenCalled();+ });+it('ends before the microphone produces its first frame and closes the late microphone', async () => {const h = mount();const device = deferred<void>();diff --git a/apps/desktop/src/renderer/voice-input/captureSession.ts b/apps/desktop/src/renderer/voice-input/captureSession.tsindex 7d11f0048b..5297ed8c70 100644--- a/apps/desktop/src/renderer/voice-input/captureSession.ts+++ b/apps/desktop/src/renderer/voice-input/captureSession.ts@@ -48,6 +48,7 @@ type VoiceInputCaptureSessionOptions = {workletUrl: string;deviceId?: string;fastActivationEnabled: boolean;+ temporaryMicrophone?: boolean;getRunId: () => string | null;setEngine: (engine: WebMicAudioEngine | null) => void;isCurrentEngine?: (engine: WebMicAudioEngine) => boolean;@@ -84,6 +85,7 @@ export async function startVoiceInputCaptureSession(workletUrl: options.workletUrl,deviceId,...createVoiceInputAudioProfile(options.fastActivationEnabled),+ releaseAfterUse: options.temporaryMicrophone,onStateChange: options.onStateChange,onInterrupted: options.onInterrupted,});diff --git a/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts b/apps/desktop/src/renderer/voice-input/composerLongPressGesture.tsindex e077680e54..0634eaf233 100644--- a/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts+++ b/apps/desktop/src/renderer/voice-input/composerLongPressGesture.ts@@ -11,6 +11,8 @@ export type ComposerLongPressVoiceGestureOptions = {holdMs?: number;moveTolerancePx?: number;getState: () => VoiceInputState;+ /** Prepare without recording. Cleanup cancels a short press or hands off after start. */+ prepare?: () => (() => void) | void;/*** 开始录音。返回 `false` 表示这次没有占用录音(已在录音、被取消等),* 调用方不得再对当前会话执行 `stop()`。@@ -68,11 +70,19 @@ export function createComposerLongPressVoiceGesture(let stopInFlight = false;let disposed = false;let startGeneration = 0;+ let releasePreparation: (() => void) | void;++ const clearPreparation = (): void => {+ const release = releasePreparation;+ releasePreparation = undefined;+ release?.();+ };const clearPending = (): void => {if (!pending) return;clearTimeout(pending.timer);pending = null;+ clearPreparation();};const endHold = (): void => {@@ -109,14 +119,18 @@ export function createComposerLongPressVoiceGesture(const beginVoiceInput = (): void => {pending = null;- if (disposed || !isIdleLike(options.getState())) return;+ if (disposed || !isIdleLike(options.getState())) {+ clearPreparation();+ return;+ }holding = true;options.onHoldStart?.();const generation = ++startGeneration;const currentStart = Promise.resolve().then(() => options.start()).then((claimed) => claimed !== false)- .catch(() => false);+ .catch(() => false)+ .finally(clearPreparation);startPromise = currentStart;void currentStart.then((claimed) => {if (startPromise !== currentStart) return;@@ -144,6 +158,11 @@ export function createComposerLongPressVoiceGesture(origin: point,timer: setTimeout(beginVoiceInput, holdMs),};+ try {+ releasePreparation = options.prepare?.();+ } catch {+ // Preparation is optional; the normal start path can still recover.+ }},move(point) {if (!pending) return;diff --git a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts b/apps/desktop/src/renderer/voice-input/useVoiceInput.tsindex 68dfc6164e..fdad5e1576 100644--- a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts+++ b/apps/desktop/src/renderer/voice-input/useVoiceInput.ts@@ -44,6 +44,7 @@ import {isSelectedMicrophoneUnavailableError,prewarmVoiceInputBenchmarkFixture,prewarmVoiceInputMicrophoneWithAutomaticFallback,+ prepareVoiceInputMicrophone,type PcmChunk,} from './WebMicAudioEngine';import { prewarmVoiceInputAudio } from './audioContextPool';@@ -145,6 +146,7 @@ export type UseVoiceInputResult = {getLastRefinement: () => VoiceInputRefinementSnapshot | null;/** 同步读取当前状态,不经过 React 渲染。长按手势靠它判断是否占用了录音。 */getState: () => VoiceInputState;+ prepare: () => (() => void) | void;/** 同步 claim 录音后立刻返回是否占用;启动流程(授权确认、采集、连接)在后台继续。 */start: (options?: VoiceInputStartOptions) => boolean;stop: (options?: VoiceInputStopOptions) => Promise<void>;@@ -196,6 +198,7 @@ export function useVoiceInput(const [state, setState] = useState<VoiceInputState>('idle');const [captureReady, setCaptureReady] = useState(false);const startupTimelineRef = useRef<VoiceInputStartupTimeline | null>(null);+ const microphonePreparationRef = useRef<(() => void) | null>(null);const [draftText, setDraftText] = useState('');const [draftSource, setDraftSource] = useState<VoiceInputDraftSource | null>(null);const [lastError, setLastError] = useState<string | null>(null);@@ -1236,6 +1239,34 @@ export function useVoiceInput(voiceInputSettings.microphoneDeviceId,]);+ const prepare = useCallback(() => {+ if (disabledRef.current || !editorRef.current || editorRef.current.isDestroyed) return;+ if (!['idle', 'done', 'error'].includes(stateRef.current)) return;+ void prewarmVoiceInputAudio(workletUrl).catch(() => undefined);+ void window.electronAPI.voiceInput.prewarm({+ sourceLanguage: voiceInputSettings.language,+ refinementEnabled: voiceInputSettings.refinementEnabled,+ }).catch(() => undefined);+ // Ordinary clicks must not trigger a new microphone permission prompt.+ if (!window.electronAPI.voiceInput.getMicrophonePermissionCached().ok) return;+ microphonePreparationRef.current?.();+ const release = prepareVoiceInputMicrophone({+ workletUrl,+ deviceId: voiceInputSettings.microphoneDeviceId ?? undefined,+ ...createVoiceInputAudioProfile(voiceInputSettings.fastActivationEnabled),+ });+ if (!release) return;+ const cleanup = () => {+ if (microphonePreparationRef.current === cleanup) microphonePreparationRef.current = null;+ release();+ };+ microphonePreparationRef.current = cleanup;+ return cleanup;+ }, [voiceInputSettings.language, voiceInputSettings.refinementEnabled,+ voiceInputSettings.microphoneDeviceId, voiceInputSettings.fastActivationEnabled]);++ useEffect(() => () => { microphonePreparationRef.current?.(); }, []);+const start = useCallback((startOptions?: VoiceInputStartOptions): boolean => {const currentState = stateRef.current;if (@@ -1280,7 +1311,8 @@ export function useVoiceInput(label: '',workletUrl,deviceId: voiceInputSettings.microphoneDeviceId ?? undefined,- fastActivationEnabled: voiceInputSettings.fastActivationEnabled,+ fastActivationEnabled: voiceInputSettings.fastActivationEnabled || !!microphonePreparationRef.current,+ temporaryMicrophone: !voiceInputSettings.fastActivationEnabled && !!microphonePreparationRef.current,getRunId: () => runIdRef.current,setEngine: (engine) => {engineRef.current = engine;@@ -1789,6 +1821,7 @@ export function useVoiceInput(getLastSubmittedText: () => lastSubmittedTextRef.current,getLastRefinement: () => lastRefinementRef.current,getState,+ prepare,start,stop: stopWithGate,cancel,diff --git a/apps/desktop/src/shared/voiceInputData.ts b/apps/desktop/src/shared/voiceInputData.tsindex f966f59afb..0c293e2646 100644--- a/apps/desktop/src/shared/voiceInputData.ts+++ b/apps/desktop/src/shared/voiceInputData.ts@@ -178,10 +178,10 @@ export type VoiceInputDictionaryLearningEvidence = Pick<export const DEFAULT_DICTIONARY_SYNC_ENABLED = true;/**- * 长按输入框语音输入的系统默认值。默认关闭:按住鼠标通常是在选字,只有用户主动- * 打开才改变这个手势的含义。+ * 长按输入框语音输入默认开启。设置页不展示开关;手动配置仍可通过+ * composerLongPressEnabledOverride 显式关闭,删除 override 恢复默认。*/-export const DEFAULT_COMPOSER_LONG_PRESS_ENABLED = false;+export const DEFAULT_COMPOSER_LONG_PRESS_ENABLED = true;export const MAX_VOICE_INPUT_REFINEMENT_INSTRUCTIONS_CHARS = 1_000;export const MAX_VOICE_INPUT_DICTIONARY_ENTRIES = 1_000;