new: [opencode-greenpt-usage] track API key consumption per day and model
Show real credit spend and a per-model breakdown under the balance: - ``src/usage.ts`` (new, pure): idempotent day buckets keyed by message id, day/month totals, retention pruning, validated KV serialization. - Real spend = delta of ``x-credits-remaining`` between two free probes (top-ups are not counted as spend), covering every client of the key. - Per-model detail = reconciliation of local GreenPT assistant messages (tokens + estimated cost, marked ``~``), re-run on every refresh and on ``session.idle``; last 10 sessions backfilled at startup. - Buckets persisted in the plugin KV store (90-day window) with new options ``trackUsage``, ``usageWindowDays``, ``usageBackfill`` and a ``greenpt.usage.reset`` command.
This commit is contained in:
24
README.md
24
README.md
@@ -1,11 +1,15 @@
|
||||
# opencode-greenpt-usage
|
||||
|
||||
An [OpenCode](https://opencode.ai) TUI plugin that shows the remaining
|
||||
**GreenPT API credit balance** in the sidebar.
|
||||
**GreenPT API credit balance** and the **consumption** of the API key
|
||||
(real credit spend plus a per-model breakdown) in the sidebar.
|
||||
|
||||
```
|
||||
▼ GreenPT
|
||||
Balance 5.31 € updated 21:35
|
||||
Spent today 0.04 € month 0.31 €
|
||||
glm-5.3-flash 12.4k tok ~0.02 €
|
||||
kimi-k2 3.1k tok ~0.01 €
|
||||
```
|
||||
|
||||
## How it works
|
||||
@@ -20,10 +24,23 @@ Balance 5.31 € updated 21:35
|
||||
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):
|
||||
- *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;
|
||||
- *per-model breakdown* (tokens and estimated cost) is reconciled from
|
||||
the GreenPT assistant messages of your local OpenCode sessions — this
|
||||
detail is therefore **local to this machine** and priced with
|
||||
OpenCode's model catalog, which may differ from GreenPT's credit
|
||||
pricing (estimates are marked with `~`).
|
||||
- At startup, the last 10 sessions are reconciled to backfill recent
|
||||
history; counters can be cleared with `greenpt.usage.reset`.
|
||||
- The section is **hidden while no GreenPT key is present**.
|
||||
- Refresh happens on startup, at the end of a session, on server
|
||||
reconnection, on a timer (300 s by default) and on demand through the
|
||||
`greenpt.refresh` command.
|
||||
`greenpt.refresh` command; usage buckets are reconciled on each refresh
|
||||
and at the end of each session.
|
||||
|
||||
There is no dependency on any local profile or command: the plugin only
|
||||
looks for a key.
|
||||
@@ -78,6 +95,9 @@ All options are optional and go in the plugin entry of `tui.json`:
|
||||
| `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. |
|
||||
| `usageWindowDays` | number | `90` | Retention window for per-day usage buckets. |
|
||||
| `usageBackfill` | number | `10` | Recent sessions reconciled at startup (`0` disables). |
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
|
||||
Reference in New Issue
Block a user