dsh-usage-prism 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 shizukunatsuki
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.md CHANGED
@@ -1,3 +1,144 @@
1
- # Temporary Holding Version
1
+ # dsh-usage-prism
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ dsh 的侧边栏用量面板:看 token 用量、时间去向与逐模型性能,按**本日 / 本周 / 本月 / 本年**四个尺度分开统计。
4
+
5
+ ![用量面板示意图:热力图、token 用量与时间去向两张卡、逐模型明细表](https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/banner.svg)
6
+
7
+ ## 设计取向
8
+
9
+ 这个插件只做一件事:**展示统计信息**。
10
+
11
+ 它读本机的会话日志,把已经发生的用量整理成看得懂的统计。按什么尺度切、列哪些指标、用什么形式画,都会随实际需要变化;不变的是它只呈现事实,不做判断。
12
+
13
+ 明确不做三件事:
14
+
15
+ - **不做计费统计。** 不把用量换算成金额。计费口径与余额接口分散在一家家 provider 手里,变动频繁;写死的换算会在某次上游调整后失效,剩下的只有错数。
16
+ - **不做预算与额度管理。** 不设阈值、不发提醒、不拦请求。那是策略层的事,该由别的工具去做。
17
+ - **不做预测和建议。** 不外推趋势,也不替人下「这个模型该换掉了」这样的结论。
18
+
19
+ 统计口径只跟 dsh 自己的投影走(见下面的「口径」),不发明指标。不记账、不管钱、不做决定。
20
+
21
+ ## 面板里有什么
22
+
23
+ - **热力图**:本日 24 格、本周 7 格、本月按日历、本年按周分列。颜色表示**本期内的相对高低**,不是绝对刻度;绝对值看悬浮卡和明细表。
24
+ - **量**(左卡):tokens 总数,拆成缓存读 / 新输入 / 输出三段;输入与输出的占比;缓存命中率;会话 / 轮次 / 步 / 次工具。
25
+ - **时间去向**(右卡):总时长 = 等待 + 生成 + 工具,三个桶各带占比。
26
+ - **明细表**:逐模型的 tokens、首 token 中位数、生成速度(t/s)、缓存命中率,点表头可排序。
27
+ - **悬浮卡**:鼠标停在格子上,看这一格的模型分布(前 3 名 + 其他)、输入输出、轮次 / 步 / 工具。
28
+
29
+ ## 效果图
30
+
31
+ 截图用的是合成数据,模型名是占位符。
32
+
33
+ ![本周视图:七个热力格子、token 用量与时间去向两张卡、逐模型明细表](https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/preview.png)
34
+
35
+ <table>
36
+ <tr>
37
+ <td width="50%" align="center"><img src="https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/images/day-light.png" alt="本日视图,鼠标停在 14:00 那一格上,弹出模型分布与输入输出" width="470"></td>
38
+ <td width="50%" align="center"><img src="https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/images/week-dark.png" alt="深色主题下的本周视图" width="470"></td>
39
+ </tr>
40
+ <tr>
41
+ <td align="center"><b>本日</b> · 24 格按小时,悬浮看明细</td>
42
+ <td align="center"><b>本周</b> · 深色主题</td>
43
+ </tr>
44
+ <tr>
45
+ <td width="50%" align="center"><img src="https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/images/month-light.png" alt="本月视图:按日历铺开的热力图" width="470"></td>
46
+ <td width="50%" align="center"><img src="https://raw.githubusercontent.com/shizukunatsuki/dsh-usage-prism/main/docs/images/year-light.png" alt="本年视图:按周分列的热力图" width="470"></td>
47
+ </tr>
48
+ <tr>
49
+ <td align="center"><b>本月</b> · 按日历铺开</td>
50
+ <td align="center"><b>本年</b> · 按周分列</td>
51
+ </tr>
52
+ </table>
53
+
54
+ ## 口径
55
+
56
+ 记账规则与 dsh 自带的 `@deepseek-ai/dsh-session-stats`、`tokenUsage` 投影一致,不自己发明:
57
+
58
+ | 指标 | 怎么算 |
59
+ |---|---|
60
+ | tokens | 未缓存输入 + 缓存读 + 缓存写 + 输出(输入侧 = 模型实际吃进去的量) |
61
+ | 等待 | 首个非空增量块 − `step/start` |
62
+ | 生成 | `assistant/message` 落盘 − 首个非空增量块 |
63
+ | 工具 | `tool/result` − `tool/call` |
64
+ | 轮次 | 每轮的第一个 `step/end` 记一次 |
65
+ | 步 | 每个 `step/end` 记一次(取消、失败、max-tokens 的步骤也各算一步) |
66
+
67
+ 三个时间桶相加恰好等于投影里的模型时间加工具时间,没有第四桶。同一 `(turn, step)` 的用量样本后写覆盖前写,与 `tokenUsage` 投影相同。缓存命中率 = `cacheRead / 输入侧总量`。
68
+
69
+ `npm run test:conformance` 会拿本机真实日志逐会话、逐项与 dsh 的投影核对(要求完全相等,不是近似)。
70
+
71
+ ## 安装
72
+
73
+ 需要 dsh `^0.2.3-alpha.3`。打开 **Plugins** 页 → **Add plugin**,填 GitHub 地址或本地目录都可以:
74
+
75
+ | 填什么 | 例子 |
76
+ |---|---|
77
+ | npm 包名 | `dsh-usage-prism` |
78
+ | GitHub 仓库地址 | `https://github.com/shizukunatsuki/dsh-usage-prism` |
79
+ | 本地目录(绝对路径) | `/your/path/dsh-usage-prism` |
80
+
81
+ ## 使用
82
+
83
+ 装好后 **Plugins** 页多一行「用量」,左侧栏也多一个同名图标(没出现就重启一次 dsh)。点开就是面板:
84
+
85
+ - 顶部的四个 chip 切尺度:本日 / 本周 / 本月 / 本年。
86
+ - 鼠标停在热力格子上看这一格的明细。
87
+ - 点明细表的表头排序,再点一次反向。
88
+ - 第一次打开要读一遍全部会话日志,会话多的时候要几秒;之后靠摘要缓存,只重折变过的会话。
89
+
90
+ ## 数据与隐私
91
+
92
+ - 只读:会话来自 `ctx.sessionQuery`,本机日志由 dsh 自己读,插件不写会话、不追加事件类型、不联网。
93
+ - 插件起的 HTTP 端点 `/api/usage-prism` 要求 dsh 的鉴权(`auth: 'required'`),不是公开路由。
94
+ - 唯一的写入是自己那份可以丢掉的摘要缓存:
95
+ - macOS:`~/Library/Caches/dsh-usage-prism/summaries.json`
96
+ - 其他平台:`$XDG_CACHE_HOME/dsh-usage-prism/summaries.json`,没设 XDG 时是 `~/.cache/dsh-usage-prism/summaries.json`
97
+ - 删掉它只会让下一次打开面板慢一点,不会丢任何数据。
98
+ - 卸载插件,面板整块消失。
99
+
100
+ ## 已知限制
101
+
102
+ - **fork 出来的会话会把父会话的历史整段带在日志里**,而折叠按「每条日志独立计算」,所以同一段历史在父会话和每个 fork 副本里各算一次,跨会话合计会偏高。单个会话自己的数字不受影响。
103
+ - 「本日」「本周」等都按本机时区的本地日历算,不跨时区归一。
104
+ - 只统计本机:换一台机器就是另一份数据。
105
+
106
+ ## 开发
107
+
108
+ ```sh
109
+ npm test # 折叠口径、渲染、缓存三组测试(不需要真实日志)
110
+ npm run test:conformance # 可选:拿本机真实日志与 dsh 投影对账
111
+ npm run demo # 重新生成 README 里的截图(需要本机 Chrome)
112
+ ```
113
+
114
+ `npm test` 只用合成事件和假 React,跑在哪台机器上结果都一样;`test:conformance` 需要本机有 dsh 的会话日志与投影缓存,找不到会直接跳过。
115
+
116
+ `npm run demo` 生成一套确定性的合成会话,渲染成静态 HTML 后用 Chrome 截图到 `docs/`。主题变量优先取本机 dsh 的主题包,找不到时用内置的近似配色。
117
+
118
+ | 路径 | 是什么 |
119
+ |---|---|
120
+ | `index.js` | Host 半边:一次只读快照 + 摘要缓存 |
121
+ | `lib/fold.js` | 纯折叠:事件数组 → 按天 / 按小时 / 按模型的计数 |
122
+ | `client.js` | Client 半边:侧边栏图标与面板本体 |
123
+ | `test/*.test.mjs` | 口径、渲染、缓存三组测试 |
124
+ | `test/conformance.mjs` | 与 dsh 投影逐项对账(可选) |
125
+ | `demo/` | 合成数据与截图脚本(`npm run demo`) |
126
+ | `locale/`、`icon.svg`、`docs/` | Plugins 页的标题、描述、图标与预览 |
127
+
128
+ 改 `lib/fold.js` 的记账规则时,记得把 `FOLD_VERSION` 加一——它会作为磁盘摘要键的前缀,不加的话旧摘要会被当成「会话没变化」继续复用。
129
+
130
+ ## 发布
131
+
132
+ 改 `package.json` 的 `version` 推到 `main` 就会发布——`.github/workflows/publish.yml` 会先跑测试,再看这个版本是不是已经在 registry 上。打 `v*` tag 走同一条流水线,也可以在 Actions 里手动触发。
133
+
134
+ 用的是 npm 的 Trusted Publishing:仓库里不存任何长期令牌,发布权限来自 GitHub 的 OIDC 短期凭证。包还没在 npm 上注册时,流水线只留一条提示、不报错。
135
+
136
+ 第一次发布需要手动一次(registry 上还不存在的包无法绑定 Trusted Publisher):
137
+
138
+ 1. 本地 `npm login`,然后 `npm publish`
139
+ 2. 在 npm 的包设置 → Trusted Publishing 填:Organization or user `shizukunatsuki`、Repository `dsh-usage-prism`、Workflow filename `publish.yml`
140
+ 3. 之后只改版本号、push
141
+
142
+ ## 许可
143
+
144
+ MIT