f993541e0b
This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
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"
|
|
import { BunProc } from "../bun"
|
|
import { Instance } from "../project/instance"
|
|
import { Flag } from "../flag/flag"
|
|
|
|
export namespace Plugin {
|
|
const log = Log.create({ service: "plugin" })
|
|
|
|
const state = Instance.state(async () => {
|
|
const client = createOpencodeClient({
|
|
baseUrl: "http://localhost:4096",
|
|
fetch: async (...args) => Server.App.fetch(...args),
|
|
})
|
|
const config = await Config.get()
|
|
const hooks = []
|
|
const input = {
|
|
client,
|
|
project: Instance.project,
|
|
worktree: Instance.worktree,
|
|
directory: Instance.directory,
|
|
$: Bun.$,
|
|
}
|
|
const plugins = [...(config.plugin ?? [])]
|
|
if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
|
|
plugins.push("opencode-copilot-auth@0.0.2")
|
|
plugins.push("opencode-anthropic-auth@0.0.2")
|
|
}
|
|
for (let plugin of plugins) {
|
|
log.info("loading plugin", { path: plugin })
|
|
if (!plugin.startsWith("file://")) {
|
|
const [pkg, version] = plugin.split("@")
|
|
plugin = await BunProc.install(pkg, version ?? "latest")
|
|
}
|
|
const mod = await import(plugin)
|
|
for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
|
|
const init = await fn(input)
|
|
hooks.push(init)
|
|
}
|
|
}
|
|
|
|
return {
|
|
hooks,
|
|
input,
|
|
}
|
|
})
|
|
|
|
export async function trigger<
|
|
Name extends Exclude<keyof Required<Hooks>, "auth" | "event">,
|
|
Input = Parameters<Required<Hooks>[Name]>[0],
|
|
Output = Parameters<Required<Hooks>[Name]>[1],
|
|
>(name: Name, input: Input, output: Output): Promise<Output> {
|
|
if (!name) return output
|
|
for (const hook of await state().then((x) => x.hooks)) {
|
|
const fn = hook[name]
|
|
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)
|
|
}
|
|
return output
|
|
}
|
|
|
|
export async function list() {
|
|
return state().then((x) => x.hooks)
|
|
}
|
|
|
|
export async function init() {
|
|
const hooks = await state().then((x) => x.hooks)
|
|
const config = await Config.get()
|
|
for (const hook of hooks) {
|
|
await hook.config?.(config)
|
|
}
|
|
Bus.subscribeAll(async (input) => {
|
|
const hooks = await state().then((x) => x.hooks)
|
|
for (const hook of hooks) {
|
|
hook["event"]?.({
|
|
event: input,
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|