chat-adapter-state-durable-object
v0.0.1
Published
Cloudflare Durable Object state adapter for the chat-sdk
Downloads
100
Maintainers
Readme
Chat SDK Durable Object State Adapater
Cloudflare Durable Object state adapter for chat. Provides persistent subscriptions, distributed locking, and key-value cache via a Durable Object (RPC only, no fetch handler).
Installation
pnpm add chat-adapter-state-durable-object chatSetup
- Export the Durable Object from your Worker and bind it in
wrangler.toml. The DO uses SQLite storage (one row per subscription, lock, and cache entry), so you must use a SQLite-backed Durable Object:
// src/index.ts
export { ChatStateDurableObject } from "chat-adapter-state-durable-object";# wrangler.toml
[[durable_objects.bindings]]
name = "EXPORTED_DO"
class_name = "ChatStateDurableObject"
# Required: enable SQLite for this DO class (run once, then optional to remove)
[[migrations]]
tag = "v1"
new_sqlite_classes = ["ChatStateDurableObject"]- Create the state adapter with a stub and pass it to Chat:
import { Chat } from "chat";
import { createDurableObjectState } from "chat-adapter-state-durable-object";
export default {
async fetch(req: Request, env: Env, ctx: ExecutionContext) {
// Get a stub - ID could be per tenant, or just a single DO.
const stub = env.EXPORTED_DO.getByName("chat-state");
const chat = new Chat({
state: createDurableObjectState(stub),
// ...adapters, handlers
});
// ...
},
};Optional logger for errors:
const state = createDurableObjectState(stub, { logger: myLogger });API
createDurableObjectState(stub, options?)– Returns aStateAdapterthat forwards all operations to the Durable Object via RPC.ChatStateDurableObject– Durable Object class to bind.
Development
Tests use Cloudflare’s Vitest Workers integration and run against a real Durable Object (no mocks). Ensure wrangler.toml and src/worker.ts are present, then:
pnpm testLicense
MIT
