refactor(plugin): simplify promise tool declarations

This commit is contained in:
Dax Raad 2026-07-10 00:17:37 -04:00
commit efeff63749
7 changed files with 91 additions and 60 deletions

View file

@ -1,7 +1,9 @@
export * as PluginPromise from "./promise"
import { Plugin } from "@opencode-ai/plugin/v2/effect"
import type { AnyTool, RegisterOptions } from "@opencode-ai/plugin/v2/tool"
import { Effect, Scope, Stream } from "effect"
import { Tool } from "../tool/tool"
type HostRegistration = { readonly dispose: Effect.Effect<void> }
type Registration = { readonly dispose: () => Promise<void> }
@ -108,7 +110,15 @@ export function fromPromise(plugin: PromisePlugin) {
reload: () => run(host.skill.reload()),
},
tool: {
transform: transform(host.tool),
transform: (callback) =>
register(
host.tool.transform((draft) =>
callback({
add: (name: string, tool: AnyTool, options?: RegisterOptions) =>
draft.add(name, fromPromiseTool(tool), options),
}),
),
),
hook: (name, callback) =>
register(host.tool.hook(name, (event) => Effect.promise(() => Promise.resolve(callback(event))))),
},
@ -125,3 +135,15 @@ export function fromPromise(plugin: PromisePlugin) {
}),
})
}
function fromPromiseTool(tool: AnyTool) {
if ("jsonSchema" in tool)
return Tool.make({
...tool,
execute: (input, context) => Effect.promise(() => tool.execute(input, context)),
})
return Tool.make({
...tool,
execute: (input, context) => Effect.promise(() => tool.execute(input, context)),
})
}