Sending and receiving
Two ways to ask, one code path underneath.
What happens when you ask
Before any code, the shape of one turn. It explains every rule on this page: why errors come in two places, why the id arrives first, and why cancelling does not take a message back.
ask() — the answer as it is written
An async generator. Loop over it with for await and append each piece as it comes.
Minimal
const chat = tigerbase.conversation();
for await (const event of chat.ask("Do you deliver to Leeds?")) {
if (event.text) process.stdout.write(event.text);
}Realistic
Harbor & Pine’s panel keeps the id for the thumbs, paints each piece into the last bubble, and reacts when the agent decides to fetch a person.
async function send(question: string) {
const bubble = addBubble({ role: "agent", text: "" });
setBusy(true);
try {
for await (const event of chat.ask(question)) {
if (event.messageId) bubble.id = event.messageId; // arrives first
if (event.text) bubble.text += event.text; // in order
if (event.handoff) showBanner("Connecting you to the team…");
if (event.done) break;
repaint(bubble);
}
} finally {
setBusy(false);
}
}askOnce() — wait for all of it
The same turn, collected into one string and trimmed. For a caller with nothing to paint into as it arrives — a script, a test, a form that shows a spinner and then the reply.
const answer = await chat.askOnce("What is your returns policy on sofas?");// "Ask about this item" on the oak dining table's page
const answer = await chat.askOnce(
`Is the ${product.name} available in walnut, and how long is delivery?`,
);
productFaq.textContent = answer;NoteaskOnce is ask, collected
The events
Each event is an object with whichever of these fields that frame carried. Check the one you care about; ignore the rest.
| messageId | The server’s id for this answer, sent before the first word. Keep it — it is what rate() needs. |
|---|---|
| text | A piece of the answer. Append it; they arrive in order. |
| handoff | { id, status }. A person was summoned — by the agent mid-answer, or because one is already on this conversation. The agent stops; see Handing to a person. |
| agentSwitch | A different agent in the account took over the answer. Typed as unknown; useful if you show who is speaking. |
| confirmation | { id, toolName, description, args }. The agent wants to run a tool you marked “ask first”, and nothing has run yet. Followed by done; answer it with confirm(), below. |
| done | The turn is over. |
NoteWhy messageId comes first
While a person has the conversation
Once a person has taken over, the agent stays out of it. ask() still sends the visitor’s message — it is recorded and pushed to whoever is handling it — and the turn comes back as a single event carrying handoff and done, with no text. Do not show an empty agent bubble for it.
for await (const event of chat.ask(question)) {
if (event.handoff && event.done) {
removeEmptyBubble(); // the person will answer, not the agent
}
}confirm() — when the agent asks first
A tool marked “ask first” does not run on the agent’s say-so. The turn ends with a confirmation event instead: show its description with a yes and a no, and pass the answer to chat.confirm(id, approve). It returns the same events as ask(), so the same loop paints the reply.
for await (const event of chat.ask("Cancel order 1234")) {
if (event.text) bubble.text += event.text;
if (event.confirmation?.id) {
const card = showCard(event.confirmation.description); // "Cancel order #1234?"
card.onAnswer = async (approve: boolean) => {
card.disable(); // a token answers once
for await (const next of chat.confirm(event.confirmation!.id!, approve)) {
if (next.text) reply.text += next.text;
}
};
}
}| description | The sentence to show. toolName is the model's internal name for the action; keep it for logs. |
|---|---|
| id | A single-use token, valid for ten minutes. Null only when the turn had no visitor id — then it cannot be confirmed, so show the description and say so. |
| Do not also ask() | The server writes the visitor's yes or no into the conversation itself. Sending it as a message too records it twice. |
| A spent token | Answering twice, or after ten minutes, throws confirmation_expired before any event. Nothing ran; ask the agent again. |
Cancelling
Somebody types a new question while the last one is still arriving. Without an abort you get two answers interleaved into one bubble.
let abort: AbortController | null = null;
async function send(question: string) {
abort?.abort(); // stop reading the previous one
abort = new AbortController();
try {
for await (const ev of chat.ask(question, { signal: abort.signal })) {
if (ev.text) append(ev.text);
}
} catch (err) {
if (err.code === "aborted") return; // you asked for this
throw err;
}
}WarningAn aborted turn still happened
History is kept for you
The browser SDK sends only the new question. The server keeps the thread against the visitor id and puts it in front of the model on every turn, so the agent remembers what was said three questions ago — and after a reload — without you passing anything.
TipDart differs here
chat.history, and sends it with each turn. The server uses its own record when it has one, so that copy is not added on top. See Flutter: sending and receiving.What the server believes
| The question | Up to 4,000 characters. A longer one is refused with a 400 before anything runs. |
|---|---|
| The visitor id | 1 to 64 characters of letters, digits and . _ : @ -. The SDK's own (tb- and a UUID) always fits; one you pass yourself must too. |
| A history you send | Only the visitor's own turns are read, and only when the server has no conversation of its own for this visitor. Its stored conversation — which also holds replies from your team — always wins. |
| How fast | 20 messages a minute per visitor and 60 a minute per network address, per agent. Over that is a 429, which the SDK reports as rate_limited. |
AskOptions has one field, signal. Facts the page knows and the agent does not belong in the agent’s instructions or knowledge, not in the turn.