Compare commits

..

8 Commits

Author SHA1 Message Date
Kit Langton
a6d6ebc0f8 Merge branch 'dev' into kit/effectify-tool-registry 2026-03-20 16:00:16 -04:00
Dax Raad
5c6ec1caac fix question cross out 2026-03-20 15:50:04 -04:00
Kit Langton
77824dc03e Merge branch 'dev' into kit/effectify-tool-registry 2026-03-19 21:16:35 -04:00
Kit Langton
bf9053c59e Merge branch 'dev' into kit/effectify-tool-registry 2026-03-19 19:27:41 -04:00
Kit Langton
ce3f5d04d9 log errors in catchCause instead of silently swallowing 2026-03-19 16:23:09 -04:00
Kit Langton
1af7d26a86 use forkScoped + Fiber.join for lazy init (match old Instance.state behavior) 2026-03-19 16:03:47 -04:00
Kit Langton
d08c119743 simplify ToolRegistry Interface tools return type 2026-03-19 15:41:30 -04:00
Kit Langton
9ac18e2042 effectify ToolRegistry: migrate from Instance.state to Effect service pattern
Replace the legacy Instance.state() lazy init with an Effect Layer that
yields InstanceContext, defines Interface/Service/layer, and exposes
promise facades for backward compat. Register ToolRegistry.Service in
the Instances layer map.
2026-03-19 15:15:34 -04:00
6 changed files with 341 additions and 337 deletions

View File

@@ -126,8 +126,8 @@ Done now:
Still open and likely worth migrating:
- [x] `Plugin`
- [ ] `ToolRegistry`
- [ ] `Plugin`
- [x] `ToolRegistry`
- [ ] `Pty`
- [ ] `Worktree`
- [ ] `Installation`

View File

@@ -1667,6 +1667,7 @@ function InlineTool(props: {
const denied = createMemo(
() =>
error()?.includes("QuestionRejectedError") ||
error()?.includes("rejected permission") ||
error()?.includes("specified a rule") ||
error()?.includes("user dismissed"),

View File

@@ -10,7 +10,7 @@ import { ProviderAuth } from "@/provider/auth-service"
import { Question } from "@/question/service"
import { Skill } from "@/skill/service"
import { Snapshot } from "@/snapshot/service"
import { Plugin } from "@/plugin"
import { ToolRegistry } from "@/tool/registry"
import { InstanceContext } from "./instance-context"
import { registerDisposer } from "./instance-registry"
@@ -27,7 +27,7 @@ export type InstanceServices =
| File.Service
| Skill.Service
| Snapshot.Service
| Plugin.Service
| ToolRegistry.Service
// NOTE: LayerMap only passes the key (directory string) to lookup, but we need
// the full instance context (directory, worktree, project). We read from the
@@ -48,7 +48,7 @@ function lookup(_key: string) {
File.layer,
Skill.defaultLayer,
Snapshot.defaultLayer,
Plugin.layer,
ToolRegistry.layer,
).pipe(Layer.provide(ctx))
}

View File

@@ -1,206 +1,144 @@
import type { Hooks, PluginInput, Plugin as PluginInstance } from "@opencode-ai/plugin"
import { Config } from "../config/config"
import { Bus } from "../bus"
import { Log } from "../util/log"
import { createOpencodeClient } from "@opencode-ai/sdk"
import { Server } from "../server/server"
import { BunProc } from "../bun"
import { Instance } from "../project/instance"
import { Flag } from "../flag/flag"
import { CodexAuthPlugin } from "./codex"
import { Session } from "../session"
import { NamedError } from "@opencode-ai/util/error"
import { Effect, Layer, ServiceMap } from "effect"
import { InstanceContext } from "@/effect/instance-context"
import { CopilotAuthPlugin } from "./copilot"
import { gitlabAuthPlugin as GitlabAuthPlugin } from "opencode-gitlab-auth"
export namespace Plugin {
const log = Log.create({ service: "plugin" })
export interface Interface {
readonly trigger: <
Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
Input = Parameters<Required<Hooks>[Name]>[0],
Output = Parameters<Required<Hooks>[Name]>[1],
>(
name: Name,
input: Input,
output: Output,
) => Effect.Effect<Output>
readonly list: () => Effect.Effect<Hooks[]>
readonly init: () => Effect.Effect<void>
}
// Built-in plugins that are directly imported (not installed from npm)
const INTERNAL_PLUGINS: PluginInstance[] = [CodexAuthPlugin, CopilotAuthPlugin, GitlabAuthPlugin]
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Plugin") {}
const state = Instance.state(async () => {
const client = createOpencodeClient({
baseUrl: "http://localhost:4096",
directory: Instance.directory,
headers: Flag.OPENCODE_SERVER_PASSWORD
? {
Authorization: `Basic ${Buffer.from(`${Flag.OPENCODE_SERVER_USERNAME ?? "opencode"}:${Flag.OPENCODE_SERVER_PASSWORD}`).toString("base64")}`,
}
: undefined,
fetch: async (...args) => Server.Default().fetch(...args),
})
const config = await Config.get()
const hooks: Hooks[] = []
const input: PluginInput = {
client,
project: Instance.project,
worktree: Instance.worktree,
directory: Instance.directory,
get serverUrl(): URL {
return Server.url ?? new URL("http://localhost:4096")
},
$: Bun.$,
}
export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const instance = yield* InstanceContext
const hooks: Hooks[] = []
let task: Promise<void> | undefined
for (const plugin of INTERNAL_PLUGINS) {
log.info("loading internal plugin", { name: plugin.name })
const init = await plugin(input).catch((err) => {
log.error("failed to load internal plugin", { name: plugin.name, error: err })
})
if (init) hooks.push(init)
}
const load = Effect.fn("Plugin.load")(function* () {
yield* Effect.promise(async () => {
const [{ Config }, { Server }, codex, copilot, gitlab] = await Promise.all([
import("../config/config"),
import("../server/server"),
import("./codex"),
import("./copilot"),
import("opencode-gitlab-auth"),
])
const internal: PluginInstance[] = [codex.CodexAuthPlugin, copilot.CopilotAuthPlugin, gitlab.gitlabAuthPlugin]
const client = createOpencodeClient({
baseUrl: "http://localhost:4096",
directory: instance.directory,
headers: Flag.OPENCODE_SERVER_PASSWORD
? {
Authorization: `Basic ${Buffer.from(`${Flag.OPENCODE_SERVER_USERNAME ?? "opencode"}:${Flag.OPENCODE_SERVER_PASSWORD}`).toString("base64")}`,
}
: undefined,
fetch: async (...args) => Server.Default().fetch(...args),
let plugins = config.plugin ?? []
if (plugins.length) await Config.waitForDependencies()
for (let plugin of plugins) {
// ignore old codex plugin since it is supported first party now
if (plugin.includes("opencode-openai-codex-auth") || plugin.includes("opencode-copilot-auth")) continue
log.info("loading plugin", { path: plugin })
if (!plugin.startsWith("file://")) {
const lastAtIndex = plugin.lastIndexOf("@")
const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
plugin = await BunProc.install(pkg, version).catch((err) => {
const cause = err instanceof Error ? err.cause : err
const detail = cause instanceof Error ? cause.message : String(cause ?? err)
log.error("failed to install plugin", { pkg, version, error: detail })
Bus.publish(Session.Event.Error, {
error: new NamedError.Unknown({
message: `Failed to install plugin ${pkg}@${version}: ${detail}`,
}).toObject(),
})
const config = await Config.get()
const input: PluginInput = {
client,
project: instance.project,
worktree: instance.worktree,
directory: instance.directory,
get serverUrl(): URL {
return Server.url ?? new URL("http://localhost:4096")
},
$: Bun.$,
}
for (const plugin of internal) {
log.info("loading internal plugin", { name: plugin.name })
const init = await plugin(input).catch((err) => {
log.error("failed to load internal plugin", { name: plugin.name, error: err })
})
if (init) hooks.push(init)
}
let plugins = config.plugin ?? []
if (plugins.length) await Config.waitForDependencies()
for (let plugin of plugins) {
// ignore old codex plugin since it is supported first party now
if (plugin.includes("opencode-openai-codex-auth") || plugin.includes("opencode-copilot-auth")) continue
log.info("loading plugin", { path: plugin })
if (!plugin.startsWith("file://")) {
const lastAtIndex = plugin.lastIndexOf("@")
const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
plugin = await BunProc.install(pkg, version).catch((err) => {
const cause = err instanceof Error ? err.cause : err
const detail = cause instanceof Error ? cause.message : String(cause ?? err)
log.error("failed to install plugin", { pkg, version, error: detail })
void import("../session").then(({ Session }) =>
Bus.publish(Session.Event.Error, {
error: new NamedError.Unknown({
message: `Failed to install plugin ${pkg}@${version}: ${detail}`,
}).toObject(),
}),
)
return ""
})
if (!plugin) continue
}
// Prevent duplicate initialization when plugins export the same function
// as both a named export and default export (e.g., `export const X` and `export default X`).
// Object.entries(mod) would return both entries pointing to the same function reference.
await import(plugin)
.then(async (mod) => {
const seen = new Set<PluginInstance>()
for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
if (seen.has(fn)) continue
seen.add(fn)
hooks.push(await fn(input))
}
})
.catch((err) => {
const message = err instanceof Error ? err.message : String(err)
log.error("failed to load plugin", { path: plugin, error: message })
void import("../session").then(({ Session }) =>
Bus.publish(Session.Event.Error, {
error: new NamedError.Unknown({
message: `Failed to load plugin ${plugin}: ${message}`,
}).toObject(),
}),
)
})
return ""
})
if (!plugin) continue
}
// Prevent duplicate initialization when plugins export the same function
// as both a named export and default export (e.g., `export const X` and `export default X`).
// Object.entries(mod) would return both entries pointing to the same function reference.
await import(plugin)
.then(async (mod) => {
const seen = new Set<PluginInstance>()
for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
if (seen.has(fn)) continue
seen.add(fn)
hooks.push(await fn(input))
}
})
})
const ensure = Effect.fn("Plugin.ensure")(function* () {
yield* Effect.promise(() => {
task ??= Effect.runPromise(
load().pipe(Effect.catchCause((cause) => Effect.sync(() => log.error("init failed", { cause })))),
)
return task
})
})
const trigger = Effect.fn("Plugin.trigger")(function* <
Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
Input = Parameters<Required<Hooks>[Name]>[0],
Output = Parameters<Required<Hooks>[Name]>[1],
>(name: Name, input: Input, output: Output) {
if (!name) return output
yield* ensure()
yield* Effect.promise(async () => {
for (const hook of hooks) {
const fn = hook[name]
if (!fn) continue
// @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
// give up.
// try-counter: 2
await fn(input, output)
}
})
return output
})
const list = Effect.fn("Plugin.list")(function* () {
yield* ensure()
return hooks
})
const init = Effect.fn("Plugin.init")(function* () {
yield* ensure()
yield* Effect.promise(async () => {
const { Config } = await import("../config/config")
const config = await Config.get()
for (const hook of hooks) {
await (hook as any).config?.(config)
}
Bus.subscribeAll(async (input) => {
for (const hook of hooks) {
hook["event"]?.({
event: input,
})
}
.catch((err) => {
const message = err instanceof Error ? err.message : String(err)
log.error("failed to load plugin", { path: plugin, error: message })
Bus.publish(Session.Event.Error, {
error: new NamedError.Unknown({
message: `Failed to load plugin ${plugin}: ${message}`,
}).toObject(),
})
})
})
}
return Service.of({ trigger, list, init })
}),
).pipe(Layer.fresh)
async function run<A, E>(effect: Effect.Effect<A, E, Service>) {
const { runPromiseInstance } = await import("@/effect/runtime")
return runPromiseInstance(effect)
}
return {
hooks,
input,
}
})
export async function trigger<
Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
Input = Parameters<Required<Hooks>[Name]>[0],
Output = Parameters<Required<Hooks>[Name]>[1],
>(name: Name, input: Input, output: Output): Promise<Output> {
return run(Service.use((svc) => svc.trigger(name, input, output)))
if (!name) return output
for (const hook of await state().then((x) => x.hooks)) {
const fn = hook[name]
if (!fn) continue
// @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
// give up.
// try-counter: 2
await fn(input, output)
}
return output
}
export async function list(): Promise<Hooks[]> {
return run(Service.use((svc) => svc.list()))
export async function list() {
return state().then((x) => x.hooks)
}
export async function init() {
return run(Service.use((svc) => svc.init()))
const hooks = await state().then((x) => x.hooks)
const config = await Config.get()
for (const hook of hooks) {
// @ts-expect-error this is because we haven't moved plugin to sdk v2
await hook.config?.(config)
}
Bus.subscribeAll(async (input) => {
const hooks = await state().then((x) => x.hooks)
for (const hook of hooks) {
hook["event"]?.({
event: input,
})
}
})
}
}

View File

@@ -1,4 +1,4 @@
import type { AuthOuathResult, Hooks } from "@opencode-ai/plugin"
import type { AuthOuathResult } from "@opencode-ai/plugin"
import { NamedError } from "@opencode-ai/util/error"
import * as Auth from "@/auth/effect"
import { ProviderID } from "./schema"
@@ -6,8 +6,6 @@ import { Array as Arr, Effect, Layer, Record, Result, ServiceMap, Struct } from
import z from "zod"
export namespace ProviderAuth {
type Hook = NonNullable<Hooks["auth"]>
export const Method = z
.object({
type: z.union([z.literal("oauth"), z.literal("api")]),
@@ -107,26 +105,20 @@ export namespace ProviderAuth {
Service,
Effect.gen(function* () {
const auth = yield* Auth.Auth.Service
let hooks: Record<ProviderID, Hook> | undefined
const hooks = yield* Effect.promise(async () => {
const mod = await import("../plugin")
const plugins = await mod.Plugin.list()
return Record.fromEntries(
Arr.filterMap(plugins, (x) =>
x.auth?.provider !== undefined
? Result.succeed([ProviderID.make(x.auth.provider), x.auth] as const)
: Result.failVoid,
),
)
})
const pending = new Map<ProviderID, AuthOuathResult>()
const load = Effect.fn("ProviderAuth.load")(function* () {
if (hooks) return hooks
hooks = yield* Effect.promise(async () => {
const mod = await import("../plugin")
const plugins = await mod.Plugin.list()
const result = {} as Record<ProviderID, Hook>
for (const item of plugins) {
if (item.auth?.provider === undefined) continue
result[ProviderID.make(item.auth.provider)] = item.auth
}
return result
})
return hooks
})
const methods = Effect.fn("ProviderAuth.methods")(function* () {
const hooks = yield* load()
return Record.map(hooks, (item) =>
item.methods.map(
(method): Method => ({
@@ -160,7 +152,6 @@ export namespace ProviderAuth {
method: number
inputs?: Record<string, string>
}) {
const hooks = yield* load()
const method = hooks[input.providerID].methods[input.method]
if (method.type !== "oauth") return
@@ -187,7 +178,6 @@ export namespace ProviderAuth {
method: number
code?: string
}) {
yield* load()
const match = pending.get(input.providerID)
if (!match) return yield* Effect.fail(new OauthMissing({ providerID: input.providerID }))
if (match.method === "code" && !input.code) {

View File

@@ -1,132 +1,240 @@
import { PlanExitTool } from "./plan"
import { QuestionTool } from "./question"
import { BashTool } from "./bash"
import { EditTool } from "./edit"
import { GlobTool } from "./glob"
import { GrepTool } from "./grep"
import { BatchTool } from "./batch"
import { ReadTool } from "./read"
import { TaskTool } from "./task"
import { TodoWriteTool, TodoReadTool } from "./todo"
import { WebFetchTool } from "./webfetch"
import { WriteTool } from "./write"
import { InvalidTool } from "./invalid"
import { SkillTool } from "./skill"
import type { Agent } from "../agent/agent"
import { Tool } from "./tool"
import { Instance } from "../project/instance"
import { Config } from "../config/config"
import path from "path"
import { type ToolContext as PluginToolContext, type ToolDefinition } from "@opencode-ai/plugin"
import z from "zod"
import { Plugin } from "../plugin"
import { ProviderID, type ModelID } from "../provider/schema"
import { WebSearchTool } from "./websearch"
import { CodeSearchTool } from "./codesearch"
import { Flag } from "@/flag/flag"
import { Log } from "@/util/log"
import { LspTool } from "./lsp"
import { Truncate } from "./truncate"
import { ApplyPatchTool } from "./apply_patch"
import { Glob } from "../util/glob"
import { pathToFileURL } from "url"
import { Effect, Layer, ServiceMap } from "effect"
import { InstanceContext } from "@/effect/instance-context"
export namespace ToolRegistry {
const log = Log.create({ service: "tool.registry" })
export const state = Instance.state(async () => {
const custom = [] as Tool.Info[]
export interface Interface {
readonly register: (tool: Tool.Info) => Effect.Effect<void>
readonly ids: () => Effect.Effect<string[]>
readonly tools: (
model: { providerID: ProviderID; modelID: ModelID },
agent?: Agent.Info,
) => Effect.Effect<(Awaited<ReturnType<Tool.Info["init"]>> & { id: string })[]>
}
const matches = await Config.directories().then((dirs) =>
dirs.flatMap((dir) =>
Glob.scanSync("{tool,tools}/*.{js,ts}", { cwd: dir, absolute: true, dot: true, symlink: true }),
),
)
if (matches.length) await Config.waitForDependencies()
for (const match of matches) {
const namespace = path.basename(match, path.extname(match))
const mod = await import(process.platform === "win32" ? match : pathToFileURL(match).href)
for (const [id, def] of Object.entries<ToolDefinition>(mod)) {
custom.push(fromPlugin(id === "default" ? namespace : `${namespace}_${id}`, def))
}
}
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/ToolRegistry") {}
const plugins = await Plugin.list()
for (const plugin of plugins) {
for (const [id, def] of Object.entries(plugin.tool ?? {})) {
custom.push(fromPlugin(id, def))
}
}
export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const instance = yield* InstanceContext
return { custom }
})
const custom: Tool.Info[] = []
let task: Promise<void> | undefined
function fromPlugin(id: string, def: ToolDefinition): Tool.Info {
return {
id,
init: async (initCtx) => ({
parameters: z.object(def.args),
description: def.description,
execute: async (args, ctx) => {
const pluginCtx = {
...ctx,
directory: Instance.directory,
worktree: Instance.worktree,
} as unknown as PluginToolContext
const result = await def.execute(args as any, pluginCtx)
const out = await Truncate.output(result, {}, initCtx?.agent)
return {
title: "",
output: out.truncated ? out.content : result,
metadata: { truncated: out.truncated, outputPath: out.truncated ? out.outputPath : undefined },
const load = Effect.fn("ToolRegistry.load")(function* () {
yield* Effect.promise(async () => {
const [{ Config }, { Plugin }] = await Promise.all([import("../config/config"), import("../plugin")])
const matches = await Config.directories().then((dirs) =>
dirs.flatMap((dir) =>
Glob.scanSync("{tool,tools}/*.{js,ts}", { cwd: dir, absolute: true, dot: true, symlink: true }),
),
)
if (matches.length) await Config.waitForDependencies()
for (const match of matches) {
const namespace = path.basename(match, path.extname(match))
const mod = await import(process.platform === "win32" ? match : pathToFileURL(match).href)
for (const [id, def] of Object.entries<ToolDefinition>(mod)) {
custom.push(fromPlugin(id === "default" ? namespace : `${namespace}_${id}`, def))
}
}
},
}),
}
const plugins = await Plugin.list()
for (const plugin of plugins) {
for (const [id, def] of Object.entries(plugin.tool ?? {})) {
custom.push(fromPlugin(id, def))
}
}
})
})
const ensure = Effect.fn("ToolRegistry.ensure")(function* () {
yield* Effect.promise(() => {
task ??= Effect.runPromise(
load().pipe(Effect.catchCause((cause) => Effect.sync(() => log.error("init failed", { cause })))),
)
return task
})
})
function fromPlugin(id: string, def: ToolDefinition): Tool.Info {
return {
id,
init: async (initCtx) => ({
parameters: z.object(def.args),
description: def.description,
execute: async (args, ctx) => {
const pluginCtx = {
...ctx,
directory: instance.directory,
worktree: instance.worktree,
} as unknown as PluginToolContext
const result = await def.execute(args as any, pluginCtx)
const out = await Truncate.output(result, {}, initCtx?.agent)
return {
title: "",
output: out.truncated ? out.content : result,
metadata: { truncated: out.truncated, outputPath: out.truncated ? out.outputPath : undefined },
}
},
}),
}
}
async function all(): Promise<Tool.Info[]> {
const { Config } = await import("../config/config")
const config = await Config.get()
const question = ["app", "cli", "desktop"].includes(Flag.OPENCODE_CLIENT) || Flag.OPENCODE_ENABLE_QUESTION_TOOL
const [
invalid,
questionMod,
bash,
read,
glob,
grep,
edit,
write,
task,
webfetch,
todo,
websearch,
codesearch,
skill,
applyPatch,
lsp,
batch,
plan,
] = await Promise.all([
import("./invalid"),
import("./question"),
import("./bash"),
import("./read"),
import("./glob"),
import("./grep"),
import("./edit"),
import("./write"),
import("./task"),
import("./webfetch"),
import("./todo"),
import("./websearch"),
import("./codesearch"),
import("./skill"),
import("./apply_patch"),
import("./lsp"),
import("./batch"),
import("./plan"),
])
return [
invalid.InvalidTool,
...(question ? [questionMod.QuestionTool] : []),
bash.BashTool,
read.ReadTool,
glob.GlobTool,
grep.GrepTool,
edit.EditTool,
write.WriteTool,
task.TaskTool,
webfetch.WebFetchTool,
todo.TodoWriteTool,
// TodoReadTool,
websearch.WebSearchTool,
codesearch.CodeSearchTool,
skill.SkillTool,
applyPatch.ApplyPatchTool,
...(Flag.OPENCODE_EXPERIMENTAL_LSP_TOOL ? [lsp.LspTool] : []),
...(config.experimental?.batch_tool === true ? [batch.BatchTool] : []),
...(Flag.OPENCODE_EXPERIMENTAL_PLAN_MODE && Flag.OPENCODE_CLIENT === "cli" ? [plan.PlanExitTool] : []),
...custom,
]
}
const register = Effect.fn("ToolRegistry.register")(function* (tool: Tool.Info) {
yield* ensure()
const idx = custom.findIndex((t) => t.id === tool.id)
if (idx >= 0) {
custom.splice(idx, 1, tool)
return
}
custom.push(tool)
})
const ids = Effect.fn("ToolRegistry.ids")(function* () {
yield* ensure()
const tools = yield* Effect.promise(() => all())
return tools.map((t) => t.id)
})
const tools = Effect.fn("ToolRegistry.tools")(function* (
model: { providerID: ProviderID; modelID: ModelID },
agent?: Agent.Info,
) {
yield* ensure()
const allTools = yield* Effect.promise(() => all())
return yield* Effect.promise(() =>
Promise.all(
allTools
.filter((t) => {
// Enable websearch/codesearch for zen users OR via enable flag
if (t.id === "codesearch" || t.id === "websearch") {
return model.providerID === ProviderID.opencode || Flag.OPENCODE_ENABLE_EXA
}
// use apply tool in same format as codex
const usePatch =
model.modelID.includes("gpt-") && !model.modelID.includes("oss") && !model.modelID.includes("gpt-4")
if (t.id === "apply_patch") return usePatch
if (t.id === "edit" || t.id === "write") return !usePatch
return true
})
.map(async (t) => {
using _ = log.time(t.id)
const tool = await t.init({ agent })
const output = {
description: tool.description,
parameters: tool.parameters,
}
const { Plugin } = await import("../plugin")
await Plugin.trigger("tool.definition", { toolID: t.id }, output)
return {
id: t.id,
...tool,
description: output.description,
parameters: output.parameters,
}
}),
),
)
})
return Service.of({ register, ids, tools })
}),
).pipe(Layer.fresh)
async function run<A, E>(effect: Effect.Effect<A, E, Service>) {
const { runPromiseInstance } = await import("@/effect/runtime")
return runPromiseInstance(effect)
}
export async function register(tool: Tool.Info) {
const { custom } = await state()
const idx = custom.findIndex((t) => t.id === tool.id)
if (idx >= 0) {
custom.splice(idx, 1, tool)
return
}
custom.push(tool)
}
async function all(): Promise<Tool.Info[]> {
const custom = await state().then((x) => x.custom)
const config = await Config.get()
const question = ["app", "cli", "desktop"].includes(Flag.OPENCODE_CLIENT) || Flag.OPENCODE_ENABLE_QUESTION_TOOL
return [
InvalidTool,
...(question ? [QuestionTool] : []),
BashTool,
ReadTool,
GlobTool,
GrepTool,
EditTool,
WriteTool,
TaskTool,
WebFetchTool,
TodoWriteTool,
// TodoReadTool,
WebSearchTool,
CodeSearchTool,
SkillTool,
ApplyPatchTool,
...(Flag.OPENCODE_EXPERIMENTAL_LSP_TOOL ? [LspTool] : []),
...(config.experimental?.batch_tool === true ? [BatchTool] : []),
...(Flag.OPENCODE_EXPERIMENTAL_PLAN_MODE && Flag.OPENCODE_CLIENT === "cli" ? [PlanExitTool] : []),
...custom,
]
return run(Service.use((svc) => svc.register(tool)))
}
export async function ids() {
return all().then((x) => x.map((t) => t.id))
return run(Service.use((svc) => svc.ids()))
}
export async function tools(
@@ -136,39 +244,6 @@ export namespace ToolRegistry {
},
agent?: Agent.Info,
) {
const tools = await all()
const result = await Promise.all(
tools
.filter((t) => {
// Enable websearch/codesearch for zen users OR via enable flag
if (t.id === "codesearch" || t.id === "websearch") {
return model.providerID === ProviderID.opencode || Flag.OPENCODE_ENABLE_EXA
}
// use apply tool in same format as codex
const usePatch =
model.modelID.includes("gpt-") && !model.modelID.includes("oss") && !model.modelID.includes("gpt-4")
if (t.id === "apply_patch") return usePatch
if (t.id === "edit" || t.id === "write") return !usePatch
return true
})
.map(async (t) => {
using _ = log.time(t.id)
const tool = await t.init({ agent })
const output = {
description: tool.description,
parameters: tool.parameters,
}
await Plugin.trigger("tool.definition", { toolID: t.id }, output)
return {
id: t.id,
...tool,
description: output.description,
parameters: output.parameters,
}
}),
)
return result
return run(Service.use((svc) => svc.tools(model, agent)))
}
}