Streaming Text

View as Markdown

A reply that appears as it's written, with a stop button.

Thinking…

When to use

A reply that appears as it's written, with a stop button. Use Conversation instead when you show whole messages with their actions.

Usage

import { Stream, type StreamStatus } from "./ui/uicontext/components/Stream.ts"
const Message = UiSchema.TaggedEnum({ ClickedStopReply: {}, ClickedRetryReply: {} })// your app appends each chunk to `text` as it arrives, and sets `status` when the reply endsexport const Reply = (h: Html.H<typeof Message.Type>, reply: { readonly status: StreamStatus; readonly text: string }) =>	Stream(h, {		key: "reply-1",		status: reply.status,		text: reply.text,		onStop: Message.ClickedStopReply(),		onRetry: Message.ClickedRetryReply(),	})

Examples

Waiting and how a reply ends

Before the first word it says it's thinking; at the end it says whether the reply finished, who stopped it, or why it failed.

Waiting

Reading 14 files…

Finished

Thought for 38s

Run the suite three times and compare which tests fail.

All 212 tests pass. The flaky checkout.spec.ts now waits for the payment frame instead of sleeping.

Stopped by a person

I'll start by renaming every customerId column to

Stopped by Grace Hopper

Failed

Failed: The model took too long to answer. Nothing was changed.

API reference

CodeBlockViewComponent

Props

CodeBlockView props
NameTypeDescription
coderequiredstring—
languagestring—
runLabelstringThe run button's label ("Run", "Preview"); no button when left out
streamingbooleanStill being written: no fold, and no run until it's whole

Events

CodeBlockView events
EventPayloadDescription
RequestedRun{ code: string; language: string | null }The person pressed the block's run button (runLabel): run or preview code

CodeBlockView function

Options

CodeBlock options
NameTypeDescription
keyrequiredstringTells several on one page apart
coderequiredstring—
languagestring—
streamingbooleanStill being written
run{ readonly label: string; readonly message: Message }A Run or Preview button for this code: its label, and the message it sends

ReasoningView function

Options

Reasoning options
NameTypeDescription
thinkingbooleanStill thinking: the shimmering label instead of the disclosure
secondsnumberHow long it thought, once done
textstringThe reasoning itself, shown when the disclosure opens; untrusted model text

StreamView function

Options

Stream options
NameTypeDescription
statusrequired"waiting" | "streaming" | "succeeded" | "stopped" | "failed"Waiting for the first words, streaming, or ended: succeeded, stopped or failed
textrequiredstringEverything received so far
waitingLabelstringShown while nothing has arrived yet; "Thinking…" when left out
reasoning{ readonly text?: string | undefined; readonly seconds?: number | undefined }The agent's reasoning: shown as "Thought for Ns" above the reply once text arrives
stoppedByActorFor "stopped": who stopped it
failurestringFor "failed": why, in a sentence
onStopMessageSend to stop it; omit when this person can't stop it
onRetryMessageSend to try again once it failed or was stopped; omit when it can't be retried
keystringTells this reply's code blocks apart from other replies' on the page: the message id

ReplyViewView function

Options

ReplyView options
NameTypeDescription
streamingbooleanThe text is still arriving: code blocks stay unfolded
run((code: string, language: string | undefined) => { readonly label: string; readonly message: Message } | undefined)A Run or Preview button for code the app can run: its label and message, or undefined for none

Keyboard and accessibility

  • Screen readers hear the end state, not every word: the footer is a status that says Writing, then done, stopped (and by whom) or failed (and why).
  • The message is marked busy while it runs, and Stop is a real button.
  • The agent's reasoning folds into a <details> that opens with Enter or Space.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/streaming-text

Source