Skip to content

fix(v2): register pty tools and slash commands via editor add() - #61

Open
lenucksi wants to merge 2 commits into
shekohex:mainfrom
lenucksi:fix/v2-tool-command-registration
Open

lenucksi wants to merge 2 commits into
shekohex:mainfrom
lenucksi:fix/v2-tool-command-registration

Conversation

@lenucksi

@lenucksi lenucksi commented Sep 18, 2026

Copy link
Copy Markdown

Problem

The V2 entry point (src/v2/index.ts) registers neither tools nor slash commands with OpenCode — silently, without any error:

  1. ToolsptyTools (src/v2/tools.ts) is exported but never passed to ctx.tool.transform, so pty_spawn/pty_write/pty_read/pty_list/pty_kill never reach OpenCode. Issue Plan OpenCode V2 plugin API support #55 explicitly listed "Register PTY tools through the V2 tool transform/registration surface" as part of the port.
  2. CommandsregisterV2Commands calls draft.update(name, cb), but OpenCode v2's CommandEditor only exposes add(definition). The guard typeof draft.update === 'function' therefore always short-circuits, and pty-open-background-spy / pty-show-server-url never appear in the / menu.

The existing tests did not catch this because they mocked a draft that happened to implement update, and only asserted module-level existence of ptyTools.

Fix

  • src/v2/tools.ts: add registerV2Tools(draft), adapting the V1 tool definitions ({ description, args, execute }) to V2 Tool.Info ({ name, input, description, execute }). The args Zod shape is converted to a JSON Schema via Zod v4's toJSONSchema, matching the plugins documentation.
  • src/v2/commands.ts: registerV2Commands now uses draft.add({ name, description, execute }).
  • src/v2/index.ts: setup calls ctx.tool.transform(registerV2Tools) in addition to the command transform.
  • src/v2/types.ts: remove the non-existent update draft method; add CommandDefinition, ToolInfoV2 and ToolDraft.
  • Tests now assert real registration by mocking add() (which is what OpenCode actually provides), for both tools and commands.

Verification

  • bun run typecheck — clean
  • bun test test/v2.test.ts test/opencode-v2-live.test.ts — 11 pass / 0 fail
  • bun run lint — clean
  • Manually verified against OpenCode v2.0.8 (@opencode-ai/plugin@0.0.0-beta-19271): all five pty_* tools are visible to the agent, both slash commands appear in the / menu, and /pty-open-background-spy starts the web UI with live sessions.

Refs #55.

The V2 setup registered neither tools nor commands with OpenCode:

- Tools: `ptyTools` was exported but never passed to `ctx.tool.transform`,
  so no `pty_*` tool ever reached OpenCode. Issue shekohex#55 explicitly asked to
  "register PTY tools through the V2 tool transform/registration surface".
- Commands: `registerV2Commands` called `draft.update(name, cb)`, but
  OpenCode v2's CommandEditor exposes `add(definition)` only. The guard
  `typeof draft.update === 'function'` therefore always short-circuited and
  the slash commands never appeared in the "/" menu.

Adapts the V1 tool definitions ({ description, args, execute }) to V2
Tool.Info ({ name, input, description, execute }), registers them through
ctx.tool.transform, and switches commands to draft.add() with an execute
handler. Tests now assert real registration (mocking add()) instead of
mere module-level existence.
opencode v2's Tool.Info expects a JSON Schema for `input` (see the
plugins docs), not a Zod schema. Convert the V1 Zod args via Zod v4's
`toJSONSchema` instead of passing the Zod object through.
@shekohex

Copy link
Copy Markdown
Owner

CI needs fixing, overall looks good.

Would be nice if you can post a small video of it.

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.

2 participants