dsh-token-dash 2.0.2 → 2.2.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.en.md +20 -12
- package/README.md +21 -13
- package/client.js +278 -27
- package/index.js +125 -16
- package/locale/en.json +27 -1
- package/locale/zh.json +27 -1
- package/package.json +1 -1
- package/src/adapters.js +88 -15
- package/src/catalog.js +146 -0
- package/src/collect.js +5 -5
- package/src/core.js +291 -41
- package/src/match.js +119 -0
- package/src/pricing.js +146 -204
- package/src/providers.js +13 -1
- package/src/quota-sources.js +101 -0
- package/src/store.js +55 -2
package/src/collect.js
CHANGED
|
@@ -30,8 +30,8 @@ export function headerFromEvent(event) {
|
|
|
30
30
|
return { provider, model }
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/** 入账一条事件;返回记录或 undefined
|
|
34
|
-
export function ingest(state, sessionId, event, seedFloor = 0) {
|
|
33
|
+
/** 入账一条事件;返回记录或 undefined。`kindOf` 缺省时 `addRecord` 走 registry 兜底。 */
|
|
34
|
+
export function ingest(state, sessionId, event, seedFloor = 0, kindOf) {
|
|
35
35
|
const seq = num(event?.seq)
|
|
36
36
|
const mark = num(state.watermarks[sessionId])
|
|
37
37
|
if (seq > 0 && seq <= mark) return undefined
|
|
@@ -55,12 +55,12 @@ export function ingest(state, sessionId, event, seedFloor = 0) {
|
|
|
55
55
|
if (record.model === '' && fallback.model !== undefined) record.model = fallback.model
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
addRecord(state, record)
|
|
58
|
+
addRecord(state, record, kindOf)
|
|
59
59
|
if (seq > 0) state.watermarks[sessionId] = seq
|
|
60
60
|
return record
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
export async function backfill(ctx, state) {
|
|
63
|
+
export async function backfill(ctx, state, kindOf) {
|
|
64
64
|
const persistence = ctx.get?.('sessionPersistence')
|
|
65
65
|
if (persistence === undefined || typeof persistence.list !== 'function') {
|
|
66
66
|
return { sessions: 0, records: 0, degraded: 'no-session-persistence' }
|
|
@@ -86,7 +86,7 @@ export async function backfill(ctx, state) {
|
|
|
86
86
|
providers.add(source.provider)
|
|
87
87
|
providerByModel.set(model, providers)
|
|
88
88
|
}
|
|
89
|
-
if (ingest(state, id, event, seed) !== undefined) records += 1
|
|
89
|
+
if (ingest(state, id, event, seed, kindOf) !== undefined) records += 1
|
|
90
90
|
}
|
|
91
91
|
} catch {
|
|
92
92
|
// 单个会话失败不影响其余会话
|
package/src/core.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Pure aggregation: day/month keys, provider/model buckets, budgets, currencies.
|
|
3
3
|
* No file or clock reads; `now` is injected.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { explainPriceMiss, resolvePrice } from './pricing.js'
|
|
6
6
|
import {
|
|
7
7
|
canBeSubscription,
|
|
8
8
|
defaultModelFor,
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
pricingLinks,
|
|
13
13
|
providerCurrency,
|
|
14
14
|
providerFromModel,
|
|
15
|
+
providerKind,
|
|
15
16
|
providerLabel,
|
|
16
17
|
} from './providers.js'
|
|
17
18
|
|
|
@@ -40,16 +41,16 @@ export function isPeak(ts) {
|
|
|
40
41
|
return (hour >= 9 && hour < 12) || (hour >= 14 && hour < 18)
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
export { normalizeProvider, providerLabel, normalizeModel,
|
|
44
|
+
export { normalizeProvider, providerLabel, normalizeModel, resolvePrice }
|
|
44
45
|
export const displayModel = modelLabel
|
|
45
46
|
|
|
46
|
-
export function priceForRecord(
|
|
47
|
-
return
|
|
47
|
+
export function priceForRecord(model, provider, ts) {
|
|
48
|
+
return resolvePrice(model, provider, ts)
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
/** Undefined price returns 0; the caller marks `priced=false`. */
|
|
51
52
|
export function costOf(record) {
|
|
52
|
-
const price =
|
|
53
|
+
const price = resolvePrice(record.model, record.provider ?? 'unknown', record.ts)
|
|
53
54
|
if (price === null) return 0
|
|
54
55
|
return (
|
|
55
56
|
(num(record.input) * price.input +
|
|
@@ -63,7 +64,7 @@ export function costOf(record) {
|
|
|
63
64
|
export function emptyState() {
|
|
64
65
|
return {
|
|
65
66
|
version: 1,
|
|
66
|
-
costVersion:
|
|
67
|
+
costVersion: 4,
|
|
67
68
|
days: {},
|
|
68
69
|
watermarks: {},
|
|
69
70
|
headers: {},
|
|
@@ -78,12 +79,32 @@ function dayStartTs(dayKey) {
|
|
|
78
79
|
return new Date(year, month - 1, day).getTime()
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
/**
|
|
83
|
+
* 枚举 [startKey, endKey] 内的全部本地日键(含两端)。
|
|
84
|
+
* 用 setDate 逐日推进而不是加 86400000,跨夏令时切换时日期才不会漂。
|
|
85
|
+
*/
|
|
86
|
+
export function eachDayKey(startKey, endKey) {
|
|
87
|
+
const start = dayStartTs(startKey)
|
|
88
|
+
if (start === null || String(endKey) < String(startKey)) return []
|
|
89
|
+
const out = []
|
|
90
|
+
const cursor = new Date(start)
|
|
91
|
+
// 账本不可能超过 100 年;非法 key 时也靠这个上限兜底退出,避免死循环
|
|
92
|
+
for (let guard = 0; guard < 36600; guard += 1) {
|
|
93
|
+
const key = localDayKey(cursor.getTime())
|
|
94
|
+
if (key > endKey) break
|
|
95
|
+
out.push(key)
|
|
96
|
+
cursor.setDate(cursor.getDate() + 1)
|
|
97
|
+
}
|
|
98
|
+
return out
|
|
99
|
+
}
|
|
100
|
+
|
|
81
101
|
/**
|
|
82
102
|
* 旧计价模型的成本在入账时写死;定价修复后按日级 token 与当日 00:00 价目重算。
|
|
83
103
|
* 日级聚合没有单条记录时间,峰谷只能按 00:00(闲时)代表,结果属于估算。
|
|
104
|
+
* 重算同样走「形态先于计价 + 币种不匹配不计价」,订阅行与零消耗行保持原状态。
|
|
84
105
|
*/
|
|
85
|
-
export function repriceState(state) {
|
|
86
|
-
if (state.costVersion ===
|
|
106
|
+
export function repriceState(state, kindOf = (id) => providerKind(id)) {
|
|
107
|
+
if (state.costVersion === 4) return { repriced: false, rows: 0 }
|
|
87
108
|
let rows = 0
|
|
88
109
|
for (const [dayKey, bucket] of Object.entries(state.days ?? {})) {
|
|
89
110
|
const ts = dayStartTs(dayKey)
|
|
@@ -92,17 +113,29 @@ export function repriceState(state) {
|
|
|
92
113
|
for (const [model, row] of Object.entries(providerBucket?.models ?? {})) {
|
|
93
114
|
// unknown 行只影响计价归属;展示层仍按模型前缀归因
|
|
94
115
|
const owner = providerId === 'unknown' ? providerFromModel(model) : providerId
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
row.
|
|
116
|
+
// 形态先于计价:订阅 provider 的行保持原值,重算不得写成金额
|
|
117
|
+
if (owner !== null && kindOf(owner) !== 'api') continue
|
|
118
|
+
const empty = num(row.input) + num(row.cacheRead) + num(row.cacheWrite) + num(row.output) === 0
|
|
119
|
+
const price = owner === null ? null : resolvePrice(model, owner, ts)
|
|
120
|
+
const missing = price === null || price.mismatch === true || price.input === null || price.output === null
|
|
121
|
+
row.cost = missing || empty
|
|
98
122
|
? 0
|
|
99
123
|
: costOf({ ts, provider: owner ?? providerId, model, input: row.input, cacheRead: row.cacheRead, cacheWrite: row.cacheWrite, output: row.output })
|
|
100
|
-
|
|
124
|
+
if (empty) {
|
|
125
|
+
row.priced = 'empty'
|
|
126
|
+
delete row.reason
|
|
127
|
+
} else if (missing) {
|
|
128
|
+
row.priced = false
|
|
129
|
+
row.reason = price === null ? explainPriceMiss(model) : 'currency-mismatch'
|
|
130
|
+
} else {
|
|
131
|
+
row.priced = true
|
|
132
|
+
delete row.reason
|
|
133
|
+
}
|
|
101
134
|
rows += 1
|
|
102
135
|
}
|
|
103
136
|
}
|
|
104
137
|
}
|
|
105
|
-
state.costVersion =
|
|
138
|
+
state.costVersion = 4
|
|
106
139
|
return { repriced: true, rows }
|
|
107
140
|
}
|
|
108
141
|
|
|
@@ -140,7 +173,7 @@ export function reattributeUnknown(state, providerByModel) {
|
|
|
140
173
|
}
|
|
141
174
|
}
|
|
142
175
|
|
|
143
|
-
export function addRecord(state, record) {
|
|
176
|
+
export function addRecord(state, record, kindOf = (id) => providerKind(id)) {
|
|
144
177
|
const day = localDayKey(record.ts)
|
|
145
178
|
const bucket = dayBucket(state, day)
|
|
146
179
|
if (bucket.providers === undefined) {
|
|
@@ -164,13 +197,82 @@ export function addRecord(state, record) {
|
|
|
164
197
|
row.cacheWrite += num(record.cacheWrite)
|
|
165
198
|
row.output += num(record.output)
|
|
166
199
|
row.requests += 1
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
200
|
+
// 形态先于计价:订阅套餐按套餐结算,套模型价会把 USD 订阅算成 RMB 用量费(REQ-12)。
|
|
201
|
+
if (kindOf(provider) === 'subscription') {
|
|
202
|
+
row.priced = 'subscription'
|
|
203
|
+
delete row.reason
|
|
204
|
+
} else {
|
|
205
|
+
const price = resolvePrice(model, provider, record.ts)
|
|
206
|
+
// 币种不匹配按未定价处理:宁可空着,也不把 CNY 报价写进 USD 桶(REQ-03)
|
|
207
|
+
if (price === null || price.mismatch === true) {
|
|
208
|
+
row.priced = false
|
|
209
|
+
row.reason = price === null ? explainPriceMiss(model) : 'currency-mismatch'
|
|
210
|
+
} else {
|
|
211
|
+
row.priced = true
|
|
212
|
+
delete row.reason
|
|
213
|
+
row.cost += (
|
|
214
|
+
num(record.input) * price.input +
|
|
215
|
+
num(record.cacheRead) * price.cacheRead +
|
|
216
|
+
num(record.cacheWrite) * price.cacheWrite +
|
|
217
|
+
num(record.output) * price.output
|
|
218
|
+
) / 1e6
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// 零消耗行与"真缺价"必须可区分:最后写,覆盖上面两种结果(REQ-10)。
|
|
222
|
+
if (row.input + row.cacheRead + row.cacheWrite + row.output === 0) row.priced = 'empty'
|
|
171
223
|
state.currentProvider = provider
|
|
172
224
|
}
|
|
173
225
|
|
|
226
|
+
/**
|
|
227
|
+
* 只补 `priced === false` 的行;`true` / `'subscription'` / `'empty'` 一律不动。
|
|
228
|
+
* `unknown` 桶按模型前缀归因,避免"展示有归属、补算却跳过"。
|
|
229
|
+
*/
|
|
230
|
+
export function repriceUnpriced(state, scope, kindOf = (id) => providerKind(id)) {
|
|
231
|
+
const month = typeof scope?.month === 'string' && /^\d{4}-\d{2}$/.test(scope.month) ? scope.month : null
|
|
232
|
+
const result = { scope: scope?.month ?? 'all', rows: 0, skipped: { 'no-owner': 0, kind: 0, 'no-price': 0, currency: 0 } }
|
|
233
|
+
for (const [dayKey, bucket] of Object.entries(state.days ?? {})) {
|
|
234
|
+
if (month !== null && !dayKey.startsWith(`${month}-`)) continue
|
|
235
|
+
const ts = dayStartTs(dayKey)
|
|
236
|
+
if (ts === null) continue
|
|
237
|
+
for (const [providerId, providerBucket] of Object.entries(bucket?.providers ?? {})) {
|
|
238
|
+
for (const [rawModel, row] of Object.entries(providerBucket?.models ?? {})) {
|
|
239
|
+
if (row?.priced !== false) continue
|
|
240
|
+
const model = normalizeModel(rawModel)
|
|
241
|
+
const owner = providerId === 'unknown' ? providerFromModel(model) : providerId
|
|
242
|
+
if (owner === null) {
|
|
243
|
+
result.skipped['no-owner'] += 1
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
if (kindOf(owner) !== 'api') {
|
|
247
|
+
result.skipped.kind += 1
|
|
248
|
+
continue
|
|
249
|
+
}
|
|
250
|
+
const price = resolvePrice(model, owner, ts)
|
|
251
|
+
if (price === null) {
|
|
252
|
+
result.skipped['no-price'] += 1
|
|
253
|
+
continue
|
|
254
|
+
}
|
|
255
|
+
// 币种不符的行保持未定价,只把原因改写清楚,等用户补 USD 单价后再补算
|
|
256
|
+
if (price.mismatch === true) {
|
|
257
|
+
result.skipped.currency += 1
|
|
258
|
+
row.reason = 'currency-mismatch'
|
|
259
|
+
continue
|
|
260
|
+
}
|
|
261
|
+
row.cost = (
|
|
262
|
+
num(row.input) * price.input +
|
|
263
|
+
num(row.cacheRead) * price.cacheRead +
|
|
264
|
+
num(row.cacheWrite) * price.cacheWrite +
|
|
265
|
+
num(row.output) * price.output
|
|
266
|
+
) / 1e6
|
|
267
|
+
row.priced = true
|
|
268
|
+
delete row.reason
|
|
269
|
+
result.rows += 1
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return result
|
|
274
|
+
}
|
|
275
|
+
|
|
174
276
|
/** 未填写时的默认预算,按 provider 币种;只在 VM 内生效,不写盘。 */
|
|
175
277
|
export const DEFAULT_BUDGET = 100
|
|
176
278
|
|
|
@@ -193,11 +295,11 @@ function budgetFor(prefs, providerId, currency) {
|
|
|
193
295
|
}
|
|
194
296
|
}
|
|
195
297
|
|
|
196
|
-
function providerEmpty(providerId) {
|
|
298
|
+
function providerEmpty(providerId, kind = 'api') {
|
|
197
299
|
return {
|
|
198
300
|
id: providerId,
|
|
199
301
|
name: providerLabel(providerId),
|
|
200
|
-
kind
|
|
302
|
+
kind,
|
|
201
303
|
canSubscribe: canBeSubscription(providerId),
|
|
202
304
|
currency: providerCurrency(providerId),
|
|
203
305
|
usage: { todayTokens: 0, monthTokens: 0, todayCost: 0, monthCost: 0, requests: 0 },
|
|
@@ -218,14 +320,27 @@ function emptyTotals() {
|
|
|
218
320
|
return { cost: 0, requests: 0, input: 0, cacheRead: 0, cacheWrite: 0, output: 0, hitRate: null }
|
|
219
321
|
}
|
|
220
322
|
|
|
221
|
-
/** 行内与 0
|
|
222
|
-
function usageCurrency(providerId, meta) {
|
|
323
|
+
/** 行内与 0 桶共用:声明 > 快照 > registry,不写死 CNY。 */
|
|
324
|
+
function usageCurrency(providerId, meta, prefs) {
|
|
325
|
+
const declared = prefs?.providerCurrency?.[providerId]
|
|
326
|
+
if (declared === 'USD' || declared === 'CNY') return declared
|
|
223
327
|
if (meta?.currency === 'USD') return 'USD'
|
|
224
328
|
if (meta?.currency === 'CNY') return 'CNY'
|
|
225
329
|
return providerCurrency(providerId)
|
|
226
330
|
}
|
|
227
331
|
|
|
228
|
-
/**
|
|
332
|
+
/** 币种来源只影响展示文案,不参与计算(BND-10)。 */
|
|
333
|
+
function currencySourceOf(prefs, providerId, meta) {
|
|
334
|
+
const declared = prefs?.providerCurrency?.[providerId]
|
|
335
|
+
if (declared === 'USD' || declared === 'CNY') return 'prefs'
|
|
336
|
+
if (meta?.currency === 'USD' || meta?.currency === 'CNY') return 'snapshot'
|
|
337
|
+
return 'community'
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* prefs 显式覆盖 > 快照确定的 mode > 历史固化值 > registry 静态声明 > api。
|
|
342
|
+
* `meta.kind` 是 adapter 规整后的回声值(非订阅一律 `api`),不能当作"探测到了 api"的证据。
|
|
343
|
+
*/
|
|
229
344
|
export function resolveProviderKind(state, prefs, providerId, meta) {
|
|
230
345
|
const id = normalizeProvider(providerId)
|
|
231
346
|
const explicit = prefs?.billingModes?.[id]
|
|
@@ -233,7 +348,7 @@ export function resolveProviderKind(state, prefs, providerId, meta) {
|
|
|
233
348
|
if (BILLING_MODES.has(meta?.mode)) return meta.mode
|
|
234
349
|
const stamped = state?.providerKinds?.[id]
|
|
235
350
|
if (BILLING_MODES.has(stamped)) return stamped
|
|
236
|
-
return
|
|
351
|
+
return providerKind(id)
|
|
237
352
|
}
|
|
238
353
|
|
|
239
354
|
/** 只固化"探测确定"的形态;unknown 不覆盖历史,返回是否有变化。 */
|
|
@@ -250,6 +365,54 @@ export function stampProviderKinds(state, snapshots) {
|
|
|
250
365
|
return changed
|
|
251
366
|
}
|
|
252
367
|
|
|
368
|
+
/** 日点数超过这个值就切「按月」桶:柱数稳定在 12 上下,避免柱子被压成线。 */
|
|
369
|
+
const MONTHLY_GRAIN_THRESHOLD = 60
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 日序列超过阈值时按月合并:柱数稳定在 12 上下,避免柱子被压成线。
|
|
373
|
+
* 只做加法,口径与日序列完全一致;命中率按当月总量重算,不是日命中率的平均。
|
|
374
|
+
*/
|
|
375
|
+
export function buildMonthlySeries(series) {
|
|
376
|
+
if (!Array.isArray(series) || series.length <= MONTHLY_GRAIN_THRESHOLD) return { grain: 'day', series }
|
|
377
|
+
const byMonth = new Map()
|
|
378
|
+
for (const point of series) {
|
|
379
|
+
const month = String(point.dayKey).slice(0, 7)
|
|
380
|
+
let entry = byMonth.get(month)
|
|
381
|
+
if (entry === undefined) {
|
|
382
|
+
entry = {
|
|
383
|
+
dayKey: month,
|
|
384
|
+
cost: {},
|
|
385
|
+
requests: 0,
|
|
386
|
+
input: 0,
|
|
387
|
+
cacheRead: 0,
|
|
388
|
+
cacheWrite: 0,
|
|
389
|
+
output: 0,
|
|
390
|
+
tokens: 0,
|
|
391
|
+
hitRate: null,
|
|
392
|
+
}
|
|
393
|
+
byMonth.set(month, entry)
|
|
394
|
+
}
|
|
395
|
+
entry.requests += point.requests
|
|
396
|
+
entry.input += point.input
|
|
397
|
+
entry.cacheRead += point.cacheRead
|
|
398
|
+
entry.cacheWrite += point.cacheWrite
|
|
399
|
+
entry.output += point.output
|
|
400
|
+
entry.tokens += point.tokens
|
|
401
|
+
for (const [currency, bucket] of Object.entries(point.cost)) {
|
|
402
|
+
const target = (entry.cost[currency] ??= { cost: 0, requests: 0 })
|
|
403
|
+
target.cost += bucket.cost
|
|
404
|
+
target.requests += bucket.requests
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const merged = [...byMonth.values()]
|
|
408
|
+
for (const entry of merged) {
|
|
409
|
+
for (const bucket of Object.values(entry.cost)) bucket.cost = round2(bucket.cost)
|
|
410
|
+
const denominator = entry.input + entry.cacheRead + entry.cacheWrite
|
|
411
|
+
entry.hitRate = denominator > 0 ? entry.cacheRead / denominator : null
|
|
412
|
+
}
|
|
413
|
+
return { grain: 'month', series: merged }
|
|
414
|
+
}
|
|
415
|
+
|
|
253
416
|
/**
|
|
254
417
|
* Range-only aggregation for P2. Unlike buildSummary this never adds
|
|
255
418
|
* "configured but unused" default rows, and it does not touch month budgets.
|
|
@@ -275,12 +438,31 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
275
438
|
}
|
|
276
439
|
const models = new Map()
|
|
277
440
|
const totals = {}
|
|
278
|
-
|
|
441
|
+
// 逐日点:口径与 P3 两格卡一致——cost 只累 billable 行,其余字段累全部行(含订阅)
|
|
442
|
+
const points = new Map()
|
|
443
|
+
const pointOf = (dayKey) => {
|
|
444
|
+
const existing = points.get(dayKey)
|
|
445
|
+
if (existing !== undefined) return existing
|
|
446
|
+
const point = {
|
|
447
|
+
dayKey,
|
|
448
|
+
cost: {},
|
|
449
|
+
requests: 0,
|
|
450
|
+
input: 0,
|
|
451
|
+
cacheRead: 0,
|
|
452
|
+
cacheWrite: 0,
|
|
453
|
+
output: 0,
|
|
454
|
+
tokens: 0,
|
|
455
|
+
hitRate: null,
|
|
456
|
+
}
|
|
457
|
+
points.set(dayKey, point)
|
|
458
|
+
return point
|
|
459
|
+
}
|
|
460
|
+
const add = (providerId, modelId, row, point) => {
|
|
279
461
|
if (hidden.has(providerId)) return
|
|
280
462
|
const meta = info.get(providerId)
|
|
281
463
|
const kind = resolveProviderKind(state, prefs, providerId, meta)
|
|
282
464
|
const billable = kind === 'api'
|
|
283
|
-
const currency = usageCurrency(providerId, meta)
|
|
465
|
+
const currency = usageCurrency(providerId, meta, prefs)
|
|
284
466
|
const key = `${providerId}:${modelId}`
|
|
285
467
|
const entry = models.get(key) ?? {
|
|
286
468
|
providerId,
|
|
@@ -307,13 +489,29 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
307
489
|
// 订阅模型按目录价算出的 cost 不是真实支出,VM 内归零,保证费用框与 Tokens 框同一集合
|
|
308
490
|
entry.cost += billable ? num(row.cost) : 0
|
|
309
491
|
if (row.priced === false) entry.priced = false
|
|
492
|
+
if (row.priced === 'empty') entry.emptyRows = (entry.emptyRows ?? 0) + 1
|
|
493
|
+
entry.rows = (entry.rows ?? 0) + 1
|
|
310
494
|
entry.kind = kind
|
|
311
495
|
entry.currency = currency
|
|
496
|
+
entry.currencySource = currencySourceOf(prefs, providerId, meta)
|
|
312
497
|
models.set(key, entry)
|
|
498
|
+
const rowTokens = num(row.input) + num(row.cacheRead) + num(row.cacheWrite) + num(row.output)
|
|
499
|
+
point.requests += num(row.requests)
|
|
500
|
+
point.input += num(row.input)
|
|
501
|
+
point.cacheRead += num(row.cacheRead)
|
|
502
|
+
point.cacheWrite += num(row.cacheWrite)
|
|
503
|
+
point.output += num(row.output)
|
|
504
|
+
point.tokens += rowTokens
|
|
505
|
+
if (billable) {
|
|
506
|
+
const bucket = (point.cost[currency] ??= { cost: 0, requests: 0 })
|
|
507
|
+
bucket.cost += num(row.cost)
|
|
508
|
+
bucket.requests += num(row.requests)
|
|
509
|
+
}
|
|
313
510
|
}
|
|
314
511
|
|
|
315
512
|
for (const dayKey of dayKeys) {
|
|
316
513
|
if (dayKey < startKey || dayKey > todayKey) continue
|
|
514
|
+
const point = pointOf(dayKey)
|
|
317
515
|
const bucket = state.days[dayKey]
|
|
318
516
|
const providers = bucket.providers ?? { unknown: { models: bucket.models ?? {} } }
|
|
319
517
|
for (const [rawProvider, providerBucket] of Object.entries(providers)) {
|
|
@@ -321,12 +519,12 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
321
519
|
if (providerId === 'unknown') {
|
|
322
520
|
for (const [rawModel, row] of Object.entries(providerBucket.models ?? {})) {
|
|
323
521
|
const owner = providerFromModel(rawModel)
|
|
324
|
-
if (owner !== null) add(owner, normalizeModel(rawModel), row)
|
|
522
|
+
if (owner !== null) add(owner, normalizeModel(rawModel), row, point)
|
|
325
523
|
}
|
|
326
524
|
continue
|
|
327
525
|
}
|
|
328
526
|
for (const [rawModel, row] of Object.entries(providerBucket.models ?? {})) {
|
|
329
|
-
add(providerId, normalizeModel(rawModel), row)
|
|
527
|
+
add(providerId, normalizeModel(rawModel), row, point)
|
|
330
528
|
}
|
|
331
529
|
}
|
|
332
530
|
}
|
|
@@ -346,7 +544,12 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
346
544
|
},
|
|
347
545
|
hitRate: denominator > 0 ? entry.cacheRead / denominator : null,
|
|
348
546
|
cost: round2(entry.cost),
|
|
349
|
-
approximate: entry.billable &&
|
|
547
|
+
approximate: entry.billable && resolvePrice(entry.modelId, entry.providerId, now)?.approximate === true,
|
|
548
|
+
priced: entry.priced !== false
|
|
549
|
+
? (entry.rows > 0 && (entry.emptyRows ?? 0) < entry.rows ? true : 'empty')
|
|
550
|
+
: false,
|
|
551
|
+
rows: undefined,
|
|
552
|
+
emptyRows: undefined,
|
|
350
553
|
}
|
|
351
554
|
})
|
|
352
555
|
rows.sort((a, b) => b.cost - a.cost)
|
|
@@ -387,6 +590,16 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
387
590
|
? total.cacheRead / (total.input + total.cacheRead + total.cacheWrite)
|
|
388
591
|
: null
|
|
389
592
|
|
|
593
|
+
// 等距连续:窗口内没有数据的日补零,柱距才不会塌;hitRate 留 null 表示「无数据」而不是 0%
|
|
594
|
+
const daySeries = eachDayKey(startKey, todayKey).map((dayKey) => {
|
|
595
|
+
const point = pointOf(dayKey)
|
|
596
|
+
for (const bucket of Object.values(point.cost)) bucket.cost = round2(bucket.cost)
|
|
597
|
+
const denominator = point.input + point.cacheRead + point.cacheWrite
|
|
598
|
+
point.hitRate = denominator > 0 ? point.cacheRead / denominator : null
|
|
599
|
+
return point
|
|
600
|
+
})
|
|
601
|
+
const { grain, series } = buildMonthlySeries(daySeries)
|
|
602
|
+
|
|
390
603
|
return {
|
|
391
604
|
range: selected,
|
|
392
605
|
startAt: dayStartTs(startKey) ?? now,
|
|
@@ -396,6 +609,8 @@ export function buildUsage(state, prefs, range, now, providerInfoInput) {
|
|
|
396
609
|
totals,
|
|
397
610
|
total,
|
|
398
611
|
models: rows,
|
|
612
|
+
series,
|
|
613
|
+
grain,
|
|
399
614
|
}
|
|
400
615
|
}
|
|
401
616
|
|
|
@@ -417,15 +632,18 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
417
632
|
const { plans: legacyPlans, mode: _mode, ...info } = item
|
|
418
633
|
const id = normalizeProvider(info.id ?? info.provider)
|
|
419
634
|
if (id === 'unknown' || hidden.has(id)) continue
|
|
420
|
-
const
|
|
635
|
+
const kind = resolveProviderKind(state, prefs, id, item)
|
|
636
|
+
const currency = usageCurrency(id, item, prefs)
|
|
637
|
+
const base = providerMap.get(id) ?? providerEmpty(id, kind)
|
|
421
638
|
providerMap.set(id, {
|
|
422
639
|
...base,
|
|
423
640
|
...info,
|
|
424
641
|
id,
|
|
425
642
|
name: info.name ?? providerLabel(id),
|
|
426
|
-
kind
|
|
643
|
+
kind,
|
|
427
644
|
canSubscribe: canBeSubscription(id),
|
|
428
|
-
currency
|
|
645
|
+
currency,
|
|
646
|
+
currencySource: currencySourceOf(prefs, id, item),
|
|
429
647
|
usage: { ...base.usage, ...(info.usage ?? {}) },
|
|
430
648
|
balance: info.balance ?? base.balance,
|
|
431
649
|
windows: info.windows ?? legacyPlans ?? base.windows ?? [],
|
|
@@ -433,8 +651,11 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
433
651
|
}
|
|
434
652
|
|
|
435
653
|
// #41/#42:unknown 行按模型名前缀视图层归因(不改 daily.json);models 与 provider.usage 均为当月口径(#42 回退跨月累计)
|
|
654
|
+
const unpriced = []
|
|
436
655
|
const accumulate = (providerId, modelId, row, isToday) => {
|
|
437
|
-
const
|
|
656
|
+
const meta = providerInfo.find((item) => normalizeProvider(item.id ?? item.provider) === providerId)
|
|
657
|
+
const kind = resolveProviderKind(state, prefs, providerId, meta)
|
|
658
|
+
const provider = providerMap.get(providerId) ?? providerEmpty(providerId, kind)
|
|
438
659
|
providerMap.set(providerId, provider)
|
|
439
660
|
const key = `${providerId}:${modelId}`
|
|
440
661
|
const entry = modelsMap.get(key) ?? {
|
|
@@ -443,34 +664,53 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
443
664
|
providerId,
|
|
444
665
|
providerName: providerLabel(providerId),
|
|
445
666
|
displayName: modelLabel(modelId),
|
|
446
|
-
kind
|
|
667
|
+
kind,
|
|
447
668
|
currency: provider.currency,
|
|
669
|
+
currencySource: provider.currencySource,
|
|
448
670
|
priced: true,
|
|
449
671
|
approximate: false,
|
|
672
|
+
reason: null,
|
|
450
673
|
requests: 0,
|
|
451
674
|
input: 0,
|
|
452
675
|
cacheRead: 0,
|
|
453
676
|
cacheWrite: 0,
|
|
454
677
|
output: 0,
|
|
455
678
|
cost: 0,
|
|
679
|
+
rows: 0,
|
|
456
680
|
}
|
|
457
681
|
entry.requests += num(row.requests)
|
|
458
682
|
entry.input += num(row.input)
|
|
459
683
|
entry.cacheRead += num(row.cacheRead)
|
|
460
684
|
entry.cacheWrite += num(row.cacheWrite)
|
|
461
685
|
entry.output += num(row.output)
|
|
462
|
-
entry.
|
|
463
|
-
|
|
464
|
-
const
|
|
686
|
+
entry.rows += 1
|
|
687
|
+
// 订阅行按套餐结算:历史里存下的目录价不算支出(REQ-12)
|
|
688
|
+
const billable = kind === 'api'
|
|
689
|
+
entry.cost += billable ? num(row.cost) : 0
|
|
690
|
+
if (row.priced === false) {
|
|
691
|
+
entry.priced = false
|
|
692
|
+
entry.reason ??= row.reason ?? 'no-model-price'
|
|
693
|
+
}
|
|
694
|
+
if (row.priced === 'empty') entry.emptyRows = (entry.emptyRows ?? 0) + 1
|
|
695
|
+
if (billable && row.priced === false) {
|
|
696
|
+
unpriced.push({
|
|
697
|
+
modelId,
|
|
698
|
+
providerId,
|
|
699
|
+
tokens: num(row.input) + num(row.cacheRead) + num(row.cacheWrite) + num(row.output),
|
|
700
|
+
requests: num(row.requests),
|
|
701
|
+
reason: row.reason ?? 'no-model-price',
|
|
702
|
+
})
|
|
703
|
+
}
|
|
704
|
+
const price = resolvePrice(modelId, providerId, now)
|
|
465
705
|
if (price?.approximate === true) entry.approximate = true
|
|
466
706
|
modelsMap.set(key, entry)
|
|
467
707
|
const tokens = num(row.input) + num(row.cacheRead) + num(row.cacheWrite) + num(row.output)
|
|
468
708
|
provider.usage.monthTokens += tokens
|
|
469
|
-
provider.usage.monthCost += num(row.cost)
|
|
709
|
+
provider.usage.monthCost += billable ? num(row.cost) : 0
|
|
470
710
|
provider.usage.requests += num(row.requests)
|
|
471
711
|
if (isToday) {
|
|
472
712
|
provider.usage.todayTokens += tokens
|
|
473
|
-
provider.usage.todayCost += num(row.cost)
|
|
713
|
+
provider.usage.todayCost += billable ? num(row.cost) : 0
|
|
474
714
|
}
|
|
475
715
|
provider.models.push(entry.modelId)
|
|
476
716
|
}
|
|
@@ -516,14 +756,17 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
516
756
|
displayName: modelLabel(fallbackModel),
|
|
517
757
|
kind: provider?.kind === 'subscription' ? 'subscription' : 'api',
|
|
518
758
|
currency: provider?.currency ?? providerCurrency(id),
|
|
519
|
-
|
|
759
|
+
currencySource: provider?.currencySource ?? 'community',
|
|
760
|
+
priced: 'empty',
|
|
520
761
|
approximate: false,
|
|
762
|
+
reason: null,
|
|
521
763
|
requests: 0,
|
|
522
764
|
input: 0,
|
|
523
765
|
cacheRead: 0,
|
|
524
766
|
cacheWrite: 0,
|
|
525
767
|
output: 0,
|
|
526
768
|
cost: 0,
|
|
769
|
+
rows: 0,
|
|
527
770
|
})
|
|
528
771
|
}
|
|
529
772
|
|
|
@@ -535,6 +778,13 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
535
778
|
: null
|
|
536
779
|
entry.cost = round2(entry.cost)
|
|
537
780
|
entry.share = 0
|
|
781
|
+
// 四态收敛:任一行真缺价 → false;全是零消耗行 → 'empty';其余 true
|
|
782
|
+
if (entry.priced !== false) {
|
|
783
|
+
entry.priced = entry.rows > 0 && (entry.emptyRows ?? 0) < entry.rows ? true : 'empty'
|
|
784
|
+
}
|
|
785
|
+
if (entry.priced !== false) entry.reason = null
|
|
786
|
+
delete entry.rows
|
|
787
|
+
delete entry.emptyRows
|
|
538
788
|
return entry
|
|
539
789
|
})
|
|
540
790
|
models.sort((a, b) => b.cost - a.cost)
|
|
@@ -618,7 +868,7 @@ export function buildSummary(state, prefs, now, providerInfoInput) {
|
|
|
618
868
|
total: totalTokens,
|
|
619
869
|
hitRate,
|
|
620
870
|
},
|
|
621
|
-
cost: { estimated: totalCost, currency: 'CNY', unpricedModels },
|
|
871
|
+
cost: { estimated: totalCost, currency: 'CNY', unpricedModels, unpriced },
|
|
622
872
|
today: { tokens: today.tokens, cost: round2(today.cost), requests: today.requests },
|
|
623
873
|
requests: { month: totals.requests, today: today.requests },
|
|
624
874
|
providers,
|