Part 1: A harness in a Durable Object
Build a bot that answers issues on a GitHub repository. When someone opens an issue, OpenCode reads the repository with tools that work in a sandbox, and the bot replies with a comment. When they reply, it continues the same session.
Each issue gets its own Durable Object, which runs OpenCode and its session, and a sandbox for its tools. This part builds that object and prompts it over HTTP. The later parts give it the repository, connect it to GitHub and clean up after it.
The finished code is in examples/cloudflare-opencode-github.
Prerequisites
Section titled “Prerequisites”Finish Cloudflare setup and Boat setup. Part 2 also needs GitHub setup.
Install
Section titled “Install”pnpm add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873bun add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873npm install https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873yarn add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873Declare the model
Section titled “Declare the model”A model is a name. FastLive says which LLM answers under it: Llama 3.3 on Workers AI, through the Worker’s own AI binding.
import * as AI from "@alchemy.run/ai";import * as AICloudflare from "@alchemy.run/ai/Cloudflare";import * as Cloudflare from "@alchemy.run/cloudflare";
export class Fast extends AI.Model<Fast>()("fast") {}
export const FastLive = AICloudflare.Model(Fast, { model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", parameters: { temperature: 0, maxTokens: 1024 },});OpenCode runs in your Durable Object, and this Layer answers its model requests there. OpenCode holds no key for the model.
Say where sandboxes run
Section titled “Say where sandboxes run”OpenCode runs in the Durable Object, which has no files and no shell. The tools that read a repository need both, so each session gets a sandbox for them. An AI.Sandboxes Layer says where. @alchemy.run/ai has one for Boat, which takes the snapshot sandboxes start from:
import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";
export const Sandbox = AIBoat.Sandboxes(Boat.Snapshot("Base", { run: ["git --version"] }));The harness asks the Layer for a sandbox by a key, one for each session. The Layer finds the sandbox labelled with that key, or creates one from Base. Boat stops a sandbox ten minutes after its last use and keeps its disk, and the Layer resumes it when the session comes back.
Or another platform
Section titled “Or another platform”Boat is one of five platforms with a Layer in @alchemy.run/ai. E2B, Daytona, Modal and Archil each swap in the same way: two imports and the image.
import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";import * as AIE2B from "@alchemy.run/ai/E2B";import * as E2B from "@alchemy.run/e2b";
export const Sandbox = AIBoat.Sandboxes(Boat.Snapshot("Base", { run: ["git --version"] }));export const Sandbox = AIE2B.Sandboxes(E2B.Template("Base", { image: "node:22-bookworm" }));The rest of this tutorial uses Boat. See Sandbox platforms for each one, or Build your own.
Yield a harness in a Durable Object
Section titled “Yield a harness in a Durable Object”The harness is yielded in the Durable Object’s outer Effect, and its Layers are provided once, at the end.
import * as AI from "@alchemy.run/ai";import * as Cloudflare from "@alchemy.run/cloudflare";import * as OpenCode from "@alchemy.run/opencode";import * as OpenCodeCloudflare from "@alchemy.run/opencode/Cloudflare";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { Fast, FastLive } from "./models.ts";import { Sandbox } from "./Sandbox.ts";
const HarnessLive = Layer.mergeAll( FastLive, OpenCode.CodingTools, OpenCodeCloudflare.DurableObjectStorage,).pipe(Layer.provideMerge(Sandbox));
export default class Issue extends Cloudflare.DurableObject<Issue>()( "Issue", Effect.gen(function* () { const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });
return Effect.gen(function* () { return {}; }); }).pipe(Effect.provide(HarnessLive)),) {}AI.CodingTools names bash, read, edit, write, grep and glob, and OpenCode.CodingTools provides each as a tool that works in the session’s sandbox, which is why Sandbox is provided to them. DurableObjectStorage runs OpenCode in this object, with its session in the object’s SQLite.
Prompt a session
Section titled “Prompt a session”Add a method that prompts the object’s session and returns the answer’s text:
Effect.gen(function* () { const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });
return Effect.gen(function* () { return {}; const session = harness.session("issue", { tools: [AI.Bash, AI.Read, AI.Grep], instructions: "You answer questions about the files in your working directory.", });
return { ask: Effect.fn(function* (text: string) { const reply = yield* session.prompt(text); return reply.text; }), }; });}).pipe(Effect.provide(HarnessLive)),harness.session("issue", defaults) is this object’s one session. Its prompts get three of the coding tools, the ones that read. The first prompt starts OpenCode in the object. The sandbox opens when a tool first needs it.
Reach it from a Worker
Section titled “Reach it from a Worker”A Worker yields Issue to bind it, and reaches the object for an issue by the issue’s number:
import * as Cloudflare from "@alchemy.run/cloudflare";import * as Effect from "effect/Effect";import { HttpServerRequest } from "effect/http/HttpServerRequest";import * as HttpServerResponse from "effect/http/HttpServerResponse";import Issue from "./Issue.ts";
export default class Bot extends Cloudflare.Worker<Bot>()( "Bot", { main: import.meta.url }, Effect.gen(function* () { const issues = yield* Issue;
return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; const [, area, number] = new URL(request.url, "http://bot").pathname.split("/"); if (area !== "issues" || number === undefined) { return HttpServerResponse.text("not found", { status: 404 }); } const answer = yield* issues.getByName(number).ask(yield* request.text); return HttpServerResponse.text(answer); }).pipe(Effect.orDie), }; }),) {}POST /issues/7 stands in for GitHub until Part 3.
Add the Stack
Section titled “Add the Stack”import * as Boat from "@alchemy.run/boat";import * as Cloudflare from "@alchemy.run/cloudflare";import * as Alchemy from "alchemy";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import Bot from "./src/Bot.ts";
export default Alchemy.Stack( "IssueBot", { providers: Layer.mergeAll(Cloudflare.providers(), Boat.providers()), state: Cloudflare.state(), }, Effect.gen(function* () { const bot = yield* Bot; return { url: bot.url }; }),);Deploy and try it
Section titled “Deploy and try it”pnpm alchemy deploybun alchemy deploynpx alchemy deployyarn alchemy deployThe first deploy builds the snapshot, which takes a couple of minutes. Then ask the session to run a command:
curl -X POST "$URL/issues/7" -d "Run uname -a with the alchemy_bash tool and tell me the kernel version."The first prompt on a new issue waits for its sandbox to start. The command runs in the sandbox, and OpenCode and the model’s Layer run in your Durable Object.
Part 2 puts the repository in the sandbox.