e82e280e38
Convert the Plugin service's internal Effect.promise blocks to native Effect operations: - Yield Config.Service directly instead of calling Config.get() async facade - Use config.get() and config.waitForDependencies() as Effect yields - Use bus.publish() for error reporting instead of Bus.publish() async facade - Replace monolithic Effect.promise with granular Effect.tryPromise calls for BunProc.install, plugin loading, and plugin initialization - Convert trigger's Effect.promise loop to individual Effect.promise yields - Add Config.defaultLayer to Plugin's defaultLayer dependency chain
226 lines
8.5 KiB
TypeScript
226 lines
8.5 KiB
TypeScript
import type { Hooks, PluginInput, 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 { BunProc } from "../bun"
|
|
import { Flag } from "../flag/flag"
|
|
import { CodexAuthPlugin } from "./codex"
|
|
import { Session } from "../session"
|
|
import { NamedError } from "@opencode-ai/util/error"
|
|
import { CopilotAuthPlugin } from "./copilot"
|
|
import { gitlabAuthPlugin as GitlabAuthPlugin } from "opencode-gitlab-auth"
|
|
import { PoeAuthPlugin } from "opencode-poe-auth"
|
|
import { Effect, Layer, ServiceMap, Stream } from "effect"
|
|
import { InstanceState } from "@/effect/instance-state"
|
|
import { makeRuntime } from "@/effect/run-service"
|
|
|
|
export namespace Plugin {
|
|
const log = Log.create({ service: "plugin" })
|
|
|
|
type State = {
|
|
hooks: Hooks[]
|
|
}
|
|
|
|
// Hook names that follow the (input, output) => Promise<void> trigger pattern
|
|
type TriggerName = {
|
|
[K in keyof Hooks]-?: NonNullable<Hooks[K]> extends (input: any, output: any) => Promise<void> ? K : never
|
|
}[keyof Hooks]
|
|
|
|
export interface Interface {
|
|
readonly trigger: <
|
|
Name extends TriggerName,
|
|
Input = Parameters<Required<Hooks>[Name]>[0],
|
|
Output = Parameters<Required<Hooks>[Name]>[1],
|
|
>(
|
|
name: Name,
|
|
input: Input,
|
|
output: Output,
|
|
) => Effect.Effect<Output>
|
|
readonly list: () => Effect.Effect<Hooks[]>
|
|
readonly init: () => Effect.Effect<void>
|
|
}
|
|
|
|
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Plugin") {}
|
|
|
|
// Built-in plugins that are directly imported (not installed from npm)
|
|
const INTERNAL_PLUGINS: PluginInstance[] = [CodexAuthPlugin, CopilotAuthPlugin, GitlabAuthPlugin, PoeAuthPlugin]
|
|
|
|
// Old npm package names for plugins that are now built-in — skip if users still have them in config
|
|
const DEPRECATED_PLUGIN_PACKAGES = ["opencode-openai-codex-auth", "opencode-copilot-auth"]
|
|
|
|
export const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const bus = yield* Bus.Service
|
|
const config = yield* Config.Service
|
|
|
|
const cache = yield* InstanceState.make<State>(
|
|
Effect.fn("Plugin.state")(function* (ctx) {
|
|
const hooks: Hooks[] = []
|
|
|
|
const { Server } = yield* Effect.promise(() => import("../server/server"))
|
|
|
|
const client = createOpencodeClient({
|
|
baseUrl: "http://localhost:4096",
|
|
directory: ctx.directory,
|
|
headers: Flag.OPENCODE_SERVER_PASSWORD
|
|
? {
|
|
Authorization: `Basic ${Buffer.from(`${Flag.OPENCODE_SERVER_USERNAME ?? "opencode"}:${Flag.OPENCODE_SERVER_PASSWORD}`).toString("base64")}`,
|
|
}
|
|
: undefined,
|
|
fetch: async (...args) => Server.Default().fetch(...args),
|
|
})
|
|
const cfg = yield* config.get()
|
|
const input: PluginInput = {
|
|
client,
|
|
project: ctx.project,
|
|
worktree: ctx.worktree,
|
|
directory: ctx.directory,
|
|
get serverUrl(): URL {
|
|
return Server.url ?? new URL("http://localhost:4096")
|
|
},
|
|
$: Bun.$,
|
|
}
|
|
|
|
for (const plugin of INTERNAL_PLUGINS) {
|
|
log.info("loading internal plugin", { name: plugin.name })
|
|
const init = yield* Effect.tryPromise({
|
|
try: () => plugin(input),
|
|
catch: (err) => {
|
|
log.error("failed to load internal plugin", { name: plugin.name, error: err })
|
|
},
|
|
}).pipe(Effect.option)
|
|
if (init._tag === "Some") hooks.push(init.value)
|
|
}
|
|
|
|
const plugins = cfg.plugin ?? []
|
|
if (plugins.length) yield* config.waitForDependencies()
|
|
|
|
for (let pluginPath of plugins) {
|
|
if (DEPRECATED_PLUGIN_PACKAGES.some((pkg) => pluginPath.includes(pkg))) continue
|
|
log.info("loading plugin", { path: pluginPath })
|
|
if (!pluginPath.startsWith("file://")) {
|
|
const idx = pluginPath.lastIndexOf("@")
|
|
const pkg = idx > 0 ? pluginPath.substring(0, idx) : pluginPath
|
|
const version = idx > 0 ? pluginPath.substring(idx + 1) : "latest"
|
|
const installed = yield* Effect.tryPromise({
|
|
try: () => BunProc.install(pkg, version),
|
|
catch: (err) => {
|
|
const cause = err instanceof Error ? err.cause : err
|
|
const detail = cause instanceof Error ? cause.message : String(cause ?? err)
|
|
log.error("failed to install plugin", { pkg, version, error: detail })
|
|
return detail
|
|
},
|
|
}).pipe(Effect.catch((detail) =>
|
|
bus.publish(Session.Event.Error, {
|
|
error: new NamedError.Unknown({
|
|
message: `Failed to install plugin ${pkg}@${version}: ${detail}`,
|
|
}).toObject(),
|
|
}).pipe(Effect.as("")),
|
|
))
|
|
if (!installed) continue
|
|
pluginPath = installed
|
|
}
|
|
|
|
// Prevent duplicate initialization when plugins export the same function
|
|
// as both a named export and default export (e.g., `export const X` and `export default X`).
|
|
// Object.entries(mod) would return both entries pointing to the same function reference.
|
|
yield* Effect.tryPromise({
|
|
try: async () => {
|
|
const mod = await import(pluginPath)
|
|
const seen = new Set<PluginInstance>()
|
|
for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
|
|
if (seen.has(fn)) continue
|
|
seen.add(fn)
|
|
hooks.push(await fn(input))
|
|
}
|
|
},
|
|
catch: (err) => {
|
|
const message = err instanceof Error ? err.message : String(err)
|
|
log.error("failed to load plugin", { path: pluginPath, error: message })
|
|
return message
|
|
},
|
|
}).pipe(Effect.catch((message) =>
|
|
bus.publish(Session.Event.Error, {
|
|
error: new NamedError.Unknown({
|
|
message: `Failed to load plugin ${pluginPath}: ${message}`,
|
|
}).toObject(),
|
|
}),
|
|
))
|
|
}
|
|
|
|
// Notify plugins of current config
|
|
for (const hook of hooks) {
|
|
yield* Effect.tryPromise({
|
|
try: () => Promise.resolve((hook as any).config?.(cfg)),
|
|
catch: (err) => {
|
|
log.error("plugin config hook failed", { error: err })
|
|
},
|
|
}).pipe(Effect.ignore)
|
|
}
|
|
|
|
// Subscribe to bus events, fiber interrupted when scope closes
|
|
yield* bus.subscribeAll().pipe(
|
|
Stream.runForEach((input) =>
|
|
Effect.sync(() => {
|
|
for (const hook of hooks) {
|
|
hook["event"]?.({ event: input as any })
|
|
}
|
|
}),
|
|
),
|
|
Effect.forkScoped,
|
|
)
|
|
|
|
return { hooks }
|
|
}),
|
|
)
|
|
|
|
const trigger = Effect.fn("Plugin.trigger")(function* <
|
|
Name extends TriggerName,
|
|
Input = Parameters<Required<Hooks>[Name]>[0],
|
|
Output = Parameters<Required<Hooks>[Name]>[1],
|
|
>(name: Name, input: Input, output: Output) {
|
|
if (!name) return output
|
|
const state = yield* InstanceState.get(cache)
|
|
for (const hook of state.hooks) {
|
|
const fn = hook[name] as any
|
|
if (!fn) continue
|
|
yield* Effect.promise(() => fn(input, output))
|
|
}
|
|
return output
|
|
})
|
|
|
|
const list = Effect.fn("Plugin.list")(function* () {
|
|
const state = yield* InstanceState.get(cache)
|
|
return state.hooks
|
|
})
|
|
|
|
const init = Effect.fn("Plugin.init")(function* () {
|
|
yield* InstanceState.get(cache)
|
|
})
|
|
|
|
return Service.of({ trigger, list, init })
|
|
}),
|
|
)
|
|
|
|
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer), Layer.provide(Config.defaultLayer))
|
|
const { runPromise } = makeRuntime(Service, defaultLayer)
|
|
|
|
export async function trigger<
|
|
Name extends TriggerName,
|
|
Input = Parameters<Required<Hooks>[Name]>[0],
|
|
Output = Parameters<Required<Hooks>[Name]>[1],
|
|
>(name: Name, input: Input, output: Output): Promise<Output> {
|
|
return runPromise((svc) => svc.trigger(name, input, output))
|
|
}
|
|
|
|
export async function list(): Promise<Hooks[]> {
|
|
return runPromise((svc) => svc.list())
|
|
}
|
|
|
|
export async function init() {
|
|
return runPromise((svc) => svc.init())
|
|
}
|
|
}
|