S2 integrates with TanStack AI through theDocumentation Index
Fetch the complete documentation index at: https://s2.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
@s2-dev/resumable-stream package. The integration persists TanStack AI
StreamChunk events to S2 and replays them through a useChat connection
adapter.
Prerequisites
- Sign up here, generate an access token, and set
it as
S2_ACCESS_TOKEN. - Create a basin with Create Stream on Append and Create Stream on Read
enabled, and set it as
S2_BASIN.
Import paths
The integration has separate server and client entrypoints.Setup
Create one chat helper and share it across routes:lib/s2.ts
enableStop is only needed if you expose a stop route.
Use one stable S2 stream name per chat:
lib/stream-name.ts
Server: POST and DELETE routes
makeSessionResponse persists the latest user text event plus chunks from your
TanStack source. The source receives the submitted TanStack UIMessage[].
src/routes/api.chat.ts
waitUntil. Without waitUntil,
makeSessionResponse waits for persistence before returning.
Server: replay
The replay route delivers both initial history and live chunks.src/routes/api.chat.replay.ts
fromSeqNum is omitted, replay compacts completed history into one
in-memory MESSAGES_SNAPSHOT, then tails live records. Snapshots are not
stored in S2. Replay frames include SSE id values; the client uses them as
the next reconnect cursor.
Client
Pass the send and replay endpoints to the TanStack connection. AddstopUrl if
you expose a stop route.
app/page.tsx
live: true because chunks arrive through the replay
subscription, not the POST response.
Flow
- The browser mounts and calls
connection.subscribe(). chat.replayemits an in-memory snapshot and keeps tailing S2.- The browser sends the normal TanStack
messagesarray. makeSessionResponsestores stream events in S2 and starts your source.- Model chunks are appended to S2 and delivered through replay.
- Reconnects continue from the last SSE
id. - If configured,
connection.stop?.()callsstopSession.
Modes
mode controls how generations map to S2 streams.
| mode | use when | refresh behavior |
|---|---|---|
single-use | Each assistant turn has its own stream name. | Replays only the active turn. |
shared | One stream name should hold the active generation only. | Replays the active generation after lease/fence coordination. |
session | You are building a full chat session. | Replay bootstraps a snapshot, then tails later chunks. |
single-use and shared, you can omit subscribeUrl and stream chunks
directly on the POST response. If you pass subscribeUrl, the client can
recover an active generation on mount.
Configuration
Relevant options oncreateResumableChat:
| option | default | what it controls |
|---|---|---|
mode | "single-use" | "single-use" uses one stream per generation. "shared" reuses one active-generation stream. "session" stores a durable chat session log. |
enableStop | false | Enables process-local stopSession support for a stop route. No active-generation map is created unless this is true. |
endpoints | S2 defaults | Optional endpoint overrides, for example when using S2 Lite. |
leaseDurationMs | 5000 | Only for shared mode. Max pause within an active generation before a new claim can take it over. |
onError | generic message | Maps upstream errors to the RUN_ERROR chunk shown to the client. |
batchSize / lingerDuration | 10 / 50ms | S2 append batching knobs. |
createS2Connection:
| option | what it controls |
|---|---|
mode | Must match the server mode. Use "session" for chat sessions. |
sendUrl | POST endpoint that starts generation. |
stopUrl | DELETE endpoint that stops process-local generation. Optional. |
subscribeUrl | GET endpoint that streams chunks. Required for session; optional for single-use and shared. |
headers / credentials | Auth and fetch options for send, stop, and subscribe requests. |
body | Extra fields merged into the POST request body, and DELETE when stopUrl is used. |
fetch | Custom fetch implementation for tests or framework integrations. |
Example
A runnable TanStack Start chat app with session replay and stop support is available in the SDK repo:examples/tanstack-ai-chat.
