Skip to main content
S2 integrates with the Anthropic Messages API through the @s2-dev/resumable-stream/anthropic entrypoint. The integration includes two helpers:
  • The createResumableChat server helper stores raw Anthropic stream events in S2 streams and replays them as Anthropic-shaped SSE.
  • The subscribe browser 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
Use 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:
  1. Append the user message to history before starting the model.
  2. Fold Anthropic stream events into a completed assistant message.
  3. Append the assistant message when message_stop arrives.
  4. On page load, render history immediately, then subscribe to replay for any active turn.

Options

createResumableChat accepts: replay accepts: subscribe accepts:

Example

A complete Bun server and browser client is available here: examples/anthropic-resumable-chat.