V1 TUI modules no longer load: replace ``{ id, tui }`` with
``Plugin.define({ id, setup })``, register the section on
``sidebar.content``, and expose commands through ``context.keymap.layer``.
Move balance, clock and usage state onto ``context.storage.store/memory``
so host subscribers rerender; plugin-created signals stayed stale in the
V2 renderer. Read V2 assistant messages (``type``,
``model.providerID``/``model.id``) and subscribe through ``context.data``.
Configure the package directory in ``cli.json`` instead of the source
file; document the V2 install. Typecheck, 49 unit tests and the live
GreenPT integration test pass, and the sidebar renders the balance.
1.8 KiB
1.8 KiB
AGENTS.md
Project
opencode-greenpt-usage — an OpenCode 2 TUI plugin (Solid + OpenTUI) that
shows the remaining GreenPT API credit balance in the sidebar. Distributed by
git clone; there is no npm publish.
Commands
npm install # install dev dependencies
npm test # unit tests (vitest)
npm run typecheck # tsc --noEmit
GREENPT_API_KEY=... npm run test:integration # live API test
Conventions
- The plugin entry is
Plugin.define({ id: "greenpt.usage", setup })insrc/index.tsx(default export). Types come from@opencode/plugin/tuiand@opencode/plugin/tui/contextas type-only imports; the host providessolid-jsand@opentui/*at runtime. - The section is registered on the
sidebar.contentslot viacontext.ui.slot({ append: ..., render }), hidden when no GreenPT credential is present (showWhenUnavailableopts into a hint). Commands usecontext.keymap.layer;setupreturns a cleanup that unregisters the slot and disposes the store. - Balance probing must stay free:
HEAD /v1/chat/completionsreturns400plusx-credits-remaining. Never replace it with a billable call. - Usage accounting lives in
src/usage.ts(pure) and is consumed by the balance store: real spend = delta ofx-credits-remainingbetween probes; per-model detail = reconciliation of local OpenCode assistant messages (V2type: "assistant",model.providerID/model.id), idempotent by message id, persisted withcontext.storage.store. - Credential resolution order:
apiKeyoption,GREENPT_API_KEY, active OpenCodeauth.json(greenpt,type: "api"). No profile-switching command may leak into this logic. - No explanatory inline comments; function contract blocks are required.