The call as a state machine
A voice call is harder to get right than a chat, because the caller and WorkBot can both speak at once. So start with the rules of the conversation alone, before any microphone or speaker is involved. Model the call as 4 states and 5 events in a pure reducer, a function that takes the current state and an event and returns the next state. Because it touches no browser objects, you can test it on its own.
Study first
- The pipeline in today's topics runs from the microphone to speech-to-text, the model, text-to-speech and the speaker. A production voice line streams at every stage and starts speaking before the model has finished. This build keeps each reply short, 300 characters at most, and speaks it whole.
- Interruption and turn-taking. The caller may speak while WorkBot is speaking, and WorkBot must then stop and listen.
- React's guide to extracting state logic into a reducer, which moves every change of state into one pure function of the state and an action. Its comparison with
useStateexplains why a pure reducer can be tested on its own, apart from any component.
Build
- Create
src/lib/voice-model.tsand defineVoiceas{ state, turns, interim }, with the statesidle,listening,thinkingandspeaking. Define the eventsstart,stop,heard,replyandspoken, where aheardevent carries the text and whether it is final. - Write the reducer
next(voice, event). A final transcript while listening adds a caller turn and moves tothinking, areplyadds a WorkBot turn and moves tospeaking, andspokenreturns tolistening. - Put the interruption rule in one branch, so that
heardwhile speaking moves tolisteningwith the words as the interim text. Ignore everything heard while idle or thinking. - Write 3 tests, for a full turn, the interruption, and the events the reducer ignores.
Done when
pnpm test src/lib/voice-modelpasses 3 tests, and the interruption test starts inspeakingand ends inlisteningwithinterimset.
Technical notes
- The reducer holds no browser objects, so the component is the only place that touches the microphone, the speech recognizer and the speech synthesizer.
- Ignoring words heard while thinking is a choice. The model is already answering what the caller said, and a final transcript at that moment would otherwise start a second request in parallel.
interimholds the caller's words so far, shown in italics, and the reducer clears it when a final transcript arrives or the call stops.


