# Streaming response

An agent's reply as its text arrives: busy while live, with a steady caret at its prose end that leaves in the render that ends it.

Live variants: https://design.arcadia.is/components/streaming-response

```tsx
import { StreamingResponse, ReplyCaret, replyCaret } from "@arcadia/ui";
```

```tsx
import { useState, useEffect } from "react";
import {
  FormattedText, Button, StreamingResponse, ReplyCaret,
} from "@arcadia/ui";

function StreamingResponseLive() {
  const [{ shown, state }, set] = useState<{ shown: number; state: StreamingResponseState }>({ shown: StreamingResponseREPLY.length, state: "done" });
  useEffect(() => {
    if (state !== "live") return;
    const timer = setInterval(() => set(now => {
      if (now.state !== "live") return now;
      const next = Math.min(StreamingResponseREPLY.length, now.shown + CHUNK);
      return { shown: next, state: next === StreamingResponseREPLY.length ? "done" : "live" };
    }), TICK_MS);
    return () => clearInterval(timer);
  }, [state]);
  return (
    <div {...sx(null, StreamingResponseStyles.stack)}>
      <StreamingResponse state={state}>
        <FormattedText source={StreamingResponseREPLY.slice(0, shown)} end={<ReplyCaret />} />
      </StreamingResponse>
      <div {...sx(null, StreamingResponseStyles.actions)}>
        <Button size="sm" variant="secondary" onClick={() => set({ shown: 0, state: "live" })}>Stream again</Button>
        <Button size="sm" disabled={state !== "live"} onClick={() => set(now => ({ ...now, state: "stopped" }))}>Stop</Button>
      </div>
    </div>
  );
}
```

## Accessibility

The reply is `aria-busy` while it streams and ordinary text otherwise; it is never a live region. The caret is hidden from assistive technology, cannot be selected and is left out of a copy. Announce the outcome once in the conversation's own status region.

## API reference

### StreamingResponse

- `state` (`"waiting" | "live" | "done" | "stopped" | "failed"`, required): The stream's state, as the product holds it. Only `live` is busy and draws the caret.
- `rootStyle` (`RootStyle`): Typed StyleX override: where the reply sits.
- `children` (`ReactNode`): The reply as the product's own renderer draws the text received so far, with a `ReplyCaret` at its prose end.

Rest props go to `HTMLAttributes<HTMLDivElement>`.
