dsh-plugin-usage 0.2.0 → 0.3.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
@@ -1,141 +1,72 @@
1
1
  # dsh-plugin-usage
2
2
 
3
- DeepSeek Harness 的 **Token 用量统计**插件(UI 参考 CC Switch 的「用量统计」功能)。
3
+ DeepSeek Harness 的 **Token 用量统计**插件:按天 / 自定义范围查看 Token、调用次数与成本,支持模型筛选、定价配置和年度热力图。
4
4
 
5
- > A **token usage statistics** plugin for DeepSeek Harness, inspired by CC Switch's usage stats.
6
- >
7
- > GitHub: [bennett-dsh/dsh-plugin-usage](https://github.com/bennett-dsh/dsh-plugin-usage) · npm: `dsh-plugin-usage`
5
+ > Token usage statistics for DeepSeek Harness.
8
6
 
9
- - 按天 / 自定义时间范围查看 Token 用量
10
- - 按模型筛选,查看调用次数、输入/输出 Token、缓存命中、费用
11
- - 可配置的每百万 Token 价格表(内置 DeepSeek 常见模型默认价,未配置的模型标记为「未定价」)
12
- - 深/浅色主题自适应(使用 DSH 主题变量),无第三方图表库(手写堆叠柱状图)
7
+ ![用量统计](image.png)
8
+ ![明细与热力图](image-1.png)
13
9
 
14
- > Features: daily / custom-range views · model filtering · call counts, in/out tokens, cache hits & cost · configurable per-million-token pricing (built-in defaults for common DeepSeek models; unpriced models are flagged) · dark/light theme via DSH tokens · zero chart dependencies (hand-written stacked bars).
10
+ ## 功能
15
11
 
16
- ## 功能特性 / Features
12
+ - 今日 / 近 7 天 / 近 30 天 / 全部 / 自定义范围(不限跨度),按小时·天聚合
13
+ - 模型筛选、按模型明细(请求数、输入/输出、缓存、成本、占比)
14
+ - KPI 网格里的「模型占比」卡:按总 Token 显示前 4 个模型与「其它」
15
+ - 参考成本可在 ¥ / $ 之间切换(固定汇率 1 USD = 7 CNY,宿主侧折算)
16
+ - 可配置的每百万 Token 价格表(内置 DeepSeek 常见模型默认价)
17
+ - 年度热力图;深/浅色自适应,只用 DSH 主题变量,无第三方图表库
17
18
 
18
- - 📊 **用量总览** — 总 Token、输入/输出 Token、调用次数、成本汇总卡片
19
- - 📅 **时间范围** — 今日 / 近 7 天 / 近 30 天 / 自定义范围
20
- - 🔍 **模型筛选** — 多选模型,按模型查看明细与占比
21
- - 📈 **每日趋势** — 按天堆叠柱状图(输入=蓝、输出=绿),hover 查看明细
22
- - 💰 **定价配置** — 为模型设置每百万 Token 价格(输入 / 输出 / 缓存命中 / 缓存创建),保存后立即生效
23
- - 🌗 **主题自适应** — 跟随 DSH 深/浅色主题,无第三方图表库
19
+ ## 安装
24
20
 
25
- ## 效果图
21
+ 插件已发布到 npm:`dsh-plugin-usage`。
26
22
 
27
- ![alt text](image.png)
23
+ - **DSH Desktop**:设置 → 插件 → 添加插件,输入 `dsh-plugin-usage`
24
+ - **Web / CLI profile**:`dsh plugin --profile <name> add dsh-plugin-usage`,重启生效
28
25
 
29
- ![alt text](image-1.png)
26
+ 升级同理(桌面端在插件页点更新,CLI 用 `update`)。
30
27
 
31
- ## 安装与构建 / Install & Build
28
+ > `desktop` profile 由 Electron 应用独占,不能用 `dsh plugin --profile desktop` 操作。
32
29
 
33
- ```sh
34
- npm install # 首次(devDependencies:typescript)
35
- npm run build # tsc 输出到 lib/,并生成 client 模块入口
36
- ```
30
+ ## 配置
37
31
 
38
- ### 通过官方命令安装(推荐)/ Install via the Official Command
32
+ 设置 → 插件 → `dsh-plugin-usage` 可改 `currency` 和 `prices`(每百万 Token 价格),写入 profile patch,重启保留。
33
+ `currency` 是默认显示币种;仪表盘上参考成本卡里的 ¥ / $ 切换只影响当前界面(同浏览器记住选择)。
39
34
 
40
- 插件已发布为 npm 包,使用 DSH 官方的插件管理命令安装:
35
+ 价格优先匹配 `provider/model`,其次裸 `model`;未配置的模型标为「未定价」,成本记 0。
41
36
 
42
- ```sh
43
- dsh plugin --profile web add dsh-plugin-usage
44
- dsh web # 重启后生效
45
- ```
37
+ ## 数据来源与口径
46
38
 
47
- 安装后 `dsh` 会检测到包的 `dsh.bundle` 声明,自动把 `dsh-plugin-usage` 加入
48
- profile 的 `dsh.profile.bundles` 层栈,无需手动编辑任何配置文件。卸载 / 更新:
39
+ 折叠 DSH 会话日志中已持久化的用量事件,不自己记日志:
49
40
 
50
- ```sh
51
- dsh plugin --profile web remove dsh-plugin-usage # 卸载
52
- dsh plugin --profile web update dsh-plugin-usage # 更新
53
- ```
41
+ - 读取走官方接口:活跃会话 `snapshotEvents()`,历史会话 `sessionPersistence.open/read`,按 `revision` 跳过未变日志
42
+ - 计费对齐官方 `tokenUsage` 投影:同一 `(turn, step)` 替换;`llm/retry-started` 后的重试累加;`assistant/attempt` 的 stream 用量也计入
43
+ - fork 会话按 `inheritedEventCount` 跳过父会话前缀,不重复计费
44
+ - 费用 = Σ(各 Token 分桶 × 对应单价) / 1e6
45
+ - 价目表以**美元 / 百万 Token** 为基准(与 DeepSeek 官方价一致);显示 `¥` 时按固定 7:1 折算,价目表本身不改写
54
46
 
55
- > 若 pnpm 因「发布时间过新」拒绝安装(`minimumReleaseAge` 校验),把
56
- > `dsh-plugin-usage@<version>` 加入 profile 的 `pnpm-workspace.yaml` 的
57
- > `minimumReleaseAgeExclude` 列表后重试(官方对新发布包同样采用此做法)。
47
+ ## 兼容性
58
48
 
59
- ### 从源码加载 / Load from Source
49
+ - DSH `>=0.2.0-rc.2 <0.3.0`(`engines.dsh`),Node ≥ 22
50
+ - 更早的宿主保留 `Session.events` / `readFrom` 回退路径
60
51
 
61
- 正式加载使用 patch 方式:把以下条目加入 `~/.dsh/profiles/web/cordis.patch.yml`(或使用 `--patch` 参数),路径替换为你本地的仓库路径:
62
-
63
- ```yaml
64
- - insert:
65
- - id: usage-host
66
- name: '/path/to/dsh-plugin-usage/lib/host/index.js'
67
- - id: usage-client
68
- name: '/path/to/dsh-plugin-usage/lib/client/index.js'
69
- ```
70
-
71
- 然后启动:
52
+ ## 开发
72
53
 
73
54
  ```sh
74
- dsh web --patch /path/to/dsh-plugin-usage/cordis.yml
55
+ npm install
56
+ npm run build # tsc → lib/,再生成 lib/client/index.js
57
+ npm test # node --test
75
58
  ```
76
59
 
77
- 开发期也可以在会话中通过动态插件(cordis_define)加载:Client UI 由 Host 的 `usage/client-source` RPC 从 `src/client/index.js` 实时读取并在浏览器中评估(UI 代码只有一份,改完刷新即生效)。
78
-
79
- ## 使用说明 / Usage
80
-
81
- 1. 打开设置(左下角)→ 左侧导航「用量统计」。
82
- 2. 顶部筛选:日期预设(今日 / 近 7 天 / 近 30 天 / 自定义)、模型多选。
83
- 3. 汇总卡:总 Token、输入 Token、输出 Token、调用次数、成本(未定价调用会在卡片下提示)。
84
- 4. 每日用量趋势:按天堆叠柱状图(输入=蓝、输出=绿),hover 查看明细。
85
- 5. 按模型明细表:请求数 / 输入 / 输出 / 总 Token / 成本 / 占比。
86
- 6. 「定价配置」区块:为模型设置每百万 Token 价格,保存后立即生效。
87
-
88
- ## 数据来源与口径 / Data Source
89
-
90
- 插件**不自己记录原始日志**,而是折叠 DSH 已持久化的会话日志:
91
-
92
- - DSH 的 agent loop 会把每次模型调用的用量以 `assistant/message` 会话事件(携带 `usage`:输入/输出/缓存命中/缓存创建 Token 与 `message.source.{provider,model}`)写入会话日志(`~/.dsh/sessions/`)。
93
- - 插件启动时**回填(backfill)全部历史会话**,之后每 2s 增量扫描活跃会话(内存),每 30s 增量扫描持久化会话(按 seq 水位)。
94
- - 覆盖范围:所有 agent loop 调用(含子代理)。compaction / session-title 等辅助调用不写入会话日志,不在统计范围内。
95
- - 去重:按会话内 `(turn, step)` 去重——同一 `(turn, step)` 的后续用量事件**替换**先前样本,而不是重复累计。
96
- - 费用 = `(输入×输入价 + 输出×输出价 + 缓存命中×缓存命中价 + 缓存创建×缓存创建价) / 1e6`,价格表可改。
97
- - 价格表持久化:官方 DSH settings 服务可用时,价格表经 `usage.prices` settings 命名空间持久化(重启后保留,并出现在官方 Plugins 配置页);否则回退到进程内存(仅本次运行有效)。
98
-
99
- > 动态插件形态下数据保存在进程内存中,重启后自动从会话日志重新回填,无需落盘文件。
100
-
101
- ## 源码结构 / Project Structure
60
+ CI 跑 install → build → test → bundle id 契约 → tarball 内容检查;推 `v*` tag 会校验版本、构建测试并创建 GitHub Release。
102
61
 
103
62
  ```
104
- src/
105
- ├── host/
106
- │ ├── index.js Host 入口:接线 fold / scan / query / settings / aggregate / rpc / client-source,驱动定时扫描
107
- │ ├── settings.js 价格表 + 费用计算(内置 DeepSeek 常见模型默认价,可选 settings 命名空间持久化)
108
- │ ├── state.js 运行时状态:records / watermarks / backfill / dedup / index
109
- │ ├── aggregate.js 增量聚合索引:add / remove / recordsInRange,供查询高效取数
110
- │ ├── fold.js 会话事件 → usage 记录(含会话内 (turn, step) 去重替换)
111
- │ ├── scan.js 启动回填 + 活跃/持久化会话的增量扫描(每 tick / 每 30s,按 seq 水位)
112
- │ ├── query.js 聚合与查询:按天/按模型汇总、未定价检测、heatmap
113
- │ ├── rpc.js RPC 注册:usage/query、usage/prices、usage/client-source(动态 harness 与静态 connection 桥)
114
- │ ├── client-source.js 读取 src/client/index.js 实时提供给浏览器(动态插件形态)
115
- │ └── time.js 时间分桶:day/hour/minute 粒度与 step 网格(纯函数)
116
- ├── client/index.js Client half:设置页「用量统计」仪表盘(筛选/汇总卡/柱状图/模型表/定价配置),样式内联
117
- └── client/styles.js UI 样式源(构建脚本已合并进 index.js,可删除)
118
- scripts/
119
- └── build-client.js 构建时生成 lib/client/index.js 模块入口(同一份 UI 代码的正式加载形态)
120
- cordis.patch.yml bundle patch:`dsh plugin add` 安装后由 `dsh.bundle` 声明自动激活
63
+ src/host/ 宿主:scan / fold / query / settings / aggregate / rpc
64
+ src/client/ 设置页「用量统计」仪表盘(纯 JS,样式内联)
65
+ scripts/ 构建客户端 bundle 入口
121
66
  ```
122
67
 
123
- ## 开发与测试 / Development & Testing
124
-
125
- ```sh
126
- npm install # 首次安装依赖(devDependencies:typescript)
127
- npm run build # tsc 输出到 lib/,再由 scripts/build-client.js 生成 client 模块入口
128
- npm test # node --test tests/
129
- ```
130
-
131
- `src/host/` 下的宿主逻辑(fold / scan / query / aggregate / time / settings 等)均为可直接
132
- import 的 ESM 模块,单元测试直接运行,无需启动 DSH harness。
133
-
134
- ## 说明 / Notes
135
-
136
- - 插件名 `usage`;动态插件运行在会话级,卸载后统计重新回填即可恢复。
137
- - 价格表持久化策略见「数据来源与口径」(官方 settings 可用时走 `usage.prices` 命名空间,否则进程内存回退)。
68
+ > 开发期也可用 `--patch` 从源码加载:`dsh web --patch cordis.yml`(路径见 `cordis.yml`)。
138
69
 
139
- ## 开源协议 / License
70
+ ## License
140
71
 
141
72
  [MIT](./LICENSE)
package/icon.svg ADDED
@@ -0,0 +1,11 @@
1
+ <svg width="36" height="36" viewBox="0 0 36 36" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Usage statistics">
2
+ <defs>
3
+ <linearGradient id="usage-bars" x1="8" y1="7.5" x2="28" y2="28" gradientUnits="userSpaceOnUse">
4
+ <stop stop-color="#67C7FE"/>
5
+ <stop offset="1" stop-color="#3F77D8"/>
6
+ </linearGradient>
7
+ </defs>
8
+ <rect x="8" y="20.5" width="5.5" height="7.5" rx="2.4" fill="url(#usage-bars)"/>
9
+ <rect x="15.25" y="14" width="5.5" height="14" rx="2.4" fill="url(#usage-bars)"/>
10
+ <rect x="22.5" y="7.5" width="5.5" height="20.5" rx="2.4" fill="url(#usage-bars)"/>
11
+ </svg>
package/image-1.png ADDED
Binary file
package/image.png ADDED
Binary file