Skip to content

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.

Finish Cloudflare setup and Boat setup. Part 2 also needs GitHub setup.

Terminal window
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:56b4873

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.

src/models.ts
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.

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:

src/Sandbox.ts
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.

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.

The harness is yielded in the Durable Object’s outer Effect, and its Layers are provided once, at the end.

src/Issue.ts
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.

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.

A Worker yields Issue to bind it, and reaches the object for an issue by the issue’s number:

src/Bot.ts
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.

alchemy.run.ts
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 };
}),
);
Terminal window
pnpm alchemy deploy

The first deploy builds the snapshot, which takes a couple of minutes. Then ask the session to run a command:

Terminal window
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.