Klein Kit

Message queueing

Send-while-streaming without ever throwing

Prompting a busy session never throws — the turn is queued and starts when its predecessors settle:

const first = session.prompt("run the tests");
const followUp = session.prompt("now fix whatever failed"); // queued, runs next

Everything a send-while-streaming input box needs:

  • turn.queued events — emitted with a 1-based position when a prompt lands in the queue.
  • session.queuedTurns — the current queue as { turnId, input }[].
  • session.cancelQueued(turnId) — remove a queued turn; returns false if it already started.
  • turn.interrupt() on a queued turn — cancels just that turn (its promise rejects with code QUEUE_CANCELED).
  • session.interrupt() — stops the running turn; the queue then advances.
session.on("turn.queued", ({ turnId, position }) => {
  ui.showQueuedChip(turnId, position);
});

// user hits ✕ on a queued message:
session.cancelQueued(turnId);

Closing the session settles everything: the active turn resolves as interrupted and queued turns reject with "session closed before turn started".