@kenz1117/dsh-ui-usage-billing 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KenZ (kenz1117)
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 ADDED
@@ -0,0 +1,156 @@
1
+ # dsh-ui-usage-billing
2
+
3
+ DeepSeek Harness 计费仪表盘插件。从持久化会话日志实时聚合模型用量,按多厂商最新官方价格估算费用,在侧边栏一键查看完整仪表盘。
4
+
5
+ [![GitHub license](https://img.shields.io/github/license/kenz1117/dsh-ui-usage-billing)](https://github.com/kenz1117/dsh-ui-usage-billing/blob/main/LICENSE)
6
+ [![GitHub stars](https://img.shields.io/github/stars/kenz1117/dsh-ui-usage-billing)](https://github.com/kenz1117/dsh-ui-usage-billing)
7
+ [![GitHub last commit](https://img.shields.io/github/last-commit/kenz1117/dsh-ui-usage-billing)](https://github.com/kenz1117/dsh-ui-usage-billing)
8
+ [![npm version](https://img.shields.io/npm/v/@kenz1117/dsh-ui-usage-billing)](https://www.npmjs.com/package/@kenz1117/dsh-ui-usage-billing)
9
+
10
+ ## 特性
11
+
12
+ - **侧边栏入口**:设置按钮上方的触发胶囊,显示累计费用;折叠栏自动切换为图标。
13
+ - **计费仪表盘**:总费用卡片、KPI 指标(缓存命中率 / Token 总量 / 单次平均成本 / 调用次数)、按日趋势图、按模型计费明细、可展开的单价表。
14
+ - **真实用量**:服务端从会话日志实时聚合,无需手工维护统计文件。
15
+ - **模型健康探测**:模型行的圆点反映各厂商接入状态(正常绿 / 异常红 / 未接入灰)。
16
+ - **订阅计划豁免**:走 coding/token 套餐的模型照常统计 token,费用记 0。
17
+ - **离线自包含**:无图表库、无外部 CDN,全部使用设计令牌,适配深色/浅色主题。
18
+
19
+ ## 截图
20
+
21
+ ![计费仪表盘](screenshots/dashboard1.png)
22
+
23
+ ![计费仪表盘(单价表展开)](screenshots/dashboard2.png)
24
+
25
+ ![侧边栏入口](screenshots/trigger.png)
26
+
27
+ ## 快速开始
28
+
29
+ 在宿主 `cordis.patch.yml` 中加入:
30
+
31
+ ```yaml
32
+ - insert:
33
+ - id: ui-usage-billing
34
+ name: '@kenz1117/dsh-ui-usage-billing'
35
+ ```
36
+
37
+ 或通过包管理器安装:
38
+
39
+ ```sh
40
+ npm install @kenz1117/dsh-ui-usage-billing
41
+ ```
42
+
43
+ 启动宿主后,侧边栏设置上方即出现计费入口。无需额外配置;`sessionPersistence` 可用时自动聚合真实用量。
44
+
45
+ ## 工作原理
46
+
47
+ 插件由服务端与浏览器端两部分组成:
48
+
49
+ ```
50
+ 浏览器端 服务端(Node)
51
+ │ │
52
+ ├─ GET /api/billing/usage-stats ────────▶ ├─ sessionPersistence 遍历持久化会话日志
53
+ │ ├─ 按 request/header 归属模型
54
+ │ ├─ token 按缓存命中 / 未命中分桶
55
+ │ └─ 按单价表估算费用(人民币)
56
+ ├─ llm.models 健康探测 ─────────────────▶ └─ 返回聚合统计 JSON
57
+ └─ 渲染仪表盘
58
+ ```
59
+
60
+ - **服务端**(`src/index.ts`):注入 `webServer` 与 `sessionPersistence`,注册 `GET /api/billing/usage-stats`。每次调用折叠全部持久化日志:一次 LLM 调用归属到其前置 `request/header` 记录的模型,token 拆分到缓存命中 / 未命中桶,日期按本机时区归天。聚合逻辑见 `src/aggregate.ts`。
61
+ - **浏览器端**(`src/client/`):请求上述接口渲染仪表盘,通过 `llm.models` 探测各厂商连接状态。真实数据到达前显示全零空快照,不展示伪造样本。
62
+
63
+ ## 计费引擎
64
+
65
+ 单价表(`src/client/pricing.ts`)采用**原生币种**存储:国内厂商直接录入人民币价格,国外厂商录入美元价格。费用统一以人民币计算与展示——仅美元计价模型经过汇率(CFETS 中间价 6.79,2026-08-14),国内模型全程不经过汇率换算。
66
+
67
+ ```
68
+ cost(CNY)= (missInput × p_input + cacheHit × p_cacheHit + output × p_output) / 10⁶
69
+ —— 价格为原生币种;美元模型按 USD × 6.79 折算
70
+ ```
71
+
72
+ 统计中的 `input` 为总输入(cacheHit + cacheMiss),估算按命中 / 未命中分拆计价,避免重复计费。支持双档计费的模型按 `DEFAULT_PEAK_SHARE`(默认 0.5)混合高峰与低谷档。
73
+
74
+ ### 支持模型(2026-08-16 主流阵容,OpenAI 兼容系列)
75
+
76
+ | 厂商 | 模型 |
77
+ |---|---|
78
+ | DeepSeek | V4 Flash、V4 Pro(按时段峰谷计费:高峰 09:00-12:00 / 14:00-18:00 北京 = 低谷 2 倍) |
79
+ | 智谱 AI | GLM-5.3、GLM-5.2、GLM-4.6 |
80
+ | 阿里通义 | Qwen3.8 Max、Qwen3.7-Max、Qwen3.5-Plus、Qwen3.5-Flash |
81
+ | 字节豆包 | Doubao Seed-2.0 Pro、Seed-2.0 Mini、Seed-1.6 |
82
+ | 月之暗面 | Kimi K3、K2.7 Code、K2.7 Code HighSpeed、K2.6 |
83
+ | MiniMax | MiniMax-M3 |
84
+ | 百度 | ERNIE-5.1 |
85
+ | 腾讯 | 混元 T1、混元 Hy3 |
86
+ | 零一万物 | Yi-Lightning |
87
+ | 阶跃星辰 | Step 3.7 Flash |
88
+ | 科大讯飞 | Spark 4.0 Ultra(套餐制)¹ |
89
+ | 商汤 | SenseNova 6.5(公测中)¹ |
90
+ | 百川智能 | Baichuan M3-Plus |
91
+ | OpenAI | GPT-5.6 Sol / Terra / Luna |
92
+ | Google | Gemini 3.1 Pro、3.6 Flash(Standard / Flex 双档,Flex = -50%) |
93
+ | xAI | Grok 4.6、Grok 4.3 |
94
+ | Meta | Llama 4 Maverick、Scout |
95
+ | 其他 | 未收录模型的统一回退定价 |
96
+
97
+ > ¹ 讯飞、商汤未公布按量单价,表内为估算价,正式定价公布后自动校准。
98
+
99
+ 新增模型:在 `MODEL_CATALOG` 追加条目,并在 `src/aggregate.ts` 的 `MODEL_KEY_ALIASES` 中映射真实模型 id。
100
+
101
+ ## HTTP API
102
+
103
+ ### `GET /api/billing/usage-stats`
104
+
105
+ 聚合统计文档,浏览器端数据源:
106
+
107
+ ```json
108
+ {
109
+ "total": {
110
+ "calls": 733,
111
+ "input": 255931033,
112
+ "output": 414286,
113
+ "cacheHit": 255525760,
114
+ "cacheMiss": 405273,
115
+ "cost": 22.87
116
+ },
117
+ "byModel": {
118
+ "flash": { "calls": 733, "input": 255931033, "output": 414286, "cacheHit": 255525760, "cacheMiss": 405273, "cost": 22.87 }
119
+ },
120
+ "byDay": {
121
+ "2026-08-15": { "calls": 74, "input": 32593373, "output": 35375, "cacheHit": 32558208, "cacheMiss": 35165, "cost": 0.52 }
122
+ }
123
+ }
124
+ ```
125
+
126
+ 字段含义:`input` 为总输入 token;`cacheHit` / `cacheMiss` 为缓存命中 / 未命中分桶;`cost` 为人民币估算费用。`sessionPersistence` 不可用时回退到配置文件(见下)。
127
+
128
+ ## 配置
129
+
130
+ | 字段 | 默认 | 说明 |
131
+ |---|---|---|
132
+ | `statsPath` | 未设置 | 回退统计文件 `.dsh-usage-stats.json` 的绝对路径(`sessionPersistence` 不可用时生效) |
133
+
134
+ ## 开发
135
+
136
+ 环境要求:Node.js ^22.19 \|\| >=24,pnpm。
137
+
138
+ ```sh
139
+ pnpm install
140
+ pnpm --filter @kenz1117/dsh-ui-usage-billing bundle # 构建 lib/index.js 与 lib/client.js
141
+ npx vitest run packages/client/ui-usage-billing/tests # 单元测试
142
+ ```
143
+
144
+ ## 发布
145
+
146
+ 本包为独立 npm 包,发布后即可被其他 DeepSeek Harness 宿主安装。
147
+
148
+ ```sh
149
+ npm publish --access public
150
+ ```
151
+
152
+ 宿主通过 `package.json` 的 `dsh.client` 声明(`platform: web`)与 `exports["./client"]` bundle 自动发现浏览器端,无需注册中心登记。
153
+
154
+ ## 许可证
155
+
156
+ [MIT](LICENSE) © 2026 KenZ (kenz1117)