A reply that appears as it's written, with a stop button.
Thinking…
import { Subscription, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schedule, Schema, Stream as EffectStream } from "effect"import { Stream } from "./ui/uicontext/components/kit/Stream.ts"import { Button } from "./ui/uicontext/components/Button.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const REPLY = `The sign-in link expires after **10 minutes**, but the mail queue has taken up to 14 this week.Raise the lifetime and keep the check that a link is used once:\`\`\`tsexport const LINK_TTL = Duration.minutes(30)export const isValid = (link: Link, now: Date) => !link.used && now.getTime() - link.sentAt.getTime() < Duration.toMillis(LINK_TTL)\`\`\`Then add a test that delivers the email 20 minutes late and still signs in.`// a few characters at a time, as a model's tokens arriveconst CHUNKS = REPLY.match(/[\s\S]{1,6}/gu) ?? []const Message = UiSchema.TaggedEnum({ ReceivedChunk: { index: Schema.Number }, ClickedStop: {}, ClickedRetry: {},})const Status = Schema.Literals(["waiting", "streaming", "succeeded", "stopped"])// Stands in for the model's stream: one chunk every 40ms while the reply runs.const tokens = (run: number) => Subscription.make("reply-tokens", run, () => EffectStream.fromIterable(CHUNKS.map((_, index) => index)).pipe( EffectStream.schedule(Schedule.spaced("40 millis")), EffectStream.map((index) => Message.ReceivedChunk({ index })), ), )const styles = stylex.create({ frame: { display: "grid", gap: space.lg, inlineSize: "min(100%, 36rem)" },})export const StreamingReply = Html.component({ name: "StreamingReply", model: Schema.Struct({ status: Status, received: Schema.Number, run: Schema.Number }), message: Message,}).make({ init: () => ({ status: "waiting" as const, received: 0, run: 1 }), subscriptions: ($) => ($.model.status === "waiting" || $.model.status === "streaming" ? [tokens($.model.run)] : []), update: { ReceivedChunk: ($, { index }) => $.model.status === "stopped" ? $.model : $.evolve({ received: index + 1, status: index + 1 >= CHUNKS.length ? "succeeded" : "streaming" }), ClickedStop: ($) => $.evolve({ status: "stopped" }), ClickedRetry: ($) => $.evolve({ status: "waiting", received: 0, run: (n) => n + 1 }), }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( Stream(h, { key: "stream-demo", status: model.status, text: CHUNKS.slice(0, model.received).join(""), reasoning: { seconds: 4, text: "Compare the link lifetime with how long the queue takes to deliver." }, onStop: Message.ClickedStop(), onRetry: Message.ClickedRetry(), }), model.status === "succeeded" && Button(h, { emphasis: "ghost", size: "sm" }).on("click", Message.ClickedRetry()).children("Play again"), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/streamUsage
import { Stream, type StreamStatus } from "./ui/uicontext/components/kit/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.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { Stream } from "./ui/uicontext/components/kit/Stream.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const grace: Actor = { id: "grace", name: "Grace Hopper", kind: "person" }const styles = stylex.create({ list: { display: "grid", gap: space.xl, inlineSize: "min(100%, 36rem)" }, label: { margin: 0, marginBlockEnd: space.xs, fontSize: font.xs, color: color.faint },})// a labelled case; the caller adds the reply as its next childconst Case = <M>(h: Html.H<M>, label: string) => h.div.children(h.p.attrs(sx(styles.label)).children(label))export const ReplyEndings = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.list)).children( Case(h, "Waiting").children(Stream(h, { key: "end-waiting", status: "waiting", text: "", waitingLabel: "Reading 14 files…" })), Case(h, "Finished").children( Stream(h, { key: "end-done", status: "succeeded", text: "All 212 tests pass. The flaky `checkout.spec.ts` now waits for the payment frame instead of sleeping.", reasoning: { seconds: 38, text: "Run the suite three times and compare which tests fail." }, }), ), Case(h, "Stopped by a person").children( Stream(h, { key: "end-stopped", status: "stopped", stoppedBy: grace, text: "I'll start by renaming every `customerId` column to", }), ), Case(h, "Failed").children( Stream(h, { key: "end-failed", status: "failed", failure: "The model took too long to answer. Nothing was changed.", text: "", }), ), )API reference
CodeBlockView function
Options
| Name | Type | Description |
|---|---|---|
keyrequired | string | — |
coderequired | string | — |
language | string | — |
ReasoningView function
Options
| Name | Type | Description |
|---|---|---|
thinking | boolean | Still thinking: the shimmering label instead of the disclosure |
seconds | number | How long it thought, once done |
text | string | The reasoning itself, shown when the disclosure opens; untrusted model text |
StreamView function
Options
| Name | Type | Description |
|---|---|---|
statusrequired | "waiting" | "streaming" | "succeeded" | "stopped" | "failed" | Waiting for the first words, streaming, or ended: succeeded, stopped or failed |
textrequired | string | Everything received so far |
waitingLabel | string | Shown 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 |
stoppedBy | Actor | For "stopped": who stopped it |
failure | string | For "failed": why, in a sentence |
onStop | Message | Send to stop it; omit when this person can't stop it |
onRetry | Message | Send to try again once it failed or was stopped; omit when it can't be retried |
key | string | Tells this reply's code blocks apart from other replies' on the page: the message id |
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.