Sending & receiving

A Dart Stream of the answer as it is written, or the whole thing awaited.

One turn, start to finish

A refusal fails before the first event. After that, every frame is one StreamEvent.

Streaming

This is what the widget does, and it is worth doing: an answer that appears a word at a time reads as thinking, and the same answer after four seconds of a spinner reads as slow.

Minimal

dart
await for (final event in chat.ask('Do you deliver to Leeds?')) {
  if (event.chunk != null) stdout.write(event.chunk);
}

Realistic

In a widget you usually want a subscription rather than await for, so you can cancel it when the screen goes away.

lib/help_screen.dart — the send method
StreamSubscription<StreamEvent>? _turn;
final _reply = StringBuffer();
String? _messageId;

void _send(String question) {
  _turn?.cancel();
  _reply.clear();
  _messageId = null;

  _turn = _chat.ask(question).listen(
    (event) {
      _messageId ??= event.messageId;        // arrives first — keep it
      final chunk = event.chunk;
      if (chunk != null && mounted) setState(() => _reply.write(chunk));
      if (event.handoff != null) _showBanner('Connecting you to the team…');
    },
    onError: (Object error) => _showError(error),
    onDone: () => setState(() => _turn = null),
    cancelOnError: true,
  );
}

Or awaited whole

dart
final answer = await chat.askOnce('What is your returns policy on sofas?');

For a background job, a test, or a screen that shows a spinner and then the reply. Same request, same history — only the shape differs. The Dart version returns the text as it arrived; the browser SDK trims it.

NoteAn empty question sends nothing

ask() trims the question first. If nothing is left, the stream simply ends — no request, no event, no error. Disable your send button on an empty field rather than relying on it.

What an event carries

chunkA piece of the answer. Append it; do not replace.
messageIdThe server's id for this answer, sent before any text. The only id a rating may carry — keep it for when rating arrives in this package.
handoffA Map with id and status. A person was summoned — by the agent itself, or because one already has this conversation, in which case the turn has no text.
donetrue when the turn is over.
errorNever seen by you: an error frame is thrown as a TigerbaseError with code unknown.
rawThe whole decoded frame, for a field this version has no getter for.

NoteOne frame can carry several of these

That is why an event is a set of nullable getters rather than a sealed type. Check the field you care about; ignore the rest.

Warningthinking and suggestions are always empty

StreamEvent has thinking and suggestions getters. The chat endpoint sends neither today, so thinking is always null and suggestions always an empty list. Do not build on them.

The history is kept for you

The server keeps the thread against the visitor id. The package also keeps it in memory, so your widget can render it without a second list: the question is added the moment you ask, and the answer when the stream ends — so history never holds half an answer.

The package sends that copy with each turn, and the server reads it only as a fallback: when it already holds a conversation for this visitor id — which also includes replies from your team during a handover — its own record wins and yours is ignored. Even then only the visitor's own turns are taken from it; an app cannot put words in the agent's mouth by sending a doctored history.

dart
chat.history;   // List<Message>, oldest first, read-only
chat.clear();   // forget the local copy — the server keeps its own
Message.roleuser, model, or agent for a person during a handover.
Message.contentWhat was said.
Message.toJson() / fromJson()The shape to persist it in, and back.

When the answer is not added

The stream ends without doneKept. A connection the server closed mid-answer still said something, and the next turn's history should match what the person can see.
The stream fails with an errorNot kept. The exception leaves ask() before the answer is appended.
You cancel the subscriptionNot kept. Cancelling stops the stream where it is.

TipRender from your own list, not from history, while streaming

Because the answer joins history only at the end, show the in-progress text from your own buffer and switch to history once the turn is done — the complete screen does exactly this.

Resuming a thread

Saving and restoring
// Save, when the screen closes
await prefs.setString(
  'hp.chat',
  jsonEncode(chat.history.map((m) => m.toJson()).toList()),
);

// Restore, next launch
final saved = (jsonDecode(prefs.getString('hp.chat') ?? '[]') as List)
    .map((m) => Message.fromJson(Map<String, dynamic>.from(m as Map)))
    .toList();

final chat = tigerbase.conversation(visitorId: id, history: saved);

Persisting is yours to do — the package keeps the thread in memory and makes no decision about your storage. The visitor id is what ties it to the same conversation on our side, so the agent remembers through the server's record whether or not you restore the local copy; restoring it is for your screen.

Cancelling

dart
final sub = chat.ask(question).listen(onEvent);

@override
void dispose() {
  sub.cancel();
  super.dispose();
}

A screen that is gone should not still be receiving an answer. Cancelling the subscription is the Dart way of saying so, and it is the same call whether the answer had started arriving or not. Nothing is thrown — the enum has an aborted code, but this package never produces it.

WarningA cancelled turn still happened

Once the request was sent, the agent answered and the turn counts against the account’s limits. Cancelling stops your reading, not the answer.