Tools
Every tool of an OpenCode session runs in the Durable Object, where OpenCode is. Your tools are AI.Tool declarations, with the object’s bindings. The coding tools are tools too, and reach files and a shell in the session’s sandbox.
The coding tools
Section titled “The coding tools”A Durable Object has no files and no shell, so OpenCode.CodingTools provides each of the six coding tool names as a tool that works in the session’s sandbox:
const HarnessLive = Layer.mergeAll( FastLive, OpenCode.CodingTools, OpenCodeCloudflare.DurableObjectStorage,).pipe(Layer.provideMerge(Sandbox));
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });The Layers require AI.Sandboxes, which is why the sandbox is provided to them. Each tool has its own Layer (OpenCode.Bash, OpenCode.Read, OpenCode.Edit, OpenCode.Write, OpenCode.Grep, OpenCode.Glob), and the model sees each under the harness’s prefix, such as alchemy_bash.
Tools only OpenCode has
Section titled “Tools only OpenCode has”OpenCode.WebFetch, OpenCode.WebSearch and OpenCode.Subagent are OpenCode’s own. They are listed as they are and need no Layer:
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools, OpenCode.WebFetch],});Code mode
Section titled “Code mode”A tool listed inside AI.CodeMode(...) is reached from a program the model writes, not one call at a time:
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools, AI.CodeMode(Add, Withdraw)],});See Code mode.
Your tools
Section titled “Your tools”List an AI.Tool and provide its Layer. The handler runs in the Durable Object:
import * as AI from "@alchemy.run/ai";import * as GitHub from "@alchemy.run/github";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import * as Schema from "effect/Schema";import { Repo } from "./Repo.ts";
export class SearchIssues extends AI.Tool<SearchIssues>()("search_issues", { description: "Search this repository's issues and pull requests.", parameters: Schema.Struct({ query: Schema.String }), success: Schema.Struct({ issues: Schema.Array(Schema.Struct({ number: Schema.Number, title: Schema.String })), }),}) {}
export const SearchIssuesLive = SearchIssues.make( Effect.gen(function* () { const search = yield* GitHub.SearchIssues(yield* Repo);
return Effect.fn(function* ({ query }) { const found = yield* search({ q: query, per_page: 10 }).pipe(Effect.orDie); return { issues: found.items.map((issue) => ({ number: issue.number, title: issue.title })), }; }); }),).pipe(Layer.provide(GitHub.SearchIssuesHttp));const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools, SearchIssues] });The GitHub token stays in the Durable Object. Nothing of this tool touches a sandbox.
How your tools are named
Section titled “How your tools are named”OpenCode names a tool it did not ship after where it came from. Yours are alchemy_<name>, so the model sees alchemy_search_issues. The harness’s name option changes the prefix:
const harness = yield* OpenCode.Harness({ name: "acme", models: [Fast], tools: [SearchIssues] });Tools in the session’s sandbox
Section titled “Tools in the session’s sandbox”A tool of yours reaches the session’s sandbox through AI.Sandbox. It is the same sandbox the coding tools work in:
export const CountBytesLive = CountBytes.make( Effect.fn(function* ({ path }) { const sandbox = yield* AI.Sandbox; const text = yield* sandbox.readTextFile(path).pipe(Effect.orDie); return { bytes: new TextEncoder().encode(text).length }; }),);Choose tools for a prompt
Section titled “Choose tools for a prompt”A prompt’s tools are exactly what the model may use:
const reply = yield* session.prompt("Which issues mention the login test?", { tools: [AI.Read, AI.Grep, SearchIssues],});Everything else is switched off for that prompt, including OpenCode’s own tools. A tool that is not on the harness fails with NotOnHarness.
Tools that fail
Section titled “Tools that fail”A tool’s declared failure reaches OpenCode as a failed tool call, with the error’s tag and fields as its text. The model reads it and can try something else.
Replace a coding tool
Section titled “Replace a coding tool”A name can be your own tool. Provide AI.Bash.make(...) in place of OpenCode.Bash: the model sees yours as alchemy_bash.