refactor(plugin): migrate to V1 PluginModule format

Convert the default export from the legacy callable Plugin to the V1 PluginModule shape (`{ id, server }`) documented by opencode's plugin SDK. This aligns oh-my-openagent with the canonical plugin entry format and removes plugin-format legacy debt.

Drop the module-level `let activePluginDispose` cleanup guard: opencode instantiates plugins in a scope-bound Layer per server and dynamic-imports fresh modules on reload, so module-level state is not preserved across reloads. Individual managers already register their own SIGINT/SIGTERM cleanup (skill-mcp-manager, background-agent process-cleanup), so the orphaned createPluginDispose call provided no runtime value.

Remove the non-standard `name` return field that opencode's Hooks interface does not include. The PluginModule's `id` now carries the plugin identity instead.

Drop the unused `lspManager` import that only fed the orphaned createPluginDispose.

Update src/index.test.ts and src/index.telemetry.test.ts to call `plugin.server(ctx)` instead of `plugin(ctx)`, and assert the V1 shape.
This commit is contained in:
YeonGyu-Kim
2026-04-18 01:37:46 +09:00
parent 2892ca4adf
commit 1b10ab36d2
3 changed files with 40 additions and 40 deletions
+21 -31
View File
@@ -1,5 +1,5 @@
import { initConfigContext } from "./cli/config-manager/config-context"
import type { Plugin } from "@opencode-ai/plugin"
import type { Hooks, Plugin, PluginModule } from "@opencode-ai/plugin"
import type { HookName } from "./config"
@@ -9,7 +9,6 @@ import { createRuntimeTmuxConfig, isTmuxIntegrationEnabled } from "./create-runt
import { createTools } from "./create-tools"
import { initializeOpenClaw } from "./openclaw"
import { createPluginInterface } from "./plugin-interface"
import { createPluginDispose, type PluginDispose } from "./plugin-dispose"
import { loadPluginConfig } from "./plugin-config"
import { createModelCacheState } from "./plugin-state"
@@ -17,27 +16,23 @@ import { createFirstMessageVariantGate } from "./shared/first-message-variant"
import { injectServerAuthIntoClient, log, logLegacyPluginStartupWarning } from "./shared"
import { detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./shared/external-plugin-detector"
import { startBackgroundCheck as startTmuxCheck } from "./tools/interactive-bash"
import { lspManager } from "./tools/lsp/client"
import { createPluginPostHog, getPostHogDistinctId } from "./shared/posthog"
let activePluginDispose: PluginDispose | null = null
const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const serverPlugin: Plugin = async (input, _options): Promise<Hooks> => {
initConfigContext("opencode", null)
log("[OhMyOpenCodePlugin] ENTRY - plugin loading", {
directory: ctx.directory,
directory: input.directory,
})
logLegacyPluginStartupWarning()
const skillPluginCheck = detectExternalSkillPlugin(ctx.directory)
const skillPluginCheck = detectExternalSkillPlugin(input.directory)
if (skillPluginCheck.detected && skillPluginCheck.pluginName) {
console.warn(getSkillPluginConflictWarning(skillPluginCheck.pluginName))
}
injectServerAuthIntoClient(ctx.client)
await activePluginDispose?.()
injectServerAuthIntoClient(input.client)
const pluginConfig = loadPluginConfig(ctx.directory, ctx)
const pluginConfig = loadPluginConfig(input.directory, input)
const posthog = createPluginPostHog()
const distinctId = getPostHogDistinctId()
@@ -78,7 +73,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const modelCacheState = createModelCacheState()
const managers = createManagers({
ctx,
ctx: input,
pluginConfig,
tmuxConfig,
modelCacheState,
@@ -86,13 +81,13 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
})
const toolsResult = await createTools({
ctx,
ctx: input,
pluginConfig,
managers,
})
const hooks = createHooks({
ctx,
ctx: input,
pluginConfig,
modelCacheState,
backgroundManager: managers.backgroundManager,
@@ -102,15 +97,8 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
availableSkills: toolsResult.availableSkills,
})
const dispose = createPluginDispose({
backgroundManager: managers.backgroundManager,
skillMcpManager: managers.skillMcpManager,
lspManager,
disposeHooks: hooks.disposeHooks,
})
const pluginInterface = createPluginInterface({
ctx,
ctx: input,
pluginConfig,
firstMessageVariantGate,
managers,
@@ -118,30 +106,32 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
tools: toolsResult.filteredTools,
})
activePluginDispose = dispose
return {
name: "oh-my-openagent",
...pluginInterface,
"experimental.session.compacting": async (
_input: { sessionID: string },
compactingInput: { sessionID: string },
output: { context: string[] },
): Promise<void> => {
await hooks.compactionContextInjector?.capture(_input.sessionID)
await hooks.compactionTodoPreserver?.capture(_input.sessionID)
await hooks.compactionContextInjector?.capture(compactingInput.sessionID)
await hooks.compactionTodoPreserver?.capture(compactingInput.sessionID)
await hooks.claudeCodeHooks?.["experimental.session.compacting"]?.(
_input,
compactingInput,
output,
)
if (hooks.compactionContextInjector) {
output.context.push(hooks.compactionContextInjector.inject(_input.sessionID))
output.context.push(hooks.compactionContextInjector.inject(compactingInput.sessionID))
}
},
}
}
export default OhMyOpenCodePlugin
const pluginModule: PluginModule = {
id: "oh-my-openagent",
server: serverPlugin,
}
export default pluginModule
export type {
OhMyOpenCodeConfig,