feat(frontend): hands-free voice conversation mode in the composer

The mic opens a voice loop that listens, sends on end of speech, reads each completed reply and reopens the mic when the agent is idle. The phase reducer, pause options and voice-mode instruction move to the shared package. TTS gains per-message completion and pause/resume. The old tap-and-drag recorder, its strip and its CSS are removed.
This commit is contained in:
Chris Scott committed 2026-10-22 20:18:16 -04:00
1 parent 80ef211a94
commit 5d8b2c9445
34 files changed
+4876 -1302

No files matched your search

@@ -0,0 +1,97 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { act, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { VOICE_PAUSE_STORAGE_KEY } from '@opencode-manager/shared/opencode'
import { STTSettings } from './STTSettings'
import { useSettings } from '@/hooks/useSettings'
import { useSTT } from '@/hooks/useSTT'
import { isWebRecognitionSupported, getAvailableLanguages } from '@/lib/webSpeechRecognizer'
import { createUseSettingsMock } from '@/test/test-utils'
import { DEFAULT_STT_CONFIG, DEFAULT_USER_PREFERENCES, type STTConfig, type UserPreferences } from '@/api/types/settings'
vi.mock('@/hooks/useSettings')
vi.mock('@/hooks/useSTT')
vi.mock('@/lib/webSpeechRecognizer')
vi.mock('@/api/stt', () => ({
sttApi: {
getModels: vi.fn().mockResolvedValue({ models: [] }),
transcribe: vi.fn(),
},
}))
function buildPreferences(stt: Partial<STTConfig> = {}): UserPreferences {
return {
...DEFAULT_USER_PREFERENCES,
stt: { ...DEFAULT_STT_CONFIG, ...stt },
}
}
function mockHooks(preferences: UserPreferences = buildPreferences({ enabled: true, provider: 'builtin' })) {
vi.mocked(useSettings).mockReturnValue(createUseSettingsMock({ preferences }))
vi.mocked(useSTT).mockReturnValue({
startRecording: vi.fn(),
stopRecording: vi.fn(),
abortRecording: vi.fn(),
isRecording: false,
isProcessing: false,
transcript: '',
interimTranscript: '',
error: null,
isExternalProvider: preferences.stt?.provider === 'external',
} as ReturnType<typeof useSTT>)
vi.mocked(isWebRecognitionSupported).mockReturnValue(true)
vi.mocked(getAvailableLanguages).mockReturnValue(['en-US'])
}
function getPauseSelect() {
return screen.queryByRole('combobox', { name: 'Pause before sending' })
}
async function renderSettings(preferences?: UserPreferences) {
if (preferences) {
mockHooks(preferences)
}
const rendered = render(<STTSettings />)
await act(async () => {})
return rendered
}
describe('STTSettings pause before sending', () => {
beforeEach(() => {
vi.clearAllMocks()
window.localStorage.clear()
mockHooks()
})
it('shows the pause select for the enabled built-in provider', async () => {
await renderSettings()
expect(getPauseSelect()).toHaveValue('1.2 s')
})
it('hides the pause select for the external provider', async () => {
await renderSettings(buildPreferences({ enabled: true, provider: 'external' }))
expect(getPauseSelect()).not.toBeInTheDocument()
})
it('hides the pause select when STT is disabled', async () => {
await renderSettings(buildPreferences({ enabled: false, provider: 'builtin' }))
expect(getPauseSelect()).not.toBeInTheDocument()
})
it('writes the selected pause to device-local storage', async () => {
const user = userEvent.setup()
await renderSettings()
const select = getPauseSelect()
expect(select).toHaveValue('1.2 s')
await user.click(select as HTMLElement)
await user.click(screen.getByRole('option', { name: '3.0 s' }))
expect(window.localStorage.getItem(VOICE_PAUSE_STORAGE_KEY)).toBe('3000')
expect(getPauseSelect()).toHaveValue('3.0 s')
})
})
@@ -4,13 +4,16 @@ import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
import { useSettings } from '@/hooks/useSettings'
import { useSTT } from '@/hooks/useSTT'
import { useVoicePauseSetting } from '@/hooks/useVoicePauseSetting'
import { isWebRecognitionSupported, getAvailableLanguages } from '@/lib/webSpeechRecognizer'
import { sttApi } from '@/api/stt'
import { Mic, Loader2, XCircle, CheckCircle2, RefreshCw, Eye, EyeOff, MicOff } from 'lucide-react'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Combobox } from '@/components/ui/combobox'
import { DEFAULT_STT_CONFIG } from '@/api/types/settings'
import { VOICE_PAUSE_OPTIONS_MS, formatVoicePauseMs } from '@opencode-manager/shared/opencode'
const sttFormSchema = z.object({
enabled: z.boolean(),
@@ -35,9 +38,15 @@ const sttFormSchema = z.object({
type STTFormValues = z.infer<typeof sttFormSchema>
const voicePauseOptions = VOICE_PAUSE_OPTIONS_MS.map((ms) => ({
value: String(ms),
label: formatVoicePauseMs(ms),
}))
export function STTSettings() {
const { preferences, updateSettings } = useSettings()
const { startRecording, stopRecording, abortRecording, isRecording, isProcessing, transcript, interimTranscript, error: sttError, isExternalProvider } = useSTT()
const { pauseMs, setPauseMs } = useVoicePauseSetting()
const [saveStatus, setSaveStatus] = useState<'idle' | 'saving' | 'saved' | 'error'>('idle')
const [isTesting, setIsTesting] = useState(false)
@@ -375,6 +384,21 @@ export function STTSettings() {
</FormItem>
)}
/>
<FormItem>
<Label>Pause before sending</Label>
<Combobox
value={String(pauseMs)}
onChange={(value) => setPauseMs(Number(value))}
options={voicePauseOptions}
placeholder="Select pause..."
allowCustomValue={false}
ariaLabel="Pause before sending"
/>
<p className="text-sm text-muted-foreground">
Voice conversation sends after this much silence. Uses your browser's speech recognition; other providers send when you tap.
</p>
</FormItem>
</>
)}
@@ -74,6 +74,8 @@ function mockHooks(preferences: UserPreferences = buildPreferences()) {
speakMessage: vi.fn(),
queueMessage: vi.fn(),
stop: vi.fn(),
pause: vi.fn(),
resume: vi.fn(),
state: 'idle',
error: null,
currentText: null,
@@ -83,6 +85,7 @@ function mockHooks(preferences: UserPreferences = buildPreferences()) {
isPlaying: false,
isLoading: false,
isIdle: true,
isPaused: false,
})
vi.mocked(useTTSDiscovery).mockReturnValue({
refreshModels: vi.fn(),