Skip to content

feat: support OpenCode 2.x alongside 1.x - #8

Open
wesselOC wants to merge 1 commit into
psinetron:mainfrom
wesselOC:feat/opencode-v2-support
Open

wesselOC wants to merge 1 commit into
psinetron:mainfrom
wesselOC:feat/opencode-v2-support

Conversation

@wesselOC

Copy link
Copy Markdown

Problem

On OpenCode 2.x the plugin fails to load:

Plugin must export a default definition with an id and an effect or setup function.
(cause: SchemaError(Expected object at ["default"]))

2.x only loads plugins whose default export is an object with an id and setup()/effect(). This package default-exports a bare 1.x plugin function, so it can't load on 2.x. See the
V1 → V2 plugin migration guide.

Change

One default export now serves both plugin APIs: id + setup(ctx) for 2.x and server() for 1.x (the dual-support shape from the migration guide).

  • core.ts (new): version-agnostic server connection, leader election, reconnects, skin selection and logging
  • index.ts: thin 1.x and 2.x adapters over the core
    • 2.x: ctx.event.subscribe() and ctx.tool.hook("execute.before" | "execute.after"). 2.x events (session.status, session.idle, session.execution.failed/session.step.failed) are mapped to the message shapes the frontend already reads, so visualizer/ is unchanged.
    • 1.x: same hooks as before
  • Connecting to the server now happens in the background rather than blocking plugin startup, and the connection is closed when the plugin is unloaded
  • Connection state is per plugin instance (2.x runs one shared service that can load the plugin more than once)
  • @opencode-ai/plugin is now types-only (devDependency). The 2.x context is typed with a small local interface, so the 2.x SDK isn't needed.
  • Tests (bun test, 15 tests), tsconfig.json, README updated for both config keys. Version bumped to 1.1.0.

Testing

Loaded this branch in the real runtimes (isolated config/data dirs) and confirmed the plugin loads once and connects to the visualizer server:

OpenCode Result
2.0.20 loads, no failed to load plugin, connects
1.19.0, 1.18.29, 1.18.28, 1.17.0, 1.16.0 loads once, connects

I also observed tool.execute.before/after events from a live 2.x session arriving at the frontend. Unit tests cover event mapping, reconnect/dispose and both adapters; I confirmed they fail when a mapping is broken.

Notes

  • 2.x local paths: 2.x requires a local plugin path to be the directory containing package.json. Pointing at index.ts gives "configured plugin path must be a directory". 1.x accepts the file. The README documents this.
  • Not verified: I did not run a full prompt through either version. session.status / session.idle forwarding is covered by unit tests, not an end-to-end run.
  • tui.ts (the /visualizer command) still targets the 1.x TUI API. A 2.x port is a separate change; the README notes this.
  • visualizer/ocv-server.ts has 5 existing type errors under current bun-types. I left it untouched, so there is no typecheck script.
    When you open the PR, check that the base is psinetron/opencode-visualiser:main and the compare branch is wesselOC:feat/opencode-v2-support. If the push is rejected or ssh -T shows an error, paste the output and I'll help sort it out.

OpenCode 2.x only loads plugins whose default export is an object with an
id and setup()/effect(); the previous bare-function default export failed
with 'Plugin must export a default definition...'.

- core.ts: version-agnostic server connection, leader election, skins
- index.ts: one default export serving both APIs (V2 id+setup, V1 server())
- connect in the background and dispose cleanly on unload
- map V2 events (session.status/idle/execution.failed) to the shapes the
  frontend already understands
- tests (bun test), tsconfig, README for both config keys
- @opencode-ai/plugin is now types-only (devDependency)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant