Skip to main content

VoiceAgent

VoiceAgent is the main entry point. It manages microphone capture (AudioWorklet), scheduled PCM playback, and session lifecycle on top of any Transport.


Factory methods

VoiceAgent.withWebSocket(config, options)

const agent = VoiceAgent.withWebSocket(
{ url: 'ws://localhost:8000/ws' },
{ onStatus, onMessage, onError }
);

VoiceAgent.withWebRTC(config, options)

const agent = VoiceAgent.withWebRTC(
{ baseUrl: 'http://localhost:8000', metadata: { session_type: 'default' } },
{ onStatus }
);

new VoiceAgent(transport, options)

Pass any Transport — including a TransportSwitcher — directly.


Options

OptionTypeDescription
onStatus(status: SessionStatus) => voididle → connecting → connected → listening → speaking → ended
onMessage(msg: BotMessage) => voidJSON control messages from the server
onQoS(snap: QoSSnapshot) => voidLive quality metrics (requires QoSMonitor)
onError(err: Error) => voidTransport-level errors
onLog(msg: string) => voidInternal diagnostic log

Methods

MethodDescription
connect()Opens transport + requests mic permission
disconnect()Closes transport + releases mic
setMicEnabled(enabled)Mute / unmute microphone
sendMessage(msg)Send a JSON control message to the server

Audio path (WebSocket)

getUserMedia (16 kHz, mono, AEC+NS)
└── AudioWorklet "pcm-sender"
├── Int16Array conversion
└── sendAudio(buffer) → WebSocket binary frame

WebSocket binary frame (PCM16 from server)
└── Int16Array → Float32Array
└── AudioBufferSourceNode (scheduled, gapless)
└── MediaStreamDestinationNode (AEC reference)