Start typing to search the documentation.

Build navigation

Cloudflare

Use @rewyyn/sdk/workerd inside a Cloudflare Durable Object. This profile uses the object’s SQLite storage, persists durable events for eviction recovery, and replaces unavailable local filesystem and process services.

bun add @rewyyn/sdk

Hold one host for the lifetime of the Durable Object instance instead of creating one for every request.

import { RewyynWorkerd } from "@rewyyn/sdk/workerd"
import myPlugin from "./my-plugin"

export class RewyynDO {
  private readonly rewyyn: Promise<RewyynWorkerd.Interface>

  constructor(state: DurableObjectState) {
    this.rewyyn = state.blockConcurrencyWhile(() =>
      RewyynWorkerd.create({
        storage: state.storage,
        config: { default_agent: "build" },
        plugins: [myPlugin],
      }),
    )
  }

  async fetch() {
    const rewyyn = await this.rewyyn
    return Response.json(await rewyyn.server.info())
  }
}

blockConcurrencyWhile keeps every Durable Object event out until the host is ready and resets the object if initialization fails. The retained Promise gives request handlers direct access to the same host after startup.

constructor(state: DurableObjectState) {
  this.rewyyn = state.blockConcurrencyWhile(() =>
    RewyynWorkerd.create({ storage: state.storage }),
  )
}

Wrangler selects Rewyyn’s Workerd-safe implementations through the workerd package condition. Cloudflare may evict a Durable Object without running cleanup, so correctness does not depend on close() being called.

wrangler.jsonc
{
  "compatibility_flags": ["nodejs_compat"]
}

Customize

Customize your Rewyyn instance by registering plugins bundled with your Worker. Pass plugins to RewyynWorkerd.create() to customize agents, models, tools, and other behavior:

import { Plugin } from "@rewyyn/plugin"
import { RewyynWorkerd } from "@rewyyn/sdk/workerd"

const plugin = Plugin.define({
  id: "customize-agent",
  async setup(ctx) {
    await ctx.agent.transform((agents) => {
      agents.update("build", (agent) => {
        agent.description = "Builds features and fixes bugs for our team"
      })
    })
  },
})

await RewyynWorkerd.create({
  storage: state.storage,
  config: { default_agent: "build" },
  plugins: [plugin],
})

See the full plugins documentation for plugin hooks, transforms, tools, and the complete plugin context.