@s2-dev/resumable-stream/tanstack-ai entrypoint.
The server helper stores TanStack StreamChunk events in S2. The client helper
builds a TanStack SubscribeConnectionAdapter so useChat can send messages
normally while reading assistant chunks from an S2 replay stream.
Install
Server Setup
lib/s2.ts
Start A Turn
app/api/chat/route.ts
Replay Route
app/api/chat/replay/route.ts
live: true for session-mode chat UIs so the replay connection stays open
at the tail and receives future turns.
Client
app/page.tsx
id: cursor. createConnection remembers
the most recent cursor inside that connection instance and sends it back as
from on the next subscribe request, so a dropped subscription can continue
after the chunks already delivered instead of rendering them twice. For a full
page refresh, pass the saved history cursor as shown below.
Completed History
TanStackuseChat expects completed messages as app state, so keep transcript
loading in your app:
- Fetch history before mounting
useChat. - Pass it as
initialMessages. - Start replay from the history cursor so old chunks are not rendered again.
/history route. That history behavior is intentionally
example code, not package API.
Options
createResumableChat accepts:
createConnection accepts:
Example
A complete TanStack Start chat app is available here:examples/tanstack-ai-chat.
