Skip to content

Add OpenCode V2 adapter (v2/) on acp-kernel - #4

Closed
rorshopping wants to merge 1 commit into
ranxianglei:masterfrom
rorshopping:main
Closed

Add OpenCode V2 adapter (v2/) on acp-kernel#4
rorshopping wants to merge 1 commit into
ranxianglei:masterfrom
rorshopping:main

Conversation

@rorshopping

Copy link
Copy Markdown
Collaborator

Summary

Adds the OpenCode V2 (opencode2) adapter under v2/ — the V2 counterpart to this repo's V1 adapter, built on the same acp-kernel core (MIT, unmodified).

This follows the direction discussed in ranxianglei/opencode-acp#287: reuse acp-kernel + the existing V1 adapter glue, and write only the V2 hook layer.

What's in v2/

  • Same bili_* tools: bili_compress, bili_decompress, bili_search, bili_status — so V1 and V2 can run side-by-side without a namespace clash.
  • V2 plugin API only: ctx.session.hook("context") + ctx.tool.transform (@opencode-ai/plugin). V1 experimental.chat.* hooks are not used.
  • acp-kernel left unmodified, inlined via noExternal: ["acp-kernel"]v2/dist/index.js is a self-contained, zero-runtime-dependency ESM bundle (MIT).
  • Reuses the same message-shape conversion, per-session AcpRuntime + lock, and token estimation from the V1 adapter.
  • Includes src/ (TypeScript), dist/, tests/, tsup/tsconfig config, README, smoke.mjs.

Verification

  • npm run build (tsup) passes; npm run typecheck passes.
  • Confirmed loading on opencode2 and the four bili_* tools register.
  • The root README now points to v2/.

Notes / open question

experimental.text.complete has no direct V2 equivalent, but the current adapter doesn't use it, so it's not blocking (as noted in the issue). Happy to adjust the layout (e.g., separate package instead of a v2/ subdir) if you'd prefer.

@rorshopping

Copy link
Copy Markdown
Collaborator Author

Hi — we were building the V2 adapter in parallel on a v2 branch (same approach: ctx.session.hook("context") + tool.transform, same four bili_* tools, reusing the V1 glue). Now that you've restructured into a monorepo, we'll align with packages/v2 instead of pushing our branch. One finding is worth sharing before it bites.

Pairing bug with a non-compress tool name. acp-kernel hardcodes the literal 'compress' in all its ACP-metadata logic: hideConsumedCompressCalls, stripOrphanedToolCalls (prune), adjustBoundariesForToolPairs (tool-pairs), collectCompressInvocations (rebuild), and the protected-tools filter. The moment the adapter registers the tool under another name (bili_compress), the kernel stops recognizing the adapter's own compression invocations:

  • consumed bili_compress calls are not hidden — the call and its result leak back into the visible context after the block is created;
  • tool-pair boundary adjustment and orphan-stripping skip them too.

On the opencode v1 adapter this surfaced as invalid_request_error: assistant tool calls must be followed by matching tool results. In V2 the adapter's own pairing mask hides the crash, but the leak is the same.

Fix is PR-ready on our fork rorshopping/acp-kernel (9666436): Config.compressToolName (default 'compress', fully backwards compatible) threaded through all five spots, plus protectedCompressTools() which unions the configured name with the legacy literal. There's also a build fix (ec9b85d): the clean script was POSIX-only (rm -rf dist) and there was no prepare hook, so git/npm installs produced no dist/.

Regression test: our smoke.mjs now has a pairing test (separate session, a real bili_compress call+result through the pipeline) that fails with consumed compress call hidden when the name isn't wired, and passes when it is.

Happy to adapt all of this onto master: kernel PR to ranxianglei/acp-kernel, then compressToolName wiring + the regression test in packages/v1 and packages/v2. The parallel branch is still at billion-context-opencode#v2 (tag v2.0.0) if you want to compare. Say the word.

@ranxianglei

Copy link
Copy Markdown
Owner

Closing as superseded. This is the earlier root-level v2/ layout (pre-monorepo); it was already superseded by #8, and now by #18 (merged) — which ports the V2 tool-registration and system-prompt fixes into the project's canonical dual-shape single package (packages/billion-context-opencode/src/index.ts setupV2), per AGENTS.md §2.3.

See the detailed explanation on #8. Thanks @rorshopping for the original V2 adapter work. 🙇

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