fix(tts): let voice mode read replies aloud on iOS

iOS rejects play() outside a user gesture unless the same media element was
already played inside one. Each chunk used a new Audio element, so replies in
voice mode failed with "Reading the reply failed".

- reuse one audio element for all external TTS playback
- add tts.unlock(), which plays a short silent clip on that element, and call
  it from the voice mode start and orb taps
- route every playback failure through failPlayback(message) and pass the
  message to onEnd so voice mode shows the underlying reason
- report a blocked play() as "Audio playback was blocked by the browser"
This commit is contained in:
Chris Scott committed 2026-10-23 14:08:33 -04:00
1 parent 10190940bd
commit 67cfd862b1
8 files changed
+354 -190

No files matched your search

@@ -39,6 +39,7 @@ const harness = vi.hoisted(() => ({
abortRecording: vi.fn(),
speakMessage: vi.fn(),
ttsStop: vi.fn(),
ttsUnlock: vi.fn(),
ttsPause: vi.fn(),
ttsResume: vi.fn(),
sendTurn: vi.fn(),
@@ -80,6 +81,7 @@ const ttsValue = {
error: null as string | null,
speakMessage: harness.speakMessage,
stop: harness.ttsStop,
unlock: harness.ttsUnlock,
pause: harness.ttsPause,
resume: harness.ttsResume,
}
@@ -118,9 +120,9 @@ async function endOfSpeech(reason: SttEndOfSpeechReason, finalText: string) {
})
}
function speechEndHandler(callIndex = 0): (reason: TTSEndReason) => void {
function speechEndHandler(callIndex = 0): (reason: TTSEndReason, error?: string) => void {
const options = harness.speakMessage.mock.calls[callIndex]?.[2] as
| { onEnd?: (reason: TTSEndReason) => void }
| { onEnd?: (reason: TTSEndReason, error?: string) => void }
| undefined
if (!options?.onEnd) throw new Error('speakMessage was not called with an onEnd handler')
return options.onEnd
@@ -202,6 +204,7 @@ describe('useVoiceConversation', () => {
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(result.current.isActive).toBe(true)
expect(harness.ttsUnlock).toHaveBeenCalledTimes(1)
expect(result.current.label).toBe('Listening')
expect(harness.startRecording).toHaveBeenCalledTimes(1)
expect(lastStartOptions().endpointing).toEqual({ pauseMs: 1200, noSpeechTimeoutMs: 10_000 })
@@ -283,6 +286,7 @@ describe('useVoiceConversation', () => {
act(() => result.current.tapOrb())
await waitFor(() => expect(result.current.phase).toBe('recording'))
expect(harness.ttsUnlock).toHaveBeenCalledTimes(2)
expect(harness.startRecording).toHaveBeenCalledTimes(1)
expect(result.current.label).toBe('Adding to queue')
@@ -546,12 +550,34 @@ describe('useVoiceConversation', () => {
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => speechEndHandler(0)('error', 'Audio playback was blocked by the browser'))
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe(
'Reading the reply failed: Audio playback was blocked by the browser',
)
expect(result.current.hint).toBe('Tap to retry')
expect(harness.startRecording).toHaveBeenCalledTimes(1)
})
it('keeps the failure label plain when the player reports no detail', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ 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(() => speechEndHandler(0)('error'))
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe('Reading the reply failed')
expect(result.current.hint).toBe('Tap to retry')
expect(harness.startRecording).toHaveBeenCalledTimes(1)
})
it('marks a reply with nothing to read as heard and listens again', async () => {
+8 -2
View File
@@ -543,12 +543,16 @@ export function useVoiceConversation(
speakingIdRef.current = message.id
spokenIdsRef.current.add(message.id)
void latestRef.current.tts.speakMessage(message.id, message.text, {
onEnd: (reason) => {
onEnd: (reason, error) => {
if (speakTokenRef.current !== token) return
if (reason === 'error') {
speakTokenRef.current += 1
speakingIdRef.current = null
dispatch({ type: 'error', message: REPLY_READ_FAILED_MESSAGE, source: 'playback' })
dispatch({
type: 'error',
message: error ? `${REPLY_READ_FAILED_MESSAGE}: ${error}` : REPLY_READ_FAILED_MESSAGE,
source: 'playback',
})
return
}
speakingIdRef.current = null
@@ -706,6 +710,7 @@ export function useVoiceConversation(
}
const latest = latestRef.current
if (latest.tts.isPlaying) latest.tts.stop()
latest.tts.unlock()
fireImpactHaptic()
invalidateTurn()
instructionEpochRef.current += 1
@@ -851,6 +856,7 @@ export function useVoiceConversation(
const tapOrb = useCallback(() => {
const phase = phaseRef.current
const latest = latestRef.current
latest.tts.unlock()
switch (phase) {
case 'listening':
void endTurnRef.current?.()