S2 integrates with the Anthropic Messages API 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/anthropic entrypoint.
The integration includes two helpers:
- The
createResumableChatserver helper stores raw Anthropic stream events in S2 streams and replays them as Anthropic-shaped SSE. - The
subscribebrowser helper is a small async generator that reads the replay endpoint.
Server Setup
lib/s2.ts
mode: 'session' can be useful when you want your chat app to have one S2
stream to hold a long-lived log for a chat. Use single-use if each response
needs its own stream.
Start A Turn
app/api/chat/route.ts
delivery: 'replay' makes the POST return 202. The client reads the actual
events from the replay route.
Replay Route
app/api/chat/stream/route.ts
live: true in session mode when the browser should stay connected at the
tail and receive future turns.
Browser Subscription
subscribe yields Anthropic RawMessageStreamEvent values, tracks the replay
cursor from SSE id: values, and reconnects from that cursor if the response
drops. It can also yield the adapter’s error envelope:
Completed History
The Anthropic helper does not infer user messages and does not build a transcript for you. You can pair the replay stream with a separate history store:- Append the user message to history before starting the model.
- Fold Anthropic stream events into a completed assistant message.
- Append the assistant message when
message_stoparrives. - On page load, render history immediately, then subscribe to replay for any active turn.
Options
createResumableChat accepts:
| option | default | description |
|---|---|---|
mode | "single-use" | "single-use" uses one stream per generation. "shared" reuses one active-generation stream. "session" appends generations to one durable stream. |
endpoints | S2 defaults | Optional endpoint overrides, commonly used with S2 Lite. |
batchSize | 10 | Maximum number of events per append batch. |
lingerDuration | 50 | Maximum batching delay in milliseconds. |
leaseDurationMs | 5000 | shared mode takeover window for stale active generations. |
onError | generic message | Maps upstream errors to an Anthropic-shaped error event. |
replay accepts:
| option | description |
|---|---|
fromSeqNum | S2 cursor to resume from. The client tracks this from SSE id: values. |
live | Session mode only. Keeps the SSE open at the tail for future records. |
subscribe accepts:
| option | description |
|---|---|
url | Replay URL. String URLs get ?from=<cursor> appended on reconnect; function URLs receive the cursor. |
signal | Optional abort signal. |
fetch | Optional fetch implementation override. |
headers | Static or lazy headers sent on every request. |
credentials | Fetch credentials mode. Defaults to same-origin. |
reconnectBackoffMs | Millisecond backoff schedule. Pass [] to disable reconnect. |
Example
A complete Bun server and browser client is available here:examples/anthropic-resumable-chat.
