opencode_offline/packages/plugin/src/index.ts

101 lines
2.8 KiB
TypeScript
Raw Normal View History

import type {
Event,
createOpencodeClient,
App,
Model,
Provider,
Permission,
UserMessage,
Part,
Auth,
2025-08-21 23:22:13 +08:00
Config,
} from "@opencode-ai/sdk"
2025-08-05 11:06:49 +08:00
import type { BunShell } from "./shell"
2025-08-03 06:50:19 +08:00
export type PluginInput = {
client: ReturnType<typeof createOpencodeClient>
app: App
2025-08-05 11:06:49 +08:00
$: BunShell
2025-08-03 06:50:19 +08:00
}
export type Plugin = (input: PluginInput) => Promise<Hooks>
export interface Hooks {
event?: (input: { event: Event }) => Promise<void>
2025-08-21 23:22:13 +08:00
config?: (input: Config) => Promise<void>
auth?: {
provider: string
loader?: (auth: () => Promise<Auth>, provider: Provider) => Promise<Record<string, any>>
methods: (
| {
type: "oauth"
label: string
authorize(): Promise<
{ url: string; instructions: string } & (
| {
method: "auto"
callback(): Promise<
2025-08-19 05:12:21 +08:00
| ({
type: "success"
2025-08-19 05:12:21 +08:00
} & (
| {
refresh: string
access: string
expires: number
}
| { key: string }
))
| {
type: "failed"
}
>
}
| {
method: "code"
callback(code: string): Promise<
2025-08-19 05:12:21 +08:00
| ({
type: "success"
2025-08-19 05:12:21 +08:00
} & (
| {
refresh: string
access: string
expires: number
}
| { key: string }
))
| {
type: "failed"
}
>
}
)
>
}
| { type: "api"; label: string }
)[]
}
2025-08-04 09:42:45 +08:00
/**
* Called when a new message is received
*/
"chat.message"?: (input: {}, output: { message: UserMessage; parts: Part[] }) => Promise<void>
/**
* Modify parameters sent to LLM
*/
"chat.params"?: (
input: { model: Model; provider: Provider; message: UserMessage },
2025-08-11 08:30:25 +08:00
output: { temperature: number; topP: number; options: Record<string, any> },
2025-08-04 09:42:45 +08:00
) => Promise<void>
"permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
"tool.execute.before"?: (
input: { tool: string; sessionID: string; callID: string },
output: { args: any },
) => Promise<void>
"tool.execute.after"?: (
input: { tool: string; sessionID: string; callID: string },
output: {
title: string
output: string
metadata: any
},
) => Promise<void>
2025-08-03 06:50:19 +08:00
}