Part 2: A sandbox with the repository
OpenCode’s tools need the repository to answer a question about it. Cloning on every issue would be slow, so the clone happens once, at deploy, into the snapshot every sandbox starts from.
Install
Section titled “Install”pnpm add https://pkg.alchemy.run/@alchemy.run/github/pr:3743:56b4873bun add https://pkg.alchemy.run/@alchemy.run/github/pr:3743:56b4873npm install https://pkg.alchemy.run/@alchemy.run/github/pr:3743:56b4873yarn add https://pkg.alchemy.run/@alchemy.run/github/pr:3743:56b4873Declare the repository
Section titled “Declare the repository”Declare the repository the bot works on. Use your own account as owner:
import * as GitHub from "@alchemy.run/github";
export const owner = "acme";export const repository = "issue-bot-demo";
export const Repo = GitHub.Repository("Repo", { owner, name: repository, visibility: "public", autoInit: true,});The deploy creates the repository with a README. It is public, so a sandbox can clone it without a token.
Clone it into the snapshot
Section titled “Clone it into the snapshot”Replace the Base snapshot with one that clones the repository to ~/repo:
import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";import * as Output from "alchemy/Output";import * as Effect from "effect/Effect";import { Repo } from "./Repo.ts";
export const Sandbox = AIBoat.Sandboxes(Boat.Snapshot("Base", { run: ["git --version"] }));export const Sandbox = AIBoat.Sandboxes( Effect.gen(function* () { const repo = yield* Repo; return yield* Boat.Snapshot("Checkout", { run: [Output.interpolate`git clone ${repo.cloneUrl} ~/repo`], }); }),);The Layer takes the snapshot as a declaration or as an Effect that returns one. This one yields the repository first, so the deploy creates the repository and builds the snapshot after.
Work in the repository
Section titled “Work in the repository”cwd is the directory sessions work in. Point it at the clone:
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools], cwd: "/home/user/repo",});The tools now run in the repository, so relative paths and git work as they would in a checkout on your machine.
Tell the model what it is doing
Section titled “Tell the model what it is doing”const instructions = [ "You answer issues on a GitHub repository, which is your working directory.", "Read the files that matter with your tools before you answer.",].join("\n");
export default class Issue extends Cloudflare.DurableObject<Issue>()(const session = harness.session("issue", { tools: [AI.Bash, AI.Read, AI.Grep], instructions: "You answer questions about the files in your working directory.", instructions,});Add the GitHub provider
Section titled “Add the GitHub provider”The Stack now deploys to GitHub too, and declares the repository:
import * as Boat from "@alchemy.run/boat";import * as Cloudflare from "@alchemy.run/cloudflare";import * as GitHub from "@alchemy.run/github";import * as Alchemy from "alchemy";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import Bot from "./src/Bot.ts";import { Repo } from "./src/Repo.ts";
export default Alchemy.Stack( "IssueBot", { providers: Layer.mergeAll(Cloudflare.providers(), Boat.providers()), providers: Layer.mergeAll(Cloudflare.providers(), Boat.providers(), GitHub.providers()), state: Cloudflare.state(), }, Effect.gen(function* () { const repo = yield* Repo; const bot = yield* Bot; return { url: bot.url }; return { url: bot.url, repository: repo.htmlUrl }; }),);Deploy and try it
Section titled “Deploy and try it”pnpm alchemy deploybun alchemy deploynpx alchemy deployyarn alchemy deployAsk about a file, on a new issue number so the session gets a sandbox from the new snapshot:
curl -X POST "$URL/issues/8" -d "What does README.md say?"OpenCode reads README.md with its own read tool and answers from the file.
Part 3 connects the bot to the repository’s issues.