@leo000001/opencode-quota-sidebar 1.13.7 → 1.13.10

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/README.md CHANGED
@@ -1,480 +1,480 @@
1
- # opencode-quota-sidebar
2
-
3
- [![npm version](https://img.shields.io/npm/v/@leo000001/opencode-quota-sidebar.svg)](https://www.npmjs.com/package/@leo000001/opencode-quota-sidebar)
4
- [![license](https://img.shields.io/npm/l/@leo000001/opencode-quota-sidebar.svg)](https://github.com/xihuai18/opencode-quota-sidebar/blob/main/LICENSE)
5
-
6
- OpenCode plugin: show token usage and subscription quota in the session sidebar title.
7
-
8
- ![Example sidebar title with usage and quota](./assets/OpenCode-Quota-Sidebar.png)
9
-
10
- ## Install
11
-
12
- Add the package name to `plugin` in your `opencode.json`. OpenCode uses Bun to install it automatically on startup:
13
-
14
- ```json
15
- {
16
- "plugin": ["@leo000001/opencode-quota-sidebar@1.13.2"]
17
- }
18
- ```
19
-
20
- Note for OpenCode `>=1.2.15`: TUI settings (`theme`/`keybinds`/`tui`) moved to `tui.json`, but plugin loading still stays in `opencode.json` (`plugin: []`).
21
- This plugin also accepts both `config.providers` and older `provider.list` runtime shapes when discovering provider options.
22
-
23
- If you prefer automatic upgrades, you can still use `@latest`, but pinning an exact version makes behavior easier to reproduce when debugging.
24
-
25
- ## Development (build from source)
26
-
27
- ```bash
28
- npm install
29
- npm run build
30
- ```
31
-
32
- Add the built file to your `opencode.json`:
33
-
34
- ```json
35
- {
36
- "plugin": ["file:///ABSOLUTE/PATH/opencode-quota-sidebar/dist/index.js"]
37
- }
38
- ```
39
-
40
- On Windows, use forward slashes: `"file:///D:/Lab/opencode-quota-sidebar/dist/index.js"`
41
-
42
- ## Supported quota providers
43
-
44
- | Provider | Endpoint | Auth | Status |
45
- | -------------- | -------------------------------------- | --------------- | --------------------------------------- |
46
- | OpenAI Codex | `chatgpt.com/backend-api/wham/usage` | OAuth (ChatGPT) | Multi-window (short-term + weekly) |
47
- | GitHub Copilot | `api.github.com/copilot_internal/user` | OAuth | Monthly quota |
48
- | RightCode | `www.right.codes/account/summary` | API key | Subscription or balance (by prefix) |
49
- | Buzz | `buzzai.cc/v1/dashboard/billing/*` | API key | Balance only (computed from total-used) |
50
- | Anthropic | `api.anthropic.com/api/oauth/usage` | OAuth | Multi-window (5h + weekly / plan-based) |
51
-
52
- Want to add support for another provider (Google Antigravity, Zhipu AI, Firmware AI, etc.)? See [CONTRIBUTING.md](CONTRIBUTING.md).
53
-
54
- ## Features
55
-
56
- - Session title becomes multiline in sidebar:
57
- - line 1: original session title
58
- - line 2: Input/Output tokens
59
- - line 3: Cache Read tokens (only if non-zero)
60
- - line 4: Cache Write tokens (only if non-zero)
61
- - line 5: `$X.XX as API cost` (equivalent API billing for subscription-auth providers)
62
- - quota lines: quota text like `OpenAI 5h 80% Rst 16:20`; short windows (`5h`, `1d`, `Daily`) show `HH:MM` on same-day resets and `MM-DD HH:MM` when crossing days, while longer windows continue to show `MM-DD`
63
- - RightCode daily quota shows `$remaining/$dailyTotal` + expiry (e.g. `RC Daily $105/$60 Exp 02-27`, without trailing percent) and also shows balance on the next indented line when available; `Exp` remains date-only
64
- - Session-scoped usage/quota can include descendant subagent sessions (enabled by default via `sidebar.includeChildren=true`). Traversal is bounded by `childrenMaxDepth` (default 6), `childrenMaxSessions` (default 128), and `childrenConcurrency` (default 5); truncation is logged when `OPENCODE_QUOTA_DEBUG=1`. Day/week/month ranges never merge children — only session scope does.
65
- - Toast message includes three sections: `Token Usage`, `Cost as API` (per provider), and `Quota`
66
- - Quota snapshots are de-duplicated before rendering to avoid repeated provider lines
67
- - Custom tools:
68
- - `quota_summary` — generate usage report for session/day/week/month (markdown + toast)
69
- - `quota_show` — toggle sidebar title display on/off (state persists across sessions)
70
- - Quota connectors:
71
- - OpenAI Codex OAuth (`/backend-api/wham/usage`)
72
- - GitHub Copilot OAuth (`/copilot_internal/user`)
73
- - RightCode API key (`/account/summary`)
74
- - Buzz API key (`/v1/dashboard/billing/subscription` + `/v1/dashboard/billing/usage`)
75
- - Anthropic Claude OAuth (`/api/oauth/usage`, with beta header)
76
- - OpenAI OAuth quota checks auto-refresh expired access token (using refresh token)
77
- - API key providers still show usage aggregation (quota only applies to subscription providers)
78
- - Incremental usage aggregation — only processes new messages since last cursor
79
- - Sidebar token units are adaptive (`k`/`m` with one decimal where applicable)
80
-
81
- ## Storage layout
82
-
83
- The plugin stores lightweight global state and date-partitioned session chunks.
84
-
85
- - Global metadata: `<opencode-data>/quota-sidebar.state.json`
86
- - `titleEnabled`
87
- - `sessionDateMap` (sessionID -> `YYYY-MM-DD`)
88
- - `quotaCache`
89
- - Session chunks: `<opencode-data>/quota-sidebar-sessions/YYYY/MM/DD.json`
90
- - per-session title state (`baseTitle`, `lastAppliedTitle`)
91
- - `createdAt`
92
- - `parentID` (when the session is a subagent child session)
93
- - cached usage summary used by `quota_summary`
94
- - incremental aggregation cursor
95
-
96
- Example tree:
97
-
98
- ```text
99
- ~/.local/share/opencode/
100
- quota-sidebar.state.json
101
- quota-sidebar-sessions/
102
- 2026/
103
- 02/
104
- 23.json
105
- 24.json
106
- ```
107
-
108
- Sessions older than `retentionDays` (default 730 days / 2 years) are evicted from
109
- memory on startup. Chunk files remain on disk for historical range scans.
110
-
111
- ## Compatibility
112
-
113
- - Node.js: >= 18 (for `fetch` + `AbortController`)
114
- - OpenCode: plugin SDK `@opencode-ai/plugin` ^1.2.10
115
- - OpenCode config split: if you are on `>=1.2.15`, keep this plugin in `opencode.json` and keep TUI-only keys in `tui.json`.
116
-
117
- ## Force refresh after npm update
118
-
119
- If `npm view @leo000001/opencode-quota-sidebar version` shows a newer version but OpenCode still behaves like an older release, OpenCode/Bun is usually reusing an older installed copy.
120
-
121
- Recommended recovery steps:
122
-
123
- 1. Pin the target plugin version in `opencode.json`.
124
- 2. Fully exit OpenCode.
125
- 3. Delete any cached installed copies of the plugin.
126
- 4. Start OpenCode again so it reinstalls the package.
127
- 5. Verify the actual installed `package.json` version under the plugin directory.
128
-
129
- Common install/cache locations:
130
-
131
- - `~/.cache/opencode/node_modules/@leo000001/opencode-quota-sidebar`
132
- - `~/node_modules/@leo000001/opencode-quota-sidebar`
133
-
134
- Windows PowerShell example:
135
-
136
- ```powershell
137
- Remove-Item -Recurse -Force "$HOME\.cache\opencode\node_modules\@leo000001\opencode-quota-sidebar" -ErrorAction SilentlyContinue
138
- Remove-Item -Recurse -Force "$HOME\node_modules\@leo000001\opencode-quota-sidebar" -ErrorAction SilentlyContinue
139
- ```
140
-
141
- macOS / Linux example:
142
-
143
- ```bash
144
- rm -rf ~/.cache/opencode/node_modules/@leo000001/opencode-quota-sidebar
145
- rm -rf ~/node_modules/@leo000001/opencode-quota-sidebar
146
- ```
147
-
148
- ## Optional commands
149
-
150
- You can add these command templates in `opencode.json` so you can run `/qday`, `/qweek`, `/qmonth`, `/qtoggle`:
151
-
152
- ```json
153
- {
154
- "command": {
155
- "qday": {
156
- "description": "Show today's usage and quota",
157
- "template": "Call tool quota_summary with period=day and toast=true."
158
- },
159
- "qweek": {
160
- "description": "Show this week's usage and quota",
161
- "template": "Call tool quota_summary with period=week and toast=true."
162
- },
163
- "qmonth": {
164
- "description": "Show this month's usage and quota",
165
- "template": "Call tool quota_summary with period=month and toast=true."
166
- },
167
- "qtoggle": {
168
- "description": "Toggle sidebar usage display on/off",
169
- "template": "Call tool quota_show (no arguments, it toggles)."
170
- }
171
- }
172
- }
173
- ```
174
-
175
- ## Configuration files
176
-
177
- Recommended global config:
178
-
179
- - `~/.config/opencode/quota-sidebar.config.json`
180
-
181
- Optional project overrides:
182
-
183
- - `<worktree>/quota-sidebar.config.json`
184
- - `<directory>/quota-sidebar.config.json` (when different from `worktree`)
185
- - `<worktree>/.opencode/quota-sidebar.config.json`
186
- - `<directory>/.opencode/quota-sidebar.config.json` (when different from `worktree`)
187
-
188
- Optional explicit override:
189
-
190
- - `OPENCODE_QUOTA_CONFIG=/absolute/path/to/config.json`
191
-
192
- Optional config-home override:
193
-
194
- - `OPENCODE_QUOTA_CONFIG_HOME=/absolute/path/to/config-home`
195
-
196
- Resolution order (low -> high):
197
-
198
- 1. Global config (`~/.config/opencode/...`)
199
- 2. `<worktree>/quota-sidebar.config.json`
200
- 3. `<directory>/quota-sidebar.config.json`
201
- 4. `<worktree>/.opencode/quota-sidebar.config.json`
202
- 5. `<directory>/.opencode/quota-sidebar.config.json`
203
- 6. `OPENCODE_QUOTA_CONFIG`
204
-
205
- Values are layered; later sources override earlier ones.
206
-
207
- ## Configuration
208
-
209
- If you do not provide any config file, the plugin uses the built-in defaults below.
210
-
211
- ### Built-in defaults
212
-
213
- Sidebar defaults:
214
-
215
- - `sidebar.enabled`: `true`
216
- - `sidebar.width`: `36` (clamped to `20`-`60`)
217
- - `sidebar.multilineTitle`: `true`
218
- - `sidebar.showCost`: `true`
219
- - `sidebar.showQuota`: `true`
220
- - `sidebar.wrapQuotaLines`: `true`
221
- - `sidebar.includeChildren`: `true`
222
- - `sidebar.childrenMaxDepth`: `6` (clamped to `1`-`32`)
223
- - `sidebar.childrenMaxSessions`: `128` (clamped to `0`-`2000`)
224
- - `sidebar.childrenConcurrency`: `5` (clamped to `1`-`10`)
225
-
226
- Quota defaults:
227
-
228
- - `quota.refreshMs`: `300000` (clamped to `>=30000`)
229
- - `quota.includeOpenAI`: `true`
230
- - `quota.includeCopilot`: `true`
231
- - `quota.includeAnthropic`: `true`
232
- - `quota.providers`: `{}` (per-adapter switches, for example `rightcode.enabled` or `buzz.enabled`)
233
- - `quota.refreshAccessToken`: `false`
234
- - `quota.requestTimeoutMs`: `8000` (clamped to `>=1000`)
235
-
236
- Other defaults:
237
-
238
- - `toast.durationMs`: `12000` (clamped to `>=1000`)
239
- - `retentionDays`: `730`
240
-
241
- ### Full example config
242
-
243
- ```json
244
- {
245
- "sidebar": {
246
- "enabled": true,
247
- "width": 36,
248
- "multilineTitle": true,
249
- "showCost": true,
250
- "showQuota": true,
251
- "wrapQuotaLines": true,
252
- "includeChildren": true,
253
- "childrenMaxDepth": 6,
254
- "childrenMaxSessions": 128,
255
- "childrenConcurrency": 5
256
- },
257
- "quota": {
258
- "refreshMs": 300000,
259
- "includeOpenAI": true,
260
- "includeCopilot": true,
261
- "includeAnthropic": true,
262
- "providers": {
263
- "buzz": {
264
- "enabled": true
265
- },
266
- "rightcode": {
267
- "enabled": true
268
- }
269
- },
270
- "refreshAccessToken": false,
271
- "requestTimeoutMs": 8000
272
- },
273
- "toast": {
274
- "durationMs": 12000
275
- },
276
- "retentionDays": 730
277
- }
278
- ```
279
-
280
- ### Notes
281
-
282
- - `sidebar.showCost` controls API-cost visibility in sidebar title, `quota_summary` markdown report, and toast message.
283
- - `quota_summary` follows the same reset compaction rules for short windows in its subscription section (`5h` / `1d` / `Daily` show time, long windows show date, RightCode `Exp` stays date-only).
284
- - `sidebar.width` is measured in terminal cells. CJK/emoji truncation is best-effort to avoid sidebar overflow.
285
- - `sidebar.multilineTitle` controls multi-line sidebar layout (default: `true`). Set `false` for compact single-line title.
286
- - `sidebar.wrapQuotaLines` controls quota line wrapping and continuation indentation (default: `true`).
287
- - `sidebar.includeChildren` controls whether session-scoped usage/quota includes descendant subagent sessions (default: `true`).
288
- - `sidebar.childrenMaxDepth` limits how many levels of nested subagents are traversed (default: `6`, clamped 1–32).
289
- - `sidebar.childrenMaxSessions` caps the total number of descendant sessions aggregated (default: `128`, clamped 0–2000).
290
- - `sidebar.childrenConcurrency` controls parallel fetches for descendant session messages (default: `5`, clamped 1–10).
291
- - `output` includes reasoning tokens (`output = tokens.output + tokens.reasoning`). Reasoning is not rendered as a separate line.
292
- - API cost bills reasoning tokens at the output rate (same as completion tokens).
293
- - API cost is computed from OpenCode model pricing metadata, not from `message.cost`. This keeps subscription-backed providers such as OpenAI OAuth usable for API-equivalent cost estimation even when OpenCode's measured cost is `0`.
294
- - When OpenCode exposes a long-context tier like `context_over_200k`, the plugin uses that premium rate for the whole request once `input > 200000`, matching OpenCode's current pricing schema.
295
- - `quota.providers` is the extensible per-adapter switch map.
296
- - If API Cost is `$0.00`, it usually means the model/provider has no pricing mapping in OpenCode at the moment, so equivalent API cost cannot be estimated.
297
- - Usage chunks cache both measured `cost` and computed `apiCost`. `quota_summary` (`/qday`, `/qweek`, `/qmonth`) usually reads those cached aggregates first, but a billing-cache version bump or missing/legacy API-cost data will trigger a rescan and persist refreshed values.
298
-
299
- ### Buzz provider example
300
-
301
- Buzz matching is based on the provider `baseURL`, similar to RightCode. Any OpenAI-compatible provider that points at `https://buzzai.cc` will be recognized by the Buzz adapter and rendered as a balance-only quota source.
302
-
303
- Provider options example:
304
-
305
- ```json
306
- {
307
- "id": "openai",
308
- "options": {
309
- "baseURL": "https://buzzai.cc",
310
- "apiKey": "sk-..."
311
- }
312
- }
313
- ```
314
-
315
- The adapter also tolerates `https://buzzai.cc/v1`, but `https://buzzai.cc` is the recommended example.
316
-
317
- With that setup, the sidebar/toast quota line will look like:
318
-
319
- ```text
320
- Buzz Balance CNY 10.17
321
- ```
322
-
323
- ## Rendering examples
324
-
325
- These examples show the quota block portion of the sidebar title.
326
-
327
- ### `sidebar.multilineTitle=true`
328
-
329
- 0 providers (no quota data):
330
-
331
- ```text
332
- (no quota block)
333
- ```
334
-
335
- 1 provider, 1 window (fits):
336
-
337
- ```text
338
- Copilot Monthly 78% Rst 04-01
339
- ```
340
-
341
- 1 provider, multi-window (for example OpenAI 5h + Weekly):
342
-
343
- ```text
344
- OpenAI
345
- 5h 78% Rst 05:05
346
- Weekly 73% Rst 03-12
347
- ```
348
-
349
- 1 provider, short window crossing into the next day:
350
-
351
- ```text
352
- Anthropic
353
- 5h 0% Rst 03-10 01:00
354
- Weekly 46% Rst 03-15
355
- ```
356
-
357
- 2+ providers (even if each provider is single-window):
358
-
359
- ```text
360
- OpenAI
361
- 5h 78% Rst 05:05
362
- Copilot
363
- Monthly 78% Rst 04-01
364
- ```
365
-
366
- 2+ providers mixed (multi-window + single-window):
367
-
368
- ```text
369
- OpenAI
370
- 5h 78% Rst 05:05
371
- Weekly 73% Rst 03-12
372
- Copilot
373
- Monthly 78% Rst 04-01
374
- ```
375
-
376
- 2+ providers mixed (window providers + Buzz balance):
377
-
378
- ```text
379
- OpenAI
380
- 5h 78% Rst 05:05
381
- Copilot
382
- Monthly 78% Rst 04-01
383
- Buzz Balance CNY 10.2
384
- ```
385
-
386
- Balance-style quota:
387
-
388
- ```text
389
- RC Balance $260
390
- ```
391
-
392
- Buzz balance quota:
393
-
394
- ```text
395
- Buzz Balance CNY 10.17
396
- ```
397
-
398
- Multi-detail quota (window + balance):
399
-
400
- ```text
401
- RC
402
- Daily $88.9/$60 Exp 02-27
403
- Balance $260
404
- ```
405
-
406
- Provider status / quota (examples):
407
-
408
- ```text
409
- Anthropic 5h 80%+
410
- Copilot unavailable
411
- OpenAI Remaining ?
412
- ```
413
-
414
- ### `sidebar.multilineTitle=false`
415
-
416
- Quota is rendered inline as part of a single-line title:
417
-
418
- ```text
419
- <base> | Input ... | Output ... | OpenAI 5h 78%+ | Copilot Monthly 78% | ...
420
- ```
421
-
422
- Mixed with Buzz balance:
423
-
424
- ```text
425
- <base> | Input ... | Output ... | OpenAI 5h 78%+ | Copilot Monthly 78% | Buzz Balance CNY 10.2
426
- ```
427
-
428
- `quota_summary` also supports an optional `includeChildren` flag (only effective for `period=session`) to override the config per call. For `day`/`week`/`month` periods, children are never merged — each session is counted independently.
429
-
430
- ## Billing cache behavior
431
-
432
- - Cached per-session usage stores token totals, measured `cost`, computed `apiCost`, provider breakdowns, and the incremental cursor.
433
- - Session-scoped sidebar aggregation can merge descendant subagents when `sidebar.includeChildren=true` (default). Measured `cost` stays aligned with the root session's OpenCode `message.cost`, while API-equivalent cost still includes descendant usage.
434
- - Range tools such as `/qday`, `/qweek`, and `/qmonth` do not merge children. They aggregate each session independently across the selected time window.
435
- - When API-cost logic changes, the plugin bumps an internal billing-cache version so historical range reports are recomputed with the new rules the next time they are queried.
436
-
437
- ## Debug logging
438
-
439
- Set `OPENCODE_QUOTA_DEBUG=1` to enable debug logging to stderr. This logs:
440
-
441
- - Chunk I/O operations
442
- - Auth refresh attempts and failures
443
- - Session eviction counts
444
- - Symlink write refusals
445
-
446
- ## Security & privacy notes
447
-
448
- - The plugin reads OpenCode credentials from `<opencode-data>/auth.json`.
449
- - If enabled, quota checks call external endpoints:
450
- - OpenAI Codex: `https://chatgpt.com/backend-api/wham/usage`
451
- - GitHub Copilot: `https://api.github.com/copilot_internal/user`
452
- - RightCode: `https://www.right.codes/account/summary`
453
- - Buzz: `https://buzzai.cc/v1/dashboard/billing/subscription` and `https://buzzai.cc/v1/dashboard/billing/usage`
454
- - Anthropic: `https://api.anthropic.com/api/oauth/usage`
455
- - **Screen-sharing warning**: Session titles and toasts surface usage/quota
456
- information. If you are screen-sharing or recording, consider toggling the
457
- sidebar display off (`/qtoggle` or `quota_show` tool) to avoid leaking
458
- subscription details.
459
- - State is persisted under `<opencode-data>/quota-sidebar.state.json` and
460
- `<opencode-data>/quota-sidebar-sessions/` (see Storage layout).
461
- - OpenAI OAuth token refresh is disabled by default; set
462
- `quota.refreshAccessToken=true` if you want the plugin to refresh access
463
- tokens when expired.
464
- - Anthropic quota currently uses a beta/internal-style OAuth usage endpoint and
465
- request header; response fields may change without notice.
466
- - State/chunk file writes refuse to write through symlinked targets (best-effort defense-in-depth).
467
- - The `OPENCODE_QUOTA_DATA_HOME` env var overrides the OpenCode data directory
468
- path (for testing); do not set this in production.
469
- - The `OPENCODE_QUOTA_CONFIG_HOME` env var overrides global config directory
470
- lookup (`<config-home>/opencode`).
471
- - The `OPENCODE_QUOTA_CONFIG` env var points to an explicit config file and
472
- applies as the highest-priority override.
473
-
474
- ## Contributing
475
-
476
- Contributions are welcome — especially new quota provider connectors. See [CONTRIBUTING.md](CONTRIBUTING.md) for a step-by-step guide on adding support for a new provider.
477
-
478
- ## License
479
-
480
- MIT. See `LICENSE`.
1
+ # opencode-quota-sidebar
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@leo000001/opencode-quota-sidebar.svg)](https://www.npmjs.com/package/@leo000001/opencode-quota-sidebar)
4
+ [![license](https://img.shields.io/npm/l/@leo000001/opencode-quota-sidebar.svg)](https://github.com/xihuai18/opencode-quota-sidebar/blob/main/LICENSE)
5
+
6
+ OpenCode plugin: show token usage and subscription quota in the session sidebar title.
7
+
8
+ ![Example sidebar title with usage and quota](./assets/OpenCode-Quota-Sidebar.png)
9
+
10
+ ## Install
11
+
12
+ Add the package name to `plugin` in your `opencode.json`. OpenCode uses Bun to install it automatically on startup:
13
+
14
+ ```json
15
+ {
16
+ "plugin": ["@leo000001/opencode-quota-sidebar@1.13.2"]
17
+ }
18
+ ```
19
+
20
+ Note for OpenCode `>=1.2.15`: TUI settings (`theme`/`keybinds`/`tui`) moved to `tui.json`, but plugin loading still stays in `opencode.json` (`plugin: []`).
21
+ This plugin also accepts both `config.providers` and older `provider.list` runtime shapes when discovering provider options.
22
+
23
+ If you prefer automatic upgrades, you can still use `@latest`, but pinning an exact version makes behavior easier to reproduce when debugging.
24
+
25
+ ## Development (build from source)
26
+
27
+ ```bash
28
+ npm install
29
+ npm run build
30
+ ```
31
+
32
+ Add the built file to your `opencode.json`:
33
+
34
+ ```json
35
+ {
36
+ "plugin": ["file:///ABSOLUTE/PATH/opencode-quota-sidebar/dist/index.js"]
37
+ }
38
+ ```
39
+
40
+ On Windows, use forward slashes: `"file:///D:/Lab/opencode-quota-sidebar/dist/index.js"`
41
+
42
+ ## Supported quota providers
43
+
44
+ | Provider | Endpoint | Auth | Status |
45
+ | -------------- | -------------------------------------- | --------------- | --------------------------------------- |
46
+ | OpenAI Codex | `chatgpt.com/backend-api/wham/usage` | OAuth (ChatGPT) | Multi-window (short-term + weekly) |
47
+ | GitHub Copilot | `api.github.com/copilot_internal/user` | OAuth | Monthly quota |
48
+ | RightCode | `www.right.codes/account/summary` | API key | Subscription or balance (by prefix) |
49
+ | Buzz | `buzzai.cc/v1/dashboard/billing/*` | API key | Balance only (computed from total-used) |
50
+ | Anthropic | `api.anthropic.com/api/oauth/usage` | OAuth | Multi-window (5h + weekly / plan-based) |
51
+
52
+ Want to add support for another provider (Google Antigravity, Zhipu AI, Firmware AI, etc.)? See [CONTRIBUTING.md](CONTRIBUTING.md).
53
+
54
+ ## Features
55
+
56
+ - Session title becomes multiline in sidebar:
57
+ - line 1: original session title
58
+ - line 2: Input/Output tokens
59
+ - line 3: Cache Read tokens (only if non-zero)
60
+ - line 4: Cache Write tokens (only if non-zero)
61
+ - line 5: `$X.XX as API cost` (equivalent API billing for subscription-auth providers)
62
+ - quota lines: quota text like `OpenAI 5h 80% Rst 16:20`; short windows (`5h`, `1d`, `Daily`) show `HH:MM` on same-day resets and `MM-DD HH:MM` when crossing days, while longer windows continue to show `MM-DD`
63
+ - RightCode daily quota shows `$remaining/$dailyTotal` + expiry (e.g. `RC Daily $105/$60 Exp 02-27`, without trailing percent) and also shows balance on the next indented line when available; `Exp` remains date-only
64
+ - Session-scoped usage/quota can include descendant subagent sessions (enabled by default via `sidebar.includeChildren=true`). Traversal is bounded by `childrenMaxDepth` (default 6), `childrenMaxSessions` (default 128), and `childrenConcurrency` (default 5); truncation is logged when `OPENCODE_QUOTA_DEBUG=1`. Day/week/month ranges never merge children — only session scope does.
65
+ - Toast message includes three sections: `Token Usage`, `Cost as API` (per provider), and `Quota`
66
+ - Quota snapshots are de-duplicated before rendering to avoid repeated provider lines
67
+ - Custom tools:
68
+ - `quota_summary` — generate usage report for session/day/week/month (markdown + toast)
69
+ - `quota_show` — toggle sidebar title display on/off (state persists across sessions)
70
+ - Quota connectors:
71
+ - OpenAI Codex OAuth (`/backend-api/wham/usage`)
72
+ - GitHub Copilot OAuth (`/copilot_internal/user`)
73
+ - RightCode API key (`/account/summary`)
74
+ - Buzz API key (`/v1/dashboard/billing/subscription` + `/v1/dashboard/billing/usage`)
75
+ - Anthropic Claude OAuth (`/api/oauth/usage`, with beta header)
76
+ - OpenAI OAuth quota checks auto-refresh expired access token (using refresh token)
77
+ - API key providers still show usage aggregation (quota only applies to subscription providers)
78
+ - Incremental usage aggregation — only processes new messages since last cursor
79
+ - Sidebar token units are adaptive (`k`/`m` with one decimal where applicable)
80
+
81
+ ## Storage layout
82
+
83
+ The plugin stores lightweight global state and date-partitioned session chunks.
84
+
85
+ - Global metadata: `<opencode-data>/quota-sidebar.state.json`
86
+ - `titleEnabled`
87
+ - `sessionDateMap` (sessionID -> `YYYY-MM-DD`)
88
+ - `quotaCache`
89
+ - Session chunks: `<opencode-data>/quota-sidebar-sessions/YYYY/MM/DD.json`
90
+ - per-session title state (`baseTitle`, `lastAppliedTitle`)
91
+ - `createdAt`
92
+ - `parentID` (when the session is a subagent child session)
93
+ - cached usage summary used by `quota_summary`
94
+ - incremental aggregation cursor
95
+
96
+ Example tree:
97
+
98
+ ```text
99
+ ~/.local/share/opencode/
100
+ quota-sidebar.state.json
101
+ quota-sidebar-sessions/
102
+ 2026/
103
+ 02/
104
+ 23.json
105
+ 24.json
106
+ ```
107
+
108
+ Sessions older than `retentionDays` (default 730 days / 2 years) are evicted from
109
+ memory on startup. Chunk files remain on disk for historical range scans.
110
+
111
+ ## Compatibility
112
+
113
+ - Node.js: >= 18 (for `fetch` + `AbortController`)
114
+ - OpenCode: plugin SDK `@opencode-ai/plugin` ^1.2.10
115
+ - OpenCode config split: if you are on `>=1.2.15`, keep this plugin in `opencode.json` and keep TUI-only keys in `tui.json`.
116
+
117
+ ## Force refresh after npm update
118
+
119
+ If `npm view @leo000001/opencode-quota-sidebar version` shows a newer version but OpenCode still behaves like an older release, OpenCode/Bun is usually reusing an older installed copy.
120
+
121
+ Recommended recovery steps:
122
+
123
+ 1. Pin the target plugin version in `opencode.json`.
124
+ 2. Fully exit OpenCode.
125
+ 3. Delete any cached installed copies of the plugin.
126
+ 4. Start OpenCode again so it reinstalls the package.
127
+ 5. Verify the actual installed `package.json` version under the plugin directory.
128
+
129
+ Common install/cache locations:
130
+
131
+ - `~/.cache/opencode/node_modules/@leo000001/opencode-quota-sidebar`
132
+ - `~/node_modules/@leo000001/opencode-quota-sidebar`
133
+
134
+ Windows PowerShell example:
135
+
136
+ ```powershell
137
+ Remove-Item -Recurse -Force "$HOME\.cache\opencode\node_modules\@leo000001\opencode-quota-sidebar" -ErrorAction SilentlyContinue
138
+ Remove-Item -Recurse -Force "$HOME\node_modules\@leo000001\opencode-quota-sidebar" -ErrorAction SilentlyContinue
139
+ ```
140
+
141
+ macOS / Linux example:
142
+
143
+ ```bash
144
+ rm -rf ~/.cache/opencode/node_modules/@leo000001/opencode-quota-sidebar
145
+ rm -rf ~/node_modules/@leo000001/opencode-quota-sidebar
146
+ ```
147
+
148
+ ## Optional commands
149
+
150
+ You can add these command templates in `opencode.json` so you can run `/qday`, `/qweek`, `/qmonth`, `/qtoggle`:
151
+
152
+ ```json
153
+ {
154
+ "command": {
155
+ "qday": {
156
+ "description": "Show today's usage and quota",
157
+ "template": "Call tool quota_summary with period=day and toast=true."
158
+ },
159
+ "qweek": {
160
+ "description": "Show this week's usage and quota",
161
+ "template": "Call tool quota_summary with period=week and toast=true."
162
+ },
163
+ "qmonth": {
164
+ "description": "Show this month's usage and quota",
165
+ "template": "Call tool quota_summary with period=month and toast=true."
166
+ },
167
+ "qtoggle": {
168
+ "description": "Toggle sidebar usage display on/off",
169
+ "template": "Call tool quota_show (no arguments, it toggles)."
170
+ }
171
+ }
172
+ }
173
+ ```
174
+
175
+ ## Configuration files
176
+
177
+ Recommended global config:
178
+
179
+ - `~/.config/opencode/quota-sidebar.config.json`
180
+
181
+ Optional project overrides:
182
+
183
+ - `<worktree>/quota-sidebar.config.json`
184
+ - `<directory>/quota-sidebar.config.json` (when different from `worktree`)
185
+ - `<worktree>/.opencode/quota-sidebar.config.json`
186
+ - `<directory>/.opencode/quota-sidebar.config.json` (when different from `worktree`)
187
+
188
+ Optional explicit override:
189
+
190
+ - `OPENCODE_QUOTA_CONFIG=/absolute/path/to/config.json`
191
+
192
+ Optional config-home override:
193
+
194
+ - `OPENCODE_QUOTA_CONFIG_HOME=/absolute/path/to/config-home`
195
+
196
+ Resolution order (low -> high):
197
+
198
+ 1. Global config (`~/.config/opencode/...`)
199
+ 2. `<worktree>/quota-sidebar.config.json`
200
+ 3. `<directory>/quota-sidebar.config.json`
201
+ 4. `<worktree>/.opencode/quota-sidebar.config.json`
202
+ 5. `<directory>/.opencode/quota-sidebar.config.json`
203
+ 6. `OPENCODE_QUOTA_CONFIG`
204
+
205
+ Values are layered; later sources override earlier ones.
206
+
207
+ ## Configuration
208
+
209
+ If you do not provide any config file, the plugin uses the built-in defaults below.
210
+
211
+ ### Built-in defaults
212
+
213
+ Sidebar defaults:
214
+
215
+ - `sidebar.enabled`: `true`
216
+ - `sidebar.width`: `36` (clamped to `20`-`60`)
217
+ - `sidebar.multilineTitle`: `true`
218
+ - `sidebar.showCost`: `true`
219
+ - `sidebar.showQuota`: `true`
220
+ - `sidebar.wrapQuotaLines`: `true`
221
+ - `sidebar.includeChildren`: `true`
222
+ - `sidebar.childrenMaxDepth`: `6` (clamped to `1`-`32`)
223
+ - `sidebar.childrenMaxSessions`: `128` (clamped to `0`-`2000`)
224
+ - `sidebar.childrenConcurrency`: `5` (clamped to `1`-`10`)
225
+
226
+ Quota defaults:
227
+
228
+ - `quota.refreshMs`: `300000` (clamped to `>=30000`)
229
+ - `quota.includeOpenAI`: `true`
230
+ - `quota.includeCopilot`: `true`
231
+ - `quota.includeAnthropic`: `true`
232
+ - `quota.providers`: `{}` (per-adapter switches, for example `rightcode.enabled` or `buzz.enabled`)
233
+ - `quota.refreshAccessToken`: `false`
234
+ - `quota.requestTimeoutMs`: `8000` (clamped to `>=1000`)
235
+
236
+ Other defaults:
237
+
238
+ - `toast.durationMs`: `12000` (clamped to `>=1000`)
239
+ - `retentionDays`: `730`
240
+
241
+ ### Full example config
242
+
243
+ ```json
244
+ {
245
+ "sidebar": {
246
+ "enabled": true,
247
+ "width": 36,
248
+ "multilineTitle": true,
249
+ "showCost": true,
250
+ "showQuota": true,
251
+ "wrapQuotaLines": true,
252
+ "includeChildren": true,
253
+ "childrenMaxDepth": 6,
254
+ "childrenMaxSessions": 128,
255
+ "childrenConcurrency": 5
256
+ },
257
+ "quota": {
258
+ "refreshMs": 300000,
259
+ "includeOpenAI": true,
260
+ "includeCopilot": true,
261
+ "includeAnthropic": true,
262
+ "providers": {
263
+ "buzz": {
264
+ "enabled": true
265
+ },
266
+ "rightcode": {
267
+ "enabled": true
268
+ }
269
+ },
270
+ "refreshAccessToken": false,
271
+ "requestTimeoutMs": 8000
272
+ },
273
+ "toast": {
274
+ "durationMs": 12000
275
+ },
276
+ "retentionDays": 730
277
+ }
278
+ ```
279
+
280
+ ### Notes
281
+
282
+ - `sidebar.showCost` controls API-cost visibility in sidebar title, `quota_summary` markdown report, and toast message.
283
+ - `quota_summary` follows the same reset compaction rules for short windows in its subscription section (`5h` / `1d` / `Daily` show time, long windows show date, RightCode `Exp` stays date-only).
284
+ - `sidebar.width` is measured in terminal cells. CJK/emoji truncation is best-effort to avoid sidebar overflow.
285
+ - `sidebar.multilineTitle` controls multi-line sidebar layout (default: `true`). Set `false` for compact single-line title.
286
+ - `sidebar.wrapQuotaLines` controls quota line wrapping and continuation indentation (default: `true`).
287
+ - `sidebar.includeChildren` controls whether session-scoped usage/quota includes descendant subagent sessions (default: `true`).
288
+ - `sidebar.childrenMaxDepth` limits how many levels of nested subagents are traversed (default: `6`, clamped 1–32).
289
+ - `sidebar.childrenMaxSessions` caps the total number of descendant sessions aggregated (default: `128`, clamped 0–2000).
290
+ - `sidebar.childrenConcurrency` controls parallel fetches for descendant session messages (default: `5`, clamped 1–10).
291
+ - `output` includes reasoning tokens (`output = tokens.output + tokens.reasoning`). Reasoning is not rendered as a separate line.
292
+ - API cost bills reasoning tokens at the output rate (same as completion tokens).
293
+ - API cost is computed from OpenCode model pricing metadata, not from `message.cost`. This keeps subscription-backed providers such as OpenAI OAuth usable for API-equivalent cost estimation even when OpenCode's measured cost is `0`.
294
+ - When OpenCode exposes a long-context tier like `context_over_200k`, the plugin uses that premium rate for the whole request once `input > 200000`, matching OpenCode's current pricing schema.
295
+ - `quota.providers` is the extensible per-adapter switch map.
296
+ - If API Cost is `$0.00`, it usually means the model/provider has no pricing mapping in OpenCode at the moment, so equivalent API cost cannot be estimated.
297
+ - Usage chunks cache both measured `cost` and computed `apiCost`. `quota_summary` (`/qday`, `/qweek`, `/qmonth`) usually reads those cached aggregates first, but a billing-cache version bump or missing/legacy API-cost data will trigger a rescan and persist refreshed values.
298
+
299
+ ### Buzz provider example
300
+
301
+ Buzz matching is based on the provider `baseURL`, similar to RightCode. Any OpenAI-compatible provider that points at `https://buzzai.cc` will be recognized by the Buzz adapter and rendered as a balance-only quota source.
302
+
303
+ Provider options example:
304
+
305
+ ```json
306
+ {
307
+ "id": "openai",
308
+ "options": {
309
+ "baseURL": "https://buzzai.cc",
310
+ "apiKey": "sk-..."
311
+ }
312
+ }
313
+ ```
314
+
315
+ The adapter also tolerates `https://buzzai.cc/v1`, but `https://buzzai.cc` is the recommended example.
316
+
317
+ With that setup, the sidebar/toast quota line will look like:
318
+
319
+ ```text
320
+ Buzz Balance CNY 10.17
321
+ ```
322
+
323
+ ## Rendering examples
324
+
325
+ These examples show the quota block portion of the sidebar title.
326
+
327
+ ### `sidebar.multilineTitle=true`
328
+
329
+ 0 providers (no quota data):
330
+
331
+ ```text
332
+ (no quota block)
333
+ ```
334
+
335
+ 1 provider, 1 window (fits):
336
+
337
+ ```text
338
+ Copilot Monthly 78% Rst 04-01
339
+ ```
340
+
341
+ 1 provider, multi-window (for example OpenAI 5h + Weekly):
342
+
343
+ ```text
344
+ OpenAI
345
+ 5h 78% Rst 05:05
346
+ Weekly 73% Rst 03-12
347
+ ```
348
+
349
+ 1 provider, short window crossing into the next day:
350
+
351
+ ```text
352
+ Anthropic
353
+ 5h 0% Rst 03-10 01:00
354
+ Weekly 46% Rst 03-15
355
+ ```
356
+
357
+ 2+ providers (even if each provider is single-window):
358
+
359
+ ```text
360
+ OpenAI
361
+ 5h 78% Rst 05:05
362
+ Copilot
363
+ Monthly 78% Rst 04-01
364
+ ```
365
+
366
+ 2+ providers mixed (multi-window + single-window):
367
+
368
+ ```text
369
+ OpenAI
370
+ 5h 78% Rst 05:05
371
+ Weekly 73% Rst 03-12
372
+ Copilot
373
+ Monthly 78% Rst 04-01
374
+ ```
375
+
376
+ 2+ providers mixed (window providers + Buzz balance):
377
+
378
+ ```text
379
+ OpenAI
380
+ 5h 78% Rst 05:05
381
+ Copilot
382
+ Monthly 78% Rst 04-01
383
+ Buzz Balance CNY 10.2
384
+ ```
385
+
386
+ Balance-style quota:
387
+
388
+ ```text
389
+ RC Balance $260
390
+ ```
391
+
392
+ Buzz balance quota:
393
+
394
+ ```text
395
+ Buzz Balance CNY 10.17
396
+ ```
397
+
398
+ Multi-detail quota (window + balance):
399
+
400
+ ```text
401
+ RC
402
+ Daily $88.9/$60 Exp 02-27
403
+ Balance $260
404
+ ```
405
+
406
+ Provider status / quota (examples):
407
+
408
+ ```text
409
+ Anthropic 5h 80%+
410
+ Copilot unavailable
411
+ OpenAI Remaining ?
412
+ ```
413
+
414
+ ### `sidebar.multilineTitle=false`
415
+
416
+ Quota is rendered inline as part of a single-line title:
417
+
418
+ ```text
419
+ <base> | Input ... | Output ... | OpenAI 5h 78%+ | Copilot Monthly 78% | ...
420
+ ```
421
+
422
+ Mixed with Buzz balance:
423
+
424
+ ```text
425
+ <base> | Input ... | Output ... | OpenAI 5h 78%+ | Copilot Monthly 78% | Buzz Balance CNY 10.2
426
+ ```
427
+
428
+ `quota_summary` also supports an optional `includeChildren` flag (only effective for `period=session`) to override the config per call. For `day`/`week`/`month` periods, children are never merged — each session is counted independently.
429
+
430
+ ## Billing cache behavior
431
+
432
+ - Cached per-session usage stores token totals, measured `cost`, computed `apiCost`, provider breakdowns, and the incremental cursor.
433
+ - Session-scoped sidebar aggregation can merge descendant subagents when `sidebar.includeChildren=true` (default). Measured `cost` stays aligned with the root session's OpenCode `message.cost`, while API-equivalent cost still includes descendant usage.
434
+ - Range tools such as `/qday`, `/qweek`, and `/qmonth` do not merge children. They aggregate each session independently across the selected time window.
435
+ - When API-cost logic changes, the plugin bumps an internal billing-cache version so historical range reports are recomputed with the new rules the next time they are queried.
436
+
437
+ ## Debug logging
438
+
439
+ Set `OPENCODE_QUOTA_DEBUG=1` to enable debug logging to stderr. This logs:
440
+
441
+ - Chunk I/O operations
442
+ - Auth refresh attempts and failures
443
+ - Session eviction counts
444
+ - Symlink write refusals
445
+
446
+ ## Security & privacy notes
447
+
448
+ - The plugin reads OpenCode credentials from `<opencode-data>/auth.json`.
449
+ - If enabled, quota checks call external endpoints:
450
+ - OpenAI Codex: `https://chatgpt.com/backend-api/wham/usage`
451
+ - GitHub Copilot: `https://api.github.com/copilot_internal/user`
452
+ - RightCode: `https://www.right.codes/account/summary`
453
+ - Buzz: `https://buzzai.cc/v1/dashboard/billing/subscription` and `https://buzzai.cc/v1/dashboard/billing/usage`
454
+ - Anthropic: `https://api.anthropic.com/api/oauth/usage`
455
+ - **Screen-sharing warning**: Session titles and toasts surface usage/quota
456
+ information. If you are screen-sharing or recording, consider toggling the
457
+ sidebar display off (`/qtoggle` or `quota_show` tool) to avoid leaking
458
+ subscription details.
459
+ - State is persisted under `<opencode-data>/quota-sidebar.state.json` and
460
+ `<opencode-data>/quota-sidebar-sessions/` (see Storage layout).
461
+ - OpenAI OAuth token refresh is disabled by default; set
462
+ `quota.refreshAccessToken=true` if you want the plugin to refresh access
463
+ tokens when expired.
464
+ - Anthropic quota currently uses a beta/internal-style OAuth usage endpoint and
465
+ request header; response fields may change without notice.
466
+ - State/chunk file writes refuse to write through symlinked targets (best-effort defense-in-depth).
467
+ - The `OPENCODE_QUOTA_DATA_HOME` env var overrides the OpenCode data directory
468
+ path (for testing); do not set this in production.
469
+ - The `OPENCODE_QUOTA_CONFIG_HOME` env var overrides global config directory
470
+ lookup (`<config-home>/opencode`).
471
+ - The `OPENCODE_QUOTA_CONFIG` env var points to an explicit config file and
472
+ applies as the highest-priority override.
473
+
474
+ ## Contributing
475
+
476
+ Contributions are welcome — especially new quota provider connectors. See [CONTRIBUTING.md](CONTRIBUTING.md) for a step-by-step guide on adding support for a new provider.
477
+
478
+ ## License
479
+
480
+ MIT. See `LICENSE`.
package/dist/cost.js CHANGED
@@ -79,24 +79,17 @@ export function guessModelCostDivisor(rates) {
79
79
  : MODEL_COST_DIVISOR_PER_TOKEN;
80
80
  }
81
81
  export function calcEquivalentApiCostForMessage(message, rates) {
82
- const info = message;
83
82
  const effectiveRates = message.tokens.input > 200_000 && rates.contextOver200k
84
83
  ? rates.contextOver200k
85
84
  : rates;
86
- const serviceTier = info.providerMetadata?.openai?.serviceTier ??
87
- info.providerMetadata?.openai?.service_tier;
88
- const priorityMultiplier = message.providerID === 'openai' && serviceTier === 'priority'
89
- ? 2
90
- : 1;
91
85
  // For providers that expose reasoning tokens separately, they are still
92
86
  // billed as output/completion tokens (same unit price). Our UI also merges
93
87
  // reasoning into the single Output statistic, so API cost should match that.
94
88
  const billedOutput = message.tokens.output + message.tokens.reasoning;
95
- const rawCost = (message.tokens.input * effectiveRates.input +
89
+ const rawCost = message.tokens.input * effectiveRates.input +
96
90
  billedOutput * effectiveRates.output +
97
91
  message.tokens.cache.read * effectiveRates.cacheRead +
98
- message.tokens.cache.write * effectiveRates.cacheWrite) *
99
- priorityMultiplier;
92
+ message.tokens.cache.write * effectiveRates.cacheWrite;
100
93
  const divisor = guessModelCostDivisor(effectiveRates);
101
94
  const normalized = rawCost / divisor;
102
95
  return Number.isFinite(normalized) && normalized > 0 ? normalized : 0;
@@ -143,34 +143,12 @@ export function createUsageService(deps) {
143
143
  tokens,
144
144
  };
145
145
  };
146
- const extractProviderMetadata = (parts) => {
147
- if (!Array.isArray(parts))
148
- return undefined;
149
- for (const part of parts) {
150
- if (!isRecord(part))
151
- continue;
152
- const meta = part.metadata;
153
- if (isRecord(meta))
154
- return meta;
155
- const stateMeta = isRecord(part.state)
156
- ? part.state?.metadata
157
- : undefined;
158
- if (isRecord(stateMeta))
159
- return stateMeta;
160
- }
161
- return undefined;
162
- };
163
146
  const decodeMessageEntry = (value) => {
164
147
  if (!isRecord(value))
165
148
  return undefined;
166
149
  const decoded = decodeMessageInfo(value.info);
167
150
  if (!decoded)
168
151
  return undefined;
169
- const metadata = extractProviderMetadata(value.parts);
170
- if (metadata && decoded.role === 'assistant') {
171
- const msg = decoded;
172
- msg.providerMetadata = metadata;
173
- }
174
152
  return { info: decoded };
175
153
  };
176
154
  const decodeMessageEntries = (value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leo000001/opencode-quota-sidebar",
3
- "version": "1.13.7",
3
+ "version": "1.13.10",
4
4
  "description": "OpenCode plugin that shows quota and token usage in session titles",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",