feat(frontend): give the composer its own strip and let voice mode skip reading replies

This commit is contained in:
Chris Scott committed 2026-11-27 22:26:32 -05:00
1 parent 5f4cf63f8b
commit 6904cfbc58
15 files changed
+341 -55

No files matched your search

@@ -6,6 +6,7 @@ import type { SessionMessageAssistant } from '@opencode-manager/shared/opencode'
import type { SpeakMessageOptions, TTSContextValue } from '@/contexts/tts-context'
import type { SttEndOfSpeechReason } from '@/lib/webSpeechRecognizer'
import type { StartRecordingOptions } from '@/hooks/useSTT'
import { useVoiceConversationStore } from '@/stores/voiceConversationStore'
import { useVoiceConversation } from './useVoiceConversation'
import type { UseVoiceConversationInput, VoiceConversationCollaborators } from './useVoiceConversation'
@@ -183,6 +184,7 @@ describe('useVoiceConversation', () => {
isPaused: false,
error: null,
})
useVoiceConversationStore.setState({ readRepliesAloud: true })
sttListeners.clear()
harness.putSessionInstruction.mockResolvedValue(undefined)
harness.removeSessionInstruction.mockResolvedValue(undefined)
@@ -581,6 +583,72 @@ describe('useVoiceConversation', () => {
expect(result.current.label).toBe('Reading the reply failed')
})
it('marks replies as heard without speaking when read-back is turned off', async () => {
useVoiceConversationStore.setState({ readRepliesAloud: false })
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props, collaborators),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(harness.speakMessage).not.toHaveBeenCalled()
expect(result.current.readRepliesAloud).toBe(false)
expect(harness.startRecording).toHaveBeenCalledTimes(2)
})
it('stops reading and listens again when read-back is turned off mid-reply', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props, collaborators),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => result.current.toggleReadRepliesAloud())
expect(harness.ttsStop).toHaveBeenCalledTimes(1)
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(result.current.readRepliesAloud).toBe(false)
expect(harness.startRecording).toHaveBeenCalledTimes(2)
})
it('keeps waiting for a busy agent when read-back is turned off mid-reply', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props, collaborators),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ status: 'busy', messages: [assistantMessage('msg_1', 'Working on it')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => result.current.toggleReadRepliesAloud())
expect(harness.ttsStop).toHaveBeenCalledTimes(1)
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ status: 'idle', messages: [assistantMessage('msg_1', 'Working on it'), assistantMessage('msg_2', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(harness.speakMessage).toHaveBeenCalledTimes(1)
})
it('marks a reply with nothing to read as heard and listens again', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props, collaborators),
+51 -15
View File
@@ -31,6 +31,7 @@ import { useSTT } from '@/hooks/useSTT'
import { useTTS } from '@/hooks/useTTS'
import { sanitizeForTTS } from '@/lib/utils'
import type { SttEndOfSpeechReason } from '@/lib/webSpeechRecognizer'
import { useVoiceConversationStore } from '@/stores/voiceConversationStore'
export interface UseVoiceConversationInput {
sessionID: string
@@ -56,6 +57,8 @@ export interface VoiceConversationController {
canStopAgent: boolean
hasHeldReading: boolean
isEndpointing: boolean
readRepliesAloud: boolean
canReadRepliesAloud: boolean
notice: string | null
start: () => void
exit: () => void
@@ -64,6 +67,7 @@ export interface VoiceConversationController {
pause: () => void
resume: () => void
stopAgent: () => void
toggleReadRepliesAloud: () => void
}
interface VoiceConversationLatest {
@@ -74,6 +78,7 @@ interface VoiceConversationLatest {
sendTurn: (text: string, options: { busy: boolean }) => Promise<void>
interrupt: () => Promise<void>
onEditInTextBox: (text: string) => void
readRepliesAloud: boolean
tts: ReturnType<typeof useTTS>
}
@@ -217,6 +222,8 @@ export function useVoiceConversation(
const listInstructionKeys = collaborators.listSessionInstructionKeys ?? DEFAULT_COLLABORATORS.listSessionInstructionKeys
const tts = ttsHook()
const stt = sttHook()
const readRepliesAloud = useVoiceConversationStore((state) => state.readRepliesAloud)
const setReadRepliesAloud = useVoiceConversationStore((state) => state.setReadRepliesAloud)
const [state, dispatch] = useReducer(
reduceVoiceConversation,
@@ -258,6 +265,7 @@ export function useVoiceConversation(
sendTurn,
interrupt,
onEditInTextBox,
readRepliesAloud,
tts,
})
@@ -289,6 +297,7 @@ export function useVoiceConversation(
sendTurn,
interrupt,
onEditInTextBox,
readRepliesAloud,
tts,
}
sttRef.current = stt
@@ -553,9 +562,10 @@ export function useVoiceConversation(
}
const spokenIds = spokenIdsRef.current
const unspoken = listUnspokenMessages(messages, state.baselineId, spokenIds)
const readable = latestRef.current.tts.isEnabled
? unspoken.find((message) => sanitizeForTTS(message.text).trim() !== '')
: undefined
const readable =
latestRef.current.tts.isEnabled && latestRef.current.readRepliesAloud
? unspoken.find((message) => sanitizeForTTS(message.text).trim() !== '')
: undefined
if (readable) {
clearWorkingTimers()
dispatch({ type: 'speak-start' })
@@ -606,6 +616,7 @@ export function useVoiceConversation(
hasPendingApproval,
clearWorkingTimers,
speakMessage,
readRepliesAloud,
])
useEffect(() => {
@@ -777,6 +788,38 @@ export function useVoiceConversation(
})
}, [setHeldPlayback])
const skipReading = useCallback(() => {
const latest = latestRef.current
for (const message of listUnspokenMessages(
latest.messages,
baselineIdRef.current,
spokenIdsRef.current,
)) {
spokenIdsRef.current.add(message.id)
}
speakTokenRef.current += 1
speakingIdRef.current = null
latest.tts.stop()
dispatch({ type: 'skip' })
}, [])
const toggleReadRepliesAloud = useCallback(() => {
const next = !latestRef.current.readRepliesAloud
latestRef.current = { ...latestRef.current, readRepliesAloud: next }
setReadRepliesAloud(next)
if (next) return
if (phaseRef.current === 'speaking') {
speakTokenRef.current += 1
speakingIdRef.current = null
latestRef.current.tts.stop()
dispatch({ type: 'speech-ended' })
return
}
if (!playbackHeldRef.current) return
setHeldPlayback(false)
latestRef.current.tts.stop()
}, [setReadRepliesAloud, setHeldPlayback])
const tapOrb = useCallback(() => {
const phase = phaseRef.current
const latest = latestRef.current
@@ -793,17 +836,7 @@ export function useVoiceConversation(
return
case 'speaking':
if (latest.status === 'idle') {
for (const message of listUnspokenMessages(
latest.messages,
baselineIdRef.current,
spokenIdsRef.current,
)) {
spokenIdsRef.current.add(message.id)
}
speakTokenRef.current += 1
speakingIdRef.current = null
latest.tts.stop()
dispatch({ type: 'skip' })
skipReading()
return
}
speakTokenRef.current += 1
@@ -820,7 +853,7 @@ export function useVoiceConversation(
default:
return
}
}, [resumeConversation])
}, [resumeConversation, skipReading])
const stopAgent = useCallback(() => {
void latestRef.current.interrupt()
@@ -926,6 +959,8 @@ export function useVoiceConversation(
canStopAgent,
hasHeldReading,
isEndpointing: stt.isEndpointing,
readRepliesAloud,
canReadRepliesAloud: tts.isEnabled,
notice,
start,
exit,
@@ -934,5 +969,6 @@ export function useVoiceConversation(
pause,
resume: resumeConversation,
stopAgent,
toggleReadRepliesAloud,
}
}