AI Stream

Streaming answers: words that sharpen in behind a caret, a skeleton that fills in line by line, and a version pager for regenerated replies.

Installation

Installation

One command adds ai-stream and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-stream.json
ai-stream-demo
components/ui/ai-stream-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { paceToken, useAiLoop } from "@/lib/ai-kit"
import {
  AiSkeletonStream,
  AiStream,
  AiVersions,
  type AiSkeletonRow,
} from "@/registry/tween/ai-stream"

const ANSWER =
  "The headline now reveals word by word. I used `text-skew-reveal` with its default 0.65s and a 0.05s stagger, so the line lands in under a second. Reduced motion shows the finished headline straight away."

function WordFade() {
  const root = React.useRef<HTMLDivElement>(null)
  const [text, setText] = React.useState(ANSWER)
  const [status, setStatus] = React.useState<"waiting" | "streaming" | "done">(
    "done"
  )
  const [rating, setRating] = React.useState<"up" | "down" | null>(null)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setRating(null)
      setText("")
      setStatus("waiting")
      await sleep(900)
      setStatus("streaming")
      let so = ""
      for (const word of ANSWER.split(" ")) {
        so = so ? `${so} ${word}` : word
        setText(so)
        await sleep(paceToken(word))
      }
      setStatus("done")
      await sleep(2600)
    },
    { gap: 600, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 380px)" }}
      onPointerDown={() => {
        setHands(true)
        setText(ANSWER)
        setStatus("done")
      }}
    >
      <AiStream
        text={text}
        status={status}
        agent={{ name: "Mira" }}
        meta="just now"
        stats="35 words · 2.6s"
        rating={rating}
        onRate={(next) => setRating((now) => (now === next ? null : next))}
        onRetry={() => setHands(false)}
      />
    </div>
  )
}

const ROWS: AiSkeletonRow[] = [
  { kind: "heading", text: "Three changes to the hero", width: 58 },
  {
    kind: "bullet",
    text: "Headline reveals with `text-skew-reveal`",
    width: 86,
  },
  { kind: "bullet", text: "Button moved above the fold", width: 70 },
  { kind: "bullet", text: "Image loads lazily below 768px", width: 78 },
  { kind: "text", text: "All three pass on phone and desktop.", width: 64 },
]

function SkeletonFirst() {
  const root = React.useRef<HTMLDivElement>(null)
  const [revealed, setRevealed] = React.useState(ROWS.length)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setRevealed(0)
      await sleep(1300)
      for (let at = 1; at <= ROWS.length; at++) {
        setRevealed(at)
        await sleep(620)
      }
      await sleep(2200)
    },
    { gap: 500 }
  )

  return (
    <div ref={root} style={{ display: "grid", width: "min(100%, 380px)" }}>
      <AiSkeletonStream
        rows={ROWS}
        revealed={revealed}
        agent={{ name: "Odo", tone: "violet", shape: "blob" }}
        meta="just now"
      />
    </div>
  )
}

const DRAFTS = [
  "Tween is a registry of animated React components built with GSAP and Three.js. You install each one with the shadcn CLI.",
  "Tween gives you animated React components as source code. One command adds a component to your project, ready to restyle.",
  "Animated React components, delivered as code you own. Add one with a single command and change anything you like.",
  "Motion for React, as files in your repo. Pick a component, run one command, and edit it like the rest of your code.",
]

function Versions() {
  const root = React.useRef<HTMLDivElement>(null)
  const [count, setCount] = React.useState(3)
  const [index, setIndex] = React.useState(1)
  const [busy, setBusy] = React.useState(false)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setCount(3)
      setIndex(1)
      await sleep(1700)
      setIndex(2)
      await sleep(1900)
      setIndex(0)
      await sleep(1900)
      setBusy(true)
      await sleep(1100)
      setBusy(false)
      setCount(4)
      setIndex(3)
      await sleep(2400)
    },
    { gap: 400, enabled: !hands }
  )

  const regenerate = () => {
    if (count >= DRAFTS.length) return setIndex(0)
    setBusy(true)
    window.setTimeout(() => {
      setBusy(false)
      setCount(count + 1)
      setIndex(count)
    }, 900)
  }

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 380px)" }}
      onPointerDown={() => setHands(true)}
    >
      <AiVersions
        versions={DRAFTS.slice(0, count)}
        index={index}
        onIndexChange={setIndex}
        regenerating={busy}
        onRegenerate={regenerate}
        agent={{ name: "Pim", tone: "green", shape: "arch" }}
      />
    </div>
  )
}

export function AiStreamDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Word fade", node: <WordFade /> },
        { name: "Skeleton first", node: <SkeletonFirst /> },
        { name: "Versions", node: <Versions /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-stream.json

Usage

import {
  AiSkeletonStream,
  AiStream,
  AiVersions,
} from "@/components/ui/ai-stream"
<AiStream
  text={text}
  status={status === "streaming" ? "streaming" : text ? "done" : "waiting"}
  agent={{ name: "Mira" }}
  meta="just now"
  stats="35 words · 2.6s"
  rating={rating}
  onRate={setRating}
  onRetry={regenerate}
/>

Grow text as tokens arrive. Each new word fades in on its own, and words wrapped in backticks render as inline code. With the AI SDK, pass the text of the message's text part and map its state to status.

Versions

  • AiStream fades each word in behind a caret. The face talks while text arrives, and the actions row (copy, retry, thumbs, stats) appears only once status is done. Copy writes the text to the clipboard and confirms with a check.
  • AiSkeletonStream takes the shape of the answer first. Each rows entry shows as a pulsing bar until revealed passes it, then the line wipes in over the bar, so nothing jumps.
  • AiVersions shows one of several finished answers with a pager and a regenerate button. Changing index fades the next version into the same space.

Props

AiStream

PropTypeDefaultDescription
textstringThe answer so far. Backticks mark inline code.
status"waiting" | "streaming" | "done"Waiting shimmers, streaming shows the caret, done shows the actions.
agent{ name, tone?, shape? }The face and name above the message.
metastringA short line beside the name.
statsstringShown at the end of the actions row.
rating"up" | "down" | nullWhich thumb is pressed.
onCopy() => voidCalled after the text is copied.
onRetry() => voidCalled from the retry button.
onRate(rating: "up" | "down") => voidCalled from the thumbs.

AiSkeletonStream

PropTypeDefaultDescription
rows{ kind: "heading" | "bullet" | "text", text, width? }[]The shape of the answer and its lines.
revealednumberHow many rows, from the top, have arrived.
agent{ name, tone?, shape? }The face and name above the message.

AiVersions

PropTypeDefaultDescription
versionsstring[]Every answer so far, oldest first.
indexnumberThe version on show.
onIndexChange(index: number) => voidCalled from the pager.
onRegenerate() => voidCalled from the regenerate button.
regeneratingbooleanfalseSpins the icon and blocks a second press.