Skip to content

Commit 3ed615e

Browse files
Apply PR #11567: fix(types): plugin trigger types are now correct and safe
2 parents 79d7f4e + 4e1a792 commit 3ed615e

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

packages/opencode/src/plugin/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,20 @@ export namespace Plugin {
9595
}
9696
})
9797

98-
export async function trigger<
99-
Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
100-
Input = Parameters<Required<Hooks>[Name]>[0],
101-
Output = Parameters<Required<Hooks>[Name]>[1],
102-
>(name: Name, input: Input, output: Output): Promise<Output> {
98+
type HookName = Exclude<keyof Hooks, "auth" | "event" | "tool" | "config">
99+
type Params<Name extends HookName> = Parameters<Required<Hooks>[Name]>
100+
101+
export const trigger = async <Name extends HookName>(
102+
name: Name,
103+
...params: Params<Name>
104+
): Promise<(typeof params)[1]> => {
105+
const [, output] = params
103106
if (!name) return output
104107
for (const hook of await state().then((x) => x.hooks)) {
105-
const fn = hook[name]
108+
// this cast is safe and correctly types `fn`
109+
const fn = hook[name] as Extract<Hooks[Name], (...params: Params<Name>) => any>
106110
if (!fn) continue
107-
// @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
108-
// give up.
109-
// try-counter: 2
110-
await fn(input, output)
111+
await fn(...params)
111112
}
112113
return output
113114
}
@@ -120,7 +121,6 @@ export namespace Plugin {
120121
const hooks = await state().then((x) => x.hooks)
121122
const config = await Config.get()
122123
for (const hook of hooks) {
123-
// @ts-expect-error this is because we haven't moved plugin to sdk v2
124124
await hook.config?.(config)
125125
}
126126
Bus.subscribeAll(async (input) => {

packages/plugin/src/index.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import { createOpencodeClient } from "@opencode-ai/sdk"
12
import type {
23
Event,
3-
createOpencodeClient,
44
Project,
55
Model,
66
Provider,
7-
Permission,
87
UserMessage,
9-
Message,
10-
Part,
118
Auth,
129
Config,
13-
} from "@opencode-ai/sdk"
10+
Agent,
11+
Message,
12+
Part,
13+
} from "@opencode-ai/sdk/v2"
1414

1515
import type { BunShell } from "./shell"
1616
import { type ToolDefinition } from "./tool"
@@ -169,29 +169,42 @@ export interface Hooks {
169169
* Modify parameters sent to LLM
170170
*/
171171
"chat.params"?: (
172-
input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
173-
output: { temperature: number; topP: number; topK: number; options: Record<string, any> },
172+
input: { sessionID: string; agent: Agent; model: Model; provider: Provider; message: UserMessage },
173+
output: { temperature?: number; topP?: number; topK?: number; options: Record<string, any> },
174174
) => Promise<void>
175175
"chat.headers"?: (
176-
input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
176+
input: { sessionID: string; agent: Omit<Agent, 'builtIn' | 'tools'>; model: Model; provider: Provider; message: UserMessage },
177177
output: { headers: Record<string, string> },
178178
) => Promise<void>
179-
"permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
179+
"permission.ask"?: (
180+
input: {
181+
id: string
182+
type: string
183+
pattern?: string | Array<string>
184+
sessionID: string
185+
messageID: string
186+
callID?: string
187+
message: string
188+
metadata: { [key: string]: unknown }
189+
time: { created: number }
190+
},
191+
output: { status: "ask" | "deny" | "allow" },
192+
) => Promise<void>
180193
"command.execute.before"?: (
181194
input: { command: string; sessionID: string; arguments: string },
182-
output: { parts: Part[] },
195+
output: { parts: Omit<Part | { id?: string }, 'sessionID' | 'messageID'>[] },
183196
) => Promise<void>
184197
"tool.execute.before"?: (
185-
input: { tool: string; sessionID: string; callID: string },
198+
input: { tool: string; sessionID: string; callID?: string },
186199
output: { args: any },
187200
) => Promise<void>
188201
"tool.execute.after"?: (
189-
input: { tool: string; sessionID: string; callID: string },
202+
input: { tool: string; sessionID: string; callID?: string },
190203
output: {
191204
title: string
192205
output: string
193206
metadata: any
194-
},
207+
} | undefined,
195208
) => Promise<void>
196209
"experimental.chat.messages.transform"?: (
197210
input: {},

0 commit comments

Comments
 (0)