(
id: number,
params: { pluginPath: string; context: PluginContext },
)
| 396 | // --------------------------------------------------------------------------- |
| 397 | |
| 398 | async function handleInitialize( |
| 399 | id: number, |
| 400 | params: { pluginPath: string; context: PluginContext }, |
| 401 | ): Promise<void> { |
| 402 | try { |
| 403 | const mod = await import(params.pluginPath); |
| 404 | const pluginFn = mod.default ?? mod; |
| 405 | |
| 406 | if (typeof pluginFn !== "function") { |
| 407 | sendError(id, -32600, "Plugin module does not export a function"); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // Build a PluginInput compatible with upstream @opencode-ai/plugin shape, |
| 412 | // while keeping `context` for backward compatibility. |
| 413 | const client = await createPluginClient(params.context, params.pluginPath); |
| 414 | const pluginInput = buildPluginInput(params.context, client); |
| 415 | |
| 416 | const hooks: Hooks = await pluginFn(pluginInput); |
| 417 | pluginHooks = hooks; |
| 418 | |
| 419 | // Collect hook names |
| 420 | const hookNames: string[] = []; |
| 421 | for (const key of Object.keys(hooks)) { |
| 422 | if (key === "auth" || key === "event" || key === "config") continue; |
| 423 | if (typeof hooks[key] === "function") { |
| 424 | hookNames.push(key); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Extract auth metadata if present |
| 429 | let authMeta: { provider: string; methods: AuthMethod[] } | undefined; |
| 430 | if (hooks.auth && typeof hooks.auth === "object") { |
| 431 | authHook = hooks.auth as AuthHook; |
| 432 | authMeta = { |
| 433 | provider: authHook.provider, |
| 434 | methods: authHook.methods.map((m) => ({ |
| 435 | type: m.type, |
| 436 | label: m.label, |
| 437 | })), |
| 438 | }; |
| 439 | } |
| 440 | |
| 441 | sendResult(id, { |
| 442 | name: params.pluginPath.split("/").pop()?.replace(/\.[tj]s$/, "") ?? "unknown", |
| 443 | hooks: hookNames, |
| 444 | auth: authMeta, |
| 445 | }); |
| 446 | } catch (err: unknown) { |
| 447 | const msg = err instanceof Error ? err.message : String(err); |
| 448 | sendError(id, -32603, `Failed to initialize plugin: ${msg}`); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | async function handleHookInvoke( |
| 453 | id: number, |
no test coverage detected