diff --git a/packages/opencode/src/plugin/index.ts b/packages/opencode/src/plugin/index.ts index 6032935f84..3b8f5ee718 100644 --- a/packages/opencode/src/plugin/index.ts +++ b/packages/opencode/src/plugin/index.ts @@ -95,19 +95,20 @@ export namespace Plugin { } }) - export async function trigger< - Name extends Exclude, "auth" | "event" | "tool">, - Input = Parameters[Name]>[0], - Output = Parameters[Name]>[1], - >(name: Name, input: Input, output: Output): Promise { + type HookName = Exclude + type Params = Parameters[Name]> + + export const trigger = async ( + name: Name, + ...params: Params + ): Promise<(typeof params)[1]> => { + const [, output] = params if (!name) return output for (const hook of await state().then((x) => x.hooks)) { - const fn = hook[name] + // this cast is safe and correctly types `fn` + const fn = hook[name] as Extract) => any> 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) + await fn(...params) } return output } @@ -120,7 +121,6 @@ export namespace Plugin { 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) => { diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index 86e7ae9342..abae1119a0 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -1,16 +1,16 @@ +import { createOpencodeClient } from "@opencode-ai/sdk" import type { Event, - createOpencodeClient, Project, Model, Provider, - Permission, UserMessage, - Message, - Part, Auth, Config, -} from "@opencode-ai/sdk" + Agent, + Message, + Part, +} from "@opencode-ai/sdk/v2" import type { BunShell } from "./shell" import { type ToolDefinition } from "./tool" @@ -169,29 +169,42 @@ export interface Hooks { * Modify parameters sent to LLM */ "chat.params"?: ( - input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage }, - output: { temperature: number; topP: number; topK: number; options: Record }, + input: { sessionID: string; agent: Agent; model: Model; provider: Provider; message: UserMessage }, + output: { temperature?: number; topP?: number; topK?: number; options: Record }, ) => Promise "chat.headers"?: ( - input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage }, + input: { sessionID: string; agent: Omit; model: Model; provider: Provider; message: UserMessage }, output: { headers: Record }, ) => Promise - "permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise + "permission.ask"?: ( + input: { + id: string + type: string + pattern?: string | Array + sessionID: string + messageID: string + callID?: string + message: string + metadata: { [key: string]: unknown } + time: { created: number } + }, + output: { status: "ask" | "deny" | "allow" }, + ) => Promise "command.execute.before"?: ( input: { command: string; sessionID: string; arguments: string }, - output: { parts: Part[] }, + output: { parts: Omit[] }, ) => Promise "tool.execute.before"?: ( - input: { tool: string; sessionID: string; callID: string }, + input: { tool: string; sessionID: string; callID?: string }, output: { args: any }, ) => Promise "tool.execute.after"?: ( - input: { tool: string; sessionID: string; callID: string }, + input: { tool: string; sessionID: string; callID?: string }, output: { title: string output: string metadata: any - }, + } | undefined, ) => Promise "experimental.chat.messages.transform"?: ( input: {},