dsh-cost-meter 1.9.80 → 1.9.82
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 +28 -4
- package/README.zh-CN.md +28 -4
- package/docs/images/README.md +6 -2
- package/docs/images/sidebar-compact-en.png +0 -0
- package/docs/images/sidebar-compact-zh.png +0 -0
- package/docs/images/sidebar-icons-deepseek-off-peak.png +0 -0
- package/docs/images/sidebar-icons-deepseek-open.png +0 -0
- package/docs/images/sidebar-icons-deepseek-peak.png +0 -0
- package/docs/images/sidebar-icons-en.png +0 -0
- package/docs/images/sidebar-icons-zh.png +0 -0
- package/docs/images/sidebar-simple-en.png +0 -0
- package/docs/images/sidebar-simple-zh.png +0 -0
- package/docs/images/sidebar-standard-en.png +0 -0
- package/docs/images/sidebar-standard-zh.png +0 -0
- package/docs/images/sidebar-summary-en.png +0 -0
- package/docs/images/sidebar-summary-zh.png +0 -0
- package/docs/plugin-compatibility-watch.md +4 -0
- package/docs/release-notes/v1.9.81.md +13 -0
- package/docs/release-notes/v1.9.82.md +16 -0
- package/lib/backfill.js +75 -25
- package/lib/billing-stream.js +6 -1
- package/lib/client.js +2 -2
- package/lib/coding-plans.js +36 -8
- package/lib/index.js +78 -20
- package/lib/ledger-writer-worker.js +1 -1
- package/lib/pricing.js +49 -0
- package/lib/session-cost-source.js +2 -1
- package/lib/session-storage-compat.js +3 -1
- package/lib/session-usage-cache.js +2 -2
- package/lib/statistics-cache.js +2 -2
- package/lib/store.js +46 -11
- package/lib/turn-cost.js +7 -4
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<p align="center">Cost tracking and subscription quotas for DeepSeek Harness</p>
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
|
-
<a href="https://github.com/Han-1413141/dsh-cost-meter/releases/latest"><img src="https://img.shields.io/badge/version-1.9.
|
|
6
|
+
<a href="https://github.com/Han-1413141/dsh-cost-meter/releases/latest"><img src="https://img.shields.io/badge/version-1.9.82-4176E6?style=flat-square" alt="version"></a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/dsh-cost-meter"><img src="https://img.shields.io/npm/dm/dsh-cost-meter?label=downloads&style=flat-square&color=4176E6" alt="npm monthly downloads"></a>
|
|
8
8
|
<a href="https://github.com/Han-1413141/dsh-cost-meter/stargazers"><img src="https://img.shields.io/github/stars/Han-1413141/dsh-cost-meter?label=stars&style=flat-square&color=D9A400" alt="GitHub stars"></a>
|
|
9
9
|
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="MIT license"></a>
|
|
@@ -42,20 +42,20 @@ Restart `dsh web` after installation or updates. **Desktop** uses its bundled CL
|
|
|
42
42
|
The PowerShell installer checks pnpm and Git and uses the `web` profile by default:
|
|
43
43
|
|
|
44
44
|
```powershell
|
|
45
|
-
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.
|
|
45
|
+
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.82/install.ps1 | iex
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
Install a pinned Git tag:
|
|
49
49
|
|
|
50
50
|
```sh
|
|
51
|
-
dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.
|
|
51
|
+
dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.82
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
See [installation and updates](docs/install.en.md) for uninstalling or installing without Git.
|
|
55
55
|
|
|
56
56
|
</details>
|
|
57
57
|
|
|
58
|
-
Current release **v1.9.
|
|
58
|
+
Current release **v1.9.82**: [release notes](docs/release-notes/v1.9.82.md) · [troubleshooting](docs/install-troubleshooting.md)
|
|
59
59
|
|
|
60
60
|
## Feature overview
|
|
61
61
|
|
|
@@ -70,6 +70,8 @@ See [features and screenshots](docs/features.en.md) for entry points and [model
|
|
|
70
70
|
|
|
71
71
|
## Screenshots & walkthrough
|
|
72
72
|
|
|
73
|
+
### Main views
|
|
74
|
+
|
|
73
75
|
| Cost trends and model ranking | Conversation cost distribution |
|
|
74
76
|
|---|---|
|
|
75
77
|
| [](docs/images/statistics.png) | [](docs/images/treemap.png) |
|
|
@@ -80,6 +82,28 @@ See [features and screenshots](docs/features.en.md) for entry points and [model
|
|
|
80
82
|
|
|
81
83
|
Select an image for full resolution. [More views: call details, prices, history, dark themes and mobile](docs/features.en.md).
|
|
82
84
|
|
|
85
|
+
### Sidebar layouts
|
|
86
|
+
|
|
87
|
+
Switch layouts under **Settings → Cost → Display → Sidebar → Sidebar layout**. Each screenshot uses the same example balance, quota and usage.
|
|
88
|
+
|
|
89
|
+
| Standard | Compact |
|
|
90
|
+
|:---:|:---:|
|
|
91
|
+
| <a href="docs/images/sidebar-standard-en.png"><img src="docs/images/sidebar-standard-en.png" width="200" alt="Standard sidebar with stacked quota rows"></a> | <a href="docs/images/sidebar-compact-en.png"><img src="docs/images/sidebar-compact-en.png" width="200" alt="Compact sidebar with two-column quota rows"></a> |
|
|
92
|
+
|
|
93
|
+
| Simple | Combined card |
|
|
94
|
+
|:---:|:---:|
|
|
95
|
+
| <a href="docs/images/sidebar-simple-en.png"><img src="docs/images/sidebar-simple-en.png" width="200" alt="Simple sidebar with shorter rows"></a> | <a href="docs/images/sidebar-summary-en.png"><img src="docs/images/sidebar-summary-en.png" width="200" alt="Combined sidebar card with usage, balance and pricing period"></a> |
|
|
96
|
+
|
|
97
|
+
### Icons layout
|
|
98
|
+
|
|
99
|
+
The DeepSeek icon shows the balance and the current pricing period. Select it to open the details.
|
|
100
|
+
|
|
101
|
+
| Off-peak | Peak | Open during peak |
|
|
102
|
+
|:---:|:---:|:---:|
|
|
103
|
+
| [](docs/images/sidebar-icons-deepseek-off-peak.png) | [](docs/images/sidebar-icons-deepseek-peak.png) | [](docs/images/sidebar-icons-deepseek-open.png) |
|
|
104
|
+
|
|
105
|
+
Thanks to [@chanyuenpang](https://github.com/chanyuenpang) for proposing and implementing the Icons sidebar layout.
|
|
106
|
+
|
|
83
107
|
## Documentation
|
|
84
108
|
|
|
85
109
|
| Topic | Guides |
|
package/README.zh-CN.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<p align="center">DeepSeek Harness 费用统计与订阅额度插件</p>
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
|
-
<a href="https://github.com/Han-1413141/dsh-cost-meter/releases/latest"><img src="https://img.shields.io/badge/version-1.9.
|
|
6
|
+
<a href="https://github.com/Han-1413141/dsh-cost-meter/releases/latest"><img src="https://img.shields.io/badge/version-1.9.82-4176E6?style=flat-square" alt="version"></a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/dsh-cost-meter"><img src="https://img.shields.io/npm/dm/dsh-cost-meter?label=downloads&style=flat-square&color=4176E6" alt="npm monthly downloads"></a>
|
|
8
8
|
<a href="https://github.com/Han-1413141/dsh-cost-meter/stargazers"><img src="https://img.shields.io/github/stars/Han-1413141/dsh-cost-meter?label=stars&style=flat-square&color=D9A400" alt="GitHub stars"></a>
|
|
9
9
|
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="MIT license"></a>
|
|
@@ -42,20 +42,20 @@ dsh plugin --profile web add dsh-cost-meter
|
|
|
42
42
|
PowerShell 脚本自动检查 pnpm 和 Git,默认安装到 `web` Profile:
|
|
43
43
|
|
|
44
44
|
```powershell
|
|
45
|
-
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.
|
|
45
|
+
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.82/install.ps1 | iex
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
固定 Git tag 安装:
|
|
49
49
|
|
|
50
50
|
```sh
|
|
51
|
-
dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.
|
|
51
|
+
dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.82
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
更新、卸载及无 Git 的安装方法见[安装说明](docs/install.md)。
|
|
55
55
|
|
|
56
56
|
</details>
|
|
57
57
|
|
|
58
|
-
当前版本 **v1.9.
|
|
58
|
+
当前版本 **v1.9.82**:[更新说明](docs/release-notes/v1.9.82.md) · [安装排障](docs/install-troubleshooting.md)
|
|
59
59
|
|
|
60
60
|
## 功能总览
|
|
61
61
|
|
|
@@ -70,6 +70,8 @@ dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.80
|
|
|
70
70
|
|
|
71
71
|
## 图文演示
|
|
72
72
|
|
|
73
|
+
### 主要界面
|
|
74
|
+
|
|
73
75
|
| 费用趋势与模型排行 | 单个会话费用分布 |
|
|
74
76
|
|---|---|
|
|
75
77
|
| [](docs/images/statistics.png) | [](docs/images/treemap.png) |
|
|
@@ -80,6 +82,28 @@ dsh plugin --profile web add github:Han-1413141/dsh-cost-meter#v1.9.80
|
|
|
80
82
|
|
|
81
83
|
点击图片查看大图。[更多界面:调用详情、价格、历史、深色与手机端](docs/features.md)
|
|
82
84
|
|
|
85
|
+
### 侧栏布局
|
|
86
|
+
|
|
87
|
+
在 **设置 → 费用 → 显示 → 侧栏 → 侧栏布局** 中切换。各张截图使用同一组示例余额、套餐额度和用量。
|
|
88
|
+
|
|
89
|
+
| 标准 | 紧凑 |
|
|
90
|
+
|:---:|:---:|
|
|
91
|
+
| <a href="docs/images/sidebar-standard-zh.png"><img src="docs/images/sidebar-standard-zh.png" width="200" alt="标准布局,套餐额度纵向排列"></a> | <a href="docs/images/sidebar-compact-zh.png"><img src="docs/images/sidebar-compact-zh.png" width="200" alt="紧凑布局,套餐额度两列排列"></a> |
|
|
92
|
+
|
|
93
|
+
| 简洁 | 合并卡片 |
|
|
94
|
+
|:---:|:---:|
|
|
95
|
+
| <a href="docs/images/sidebar-simple-zh.png"><img src="docs/images/sidebar-simple-zh.png" width="200" alt="简洁布局,以短行显示用量和额度"></a> | <a href="docs/images/sidebar-summary-zh.png"><img src="docs/images/sidebar-summary-zh.png" width="200" alt="合并卡片布局,同一张卡片显示用量、余额和峰谷时段"></a> |
|
|
96
|
+
|
|
97
|
+
### 图标布局
|
|
98
|
+
|
|
99
|
+
DeepSeek 图标显示余额和当前计价时段,点击可展开明细。
|
|
100
|
+
|
|
101
|
+
| 平价时段 | 高峰时段 | 高峰时段展开余额 |
|
|
102
|
+
|:---:|:---:|:---:|
|
|
103
|
+
| [](docs/images/sidebar-icons-deepseek-off-peak.png) | [](docs/images/sidebar-icons-deepseek-peak.png) | [](docs/images/sidebar-icons-deepseek-open.png) |
|
|
104
|
+
|
|
105
|
+
感谢 [@chanyuenpang](https://github.com/chanyuenpang) 提出并实现侧栏图标布局。
|
|
106
|
+
|
|
83
107
|
## 文档
|
|
84
108
|
|
|
85
109
|
| 主题 | 文档 |
|
package/docs/images/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# 界面截图 / UI screenshots
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
会话和功能截图选自 2026-10-25 的项目配图库,由实际界面组件和示例数据生成。金额、账户余额、套餐百分比和会话内容用于展示功能。当前安装与功能说明以文档正文为准。
|
|
4
4
|
|
|
5
|
-
The images come from the project's 2026-10-25 screenshot collection, rendered with application components and sample data. Amounts, balances, quota percentages and conversation content are examples.
|
|
5
|
+
The conversation and feature images come from the project's 2026-10-25 screenshot collection, rendered with application components and sample data. Amounts, balances, quota percentages and conversation content are examples.
|
|
6
|
+
|
|
7
|
+
各侧栏布局截图来自 DeepSeek Harness Web,使用同一组示例数据。`sidebar-icons-deepseek-*.png` 取自维护者提供的合并后演示图。[PR #548](https://github.com/Han-1413141/dsh-cost-meter/pull/548)
|
|
8
|
+
|
|
9
|
+
The sidebar layout screenshots show dsh-cost-meter running in DeepSeek Harness Web with the same example data. The `sidebar-icons-deepseek-*.png` images come from the maintainer’s post-merge comparison. [PR #548](https://github.com/Han-1413141/dsh-cost-meter/pull/548)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -124,3 +124,7 @@ SHA-512 校验、静态契约检查和实际发布包回归通过:原生及代
|
|
|
124
124
|
### 2026-11-20:Codex Subscription 3.2.26 与 Connect 0.2.0-alpha.16
|
|
125
125
|
|
|
126
126
|
[#551](https://github.com/Han-1413141/dsh-cost-meter/issues/551) 与 [#552](https://github.com/Han-1413141/dsh-cost-meter/issues/552) 的 npm 发布包 SHA-512 校验通过。Subscription 移除了 `dsh-home-paths` 依赖。Connect 调整了宿主版本范围并移除了 `./invariant` 导出,其当前宿主声明不再包含 DSH `0.2.0-rc.2`。额度相关的函数指纹一致。Subscription 在两套宿主传输上通过额度解析、刷新和授权失效回归,Connect 通过发布包的额度解析及只读 HTTP 回归。测试使用本机数据。
|
|
127
|
+
|
|
128
|
+
### 2026-11-21:Codex Subscription 3.2.27
|
|
129
|
+
|
|
130
|
+
[#560](https://github.com/Han-1413141/dsh-cost-meter/issues/560) 报告 `3.2.27` 发布。npm 发布包 SHA-512 校验通过,静态契约匹配。`createSubscriptionRpcHandler` 仅在 `preferences/update` 分支新增了 `customInstructions` 校验,`usage` 额度分支及 `registerSubscriptionTransport`、`parseCodexUsage`、`createCodexUsageReader` 三个额度函数与 `3.2.26` 一致。使用发布包在 DSH `0.2.0-rc.2` 和 `0.2.2-alpha.3` 上通过额度解析、强制刷新、账号切换、授权失效恢复与宿主 RPC 回归;兼容基线与两套 CI 矩阵已加入 `3.2.27`。测试仅使用本机数据,外部请求为 0。
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
## v1.9.81 更新
|
|
2
|
+
|
|
3
|
+
- **侧栏布局截图**。中英文仓库首页增加标准、紧凑、简洁、合并卡片与图标布局截图,使用同一组示例用量、余额和套餐额度。
|
|
4
|
+
|
|
5
|
+
## 升级
|
|
6
|
+
|
|
7
|
+
已安装用户在任意终端重跑一行即可(重跑即为更新):
|
|
8
|
+
|
|
9
|
+
```powershell
|
|
10
|
+
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.81/install.ps1 | iex
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
升级后**重启 dsh web** 使插件服务端代码生效,浏览器端刷新页面即可。
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
## v1.9.82 更新
|
|
2
|
+
|
|
3
|
+
- **路由响应模型计价回落**:当配置的路由模型 ID(如 `openrouter/auto`)无法识别计价时,自动回落使用供应商实际返回的 `responseModel`(实时流 `finish.replayState.response.responseModel` 与会话日志 `assistant/message` / `compaction/summary`)计算费用并归属到实际模型;已定价、本地、显式免费路由与手动覆盖/动态报价配置保持原样。感谢 [@res0Nanz](https://github.com/res0Nanz) 在 [#558](https://github.com/Han-1413141/dsh-cost-meter/issues/558) 提出需求并指明日志字段路径。
|
|
4
|
+
- **Kimi 降级占位响应保护**:忽略 Kimi Code 上游偶发返回的 68 字节降级占位响应(无 `usage`/`limits` 计数块与 `reset_time` 且 `used_ratio` 恒为 0),保留上一次有效额度读数与采样时间并在冷却后自动重试,不再将真实周额度覆盖为 0%。感谢 [@yzchangle](https://github.com/yzchangle) 在 [#559](https://github.com/Han-1413141/dsh-cost-meter/issues/559) 提供实测响应样本、复现步骤与解析定位。
|
|
5
|
+
- **总览页「今日会话」标题行布局**:将「导出费用记录」按钮对齐到「今日会话」标题行右侧,并与下方会话列表保持统一分组间距。感谢 [@y2bz](https://github.com/y2bz) 在 [#561](https://github.com/Han-1413141/dsh-cost-meter/issues/561) 报告布局问题、附截图并定位组件位置。
|
|
6
|
+
- **Codex Subscription 3.2.27 兼容核验**:验证 `dsh-codex-subscription` 3.2.27 的 npm 发布包 SHA-512 与两套 DSH 宿主回归,更新兼容基线与 CI 矩阵。版本及接口指纹变化由 [github-actions[bot]](https://github.com/apps/github-actions) 在 [#560](https://github.com/Han-1413141/dsh-cost-meter/issues/560) 报告。
|
|
7
|
+
|
|
8
|
+
## 升级
|
|
9
|
+
|
|
10
|
+
已安装用户在任意终端重跑一行即可(重跑即为更新):
|
|
11
|
+
|
|
12
|
+
```powershell
|
|
13
|
+
irm https://raw.githubusercontent.com/Han-1413141/dsh-cost-meter/v1.9.82/install.ps1 | iex
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
升级后**重启 dsh web** 使插件服务端代码生效,浏览器端刷新页面即可。
|
package/lib/backfill.js
CHANGED
|
@@ -17,7 +17,7 @@ import { totalUsageTokens } from './usage-totals.js'
|
|
|
17
17
|
import * as zlib from 'node:zlib'
|
|
18
18
|
import { hasRecordedQuotes, findRecordedQuote } from './dynamic-pricing.js'
|
|
19
19
|
import { attemptUsage, forkSeedLength } from './session-billing-events.js'
|
|
20
|
-
import { tierFor, costOf, providerPriceEntryFor, usdFromCost, isWrapperProviderId, wrapperUpstreamProvider } from './pricing.js'
|
|
20
|
+
import { tierFor, costOf, providerPriceEntryFor, usdFromCost, isWrapperProviderId, wrapperUpstreamProvider, responseModelOfEvent, resolveResponseModelFallback } from './pricing.js'
|
|
21
21
|
import { localDayKey, zeroDay, splitLedgerApiCost } from './store.js'
|
|
22
22
|
import { billingClassOf, enabledPlanSetOf, planProviderIdOf, isRoutedThirdPartyCall, appendHourBucket, planUsageFingerprint, HOUR_BUCKET_RETENTION_MS, localMidnightMs } from './plan-billing.js'
|
|
23
23
|
import { USAGE_DEDUP_WINDOW_MS, usageFingerprint } from './usage-dedup.js'
|
|
@@ -494,10 +494,10 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
494
494
|
let last = null
|
|
495
495
|
const days = {}
|
|
496
496
|
const seedDays = {}
|
|
497
|
-
const shift = (sample, sign, target) => {
|
|
497
|
+
const shift = (sample, sign, target, pruneEmpty = false) => {
|
|
498
498
|
const dayMap = target[sample.date] ?? (target[sample.date] = {})
|
|
499
499
|
const current = dayMap[sample.providerKey] ?? zeroBuckets()
|
|
500
|
-
|
|
500
|
+
const next = {
|
|
501
501
|
...addFrozen({ ...frozenFields(current) }, sample, sign),
|
|
502
502
|
input: current.input + sign * sample.buckets.input,
|
|
503
503
|
output: current.output + sign * sample.buckets.output,
|
|
@@ -508,6 +508,13 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
508
508
|
cost: current.cost + sign * sample.cost,
|
|
509
509
|
apiCost: (current.apiCost ?? 0) + sign * (sample.apiCost ?? sample.cost),
|
|
510
510
|
}
|
|
511
|
+
if (pruneEmpty && sign < 0 && next.calls === 0 && next.input === 0 && next.output === 0 && next.cacheRead === 0
|
|
512
|
+
&& next.cacheWrite === 0 && next.reasoning === 0 && next.cost === 0 && next.apiCost === 0) {
|
|
513
|
+
delete dayMap[sample.providerKey]
|
|
514
|
+
if (Object.keys(dayMap).length === 0) delete target[sample.date]
|
|
515
|
+
} else {
|
|
516
|
+
dayMap[sample.providerKey] = next
|
|
517
|
+
}
|
|
511
518
|
}
|
|
512
519
|
// 两遍扫预扫描(issue #76):按与主循环一致的 header 状态机,收集全部非包装
|
|
513
520
|
// provider 的 (model, 五桶) 指纹及其事件时刻。主循环遇到包装层 provider 的
|
|
@@ -515,10 +522,12 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
515
522
|
// 流无论出现在包装层事件之前还是之后,转发对都只计一次(与实时投影/账本
|
|
516
523
|
// 入账钩子的指纹窗口去重同语义,保证回放与实时逐位一致)。
|
|
517
524
|
const plainFingerprints = new Map()
|
|
525
|
+
let preSessionId = ''
|
|
518
526
|
let preProvider = 'deepseek'
|
|
519
527
|
let preModel = 'default'
|
|
520
528
|
for (const event of records) {
|
|
521
529
|
if (event === null || typeof event !== 'object') continue
|
|
530
|
+
if (event.type === 'session' && typeof event.id === 'string') { preSessionId = event.id; continue }
|
|
522
531
|
if (event.type === 'session/title') continue
|
|
523
532
|
if (PACKED_ROW_TYPES.has(event.type)) continue
|
|
524
533
|
if (event.type === 'request/header') {
|
|
@@ -534,14 +543,33 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
534
543
|
else continue
|
|
535
544
|
const source = event.type === 'assistant/message' ? event.data?.message?.source : null
|
|
536
545
|
const sampleProvider = typeof source?.provider === 'string' && source.provider.length > 0 ? source.provider : preProvider
|
|
537
|
-
|
|
546
|
+
let sampleModel = typeof source?.model === 'string' && source.model.length > 0 ? source.model : preModel
|
|
538
547
|
if (isWrapperProviderId(sampleProvider)) continue
|
|
539
548
|
const at = Number(event.time)
|
|
540
549
|
if (!Number.isFinite(at) || at <= 0) continue
|
|
541
|
-
const
|
|
550
|
+
const preBuckets = {
|
|
542
551
|
input: num(u.inputTokens), output: num(u.outputTokens), cacheRead: num(u.cacheReadTokens),
|
|
543
552
|
cacheWrite: num(u.cacheWriteTokens), reasoning: num(u.reasoningTokens),
|
|
544
|
-
}
|
|
553
|
+
}
|
|
554
|
+
const sampleResponseModel = event.type === 'assistant/message' ? responseModelOfEvent(event) : ''
|
|
555
|
+
if (sampleResponseModel) {
|
|
556
|
+
const prePricing = pricingConfigAt(config, at, preSessionId, `${sampleProvider}:${sampleModel}`)
|
|
557
|
+
const preResolved = resolvePrice(sampleProvider, sampleModel, prePricing)
|
|
558
|
+
const preQuote = findRecordedQuote(config, localDayKey(at), preSessionId, `${sampleProvider}:${sampleModel}`, preBuckets, at, at)
|
|
559
|
+
const preFallback = resolveResponseModelFallback({
|
|
560
|
+
configuredModel: sampleModel,
|
|
561
|
+
responseModel: sampleResponseModel,
|
|
562
|
+
primaryResolved: preResolved,
|
|
563
|
+
hasConfiguredQuote: Boolean(preQuote),
|
|
564
|
+
resolveFallback: candidate => {
|
|
565
|
+
const nextPricing = pricingConfigAt(config, at, preSessionId, `${sampleProvider}:${candidate}`)
|
|
566
|
+
const nextQuote = findRecordedQuote(config, localDayKey(at), preSessionId, `${sampleProvider}:${candidate}`, preBuckets, at, at)
|
|
567
|
+
return { pricing: nextPricing, resolved: resolvePrice(sampleProvider, candidate, nextPricing), quote: nextQuote || null }
|
|
568
|
+
},
|
|
569
|
+
})
|
|
570
|
+
if (preFallback !== null) sampleModel = preFallback.model
|
|
571
|
+
}
|
|
572
|
+
const fp = usageFingerprint(sampleModel, preBuckets, sampleProvider)
|
|
545
573
|
const times = plainFingerprints.get(fp)
|
|
546
574
|
if (times === undefined) plainFingerprints.set(fp, [at])
|
|
547
575
|
else times.push(at)
|
|
@@ -641,10 +669,12 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
641
669
|
// 摘要样本的事件自带路由(header 状态不受影响,后续循环步仍按其 header 计费)。
|
|
642
670
|
let sampleProvider = provider
|
|
643
671
|
let sampleModel = model
|
|
672
|
+
let sampleResponseModel = ''
|
|
644
673
|
if (event.type === 'assistant/message') {
|
|
645
674
|
const source = event.data?.message?.source
|
|
646
675
|
if (typeof source?.provider === 'string' && source.provider.length > 0) sampleProvider = source.provider
|
|
647
676
|
if (typeof source?.model === 'string' && source.model.length > 0) sampleModel = source.model
|
|
677
|
+
sampleResponseModel = responseModelOfEvent(event)
|
|
648
678
|
}
|
|
649
679
|
if (isCompaction || metered) {
|
|
650
680
|
const source = event.data?.message?.source ?? {}
|
|
@@ -652,6 +682,7 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
652
682
|
else if (typeof event.data.provider === 'string' && event.data.provider.length > 0) sampleProvider = event.data.provider
|
|
653
683
|
if (typeof source.model === 'string' && source.model.length > 0) sampleModel = source.model
|
|
654
684
|
else if (typeof event.data.model === 'string' && event.data.model.length > 0) sampleModel = event.data.model
|
|
685
|
+
if (isCompaction && !metered) sampleResponseModel = responseModelOfEvent(event)
|
|
655
686
|
}
|
|
656
687
|
// modlens 视觉包装层转发对去重(issue #70/#76):包装层样本先改挂上游 id
|
|
657
688
|
// (与账本入账钩子/实时投影同口径),仅当窗口内既无相同 (model, 五桶) 的
|
|
@@ -660,18 +691,45 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
660
691
|
// id 的转售路由(如 modlens-go-ds4f)因此整单漏计(账本 sessions=0)。
|
|
661
692
|
// compaction/summary 豁免窗口去重:它是单源事件(摘要结果只写一条日志,
|
|
662
693
|
// 没有包装转发对),与实时投影同口径(既不查窗也不入窗)。
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
694
|
+
const effectiveProvider = isWrapperProviderId(sampleProvider)
|
|
695
|
+
? (wrapperUpstreamProvider(sampleProvider) ?? sampleProvider) : sampleProvider
|
|
696
|
+
// 按事件时刻计费(历史正确):峰谷时代前用 legacyBase,之后按峰谷两档。
|
|
697
|
+
// 计价/归类/归因一律用改挂后的 effectiveProvider(包装层转发对挂语义上游)。
|
|
698
|
+
const pricedAtMs = !isCompaction && !metered ? stepStarts.get(`${turn}:${step}`) ?? atMs : atMs
|
|
699
|
+
const requestFee = isNativeSearch ? event.data.requestFee : undefined
|
|
700
|
+
let pricing = pricingConfigAt(config, pricedAtMs, sessionId, `${effectiveProvider}:${sampleModel}`)
|
|
701
|
+
let resolved = requestFee ? { priced: true, entry: {}, currency: 'CNY', billingMode: 'flat' } : resolvePrice(effectiveProvider, sampleModel, pricing)
|
|
702
|
+
let quote = !metered && !isSeed && findRecordedQuote(config, localDayKey(pricedAtMs), sessionId,
|
|
703
|
+
effectiveProvider + ':' + sampleModel, buckets, pricedAtMs, atMs)
|
|
704
|
+
if (!requestFee && !metered && sampleResponseModel) {
|
|
705
|
+
const fallback = resolveResponseModelFallback({
|
|
706
|
+
configuredModel: sampleModel,
|
|
707
|
+
responseModel: sampleResponseModel,
|
|
708
|
+
primaryResolved: resolved,
|
|
709
|
+
hasConfiguredQuote: Boolean(quote),
|
|
710
|
+
resolveFallback: candidate => {
|
|
711
|
+
const nextPricing = pricingConfigAt(config, pricedAtMs, sessionId, `${effectiveProvider}:${candidate}`)
|
|
712
|
+
const nextResolved = resolvePrice(effectiveProvider, candidate, nextPricing)
|
|
713
|
+
const nextQuote = !isSeed && findRecordedQuote(config, localDayKey(pricedAtMs), sessionId,
|
|
714
|
+
effectiveProvider + ':' + candidate, buckets, pricedAtMs, atMs)
|
|
715
|
+
return { pricing: nextPricing, resolved: nextResolved, quote: nextQuote || null }
|
|
716
|
+
},
|
|
717
|
+
})
|
|
718
|
+
if (fallback !== null) {
|
|
719
|
+
sampleModel = fallback.model
|
|
720
|
+
pricing = fallback.pricing
|
|
721
|
+
resolved = fallback.resolved
|
|
722
|
+
quote = fallback.quote || false
|
|
673
723
|
}
|
|
674
724
|
}
|
|
725
|
+
if (isWrapperProviderId(sampleProvider) && !isCompaction && !metered) {
|
|
726
|
+
const fp = usageFingerprint(sampleModel, buckets, effectiveProvider)
|
|
727
|
+
const plainTimes = plainFingerprints.get(fp)
|
|
728
|
+
const plainHit = plainTimes !== undefined && plainTimes.some(t => Math.abs(atMs - t) <= USAGE_DEDUP_WINDOW_MS)
|
|
729
|
+
if (plainHit || remappedAdmitted.some(entry => entry.fp === fp && Math.abs(atMs - entry.at) <= USAGE_DEDUP_WINDOW_MS)) continue
|
|
730
|
+
remappedAdmitted.push({ fp, at: atMs })
|
|
731
|
+
if (remappedAdmitted.length > 4096) remappedAdmitted.shift()
|
|
732
|
+
}
|
|
675
733
|
const key = metered ? `${isBillion ? 'billion' : 'search'}:${event.data.requestId}` : isCompaction
|
|
676
734
|
? `compaction:${Number.isFinite(Number(event.seq)) && Number(event.seq) >= 0 ? Number(event.seq) : 't' + String(event.time ?? '')}`
|
|
677
735
|
: `${turn}:${step}`
|
|
@@ -685,12 +743,6 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
685
743
|
if (attempt) last = null
|
|
686
744
|
continue
|
|
687
745
|
}
|
|
688
|
-
// 按事件时刻计费(历史正确):峰谷时代前用 legacyBase,之后按峰谷两档。
|
|
689
|
-
// 计价/归类/归因一律用改挂后的 effectiveProvider(包装层转发对挂语义上游)。
|
|
690
|
-
const pricedAtMs = !isCompaction && !metered ? stepStarts.get(`${turn}:${step}`) ?? atMs : atMs
|
|
691
|
-
const pricing = pricingConfigAt(config, pricedAtMs, sessionId, `${effectiveProvider}:${sampleModel}`)
|
|
692
|
-
const requestFee = isNativeSearch ? event.data.requestFee : undefined
|
|
693
|
-
const resolved = requestFee ? { priced: true, entry: {}, currency: 'CNY', billingMode: 'flat' } : resolvePrice(effectiveProvider, sampleModel, pricing)
|
|
694
746
|
const peak = {
|
|
695
747
|
enabled: ['deepseek-peak', 'utc-peak'].includes(resolved.billingMode) && pricing?.peakEnabled === true,
|
|
696
748
|
effectiveAtMs: Date.parse(pricing?.peakEffectiveAt ?? ''),
|
|
@@ -701,8 +753,6 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
701
753
|
// 按展示汇率折算为美元入账——与 store.account() 完全同口径。
|
|
702
754
|
const priced = requestFee ? requestFeeCostCny(requestFee, atMs) : resolved.priced ? costOf(buckets, resolved.entry, pricedAtMs, peak) : 0
|
|
703
755
|
// 每个回放样本只带冻结金额用得到的汇率,不复制整张展示币种目录。
|
|
704
|
-
const quote = !metered && !isSeed && findRecordedQuote(config, localDayKey(pricedAtMs), sessionId,
|
|
705
|
-
effectiveProvider + ':' + sampleModel, buckets, pricedAtMs, atMs)
|
|
706
756
|
const fxRates = quote?.fx ?? fxHistoryRatesOf(pricing)
|
|
707
757
|
const canonical = quote ? quote.total / quote.fx[quote.currency] : usdFromCost(priced, resolved.currency, fxRates.CNY)
|
|
708
758
|
// 冻结币种(CNY/EUR)用记录时的汇率折算;其余展示币种没有历史汇率,按 1 处理,
|
|
@@ -716,7 +766,7 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
|
|
|
716
766
|
const providerKey = `${effectiveProvider}:${sampleModel}`
|
|
717
767
|
// 聚合目标按事件分段路由;被替换的旧样本从它当时所在的段扣回(与旧版
|
|
718
768
|
// 单流先减后加的净效果一致,仅种子段的量落到 seedDays 而非 days)。
|
|
719
|
-
if (prev !== null) shift(prev, -1, prev.seed ? seedDays : days)
|
|
769
|
+
if (prev !== null) shift(prev, -1, prev.seed ? seedDays : days, !prev.seed && prev.providerKey !== providerKey)
|
|
720
770
|
const sample = { key, date, providerKey, buckets, cost, apiCost, ...frozenAmounts(cost, apiCost, quote ? { fxRates } : pricing), seed: isSeed }
|
|
721
771
|
if (samples) {
|
|
722
772
|
if (prev) samples.delete(prev)
|
package/lib/billing-stream.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
19
|
+
import { responseModelOfReplayState } from './pricing.js'
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* 嵌套深度标记:值恒为 1(存在即嵌套);按异步上下文隔离,并发流互不串扰。
|
|
@@ -50,6 +51,7 @@ export function createLlmStreamBilling({ account, now = Date.now, bridge, activi
|
|
|
50
51
|
const proxy = bridge?.createScope(options, startedAtMs)
|
|
51
52
|
return (async function* costMeterStream() {
|
|
52
53
|
let usage = null
|
|
54
|
+
let responseModel = ''
|
|
53
55
|
const release = activity?.enter(options?.sessionId)
|
|
54
56
|
let iterator
|
|
55
57
|
let completed = false
|
|
@@ -64,6 +66,9 @@ export function createLlmStreamBilling({ account, now = Date.now, bridge, activi
|
|
|
64
66
|
if (chunk !== null && chunk !== undefined && chunk.type === 'usage' && chunk.usage != null) {
|
|
65
67
|
usage = chunk.usage
|
|
66
68
|
proxy?.observe(usage)
|
|
69
|
+
} else if (chunk !== null && chunk !== undefined && chunk.type === 'finish') {
|
|
70
|
+
const candidate = responseModelOfReplayState(chunk.replayState)
|
|
71
|
+
if (candidate) responseModel = candidate
|
|
67
72
|
}
|
|
68
73
|
yield chunk
|
|
69
74
|
}
|
|
@@ -86,7 +91,7 @@ export function createLlmStreamBilling({ account, now = Date.now, bridge, activi
|
|
|
86
91
|
const covered = proxy ? await proxy.finish(usage) : false
|
|
87
92
|
if (usage !== null && !covered) {
|
|
88
93
|
try {
|
|
89
|
-
account(usage, options?.model, options?.sessionId, startedAtMs, options?.provider)
|
|
94
|
+
account(usage, options?.model, options?.sessionId, startedAtMs, options?.provider, responseModel || undefined)
|
|
90
95
|
} catch (error) {
|
|
91
96
|
console.warn(`[dsh-cost-meter] 计费失败: ${String(error)}`)
|
|
92
97
|
}
|