Skip to Content

Text chat with the SDK

Build a chat UI for the same agent your callers reach — send messages, stream replies, handle interruptions.

KejueTextCall runs the same agent a voice call reaches, as a typed conversation. No microphone, so no mute, no audio routing, and no autoplay gesture to worry about.

Mint credentials with modality: "text" — see Web calls.

Beta — text chat is enabled per workspace. Ask us to turn it on for yours.

A complete chat UI

import { KejueTextCall } from '@kejue/voice-web';

const chat = await KejueTextCall.start({
  credentials: () =>
    fetch('/api/start-chat', { method: 'POST' }).then((r) => r.json()),
});

chat.on('stateChange', (s) => setBusy(s === 'thinking' || s === 'replying'));
chat.on('transcriptUpdate', (lines) => setMessages(lines)); // authoritative
chat.on('delta', ({ generation, text }) => appendToDraft(generation, text));
chat.on('replyEnd', () => clearDraft());
chat.on('messageAck', ({ id }) => markDelivered(id));
chat.on('disconnected', () => showEndScreen());

sendButton.onclick = async () => {
  const id = await chat.send(input.value);
  showPendingBubble(id, input.value);
  input.value = '';
};

// End the conversation when the user closes the chat.
window.addEventListener('pagehide', () => chat.hangup());

send(text) resolves once the message is on the wire and returns its id — mark your optimistic bubble delivered when messageAck arrives with that id.

Empty messages and messages over 4,000 characters are refused rather than clipped, because a truncated message would be answered as though it were whole.

Agent state

The vocabulary differs from voice on purpose — listening and speaking describe a microphone and a speaker, which a chat doesn't have.

connecting, initializing, idle (waiting for the person to type), thinking, replying, disconnected.

Events

EventPayloadFires when
stateChangeTextAgentStateThe agent's state changes
transcript{ role, text, ts }Once per new line
transcriptUpdateTranscriptLine[]The whole conversation. Render this
delta{ generation, text, ts }A chunk of the reply being written, for a live-typing effect
replyEnd{ generation, interrupted, ts }The reply finished
messageAck{ id, ts }The agent accepted your message
toolToolActivityAs on voice calls; payloads withheld by default
handoff{ fromAgent?, targetAgent?, reason?, ts }Another agent took over, continuing the conversation
errorErrorNon-fatal — a refused message, a failure mid-reply
disconnected—The conversation ended

Streaming versus the transcript

transcriptUpdate is the source of truth. delta is an accelerator for the typing effect, and each chunk is superseded by the transcriptUpdate that follows — a client that ignores delta entirely is still correct.

Each delta carries a generation identifying which reply it belongs to; discard chunks older than the newest generation you've seen.

Deltas are optional. With guardrails that check the finished reply, nothing is released until it passes, so replies arrive whole and no deltas are emitted. Build for both.

Interruptions

Sending a new message while the agent is replying interrupts the reply — the text equivalent of barging in. The abandoned reply ends with replyEnd.interrupted: true; the next transcriptUpdate shows what stands. Treat the interrupted draft as void: clear it, don't promote it to a bubble.

End the conversation deliberately

A text call is metered exactly like a voice call — minutes, from connect to end, against the same web-call concurrency pool. A chat window left open is a call left open.

Call hangup() when the user closes or navigates away. We also close conversations idle for 15 minutes and reap ones whose browser has gone, but both bill until they fire — neither substitutes for ending deliberately.

hangup() tells the agent the conversation is over before dropping the transport, so the record ends with a clean reason, not a disconnect.

Next

On this page