dsh-token-dash 2.5.0 → 2.5.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/README.en.md +2 -2
- package/README.md +2 -2
- package/index.js +9 -0
- package/package.json +7 -3
- package/src/collect.js +39 -12
- package/src/core.js +17 -5
package/README.en.md
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
<p align="center">
|
|
12
12
|
See today's model spend, monthly budget headroom, and subscription windows for 8 providers right next to the DSH input box.<br />
|
|
13
|
-
|
|
14
|
-
export the current month and import it back after switching machines or reinstalling.
|
|
13
|
+
Pill for the current model, floating panel for budget and usage, full dashboard for every provider card; cache hit rate, usage trends and a dual-currency cost chart in one view.<br />
|
|
14
|
+
Paste a key and the billing mode is detected automatically; cost, Tokens and cache hits are estimated locally; export the current month and import it back after switching machines or reinstalling.
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
17
|
<p align="center">
|
package/README.md
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
<p align="center">
|
|
12
12
|
在 DSH 输入框旁看清今天的模型消耗、本月的预算余量,以及 8 家供应商的订阅窗口。<br />
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
Pill 看当前模型,浮层看预算与用量,整页看全量卡片;缓存命中率、用量趋势与双币种双轴成本曲线一屏读完。<br />
|
|
14
|
+
填 Key 自动识别订阅或 API 用量;费用、Tokens、缓存命中全部在本地估算;当月数据可导出备份,换机或重装后一键导入恢复。
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
17
|
<p align="center">
|
package/index.js
CHANGED
|
@@ -371,6 +371,15 @@ export function apply(ctx) {
|
|
|
371
371
|
const sessionId = session?.id ?? session?.header?.id ?? 'live'
|
|
372
372
|
const record = ingest(state, sessionId, event, num(session?.header?.seedLength), kindOf)
|
|
373
373
|
if (record !== undefined) {
|
|
374
|
+
// 归属走回退层时必须留痕:跨模型会话里这是唯一能证明「有没有串到相邻模型」的证据
|
|
375
|
+
if (record.attribution === 'fallback') {
|
|
376
|
+
logger.info('collect', 'usage.attr-fallback', {
|
|
377
|
+
sessionId: record.sessionId,
|
|
378
|
+
provider: record.provider,
|
|
379
|
+
model: record.model,
|
|
380
|
+
from: record.attributionSource,
|
|
381
|
+
})
|
|
382
|
+
}
|
|
374
383
|
// 新记录改变用量口径,缓存的 usage 结果需要重算
|
|
375
384
|
usageCache.clear()
|
|
376
385
|
flushReady?.schedule?.()
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-token-dash",
|
|
3
|
-
"version": "2.5.
|
|
4
|
-
"description": "DeepSeek Harness Token
|
|
3
|
+
"version": "2.5.1",
|
|
4
|
+
"description": "DeepSeek Harness Token 与费用看板:8 家供应商开箱即用,按供应商查看预算、订阅配额(GLM / Kimi / OpenCode Go / Command Code)与账户余额;Pill、浮层、整页三处呈现费用与真实 Tokens、缓存命中率与用量趋势,成本趋势双币种双轴;填第三方 API Key 的自建 / 中转 / 本地兼容服务同样能记账,未定价模型不按 0 元计,可导出 / 导入本地 JSON 备份。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"exports": {
|
|
@@ -27,9 +27,13 @@
|
|
|
27
27
|
"deepseek",
|
|
28
28
|
"deepseek-harness",
|
|
29
29
|
"dsh",
|
|
30
|
+
"dsh-plugin",
|
|
30
31
|
"token",
|
|
31
32
|
"usage",
|
|
32
|
-
"
|
|
33
|
+
"cost",
|
|
34
|
+
"budget",
|
|
35
|
+
"quota",
|
|
36
|
+
"dashboard"
|
|
33
37
|
],
|
|
34
38
|
"license": "MIT",
|
|
35
39
|
"repository": {
|
package/src/collect.js
CHANGED
|
@@ -20,14 +20,28 @@ export function recordFromEvent(event) {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* #43:message.source 缺失时的归属回退层(形状参考 dsh-ui-usage-billing aggregate)。
|
|
25
|
+
* 两级信号:`request/header` 是「这次请求实际用的模型」,`model/selection` 是用户的模型选择;
|
|
26
|
+
* 后者只在前者尚未出现时兜底,避免宿主漏发请求头时整段用量落空。
|
|
27
|
+
* 返回值带 `source`,供 `ingest` 标记回退来源并落日志。
|
|
28
|
+
*/
|
|
24
29
|
export function headerFromEvent(event) {
|
|
25
|
-
if (event?.type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
if (event?.type === 'request/header') {
|
|
31
|
+
const config = event.data?.header?.config ?? event.data?.config
|
|
32
|
+
const provider = typeof config?.provider === 'string' && config.provider !== '' ? config.provider : undefined
|
|
33
|
+
const model = typeof config?.model === 'string' && config.model !== '' ? config.model : undefined
|
|
34
|
+
if (provider === undefined && model === undefined) return undefined
|
|
35
|
+
return { provider, model, source: 'request-header' }
|
|
36
|
+
}
|
|
37
|
+
if (event?.type === 'model/selection') {
|
|
38
|
+
const data = event.data ?? {}
|
|
39
|
+
const provider = typeof data.provider === 'string' && data.provider !== '' ? data.provider : undefined
|
|
40
|
+
const model = typeof data.model === 'string' && data.model !== '' ? data.model : undefined
|
|
41
|
+
if (provider === undefined && model === undefined) return undefined
|
|
42
|
+
return { provider, model, source: 'model-selection' }
|
|
43
|
+
}
|
|
44
|
+
return undefined
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
/** 入账一条事件;返回记录或 undefined。`kindOf` 缺省时 `addRecord` 走 registry 兜底。 */
|
|
@@ -47,12 +61,20 @@ export function ingest(state, sessionId, event, seedFloor = 0, kindOf) {
|
|
|
47
61
|
return undefined
|
|
48
62
|
}
|
|
49
63
|
record.sessionId = sessionId
|
|
50
|
-
// #43:usage 事件无 message.source
|
|
64
|
+
// #43:usage 事件无 message.source 时,回退最近一次归属信号(请求头优先,其次模型选择)。
|
|
65
|
+
// 回退命中的记录带 attribution='fallback',调用方据此落 usage.attr-fallback 日志,
|
|
66
|
+
// 让「跨模型会话是否串行归因」可以被日志直接回答,而不是靠目录复算反推。
|
|
51
67
|
if (record.provider === 'unknown' || record.model === '') {
|
|
52
68
|
const fallback = state.headers[sessionId]
|
|
53
69
|
if (fallback !== undefined) {
|
|
54
|
-
|
|
55
|
-
|
|
70
|
+
const providerMissing = record.provider === 'unknown' && fallback.provider !== undefined
|
|
71
|
+
const modelMissing = record.model === '' && fallback.model !== undefined
|
|
72
|
+
if (providerMissing) record.provider = fallback.provider
|
|
73
|
+
if (modelMissing) record.model = fallback.model
|
|
74
|
+
if (providerMissing || modelMissing) {
|
|
75
|
+
record.attribution = 'fallback'
|
|
76
|
+
record.attributionSource = fallback.source ?? 'request-header'
|
|
77
|
+
}
|
|
56
78
|
}
|
|
57
79
|
}
|
|
58
80
|
addRecord(state, record, kindOf)
|
|
@@ -67,6 +89,7 @@ export async function backfill(ctx, state, kindOf) {
|
|
|
67
89
|
}
|
|
68
90
|
let sessions = 0
|
|
69
91
|
let records = 0
|
|
92
|
+
let fallbacks = 0
|
|
70
93
|
const providerByModel = new Map()
|
|
71
94
|
const list = await persistence.list()
|
|
72
95
|
for (const header of Array.isArray(list) ? list : []) {
|
|
@@ -86,7 +109,11 @@ export async function backfill(ctx, state, kindOf) {
|
|
|
86
109
|
providers.add(source.provider)
|
|
87
110
|
providerByModel.set(model, providers)
|
|
88
111
|
}
|
|
89
|
-
|
|
112
|
+
const record = ingest(state, id, event, seed, kindOf)
|
|
113
|
+
if (record !== undefined) {
|
|
114
|
+
records += 1
|
|
115
|
+
if (record.attribution === 'fallback') fallbacks += 1
|
|
116
|
+
}
|
|
90
117
|
}
|
|
91
118
|
} catch {
|
|
92
119
|
// 单个会话失败不影响其余会话
|
|
@@ -99,5 +126,5 @@ export async function backfill(ctx, state, kindOf) {
|
|
|
99
126
|
}
|
|
100
127
|
}
|
|
101
128
|
reattributeUnknown(state, providerByModel)
|
|
102
|
-
return { sessions, records, degraded: null }
|
|
129
|
+
return { sessions, records, fallbacks, degraded: null }
|
|
103
130
|
}
|
package/src/core.js
CHANGED
|
@@ -684,7 +684,7 @@ export function buildSummary(state, prefs, now, providerInfoInput, activeProvide
|
|
|
684
684
|
|
|
685
685
|
// #41/#42:unknown 行按模型名前缀视图层归因(不改 daily.json);models 与 provider.usage 均为当月口径(#42 回退跨月累计)
|
|
686
686
|
const unpriced = []
|
|
687
|
-
const accumulate = (providerId, modelId, row, isToday) => {
|
|
687
|
+
const accumulate = (providerId, modelId, row, isToday, dayTs) => {
|
|
688
688
|
const meta = providerInfo.find((item) => normalizeProvider(item.id ?? item.provider) === providerId)
|
|
689
689
|
const kind = resolveProviderKind(state, prefs, providerId, meta)
|
|
690
690
|
const provider = providerMap.get(providerId) ?? providerEmpty(providerId, kind)
|
|
@@ -739,8 +739,18 @@ export function buildSummary(state, prefs, now, providerInfoInput, activeProvide
|
|
|
739
739
|
const tokens = num(row.input) + num(row.cacheRead) + num(row.cacheWrite) + num(row.output)
|
|
740
740
|
provider.usage.monthTokens += tokens
|
|
741
741
|
provider.usage.monthCost += billable ? num(row.cost) : 0
|
|
742
|
-
// 订阅行的目录价不算支出,但按「本月估算」单独累计,供订阅卡展示(D-02
|
|
743
|
-
|
|
742
|
+
// 订阅行的目录价不算支出,但按「本月估算」单独累计,供订阅卡展示(D-02)。
|
|
743
|
+
// Issue #3:订阅行存量 cost 恒 0(REQ-12),不能再读 row.cost;
|
|
744
|
+
// 改为与 buildUsage · add 完全同口径——按当日 ts 现算估算,缺价行跳过不计。
|
|
745
|
+
if (!billable) {
|
|
746
|
+
const estimatePrice = resolvePrice(modelId, providerId, dayTs)
|
|
747
|
+
if (estimatePrice !== null) {
|
|
748
|
+
provider.usage.monthEstimate += costOf({
|
|
749
|
+
ts: dayTs, provider: providerId, model: modelId,
|
|
750
|
+
input: row.input, cacheRead: row.cacheRead, cacheWrite: row.cacheWrite, output: row.output,
|
|
751
|
+
})
|
|
752
|
+
}
|
|
753
|
+
}
|
|
744
754
|
provider.usage.requests += num(row.requests)
|
|
745
755
|
if (isToday) {
|
|
746
756
|
provider.usage.todayTokens += tokens
|
|
@@ -759,13 +769,13 @@ export function buildSummary(state, prefs, now, providerInfoInput, activeProvide
|
|
|
759
769
|
for (const [rawModel, row] of Object.entries(providerBucket.models ?? {})) {
|
|
760
770
|
const owner = providerFromModel(rawModel)
|
|
761
771
|
if (owner === null || hidden.has(owner)) continue
|
|
762
|
-
accumulate(owner, normalizeModel(rawModel), row, isToday)
|
|
772
|
+
accumulate(owner, normalizeModel(rawModel), row, isToday, Date.parse(dayKey))
|
|
763
773
|
}
|
|
764
774
|
continue
|
|
765
775
|
}
|
|
766
776
|
if (hidden.has(providerId)) continue
|
|
767
777
|
for (const [rawModel, row] of Object.entries(providerBucket.models ?? {})) {
|
|
768
|
-
accumulate(providerId, normalizeModel(rawModel), row, isToday)
|
|
778
|
+
accumulate(providerId, normalizeModel(rawModel), row, isToday, Date.parse(dayKey))
|
|
769
779
|
}
|
|
770
780
|
}
|
|
771
781
|
}
|
|
@@ -833,6 +843,8 @@ export function buildSummary(state, prefs, now, providerInfoInput, activeProvide
|
|
|
833
843
|
}
|
|
834
844
|
provider.usage.todayCost = round2(provider.usage.todayCost)
|
|
835
845
|
provider.usage.monthCost = round2(provider.usage.monthCost)
|
|
846
|
+
// 估算值与 monthCost 同步收敛到分位,保证与 /usage 明细口径逐位可对账(Issue #3)
|
|
847
|
+
provider.usage.monthEstimate = round2(provider.usage.monthEstimate ?? 0)
|
|
836
848
|
return { ...provider, budget }
|
|
837
849
|
})
|
|
838
850
|
// 选卡口径三级回退:前端显式选择 → 最近观测 → 记账口径 → 首卡。
|