diff --git a/frontend/src/hooks/useVoiceConversation.test.tsx b/frontend/src/hooks/useVoiceConversation.test.tsx index a70b0c90..b921758b 100644 --- a/frontend/src/hooks/useVoiceConversation.test.tsx +++ b/frontend/src/hooks/useVoiceConversation.test.tsx @@ -1,7 +1,7 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest' +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { act, renderHook, waitFor } from '@testing-library/react' import { useReducer, useEffect } from 'react' -import { VOICE_MODE_INSTRUCTION_KEY } from '@opencode-manager/shared/opencode' +import { VOICE_MODE_INSTRUCTION_KEY, VOICE_NO_SPEECH_TIMEOUT_MS } from '@opencode-manager/shared/opencode' import type { SessionMessageAssistant } from '@opencode-manager/shared/opencode' import type { TTSEndReason } from '@/contexts/tts-context' import type { SttEndOfSpeechReason } from '@/lib/webSpeechRecognizer' @@ -17,6 +17,7 @@ interface MockSttValues { isRecording: boolean isProcessing: boolean isEndpointing: boolean + isExternalProvider: boolean isEnabled: boolean isSupported: boolean isError: boolean @@ -60,6 +61,7 @@ const sttValue: MockSttValues = { isRecording: false, isProcessing: false, isEndpointing: false, + isExternalProvider: false, isEnabled: true, isSupported: true, isError: false, @@ -158,6 +160,7 @@ describe('useVoiceConversation', () => { isRecording: false, isProcessing: false, isEndpointing: false, + isExternalProvider: false, isEnabled: true, isSupported: true, isError: false, @@ -183,6 +186,10 @@ describe('useVoiceConversation', () => { harness.startRecording.mockResolvedValue(true) }) + afterEach(() => { + vi.useRealTimers() + }) + it('starts listening and opens capture with the pause budget when idle', async () => { const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), { initialProps: createProps(), @@ -366,6 +373,63 @@ describe('useVoiceConversation', () => { expect(result.current.canEditInTextBox).toBe(false) }) + it('keeps an external-provider dictation open past the no-speech timeout', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) + const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), { + initialProps: createProps(), + }) + + act(() => result.current.start()) + await waitFor(() => expect(result.current.phase).toBe('listening')) + + updateStt({ isExternalProvider: true, isRecording: true, interimTranscript: 'Recording...' }) + + act(() => { + vi.advanceTimersByTime(VOICE_NO_SPEECH_TIMEOUT_MS + 2_000) + }) + + expect(harness.abortRecording).not.toHaveBeenCalled() + expect(result.current.phase).toBe('listening') + expect(result.current.label).toBe('Listening') + }) + + it('keeps an external-provider capture running when the session starts a turn', async () => { + const { result, rerender } = renderHook( + (props: UseVoiceConversationInput) => useVoiceConversation(props), + { initialProps: createProps() }, + ) + + act(() => result.current.start()) + await waitFor(() => expect(result.current.phase).toBe('listening')) + + updateStt({ isExternalProvider: true, isRecording: true, interimTranscript: 'Recording...' }) + rerender(createProps({ status: 'busy' })) + + expect(harness.abortRecording).not.toHaveBeenCalled() + expect(result.current.phase).toBe('listening') + expect(result.current.label).toBe('Listening') + }) + + it('pauses when the browser provider captures no speech for the timeout', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) + const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), { + initialProps: createProps(), + }) + + act(() => result.current.start()) + await waitFor(() => expect(result.current.phase).toBe('listening')) + + updateStt({ isRecording: true }) + + act(() => { + vi.advanceTimersByTime(VOICE_NO_SPEECH_TIMEOUT_MS) + }) + + expect(harness.abortRecording).toHaveBeenCalled() + expect(result.current.phase).toBe('paused') + expect(result.current.label).toBe("Didn't catch that") + }) + it('pauses without a message when the turn produces no speech', async () => { const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), { initialProps: createProps(), diff --git a/frontend/src/hooks/useVoiceConversation.ts b/frontend/src/hooks/useVoiceConversation.ts index 3d9b4793..f038167e 100644 --- a/frontend/src/hooks/useVoiceConversation.ts +++ b/frontend/src/hooks/useVoiceConversation.ts @@ -587,7 +587,7 @@ export function useVoiceConversation( clearListeningTimers() return } - if (interimText || turnTextValue) { + if (stt.isExternalProvider || interimText || turnTextValue) { clearTimeoutRef(noSpeechTimerRef) } else if (noSpeechTimerRef.current === null) { noSpeechTimerRef.current = setTimeout(() => { @@ -607,6 +607,7 @@ export function useVoiceConversation( state.phase, stt.isRecording, stt.isEndpointing, + stt.isExternalProvider, interimText, turnTextValue, handleEndOfSpeech, @@ -682,10 +683,20 @@ export function useVoiceConversation( useEffect(() => { if (state.phase !== 'listening' || status === 'idle') return + if (stt.isExternalProvider && (stt.isRecording || stt.isProcessing)) return if (turnTextValue || interimText) return sttRef.current?.abortRecording() dispatch({ type: 'turn-started', baselineId: currentTurnBaseline(messages) }) - }, [state.phase, status, interimText, turnTextValue, messages]) + }, [ + state.phase, + status, + interimText, + turnTextValue, + messages, + stt.isExternalProvider, + stt.isRecording, + stt.isProcessing, + ]) const start = useCallback(() => { if (phaseRef.current !== 'off') return