diff options
| author | Dax <[email protected]> | 2025-09-18 03:58:21 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-18 03:58:21 -0400 |
| commit | 3b6c0ec0b3b4f2224311888d8f7be178ed2335da (patch) | |
| tree | 33d2de1a0f3860add12f1211d23b75565eba3e9e /packages/plugin/src | |
| parent | e9d902d84471faff3daf62455d6bc97750e4dc15 (diff) | |
| download | opencode-3b6c0ec0b3b4f2224311888d8f7be178ed2335da.tar.gz opencode-3b6c0ec0b3b4f2224311888d8f7be178ed2335da.zip | |
support custom tools (#2668)
Diffstat (limited to 'packages/plugin/src')
| -rw-r--r-- | packages/plugin/src/example.ts | 20 | ||||
| -rw-r--r-- | packages/plugin/src/index.ts | 42 | ||||
| -rw-r--r-- | packages/plugin/src/tool.ts | 20 |
3 files changed, 41 insertions, 41 deletions
diff --git a/packages/plugin/src/example.ts b/packages/plugin/src/example.ts index 04c48c918..fd6a404d9 100644 --- a/packages/plugin/src/example.ts +++ b/packages/plugin/src/example.ts @@ -1,14 +1,20 @@ import { Plugin } from "./index" +import { tool } from "./tool" -export const ExamplePlugin: Plugin = async ({ - client: _client, - $: _shell, - project: _project, - directory: _directory, - worktree: _worktree, -}) => { +export const ExamplePlugin: Plugin = async (ctx) => { return { permission: {}, + tool: { + mytool: tool((zod) => ({ + description: "This is a custom tool tool", + args: { + foo: zod.string(), + }, + async execute(args, ctx) { + return `Hello ${args.foo}!` + }, + })), + }, async "chat.params"(_input, output) { output.topP = 1 }, diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index f8b6d46f7..9c2647c60 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -10,7 +10,11 @@ import type { Auth, Config, } from "@opencode-ai/sdk" + import type { BunShell } from "./shell" +import { type ToolDefinition } from "./tool" + +export * from "./tool" export type PluginInput = { client: ReturnType<typeof createOpencodeClient> @@ -18,34 +22,16 @@ export type PluginInput = { directory: string worktree: string $: BunShell - Tool: { - define(id: string, init: any | (() => Promise<any>)): any - } - z: any // Zod instance for creating schemas } -export type Plugin = (input: PluginInput) => Promise<Hooks> -// Lightweight schema spec for HTTP-registered tools -export type HttpParamSpec = { - type: "string" | "number" | "boolean" | "array" - description?: string - optional?: boolean - items?: "string" | "number" | "boolean" -} -export type HttpToolRegistration = { - id: string - description: string - parameters: { - type: "object" - properties: Record<string, HttpParamSpec> - } - callbackUrl: string - headers?: Record<string, string> -} +export type Plugin = (input: PluginInput) => Promise<Hooks> export interface Hooks { event?: (input: { event: Event }) => Promise<void> config?: (input: Config) => Promise<void> + tool?: { + [key: string]: ToolDefinition + } auth?: { provider: string loader?: (auth: () => Promise<Auth>, provider: Provider) => Promise<Record<string, any>> @@ -121,16 +107,4 @@ export interface Hooks { metadata: any }, ) => Promise<void> - /** - * Allow plugins to register additional tools with the server. - * Use registerHTTP to add a tool that calls back to your plugin/service. - * Use register to add a native/local tool with direct function execution. - */ - "tool.register"?: ( - input: {}, - output: { - registerHTTP: (tool: HttpToolRegistration) => void | Promise<void> - register: (tool: any) => void | Promise<void> // Tool.Info type from opencode - }, - ) => Promise<void> } diff --git a/packages/plugin/src/tool.ts b/packages/plugin/src/tool.ts new file mode 100644 index 000000000..7c1d3d7c5 --- /dev/null +++ b/packages/plugin/src/tool.ts @@ -0,0 +1,20 @@ +import { z } from "zod/v4" + +export type ToolContext = { + sessionID: string + messageID: string + agent: string + abort: AbortSignal +} + +export function tool<Args extends z.ZodRawShape>( + input: (zod: typeof z) => { + description: string + args: Args + execute: (args: z.infer<z.ZodObject<Args>>, ctx: ToolContext) => Promise<string> + }, +) { + return input(z) +} + +export type ToolDefinition = ReturnType<typeof tool> |
