Apply PR #11567: fix(types): plugin trigger types are now correct and safe

This commit is contained in:
opencode-agent[bot]
2026-02-01 14:06:41 +00:00
2 changed files with 37 additions and 24 deletions

View File

@@ -95,19 +95,20 @@ export namespace Plugin {
}
})
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> {
type HookName = Exclude<keyof Hooks, "auth" | "event" | "tool" | "config">
type Params<Name extends HookName> = Parameters<Required<Hooks>[Name]>
export const trigger = async <Name extends HookName>(
name: Name,
...params: Params<Name>
): 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<Hooks[Name], (...params: Params<Name>) => 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) => {

View File

@@ -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<string, any> },
input: { sessionID: string; agent: Agent; model: Model; provider: Provider; message: UserMessage },
output: { temperature?: number; topP?: number; topK?: number; options: Record<string, any> },
) => Promise<void>
"chat.headers"?: (
input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
input: { sessionID: string; agent: Omit<Agent, 'builtIn' | 'tools'>; model: Model; provider: Provider; message: UserMessage },
output: { headers: Record<string, string> },
) => Promise<void>
"permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
"permission.ask"?: (
input: {
id: string
type: string
pattern?: string | Array<string>
sessionID: string
messageID: string
callID?: string
message: string
metadata: { [key: string]: unknown }
time: { created: number }
},
output: { status: "ask" | "deny" | "allow" },
) => Promise<void>
"command.execute.before"?: (
input: { command: string; sessionID: string; arguments: string },
output: { parts: Part[] },
output: { parts: Omit<Part | { id?: string }, 'sessionID' | 'messageID'>[] },
) => Promise<void>
"tool.execute.before"?: (
input: { tool: string; sessionID: string; callID: string },
input: { tool: string; sessionID: string; callID?: string },
output: { args: any },
) => Promise<void>
"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<void>
"experimental.chat.messages.transform"?: (
input: {},