opencode-token-tracker 1.6.4 → 1.7.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/README.md CHANGED
@@ -23,6 +23,14 @@ This project uses the [AI Engineering Framework (AIEF)](https://github.com/tongs
23
23
 
24
24
  If you are building AI-assisted engineering workflows, we strongly recommend adopting AIEF in your own repositories for clearer context management and more consistent agent outputs.
25
25
 
26
+
27
+ ## Accuracy & Limitations
28
+
29
+ - **Costs are estimates**, computed locally from your token logs and the built-in (or user-configured) pricing table. They may differ from your provider's official invoice — for example, when promotional credits, discounts, or enterprise pricing structures apply.
30
+ - **Budgets are warnings, not enforcement.** This plugin does not block API calls, throttle requests, or interrupt active sessions. It is designed purely as an observability and tracking tool.
31
+ - **Subscription or bundled providers** (such as GitHub Copilot, Cursor, etc.) or free local models should be configured with zero-cost overrides in your configuration file (see [Configuration](#configuration)).
32
+ - **Pricing freshness**: The built-in pricing table is manually maintained. Please run `opencode-tokens models` to inspect which of your used models currently fall back to the default pricing, and configure overrides if necessary.
33
+
26
34
  ## Installation
27
35
 
28
36
  Add to your OpenCode config file (`~/.config/opencode/opencode.json`):
@@ -38,6 +46,8 @@ Restart OpenCode and the plugin will be automatically installed.
38
46
 
39
47
  ## Usage
40
48
 
49
+ For an end-to-end setup and verification path, see [walkthrough.md](./walkthrough.md).
50
+
41
51
  ### Toast Notifications
42
52
 
43
53
  Once installed, you'll see Toast notifications after each AI response:
@@ -237,8 +247,12 @@ opencode-tokens models
237
247
  # Show current config
238
248
  opencode-tokens config
239
249
 
240
- # Generate example config based on your usage
250
+ # Print clean example JSON to stdout without writing a file
241
251
  opencode-tokens config init
252
+
253
+ # Write example config to ~/.config/opencode/token-tracker.json
254
+ # Existing config is backed up to token-tracker.json.bak
255
+ opencode-tokens config generate
242
256
  ```
243
257
 
244
258
  Example `models` output:
@@ -255,6 +269,8 @@ This helps you understand:
255
269
  - Whether pricing is from built-in table, your config, or default fallback
256
270
  - What to add to your config file
257
271
 
272
+ `config init` is safe for piping because stdout contains only valid JSON and no file is written. `config generate` is the file-writing path: stdout stays empty, guides and status messages go to stderr, the parent directory is created when needed, and an existing config is backed up before overwrite.
273
+
258
274
  ## Log Files
259
275
 
260
276
  Token usage is logged to:
@@ -367,11 +383,14 @@ All prices are in **USD per 1 million tokens**:
367
383
 
368
384
  Pricing is resolved in this order (first match wins):
369
385
 
370
- 1. **Provider-level** - Override all models for a provider
371
- 2. **Provider-specific model config** - Custom pricing for the same model under different providers
372
- 3. **User model config** - Generic custom model pricing in config file
373
- 4. **Built-in pricing** - Default pricing table
374
- 5. **Fallback** - $1/M input, $4/M output
386
+ 1. **Provider-level override** - Override all models for a provider
387
+ 2. **Exact user model config** - Custom pricing for a specific model or provider-specific model entry
388
+ 3. **Built-in exact match** - Exact key in the built-in pricing table
389
+ 4. **Built-in partial match** - Longest matching built-in key for variant model names
390
+ 5. **User model partial match** - Longest matching user config key
391
+ 6. **Fallback** - $1/M input, $4/M output
392
+
393
+ Exact user config is intentionally checked before built-ins, while broad partial user keys are checked after built-ins so a generic key like `"claude"` does not accidentally override a precise built-in model price.
375
394
 
376
395
  #### Example: Free providers
377
396
 
@@ -437,12 +456,15 @@ npm install
437
456
  # Build
438
457
  npm run build
439
458
 
440
- # Link for local testing
441
- npm link
442
- cd ~/.config/opencode
443
- npm link opencode-token-tracker
459
+ # Unit and CLI tests
460
+ npm test
461
+
462
+ # Real local OpenCode CLI dogfood
463
+ node scripts/real-opencode-cli-smoke.mjs --use-temporary-link --model deepseek/deepseek-chat
444
464
  ```
445
465
 
466
+ The dogfood script is repo-only and is not published as an npm command. It verifies the real local `opencode run` path, including OpenCode's cache package directory, and restores any temporary package links after the run.
467
+
446
468
  ## License
447
469
 
448
470
  MIT © [tongsh6](https://github.com/tongsh6)
package/README.zh-CN.md CHANGED
@@ -23,6 +23,14 @@
23
23
 
24
24
  如果你在构建 AI-assisted engineering 工作流,推荐在你的仓库中采用 AIEF,以获得更清晰的上下文管理与更稳定的 agent 输出。
25
25
 
26
+
27
+ ## 准确性与限制
28
+
29
+ - **成本均为估算值**,由本地 Token 日志及内置(或用户配置)的定价表计算得出。这可能与您的 Provider 官方账单存在差异 —— 例如,当您使用促销额度、企业折扣或特定定价优惠时。
30
+ - **预算提醒仅为警告,非强制阻断**。本插件不会拦截 API 调用、节流请求或中断您的会话。其设计初衷纯粹是为了可观测性与用量追踪。
31
+ - **订阅制或打包服务商**(如 GitHub Copilot、Cursor 等)或免费的本地模型,应在您的配置文件中将其价格覆写为 0(参见 [配置说明](#配置说明))。
32
+ - **定价数据时效性**:内置的定价表为手动维护。请运行 `opencode-tokens models` 来检查您当前使用的哪些模型退化(fallback)到了默认定价,并在需要时手动配置覆写。
33
+
26
34
  ## 安装
27
35
 
28
36
  在 OpenCode 配置文件 `~/.config/opencode/opencode.json` 中添加插件:
@@ -38,6 +46,8 @@
38
46
 
39
47
  ## 使用
40
48
 
49
+ 端到端安装、验证与 dogfood 路径见 [walkthrough.md](./walkthrough.md)。
50
+
41
51
  ### Toast 提示
42
52
 
43
53
  安装后,每次 AI 响应会看到类似提示:
@@ -152,6 +162,81 @@ opencode-tokens --by daily
152
162
  gpt-5.2 86.9K $0.0000 18
153
163
  ```
154
164
 
165
+ `--by` 可选:
166
+ - `model`:按模型分组
167
+ - `agent`:按 agent 分组
168
+ - `provider`:按 provider 分组
169
+ - `daily`:按天分组
170
+ - `session`:按 session ID 分组
171
+ - `all`:显示全部分组
172
+
173
+ ### 趋势图
174
+
175
+ 查看每日 token、成本或消息数趋势:
176
+
177
+ ```bash
178
+ # 30 天成本趋势(默认)
179
+ opencode-tokens trend
180
+
181
+ # 7 天 token 趋势
182
+ opencode-tokens trend --days 7 --metric tokens
183
+
184
+ # 更紧凑的图表
185
+ opencode-tokens trend --width 40
186
+ ```
187
+
188
+ 可选参数:
189
+ - `--days N`:统计天数,默认 30
190
+ - `--metric`:`cost`、`tokens` 或 `messages`,默认 `cost`
191
+ - `--width W`:图表宽度,默认 60
192
+
193
+ ### 数据导出
194
+
195
+ 导出 JSONL 聚合后的用量数据:
196
+
197
+ ```bash
198
+ # 导出全部数据为 CSV
199
+ opencode-tokens export
200
+
201
+ # 导出本月数据为 JSON
202
+ opencode-tokens export --format json --period month
203
+
204
+ # 写入文件
205
+ opencode-tokens export --format csv --output usage.csv
206
+ ```
207
+
208
+ 可选参数:
209
+ - `--format`:`csv` 或 `json`,默认 `csv`
210
+ - `--period`:`today`、`week`、`month`、`all`,默认 `all`
211
+ - `--output FILE`:写入文件,而不是 stdout
212
+
213
+ ### 配置管理
214
+
215
+ 可直接通过 CLI 管理预算与 Toast 配置:
216
+
217
+ ```bash
218
+ # 查看当前配置
219
+ opencode-tokens config
220
+
221
+ # 设置每日预算为 $10
222
+ opencode-tokens config set budget.daily 10
223
+
224
+ # 关闭 Toast
225
+ opencode-tokens config set toast.enabled false
226
+
227
+ # 查看某个值
228
+ opencode-tokens config get budget.warnAt
229
+
230
+ # 恢复默认
231
+ opencode-tokens config unset budget.daily
232
+ ```
233
+
234
+ 可设置字段:
235
+ - `budget.daily`、`budget.weekly`、`budget.monthly`、`budget.warnAt`
236
+ - `toast.enabled`、`toast.duration`、`toast.showOnIdle`
237
+
238
+ 配置变更会自动备份到 `token-tracker.json.bak`。
239
+
155
240
  ### 定价与配置命令
156
241
 
157
242
  ```bash
@@ -167,8 +252,12 @@ opencode-tokens models
167
252
  # 查看当前配置
168
253
  opencode-tokens config
169
254
 
170
- # 基于当前使用情况生成示例配置
255
+ # 输出干净 JSON 到 stdout,不写文件
171
256
  opencode-tokens config init
257
+
258
+ # 写入 ~/.config/opencode/token-tracker.json
259
+ # 已有配置会备份到 token-tracker.json.bak
260
+ opencode-tokens config generate
172
261
  ```
173
262
 
174
263
  `models` 示例输出:
@@ -186,6 +275,8 @@ opencode-tokens config init
186
275
  - 定价来源是内置表、用户配置还是默认回退
187
276
  - 需要在配置文件中补充哪些模型定价
188
277
 
278
+ `config init` 适合管道重定向,因为 stdout 只包含合法 JSON,且不会写文件。`config generate` 是写文件路径:stdout 保持为空,说明和状态信息输出到 stderr;父目录不存在时会自动创建,覆盖已有配置前会先备份。
279
+
189
280
  ## 日志文件
190
281
 
191
282
  token 记录保存在:
@@ -285,10 +376,13 @@ token 记录保存在:
285
376
  定价解析顺序(命中即止):
286
377
 
287
378
  1. **Provider 覆盖** — 为某个 provider 的所有模型统一设置
288
- 2. **按 provider 的 model 配置** — 同一模型在不同 provider 下使用不同定价
289
- 3. **用户 model 配置** — 为特定模型自定义通用定价
290
- 4. **内置定价** — 默认定价表
291
- 5. **默认回退** — $1/M input,$4/M output
379
+ 2. **用户 model 精确匹配** — 为特定模型或 provider-specific model entry 自定义定价
380
+ 3. **内置定价精确匹配** — 命中内置定价表中的精确 key
381
+ 4. **内置定价部分匹配** — 对变体模型名使用最长的内置 key 匹配
382
+ 5. **用户 model 部分匹配** — 使用最长的用户配置 key 匹配
383
+ 6. **默认回退** — $1/M input,$4/M output
384
+
385
+ 精确用户配置会优先于内置定价;但宽泛的用户部分匹配会排在内置匹配之后,避免 `"claude"` 这类泛 key 意外覆盖精确的内置模型价格。
292
386
 
293
387
  #### 示例:免费 provider
294
388
 
@@ -368,12 +462,15 @@ npm install
368
462
  # 构建
369
463
  npm run build
370
464
 
371
- # 本地联调
372
- npm link
373
- cd ~/.config/opencode
374
- npm link opencode-token-tracker
465
+ # 单元与 CLI 测试
466
+ npm test
467
+
468
+ # 真实本机 OpenCode CLI dogfood
469
+ node scripts/real-opencode-cli-smoke.mjs --use-temporary-link --model deepseek/deepseek-chat
375
470
  ```
376
471
 
472
+ dogfood 脚本只作为仓库内开发工具,不作为 npm 包命令发布。它验证真实本机 `opencode run` 路径,包括 OpenCode 的 cache package 目录;运行结束后会恢复临时 package link。
473
+
377
474
  ## License
378
475
 
379
476
  MIT © [tongsh6](https://github.com/tongsh6)