Repository navigation
feat: support OpenCode V2 alongside V1 (and report the prompt-cache hit rate) - #1
Merged
Merged
Conversation
Move event translation out of the controller so it no longer imports the V1 plugin API. The core now takes normalized MeterEvents and returns whether state changed; src/v1.ts owns the V1 message.*/session.deleted wiring and the TUI layer keeps only rendering.
Map the V2 session.* event stream onto the shared meter core: step.started opens a turn (TTFT start), text/reasoning deltas feed the rate, tool.called starts a new segment, step.ended/failed accumulate exact provider tokens, and execution.succeeded/failed/interrupted settle the turn with the summed total.
Ship a dual-shape default export so one package serves both generations: V1 reads 'tui', V2 reads 'id' + 'setup' and renders into the prompt.footer.status slot with the resolved V2 theme. Add a no-op server entry so the V2 server never loads OpenTUI, and rename the entrypoint to src/tui.tsx. Bump to 0.3.0 with @opencode/plugin and OpenTUI dev dependencies, and update the packed-package smoke test for the new entrypoints (also fixing its Windows invocations).
Split the install section by generation (V1 tui.json tuple options vs V2 plugins object options), add the V2 agent procedure, and describe both event mappings in the how-it-works section.
TUI plugin options are read from cli.json, not opencode.json; options in opencode.json go to server plugins and never reach ctx.options. Document the real install path and drop the incorrect no-hot-reload claim.
Compaction streams a summary and reports its own tokens through session.compaction.*, which emits none of the step/execution events the adapter listened for, so the whole generation went uncounted. Map the compaction lifecycle to a turn and guard execution completions that land mid-compaction, so the graph is not rescaled twice.
Providers report cache.read / cache.write alongside the output tokens, and both adapters discarded them, so a cached prompt was invisible in the meter. Read the counters in both adapters, accumulate them across a V2 execution's steps, and surface `cache <n>%` on the frozen frame. The share is read / (input + read + write). It is omitted when the provider reports no prompt tokens, and clears once the next turn starts generating.
Later steps in one execution keep the same meter. Compaction drops the previous execution's provider totals. The server entry exports server() so the V1 installer can load it, and npm ci accepts the existing peer set.
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
Adds OpenCode V2 support alongside the existing V1 support, from the same package.
V2's TUI plugin API differs enough from V1 that the current build cannot load at all — the failure is real and observable:
This PR ports the plugin so one package works on both generations, and also adds a prompt-cache hit rate.
What changed
Structure
Measurement logic is split from the transport so it can serve two APIs:
src/meter.ts— pure measurement.src/controller.ts— platform-agnostic core over a normalized event union (turn/delta/segment/done/drop).src/v1.ts— thin V1 adapter (message.updated,message.part.delta,message.part.updated,session.deleted).src/v2.ts— thin V2 adapter over thesession.*event bus.src/tui.tsx(wassrc/index.tsx) — one dual-shape default export:{ id, setup(ctx) }for V2 and{ tui(api, options) }for V1.src/server.ts— no-op V2 server entry, required so V2 can resolve the package.V2 renders into
prompt.footer.status; V1 keepssession_prompt_right. Both share one<MeterLine>component; only the palette source differs (ctx.theme.*vsapi.theme.current.*).V2 event mapping
session.step.startedturn(incl. TTFT start)session.text.delta/session.reasoning.deltadeltasession.tool.calledsegment(graph stays continuous across the pause)session.step.ended/.failedsession.execution.succeeded/.failed/.interrupteddonesession.compaction.startedturn(compaction is its own generation)session.compaction.deltadeltasession.compaction.ended/.faileddonewith the reported tokenssession.deleteddropCompaction
Compaction streams a summary and reports its own token usage, but emits none of the step/execution events, so it would otherwise be invisible to the meter. It is now counted as its own generation.
A
compactingguard skipsexecution.*completions that land mid-compaction; otherwise both sides would callsettleandrescaleBucketswould be applied twice, distorting the VU graph.Prompt-cache hit rate
Providers report
cache.read/cache.writealongside the output tokens, which the adapters previously discarded. The counters are now read in both adapters and accumulated across a V2 execution's steps, and acache <n>%segment is appended on settle:The share is
read / (input + read + write). It is omitted when the provider reports no prompt tokens (rather than showing a misleading0%), and it clears once the next turn starts generating.inputandcacheare read defensively — some providers omit them entirely. NewshowCacheoption, defaulttrue.Configuration
V2 options are read from the CLI config (
~/.config/opencode/cli.json), notopencode.json:Options in
opencode.jsonare delivered to server plugins and never reachctx.options. Keep the entry in only one of the two files. V1 keeps itstui.jsontuple form.Verification
npm test— 114 tests, 0 fail (12 suites)npm run typecheck— cleannpm run smoke— OKstage=load entrypoint=.../src/tui.tsx→stage=setup plugin=opencode-token-metrics, zero errors. A deliberately-throwing plugin in the same run confirms the log distinguishes success from failure.cli.jsonoptions reachctx.options.Notes
session.compaction.startedcarries no timestamp in the V2 schema, so its arrival time is used as the TTFT start. TTFT is therefore measured from event arrival, not model start.tui.jsonbehavior is untouched.