@jerry_chzy/dsh-token-usage 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.i18n.yaml +6 -0
- package/README.md +162 -2
- package/README.zh.md +163 -0
- package/cordis.patch.yml +7 -0
- package/lib/client.js +311 -0
- package/lib/index.js +891 -0
- package/lib/types/backfill.d.ts +110 -0
- package/lib/types/client/TokenUsageDock.d.ts +28 -0
- package/lib/types/client/index.d.ts +30 -0
- package/lib/types/client/locales.d.ts +42 -0
- package/lib/types/command.d.ts +75 -0
- package/lib/types/format.d.ts +30 -0
- package/lib/types/index.d.ts +86 -0
- package/lib/types/pricing.d.ts +92 -0
- package/lib/types/projection.d.ts +52 -0
- package/package.json +53 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.i18n.yaml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
|
|
2
|
+
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
|
+
# after editing either side, bring the other along and re-record with:
|
|
4
|
+
# pnpm run verify-translation-pairing --write packages/client/ui-token-usage/README.md
|
|
5
|
+
README.md: 03a27eeabcfbbffc9511811026ddc4f5e183697e
|
|
6
|
+
README.zh.md: 17e3825cae5b4c0cfb299e6c0238936b2b851277
|
package/README.md
CHANGED
|
@@ -1,3 +1,163 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
description: "Web token-usage readout: the composer-dock line classifying the current conversation's durable usage into four billing buckets and pricing them; for users and maintainers of session cost displays."
|
|
3
|
+
kind: "package-reference"
|
|
4
|
+
---
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
# @deepseek-ai/dsh-client-ui-token-usage
|
|
7
|
+
|
|
8
|
+
English | [中文](README.zh.md)
|
|
9
|
+
|
|
10
|
+
## Summary
|
|
11
|
+
|
|
12
|
+
This package renders one composer-dock line under the Web GUI's composer: the current conversation's token usage split into its four disjoint billing buckets — uncached input, cache read, cache write, and output — plus an estimated cost. The counts come from the durable `tokenUsage` projection, so paging and compaction cannot change them; the slot's session scope makes the readout per-conversation. Cost comes from the plugin's `pricing` config, matched against the session's last-used model, and stays hidden when that model has no configured price. The host half also backfills the money Web never showed: one pass over the archived corpus writes a `TOKEN_COST.md` per session working directory, and the readout points at it when a route has no price.
|
|
13
|
+
|
|
14
|
+
## Table of Contents
|
|
15
|
+
|
|
16
|
+
- [Use this package](#use-this-package)
|
|
17
|
+
- [Understand the implementation](#understand-the-implementation)
|
|
18
|
+
- [Further Exploration](#further-exploration)
|
|
19
|
+
- [Model Experience](#model-experience)
|
|
20
|
+
- [Known Limitations and Deferred Work](#known-limitations-and-deferred-work)
|
|
21
|
+
- [Dev Note](#dev-note)
|
|
22
|
+
|
|
23
|
+
-----
|
|
24
|
+
|
|
25
|
+
<a id="use-this-package"></a>
|
|
26
|
+
## Use this package
|
|
27
|
+
|
|
28
|
+
Mount this plugin alongside `@deepseek-ai/dsh-token-meter` and the Session Controller; the readout then appears under the composer whenever the conversation has billed at least one token. The line starts with the session total, continues with the non-zero bucket split and the estimated cost, and ends with the context occupancy once the meter reports both a numerator and a route capacity. It renders nothing before the first billed step, drops zero cache buckets, and keeps long rows to compact counts (517 / 12.3K / 1.2M).
|
|
29
|
+
|
|
30
|
+
### Install into a profile
|
|
31
|
+
|
|
32
|
+
A profile installs this package as a direct dependency, which is what the Web GUI's installed-plugin list shows and toggles:
|
|
33
|
+
|
|
34
|
+
```powershell
|
|
35
|
+
node packages/client/ui-token-usage/scripts/pack-local.mjs
|
|
36
|
+
dsh plugin --profile <name> add <the .tgz path the script prints>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The script stages a copy whose manifest rewrites every `workspace:` range to the workspace version and drops `devDependencies`, because a workspace `pnpm pack` leaves `workspace:` in `devDependencies` and a profile install then fails with `ERR_PNPM_CANNOT_RESOLVE_WORKSPACE_PROTOCOL`. The package declares `dsh.bundle.patch`, so the profile mounts the readout row from the package's own `cordis.patch.yml` without a manual insert. The `desktop` profile belongs to the Electron application and rejects `dsh plugin` by design; install there through the application's own plugin surface.
|
|
40
|
+
|
|
41
|
+
Configure prices through this plugin's row config. `pricing` maps a model-id prefix to CNY per million tokens, and the longest matching prefix wins, so dated or vision routes can share or override a base model's price:
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
- id: ui-token-usage
|
|
45
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
46
|
+
config:
|
|
47
|
+
currencySymbol: '¥'
|
|
48
|
+
pricing:
|
|
49
|
+
deepseek-v4-pro: { inputMiss: 9, inputHit: 0.3, output: 27 }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Defaults are the advisory DeepSeek official table (peak rates). A model absent from the resolved table shows its token counts without a cost figure, never a guessed one.
|
|
53
|
+
|
|
54
|
+
### History backfill
|
|
55
|
+
|
|
56
|
+
The host half also walks the archived session corpus and writes a `TOKEN_COST.md` into each session's own working directory: one row per conversation recorded there, with its four billing buckets, its peak and off-peak amounts, and its total. `backfill.pricing` prices it with `[off-peak, peak]` pairs per million tokens, and `backfill.peakHours` replaces the Beijing-time peak windows (default: weekdays 09:00–12:00 and 14:00–18:00). `backfill.enabled: false` turns both the pass and its browser notice off.
|
|
57
|
+
|
|
58
|
+
```yaml
|
|
59
|
+
- id: ui-token-usage
|
|
60
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
61
|
+
config:
|
|
62
|
+
backfill:
|
|
63
|
+
pricing:
|
|
64
|
+
deepseek-v4-pro:
|
|
65
|
+
inputMiss: [4.5, 9]
|
|
66
|
+
inputHit: [0.15, 0.3]
|
|
67
|
+
output: [13.5, 27]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Where no recorded route is priced, that same file instead lists the routes the table misses and the YAML row that would price them, and the composer readout gains a notice pointing at the file. The pass runs once per process start and skips a directory whose marker comment already carries the current fold version, price table, and every folded session watermark, so a restart rewrites nothing; a session that records no working directory is skipped rather than written to a guessed path.
|
|
71
|
+
|
|
72
|
+
### The `/usage` command
|
|
73
|
+
|
|
74
|
+
`/usage` reports the session's four buckets, the estimated cost, and the context occupancy without calling the provider. It reaches every client through two surfaces:
|
|
75
|
+
|
|
76
|
+
- **Clients that execute commands** (the `dsh` CLI, the Web composer) run the registered command: no model message is created and nothing is settled.
|
|
77
|
+
- **Clients that can only send a message** (a phone client, a remote page that relays session messages) reach the model boundary, where the node half answers with a stream that reports no usage. The provider is never called, and because no settlement is written the context occupancy reading keeps its value.
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
- id: ui-token-usage
|
|
81
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
82
|
+
config:
|
|
83
|
+
command:
|
|
84
|
+
name: usage
|
|
85
|
+
labels:
|
|
86
|
+
total: 合计
|
|
87
|
+
input: 输入
|
|
88
|
+
cacheRead: 缓存读
|
|
89
|
+
cacheWrite: 缓存写
|
|
90
|
+
output: 输出
|
|
91
|
+
context: 上下文
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The message fallback matches a whole text block of the newest human message; the harness's instruction, context, and skill-catalog messages carry other source kinds and never match. A session whose `tokenUsage` projection is unavailable falls through to the model, and the command reports a short diagnostic instead of guessing. `command.enabled: false` removes both surfaces, and `command.name` renames both the command and its `/name` trigger.
|
|
95
|
+
|
|
96
|
+
-----
|
|
97
|
+
|
|
98
|
+
<a id="understand-the-implementation"></a>
|
|
99
|
+
## Understand the implementation
|
|
100
|
+
|
|
101
|
+
<details>
|
|
102
|
+
<summary>Implementation internals — click to expand</summary>
|
|
103
|
+
|
|
104
|
+
The browser half registers one entry on `conversation.composer.dock` (`id: 'token-usage'`, after the chat stats pills). It reads only framework hooks: `useProjection('tokenUsage')` for the four buckets, `useProjection('modelSelection')` for the route that produced them (preferring `lastUsed` over `next` because the last used route is the one the billed usage came from), and `useProjection('contextPressure')` for the occupancy cell, where `projectedTokens` wins over `pressureTokens` exactly as the conversation meter reads it. The plugin owns no state, issues no RPC, and adds no model-visible content.
|
|
105
|
+
|
|
106
|
+
The cost estimate bills uncached input plus cache writes at the miss rate, cache reads at the hit rate, and output at the output rate: `((uncached + cacheWrite) * inputMiss + cacheRead * inputHit + output * output) / 1e6`. Sub-fen amounts keep micro-precision rather than rounding to zero. The host half registers the configured table as the `tokenPricing` session projection, because browser plugin rows carry no `config`; the browser component reads it with `useProjection('tokenPricing')` alongside `tokenUsage` and `modelSelection`.
|
|
107
|
+
|
|
108
|
+
The host half also registers `tokenCostBackfill`: a fold over `request/header` events that reports whether the session used a priced route, which routes the price table misses, and the report path derived from the session header's working directory. The component reads it with `useProjection('tokenCostBackfill')` and renders a locale-owned notice under the readout whenever a route is unpriced. The backfill itself is a separate effect on the plugin's fiber: it resolves `ctx.sessionQuery` optionally, reads each archived log through `readSession`, and writes with a temporary file plus a rename. It folds peak and off-peak usage per settlement — an `assistant/message` or a failed `assistant/attempt`, whose usage arrives either in the event's own field or as the last `usage` chunk of its embedded stream. Within one turn and step a later settlement replaces the earlier one, except that `llm/retry-started` closes that replacement slot, so the attempt after a retry adds to the one it followed.
|
|
109
|
+
|
|
110
|
+
</details>
|
|
111
|
+
|
|
112
|
+
-----
|
|
113
|
+
|
|
114
|
+
<a id="further-exploration"></a>
|
|
115
|
+
## Further Exploration
|
|
116
|
+
|
|
117
|
+
Read these pages when the readout's numbers are not enough. They own the measurement and the composer surface it sits in.
|
|
118
|
+
|
|
119
|
+
- [dsh-token-meter](../../llm/token-meter/README.md) — the `tokenUsage` and `contextPressure` projections this package reads.
|
|
120
|
+
- [ui-chat](../ui-chat/README.md) — the composer statistics pills, including the total-token dialog this line complements.
|
|
121
|
+
- [Session Controller](../../api/session-controller/README.md) — owns the `modelSelection` projection and the projection read seat.
|
|
122
|
+
- [Web client architecture](../../../.agents/notes/implemented/architecture/2026-07-19-gui-web-client-architecture.md) — how browser plugin rows load and register slots.
|
|
123
|
+
|
|
124
|
+
-----
|
|
125
|
+
|
|
126
|
+
<a id="model-experience"></a>
|
|
127
|
+
## Model Experience
|
|
128
|
+
|
|
129
|
+
The message fallback is the package's only model-visible output: one assistant message assembled from already-logged projections, carrying no usage sample and never sent to a provider. The command surface and the browser readout stay outside model history.
|
|
130
|
+
|
|
131
|
+
#### KV Cache effect
|
|
132
|
+
|
|
133
|
+
None; the package never assembles or sends provider requests.
|
|
134
|
+
|
|
135
|
+
## Known Limitations and Deferred Work
|
|
136
|
+
|
|
137
|
+
<a id="known-limitations-and-deferred-work"></a>
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
These limits define how accurate the readout can be, not a billing-grade cost feature.
|
|
141
|
+
|
|
142
|
+
- **The cost is an estimate, not a bill** — provider prices change, and the provider's own statement is authoritative. Default prices are advisory and deployments override them through config.
|
|
143
|
+
- **Peak and off-peak rates are not separated** — the projection carries no per-sample timestamps, so one rate prices the whole conversation. A table filled with peak rates overstates off-peak usage by up to the provider's peak discount.
|
|
144
|
+
- **One route prices a multi-model conversation** — `modelSelection.lastUsed` is the newest consumed route, so a conversation that switched models between priced routes prices all tokens at the last one. A route-aware cost projection is deferred until pricing becomes a host-owned seam.
|
|
145
|
+
- **Counts are compact, not exact** — the line rounds above 1,000 tokens; the chat statistics pill's dialog remains the exact-integer surface.
|
|
146
|
+
- **The report is a snapshot** — both surfaces reflect the session state when they run, so a step still in flight is excluded and neither settles usage.
|
|
147
|
+
- **The backfill only counts usage the log records** — a settlement without a provider-reported `usage` contributes nothing, so `TOKEN_COST.md` can understate a conversation whose provider never reported one.
|
|
148
|
+
- **An unpriced route gets instructions, not an amount** — when no recorded route matches `backfill.pricing`, the report holds the routes that need a price and the YAML row to add, and the composer notice says so; nothing is guessed and no partial amount is written.
|
|
149
|
+
- **A session without a working directory is skipped** — the pass logs it and writes nothing, rather than falling back to the process working directory or the user's home.
|
|
150
|
+
- **The backfill splits peak and off-peak per settlement** — each usage sample is priced by its own event timestamp in Beijing time, which is what the single-rate readout above cannot do.
|
|
151
|
+
- **Every start re-reads the archived corpus** — the pass folds the stored logs itself instead of reusing a projection cache, so its read cost grows linearly with the archived session count; the marker saves the write, not the read.
|
|
152
|
+
|
|
153
|
+
<a id="dev-note"></a>
|
|
154
|
+
### Dev Note
|
|
155
|
+
|
|
156
|
+
<details>
|
|
157
|
+
<summary>Working context for maintainers — click to expand</summary>
|
|
158
|
+
|
|
159
|
+
None.
|
|
160
|
+
|
|
161
|
+
</details>
|
|
162
|
+
|
|
163
|
+
**Runtime invariant:** No companion is published. The package is a read-only projection of two durable session projections onto one composer-dock entry; it emits no Cordis events, owns no cross-plugin mutable state, and its single slot registration proves disposal through the HMR-safety spec.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: "Web token 用量读数:composer 下方一行,把当前对话的持久用量分类为四个计费桶并计价;供会话花费显示的用户与维护者阅读。"
|
|
3
|
+
kind: "package-reference"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @deepseek-ai/dsh-client-ui-token-usage
|
|
7
|
+
|
|
8
|
+
[English](README.md) | 中文
|
|
9
|
+
|
|
10
|
+
## 概述
|
|
11
|
+
|
|
12
|
+
本包在 Web GUI 的 composer 下方渲染一行 token 用量读数:当前对话的用量拆成四个互不相交的计费桶——未缓存输入、缓存读、缓存写、输出——再加一个估算花费。计数来自持久的 `tokenUsage` 投影,因此分页与压缩都不会改变它;slot 的会话作用域让读数按对话独立。花费来自本插件的 `pricing` 配置,按会话最后使用的模型匹配;该模型没有配置价格时不显示金额。宿主半边还会把 Web 从未显示的金额补出来:对存档语料跑一趟,给每个会话工作目录写一个 `TOKEN_COST.md`;当某条路由没有价格时,读数会指向该文件。
|
|
13
|
+
|
|
14
|
+
## 目录
|
|
15
|
+
|
|
16
|
+
- [使用本包](#use-this-package)
|
|
17
|
+
- [理解实现](#understand-the-implementation)
|
|
18
|
+
- [进一步探索](#further-exploration)
|
|
19
|
+
- [模型体验](#model-experience)
|
|
20
|
+
- [已知限制与延期工作](#known-limitations-and-deferred-work)
|
|
21
|
+
- [开发备注](#dev-note)
|
|
22
|
+
|
|
23
|
+
-----
|
|
24
|
+
|
|
25
|
+
<a id="use-this-package"></a>
|
|
26
|
+
## 使用本包
|
|
27
|
+
|
|
28
|
+
把本插件与 `@deepseek-ai/dsh-token-meter`、会话控制器一起挂载;此后只要对话已经产生计费用量,读数就会出现在 composer 下方。该行以会话合计开头,接着是非零桶的分类与估算花费,最后在计量器同时给出分子与路由容量时补上上下文占用。第一个计费步骤之前不渲染;缓存桶为零时省略;长行保持紧凑计数(517 / 12.3K / 1.2M)。
|
|
29
|
+
|
|
30
|
+
### 安装到 profile
|
|
31
|
+
|
|
32
|
+
把本包装成 profile 的直接依赖后,它会出现在 Web GUI 的已安装插件列表里,可以就地启用或卸载:
|
|
33
|
+
|
|
34
|
+
```powershell
|
|
35
|
+
node packages/client/ui-token-usage/scripts/pack-local.mjs
|
|
36
|
+
dsh plugin --profile <名字> add <脚本打印出的 .tgz 路径>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
脚本会暂存一份副本,把清单里所有 `workspace:` 范围改写为工作区版本并删掉 `devDependencies`——因为工作区里直接 `pnpm pack` 会在 `devDependencies` 里留下 `workspace:`,profile 安装时就会报 `ERR_PNPM_CANNOT_RESOLVE_WORKSPACE_PROTOCOL`。包声明了 `dsh.bundle.patch`,因此 profile 会从包自带的 `cordis.patch.yml` 挂载读数行,不需要手工插入。`desktop` profile 归 Electron 应用所有,按设计拒绝 `dsh plugin`;在那里请走应用自己的插件界面。
|
|
40
|
+
|
|
41
|
+
通过本插件的行配置设置价格。`pricing` 把模型 id 前缀映射到「元/百万 tokens」,最长的匹配前缀胜出,因此带日期的快照或 vision 路由可以共用或覆盖基础模型的价目:
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
- id: ui-token-usage
|
|
45
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
46
|
+
config:
|
|
47
|
+
currencySymbol: '¥'
|
|
48
|
+
pricing:
|
|
49
|
+
deepseek-v4-pro: { inputMiss: 9, inputHit: 0.3, output: 27 }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
默认值是 DeepSeek 官方价目的参考表(高峰价)。解析后的表里没有的模型只显示 token 计数、不显示金额,绝不会给出猜来的数字。
|
|
53
|
+
|
|
54
|
+
### 历史回填
|
|
55
|
+
|
|
56
|
+
宿主半边还会遍历存档会话,在每个会话自己的工作目录里写一个 `TOKEN_COST.md`:每个记录在该目录的对话一行,含四个计费桶、高峰与空闲金额、以及合计。`backfill.pricing` 用「元/百万 tokens」的 `[空闲, 高峰]` 价对给它计价,`backfill.peakHours` 替换北京时间的峰段(默认:工作日 09:00–12:00 与 14:00–18:00)。`backfill.enabled: false` 会同时关掉这次回填与它的浏览器提示。
|
|
57
|
+
|
|
58
|
+
```yaml
|
|
59
|
+
- id: ui-token-usage
|
|
60
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
61
|
+
config:
|
|
62
|
+
backfill:
|
|
63
|
+
pricing:
|
|
64
|
+
deepseek-v4-pro:
|
|
65
|
+
inputMiss: [4.5, 9]
|
|
66
|
+
inputHit: [0.15, 0.3]
|
|
67
|
+
output: [13.5, 27]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
当没有任何已记录路由命中价目时,同一个文件改为列出表里缺价格的路由以及可给它们定价的 YAML 行,composer 读数下方也会出现指向该文件的提示。回填每次进程启动跑一次,若目录里已有的标记注释仍带着当前折叠语义版本、价目表与全部会话水位,就直接跳过,因此重启不会重写任何文件;没有记录工作目录的会话会被跳过,而不会写进猜出来的路径。
|
|
71
|
+
|
|
72
|
+
### `/usage` 指令
|
|
73
|
+
|
|
74
|
+
`/usage` 报告会话的四个桶、估算花费与上下文占用,且不调用提供方。它通过两条通道抵达所有客户端:
|
|
75
|
+
|
|
76
|
+
- **会执行命令的客户端**(`dsh` CLI、Web composer)走注册命令:不创建模型消息,也不产生任何结算。
|
|
77
|
+
- **只能发消息的客户端**(手机客户端、只转发会话消息的远端页面)会到达模型边界,由 node 半边用一个**不携带 usage** 的流回答。提供方从不被调用;由于没有写入任何结算,上下文占用读数保持原值。
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
- id: ui-token-usage
|
|
81
|
+
name: '@deepseek-ai/dsh-client-ui-token-usage'
|
|
82
|
+
config:
|
|
83
|
+
command:
|
|
84
|
+
name: usage
|
|
85
|
+
labels:
|
|
86
|
+
total: 合计
|
|
87
|
+
input: 输入
|
|
88
|
+
cacheRead: 缓存读
|
|
89
|
+
cacheWrite: 缓存写
|
|
90
|
+
output: 输出
|
|
91
|
+
context: 上下文
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
消息回退按「最新一条人类消息的某个完整文本块整体相等」匹配;harness 注入的指令、上下文与技能目录消息带着其它 source kind,永远不会误命中。拿不到 `tokenUsage` 投影的会话会落回模型,而命令则返回一条简短诊断而不是猜数字。`command.enabled: false` 会同时移除两条通道,`command.name` 会同时改名命令与其 `/名字` 触发。
|
|
95
|
+
|
|
96
|
+
-----
|
|
97
|
+
|
|
98
|
+
<a id="understand-the-implementation"></a>
|
|
99
|
+
## 理解实现
|
|
100
|
+
|
|
101
|
+
<details>
|
|
102
|
+
<summary>实现细节——点击展开</summary>
|
|
103
|
+
|
|
104
|
+
浏览器半边在 `conversation.composer.dock` 上注册一个条目(`id: 'token-usage'`,排在 chat 统计药丸之后)。它只读框架 hook:`useProjection('tokenUsage')` 取四个桶,`useProjection('modelSelection')` 取产生这些用量的路由(优先 `lastUsed` 而非 `next`,因为计费用量来自最后实际使用的路由),`useProjection('contextPressure')` 取上下文占用格——其中 `projectedTokens` 优先于 `pressureTokens`,与会话计量器的读法完全一致。本插件不持有状态、不发 RPC,也不增加任何模型可见内容。
|
|
105
|
+
|
|
106
|
+
花费估算把未缓存输入加缓存写按未命中价、缓存读按命中价、输出按输出价计费:`((uncached + cacheWrite) * inputMiss + cacheRead * inputHit + output * output) / 1e6`。不足一分的金额保留微精度,不会被舍成零。宿主半边把配置好的价目表注册为 `tokenPricing` 会话投影,因为浏览器插件行不带 `config`;浏览器组件与 `tokenUsage`、`modelSelection` 一起用 `useProjection('tokenPricing')` 读取它。
|
|
107
|
+
|
|
108
|
+
宿主半边还注册 `tokenCostBackfill`:它折叠 `request/header` 事件,报告该会话是否用了有价路由、价目表漏掉了哪些路由、以及由会话 header 的工作目录推出的报告路径。组件用 `useProjection('tokenCostBackfill')` 读取它,并在存在无价路由时于读数下方渲染一条 locale 拥有的提示。回填本身是插件 fiber 上独立的 effect:它以可选方式解析 `ctx.sessionQuery`,用 `readSession` 读取每条存档日志,并以「临时文件 + rename」写盘。它按结算事件折叠高峰与空闲用量——`assistant/message` 或失败的 `assistant/attempt`,用量来自事件自身的字段,或来自其内嵌流里最后一个 `usage` chunk。同一 turn/step 内后一个结算会替换前一个,唯一的例外是 `llm/retry-started` 会关闭这个替换槽,因此重试后的 attempt 与其之前那次累加。
|
|
109
|
+
|
|
110
|
+
</details>
|
|
111
|
+
|
|
112
|
+
-----
|
|
113
|
+
|
|
114
|
+
<a id="further-exploration"></a>
|
|
115
|
+
## 进一步探索
|
|
116
|
+
|
|
117
|
+
当读数本身不够用时,请阅读以下页面。它们拥有测量与读数所在的 composer 界面。
|
|
118
|
+
|
|
119
|
+
- [dsh-token-meter](../../llm/token-meter/README.zh.md)——本包读取的 `tokenUsage` 与 `contextPressure` 投影。
|
|
120
|
+
- [ui-chat](../ui-chat/README.zh.md)——composer 统计药丸,包括本行所补充的总量用量弹层。
|
|
121
|
+
- [会话控制器](../../api/session-controller/README.zh.md)——拥有 `modelSelection` 投影与投影读取座位。
|
|
122
|
+
- [Web 客户端架构](../../../.agents/notes/implemented/architecture/2026-07-19-gui-web-client-architecture.zh.md)——浏览器插件行如何加载并注册 slot。
|
|
123
|
+
|
|
124
|
+
-----
|
|
125
|
+
|
|
126
|
+
<a id="model-experience"></a>
|
|
127
|
+
## 模型体验
|
|
128
|
+
|
|
129
|
+
消息回退是本包唯一的模型可见输出:一条由已记录投影拼出的 assistant 消息,不携带 usage 样本,也从不发给提供方。命令面与浏览器读数都留在模型历史之外。
|
|
130
|
+
|
|
131
|
+
#### KV Cache 影响
|
|
132
|
+
|
|
133
|
+
无;本包从不组装或发送提供方请求。
|
|
134
|
+
|
|
135
|
+
## 已知限制与延期工作
|
|
136
|
+
|
|
137
|
+
<a id="known-limitations-and-deferred-work"></a>
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
这些限制界定读数能有多准,本功能不是账单级计价。
|
|
141
|
+
|
|
142
|
+
- **花费是估算而不是账单**——提供方价格会变,以平台账单为准。默认价目是参考值,部署通过配置覆盖。
|
|
143
|
+
- **高峰与空闲价没有分开**——投影不带逐样本时间戳,所以整段对话按同一价目计价。用高峰价填表会把空闲时段的用量高估最多一个高峰期折扣。
|
|
144
|
+
- **多模型对话只按一个路由计价**——`modelSelection.lastUsed` 是最新被消费的路由,因此中途切换过模型(且两条路由都有价)的对话会按最后一条路由给全部 token 计价。路由感知的花费投影要等定价成为宿主拥有的 seam 后再做。
|
|
145
|
+
- **计数是紧凑值而不是精确值**——本行在 1,000 以上会取整;精确整数仍由 chat 统计药丸的弹层承担。
|
|
146
|
+
- **报告是快照**——两条通道都反映执行时刻的会话状态,仍在进行的步骤不计入,两者也都不产生任何结算。
|
|
147
|
+
- **回填只统计日志里记录的用量**——没有提供方上报 `usage` 的结算不贡献任何数字,所以 `TOKEN_COST.md` 可能低估那些提供方从未上报用量的对话。
|
|
148
|
+
- **无价路由得到的是说明而不是金额**——当没有任何已记录路由命中 `backfill.pricing` 时,报告给出缺价格的路由与可直接粘贴的 YAML 行,composer 提示也如此说明;不猜价格,也不写出部分金额。
|
|
149
|
+
- **没有工作目录的会话会被跳过**——回填只记日志、不写文件,不会退回到进程工作目录或用户主目录。
|
|
150
|
+
- **回填按样本区分高峰与空闲**——每个用量样本按自己的事件时间戳(北京时间)计价,这正是上面那条单一费率读数做不到的。
|
|
151
|
+
- **每次启动都会重读整个存档语料**——回填自己折叠存储的日志,而不复用投影缓存,因此读取成本随存档会话数线性增长;标记只省下写入,省不下读取。
|
|
152
|
+
|
|
153
|
+
<a id="dev-note"></a>
|
|
154
|
+
### 开发备注
|
|
155
|
+
|
|
156
|
+
<details>
|
|
157
|
+
<summary>维护者的工作上下文——点击展开</summary>
|
|
158
|
+
|
|
159
|
+
无。
|
|
160
|
+
|
|
161
|
+
</details>
|
|
162
|
+
|
|
163
|
+
**运行时不变式:** 不发布伴生入口。本包只是把两个持久会话投影以只读方式投影为一个 composer-dock 条目;不发出 Cordis 事件,不持有跨插件可变状态,其唯一的 slot 注册通过 HMR(热模块替换)安全性规范验证了资源释放行为。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Bundle patch: mount the readout row when this package is installed into a
|
|
2
|
+
# profile with `dsh plugin add`. The shipped web-app bundle mounts the same row;
|
|
3
|
+
# the Loader reuses an entry by id, so a profile that composes both stays a
|
|
4
|
+
# single entry.
|
|
5
|
+
- insert:
|
|
6
|
+
- id: ui-token-usage
|
|
7
|
+
name: '@jerry_chzy/dsh-token-usage'
|