@chenlongapps/opencode-token-usage 0.2.4 → 0.2.5
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 +2 -0
- package/README.zh-CN.md +2 -0
- package/ROADMAP.md +14 -0
- package/dist/usage.d.ts +1 -0
- package/dist/usage.js +10 -2
- package/docs/verification.md +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,7 @@ For remote sessions, add the package name to `plugins` in your local `~/.config/
|
|
|
49
49
|
| `Cache Rate` | `Cache Read ÷ (Input + Cache Read + Cache Write)` |
|
|
50
50
|
| `Total` | Sum of all five token categories |
|
|
51
51
|
| `Context` | Latest context usage after the most recent completed compaction in the viewed session; not aggregated across the subtree |
|
|
52
|
+
| `Steps` | Assistant message count across the session tree, including subagents; follows OpenCode's own stats definition, so compaction and user messages are not steps |
|
|
52
53
|
| `Cost` | Estimated cost for the entire tree, recalculated using the viewed session's active model; not a provider bill |
|
|
53
54
|
| `TPS` | Generation throughput for `Output + Reasoning` across the tree; live estimates are marked with `~` |
|
|
54
55
|
| `TTFT` | Average time to first token across measurable assistant steps in the tree |
|
|
@@ -58,6 +59,7 @@ For remote sessions, add the package name to `plugins` in your local `~/.config/
|
|
|
58
59
|
- Usage includes server-reported assistant and compaction messages throughout the session tree, including unopened subagents. Token counts are never inferred from text length.
|
|
59
60
|
- A fork is a separate session tree. Inherited message copies are attributed only to their original source to prevent double counting.
|
|
60
61
|
- `Context` only searches messages after the most recent compaction with `status === "completed"` and is hidden when reliable usage or a model context limit is unavailable.
|
|
62
|
+
- `Steps` counts every assistant message in the tree, whether or not it reported usage, and reuses the fork-copy de-duplication so inherited history is never counted twice.
|
|
61
63
|
- `Cost` applies the viewed session's active model to the whole tree. Missing applicable prices fall back to 0, matching OpenCode's behavior.
|
|
62
64
|
- Initial read failures display `Unavailable`. Later failures retain the last complete snapshot, display `Not updated`, and retry automatically.
|
|
63
65
|
|
package/README.zh-CN.md
CHANGED
|
@@ -49,6 +49,7 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
49
49
|
| `Cache Rate` | `Cache Read ÷ (Input + Cache Read + Cache Write)` |
|
|
50
50
|
| `Total` | 五类 token 之和 |
|
|
51
51
|
| `Context` | 当前会话最后一次已完成压缩后的最新上下文用量,不随子树累计 |
|
|
52
|
+
| `Steps` | 全树(含子代理)的 assistant 消息数,口径与 OpenCode 自带统计一致,compaction 与用户消息不计为 step |
|
|
52
53
|
| `Cost` | 按当前会话活动模型重新估算的全树费用,不代表提供商账单 |
|
|
53
54
|
| `TPS` | 全树 `Output + Reasoning` 的生成速度;生成中的估算值带 `~` 标记 |
|
|
54
55
|
| `TTFT` | 全树中可测 assistant step 的平均首 token 时间 |
|
|
@@ -58,6 +59,7 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
58
59
|
- 用量覆盖会话树中服务端上报的 assistant 与 compaction 消息,包括未打开的子代理;不按文本长度估算 token。
|
|
59
60
|
- 分叉会话是独立会话树。继承消息副本只归原始来源,避免重复计费。
|
|
60
61
|
- `Context` 仅搜索最后一次 `status === "completed"` 的 compaction 之后;没有可靠用量或模型上限时隐藏。
|
|
62
|
+
- `Steps` 统计树中全部 assistant 消息,无论是否已上报用量;复用分叉副本去重,继承历史不会重复计数。
|
|
61
63
|
- `Cost` 使用当前查看会话的活动模型重算整棵树;缺失适用价格时按 OpenCode 行为回退为 0。
|
|
62
64
|
- 首次读取失败显示 `Unavailable`;后续失败保留上次完整快照、标注 `Not updated` 并自动重试。
|
|
63
65
|
|
package/ROADMAP.md
CHANGED
|
@@ -54,6 +54,20 @@ TPS 48.7 tok/s
|
|
|
54
54
|
TTFT 2.7s
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
## v0.2.5 — Steps 计数
|
|
58
|
+
|
|
59
|
+
目标:让整棵树的模型调用次数可见。
|
|
60
|
+
|
|
61
|
+
* [x] 显示全树(含子代理)的 `Steps` 计数
|
|
62
|
+
* [x] 口径与 OpenCode 官方统计一致:每条 assistant 消息为一个 step,compaction 与用户消息不计
|
|
63
|
+
* [x] 分叉继承副本不重复计数
|
|
64
|
+
* [x] 行位置为 Context 之后、Input 之前;无 Context 时位于面板第一行
|
|
65
|
+
* [x] 实际打包产物在 OpenCode 2.0.11 中验证显示与刷新
|
|
66
|
+
|
|
67
|
+
```text
|
|
68
|
+
Steps 1,234
|
|
69
|
+
```
|
|
70
|
+
|
|
57
71
|
## v0.3.0 — 公开价格目录
|
|
58
72
|
|
|
59
73
|
目标:提供可查询的价格目录,便于用户了解不同模型的计费档位。
|
package/dist/usage.d.ts
CHANGED
package/dist/usage.js
CHANGED
|
@@ -22,8 +22,13 @@ export function estimate(tokens, prices = []) {
|
|
|
22
22
|
export function summarize(messages, prices = []) {
|
|
23
23
|
const tokens = normalize();
|
|
24
24
|
let cost = 0;
|
|
25
|
+
let steps = 0;
|
|
25
26
|
let defaultPrice = prices.length === 0;
|
|
26
27
|
for (const message of messages) {
|
|
28
|
+
// Matches OpenCode SessionStats: every assistant message is one step,
|
|
29
|
+
// with or without reported usage; compaction and user messages are not.
|
|
30
|
+
if (message.type === "assistant")
|
|
31
|
+
steps++;
|
|
27
32
|
if ((message.type !== "assistant" && message.type !== "compaction") || !message.tokens)
|
|
28
33
|
continue;
|
|
29
34
|
const t = normalize(message.tokens);
|
|
@@ -36,7 +41,10 @@ export function summarize(messages, prices = []) {
|
|
|
36
41
|
cost += value.cost;
|
|
37
42
|
defaultPrice ||= value.defaultPrice;
|
|
38
43
|
}
|
|
39
|
-
return {
|
|
44
|
+
return {
|
|
45
|
+
tokens, total: total(tokens), cacheRate: incoming(tokens) ? tokens.cache.read / incoming(tokens) : 0,
|
|
46
|
+
steps, cost, defaultPrice,
|
|
47
|
+
};
|
|
40
48
|
}
|
|
41
49
|
const usableLimit = (value) => value !== undefined && Number.isFinite(value) && value > 0;
|
|
42
50
|
// Matches the OpenCode 2.0.10 sidebar: the last assistant message with tokens that follows
|
|
@@ -73,7 +81,7 @@ export function usageRows(summary, context, performance) {
|
|
|
73
81
|
const rows = [];
|
|
74
82
|
if (context)
|
|
75
83
|
rows.push(["Context", `${formatTokens(context.used)} / ${formatTokens(context.limit)} (${context.percent.toFixed(1)}%)`]);
|
|
76
|
-
rows.push(["Input", number(t?.input)], ["Output", number(t?.output)], ["Reasoning", number(t?.reasoning)], ["Cache Read", number(t?.cache.read)]);
|
|
84
|
+
rows.push(["Steps", number(summary?.steps)], ["Input", number(t?.input)], ["Output", number(t?.output)], ["Reasoning", number(t?.reasoning)], ["Cache Read", number(t?.cache.read)]);
|
|
77
85
|
if (t && t.cache.write > 0)
|
|
78
86
|
rows.push(["Cache Write", number(t.cache.write)]);
|
|
79
87
|
rows.push(["Cache Rate", summary ? `${(summary.cacheRate * 100).toFixed(1)}%` : "—"], ["Total", number(summary?.total)]);
|
package/docs/verification.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
| 检查 | 结果 |
|
|
10
10
|
| --- | --- |
|
|
11
11
|
| `npm run typecheck` | 通过 |
|
|
12
|
-
| `npm test` |
|
|
12
|
+
| `npm test` | 32 项通过 |
|
|
13
13
|
| `npm run build` | 通过,输出 ESM 与类型声明 |
|
|
14
14
|
| `npm run test:smoke` | 通过,使用实际 `.tgz` 安装产物和真实 OpenCode 2.0.11 |
|
|
15
15
|
|
|
@@ -17,9 +17,11 @@ SDK 从 2.0.9 升级到 2.0.11 后,四个顶层 OpenCode 包及锁文件中的
|
|
|
17
17
|
|
|
18
18
|
新增共享性能监视器测试覆盖:查看会话 A 时捕获独立会话 B 的流,切换到 B 后立即恢复实时 TPS/TTFT;生成中切走、继续接收 delta 再切回;控制器销毁重建后恢复共享流;不同会话树隔离、新子代理即时纳入,以及删除会话清理运行期状态。既有重复事件去重与完成后收敛为精确 TPS 的断言继续通过。
|
|
19
19
|
|
|
20
|
+
新增 Steps 计数覆盖:`summarize` 对带与不带 `tokens` 的 assistant 消息各计一个 step、compaction 与 user 消息不计、无 summary 时显示 `—`;`uniqueMessages` 链路上分叉继承副本不重复计数(快照 2 个 step、独立分叉树 1 个 step)、compaction 不产生 step、孙会话视图与根视图同为 6 个 step;行序断言锁定 Context → Steps → Input,无 Context 时 Steps 位于面板第一行。基准行数由 6 行调整为 7 行。
|
|
21
|
+
|
|
20
22
|
### 真实 OpenCode 集成
|
|
21
23
|
|
|
22
|
-
隔离 smoke
|
|
24
|
+
隔离 smoke 测试确认打包插件可加载;空会话隐藏无数据行并显示 `Steps 0`;在独立会话已经开始慢速流后,同一 TUI 切换过去会立即显示 `TPS ~…` 与 TTFT,完成后变为无 `~` 的精确 TPS;既有 token、上下文与费用刷新、真实子代理全树累计、会话局部上下文及模型切换验证均继续通过。Steps 在真实集成中断言为:首条 assistant 完成后 `Steps 1` 且行序位于 Context 与 Input 之间;真实子代理场景下根视图与子代理视图均为 `Steps 4`(父 3 条 + 子 1 条),而被查看会话自身仍为 1,确认 Steps 随整棵树累计、上下文保持会话局部。测试产物保存在 `/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/token-usage-smoke-423PQP`。
|
|
23
25
|
|
|
24
26
|
### 验证边界
|
|
25
27
|
|