Skip to content

feat: OpenCode v2 plugin entrypoint (dual v1/v2 export) - #5

Open
tleclaire wants to merge 2 commits into
tickernelz:mainfrom
tleclaire:feat/opencode-v2-entrypoint
Open

tleclaire wants to merge 2 commits into
tickernelz:mainfrom
tleclaire:feat/opencode-v2-entrypoint

Conversation

@tleclaire

Copy link
Copy Markdown

OpenCode v2 replaces the plugin API, so v1 plugin implementations stop working. This adds a v2 entrypoint next to the existing one instead of replacing it: the default export carries both, and the shared implementation lives in one place.

What changed

  • src/memoryTool.ts (new) — the API-free core: tool description, JSON schema, runtime wiring and the six handlers, moved out of index.ts unchanged.
  • src/v2.ts (new) — the v2 glue via @opencode/plugin:
    • ctx.tool.transform(e => e.add({ name, description, input, execute })) registers the memory tool (plain JSON Schema instead of the v1 tool.schema args)
    • ctx.session.hook("context", …) injects MEMORY/IDENTITY/USER plus the instructions
    • ctx.tool.hook("execute.after", …) keeps the cache invalidation and the daily-log reminder
    • ctx.event.subscribe({ signal }) tracks the session lifecycle; cleanup is returned from setup()
  • src/index.ts — now the slim v1 entrypoint: export default { ...V2Plugin, ...{ id: "memory-md", server: MemoryPlugin } }.
  • @opencode-ai/plugin → ^1.18.30 (the object entrypoint and its PluginModule type exist from 1.18.29), @opencode/plugin ^2.0.9 as a devDependency (types only — the v2 runtime is never imported in a v1 process), version → 1.3.0.

Hook mapping

v1 v2
tool: { memory: tool({ … }) } ctx.tool.transform(e => e.add({ … }))
"tool.execute.after" ctx.tool.hook("execute.after", …)
"experimental.chat.system.transform" ctx.session.hook("context", …)
event ctx.event.subscribe(…)
ctx.client.tui.showToast not part of the plugin context in v2 (the tui is a separate package) — logs to stderr instead

Verified

  • bun run format:check, bun run typecheck and bun run build all pass — compiled against the real @opencode/plugin@2.0.9 types.
  • The v1 leg runs in production on opencode 1.18.31 from exactly this code: the memory tool is registered and read/write/search/list work.
  • The v2 leg is type-checked and structurally tested, but not executed — there is no 2.0.x runtime available here to load it. The tui difference above is a deliberate, documented deviation.

One caveat worth deciding on

The object entrypoint ({ id, server }) is only understood from opencode 1.18.29 onwards; on older versions the default export gets called as a function and would fail. If the v1 path should keep working on older releases, the v2 entrypoint can live behind a subpath export (@zhafron/opencode-memory-md/v2) instead — happy to rework it that way.

The LICENSE file is a separate commit and easy to drop if you would rather not add it.

OpenCode v2 replaces the plugin API, so the default export now carries both
entrypoints: v1 keeps calling server(), v2 calls setup().

- src/memoryTool.ts: shared, API-free core (tool description, JSON schema,
  runtime, the six handlers) used by both entrypoints
- src/v2.ts: v2 glue via @opencode/plugin — ctx.tool.transform() registers
  the memory tool, ctx.session.hook("context") injects the memory context,
  ctx.tool.hook("execute.after") handles cache invalidation and the
  daily-log reminder, ctx.event.subscribe() tracks the session lifecycle
- @opencode-ai/plugin raised to ^1.18.30: the object entrypoint
  ({ id, server }) and its PluginModule type exist from 1.18.29
- @opencode/plugin is a devDependency (types only, no runtime coupling)

V1 behaviour is unchanged; both entrypoints share one implementation.
- README: v2 section with the v1 -> v2 hook mapping and the >= 1.18.29 note
- LICENSE: MIT text, matching the license already declared in package.json
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