new: [opencode-greenpt-usage] port plugin to OpenCode v2

Rewrite the TUI plugin against the OpenCode v2 plugin API
(``@opencode/plugin`` + ``@opencode/plugin/tui``):

- ``src/index.ts``: minimal server-side ``Plugin.define`` entry.
- ``src/tui.tsx``: CLI plugin registered as ``./tui``, appending the
  widget to the ``sidebar.content`` slot through ``context.ui.slot``
  and exposing the refresh/reset commands via ``context.keymap.layer``.
- ``src/store.ts``: durable plugin storage (``context.storage.store``)
  replaces the KV store; ``session.idle``/``session.status`` events and
  the ``client.session`` API drive reconciliation and backfill.
- ``src/widget.tsx``: v2 semantic theme tokens
  (``theme.text.base``/``text.muted``/``text.feedback``).
- Tests updated to a structural fake of the v2 context.

The free ``HEAD /v1/chat/completions`` balance probe and the credential
resolution order are unchanged. The ``order`` option is gone: v2 slot
placement is additive.
This commit is contained in:
Stéphan Sainléger
2026-10-19 17:00:48 +02:00
parent 9c84473266
commit 5129f295da
12 changed files with 4704 additions and 654 deletions

View File

@@ -24,8 +24,8 @@ kimi-k2 3.1k tok ~0.01 €
the API answers `400 Bad Request` before any billing, yet the response
still carries the `x-credits-remaining` header. No credit is consumed
and no generation is triggered.
- **Usage tracking** keeps per-day buckets, persisted in the plugin KV
store (90 days by default):
- **Usage tracking** keeps per-day buckets, persisted in the plugin durable
storage (90 days by default):
- *real credit spend* is observed as the drop of `x-credits-remaining`
between two balance probes — this covers **every client** of the API
key, not just OpenCode, but has no per-model detail;
@@ -47,7 +47,8 @@ looks for a key.
## Requirements
- OpenCode `>= 1.18.0` (TUI plugin system).
- OpenCode `>= 2.0.0` (v2 CLI/TUI plugin system; for OpenCode 1.x use the
`main` branch).
- A GreenPT account and API key.
## Installation
@@ -64,35 +65,37 @@ looks for a key.
(Alternative: export `GREENPT_API_KEY` in your environment.)
3. **Register the plugin** in `~/.config/opencode/tui.json`, setting the
3. **Register the plugin** in `~/.config/opencode/cli.json`, setting the
**absolute path to your clone**:
```jsonc
{
"$schema": "https://opencode.ai/tui.json",
"plugin": [
["file:///ABSOLUTE/PATH/TO/opencode-greenpt-usage/src/index.tsx", { "order": 700 }]
"$schema": "https://opencode.ai/v2/cli.json",
"plugins": [
{
"package": "file:///ABSOLUTE/PATH/TO/opencode-greenpt-usage",
"options": { "showWhenUnavailable": false }
}
]
}
```
> Use a `file://` URL pointing at the source file `src/index.tsx`
> (OpenCode compiles the TSX itself). Do not point at a bundle: that
> would embed a second copy of `solid-js` and the section would stay
> blank.
> Point at the repository root, not at a source file: OpenCode v2 loads
> the `./tui` entry (`src/tui.tsx`) from the package `exports` and
> resolves `@opencode/plugin` at runtime.
4. **Fully quit OpenCode and relaunch it** (plugins are not hot-reloaded).
## Configuration
All options are optional and go in the plugin entry of `tui.json`:
All options are optional and go in the `options` object of the plugin entry
in `cli.json`:
| Option | Type | Default | Description |
| --------------------- | ------- | --------------------------- | ------------------------------------------------------- |
| `apiKey` | string | resolved automatically | Explicit GreenPT key. |
| `baseUrl` | string | `https://api.greenpt.ai/v1` | GreenPT API base URL. |
| `refreshInterval` | number | `300` | Refresh interval in seconds (`0` disables the timer). |
| `order` | number | `700` | Sidebar section position. |
| `showWhenUnavailable` | boolean | `false` | Show a hint when no GreenPT key exists. |
| `unit` | string | `€` | Unit appended after the amount. |
| `trackUsage` | boolean | `true` | Track credit spend and per-model usage. |
@@ -103,7 +106,7 @@ All options are optional and go in the plugin entry of `tui.json`:
- **The section does not appear**: make sure `opencode auth login` stored a
`greenpt` key (or that `GREENPT_API_KEY` is set), and that the `file://`
URL in `tui.json` points at the right path.
path in `cli.json` points at the repository root.
- **"invalid GreenPT key"**: the API key was rejected (HTTP 401).
- **"balance unavailable"**: GreenPT did not return the balance header; try
again later.