Skip to content

Setup

You need:

OpenCode needs no account. It is a dependency of the harness and is bundled into your Worker.

Install the harness and the shared AI package, and your sandbox platform’s provider if you use the coding tools:

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/boat/pr:3743:56b4873

The coding tools work in a sandbox, so the Stack needs the platform’s provider next to Cloudflare’s:

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 Api from "./src/Api.ts";
export default Alchemy.Stack(
"MyApp",
{
providers: Layer.mergeAll(Cloudflare.providers(), Boat.providers()),
state: Cloudflare.state(),
},
Effect.gen(function* () {
const api = yield* Api;
return { url: api.url };
}),
);

Sign in to the platform with your profile:

Terminal window
pnpm alchemy profile edit --add Boat

A harness needs a model. Tools that use files or a shell also need an AI.Sandboxes Layer. See Models and Sandboxes.

Yield the harness in a Durable Object’s outer Effect and provide its Layers once:

src/Coder.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 { FastLive, Fast } from "./models.ts";
import { Sandbox } from "./Sandbox.ts";
const HarnessLive = Layer.mergeAll(
FastLive,
OpenCode.CodingTools,
OpenCodeCloudflare.DurableObjectStorage,
).pipe(Layer.provideMerge(Sandbox));
export default class Coder extends Cloudflare.DurableObject<Coder>()(
"Coder",
Effect.gen(function* () {
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });
return Effect.gen(function* () {
return {
prompt: Effect.fn(function* (name: string, text: string) {
const reply = yield* harness.session(name).prompt(text);
return reply.text;
}),
};
});
}).pipe(Effect.provide(HarnessLive)),
) {}

OpenCodeCloudflare.DurableObjectStorage runs OpenCode in the Durable Object, with its sessions in the object’s SQLite. Layer.provideMerge(Sandbox) gives the sandboxes to the coding tools and to the harness.

Nothing of OpenCode is installed in the sandbox. The coding tools run bash, grep and base64 there, which Boat’s own image and node:22-bookworm have.

Next: the tutorial.