All Agent Engineering

Migrate OpenCode V1 plugin event subscriptions and cleanup to V2

Port V1 event callbacks and self-owned resources to V2 setup, subscriptions and cleanup, with a compatibility acceptance table.

AINavWritten / Updated
On this page

At a glance

A V1 plugin is a function that returns an object of hooks; a V2 plugin is a default-exported definition with a stable id. The documentation says V1 implementations do not run in V2, so moving a file or renaming a config key does not finish the migration. This guide is for an existing V1 plugin that listens to events and manages resources. It produces three things: a V2 entry, a migration plan for event subscription and cleanup, and a compatibility acceptance table. It does not repeat project configuration, command approval or tool registration.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • OpenCode Migrate OpenCode V1 plugin event subscriptions and cleanup to V2

Execution locations and responsibilities

  • OpenCode configuration loader and runtime Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Inventory the V1 callbacks, state and resources → 2. Rewrite the config entry and local plugin location → 3. Replace the entry and fix a stable id → 4. Migrate the event subscription → 5. Migrate the cleanup function → 6. Run the compatibility acceptance. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Inventory the V1 callbacks, state and resources2. Rewrite the config entry and local plugin location3. Replace the entry and fix a stable id4. Migrate the event subscription5. Migrate the cleanup function6. Run the compatibility acceptance

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • The config key changes from plugin to plugins, and tuples become objects with package and options. The entry becomes a default export of Plugin.define with a stable id, and initialization moves into setup(ctx).Change the entry import from @opencode-ai/plugin to @opencode/plugin and select a published dependency version compatible with the target OpenCode release.

When not to use

  • V1 implementations do not run in V2; configuration may be normalized automatically, but the plugin implementation code must be ported by hand.
  • ctx.location is only where the plugin instance loaded, not where every session or event occurs, so session-related logic must read event or session data.
  • The material gives only session.idle as an event-type example and no complete event catalog; other event types, behavior when a subscription errors, and whether the cleanup function may be asynchronous are not judged here.
  • Single-package dual entrypoints apply to V1 1.18.29 and newer without translating hooks. Earlier releases require separate package versions or entrypoints AND testing the oldest claimed release. Remove V1 after its support window.

Prerequisites and input

  • You hold the V1 plugin source and its local configuration, and can list the events it listens to, the options it reads, the state it saves, and the timers, subprocesses or connections it creates itself.
  • You can edit the configuration and plugin directory and reload or remove the plugin to observe cleanup; for a published package, you also know which OpenCode versions you intend to support.

Engineering checks

Entry and configuration — The config key changes from plugin to plugins, and tuples become objects with package and options. The entry becomes a default export of Plugin.define with a stable id, and initialization moves into setup(ctx).Change the entry import from @opencode-ai/plugin to @opencode/plugin and select a published dependency version compatible with the target OpenCode release.

Subscription and state — The V1 returned event hook maps to ctx.event.subscribe(), an async subscription to the public server event stream. Options come from ctx.options, and plugin-private durable state uses ctx.storage instead of a self-made global file.

Cleanup and compatibility boundary — The V1 returned dispose maps to the cleanup function returned by setup, used for timers, subprocesses, sockets and other resources the plugin owns. Hook and transform registrations are cleaned up when the plugin unloads. Supporting V1 from the same package needs a separate server(), and the two APIs do not translate into each other.

1. Inventory the V1 callbacks, state and resources

Where: the V1 plugin source and local configuration. Input: event callbacks, dispose logic, option reads, global files or variables. Action: build a list with one row per item, giving V1 source, V2 destination and whether the plugin owns a resource. Mark uses of directory and project as ctx.location, and rewrite client uses as domain methods. Completion check: every callback and resource has a row, and no row says copy as-is. Stop condition: if a V1 hook has no counterpart in the documented table, such as some experimental hooks, stop and re-evaluate the need against the V2 session, provider, model and event APIs instead of keeping an old internal lifecycle assumption.

2. Rewrite the config entry and local plugin location

Where: the OpenCode configuration loader. Input: the V1 plugin array and its tuples. Action: rename the key to plugins and turn tuples with options into objects containing package and options. Place local plugins under .opencode/plugins/; a configured local path must name a directory, so move the implementation to index.ts inside it and keep supporting modules beside it. Completion check: no configured path points at a single .ts file. Stop condition: if the log shows a warning that the path was skipped, fix the path before continuing.

3. Replace the entry and fix a stable id

Where: the plugin entry file. Input: the exported V1 plugin function. Action: switch to a default export of Plugin.define, write an id that will not be renamed, and move the old initialization into setup(ctx). Plugin storage is scoped by this id and status and diagnostics identify the plugin by it, so changing the id means a different storage space. Completion check: the id is unique among your plugins and every initialization item from the inventory has landed in setup(ctx). Stop condition: do not write any persistent state until the id is settled.Replace V1 import type { Plugin } from "@opencode-ai/plugin" with V2 import { Plugin } from "@opencode/plugin" and check that the published dependency is compatible with the target release.

4. Migrate the event subscription

Where: inside setup(ctx). Input: the V1 event callback and its branching by event type. Action: create a controller with AbortController, then in an async task that does not block setup, iterate ctx.event.subscribe() with for await, passing the controller's signal. Rebuild the old branches on event.type, for example session.idle as in the documentation example. If logic depends on a session's location, read it from the event or session data rather than ctx.location, which only describes where the plugin instance loaded. Completion check: every branch of the old callback has a counterpart. Stop condition: if the event type a branch relies on cannot be confirmed in the official material, mark it unverified and pause that branch.

5. Migrate the cleanup function

Where: the return value of setup. Input: the resources marked as owned in the step 1 inventory, plus the controller from the previous step. Action: have setup return a function that aborts the subscription controller and closes timers, subprocesses, sockets and similar resources. The Bun shell helper is not provided in V2, so import and manage whichever process API you need. Ordinary hooks and transforms need no manual cleanup; keep a registration's return value and call dispose only to remove one early. Completion check: every owned resource has a matching release action in the cleanup function. Stop condition: if a resource cannot be released there, record why and do not count it as migrated.

6. Run the compatibility acceptance

Where: the target OpenCode, package and local acceptance sheet. Input: migration inventory and supported versions. For each checklist row record input, expected result, actual observation and passed/unverified status. Confirm ID/source, exercise all registered hooks, transforms, tools, commands and subscriptions, verify reload/removal cleanup, test options/state in a clean project and test an installed published package rather than only a workspace link. For V2-only support retain no V1 implementation. A single-package dual implementation applies to V1>=1.18.29 with separate server() and setup(). Older V1 needs separate package versions or entrypoints AND testing of the oldest claimed release; remove V1 after its support window. Completion: every row has an observation or explicit unverified status, and release requires all necessary rows to pass. Stop failures at the owning step; unexecuted checks never pass.

Task example

This is an original hypothetical walkthrough and was not run. Suppose a V1 plugin has an event callback that prints one log line on session.idle, a periodic timer, and an option named strict. The inventory gives three rows: the callback goes to ctx.event.subscribe(), strict goes to ctx.options, and the timer is an owned resource to be closed in the cleanup function. The config changes plugin to plugins and the local path becomes a directory. The entry uses Plugin.define with a stable id. Expected observations (hypothetical, not actual results): the plugin list shows that id; one log line appears when a session goes idle; after a reload the old subscription no longer logs and the timer no longer fires. If duplicate logs persist after a reload, a subscription or timer was not released, and you return to the cleanup step.

Completion checks

  • The active plugin list shows the migrated id and source, the local path is a directory rather than a single file, and the log has no skipped-path warning.
  • Every event branch was triggered once, and the log or side effect matches the V1 inventory's expectation; expectation and observation are recorded separately.
  • After reloading or removing the plugin, subscriptions, timers, subprocesses and connections produce no further activity and no duplicate callbacks.
  • Options and persisted state were exercised in a clean project, and state keys belong to the current id.
  • Compatibility acceptance rows: V2 entry/import/dependency; independent server for same-package V1>=1.18.29; separate packages/entries and oldest-release test for earlier V1; installed published package. Each records target version, expectation, observation and passed/unverified status, with reasons for not-applicable rows.

Verification scope

Only the official migration documentation was read. OpenCode was not installed, no login was made, no API was executed, and no actual output was observed. The example and expected observations are hypothetical deductions from the documentation, not verification results.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. OpenCode · OpenCode documentation: opencode-plugin-migration

Found an error? Tell us →