Voice calls with the SDK
Build a browser voice UI: agent state, live transcript, tool activity, mute, and hangup.
KejueWebCall runs a voice conversation over WebRTC and turns everything the
agent does into typed events. You need the backend endpoint from
Web calls.
npm install @kejue/voice-web livekit-clientlivekit-client is a peer dependency, so your app owns the version.
A complete call UI
import { KejueWebCall } from '@kejue/voice-web';
// Call start() from a click handler — the user gesture unlocks audio playback.
async function onCallButtonClick() {
const call = await KejueWebCall.start({
// A function, so the token is minted fresh at connect time.
credentials: () =>
fetch('/api/start-call', { method: 'POST' }).then((r) => r.json()),
});
call.on('stateChange', (state) => orb.setState(state));
call.on('transcriptUpdate', (lines) => renderTranscript(lines));
call.on('tool', ({ name, status }) => showActivity(`${name}: ${status}`));
call.on('error', (err) => console.warn('non-fatal:', err.message));
call.on('disconnected', () => showEndScreen());
muteButton.onclick = () => call.setMuted(!call.muted);
endButton.onclick = () => call.hangup();
}start() resolves once you're connected and rejects on anything fatal — bad
credentials, unreachable network. The microphone publishes with a pre-connect
buffer, so a first word spoken before the transport is fully up isn't
clipped.
Options
| Option | Default | What it does |
|---|---|---|
credentials | — | The POST /web-calls response, or a function returning it. Prefer the function |
startMuted | false | Connect without publishing the microphone |
attachAudio | true | Route the agent's audio to the page — see below |
includeToolPayloads | false | Include raw tool arguments and output on tool events |
iceTransportPolicy | 'all' | Force 'relay' only when a deployment's media ports aren't publicly reachable |
Agent state
stateChange fires on every transition, and call.state holds the current
value: connecting, initializing, listening, thinking, speaking,
disconnected.
Events
on() returns an unsubscribe function. A listener that throws can't break
the call — its error comes back on error.
| Event | Payload | Fires when |
|---|---|---|
stateChange | AgentState | The agent's state changes |
transcript | { role, text, ts } | Once per new line |
transcriptUpdate | TranscriptLine[] | The whole transcript, after each change |
tool | { name, status, args?, output?, ts } | A tool started (running) or finished (ok / error) |
noise | { ts } | Caller audio was unintelligible and the turn was dropped |
handoff | { fromAgent?, targetAgent?, reason?, ts } | Another agent took over |
dtmf | { digitCount, reason, ts } | Keys were pressed. You get the count, never the digits |
error | Error | Something non-fatal; fatal problems reject start() |
disconnected | — | The call ended |
Render transcriptUpdate rather than appending transcript lines if you
want the simplest correct implementation — it's the authoritative version of
the conversation.
Methods
| Member | What it does |
|---|---|
setMuted(muted) | Publish or unpublish the microphone |
mute() / unmute() | Shorthand |
hangup() | End the call. Safe to call twice |
state, muted | Current values |
callId | Correlate with GET /calls/{id} |
transcript | A copy of the conversation so far |
Tool payloads are withheld by default
tool events always carry the tool's name and status — enough to
render "Checking your account…". Arguments and output need
includeToolPayloads: true.
Tool arguments and outputs are raw responses from your own systems — account numbers, balances, whatever the tool returned — and this SDK runs in your end user's browser, where devtools sees everything you let through. Enable the flag only on a trusted internal page like a support console, never one a customer can reach.
Playing the audio
With attachAudio: true (the default) the SDK attaches the agent's audio to
a hidden element and plays it.
WebRTC subscribes to the audio track but never routes it to a speaker by
itself. Skip the attach step and the call connects, the state cycles through
speaking, the transcript fills in — and your user hears nothing.
Set attachAudio: false only when you want to render the audio yourself, for
example to drive a visualiser.
Browsers block autoplay until the page has had a user gesture, which calling
start() from a click handler satisfies. If playback is still blocked, the
SDK emits an error saying so rather than leaving you with a silent call
that looks healthy.
Next
- Text chat with the SDK — the same agent as a typed chat.
- SDK reference — every export and type.