@chenlongapps/opencode-token-usage 0.4.23 → 0.4.25
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 +21 -10
- package/README.zh-CN.md +21 -10
- package/ROADMAP.md +4 -1
- package/dist/performance.d.ts +5 -1
- package/dist/performance.js +51 -6
- package/dist/tui.js +1 -1
- package/dist/usage.d.ts +1 -0
- package/docs/verification.md +74 -0
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ Use it to see how many tokens a session and its subagents consume, how full the
|
|
|
28
28
|
- **Estimated cost by model:** Price recorded calls using their actual models, with a built-in first-party price snapshot as a fallback. Estimates are not provider bills.
|
|
29
29
|
- **Performance at a glance:** Monitor tokens per second (TPS), time to first token (TTFT) and the viewed session's own elapsed response time. Live TPS and elapsed-time estimates are marked with `~`.
|
|
30
30
|
- **Native terminal UI:** Keep a sidebar panel in the main session and a single-line summary in subagent views; open details only when needed.
|
|
31
|
-
- **On-demand details:** Use `/usage` for compact or detailed statistics, including per-model usage and rates, without sending a prompt to the model.
|
|
31
|
+
- **On-demand details:** Use `/token-usage` for compact or detailed statistics, including per-model usage and rates, without sending a prompt to the model.
|
|
32
32
|
|
|
33
33
|
[](https://www.npmjs.com/package/@chenlongapps/opencode-token-usage)
|
|
34
34
|
|
|
@@ -47,7 +47,7 @@ Restart OpenCode after installation.
|
|
|
47
47
|
### Quick start
|
|
48
48
|
|
|
49
49
|
1. **View usage:** Open a session. The **Token Usage** panel appears in the native sidebar when `session.sidebar` is set to `auto` and the terminal is wide enough.
|
|
50
|
-
2. **Explore details:** Enter `/usage` or left-click the panel's **Token Usage** title. Press `d` to toggle compact/detailed mode and Escape to close. Opening details does not send a prompt to the model.
|
|
50
|
+
2. **Explore details:** Enter `/token-usage` or left-click the panel's **Token Usage** title. Press `d` to toggle compact/detailed mode and Escape to close. Opening details does not send a prompt to the model.
|
|
51
51
|
3. **Inspect subagents:** In subagent views, click the live summary above the composer to open the full panel. Closing it does not interrupt the subagent.
|
|
52
52
|
|
|
53
53
|
For exact definitions and edge cases, see [Detailed usage](#detailed-usage), [Metrics](#metrics) and [price sources and limitations](docs/pricing.md).
|
|
@@ -69,11 +69,17 @@ For remote sessions, add the package name to `plugins` in your local `~/.config/
|
|
|
69
69
|
|
|
70
70
|
### Compatibility
|
|
71
71
|
|
|
72
|
-
Requires Node.js 22+ (see the OpenTUI engine note under [Development](#development)).
|
|
72
|
+
Requires Node.js 22+ (see the OpenTUI engine note under [Development](#development)). With SDK **2.3.0**, the full packaged integration suite has passed on OpenCode **2.0.28** and **2.3.0**. Earlier SDK/release combinations were tested on 2.0.9–2.0.11, 2.0.22, 2.0.24, 2.0.26 and 2.1.2; those historical results do not establish compatibility with the current SDK. See the [verification record](docs/verification.md) for tested combinations.
|
|
73
|
+
|
|
74
|
+
Since **0.4.24**, the plugin uses `/token-usage` for details; `/usage` is reserved for OpenCode. Native first-token timestamps improve TTFT and historical TPS when available, with fallbacks for older hosts.
|
|
75
|
+
|
|
76
|
+
OpenCode **2.3.0 now passes the full suite**, including plugin details alongside built-in `/usage`, native performance timing, history restoration, subagents, forks and compaction. The earlier 6,350 vs 5,080 mismatch was an outdated fixture assumption: background child completion triggers an additional parent model call. Smoke now independently reconciles totals against actual mock-provider requests.
|
|
73
77
|
|
|
74
78
|
### Detailed usage
|
|
75
79
|
|
|
76
|
-
Enter `/usage` in a session or left-click the sidebar's **Token Usage** title to open the same centered native dialog with the session tree's token totals and estimated cost by recorded model. It opens in compact mode. Scroll with ↑/↓, Page Up/Down, Home/End, press `d` to switch between compact and detailed numbers and reveal model rates, and close with Escape or **esc**. Neither entry sends a prompt to the model. Other sidebar rows remain selectable; dragging to select text does not open the dialog.
|
|
80
|
+
Enter `/token-usage` in a session or left-click the sidebar's **Token Usage** title to open the same centered native dialog with the session tree's token totals and estimated cost by recorded model. It opens in compact mode. Scroll with ↑/↓, Page Up/Down, Home/End, press `d` to switch between compact and detailed numbers and reveal model rates, and close with Escape or **esc**. Neither entry sends a prompt to the model. Other sidebar rows remain selectable; dragging to select text does not open the dialog.
|
|
81
|
+
|
|
82
|
+
The plugin command was renamed from `/usage` to `/token-usage` on all supported hosts to avoid OpenCode's built-in `/usage` command. The old name is not registered as an alias. The **Token usage** command-palette entry is also available.
|
|
77
83
|
|
|
78
84
|
The subagent picker keeps the summary in the form `Token Usage · Context … · Total … · Cost … · Time … · TPS …`. Time sums the viewed subagent's own native per-turn response durations; token totals and cost still cover the whole tree. Narrow terminals omit lower-priority fields, keeping Time visible when present without adding a second line. Missing values remain unavailable rather than becoming zero, and the line does not advertise a keyboard shortcut. Clicking it opens a smaller dialog with the sidebar's exact rows (including Steps, TPS, TTFT and Run Time) without reserving space for the full panel while closed. When shown, Run Time is the last row in both the sidebar and this dialog.
|
|
79
85
|
|
|
@@ -109,7 +115,7 @@ Costs follow the sidebar's pricing and `partial`/unavailable/free conventions. I
|
|
|
109
115
|
| `Est. Cost` | Estimated cost across the tree, pricing each assistant and compaction call with its actual model |
|
|
110
116
|
| `Run Time` | The viewed session's cumulative native per-turn response duration; while running, includes a display-only estimate of the current turn's elapsed time marked with `~`; never adds child-session durations |
|
|
111
117
|
| `TPS` | Provider-reported average throughput for `Output + Reasoning` across the tree. While streaming, a valid recent observable text, reasoning-summary and tool-input estimate (UTF-8 bytes / 4) takes precedence and is marked with `~`. It requires at least 20 estimated tokens accumulated in that response, three merged batches in the current window and a span of at least 500 ms, excluding pre-first-token waiting. APIs such as OpenAI Responses do not expose complete hidden reasoning tokens in real time, so live values cannot represent hidden reasoning throughput. Completion restores provider-reported throughput without `~` |
|
|
112
|
-
| `TTFT` | Average time to first token across measurable assistant steps in the tree |
|
|
118
|
+
| `TTFT` | Average time to first token across measurable assistant steps in the tree, preferring OpenCode's native first-token timestamp |
|
|
113
119
|
|
|
114
120
|
#### Behavior
|
|
115
121
|
|
|
@@ -121,14 +127,15 @@ Costs follow the sidebar's pricing and `partial`/unavailable/free conventions. I
|
|
|
121
127
|
- While the viewed session is running, `Run Time` estimates **elapsed time so far**, not final or remaining time, and prefixes the cumulative display with `~` (for example, `~42.5s`). A shared 500 ms clock replaces the current turn's already-counted duration rather than adding an overlapping steer/retry duration. Clock ticks only update memory: history reads remain event-driven. One optional read-only server-plugin clock RPC at startup/reconnection supplies a server-time anchor, with timestamped live events as a fallback; local monotonic time advances it without assuming that a remote server shares the client's clock. Opening an already-running session restores the turn's start from history. A reconnect history refresh arriving before clock resynchronization keeps the last trusted display for the same retained open turn and unchanged historical subtotal, rather than dropping it to zero. Without a trusted anchor or valid timing, native totals remain unchanged.
|
|
122
128
|
- Completion, failure or interruption stops projection; reported native completion timing replaces the estimate and removes `~`, so a small downward correction is possible. Only native completed durations enter the historical subtotal. Totals use unrounded milliseconds before formatting (milliseconds below one second, one decimal below one minute, then minutes/hours), so they need not equal the sum of rounded footer labels. The existing message-history read restores totals after restarts, including sessions predating installation; no experimental execution-log API or server plugin is required. Missing historical timing data displays `—`, not zero. Read failures freeze the last displayed value and mark it `Not updated` (`stale` in the short summary).
|
|
123
129
|
- `Run Time` / `Time` shows the duration and its estimate/freshness markers, without running-status text or animation. An idle parent's time never advances just because a child is active. Closing or switching views cleans up their estimates, and the shared elapsed-time ticker stops when no view needs it. Unavailable/stale timing keeps its own marker.
|
|
124
|
-
- A confirmed zero `Run Time` / `Time` is hidden in the sidebar, subagent summary/dialog and both `/usage` modes, without leaving an empty row or field. A nonzero live estimate makes the first turn visible even before any response has completed. Nonzero millisecond durations, unavailable values and stale timing remain visible.
|
|
130
|
+
- A confirmed zero `Run Time` / `Time` is hidden in the sidebar, subagent summary/dialog and both `/token-usage` modes, without leaving an empty row or field. A nonzero live estimate makes the first turn visible even before any response has completed. Nonzero millisecond durations, unavailable values and stale timing remain visible.
|
|
125
131
|
- Runtime follows retained history: agent/model switches and compaction preserve past turns; fork copies never contribute to the fork's own time. A staged revert does not remove turns, but a committed revert deleting messages also removes their durations. Deleted historical timing cannot be reconstructed.
|
|
126
132
|
- `Est. Cost` prices every message with its recorded model. A complete non-zero price resolved by OpenCode takes precedence. If OpenCode reports a complete zero price, a complete first-party snapshot price overrides it; incomplete prices fall back for the whole message without mixing rates.
|
|
127
133
|
- The checked-in fallback snapshot is generated from [models.dev](https://models.dev/api.json) using only reviewed first-party provider/model families; a small set of manufacturer-verified exceptions is kept separately. It covers priced text models, without downloading prices while the plugin runs. Gateway models match exact manufacturer IDs, documented aliases, and known wrappers; only a terminal `-free` or `:free` can be removed for a second exact lookup.
|
|
128
134
|
- Confirmed free usage displays `$0.00`; positive estimates below `$0.001` display `<$0.001`, estimates through and including `$0.01` use three decimal places, and larger estimates use two. Unavailable prices display `—`; known subtotals with unpriced messages are marked `partial`. The snapshot excludes gateway markups, regional premiums, unlisted discounts, non-text billing, tool fees, and taxes, so Est. Cost is not a provider bill. See [price sources and limitations](docs/pricing.md).
|
|
129
135
|
- Live `TPS` uses a ~2s sliding window and batches deliveries within 100 ms. It starts calculating and displaying only when all three conditions hold: that response has accumulated at least 20 estimated tokens (80 UTF-8 bytes), the current window contains at least three merged batches, and those batches span at least 500 ms. The first batch counts toward the response-wide threshold but is only a speed baseline: its bytes are not counted as output generated after its timestamp. Token readiness is separate for each response, never pooled across responses or subagents. Smoothing follows elapsed time rather than delta count. Before readiness, historical TPS takes over, or the row is hidden if unavailable. After 2s without a new delta, a shared in-memory timeout expires the estimate without reading history. Resuming output after expiry starts a fresh baseline and smoothing state and must satisfy the batch/span conditions again; the same response's accumulated token count is retained.
|
|
130
136
|
- Reconnection resets live TPS windows and reconciles all tracked sessions, including offscreen sessions, with the server's active-session set. Quiet unfinished streams get a shared activity check after 60s, without reading message history. Confirmed inactive streams and their TTFT observations expire after a further 60s. Running tools, retries and pre-first-token waits have no hard lifetime; failed activity reads retain state and retry. Provider `streamed` metadata alone never means that tools have finished. Checks stop when no unfinished streams remain.
|
|
131
|
-
- Live TPS measures observable delivery, not the model's internal token-generation clock. Responses summary bursts, done-only content and transport buffering can still affect estimates; complete hidden reasoning cannot be reconstructed. Without `~`, TPS is `Σ(Output + Reasoning) ÷ Σ(time.streamed − time.created)` in seconds across valid assistant samples in the tree
|
|
137
|
+
- Live TPS measures observable delivery, not the model's internal token-generation clock. Responses summary bursts, done-only content and transport buffering can still affect estimates; complete hidden reasoning cannot be reconstructed. Without `~`, TPS is `Σ(Output + Reasoning) ÷ Σ(time.streamed − (time.firstToken ?? time.created))` in seconds across valid assistant samples in the tree. Valid native first-token timing excludes pre-first-token waiting, matching the host's per-step denominator; older hosts/history without it retain the full request-duration fallback. This is a tree-wide weighted average, not just the current turn or recent delivery window. No model-specific multipliers, hard TPS caps or hidden-reasoning guesses are applied.
|
|
138
|
+
- `TTFT` prefers native `time.firstToken − time.created` in history and `session.step.first-token` during streaming. A buffered delta cannot overwrite native timing. Older hosts fall back to the first nonempty text/reasoning/tool-input delta, or historical reasoning/tool creation times when measurable; text-only history without a native first-token timestamp has no recoverable TTFT. Native timing survives reopening and live-observation expiry. Retries reusing a message ID replace the previous attempt's sample. TTFT remains the average across measurable steps in the whole tree, whereas OpenCode's footer uses the first measurable step in the current turn, so multi-step/subagent values need not match.
|
|
132
139
|
- Initial read failures display `Unavailable`. Later failures retain the last complete snapshot, display `Not updated`, and retry automatically.
|
|
133
140
|
|
|
134
141
|
### Development
|
|
@@ -141,7 +148,9 @@ npm run build
|
|
|
141
148
|
npm run test:smoke
|
|
142
149
|
```
|
|
143
150
|
|
|
144
|
-
|
|
151
|
+
For native TTFT/TPS checks on a host with `time.firstToken`, run `npm run test:smoke -- --performance-only`. This targeted lane verifies streaming timing, the native footer and history restoration after reopening; it does not replace the full integration suite.
|
|
152
|
+
|
|
153
|
+
The OpenCode SDK packages are pinned to `2.3.0`, with OpenTUI `0.8.4` and Solid/compiler `1.9.16`. OpenTUI declares Node.js `>=26.4.0` for its native Node runtime, so npm reports `EBADENGINE` on Node 22; `engine-strict` installations require a Node version meeting that dependency's engine requirement. The plugin's TUI runs inside OpenCode, not as a standalone Node 22 renderer. The project's Node.js 22+ minimum is unchanged; see the [verification record](docs/verification.md) for checks of this dependency set.
|
|
145
154
|
|
|
146
155
|
The [price update workflow](.github/workflows/update-prices.yml) checks models.dev every 4 hours (or on demand via **Run workflow** on `main`). No snapshot diff means no release. Changes within the reviewed first-party source allowlist pass typecheck, tests, build, and pack validation, then automatically bump the patch version (for example, `0.4.4` → `0.4.5`), commit only `src/prices.generated.ts`, `package.json`, and `package-lock.json`, and atomically push `main` and its version tag. The workflow creates a GitHub Release and explicitly dispatches [`publish.yml`](.github/workflows/publish.yml) on that tag to publish via npm Trusted Publishing/OIDC; no long-lived npm token is needed. GitHub Actions must be allowed to push to `main` and create tags, and npm must trust `publish.yml` with permission to run `npm publish`.
|
|
147
156
|
|
|
@@ -149,16 +158,18 @@ Failed validation never bumps the version. An unfinished tagged price release is
|
|
|
149
158
|
|
|
150
159
|
For a manual refresh, run `npm run prices:update` in a networked environment, review the generated diff and exceptions, then run the checks above. Builds and plugin refreshes do not contact models.dev.
|
|
151
160
|
|
|
152
|
-
`test:smoke` packages the real artifact and validates loading, refreshes, `/usage`, sidebar title clicks, subagent aggregation, per-message pricing, first-party price fallback, model switching, TPS, TTFT, Responses summary bursts/done-only summaries/quiet expiry, and session-local native turn durations (unfinished responses, first-turn synthetic continuations, reopening a running TUI, terminal focus events, idle gaps, interruption, message-history restoration, forks and narrow child views) against an isolated OpenCode instance and local mock providers. It requires Python 3, an available local port, and npm network access. It never modifies your existing OpenCode configuration or calls paid models. Terminal focus checks do not establish physical macOS multi-display, App Nap or sleep/wake behavior.
|
|
161
|
+
`test:smoke` packages the real artifact and validates loading, refreshes, `/token-usage`, sidebar title clicks, subagent aggregation, per-message pricing, first-party price fallback, model switching, TPS, TTFT, Responses summary bursts/done-only summaries/quiet expiry, and session-local native turn durations (unfinished responses, first-turn synthetic continuations, reopening a running TUI, terminal focus events, idle gaps, interruption, message-history restoration, forks and narrow child views) against an isolated OpenCode instance and local mock providers. On OpenCode 2.3+, it also checks that built-in `/usage` opens the host's own dialog. It requires Python 3, an available local port, and npm network access. It never modifies your existing OpenCode configuration or calls paid models. Terminal focus checks do not establish physical macOS multi-display, App Nap or sleep/wake behavior.
|
|
153
162
|
|
|
154
163
|
Performance lifecycle checks also use the real host's active-session API, with ending events withheld from an offscreen monitor, injected reconnect handshakes and an accelerated 25-hour tool/child wait. They verify preservation while running and reclamation after completion without history reads.
|
|
155
164
|
|
|
156
165
|
By default, smoke uses `opencode` from `PATH`. Stable OpenCode 2 versions from 2.0.9 onward can run; a host not yet verified with the current SDK prints a warning and continues through the actual compatibility checks. Passing the version check alone does not establish compatibility. The SDK dependencies remain pinned independently of your local CLI updates.
|
|
157
166
|
|
|
167
|
+
CI configures full packaged smoke jobs for pinned OpenCode **2.0.28** and **2.3.0**, using isolated local mock providers and uploading diagnostic artifacts. Tree totals are checked against actual mock-provider requests: background child-completion notifications can trigger an additional parent call on newer hosts.
|
|
168
|
+
|
|
158
169
|
To reproduce a baseline with a separately installed binary, without downgrading your normal installation:
|
|
159
170
|
|
|
160
171
|
```bash
|
|
161
|
-
OPENCODE_BIN=/absolute/path/to/opencode-2.0.
|
|
172
|
+
OPENCODE_BIN=/absolute/path/to/opencode-2.0.28 npm run test:smoke
|
|
162
173
|
```
|
|
163
174
|
|
|
164
175
|
`OPENCODE_BIN` selects the same executable for the version check, isolated server, and TUI. Relative paths resolve from the directory where you run the command. The host version and selected binary are recorded in the smoke output and `result.json`; see the [verification record](docs/verification.md) for tested combinations.
|
package/README.zh-CN.md
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
- **按模型估算费用:** 按每次调用实际使用的模型计价,以内置原厂价格快照作为回退。估算费用不等于提供商账单。
|
|
29
29
|
- **性能一目了然:** 查看每秒 token 数(TPS)、首 token 时间(TTFT)及当前会话自身的对话耗时。实时 TPS 与已耗时估算以 `~` 标记。
|
|
30
30
|
- **原生终端界面:** 主会话使用侧边栏,子代理视图保留单行摘要,按需打开完整面板。
|
|
31
|
-
- **按需查看详情:** 通过 `/usage` 切换紧凑/详细统计,查看各模型的用量与费率,不向模型发送提示词。
|
|
31
|
+
- **按需查看详情:** 通过 `/token-usage` 切换紧凑/详细统计,查看各模型的用量与费率,不向模型发送提示词。
|
|
32
32
|
|
|
33
33
|
[](https://www.npmjs.com/package/@chenlongapps/opencode-token-usage)
|
|
34
34
|
|
|
@@ -47,7 +47,7 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
47
47
|
### 快速上手
|
|
48
48
|
|
|
49
49
|
1. **查看用量:** 打开会话。终端足够宽且 `session.sidebar` 设为 `auto` 时,原生侧边栏会显示 **Token Usage** 面板。
|
|
50
|
-
2. **展开详情:** 输入 `/usage`,或左键点击面板的 **Token Usage** 标题。按 `d` 切换紧凑/详细模式,按 Escape 关闭;打开详情不会向模型发送提示词。
|
|
50
|
+
2. **展开详情:** 输入 `/token-usage`,或左键点击面板的 **Token Usage** 标题。按 `d` 切换紧凑/详细模式,按 Escape 关闭;打开详情不会向模型发送提示词。
|
|
51
51
|
3. **查看子代理:** 在子代理视图中,点击输入区上方的实时摘要可打开完整面板,关闭面板不会中断子代理。
|
|
52
52
|
|
|
53
53
|
精确统计口径及边界情况见[详细用量](#详细用量)、[指标](#指标)和[价格来源与限制](docs/pricing.md)。
|
|
@@ -69,11 +69,17 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
69
69
|
|
|
70
70
|
### 兼容性
|
|
71
71
|
|
|
72
|
-
需要 Node.js 22+(见[开发](#开发)中的 OpenTUI
|
|
72
|
+
需要 Node.js 22+(见[开发](#开发)中的 OpenTUI 引擎说明)。使用 SDK **2.3.0** 的完整打包集成检查已在 OpenCode **2.0.28** 和 **2.3.0** 上通过。此前 SDK/发布组合曾验证 2.0.9–2.0.11、2.0.22、2.0.24、2.0.26 和 2.1.2;这些历史结果不代表已验证当前 SDK 的兼容性。已测试组合见[验证记录](docs/verification.md)。
|
|
73
|
+
|
|
74
|
+
从 **0.4.24** 起,插件使用 `/token-usage` 打开详情,`/usage` 留给 OpenCode 原生命令。TTFT 与历史 TPS 在可用时优先采用原生首 token 时间,旧宿主保留回退。
|
|
75
|
+
|
|
76
|
+
OpenCode **2.3.0 现已通过完整检查**,包含插件详情与原生 `/usage` 共存、原生性能计时、历史恢复、子代理、分叉和压缩。此前 6,350 与 5,080 的差异来自旧夹具假设:后台子代理完成会触发父会话额外调用一次模型。冒烟现按实际模拟提供商请求独立核验总量。
|
|
73
77
|
|
|
74
78
|
### 详细用量
|
|
75
79
|
|
|
76
|
-
在会话中输入 `/usage`,或左键点击侧边栏的 **Token Usage** 标题,可打开同一个居中原生弹窗,查看当前会话树的 token 总量,以及按消息实际模型汇总的估算费用。弹窗默认紧凑模式。使用 ↑/↓、Page Up/Down、Home/End 滚动,按 `d` 切换紧凑/详细数字并查看模型费率,按 Escape 或点击 **esc** 关闭。两个入口均不会向模型发送消息。侧边栏其他统计行仍可选择、复制,拖选文字不会打开弹窗。
|
|
80
|
+
在会话中输入 `/token-usage`,或左键点击侧边栏的 **Token Usage** 标题,可打开同一个居中原生弹窗,查看当前会话树的 token 总量,以及按消息实际模型汇总的估算费用。弹窗默认紧凑模式。使用 ↑/↓、Page Up/Down、Home/End 滚动,按 `d` 切换紧凑/详细数字并查看模型费率,按 Escape 或点击 **esc** 关闭。两个入口均不会向模型发送消息。侧边栏其他统计行仍可选择、复制,拖选文字不会打开弹窗。
|
|
81
|
+
|
|
82
|
+
为避免与 OpenCode 原生 `/usage` 冲突,插件命令在所有支持的宿主上统一从 `/usage` 改为 `/token-usage`,不保留旧名称别名。也可通过命令面板中的 **Token usage** 打开。
|
|
77
83
|
|
|
78
84
|
子代理视图中的摘要格式为 `Token Usage · Context … · Total … · Cost … · Time … · TPS …`。Time 仅累计被查看子代理自身每轮原生显示的对话耗时,token 总量和费用仍统计整棵树。窄终端会省略优先级较低的字段,在 Time 未隐藏时优先保留它,且不增加第二行。缺失数据仍显示不可用,不会被当作零,摘要也不提示快捷键。点击后打开较小的弹窗,按侧边栏原有行序展示精确数值(包括 Steps、TPS、TTFT 和 Run Time);关闭时不会为完整面板预留高度。Run Time 显示时,在侧边栏及该弹窗中均为最后一行。
|
|
79
85
|
|
|
@@ -109,7 +115,7 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
109
115
|
| `Est. Cost` | 按每条 assistant 与 compaction 消息实际模型估算的全树费用 |
|
|
110
116
|
| `Run Time` | 被查看会话自身的累计原生对话耗时;运行中包含仅用于显示的当前轮已耗时估算,以 `~` 标记;不额外叠加子会话耗时 |
|
|
111
117
|
| `TPS` | 服务端已上报的全树 `Output + Reasoning` 平均吞吐。流式进行中时,优先显示有效的近期可观察文本、reasoning summary 和工具输入估算(UTF-8 字节数 / 4),以 `~` 标记;需单次响应累计至少 20 个估算 Token、当前窗口至少 3 个合并批次且跨度至少 500ms,不计首 token 前等待时间。OpenAI Responses 等 API 不会实时暴露完整的隐藏 reasoning token,因此实时值不能表示隐藏推理吞吐。完成后恢复不带 `~` 的服务端上报吞吐 |
|
|
112
|
-
| `TTFT` | 全树中可测 assistant step 的平均首 token
|
|
118
|
+
| `TTFT` | 全树中可测 assistant step 的平均首 token 时间,优先使用 OpenCode 原生首 token 时间戳 |
|
|
113
119
|
|
|
114
120
|
#### 行为
|
|
115
121
|
|
|
@@ -121,14 +127,15 @@ opencode plugin add @chenlongapps/opencode-token-usage
|
|
|
121
127
|
- 被查看会话运行中时,`Run Time` 估算的是**截至当前已经过去的时间**,不是最终耗时或剩余时间,累计显示值带 `~`(例如 `~42.5s`)。共享的 500ms 时钟替换当前轮已经计入的时长,不额外叠加重叠的 steer/retry 耗时。时钟 tick 只更新内存,历史读取仍由事件触发。启动/重连时通过一次可选的服务端插件只读时间 RPC 取得服务端锚点,带时间戳的实时事件可作为回退;用本地单调时钟推进,不假定远程服务端与客户端时钟一致。中途打开正在运行的会话时,从历史恢复当前轮起点。重连时若历史刷新早于校时完成,仅在同一开放轮次仍保留且历史小计未变时冻结上次可信显示,避免回落到零。没有可信锚点或有效时间时,保留原生累计值。
|
|
122
128
|
- 完成、失败或中断时停止估算;上报的原生完成耗时替换估算值并移除 `~`,因此允许小幅向下校正。历史累计只包含原生已完成耗时。先累加未取整的毫秒值,再格式化(不足一秒显示毫秒,不足一分钟的秒值保留一位小数,其后显示分/小时),因此不一定等于相加各轮已取整的页脚标签。复用现有消息历史读取,重启后可恢复,也适用于安装之前的会话;不依赖实验性执行日志 API 或服务端插件。历史计时数据缺失时显示 `—`,不会显示伪零。读取失败冻结上次显示值并标注 `Not updated`(短摘要中为 `stale`)。
|
|
123
129
|
- `Run Time`/`Time` 仅显示耗时及估算/未更新标记,不附加运行状态文本或动画。已空闲父会话的耗时不会因子代理活跃而增长。关闭视图或切换会话时清理相应估算,无视图需要估算时停止共享耗时 ticker。不可用/未更新的耗时仍保留各自标记。
|
|
124
|
-
- 正常状态下确认累计值为零时,侧边栏、子代理摘要/浮窗及 `/usage` 紧凑/详细模式均隐藏整个 `Run Time`/`Time` 项,不留空行或空字段。当前轮实时估算非零时,首轮回答尚未完成也会显示时间。非零毫秒耗时、不可用值和未更新的耗时仍保留显示。
|
|
130
|
+
- 正常状态下确认累计值为零时,侧边栏、子代理摘要/浮窗及 `/token-usage` 紧凑/详细模式均隐藏整个 `Run Time`/`Time` 项,不留空行或空字段。当前轮实时估算非零时,首轮回答尚未完成也会显示时间。非零毫秒耗时、不可用值和未更新的耗时仍保留显示。
|
|
125
131
|
- 耗时随保留历史变化:切换 agent/模型与压缩保留过去轮次;分叉继承副本不计入分叉自身耗时。暂存回退不会删除轮次,但已提交回退删除消息后,也会移除这些消息对应的耗时;已删除的历史计时无法恢复。
|
|
126
132
|
- `Est. Cost` 按每条消息记录的实际模型分别计算。OpenCode 当前解析出的完整非零价格优先;若 OpenCode 给出完整零价,且内置价格快照可以完整计价,则采用快照价格。价格不完整时整条消息回退,不混用两套费率。
|
|
127
133
|
- 内置回退快照从 [models.dev](https://models.dev/api.json) 生成,只采用审核过的原厂 Provider 与自家模型系列;少量经原厂核实的例外单独维护。快照覆盖有价格的文本模型,插件运行时不下载价格。网关模型通过精确原厂 ID、明确别名和已知包装格式匹配;只有终尾 `-free` 或 `:free` 会在再次精确查找前移除。
|
|
128
134
|
- 确认免费时显示 `$0.00`;正费用低于 `$0.001` 时显示 `<$0.001`,不超过 `$0.01` 的费用保留三位小数,更高费用保留两位。价格不可用时显示 `—`;只有部分消息可计算时在已知小计后标注 `partial`。快照不含网关加价、区域溢价、未列明的折扣、非文本计费、工具费和税费;Est. Cost 是估算而非账单。覆盖范围、来源和限制见[价格来源与限制](docs/pricing.md)。
|
|
129
135
|
- 实时 `TPS` 使用约 2 秒滑动窗口,合并 100ms 内的增量。只有同时满足三项条件才开始计算和显示:单次响应累计至少 20 个估算 Token(80 个 UTF-8 字节)、当前窗口至少 3 个合并批次、这些批次的有效跨度至少 500ms。首批增量计入响应累计门槛,但仅作测速基线,其字节不计为该时间戳之后生成的输出。各响应独立累计,不跨响应或子代理合并门槛。平滑按经过时间而非 delta 条数推进。门槛未满足时回退到历史 TPS,没有可用历史值则隐藏该行。2 秒没有新增量时,共享的内存超时任务使估算过期,不读取历史。过期后恢复输出时,重新建立基线与平滑状态,并重新满足批次数和跨度条件;同一响应的累计 Token 数保留。
|
|
130
136
|
- 重连时重置实时 TPS 窗口,并通过服务端活动会话列表对账全部已跟踪会话,包括离屏会话。未结束的流静默 60 秒后进行共享活动检查,不读取消息历史;确认会话已停止后,再保留 60 秒并回收流及其 TTFT 观察。仍在运行的工具、重试及首 token 前等待没有硬性寿命上限;活动查询失败时保留状态并重试。仅有提供商的 `streamed` 元数据不代表工具已结束。没有未结束流时停止检查。
|
|
131
|
-
- 实时 TPS 测量可观察的内容交付,而非模型内部生成 token 的时钟。Responses 摘要突发、仅在 done 中返回的内容和传输缓冲仍可能影响估算,不能还原完整隐藏推理。不带 `~` 时,TPS 为全树有效 assistant 样本的 `Σ(Output + Reasoning) ÷ Σ(time.streamed − time.created)
|
|
137
|
+
- 实时 TPS 测量可观察的内容交付,而非模型内部生成 token 的时钟。Responses 摘要突发、仅在 done 中返回的内容和传输缓冲仍可能影响估算,不能还原完整隐藏推理。不带 `~` 时,TPS 为全树有效 assistant 样本的 `Σ(Output + Reasoning) ÷ Σ(time.streamed − (time.firstToken ?? time.created))`(换算为秒)。有效原生首 token 时间会排除首 token 前等待,与宿主的单步分母一致;旧宿主/历史缺少该时间时保留完整请求时长回退。它是全树加权平均,不是仅取当前轮或近期交付窗口。不做模型专属倍率、TPS 硬上限或隐藏 reasoning token 猜测。
|
|
138
|
+
- `TTFT` 在历史中优先使用原生 `time.firstToken − time.created`,流式过程中使用 `session.step.first-token`;缓冲后的 delta 不会覆盖原生时间。旧宿主回退到首个非空文本/推理/工具输入 delta,或可测的历史 reasoning/tool 创建时间;没有原生首 token 时间的纯文本历史无法恢复 TTFT。原生时间可在重新打开会话和实时观察过期后恢复。重试复用消息 ID 时替换上次尝试的样本。TTFT 仍是整棵树可测 step 的平均值,而 OpenCode 页脚取当前轮首个可测 step,多步/子代理场景不保证数值相同。
|
|
132
139
|
- 首次读取失败显示 `Unavailable`;后续失败保留上次完整快照、标注 `Not updated` 并自动重试。
|
|
133
140
|
|
|
134
141
|
### 开发
|
|
@@ -141,7 +148,9 @@ npm run build
|
|
|
141
148
|
npm run test:smoke
|
|
142
149
|
```
|
|
143
150
|
|
|
144
|
-
|
|
151
|
+
针对提供 `time.firstToken` 的宿主,可运行 `npm run test:smoke -- --performance-only` 验证原生 TTFT/TPS。该专项检查覆盖流式计时、原生页脚和重开会话后的历史恢复,不能替代完整集成检查。
|
|
152
|
+
|
|
153
|
+
OpenCode SDK 包精确锁定为 `2.3.0`,OpenTUI 为 `0.8.4`,Solid 与编译器为 `1.9.16`。OpenTUI 为其原生 Node 运行时声明 Node.js `>=26.4.0`,因此 Node 22 安装时 npm 会输出 `EBADENGINE`;启用 `engine-strict` 时须使用满足该依赖引擎要求的 Node 版本。插件的 TUI 在 OpenCode 内运行,并非独立的 Node 22 渲染器。项目 Node.js 22+ 的最低要求不变;该依赖组合的检查见[验证记录](docs/verification.md)。
|
|
145
154
|
|
|
146
155
|
[价格更新工作流](.github/workflows/update-prices.yml) 每 4 小时检查 models.dev,也可在 `main` 上通过 **Run workflow** 手动触发。快照无变化就不发布。经审核的原厂来源白名单内有变化时,先通过类型检查、测试、构建和打包检查,再自动升级 patch 版本(例如 `0.4.4` → `0.4.5`),只提交 `src/prices.generated.ts`、`package.json` 和 `package-lock.json`,并原子推送 `main` 与版本标签。工作流创建 GitHub Release,显式触发该标签上的 [`publish.yml`](.github/workflows/publish.yml),通过 npm Trusted Publishing/OIDC 发布,无需长期 npm token。仓库必须允许 GitHub Actions 推送 `main` 和创建标签;npm 必须信任 `publish.yml` 并允许执行 `npm publish`。
|
|
147
156
|
|
|
@@ -149,16 +158,18 @@ OpenCode SDK 包精确锁定为 `2.0.24`,OpenTUI 为 `0.5.14`。Node.js 22 下
|
|
|
149
158
|
|
|
150
159
|
手动更新时,在联网的维护环境运行 `npm run prices:update`,审阅生成文件及例外差异,再执行上述检查。构建和插件刷新不会请求 models.dev。
|
|
151
160
|
|
|
152
|
-
`test:smoke` 会打包真实产物,并在隔离的 OpenCode 与本地模拟提供商中验证加载、刷新、`/usage`、侧边栏标题点击、子代理累计、逐消息计价、原厂价格补全、模型切换、TPS、TTFT、Responses 摘要突发/done-only 摘要/静默过期,以及会话自身的原生对话耗时(未完成回答、首轮 synthetic 延续、重开运行中 TUI
|
|
161
|
+
`test:smoke` 会打包真实产物,并在隔离的 OpenCode 与本地模拟提供商中验证加载、刷新、`/token-usage`、侧边栏标题点击、子代理累计、逐消息计价、原厂价格补全、模型切换、TPS、TTFT、Responses 摘要突发/done-only 摘要/静默过期,以及会话自身的原生对话耗时(未完成回答、首轮 synthetic 延续、重开运行中 TUI、终端焦点事件、空闲、中断、消息历史恢复、分叉和窄终端子代理视图)。在 OpenCode 2.3+ 上还验证原生 `/usage` 打开宿主自身弹窗。它需要 Python 3、可用的本地端口和 npm 网络访问,不会修改现有 OpenCode 配置或调用付费模型。终端焦点检查不代表已验证物理 macOS 多屏切换、App Nap 或睡眠/唤醒行为。
|
|
153
162
|
|
|
154
163
|
性能生命周期检查还会使用真实宿主的活动会话 API,对离屏监视器过滤结束事件、注入重连握手,并加速模拟 25 小时的工具/子代理等待,确认运行时保留状态、结束后无需读取历史即可回收。
|
|
155
164
|
|
|
156
165
|
默认使用 `PATH` 中的 `opencode`。OpenCode 2.0.9 及之后的稳定版 2.x 可以运行;尚未与当前 SDK 验证的宿主会输出警告,然后继续实际兼容性检查。仅通过版本检查不代表兼容性已验证。SDK 依赖仍独立锁定,不随本机 CLI 更新而自动升级。
|
|
157
166
|
|
|
167
|
+
CI 配置了固定 OpenCode **2.0.28** 与 **2.3.0** 的完整打包冒烟任务,使用隔离的本地模拟提供商并上传诊断产物。会话树总量按实际模拟请求独立核验:新宿主的后台子代理完成通知可能让父会话额外调用一次模型。
|
|
168
|
+
|
|
158
169
|
如需用独立安装的二进制复现基准,而不降级日常使用的安装:
|
|
159
170
|
|
|
160
171
|
```bash
|
|
161
|
-
OPENCODE_BIN=/absolute/path/to/opencode-2.0.
|
|
172
|
+
OPENCODE_BIN=/absolute/path/to/opencode-2.0.28 npm run test:smoke
|
|
162
173
|
```
|
|
163
174
|
|
|
164
175
|
`OPENCODE_BIN` 为版本检查、隔离服务端和 TUI 统一选择同一个可执行文件。相对路径以执行命令时的目录为基准解析。smoke 输出和 `result.json` 会记录宿主版本与所选二进制;已测试组合见[验证记录](docs/verification.md)。
|
package/ROADMAP.md
CHANGED
|
@@ -110,7 +110,7 @@ Steps 1,234
|
|
|
110
110
|
* [x] 按 OpenCode 2.0.26 原生回答页脚口径累计当前查看会话的全部保留轮次,每轮只取最终已完成回答
|
|
111
111
|
* [x] 不计轮次之间的空闲,不额外叠加子代理耗时,未完成回答不写入原生累计值
|
|
112
112
|
* [x] 复用完整消息历史,重开会话和插件重启后恢复累计值,不依赖执行日志 API
|
|
113
|
-
* [x] 侧边栏 `Run Time`、子代理单行 `Time`、`/usage` 的 `Run Time (this session)` 三处显示
|
|
113
|
+
* [x] 侧边栏 `Run Time`、子代理单行 `Time`、`/token-usage` 的 `Run Time (this session)` 三处显示
|
|
114
114
|
* [x] 移除各耗时入口的 `Running...` 文本及点号动画,保留 `~` 实时估算、未更新标记与零值隐藏
|
|
115
115
|
* [x] 运行中以 `~` 显示当前轮已耗时估算,首轮非零估算可见;共享 500ms 单调时钟,不定时读取 API,结束后校正为原生值
|
|
116
116
|
* [x] 时间缺失显示不可用,读取失败保留旧值并标注未更新;分叉副本不继承耗时,已提交回退随删除历史扣除
|
|
@@ -124,5 +124,8 @@ Steps 1,234
|
|
|
124
124
|
|
|
125
125
|
## 待解决问题
|
|
126
126
|
|
|
127
|
+
- [x] 原生 TTFT 与历史 TPS 对齐:优先 first-token 事件/历史时间,旧宿主回退,重试去重与历史恢复;269 项测试、真实 OpenCode 2.0.28 完整回归及 2.3.0 专项打包集成验证通过
|
|
128
|
+
- [x] 插件命令统一改为 `/token-usage`,不保留 `/usage` 别名;真实 OpenCode 2.3.0 已验证原生与插件命令分别打开正确弹窗、不发送模型请求
|
|
129
|
+
- [ ] OpenCode 2.3.0 完整冒烟的子代理固定总量夹具适配:命令及详情检查通过后,实际 Total 6,350 与旧夹具 5,080 不符,详见验证记录
|
|
127
130
|
- [x] TPS 显示机制修改:100ms 突发合并、首批基线、500ms 最小跨度、时间加权平滑与静默过期;209 项测试及真实 OpenCode 2.0.26 本地 Responses SSE 打包集成验证通过
|
|
128
131
|
- [x] 异常流与 TTFT 回收:离屏会话的共享活动检查、重连对账、丢失结束事件回收及异步竞态保护;长工具/重试等待不按静默时长误清理,234 项测试及真实 OpenCode 2.0.26 打包集成验证通过(长等待采用加速时钟)
|
package/dist/performance.d.ts
CHANGED
|
@@ -44,7 +44,11 @@ export interface PreparedPerformance {
|
|
|
44
44
|
tps?: number;
|
|
45
45
|
ttftSum: number;
|
|
46
46
|
ttftCount: number;
|
|
47
|
-
historical: ReadonlyMap<string,
|
|
47
|
+
historical: ReadonlyMap<string, {
|
|
48
|
+
started: number;
|
|
49
|
+
ttft: number | undefined;
|
|
50
|
+
native: boolean;
|
|
51
|
+
}>;
|
|
48
52
|
}
|
|
49
53
|
export type PerformanceSubscribe = (listener: (event: PerformanceEvent) => void) => () => void;
|
|
50
54
|
export type PerformanceListener = (sessionID: string) => void;
|
package/dist/performance.js
CHANGED
|
@@ -40,10 +40,23 @@ export function smoothLiveTps(previous, raw, elapsed = LIVE_MIN_SPAN_MS) {
|
|
|
40
40
|
return previous * (1 - alpha) + raw * alpha;
|
|
41
41
|
}
|
|
42
42
|
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
43
|
+
function nativeFirst(message) {
|
|
44
|
+
const time = message.time;
|
|
45
|
+
if (!finite(time?.created) || !finite(time.firstToken) || time.firstToken < time.created)
|
|
46
|
+
return undefined;
|
|
47
|
+
if (finite(time.streamed) && time.firstToken > time.streamed)
|
|
48
|
+
return undefined;
|
|
49
|
+
if (finite(time.completed) && time.firstToken > time.completed)
|
|
50
|
+
return undefined;
|
|
51
|
+
return time.firstToken;
|
|
52
|
+
}
|
|
43
53
|
function historicalFirst(message) {
|
|
44
54
|
const started = message.time?.created;
|
|
45
55
|
if (!finite(started))
|
|
46
56
|
return undefined;
|
|
57
|
+
const first = nativeFirst(message);
|
|
58
|
+
if (first !== undefined)
|
|
59
|
+
return first - started;
|
|
47
60
|
for (const part of message.content ?? []) {
|
|
48
61
|
if (part.type === "text") {
|
|
49
62
|
if (part.text)
|
|
@@ -79,15 +92,18 @@ export function preparePerformance(messages) {
|
|
|
79
92
|
continue;
|
|
80
93
|
const created = message.time?.created;
|
|
81
94
|
const streamed = message.time?.streamed;
|
|
95
|
+
const first = nativeFirst(message);
|
|
96
|
+
const streamStart = first ?? created;
|
|
82
97
|
const tokens = normalize(message.tokens);
|
|
83
98
|
const generated = tokens.output + tokens.reasoning;
|
|
84
|
-
if (finite(
|
|
99
|
+
if (finite(streamStart) && finite(streamed) && streamed > streamStart && generated > 0) {
|
|
85
100
|
output += generated;
|
|
86
|
-
duration += streamed -
|
|
101
|
+
duration += streamed - streamStart;
|
|
87
102
|
}
|
|
88
103
|
const sample = historicalFirst(message);
|
|
104
|
+
if (finite(created))
|
|
105
|
+
historical.set(message.id, { started: created, ttft: sample, native: first !== undefined });
|
|
89
106
|
if (sample !== undefined && finite(sample) && sample >= 0) {
|
|
90
|
-
historical.set(message.id, sample);
|
|
91
107
|
ttftSum += sample;
|
|
92
108
|
ttftCount++;
|
|
93
109
|
}
|
|
@@ -104,8 +120,12 @@ function performanceWithObservations(prepared, observations) {
|
|
|
104
120
|
let ttftSamples = prepared.ttftCount;
|
|
105
121
|
for (const sample of observed.values()) {
|
|
106
122
|
const old = prepared.historical.get(sample.messageID);
|
|
107
|
-
|
|
108
|
-
|
|
123
|
+
// A retry can reuse the message ID. Only observations for the same or a
|
|
124
|
+
// newer attempt may replace history; native history beats a buffered delta.
|
|
125
|
+
if (old && (old.started > sample.started || (old.started === sample.started && old.native)))
|
|
126
|
+
continue;
|
|
127
|
+
if (old?.ttft !== undefined) {
|
|
128
|
+
ttft -= old.ttft;
|
|
109
129
|
ttftSamples--;
|
|
110
130
|
}
|
|
111
131
|
ttft += sample.first - sample.started;
|
|
@@ -363,7 +383,8 @@ export class PerformanceMonitor {
|
|
|
363
383
|
if (event.type === "session.step.started") {
|
|
364
384
|
if (!finite(data.started))
|
|
365
385
|
return false;
|
|
366
|
-
|
|
386
|
+
const previous = this.active.get(key);
|
|
387
|
+
if (previous && (data.started <= previous.started || data.started < previous.latest))
|
|
367
388
|
return false;
|
|
368
389
|
// A session's foreground steps are serial; missing endings must not accumulate
|
|
369
390
|
// forever just because the same session continues to run later steps.
|
|
@@ -383,12 +404,36 @@ export class PerformanceMonitor {
|
|
|
383
404
|
messageID, sessionID, started: data.started, bytes: 0, samples: [], events: new Set(),
|
|
384
405
|
checkedAt: this.monotonic(), revision: 0, latest: Math.max(data.started, finite(event.created) ? event.created : data.started),
|
|
385
406
|
});
|
|
407
|
+
this.observed.delete(key);
|
|
408
|
+
if (previous)
|
|
409
|
+
this.publish(sessionID);
|
|
386
410
|
this.scheduleCleanup();
|
|
387
411
|
return false;
|
|
388
412
|
}
|
|
389
413
|
const step = this.active.get(key);
|
|
390
414
|
if (!step)
|
|
391
415
|
return false;
|
|
416
|
+
if (event.type === "session.step.first-token") {
|
|
417
|
+
if (step.nativeFirst || !finite(event.created) || event.created < step.started)
|
|
418
|
+
return false;
|
|
419
|
+
if (step.streamed !== undefined && event.created > step.streamed)
|
|
420
|
+
return false;
|
|
421
|
+
if (step.finishedAt !== undefined && !step.inactive && event.created > step.latest)
|
|
422
|
+
return false;
|
|
423
|
+
// The host records this before its 100ms delta delivery buffer. It may
|
|
424
|
+
// arrive after a delta or completion; replace the fallback, not its expiry.
|
|
425
|
+
if (step.finishedAt === undefined || (step.inactive && event.created >= step.latest))
|
|
426
|
+
this.touch(step, event.created);
|
|
427
|
+
step.first = event.created;
|
|
428
|
+
step.nativeFirst = true;
|
|
429
|
+
this.observed.set(key, {
|
|
430
|
+
messageID, sessionID, started: step.started, first: event.created,
|
|
431
|
+
...(step.finishedAt !== undefined ? { expiresAt: step.finishedAt + this.retention } : {}),
|
|
432
|
+
});
|
|
433
|
+
this.scheduleCleanup();
|
|
434
|
+
this.publish(sessionID);
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
392
437
|
if (event.type === "session.retry.scheduled" || (event.type.startsWith("session.tool.") && event.type !== "session.tool.input.delta")) {
|
|
393
438
|
if (!finite(event.created) || event.created < step.latest)
|
|
394
439
|
return false;
|
package/dist/tui.js
CHANGED
package/dist/usage.d.ts
CHANGED
package/docs/verification.md
CHANGED
|
@@ -1,5 +1,79 @@
|
|
|
1
|
+
# 0.4.25 发布准备(2026-11-17)
|
|
2
|
+
|
|
3
|
+
[GitHub CI 43921929492](https://github.com/chenlongapps/opencode-token-usage/actions/runs/43921929492) 已在依赖升级提交 `c8906a8` 上通过:Node 22 干净安装、类型检查、测试、构建、打包检查,以及 OpenCode 2.0.28/2.3.0 的完整打包冒烟均成功。该 CI 检查的是版本升级和本节文档变更之前的提交;各次记录中的发布状态均以当时为准。
|
|
4
|
+
|
|
5
|
+
本地 macOS、Node.js 22.23.2 / npm 10.9.8 在版本升级前重新通过类型检查、269 项测试、构建、`npm pack --dry-run` 与 `git diff --check`。上述双宿主结果来自对应的真实 GitHub CI 运行及下方既有本地验证。
|
|
6
|
+
|
|
7
|
+
# SDK 2.3.0 与双宿主完整冒烟(2026-11-17)
|
|
8
|
+
|
|
9
|
+
环境:macOS arm64、Node.js v22.23.2、npm 10.9.8。`@opencode/plugin/client/schema/theme` 统一从 2.0.24 升至 2.3.0,OpenTUI 从 0.5.14 升至 0.8.4,Solid 与 `babel-preset-solid` 升至 1.9.16,以满足新版 OpenTUI 编译器的 peer 要求。插件业务源码无需迁移。
|
|
10
|
+
|
|
11
|
+
## 夹具修复与实际原因
|
|
12
|
+
|
|
13
|
+
- 使用旧 SDK 在真实 2.3.0 复现原子代理摘要超时。`requests.json` 与流事件证实:父会话初始回答、subagent 工具调用、工具返回后的回答、子代理回答,以及 `<subagent state="completed">` 通知后的父会话回答共 5 次模型调用。每次固定上报 1,270 tokens,总量 6,350、费用 `$0.0063` 正确;旧宿主同步等待子代理,只有 4 次调用、5,080 tokens、`$0.00504`。这是测试夹具的生命周期假设失效,不是插件重复计费。
|
|
14
|
+
- 冒烟按实际模拟提供商请求独立计算期望总量/费用,并逐条核对父子会话原始 assistant 数及五类 tokens,再检查父侧边栏、子摘要与弹窗、模型切换后的历史计价。`tree-fixture.json` 保存期望值和原始消息;`result.json` 记录实际调用数与总量。
|
|
15
|
+
- 2.3+ 等待子代理完成通知被父会话消费后再读取最终状态。空闲父会话耗时检查以该通知触发的合法父轮次结算后为基线;离屏回收场景在新宿主显式保持父提供商响应运行,在旧宿主仍保持父工具等待,均以真实 `session.active()` 核验。
|
|
16
|
+
- PTY stdout 直接以字节流交给 xterm,原始 ANSI 保存为 Buffer,修复 UTF-8 条形图字符跨块时被 `toString()` 解码为 `�` 的偶发断言失败。
|
|
17
|
+
|
|
18
|
+
## 完整打包集成结果
|
|
19
|
+
|
|
20
|
+
| SDK | 宿主 | 结果 | 全树调用 / tokens / 费用 |
|
|
21
|
+
| --- | --- | --- | --- |
|
|
22
|
+
| 2.3.0 | OpenCode 2.0.28 | 完整通过 | 4 / 5,080 / $0.00504 |
|
|
23
|
+
| 2.3.0 | OpenCode 2.3.0 | 完整通过 | 5 / 6,350 / $0.0063 |
|
|
24
|
+
|
|
25
|
+
两版均验证真实打包/隔离安装、侧边栏与标题点击、`/token-usage` 紧凑/详细/窄终端、子代理、模型切换/缺失上下文上限/价格回退、实时性能/Responses 突发与静默过期、原生累计与实时估算耗时、中断、首轮 synthetic 延续、视图恢复、终端焦点事件、异常流回收。2.3.0 另通过原生 `/usage` 共存及 first-token 性能/同屏页脚/新 TUI 历史恢复:TTFT 306ms,首 delta 晚 103ms,历史 TPS 43.6 tok/s。
|
|
26
|
+
|
|
27
|
+
真实分叉在两版均保留 `_序号` 继承 ID、独立根、继承上下文与来源计费;新调用只计分叉自身。真实 compaction 均为 `status === "completed"`,升序完整历史保留旧消息,压缩边界清除旧上下文,后续 assistant 恢复上下文,compaction 不计 assistant Steps。加速 25 小时的生命周期检查不代表实际等待 25 小时,终端焦点检查不代表物理多屏/App Nap/睡眠唤醒验证。
|
|
28
|
+
|
|
29
|
+
产物:
|
|
30
|
+
|
|
31
|
+
- 2.3.0:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-qsXSiu`
|
|
32
|
+
- 2.0.28:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-QMm0QN`
|
|
33
|
+
- 旧 SDK 的 2.3.0 总量失败复现:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-OLYgeh`
|
|
34
|
+
|
|
35
|
+
## 版本与 CI 策略
|
|
36
|
+
|
|
37
|
+
当前 SDK 的已验证宿主列表收敛为 2.0.28、2.3.0,旧 SDK 的历史验证保留在下方,不自动继承。仍允许稳定版 `>=2.0.9 <3.0.0` 实际执行检查,未验证版输出警告。CI 新增固定双宿主完整冒烟矩阵、Python 终端环境及诊断产物上传;actionlint 静态检查通过,尚未推送或触发真实 GitHub Actions,不能将本地通过称为远程 CI 通过。
|
|
38
|
+
|
|
39
|
+
Node 22 干净 `npm ci`、类型检查、269 项自动化测试(零失败/跳过)、构建、`npm pack --dry-run --json`、`npm ls --all`、冒烟脚本语法检查与 `git diff --check` 通过。旧安装树在原地升级时触发 npm 10 的 Effect peer 解析冲突,重新生成锁文件并干净安装后解决;Solid/编译器协调升级后依赖树无 invalid peer。OpenTUI 的既有 Node >=26.4 引擎警告保留,Node 22 工具链与宿主内 TUI 的通过不代表独立 Node 22 原生渲染受支持。首次旧 SDK 打包安装还遇到继承 npm 缓存的 AWS ETARGET,使用独立缓存后恢复。包版本仍为 0.4.24,本次未提交、推送或发布。
|
|
40
|
+
|
|
41
|
+
# 0.4.24 发布前验证(2026-11-15)
|
|
42
|
+
|
|
43
|
+
本次在 Linux、Node.js 24.22.1、npm 11.9.0 环境中重新执行:干净安装(独立 npm 缓存)、`npm run typecheck`、269 项 `npm test`、`npm run build`、`npm pack --dry-run` 与 `git diff --check`,均通过。安装仍有固定 OpenTUI 依赖的 Node >=26.4 引擎警告。
|
|
44
|
+
|
|
45
|
+
同步中英文 README 的命令迁移与兼容性边界,并按标签差异补齐 0.4.12–0.4.24 更新日志。本环境没有 OpenCode 二进制,未重新运行真实宿主冒烟;2.0.28 完整回归与 2.3.0 部分验证沿用下方原始记录,2.3.0 子代理总量不匹配仍未解决。下方“未发布”描述保留各次验证当时的状态,实际发布以 GitHub Release 与 npm registry 为准。
|
|
46
|
+
|
|
1
47
|
# 验证记录
|
|
2
48
|
|
|
49
|
+
## 2026-11-16 · 详情命令改为 `/token-usage`(未发布)
|
|
50
|
+
|
|
51
|
+
`src/tui.tsx` 的斜杠命令从 `/usage` 统一改为 `/token-usage`,不注册旧名称别名,避免占用 OpenCode 2.3.0 的原生账户用量入口。命令 ID `opencode-token-usage.show`、命令面板标题 `Token usage`、标题点击和详情弹窗共用入口保留。中英文 README、当前产品约定和冒烟输入已同步;历史记录中的旧名称保持原样。
|
|
52
|
+
|
|
53
|
+
类型检查、269 项自动化测试和冒烟脚本语法检查通过。真实 OpenCode 2.3.0 的完整冒烟已通过构建/打包/隔离安装,实测原生 `/usage` 打开 Console 用量弹窗、`/token-usage` 打开插件详情;两者均不新增消息或提供商请求。紧凑/详细、关闭重开、标题点击、窄终端详情以及原生 TTFT/TPS 与历史恢复检查通过。
|
|
54
|
+
|
|
55
|
+
本轮 2.3.0 完整冒烟仍未全部通过:已越过原先的命令冲突,后来停在 `subagent usage summary with its own runtime`。`final-6.txt` 实际显示子代理摘要 `Total 6,350 · Cost $0.006`,而旧夹具固定期待 `Total 5,080 · Cost $0.005`;摘要本身已挂载,不能将该超时描述成命令改名失败或子代理面板缺失。此处尚未调整固定总量夹具,也不宣称完整 2.3.0 兼容验证通过。产物:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-CyDpUu`。
|
|
56
|
+
|
|
57
|
+
命令改名后的真实 OpenCode 2.0.28 完整冒烟通过,覆盖 `/token-usage`、标题点击、紧凑/详细/窄终端、子代理、耗时、模型/费用、流式性能、分叉、压缩与异常流回收等既有检查。产物:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-VWXWsr`。
|
|
58
|
+
|
|
59
|
+
插件包仍为 0.4.23,未升级版本、提交、推送或发布。
|
|
60
|
+
|
|
61
|
+
## 2026-11-16 · 原生 TTFT 与历史 TPS 对齐(未发布)
|
|
62
|
+
|
|
63
|
+
排查 OpenCode 2.3.0 的原生页脚与截图对应消息,确认宿主以 `time.firstToken − time.created` 计算 TTFT,并在文本/推理/工具输入开始处发布持久化 `session.step.first-token`。首个文本 delta 经过宿主 100ms 缓冲后才发布,旧插件用 delta 作为首 token 时点,因而稍晚。截图数据的原生 TTFT 为 1,926ms;输出 69 tokens,首 token 后的生成时长为 689ms,完整请求时长为 2,615ms,分别对应原生 `1.9s / 100.1 tok/s` 与旧插件 `2.0s / 26.4 tok/s`。本次没有保存截图当时的 ephemeral delta,不能据显示值断言它的精确毫秒值。
|
|
64
|
+
|
|
65
|
+
`src/performance.ts` 现在优先读取历史 `time.firstToken` 并监听原生首 token 事件,原生历史不会被缓冲 delta 覆盖。旧宿主仍回退到首个非空 delta 或可测 reasoning/tool 历史时间。历史 TPS 使用 `streamed − (firstToken ?? created)`,排除有效原生首 token 前等待;无效原生时间回退,零生成时长不伪造 TPS。重试复用消息 ID 时重置观察,以开始时间隔离旧尝试;迟到原生事件不延长完成保留期。全树平均范围、实时 TPS 的字节窗口/门槛/平滑及耗时逻辑保持原约定。
|
|
66
|
+
|
|
67
|
+
环境:macOS、Node.js v22.23.2、npm 10.9.8,插件包 0.4.23、SDK 2.0.24。类型检查、269 项自动化测试、构建、冒烟脚本语法检查和 `git diff --check` 通过。新增 12 项用例覆盖截图固定时间数据、原生/delta/历史优先级与乱序到达、完成保留期、重新打开、重试复用 ID、无效时间、零生成时长、全树加权和控制器仅内存更新。
|
|
68
|
+
|
|
69
|
+
真实 OpenCode 2.3.0 的 `npm run test:smoke -- --performance-only` 已通过打包、隔离安装、流式显示、完成后与原生页脚比较、真实原生事件重放及新 TUI 的纯文本历史恢复。实测 native TTFT 312ms,首 delta 晚 106ms;原生页脚为 `43.3 tok/s · 312ms ttft`,插件为 `TPS 43.3 tok/s / TTFT 0.3s`(原有秒级一位小数格式)。产物:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-HTcAHy`。
|
|
70
|
+
|
|
71
|
+
本轮 2.3.0 **完整**冒烟未通过:`/usage` 打开宿主的 OpenCode Console 用量弹窗而非插件详情,停在 `empty /usage dialog`;标题点击入口已通过。该命令冲突尚未修复,专项通过不能视为完整宿主兼容验证,既有验证版本列表未扩展。失败产物:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-61zsdm`。更早一次安装被 npm 过期缓存中的 AWS 依赖版本阻塞,改用独立新缓存后正常安装;Node 22 的既有 OpenTUI 引擎警告保留。
|
|
72
|
+
|
|
73
|
+
真实 OpenCode 2.0.28 的完整 `npm run test:smoke` 通过,确认缺少原生 first-token 字段的宿主仍可使用原有回退。覆盖实时 TPS/TTFT、侧边栏、`/usage` 紧凑/详细与窄终端、子代理汇总、原生累计耗时、模型切换/定价、Responses 突发与静默过期、真实分叉、已完成 compaction 与消息排序、异常流回收及首轮 synthetic 延续。旧宿主使用官方 npm 包经 SHA-512 校验后提取的独立二进制,未替换日常安装;产物:`/private/var/folders/rw/bmx6c8hd737brl55m0_ff_b80000gn/T/opencode/token-usage-smoke-XV0SZj`。
|
|
74
|
+
|
|
75
|
+
集成请求由本地模拟提供商响应,未调用真实付费模型或修改日常 OpenCode 配置。本次未升级版本、提交、推送或发布。
|
|
76
|
+
|
|
3
77
|
## 2026-10-21 · 实时 TPS 显示门槛(未发布)
|
|
4
78
|
|
|
5
79
|
`src/performance.ts` 的实时估算仅在三项条件同时满足时开始计算和显示:单条 assistant 响应累计可观察增量至少 20 个估算 Token(UTF-8 字节数 / 4,即至少 80 字节)、当前约 2 秒窗口中至少 3 个按 100ms 合并的独立批次、有效观察跨度至少 500ms。首批内容计入累计门槛,但仍只作测速基线,不进入速度分子;不跨响应或子代理合并门槛。原有时间加权平滑、历史 TPS、TTFT 和 2000ms 静默过期规则不变。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chenlongapps/opencode-token-usage",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.25",
|
|
4
4
|
"description": "OpenCode 2 plugin for session-tree token usage, context window monitoring, estimated costs, and performance metrics (TPS, TTFT, run time).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"opencode",
|
|
@@ -62,24 +62,24 @@
|
|
|
62
62
|
"registry": "https://registry.npmjs.org/"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@opencode/plugin": "2.0
|
|
65
|
+
"@opencode/plugin": "2.3.0"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@opentui/core": "0.
|
|
69
|
-
"@opentui/solid": "0.
|
|
68
|
+
"@opentui/core": "0.8.4",
|
|
69
|
+
"@opentui/solid": "0.8.4",
|
|
70
70
|
"solid-js": ">=1.9.0"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
73
|
"@babel/core": "^7.28.0",
|
|
74
|
-
"@opencode/client": "2.0
|
|
75
|
-
"@opencode/schema": "2.0
|
|
76
|
-
"@opencode/theme": "2.0
|
|
77
|
-
"@opentui/core": "0.
|
|
78
|
-
"@opentui/solid": "0.
|
|
74
|
+
"@opencode/client": "2.3.0",
|
|
75
|
+
"@opencode/schema": "2.3.0",
|
|
76
|
+
"@opencode/theme": "2.3.0",
|
|
77
|
+
"@opentui/core": "0.8.4",
|
|
78
|
+
"@opentui/solid": "0.8.4",
|
|
79
79
|
"@types/node": "^22.0.0",
|
|
80
80
|
"@xterm/headless": "6.0.0",
|
|
81
|
-
"babel-preset-solid": "1.9.
|
|
82
|
-
"solid-js": "1.9.
|
|
81
|
+
"babel-preset-solid": "1.9.16",
|
|
82
|
+
"solid-js": "1.9.16",
|
|
83
83
|
"tsx": "^4.20.0",
|
|
84
84
|
"typescript": "^5.9.0"
|
|
85
85
|
},
|