fix: align OpenCode plugin with @opencode-ai/plugin SDK v1.2.x#1376
Open
runzexia wants to merge 1 commit intothedotmack:feature/npxfrom
Open
fix: align OpenCode plugin with @opencode-ai/plugin SDK v1.2.x#1376runzexia wants to merge 1 commit intothedotmack:feature/npxfrom
runzexia wants to merge 1 commit intothedotmack:feature/npxfrom
Conversation
- Fix hook API: use flat string keys ("tool.execute.after", "chat.message")
instead of nested objects that silently fail in the current SDK
- Fix event handler: use SDK Event signature event({ event }) instead of
string-based (eventName, payload) that never receives dispatched events
- Fix tool definition: use Zod schema via tool() helper instead of raw
JSON schema that crashes OpenCode on startup
- Add session.idle handler for session completion (workaround: OpenCode
has no session.completed event; session.idle is the closest equivalent)
- Add chat.message hook for richer assistant response capture
- Add @opencode-ai/plugin as external in esbuild config
- Use proper SDK types (Plugin, PluginInput, Hooks) instead of manual
interface declarations
- Fix search endpoint to /api/search (MCP format) with legacy fallback
Tested against @opencode-ai/plugin@1.2.27 with full session lifecycle:
session.created → tool.execute.after → chat.message → session.idle → complete
Author
|
Hey @thedotmack — this is the rewritten plugin code I mentioned in #1258 (issuecomment-4024344296). Quick context on the approach: The
All three issues cause the plugin to load without errors but capture zero data — which makes debugging tricky. Two workarounds worth noting:
Happy to iterate on this. The full test results are in the PR description. |
|
+1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rewrites the OpenCode plugin (
src/integrations/opencode-plugin/index.ts) to align with the actual@opencode-ai/pluginSDK v1.2.x API. The existing implementation uses API patterns that silently fail or crash — hooks never fire, events are never received, and the search tool crashes OpenCode on startup.All fixes have been tested end-to-end against
@opencode-ai/plugin@1.2.27with a live OpenCode session, confirming the full pipeline:session.created → tool.execute.after → chat.message → session.idle → session.complete.Problems Fixed
1. Hook API Mismatch (BREAKING — hooks silently fail)
The SDK expects flat string keys on the returned Hooks object:
Impact: No tool executions were being captured. Zero observations generated from tool use.
2. Event Handler Signature (BREAKING — events never received)
The SDK dispatches events as
event({ event: Event })where Event has.typeand.properties:Impact: No session lifecycle events were received. Sessions never initialized, never completed.
3. Tool Definition Crash (BREAKING — crashes OpenCode on startup)
The SDK requires Zod schemas via the
tool()helper:Impact: OpenCode crashes during plugin initialization with a Zod validation error.
4. Missing Session Completion (sessions never clean up)
WORKAROUND: OpenCode does not emit a
session.completedevent. Addedsession.idlehandler as the closest equivalent — it fires when a conversation turn finishes. This may fire multiple times per session, but the worker handles duplicate completion calls gracefully (idempotent).5. Search Endpoint (wrong path)
Changed from
/api/search/observationsto/api/search— the worker's actual MCP-format endpoint. Added support for both MCP response format ({ content: [{ type: 'text', text }] }) and legacy format ({ items: [...] }).Changes
src/integrations/opencode-plugin/index.tsPlugin,PluginInput,Hooks)(eventName, payload)→({ event })tool()helpersession.idlehandler for session completionchat.messagehook for assistant response capturetruncate()helper to reduce duplicationworkerPost()(only fire-and-forget is needed)scripts/build-hooks.js@opencode-ai/plugin,@opencode-ai/plugin/tool, andzodto esbuild externals (provided by OpenCode runtime)Workarounds & Notes
session.idleas session completion — OpenCode SDK does not expose asession.completedevent.session.idlefires when a conversation turn finishes, which is the closest available signal. Duplicate calls are safe (worker is idempotent).file.editedhandler skipped — This event doesn't carry asessionIDin its properties, making it impossible to associate with a content session. File edits are already captured via thetool.execute.afterhook when the agent uses file editing tools.message.updatedperformance note — Assistant message events (message.updatedwhere role=assistant) can generate high-volume, low-signal observations. In heavy usage, these may account for ~85% of queued messages while producing near-zero useful observations. Thechat.messagehook captures the same content more efficiently. Consider disabling themessage.updatedhandler if queue congestion is observed.claude_mem_searchvs MCP server — The plugin's built-in search tool provides basic search capability for standalone usage. Users who additionally configure the claude-mem MCP server in theiropencode.jsonget access to a richer toolset (search, timeline, observations, smart code search). Both approaches can coexist, but the MCP server provides a superior experience.Test Results
Tested with:
@opencode-ai/plugin@1.2.27@opencode-ai/sdk@1.2.27