Skip to main content
S2 integrates with the AI SDK through the @s2-dev/resumable-stream/aisdk entrypoint. The package stores AI SDK UIMessageChunk events in S2 and replays them with the same SSE format that the AI SDK transport expects.

Install

This integration requires ai >= 5.0; older AI SDK releases do not export UIMessageChunk. Create a basin with Create Stream on Append enabled. If your app reads history streams before writing to them, also enable Create Stream on Read.

Server Setup

Create the S2 chat helper once and reuse it in your routes.
lib/s2.ts

Start A Turn

The POST route starts the model call, writes chunks to S2, and streams the same chunks back to the AI SDK client.
app/api/chat/route.ts

Reconnect Route

resume: true in useChat calls the reconnect route on mount. With the default AI SDK transport, that route is ${api}/${chatId}/stream.
app/api/chat/[id]/stream/route.ts
If the turn is still active, S2 replays the remaining chunks. If there is no active turn, the route returns 204.

Client

Use the AI SDK useChat React hook to create a resumable chat.
app/page.tsx

Completed History

@s2-dev/resumable-stream/aisdk stores the streaming chunks for resumability. It does not decide where your completed transcript lives. For a chat app, keep a transcript stream or database table in app code:
  1. Load completed messages before rendering the page.
  2. Pass them to useChat as initial state if your UI needs refresh-after-done history.
  3. Use chat.makeResumable(...) for the active assistant turn.
The runnable example stores completed messages in a separate S2 history stream and uses a live stream for the active generation.

Options

createResumableChat accepts the shared resumable-stream options: makeResumable accepts:

Example

A complete Bun server and browser client is available here: examples/ai-sdk-resumable-chat. The TypeScript SDK repo also includes direct S2 examples for: