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.

One turn. A refusal is decided before the stream opens; everything after is the answer, a piece at a time.

ask() — the answer as it is written

An async generator. Loop over it with for await and append each piece as it comes.

Minimal

ts
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.

Streaming into a bubble
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.

ts
const answer = await chat.askOnce("What is your returns policy on sofas?");
Realistic — an answer under a product page
// "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

Not a second route and not a second implementation. Two implementations drift: one learns about handoff and the other does not, and you find out from a customer.

The events

Each event is an object with whichever of these fields that frame carried. Check the one you care about; ignore the rest.

messageIdThe server’s id for this answer, sent before the first word. Keep it — it is what rate() needs.
textA 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.
agentSwitchA 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.
doneThe turn is over.

NoteWhy messageId comes first

It is minted before any text is generated, deliberately. The widget used to invent its own id in the browser, so a thumbs-up pointed at nothing the server could match — ratings counted and could not be attached to an answer. Fixed by sending the real id ahead of the first chunk.

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.

ts
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.

A yes or no before the order is cancelled
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;
      }
    };
  }
}
descriptionThe sentence to show. toolName is the model's internal name for the action; keep it for logs.
idA 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 tokenAnswering 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.

ts
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

The question reached the agent and the server keeps writing the answer into the conversation’s history. Aborting stops your reading of the stream, not the turn — and it counts against the account’s message limits either way.

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

The Dart package also keeps the thread in memory, as 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 questionUp to 4,000 characters. A longer one is refused with a 400 before anything runs.
The visitor id1 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 sendOnly 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 fast20 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.