Feedback and typing

Two small things that are easy to get wrong in ways nobody notices.

rate()

A thumb on an answer. It needs the messageId that arrived before that answer’s first word — so keep it on the message in your state the moment it arrives.

messageIdThe id from that answer's messageId event. Required.
verdict"up" or "down".
options.commentOptional, up to 2,000 characters. Ask for it after a thumbs-down, not before.
ReturnsPromise<void>. Throws a TigerbaseError like every other call.

Minimal

ts
await chat.rate(messageId, "up");

Realistic

components/Message.tsx — Harbor & Pine
function Thumbs({ message }: { message: { id?: string } }) {
  const [sent, setSent] = useState<"up" | "down" | null>(null);

  // No id, no rating: an answer the server never stored cannot be rated.
  if (!message.id) return null;

  async function vote(verdict: "up" | "down") {
    setSent(verdict);
    if (verdict === "down") {
      const comment = window.prompt("What was wrong with this answer?") ?? undefined;
      await chat.rate(message.id!, "down", { comment });
    } else {
      await chat.rate(message.id!, "up");
    }
  }

  return sent ? <span>Thanks for the feedback</span> : (
    <span>
      <button onClick={() => vote("up")} aria-label="Helpful">👍</button>
      <button onClick={() => vote("down")} aria-label="Not helpful">👎</button>
    </span>
  );
}

NoteWhy the id matters more than it looks

The widget used to invent its own id in the browser. Ratings were counted and could not be attached to any answer the server had stored, so the dashboard showed totals that pointed at nothing and nobody could ask which answers people disliked — which is the only useful question.

The server now mints the id and sends it ahead of the first chunk. Keep it on the message in your state and this is correct for free.

The typing indicator

So a person who has taken over the conversation can see the visitor is writing. Call it on every keystroke; the SDK handles the rest.

typing.keystroke()Call on every keystroke. Sends "typing" on the first one, then a heartbeat every 2 seconds while they keep going.
typing.stop()Call on send, when the box is cleared, and on unmount. It also stops by itself after 3 seconds without a keystroke.
components/Composer.tsx
<textarea
  value={input}
  onChange={(e) => {
    setInput(e.target.value);
    e.target.value ? chat.typing.keystroke() : chat.typing.stop();
  }}
  onKeyDown={(e) => {
    if (e.key === "Enter" && !e.shiftKey) {
      e.preventDefault();
      chat.typing.stop();      // a sent message is a finished one
      send(input);
    }
  }}
/>

NoteIt does nothing when nobody is watching

The signal travels over the handoff socket, and that socket is only opened by on() or requestHuman(). Before that there is nothing to send on, so calling it on every keystroke costs nothing. Once it is open, the SDK sends a heartbeat at a fixed interval rather than one message per character.

The agent is never told. A model has no use for knowing somebody is mid-sentence.

Three ways to be finished

A person who stops typing, a person who presses enter and a person who closes the tab are three different exits, and each needs the indicator to go away. The pause is handled for you; stop() on send and end() on unmount cover the other two.

Watching the other direction

Show that the person is typing back.

Their typing
chat.on("typing", ({ who, active }) => {
  if (who === "human") setTheyAreTyping(active);
});

WarningIt rides the handoff socket

The server sends the person’s typing as agent_typing on the same socket as the rest of handoff, and the package turns it into this event. That socket needs socket.io-client installed; without it you get an error event instead. See Handing to a person.

TipExpire what you receive

A person whose laptop closes mid-word never sends “stopped”. If you drive your own transport, forget a received “typing” after TYPING_EXPIRY_MS (5 seconds, exported) unless it is renewed — comfortably longer than the 2-second heartbeat, so an ordinary delay does not make it flicker. createTypingSignal(send) gives you the sending half with the same timings.