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(voice): 避免普通点击输入框提前打开麦克风

Dashca535f20313f
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.ts
index 60da41b906..b9b64c5f28 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
@@ -54,7 +54,9 @@ vi.mock('../WebMicAudioEngine', () => ({
constructor(readonly options: { keepAlive: boolean }) {
mocks.engines.push(this);
}
- get keepAlive() { return this.options.keepAlive; }
+ get keepAlive() {
+ return this.options.keepAlive;
+ }
onPcm16k(callback: (chunk: PcmChunk) => void) {
this.emit = callback;
}
@@ -81,6 +83,10 @@ vi.mock('../WebMicAudioEngine', () => ({
}));
import { useVoiceInput } from '../useVoiceInput';
+import {
+ createComposerLongPressVoiceGesture,
+ COMPOSER_LONG_PRESS_VOICE_INPUT_MS,
+} from '../composerLongPressGesture';
function deferred<T>() {
let resolve!: (value: T) => void;
@@ -164,29 +170,65 @@ afterEach(() => {
});
describe('local voice capture independent of startup checks and cloud', () => {
- it('prepares only authorized devices without claiming recording or starting ASR', async () => {
+ it('keeps an authorized microphone closed through a short composer click', 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();
+ h.api.prewarm.mockClear();
+ const gesture = createComposerLongPressVoiceGesture({
+ getState: () => h.result.current.state,
+ prepare: () => h.result.current.prepare(),
+ start: () => h.result.current.start(),
+ stop: () => h.result.current.stop(),
+ });
+ gesture.press({ x: 0, y: 0 });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(COMPOSER_LONG_PRESS_VOICE_INPUT_MS - 1);
+ });
+ gesture.release();
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(COMPOSER_LONG_PRESS_VOICE_INPUT_MS);
+ });
+ expect(h.api.prewarm).toHaveBeenCalledOnce();
+ // Opening even an authorized device can switch Bluetooth headphones to call audio.
+ expect(mocks.prepareMicrophone).not.toHaveBeenCalled();
+ expect(mocks.startEngine).not.toHaveBeenCalled();
+ expect(h.api.muteSystemAudio).not.toHaveBeenCalled();
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();
+ gesture.dispose();
});
- it('releases unused press preparation when the composer unmounts', () => {
+ it('opens the microphone only after the composer hold threshold', async () => {
const h = mount();
h.api.getMicrophonePermissionCached.mockReturnValue({ ok: true, status: 'granted' });
- h.result.current.prepare();
- h.unmount();
- expect(mocks.releasePreparation).toHaveBeenCalledOnce();
+ const gesture = createComposerLongPressVoiceGesture({
+ getState: () => h.result.current.state,
+ prepare: () => h.result.current.prepare(),
+ start: () => h.result.current.start(),
+ stop: () => h.result.current.stop(),
+ });
+ gesture.press({ x: 0, y: 0 });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(COMPOSER_LONG_PRESS_VOICE_INPUT_MS - 1);
+ });
+ expect(mocks.prepareMicrophone).not.toHaveBeenCalled();
+ expect(mocks.startEngine).not.toHaveBeenCalled();
expect(h.api.start).not.toHaveBeenCalled();
+ await act(async () => {
+ h.mute.resolve({ ok: true });
+ h.connection.resolve({ ok: true, runId: 'held-composer' });
+ await vi.advanceTimersByTimeAsync(1);
+ });
+ expect(mocks.startEngine).toHaveBeenCalledOnce();
+ expect(mocks.engines[0].keepAlive).toBe(false);
+ expect(h.result.current.state).toBe('listening');
+ await act(async () => {
+ gesture.release();
+ });
+ expect(mocks.stopEngine).toHaveBeenCalled();
+ expect(h.result.current.state).toBe('done');
+ gesture.dispose();
});
it('ends before the microphone produces its first frame and closes the late microphone', async () => {
diff --git a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts b/apps/desktop/src/renderer/voice-input/useVoiceInput.ts
index fdad5e1576..70da517928 100644
--- a/apps/desktop/src/renderer/voice-input/useVoiceInput.ts
+++ b/apps/desktop/src/renderer/voice-input/useVoiceInput.ts
@@ -44,7 +44,6 @@ import {
isSelectedMicrophoneUnavailableError,
prewarmVoiceInputBenchmarkFixture,
prewarmVoiceInputMicrophoneWithAutomaticFallback,
- prepareVoiceInputMicrophone,
type PcmChunk,
} from './WebMicAudioEngine';
import { prewarmVoiceInputAudio } from './audioContextPool';
@@ -146,7 +145,8 @@ export type UseVoiceInputResult = {
getLastRefinement: () => VoiceInputRefinementSnapshot | null;
/** 同步读取当前状态,不经过 React 渲染。长按手势靠它判断是否占用了录音。 */
getState: () => VoiceInputState;
- prepare: () => (() => void) | void;
+ /** 仅预热音频处理与服务连接,不打开麦克风。 */
+ prepare: () => void;
/** 同步 claim 录音后立刻返回是否占用;启动流程(授权确认、采集、连接)在后台继续。 */
start: (options?: VoiceInputStartOptions) => boolean;
stop: (options?: VoiceInputStopOptions) => Promise<void>;
@@ -198,7 +198,6 @@ 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);
@@ -1242,30 +1241,15 @@ export function useVoiceInput(
const prepare = useCallback(() => {
if (disabledRef.current || !editorRef.current || editorRef.current.isDestroyed) return;
if (!['idle', 'done', 'error'].includes(stateRef.current)) return;
+ // Every composer mouse-down reaches here, including ordinary clicks and
+ // text selection. Opening even an authorized microphone can switch Bluetooth
+ // headphones to call audio, so leave device capture to start() after the hold.
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?.(); }, []);
+ }, [voiceInputSettings.language, voiceInputSettings.refinementEnabled]);
const start = useCallback((startOptions?: VoiceInputStartOptions): boolean => {
const currentState = stateRef.current;
@@ -1311,8 +1295,7 @@ export function useVoiceInput(
label: '',
workletUrl,
deviceId: voiceInputSettings.microphoneDeviceId ?? undefined,
- fastActivationEnabled: voiceInputSettings.fastActivationEnabled || !!microphonePreparationRef.current,
- temporaryMicrophone: !voiceInputSettings.fastActivationEnabled && !!microphonePreparationRef.current,
+ fastActivationEnabled: voiceInputSettings.fastActivationEnabled,
getRunId: () => runIdRef.current,
setEngine: (engine) => {
engineRef.current = engine;