Browser client
The browser client connects a web page to one session: it opens the mic, connects to the engine over WebRTC (falling
back to WebSocket when WebRTC can't connect), plays the agent's voice on an <audio> element and hands you live
events. For the walkthrough, see the tutorial.
Getting the client
The client is TypeScript source, not yet a published npm package. Copy the voice-engine repo's packages/client/src
(without the tests) into your app, with what it imports from the protocol:
packages/protocol/src/events.ts, whole;- from
packages/protocol/src/api.ts, only theIceServerandConnectResponsetypes. Copy those two into a file of your own:api.tsitself imports the rest of the protocol.
Point the @voice-engine/protocol/events and @voice-engine/protocol/api imports at them. It depends on zod. Note
the commit you copied from, and copy again when the protocol changes.
Connecting
connectEngineSession(options) resolves once audio flows, and rejects with EngineConnectError if neither transport
connects. If the participant refuses the mic, it rejects with the browser's own error (NotAllowedError). Call it from a click: browsers only open the mic and play audio after the user did something on the page.
| Option | Required | Meaning |
|---|---|---|
engineUrl | yes | engine_url from starting the session. |
sessionId | yes | session_id from starting the session. |
clientToken | yes | client_token from starting the session. |
iceServers | yes | ice_servers from starting the session. |
audioElement | yes | The <audio> element that plays the agent's voice. |
onEvent(event) | yes | Receives every live event. |
onStateChange(state) | no | connecting → connected → disconnected. disconnected is final. |
onMicCaptureChange(capturing) | no | Push-to-talk only: the mic started or stopped capturing, for the talk button's look. |
micStream | no | A mic capture your page already has (for a recording, say). The client clones its track instead of asking for the mic again. |
transport | no | auto (default): WebRTC, falling back to WebSocket. webrtc or websocket forces one. |
backstopMs | no | With auto, how long WebRTC gets to connect before the fallback. Default 8000. |
The connection hangs up by itself when the page is closed.
Live events
What onEvent receives:
| Event | When |
|---|---|
session.started | The session is live. On a push-to-talk session it carries push_to_talk (an empty object for now). |
participant.speech.started / .ended | The participant started or stopped speaking. |
agent.speech.started / .ended | The agent started or stopped speaking. interrupted and heard_ms say if the participant cut in. |
transcript.delta / transcript.done | Text of a turn as it comes in, then the whole turn. inaudible marks a participant turn that couldn't be heard: show a placeholder. |
transcript.removed | A turn was taken back. Remove it from the screen. |
tool.called | The agent called a tool that goes to the browser. |
frame.request | Vision sessions: the agent wants a camera or screen frame, at most max_width wide when given. Answer with a frame (below). |
sidecar.result | A helper model's result, such as what it read from the camera. A helper that runs as a decision sends its answers too (data.answers). |
metrics | An agent turn's latency, once it starts playing. Ignore it unless you chart it. |
session.ending / session.ended | The session is closing, with an end_reason. |
error | Something went wrong; fatal says whether the session is over. |
Ignore event types you don't know: new ones may be added. Every event's fields are in Live protocol.
To answer a frame.request, send the image as a data:image/… URL (JPEG is best, up to 1,500,000 characters) with
when you grabbed it, in milliseconds since the epoch:
connection.send({ type: 'frame', request_id: event.request_id, data_url, captured_at: Date.now() });
Ending the conversation
The agent can end the conversation itself: you then get session.ending and session.ended with agent_ended. To end
it from the page, send session.end. Add finished: true when the conversation had reached its natural end (the agent
wrapped up and the participant moved on), so the session counts as completed:
connection.send({ type: 'session.end', finished: true });
Tools
A tool set to go to the Participant's browser arrives as tool.called. When event.await_result is true the agent
waits for your answer: send it back on the same connection before the tool's timeout, or the agent hears
"unavailable".
case 'tool.called':
if (event.name === 'show_next_step') {
goToStep(event.args.step);
if (event.await_result) {
connection?.send({ type: 'tool.result', call_id: event.call_id, result: { shown: true } });
}
}
break;
If the tool couldn't do its work, say so with failed: true (result can be null): the agent hears what the tool
says for a failure, as when it times out.
A tool that goes to both the browser and a webhook is only announced to the browser; the webhook's answer is the one the agent gets.
Push-to-talk
The agent's version says whether its sessions are push-to-talk: on, off, or read from a variable (send
variables.push_to_talk: true). Nothing else decides: there is no session option for it. session.started says which a
session runs with.
Wire your talk button to connection.pushToTalk.press() and .release(). The client opens the mic on the press and
mutes it on the release. The engine takes the answer as soon as the audio from before the release has arrived, so
nothing said after the release is sent, and the agent starts replying right away. onMicCaptureChange tells you when
the mic is actually capturing.
To send the messages yourself instead, see Push-to-talk in the live protocol.
Other controls
| Call | Does |
|---|---|
connection.setMicEnabled(false) | Sends silence instead of the mic (after the conversation, say). |
connection.setOutputMuted(true) | Silences the speakers. The audio element still carries the agent's voice, for a recording. |
connection.send(message) | Sends a message to the engine, such as session.end, tool.result, or text.input for a turn typed instead of spoken. Every message is in Live protocol. |
connection.close() | Hangs up. |
Without the JavaScript client
A client that can't use this one (a native app, another language) speaks the engine's protocol itself: connecting over WebRTC or a WebSocket, how audio is framed on each, and every message both ways are in Live protocol.