chg: [opencode-greenpt-usage] port the TUI plugin to OpenCode 2
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.
This commit is contained in:
41
README.md
41
README.md
@@ -1,7 +1,7 @@
|
||||
# opencode-greenpt-usage
|
||||
|
||||
An [OpenCode](https://opencode.ai) TUI plugin that shows the remaining
|
||||
**GreenPT API credit balance** and the **consumption** of the API key
|
||||
An [OpenCode](https://opencode.ai) **OpenCode 2** TUI plugin that shows the
|
||||
remaining **GreenPT API credit balance** and the **consumption** of the API key
|
||||
(real credit spend plus a per-model breakdown) in the sidebar.
|
||||
|
||||
```
|
||||
@@ -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's durable
|
||||
TUI store (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,7 @@ looks for a key.
|
||||
|
||||
## Requirements
|
||||
|
||||
- OpenCode `>= 1.18.0` (TUI plugin system).
|
||||
- OpenCode `>= 2.0.0` (V2 TUI plugin API).
|
||||
- A GreenPT account and API key.
|
||||
|
||||
## Installation
|
||||
@@ -64,35 +64,38 @@ looks for a key.
|
||||
|
||||
(Alternative: export `GREENPT_API_KEY` in your environment.)
|
||||
|
||||
3. **Register the plugin** in `~/.config/opencode/tui.json`, setting the
|
||||
**absolute path to your clone**:
|
||||
3. **Register the plugin** in `~/.config/opencode/cli.json`, pointing at the
|
||||
**repository directory** (the package's `./tui` export):
|
||||
|
||||
```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": { "refreshInterval": 60 }
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
> 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.
|
||||
> V2 loads a package directory, not a source file. Do not point at
|
||||
> `src/index.tsx`: a configured file is skipped. OpenCode compiles the TSX
|
||||
> entrypoint itself.
|
||||
|
||||
4. **Fully quit OpenCode and relaunch it** (plugins are not hot-reloaded).
|
||||
4. **Reload OpenCode**. V2 reloads configuration and plugins after a save; if
|
||||
a running session keeps the old module, quit and relaunch it.
|
||||
|
||||
## Configuration
|
||||
|
||||
All options are optional and go in the plugin entry of `tui.json`:
|
||||
All options are optional and go in the plugin entry of `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. |
|
||||
| `order` | number | ignored | Accepted for V1 compatibility; V2 orders slots by plugin load order. |
|
||||
| `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,9 @@ 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.
|
||||
entry in `cli.json` points at the **repository directory**, not at
|
||||
`src/index.tsx`. Check that `opencode plugin list` lists your repository
|
||||
directory.
|
||||
- **"invalid GreenPT key"**: the API key was rejected (HTTP 401).
|
||||
- **"balance unavailable"**: GreenPT did not return the balance header; try
|
||||
again later.
|
||||
|
||||
Reference in New Issue
Block a user