opencode-usage-insights 0.1.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +97 -0
- package/README.md +330 -200
- package/dist/core.d.ts +10 -0
- package/dist/gui-experimental.cjs +8 -0
- package/dist/index.d.ts +29 -4
- package/dist/index.js +3003 -50
- package/dist/index.js.map +17 -8
- package/dist/limits.d.ts +15 -0
- package/dist/panel-build.d.ts +0 -4
- package/dist/panel-data.d.ts +37 -6
- package/dist/panel.d.ts +10 -22
- package/dist/quota-client.d.ts +13 -0
- package/dist/quota.d.ts +114 -0
- package/dist/rpc-client.d.ts +24 -0
- package/dist/rpc.d.ts +1022 -0
- package/dist/rpc.js +294 -0
- package/dist/rpc.js.map +11 -0
- package/dist/service.d.ts +36 -0
- package/dist/sidebar.d.ts +7 -7
- package/dist/stats/db.d.ts +2 -2
- package/dist/stats/maintenance.d.ts +10 -0
- package/dist/stats/queries.d.ts +6 -0
- package/dist/stats-client.d.ts +65 -0
- package/dist/stats-worker.d.ts +1 -0
- package/dist/stats-worker.js +3697 -0
- package/dist/stats-worker.js.map +23 -0
- package/dist/tui.d.ts +22 -10
- package/dist/tui.js +1067 -1604
- package/dist/tui.js.map +14 -14
- package/docs/demo.md +212 -0
- package/docs/images/line.png +0 -0
- package/docs/images/panel-cache.png +0 -0
- package/docs/images/panel-overview.png +0 -0
- package/docs/images/panel-providers.png +0 -0
- package/docs/images/sidebar.png +0 -0
- package/docs/internals.md +455 -0
- package/package.json +43 -18
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
# Internals
|
|
2
|
+
|
|
3
|
+
How the plugin works under the hood: the local database, where the identity of a
|
|
4
|
+
step comes from, the rules the hot paths keep, and how the clients are tested.
|
|
5
|
+
The [README](../README.md) covers what a user sees; this file covers why it
|
|
6
|
+
behaves the way it does.
|
|
7
|
+
|
|
8
|
+
## Database
|
|
9
|
+
|
|
10
|
+
The recorder writes to the per-user state directory of the current OS:
|
|
11
|
+
|
|
12
|
+
| OS | Path |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| Linux / other | `$XDG_STATE_HOME/opencode/opencode-usage-insights/usage.db` (default `~/.local/state/opencode/opencode-usage-insights/usage.db`) |
|
|
15
|
+
| macOS | `~/Library/Application Support/opencode/opencode-usage-insights/usage.db` |
|
|
16
|
+
| Windows | `%LOCALAPPDATA%\opencode\opencode-usage-insights\usage.db` |
|
|
17
|
+
|
|
18
|
+
`OPENCODE_USAGE_INSIGHTS_DB=/path/to/usage.db` overrides the path, and the
|
|
19
|
+
`db` option overrides both. The database is opened with `journal_mode = WAL`, so
|
|
20
|
+
the worker's reader and the server's writer never block each other. The TUI and
|
|
21
|
+
the Desktop GUI never open the file: they call the server's usage RPC.
|
|
22
|
+
Every write is an upsert keyed by message id, which makes recording idempotent:
|
|
23
|
+
replayed events, restarts and re-imports never duplicate rows.
|
|
24
|
+
|
|
25
|
+
Tables: `sessions`, `prompts`, `steps`, `events`, `usage` and `meta` (the
|
|
26
|
+
schema version). The `steps` table is the interesting one: `message_id` (primary key),
|
|
27
|
+
`session_id`, `parent_id`, provider/model/variant, input/output/reasoning and
|
|
28
|
+
cache-read/cache-write tokens, charged `cost`, list-price `list_cost`,
|
|
29
|
+
`created_at`, `streamed_at`, `completed_at`, `finish` and `recorded_at`.
|
|
30
|
+
`list_cost` is nullable on purpose: `NULL` means the step was charged or its
|
|
31
|
+
price was unknown; `0` means the host's model list knows the model is free; a
|
|
32
|
+
positive value is an estimate for a step the host reported as `$0`. Storing zero for a
|
|
33
|
+
free model keeps it out of the backfill's "needs an estimate" set without
|
|
34
|
+
inventing money.
|
|
35
|
+
|
|
36
|
+
Throughput is not stored: `tps` is everything the model generated
|
|
37
|
+
(`output + reasoning`) over the streaming window (`created → streamed`), and the
|
|
38
|
+
`latency` view computes it from the timings and reports its p50/p90/p99. The
|
|
39
|
+
window is the streaming one on purpose — `streamed` marks the end of streaming
|
|
40
|
+
and `completed` the end of the step, which for a tool-call step happens after
|
|
41
|
+
the tool calls were handled — so a step whose window or produced tokens are
|
|
42
|
+
unknown has no throughput and is left out of the percentiles.
|
|
43
|
+
|
|
44
|
+
Two expression indexes carry every ranged view: `steps_at` on
|
|
45
|
+
`COALESCE(created_at, recorded_at)` (the step's own clock, falling back to the
|
|
46
|
+
recording time for a step that was already in flight when the plugin loaded)
|
|
47
|
+
and `steps_session_at` on the same expression scoped to a session, which is what
|
|
48
|
+
the per-prompt history and the sidebar read. Without them a range filter is a
|
|
49
|
+
full table scan on every panel rebuild.
|
|
50
|
+
|
|
51
|
+
The file is private to the user: its directory is created `0700` and the file
|
|
52
|
+
`0600` (POSIX), re-applied every time either is opened. Schema migrations run on
|
|
53
|
+
the server's writer connection. The worker opens a read-only connection for the
|
|
54
|
+
panel and the turn history — it can never write, never migrate and never hold
|
|
55
|
+
the writer's lock — and prune, wipe, the list-price backfill and compact use a
|
|
56
|
+
separate short-lived writer inside the worker with a 250 ms busy timeout, so
|
|
57
|
+
maintenance fails fast with a message instead of stalling the recorder.
|
|
58
|
+
|
|
59
|
+
## The identity of a step
|
|
60
|
+
|
|
61
|
+
Every view groups a step by one thing: the **provider the model ran through**,
|
|
62
|
+
as OpenCode is configured (`provider_id` in the table, `opencode`,
|
|
63
|
+
`opencode-go`, `deepseek`, …), and the model itself.
|
|
64
|
+
|
|
65
|
+
That is the whole answer available, and it is deliberate. For a while the plugin
|
|
66
|
+
also recorded the *routing endpoint* a gateway named in a response header
|
|
67
|
+
(`x-opencode-endpoint-id`), which said which downstream operator actually ran the
|
|
68
|
+
inference behind a gateway. The Zen routes stopped publishing it — the header
|
|
69
|
+
came from a legacy handler that was removed when the routes became a thin
|
|
70
|
+
forwarding proxy ([anomalyco/opencode#54563](https://github.com/anomalyco/opencode/pull/54563))
|
|
71
|
+
— and no other source names that upstream: the response body of a routed request
|
|
72
|
+
is provider-neutral, the request host is the gateway, and the host binary
|
|
73
|
+
contains no reference to the header. So there is nothing left to discover, and
|
|
74
|
+
nothing is inferred in its place.
|
|
75
|
+
|
|
76
|
+
What the recorder writes is what the event stream carries. `recordStep` takes
|
|
77
|
+
the provider from the step's own model reference (or, for a step that started
|
|
78
|
+
before the plugin loaded, from the last model the session selected), and
|
|
79
|
+
validates it as a single token (letters, digits, `. _ : / -`, at most 64
|
|
80
|
+
characters) before it becomes a grouping key: a value that is not a provider id
|
|
81
|
+
is stored as absent rather than splitting the views on a typo. Case is left
|
|
82
|
+
alone — the provider reads the way it is configured.
|
|
83
|
+
|
|
84
|
+
Two consequences are load-bearing in the analysis layer:
|
|
85
|
+
|
|
86
|
+
- **A provider switch forfeits the cache.** The prompt cache belongs to the
|
|
87
|
+
provider that holds it, so `cacheDrop` — the rule shared with the live line —
|
|
88
|
+
takes a scope. The switch and loss reports measure a drop across a provider
|
|
89
|
+
switch (`"model"`: the model is the same, so the two steps share a cache to
|
|
90
|
+
lose), while the survival report and the TTL evidence keep the strict
|
|
91
|
+
`"provider"` scope: two providers are two caches, and a switch between them is
|
|
92
|
+
not an idle-gap sample.
|
|
93
|
+
- **A model that ran through two providers is one model with two rows.** The
|
|
94
|
+
`models` view keeps the family together and names the providers in a column;
|
|
95
|
+
the `providers` view leads with the provider and lists the models it served.
|
|
96
|
+
|
|
97
|
+
## What the line counts
|
|
98
|
+
|
|
99
|
+
The line has two totals groups — the turn and the session — and they follow
|
|
100
|
+
different rules on purpose:
|
|
101
|
+
|
|
102
|
+
- **Tokens follow the view.** `turnTotals: "latest"` (the default) reports the
|
|
103
|
+
prompt the turn ended with: summing `input`/`cached` over the steps of a
|
|
104
|
+
tool-call loop would count the same re-sent prompt once per step, which is not
|
|
105
|
+
what a context view means. `"sum"` adds every step instead, which is the
|
|
106
|
+
billing view of the tokens.
|
|
107
|
+
- **Money is always summed.** Every step of a turn is paid for, so the turn's
|
|
108
|
+
cost is the sum over its steps whatever the token view shows (`totalsText`
|
|
109
|
+
takes the cost explicitly for that reason, never the `latest` step's share).
|
|
110
|
+
For the session, the charged cost is the one the host reports; the list
|
|
111
|
+
estimate is `SUM(list_cost)` over the plugin's recorded steps, fetched over
|
|
112
|
+
the RPC and cached by the client, invalidated on every write event.
|
|
113
|
+
- **A zero cost is left out.** A free model reads as free instead of repeating
|
|
114
|
+
`$0`; `fields.cost: false` hides both charged cost and estimate.
|
|
115
|
+
- **A list estimate is labelled.** OpenCode 2.2.0 trusts a reported `cost: 0`
|
|
116
|
+
instead of falling back to its model list, and OpenCode Go reports `0` for
|
|
117
|
+
subscription-covered requests. The recorder keeps that charged `0` and stores
|
|
118
|
+
the model-list price in `list_cost`; the line renders `cost ≈ $0.0344`. A turn
|
|
119
|
+
that mixes charged and estimated steps renders both parts
|
|
120
|
+
(`cost $0.12 + ≈ $3.40`) rather than adding them silently. The `sess`
|
|
121
|
+
segment applies the same rule: the host's charged total plus the estimate
|
|
122
|
+
summed from the recorded steps, so a Go session shows its list cost there
|
|
123
|
+
too.
|
|
124
|
+
- **Switches the line warns about are between turns.** A provider change against
|
|
125
|
+
the previous prompt is `changed`, a model change inside one provider is
|
|
126
|
+
`model changed`; the same comparison between *steps* is what the `models`
|
|
127
|
+
view's detail line counts.
|
|
128
|
+
|
|
129
|
+
## List-price estimates
|
|
130
|
+
|
|
131
|
+
The “catalog” is the model list exposed by the OpenCode process that is
|
|
132
|
+
currently serving the session — not a file and not a plugin-owned network
|
|
133
|
+
request. The host resolves provider configuration and overrides before
|
|
134
|
+
exposing it. Prices are USD per 1,000,000 tokens. `steps.cost` is always the
|
|
135
|
+
host's reported charge; the plugin does not recalculate it.
|
|
136
|
+
|
|
137
|
+
| Surface | Source |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| Server recorder and backfill | `ctx.model.list()` → `ModelListOutput.data: ModelInfo[]` |
|
|
140
|
+
| Live line (TUI/Desktop) | `data.location.model.list(location)` → `ModelInfo[] \| undefined` |
|
|
141
|
+
| Panel, sidebar, NDJSON, CLI report | stored `steps.list_cost` |
|
|
142
|
+
| `sess` segment | `SUM(list_cost)` from the database, served over the RPC |
|
|
143
|
+
|
|
144
|
+
Each `ModelInfo` carries the prices in `cost: ModelCost[]`, with
|
|
145
|
+
`input`, `output`, `cache.read`, `cache.write` and an optional
|
|
146
|
+
`tier: { type: "context", size }`. The plugin keys its table by
|
|
147
|
+
`providerID/modelID`; variants do not change the price.
|
|
148
|
+
|
|
149
|
+
The plugin deliberately does **not** read `~/.cache/opencode/models.json`, does
|
|
150
|
+
**not** use `opencode models` for prices (that command lists model IDs only),
|
|
151
|
+
and makes no external network call of its own. The `go` view's local read of
|
|
152
|
+
the host's usage-limits endpoint is the only host API call; it never reaches the
|
|
153
|
+
Console.
|
|
154
|
+
|
|
155
|
+
The server loads the list without blocking the recorder, refreshes it on
|
|
156
|
+
`model.updated` and on a coarse TTL, and keeps the previous table when a refresh
|
|
157
|
+
fails. The backfill uses that table. The TUI and the Desktop build their own
|
|
158
|
+
table for the live line, with the same event invalidation and TTL; a missing
|
|
159
|
+
table means no estimate. The historical views never consult it: they read
|
|
160
|
+
`steps.list_cost`, which is why they work without a host runtime.
|
|
161
|
+
|
|
162
|
+
The estimate is the host's own `cost` fallback:
|
|
163
|
+
|
|
164
|
+
```text
|
|
165
|
+
input tokens.input (fresh, uncached prompt)
|
|
166
|
+
cache.read tokens.cache.read
|
|
167
|
+
cache.write tokens.cache.write
|
|
168
|
+
output visible output tokens
|
|
169
|
+
reasoning reasoning tokens
|
|
170
|
+
|
|
171
|
+
prompt = input + cache.read + cache.write
|
|
172
|
+
tier = the largest tier with tier.type === "context" and prompt > tier.size
|
|
173
|
+
else the untiered entry
|
|
174
|
+
cost = (input * inputPrice
|
|
175
|
+
+ (output + reasoning) * outputPrice
|
|
176
|
+
+ cache.read * cacheReadPrice
|
|
177
|
+
+ cache.write * cacheWritePrice) / 1e6
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Reasoning is billed at the output price. For a model with context tiers, the
|
|
181
|
+
strict `>` matters: at exactly the tier size the untiered price applies. No
|
|
182
|
+
applicable tier or an unknown model means no estimate; an all-zero applicable
|
|
183
|
+
price returns `0`, which records a known free model.
|
|
184
|
+
|
|
185
|
+
These are the same buckets the line folds for display: `in = input +
|
|
186
|
+
cache.write`, `cached = cache.read`, `out = output + reasoning`, and `prompt =
|
|
187
|
+
in + cached`. In particular, the formula's `input` is **not** the line's `in`;
|
|
188
|
+
using the displayed `in` would count cache writes twice.
|
|
189
|
+
|
|
190
|
+
A worked example with the `opencode-go/deepseek-v4.1-flash` prices
|
|
191
|
+
(`input 0.15`, `output 0.6`, `cache.read 0.003`):
|
|
192
|
+
|
|
193
|
+
```text
|
|
194
|
+
2,591 × 0.15 + (564 + 3,239) × 0.6 + 514,944 × 0.003
|
|
195
|
+
= 4,215.282 per 1,000,000 tokens = $0.004215282
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
`list_cost` is `NULL` when the step was charged or its price is unknown, `0`
|
|
199
|
+
for a known free model, and positive for an estimate. The line omits a zero
|
|
200
|
+
cost; the panel has a cell to fill, so it shows `$0` for a free or unknown-price
|
|
201
|
+
step.
|
|
202
|
+
|
|
203
|
+
The estimate is stored instead of recomputed on read for a practical reason:
|
|
204
|
+
the panel, the sidebar, the NDJSON export and `bun run report` read the database
|
|
205
|
+
without a host runtime. `list_cost` freezes the price at record time; the live
|
|
206
|
+
turn prices from the client's current model list, which may be newer. The
|
|
207
|
+
maintenance backfill intentionally uses today's model list and is user-triggered,
|
|
208
|
+
because a model that was free when the step ran may not be free now.
|
|
209
|
+
|
|
210
|
+
The session segment follows a split rule: its charged total is the host's, and
|
|
211
|
+
its estimate is `SUM(list_cost)` over the plugin's recorded steps, fetched over
|
|
212
|
+
the RPC and cached by the client, invalidated on every write event. If the
|
|
213
|
+
plugin began recording mid-session, the estimate covers only the recorded steps;
|
|
214
|
+
the charged total still comes from the host. If the database is unavailable, the
|
|
215
|
+
line falls back to the host total with no estimate.
|
|
216
|
+
|
|
217
|
+
The backfill only considers steps with a provider, a model and at least one
|
|
218
|
+
token; it prices free models as `0` and leaves models the host's current model
|
|
219
|
+
list no longer contains as `NULL`, so `e` can be retried after the list changes.
|
|
220
|
+
`pricing: "off"` stops new estimates, hides stored ones in the line, panel and
|
|
221
|
+
sidebar, and disables the backfill. It does not delete the column.
|
|
222
|
+
|
|
223
|
+
## Non-step usage
|
|
224
|
+
|
|
225
|
+
The host also publishes `session.usage.recorded` with `source: "compaction"` or
|
|
226
|
+
`source: "title"` for generation that has no assistant step of its own. The
|
|
227
|
+
recorder stores it in the `usage` table (`id` = host event ID, `session_id`,
|
|
228
|
+
`source`, `at`, `cost`, and the token buckets). The primary key makes a
|
|
229
|
+
replayed event a no-op; the compaction event itself is stored in `events` for
|
|
230
|
+
the cache-loss reason and keyed by its own host ID.
|
|
231
|
+
|
|
232
|
+
The host includes this usage in the session's own `cost` and token totals, so
|
|
233
|
+
the views that claim to describe a whole selection include its cost: the
|
|
234
|
+
`overview`, the `periods` buckets and the session rows. The overview names the
|
|
235
|
+
part as `other`; a session row names it on its detail line. Tokens and the
|
|
236
|
+
cache views stay step-based (the `input`/`cache_read` columns are summed over
|
|
237
|
+
steps only), because the non-step record carries no model and no cache
|
|
238
|
+
comparison.
|
|
239
|
+
|
|
240
|
+
The `models`, `providers` and `turns` rows stay step-based: the host's non-step
|
|
241
|
+
usage record carries no model, and a title generation has none at all, so
|
|
242
|
+
attributing the money to a model would invent an identity. The `sess` segment is
|
|
243
|
+
unaffected: it already reads the host's session total, which includes this
|
|
244
|
+
usage; the estimate half is still `SUM(list_cost)` over steps, so nothing is
|
|
245
|
+
added twice.
|
|
246
|
+
|
|
247
|
+
This is host money, not a list-price estimate, so `pricing: "off"` does not hide
|
|
248
|
+
it. A database written by an older plugin is healed at open: the stored
|
|
249
|
+
compaction events already carry `cost` and `tokens`, so a migration recovers
|
|
250
|
+
them with a stable synthetic ID. Title usage was never stored anywhere before
|
|
251
|
+
the upgrade and stays missing; the line keeps showing the host's whole session
|
|
252
|
+
total meanwhile.
|
|
253
|
+
|
|
254
|
+
### Event idempotency
|
|
255
|
+
|
|
256
|
+
An event with a host ID is stored with `message_id`; a partial unique index
|
|
257
|
+
(`kind, message_id`) turns a replay into `INSERT OR IGNORE`. A migration
|
|
258
|
+
deduplicates compaction rows an older recorder inserted on every delivery
|
|
259
|
+
before the index is created, so the event count and any cost read from the
|
|
260
|
+
table stay honest.
|
|
261
|
+
|
|
262
|
+
## OpenCode Go allowance
|
|
263
|
+
|
|
264
|
+
The `go` view is the one panel view that does not read SQLite. The host publishes
|
|
265
|
+
the Go allowance at `GET /api/experimental/usage/limits` and exposes the same
|
|
266
|
+
call to clients as `client.experimental.usage.limits()`. The plugin reads it
|
|
267
|
+
from the TUI and the Desktop; the server part's plugin context does not expose
|
|
268
|
+
that endpoint, so a server-rendered view would have no data. A caller that asks
|
|
269
|
+
the RPC for `go` gets an explanatory line instead of a table.
|
|
270
|
+
|
|
271
|
+
The payload has one meter per window:
|
|
272
|
+
|
|
273
|
+
```json
|
|
274
|
+
{
|
|
275
|
+
"go": {
|
|
276
|
+
"rolling": { "percent": 2, "limited": false, "resetsAt": 1796067785000 },
|
|
277
|
+
"weekly": { "percent": 9, "limited": false, "resetsAt": 1796601600000 },
|
|
278
|
+
"monthly": { "percent": 40, "limited": false, "resetsAt": 1797285964000 }
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
`percent` is a whole number, `limited` is Go refusing requests, and `resetsAt`
|
|
284
|
+
is the window's reset. The monthly reset is also the paid-period renewal; the UI
|
|
285
|
+
calls it `renews` and prints the exact local date and time. The payload carries
|
|
286
|
+
neither a cycle start nor a plan type: Go, Go Plus and Go Teams share the same
|
|
287
|
+
meters. The UI therefore shows `quotaName` when configured, else the host
|
|
288
|
+
provider name, else the full `OpenCode Go`; a Go Plus/Teams user can set
|
|
289
|
+
`quotaName` to their plan name. The compact blocks show that name before the
|
|
290
|
+
numbers.
|
|
291
|
+
|
|
292
|
+
The payload carries no cycle start, so the forecast infers it as one calendar
|
|
293
|
+
month before the reset:
|
|
294
|
+
|
|
295
|
+
```text
|
|
296
|
+
start = previousCycleStart(monthly.resetsAt)
|
|
297
|
+
elapsed = now - start
|
|
298
|
+
rate = percent / elapsedDays
|
|
299
|
+
projected = percent + rate * remainingDays
|
|
300
|
+
exhaustAt = now + (100 - percent) / rate
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
The projection is shown only after 30 minutes of the cycle, and only when the
|
|
304
|
+
percent is a useful signal; a fresh cycle reads `too early to project`. A
|
|
305
|
+
limited or 100% meter reads `monthly limit reached` instead of a projection.
|
|
306
|
+
There is deliberately no sample history: the host exposes one current value, so
|
|
307
|
+
a line chart would imply observations the plugin never took. The Desktop draws
|
|
308
|
+
current-versus-forecast bars from this single sample. If a real trend chart is
|
|
309
|
+
ever wanted, the clients must record quota samples first.
|
|
310
|
+
|
|
311
|
+
OpenCode 2.4 also has its own **Usage** dialog: Desktop's **View usage** lists
|
|
312
|
+
the Go meters and Console budgets; the TUI `/usage` lists Console budgets and
|
|
313
|
+
per-model remaining. The plugin does not duplicate the Console budget section;
|
|
314
|
+
it adds the forecast, the exact renewal, the TUI sidebar block, the Desktop
|
|
315
|
+
Turns block and the panel chart. The
|
|
316
|
+
plugin's command is `/usage-insights` (alias `/insights`) so the host keeps
|
|
317
|
+
`/usage`.
|
|
318
|
+
|
|
319
|
+
## Privacy and security
|
|
320
|
+
|
|
321
|
+
The plugin is local by construction: no external network client, no
|
|
322
|
+
subprocess, no credentials, no telemetry. Everything it records stays in the
|
|
323
|
+
SQLite file in the user's own state directory. The one extra call is local: the
|
|
324
|
+
`go` view asks the OpenCode server for the allowance the host already fetched
|
|
325
|
+
from the OpenCode Console. The plugin never sees the Console API key and never
|
|
326
|
+
contacts the Console itself; `quota: "off"` stops that read.
|
|
327
|
+
|
|
328
|
+
**What is stored.** Per step: timings, token counts, charged cost and the
|
|
329
|
+
list-price estimate when the host reported `$0`, the provider/model/variant it
|
|
330
|
+
ran as, the finish reason, the session and parent ids, and the session's title
|
|
331
|
+
and working directory as the host reports them. Per compaction or title
|
|
332
|
+
generation: the host event ID, its source, time, cost and token counts. Prompt
|
|
333
|
+
text and generated text are never stored, including for those records.
|
|
334
|
+
**Prompts and responses are not stored**: the recorder reads event payloads for
|
|
335
|
+
numbers and identifiers and keeps nothing else — no message text, no tool
|
|
336
|
+
arguments, no file contents, no environment variables.
|
|
337
|
+
|
|
338
|
+
**What protects it.**
|
|
339
|
+
|
|
340
|
+
- **Private files.** The database directory is created `0700` and the file
|
|
341
|
+
`0600` (POSIX), re-applied on every open; on Windows the file inherits the
|
|
342
|
+
user profile's ACL.
|
|
343
|
+
- **The Go allowance is read, not stored.** The `go` view keeps the meters and
|
|
344
|
+
the inferred forecast in memory only. No workspace id, usage URL or quota
|
|
345
|
+
sample is written to the database or sent over the plugin RPC.
|
|
346
|
+
- **A read-only reader.** The worker opens the database read-only, so panel and
|
|
347
|
+
history queries can never write, migrate or take the recorder's lock. Prune,
|
|
348
|
+
wipe and compact open a short-lived writer inside the worker with a 250 ms
|
|
349
|
+
busy timeout: a maintenance action that cannot take the lock tells the user
|
|
350
|
+
and stops, instead of stalling the request pipeline that records usage.
|
|
351
|
+
- **A bounded, validated RPC.** The TUI and the Desktop GUI read the worker
|
|
352
|
+
through JSON-Schema-validated methods with closed objects, enums and clamps.
|
|
353
|
+
There is no SQL, path, import or export method. Maintenance is gated by the
|
|
354
|
+
`maintenance` option and an explicit confirmation; the worker runs every query
|
|
355
|
+
off the server's event loop and the client queue is bounded while the worker
|
|
356
|
+
is in use. Reads have a 10 s watchdog; prune, wipe, vacuum and backfill get
|
|
357
|
+
five minutes because rewriting a large file is legitimately slower. The
|
|
358
|
+
in-process fallback shares the server's event loop and has no separate queue.
|
|
359
|
+
- **Validated input.** Anything that reaches the database from outside — a
|
|
360
|
+
provider id the host reports, a session title, an imported NDJSON dump — is
|
|
361
|
+
validated as an identifier token or stripped of control characters and capped
|
|
362
|
+
in length, both when it is stored and again when it is rendered. A terminal
|
|
363
|
+
escape sequence in a title cannot reach the terminal, and a dump cannot import
|
|
364
|
+
a value that does not fit its column.
|
|
365
|
+
- **Parameterized SQL everywhere**, and no dynamic table or column names taken
|
|
366
|
+
from data.
|
|
367
|
+
|
|
368
|
+
## Performance
|
|
369
|
+
|
|
370
|
+
The plugin sits on paths the host waits for: the recorder's event handlers, the
|
|
371
|
+
TUI line and the Desktop header badge. The rules it keeps are:
|
|
372
|
+
|
|
373
|
+
- **Writes are small and rare.** A step is one upsert, written when it ends;
|
|
374
|
+
nothing is written on a response, because the response carries nothing the
|
|
375
|
+
views read.
|
|
376
|
+
- **Reads are index-driven.** Every range filter compares the expression the
|
|
377
|
+
`steps_at` index covers, and the per-prompt history starts from the newest
|
|
378
|
+
prompts. It widens only until it holds the requested number of finished
|
|
379
|
+
turns, so a session with enough history stays bounded; a session with fewer
|
|
380
|
+
finished turns can walk its history once. The line never scans a session's
|
|
381
|
+
whole history.
|
|
382
|
+
- **Clients read through the worker, at most 300 ms apart.** The TUI and the
|
|
383
|
+
Desktop GUI each call the RPC; a burst of step events is coalesced into one
|
|
384
|
+
request, and the worker runs the query off the server's event loop. The
|
|
385
|
+
panel's and the history's rows are shared; the file's own numbers (`dbInfo`)
|
|
386
|
+
are only asked for by the `db` view.
|
|
387
|
+
- **The Desktop badge reads the session mid-turn.** While a turn runs it reads
|
|
388
|
+
the host's session cost and tokens plus the cached list estimate, both O(1),
|
|
389
|
+
and never the transcript; only an idle badge walks the transcript tail.
|
|
390
|
+
- **The panel's cost is the range it shows.** A view folds the steps of its
|
|
391
|
+
range — in SQLite where the answer is a sum or a count, in JavaScript where it
|
|
392
|
+
is a percentile or a comparison between consecutive steps. That is why the
|
|
393
|
+
range is a first-class setting, why each view reads the rows once and shares
|
|
394
|
+
them between its reports, and why the budgets in `scripts/bench.ts` are per
|
|
395
|
+
view.
|
|
396
|
+
|
|
397
|
+
Two rules came out of measuring, and the bench enforces both:
|
|
398
|
+
|
|
399
|
+
- **Aggregate in SQL, fold in JS.** `overview` and the `sessions` and `turns`
|
|
400
|
+
views are sums and counts: they never move a row into JavaScript
|
|
401
|
+
(`coverageOf` is one SQL statement). Percentiles and pair comparisons read the
|
|
402
|
+
four to sixteen columns they use and fold them in JS, because SQLite's window
|
|
403
|
+
functions cost several times more per row than the projection does.
|
|
404
|
+
- **One read per report per view.** `test/panel-build.test.ts` counts the
|
|
405
|
+
statements a view prepares, and fails if one view asks the database for the
|
|
406
|
+
steps more than twice.
|
|
407
|
+
|
|
408
|
+
`bun run bench` is the check: it fails on a budget overrun or on a read that
|
|
409
|
+
stopped using an index. `bun run bench` checks the budgets on 20k steps,
|
|
410
|
+
`bun run bench --full` on 120k, and `--explain` prints the query plans.
|
|
411
|
+
|
|
412
|
+
## Testing the clients
|
|
413
|
+
|
|
414
|
+
The TUI harness is the same one the tests and the documentation images use. The
|
|
415
|
+
Desktop line helpers have their own pure tests (`test/gui-line.test.ts`),
|
|
416
|
+
`test/gui-bundle.test.ts` evaluates the built CJS bundle with the app's shared
|
|
417
|
+
modules, and `test/server.test.ts` drives the real server RPC through the
|
|
418
|
+
worker. `test/limits.test.ts` keeps the client clamps and the RPC schema bounds
|
|
419
|
+
in step, and `test/stats-client.test.ts` covers the worker start/shutdown and
|
|
420
|
+
the separate read/maintenance watchdogs. `bun test` runs through
|
|
421
|
+
`bunfig.toml`, which preloads `test/preload.ts`.
|
|
422
|
+
Bun resolves the bare `solid-js` specifier to the package's server build, where
|
|
423
|
+
signals do not track and render effects run once; the host resolves the client
|
|
424
|
+
build (the one `@opentui/solid` uses internally). The preload points the
|
|
425
|
+
specifier at the client build so the harness, the plugin and the renderer share
|
|
426
|
+
one instance — otherwise every re-render assertion would pass vacuously.
|
|
427
|
+
`scripts/screenshot.ts` does the same alignment before it loads the harness, so
|
|
428
|
+
the documentation images are rendered by the real components.
|
|
429
|
+
|
|
430
|
+
Two consequences for render tests:
|
|
431
|
+
|
|
432
|
+
- The headless renderer paints a single frame, so assertions read the
|
|
433
|
+
**renderable tree** (`treeText` in `test/plugin.test.ts`) after an update
|
|
434
|
+
rather than the frame buffer.
|
|
435
|
+
- A render test must **drive an update** and check that the output was replaced,
|
|
436
|
+
not appended. The line regressed once exactly that way: a `StyledText` passed
|
|
437
|
+
as a child of `<text>` is appended as chunks that are never removed, so each
|
|
438
|
+
reactive update added another full copy of the line. `bun run demo` shows the
|
|
439
|
+
same cases as plain text.
|
|
440
|
+
|
|
441
|
+
## Pre-release checklist
|
|
442
|
+
|
|
443
|
+
```sh
|
|
444
|
+
bun install
|
|
445
|
+
bun run typecheck
|
|
446
|
+
bun run build
|
|
447
|
+
bun test # whole suite, Linux
|
|
448
|
+
bun run check:package
|
|
449
|
+
bun run check:platform
|
|
450
|
+
bun run bench --full # budgets at 120k steps
|
|
451
|
+
npm pack --dry-run
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
Inspect the pack list (`dist/`, `README.md`, `LICENSE` only). Scan for secrets
|
|
455
|
+
before `npm publish`.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-usage-insights",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "OpenCode V2 plugin that puts per-turn and per-session cost back in the TUI — charged cost, list-price estimates, tokens, prompt-cache reuse, latency
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "OpenCode V2 plugin that puts per-turn and per-session cost back in the TUI and OpenCode Desktop — charged cost, list-price estimates, tokens, prompt-cache reuse, latency, local history and an OpenCode Go allowance forecast in a live line, sidebar, header badge and /usage-insights panel.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": {
|
|
@@ -32,10 +32,19 @@
|
|
|
32
32
|
"cost-estimation",
|
|
33
33
|
"local-first",
|
|
34
34
|
"bun",
|
|
35
|
-
"typescript"
|
|
35
|
+
"typescript",
|
|
36
|
+
"opencode-desktop",
|
|
37
|
+
"desktop",
|
|
38
|
+
"electron",
|
|
39
|
+
"gui",
|
|
40
|
+
"rpc",
|
|
41
|
+
"opencode-go",
|
|
42
|
+
"quota",
|
|
43
|
+
"allowance",
|
|
44
|
+
"forecast"
|
|
36
45
|
],
|
|
37
46
|
"engines": {
|
|
38
|
-
"opencode": ">=2.
|
|
47
|
+
"opencode": ">=2.4.2"
|
|
39
48
|
},
|
|
40
49
|
"publishConfig": {
|
|
41
50
|
"access": "public"
|
|
@@ -43,7 +52,9 @@
|
|
|
43
52
|
"files": [
|
|
44
53
|
"dist",
|
|
45
54
|
"README.md",
|
|
46
|
-
"
|
|
55
|
+
"CONTRIBUTING.md",
|
|
56
|
+
"LICENSE",
|
|
57
|
+
"docs"
|
|
47
58
|
],
|
|
48
59
|
"exports": {
|
|
49
60
|
".": {
|
|
@@ -55,34 +66,48 @@
|
|
|
55
66
|
"types": "./dist/tui.d.ts",
|
|
56
67
|
"import": "./dist/tui.js",
|
|
57
68
|
"default": "./dist/tui.js"
|
|
58
|
-
}
|
|
69
|
+
},
|
|
70
|
+
"./rpc": {
|
|
71
|
+
"types": "./dist/rpc.d.ts",
|
|
72
|
+
"import": "./dist/rpc.js",
|
|
73
|
+
"default": "./dist/rpc.js"
|
|
74
|
+
},
|
|
75
|
+
"./gui-experimental": "./dist/gui-experimental.cjs"
|
|
59
76
|
},
|
|
60
77
|
"scripts": {
|
|
61
78
|
"test": "bun test",
|
|
62
|
-
"typecheck": "tsc --noEmit",
|
|
79
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.gui.json",
|
|
63
80
|
"build": "bun run scripts/build.ts",
|
|
64
81
|
"demo": "bun run scripts/demo.ts",
|
|
65
82
|
"docs:images": "bun run scripts/screenshot.ts",
|
|
66
83
|
"report": "bun run scripts/report.ts",
|
|
67
|
-
"prepublishOnly": "bun run typecheck && bun test && bun run
|
|
84
|
+
"prepublishOnly": "bun run typecheck && bun run build && bun test && bun run check:package",
|
|
68
85
|
"bench": "bun run scripts/bench.ts",
|
|
69
|
-
"check:platform": "bun run scripts/check-platform.ts"
|
|
86
|
+
"check:platform": "bun run scripts/check-platform.ts",
|
|
87
|
+
"build:gui": "vite build",
|
|
88
|
+
"dev:gui": "vite build --watch",
|
|
89
|
+
"check:package": "bun run scripts/check-package.ts"
|
|
70
90
|
},
|
|
71
91
|
"peerDependencies": {
|
|
72
|
-
"@opencode/plugin": ">=2.
|
|
73
|
-
"@opencode/theme": ">=2.
|
|
74
|
-
"@opentui/core": ">=0.
|
|
75
|
-
"@opentui/solid": ">=0.
|
|
92
|
+
"@opencode/plugin": ">=2.4.2",
|
|
93
|
+
"@opencode/theme": ">=2.4.2",
|
|
94
|
+
"@opentui/core": ">=0.8.0",
|
|
95
|
+
"@opentui/solid": ">=0.8.0",
|
|
96
|
+
"effect": ">=4.1.0",
|
|
76
97
|
"solid-js": ">=1.9.12"
|
|
77
98
|
},
|
|
78
99
|
"devDependencies": {
|
|
79
|
-
"@
|
|
80
|
-
"@opencode/
|
|
81
|
-
"@
|
|
82
|
-
"@opentui/
|
|
100
|
+
"@happy-dom/global-registrator": "20.0.11",
|
|
101
|
+
"@opencode/plugin": "2.4.2",
|
|
102
|
+
"@opencode/theme": "2.4.2",
|
|
103
|
+
"@opentui/core": "0.9.9",
|
|
104
|
+
"@opentui/solid": "0.9.9",
|
|
83
105
|
"@types/bun": "^1.4.1",
|
|
84
106
|
"@types/node": "^24.0.0",
|
|
107
|
+
"effect": "4.1.0",
|
|
85
108
|
"solid-js": "1.9.12",
|
|
86
|
-
"typescript": "^5.8.0"
|
|
109
|
+
"typescript": "^5.8.0",
|
|
110
|
+
"vite": "^7",
|
|
111
|
+
"vite-plugin-solid": "^2.11.17"
|
|
87
112
|
}
|
|
88
113
|
}
|