Cloudflare
Use @opencode-ai/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 @opencode-ai/sdk@dev
Hold one host for the lifetime of the Durable Object instance instead of creating one for every request.
import { OpenCodeWorkerd } from "@opencode-ai/sdk/workerd"
import myPlugin from "./my-plugin"
export class OpenCodeDO {
private readonly opencode: Promise<OpenCodeWorkerd.Interface>
constructor(state: DurableObjectState) {
this.opencode = state.blockConcurrencyWhile(() =>
OpenCodeWorkerd.create({
storage: state.storage,
config: { default_agent: "build" },
plugins: [myPlugin],
}),
)
}
async fetch() {
const opencode = await this.opencode
return Response.json(await opencode.health.get())
}
}
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.opencode = state.blockConcurrencyWhile(() =>
OpenCodeWorkerd.create({ storage: state.storage }),
)
}
Wrangler selects OpenCode’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.
{
"compatibility_flags": ["nodejs_compat"]
}Customize
Customize your OpenCode instance by registering plugins bundled with your
Worker. Pass plugins to OpenCodeWorkerd.create() to customize agents, models,
tools, and other behavior:
import { Plugin } from "@opencode-ai/plugin"
import { OpenCodeWorkerd } from "@opencode-ai/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 OpenCodeWorkerd.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.