opencode_offline/packages/opencode/src/plugin/index.ts

100 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-08-03 06:50:19 +08:00
import type { Hooks, Plugin as PluginInstance } from "@opencode-ai/plugin"
import { Config } from "../config/config"
import { Bus } from "../bus"
import { Log } from "../util/log"
import { createOpencodeClient } from "@opencode-ai/sdk"
import { Server } from "../server/server"
2025-08-04 09:19:03 +08:00
import { BunProc } from "../bun"
import { Instance } from "../project/instance"
import { Flag } from "../flag/flag"
import { ToolRegistry } from "../tool/registry"
2025-08-03 06:50:19 +08:00
export namespace Plugin {
const log = Log.create({ service: "plugin" })
const state = Instance.state(async () => {
2025-08-03 06:50:19 +08:00
const client = createOpencodeClient({
baseUrl: "http://localhost:4096",
fetch: async (...args) => Server.App.fetch(...args),
2025-08-03 06:50:19 +08:00
})
const config = await Config.get()
const hooks = []
const input = {
client,
project: Instance.project,
worktree: Instance.worktree,
directory: Instance.directory,
$: Bun.$,
2025-09-10 11:43:37 +08:00
Tool: await import("../tool/tool").then((m) => m.Tool),
z: await import("zod").then((m) => m.z),
}
const plugins = [...(config.plugin ?? [])]
if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
2025-08-20 23:46:14 +08:00
plugins.push("opencode-copilot-auth@0.0.2")
2025-08-19 05:12:21 +08:00
plugins.push("opencode-anthropic-auth@0.0.2")
}
for (let plugin of plugins) {
2025-08-03 06:50:19 +08:00
log.info("loading plugin", { path: plugin })
2025-08-04 09:19:03 +08:00
if (!plugin.startsWith("file://")) {
const [pkg, version] = plugin.split("@")
plugin = await BunProc.install(pkg, version ?? "latest")
}
2025-08-03 06:50:19 +08:00
const mod = await import(plugin)
for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
const init = await fn(input)
2025-08-03 06:50:19 +08:00
hooks.push(init)
}
}
return {
hooks,
input,
2025-08-03 06:50:19 +08:00
}
})
export async function trigger<
Name extends Exclude<keyof Required<Hooks>, "auth" | "event">,
2025-08-04 09:42:45 +08:00
Input = Parameters<Required<Hooks>[Name]>[0],
Output = Parameters<Required<Hooks>[Name]>[1],
>(name: Name, input: Input, output: Output): Promise<Output> {
if (!name) return output
2025-08-03 06:50:19 +08:00
for (const hook of await state().then((x) => x.hooks)) {
2025-08-04 09:42:45 +08:00
const fn = hook[name]
2025-08-03 06:50:19 +08:00
if (!fn) continue
2025-08-04 05:09:19 +08:00
// @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
2025-08-03 06:50:19 +08:00
await fn(input, output)
}
return output
}
export async function list() {
return state().then((x) => x.hooks)
}
2025-08-21 23:22:13 +08:00
export async function init() {
const hooks = await state().then((x) => x.hooks)
const config = await Config.get()
for (const hook of hooks) {
2025-08-21 23:24:48 +08:00
await hook.config?.(config)
// Let plugins register tools at startup
2025-09-10 11:43:37 +08:00
await hook["tool.register"]?.(
{},
{
registerHTTP: ToolRegistry.registerHTTP,
register: ToolRegistry.register,
},
)
2025-08-21 23:22:13 +08:00
}
2025-08-03 06:50:19 +08:00
Bus.subscribeAll(async (input) => {
const hooks = await state().then((x) => x.hooks)
for (const hook of hooks) {
hook["event"]?.({
event: input,
})
}
})
}
}