Cloudflare
Cloudflare Durable Object 内では @opencode/sdk/workerd を使用します。このプロファイルは、インスタンスがメモリから解放された後も復旧できるよう、オブジェクトの SQLite ストレージに永続イベントを保存します。また、この環境で利用できないローカルファイルシステムやプロセスのサービスを置き換えます。
bun add @opencode/sdkリクエストごとに作成するのではなく、Durable Object インスタンスの存続期間中 1 つのホストを保持します。
import { OpenCodeWorkerd } from "@opencode/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.server.info())
}
}blockConcurrencyWhile はホストの準備ができるまで Durable Object のすべてのイベントを待機させ、初期化に失敗した場合はオブジェクトをリセットします。保持された Promise により、起動後はリクエストハンドラーが同じホストに直接アクセスできます。
constructor(state: DurableObjectState) {
this.opencode = state.blockConcurrencyWhile(() =>
OpenCodeWorkerd.create({ storage: state.storage }),
)
}Wrangler は workerd のパッケージ条件に基づき、Workerd で動作する OpenCode の実装を選びます。Cloudflare はクリーンアップを実行せずに Durable Object をメモリから解放することがあるため、close() が呼ばれなくても正しく動作する設計になっています。
{
"compatibility_flags": ["nodejs_compat"]
}fetch を渡すと、OpenCode の外向きの HTTP リクエスト(OpenCode Console の設定、モデルリクエスト、Web フェッチと検索、well-known 設定、リモートの MCP サーバー)を、Service Binding などのカスタムハンドラー経由でルーティングできます。Code Mode のエージェントコードは引き続き globalThis.fetch を使用します。
OpenCodeWorkerd.create({
storage: state.storage,
fetch: (input, init) => {
const request = new Request(input, init)
return new URL(request.url).hostname === "console.example.com" ? env.CONSOLE.fetch(request) : fetch(request)
},
})Worker にバンドルしたプラグインを登録して OpenCode のインスタンスをカスタマイズします。OpenCodeWorkerd.create() にプラグインを渡すと、エージェント、モデル、ツール、その他の動作をカスタマイズできます。
import { Plugin } from "@opencode/plugin"
import { OpenCodeWorkerd } from "@opencode/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],
})プラグインのフック、transform、ツール、完全なプラグインコンテキストについてはプラグインの完全なドキュメントを参照してください。