S2 can be used as a backend for stream resumption in AI chatbot applications with the @s2-dev/resumable-stream npm package.

This is inspired by Vercel’s take on stream resumption.

Prerequisites

npm install @s2-dev/resumable-stream

To use this package, you need to create an S2 access token and a basin to store all your streams.

  • Sign up here, generate an access token and set it as S2_ACCESS_TOKEN in your env.

  • Create a new basin from the Basins tab with the Create Stream on Append option enabled, and set it as S2_BASIN in your env.

The incoming stream is batched and the batch size can be changed by setting S2_BATCH_SIZE.

Ingegrating with the Chat SDK

Import the Package

route.ts
// Update imports
import {
  createResumableStreamContext,
  ResumableStreamContext,
} from '@s2-dev/resumable-stream';

Stream Creation

route.ts
export async function POST(request: Request) {
  let requestBody: PostRequestBody;

  try {
    // ... Usage for this method remains the same as in the original template

    const streamContext = getStreamContext();

    if (streamContext) {
      return new Response(
        await streamContext.resumableStream(streamId, stream),
      );
    } else {
      return new Response(stream);
    }
  } catch (error) {
    if (error instanceof ChatSDKError) {
      return error.toResponse();
    }
  }
}

Stream Resumption

route.ts
export async function GET(request: Request) {
  const streamContext = getStreamContext();
  // ...

  const emptyDataStream = createDataStream({
    execute: () => {},
  });

  const stream = await streamContext.resumeStream(recentStreamId);
  // ...
  return new Response(stream, { status: 200 });
}

Complete usage example can be found here.