@angelyeye/dsh-cost-tracker 1.10.1 → 1.11.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/CHANGELOG.md +122 -0
- package/README.en.md +7 -3
- package/README.md +8 -4
- package/client.js +66 -21
- package/config.js +20 -2
- package/docs/provider-pricing.json +326 -21
- package/fast-tier.js +145 -0
- package/import.js +3 -2
- package/index.js +115 -17
- package/package.json +4 -2
- package/pricing.js +63 -8
- package/sync.js +10 -0
- package/token-format.js +61 -0
- package/vendor-catalog.js +191 -17
package/vendor-catalog.js
CHANGED
|
@@ -26,13 +26,20 @@ import { readFileSync } from 'node:fs'
|
|
|
26
26
|
import { createHash } from 'node:crypto'
|
|
27
27
|
|
|
28
28
|
export const CATALOG_META = {
|
|
29
|
-
generatedAt: '2026-
|
|
30
|
-
note: 'USD per 1M tokens; unpriced entries have no verified official price.',
|
|
31
|
-
origin: 'adapted from dsh-cost-meter docs/provider-pricing.json (MIT); refreshed from vendor pricing pages 2026-
|
|
29
|
+
generatedAt: '2026-11-13',
|
|
30
|
+
note: 'USD per 1M tokens; unpriced entries have no verified official price. fast-tier prices live under serviceTiers.fast.',
|
|
31
|
+
origin: 'adapted from dsh-cost-meter docs/provider-pricing.json (MIT); refreshed from vendor pricing pages 2026-11-13',
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/** 目录价 USD→CNY
|
|
35
|
-
export const CATALOG_FX_DEFAULT =
|
|
34
|
+
/** 目录价 USD→CNY 默认汇率(可在配置中覆盖)。v1.11.0 起由 7.2 调整为 6.67。 */
|
|
35
|
+
export const CATALOG_FX_DEFAULT = 6.67
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 历史默认汇率:v1.10.x 及更早版本的默认值。
|
|
39
|
+
* 老配置文件里如果存着这个值,说明用户**从未改过**(只是被默认值写了进去),
|
|
40
|
+
* 升级后应跟随新默认 6.67;用户显式改成别的数字(如 6.9)则保留。
|
|
41
|
+
*/
|
|
42
|
+
export const CATALOG_FX_LEGACY_DEFAULT = 7.2
|
|
36
43
|
|
|
37
44
|
/** 与 pricing.js 的 normalizeModelName 同一实现(避免相互 import 成环) */
|
|
38
45
|
export function normalizeModelName(m) {
|
|
@@ -46,7 +53,7 @@ export const PROVIDER_ALIASES = {
|
|
|
46
53
|
google: 'google', gemini: 'google', deepmind: 'google',
|
|
47
54
|
moonshot: 'moonshot', 'moonshot-ai': 'moonshot', 'moonshotai': 'moonshot', kimi: 'moonshot',
|
|
48
55
|
'z-ai': 'z-ai', zai: 'z-ai', zhipu: 'z-ai', bigmodel: 'z-ai', glm: 'z-ai',
|
|
49
|
-
xai: 'xai', grok: 'xai',
|
|
56
|
+
xai: 'xai', grok: 'xai', 'x-ai': 'xai', 'x-ai-inc': 'xai', xaiinc: 'xai',
|
|
50
57
|
alibaba: 'alibaba', qwen: 'alibaba', dashscope: 'alibaba', ali: 'alibaba',
|
|
51
58
|
minimax: 'minimax',
|
|
52
59
|
tencent: 'tencent', hunyuan: 'tencent',
|
|
@@ -85,21 +92,35 @@ export function catalogProviders() {
|
|
|
85
92
|
return Object.keys(ps).map((id) => ({ id, count: Object.keys((ps[id] && ps[id].models) || {}).length }))
|
|
86
93
|
}
|
|
87
94
|
|
|
88
|
-
/** 惰性索引:目录键 → Map(归一化模型名 → 条目) */
|
|
95
|
+
/** 惰性索引:目录键 → Map(归一化模型名 → 条目);条目不改写,原名另存于 KEY_ORIGINAL */
|
|
89
96
|
let INDEX = null
|
|
97
|
+
/** 归一化模型名 → 目录里的原始写法(展示用,避免出现 gpt61sol 这种名字) */
|
|
98
|
+
let KEY_ORIGINAL = null
|
|
90
99
|
function catalogIndex() {
|
|
91
100
|
if (INDEX) return INDEX
|
|
92
101
|
INDEX = new Map()
|
|
102
|
+
KEY_ORIGINAL = new Map()
|
|
93
103
|
const ps = rawProviders()
|
|
94
104
|
for (const pid of Object.keys(ps)) {
|
|
95
105
|
const models = (ps[pid] && ps[pid].models) || {}
|
|
96
106
|
const m = new Map()
|
|
97
|
-
for (const key of Object.keys(models))
|
|
107
|
+
for (const key of Object.keys(models)) {
|
|
108
|
+
const norm = normalizeModelName(key)
|
|
109
|
+
m.set(norm, models[key])
|
|
110
|
+
if (!KEY_ORIGINAL.has(norm)) KEY_ORIGINAL.set(norm, key)
|
|
111
|
+
}
|
|
98
112
|
INDEX.set(pid, m)
|
|
99
113
|
}
|
|
100
114
|
return INDEX
|
|
101
115
|
}
|
|
102
116
|
|
|
117
|
+
/** 归一化模型名 → 目录原始写法(无记录时回退为归一化名) */
|
|
118
|
+
export function catalogOriginalName(normalized) {
|
|
119
|
+
catalogIndex()
|
|
120
|
+
const n = String(normalized == null ? '' : normalized)
|
|
121
|
+
return (KEY_ORIGINAL && KEY_ORIGINAL.get(n)) || n
|
|
122
|
+
}
|
|
123
|
+
|
|
103
124
|
/** 反向索引:归一化模型名 → 拥有它的目录键列表(跨厂商同名匹配用,惰性) */
|
|
104
125
|
let REVERSE = null
|
|
105
126
|
function reverseIndex() {
|
|
@@ -125,23 +146,100 @@ export function catalogProviderKey(np) {
|
|
|
125
146
|
return ''
|
|
126
147
|
}
|
|
127
148
|
|
|
149
|
+
/**
|
|
150
|
+
* v1.11.0:按**模型名**推断厂商目录键。
|
|
151
|
+
*
|
|
152
|
+
* 为什么需要:真实流量常走「中转站 / 聚合网关」(本机就是 `zhouhy-relay`、
|
|
153
|
+
* `muteki`、`sub.zhouhy.net` 这类名字),provider 名与厂商毫无关系。
|
|
154
|
+
* 这些 provider 在 catalogProviderKey 里查不到,于是所有模型都掉到
|
|
155
|
+
* 通用兜底价(2/8/0.5 CNY)——比官方价低约 6 倍,Fast 档也就永远不生效。
|
|
156
|
+
*
|
|
157
|
+
* 这里只做「前缀 -> 厂商」的确定性映射:模型名本身就带厂商标识
|
|
158
|
+
* (gpt- -> openai、claude- -> anthropic、gemini- -> google …)。
|
|
159
|
+
* 返回的键仍会走正常的命中流程,**推断不出具体条目就返回 null 回退兜底**,
|
|
160
|
+
* 因此不会给陌生模型乱安价格。
|
|
161
|
+
*
|
|
162
|
+
* @param {string} model
|
|
163
|
+
* @returns {string} 目录键,或 ''
|
|
164
|
+
*/
|
|
165
|
+
export function inferProviderFromModel(model) {
|
|
166
|
+
const m = String(model == null ? '' : model).toLowerCase()
|
|
167
|
+
if (!m) return ''
|
|
168
|
+
const RULES = [
|
|
169
|
+
[/^(gpt-|chatgpt-|o[134](?:-|$)|codex)/, 'openai'],
|
|
170
|
+
[/^claude/, 'anthropic'],
|
|
171
|
+
[/^(gemini|gemma|imagen)/, 'google'],
|
|
172
|
+
[/^(kimi|moonshot)/, 'moonshot'],
|
|
173
|
+
[/^(glm|zai-|z-ai)/, 'z-ai'],
|
|
174
|
+
[/^grok/, 'xai'],
|
|
175
|
+
[/^(qwen|qwq|qvq)/, 'alibaba'],
|
|
176
|
+
[/^minimax/, 'minimax'],
|
|
177
|
+
[/^(hy\d|hunyuan)/, 'tencent'],
|
|
178
|
+
[/^mimo/, 'xiaomi'],
|
|
179
|
+
[/^solar/, 'upstage'],
|
|
180
|
+
[/^nemotron/, 'nvidia'],
|
|
181
|
+
[/^(mistral|ministral|codestral|devstral|magistral)/, 'mistral'],
|
|
182
|
+
[/^muse/, 'meta'],
|
|
183
|
+
]
|
|
184
|
+
for (const [re, pid] of RULES) {
|
|
185
|
+
if (re.test(m) && rawProviders()[pid]) return pid
|
|
186
|
+
}
|
|
187
|
+
return ''
|
|
188
|
+
}
|
|
189
|
+
|
|
128
190
|
function toRates(entry, fx) {
|
|
129
191
|
const num = (v) => { const n = Number(v); return Number.isFinite(n) && n >= 0 ? n : 0 }
|
|
130
192
|
const cache = num(entry.cachedInput)
|
|
193
|
+
// v1.11.0:缓存写价优先用条目自带的 cacheWriteInput(OpenAI / Anthropic 与缓存读不同价),
|
|
194
|
+
// 没有该字段时沿用旧约定(写价 = 缓存命中价),保证历史条目行为不变。
|
|
195
|
+
const cacheWrite = entry.cacheWriteInput === undefined ? cache : num(entry.cacheWriteInput)
|
|
131
196
|
return {
|
|
132
197
|
input: num(entry.input) * fx,
|
|
133
198
|
output: num(entry.output) * fx,
|
|
134
199
|
cacheRead: cache * fx,
|
|
135
|
-
cacheWrite:
|
|
200
|
+
cacheWrite: cacheWrite * fx,
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 取条目某个档位(fast / standard)的价格块,若请求输入量超过长上下文阈值则改用 longContext。
|
|
206
|
+
* @param {object} entry - 目录条目
|
|
207
|
+
* @param {{input?:number, cacheRead?:number, cacheWrite?:number}} [tokens] - 本次用量
|
|
208
|
+
* @returns {{ block: object, contextTier: 'short'|'long' }}
|
|
209
|
+
*/
|
|
210
|
+
function pickBlock(entry, tokens) {
|
|
211
|
+
const long = entry && entry.longContext
|
|
212
|
+
if (!long) return { block: entry || {}, contextTier: 'short' }
|
|
213
|
+
const t = tokens || {}
|
|
214
|
+
const num = (v) => { const n = Number(v); return Number.isFinite(n) && n > 0 ? n : 0 }
|
|
215
|
+
// 阈值只按**输入侧**判定(官方口径:prompt > 272K),输出 token 不计入,
|
|
216
|
+
// 否则「长输入短输出」的请求会被误判成长档。
|
|
217
|
+
const promptTokens = num(t.input) + num(t.cacheRead) + num(t.cacheWrite)
|
|
218
|
+
const threshold = num(long.inputTokensAbove)
|
|
219
|
+
if (threshold > 0 && promptTokens > threshold) {
|
|
220
|
+
// 只覆盖价格字段,**不能**把 longContext / serviceTiers 带进来:
|
|
221
|
+
// 否则标准档会误用 Fast 价(曾实测:standard 请求被算成 $4 而不是 $2)。
|
|
222
|
+
return {
|
|
223
|
+
block: {
|
|
224
|
+
input: long.input, cachedInput: long.cachedInput, output: long.output,
|
|
225
|
+
...(long.cacheWriteInput === undefined ? {} : { cacheWriteInput: long.cacheWriteInput }),
|
|
226
|
+
},
|
|
227
|
+
contextTier: 'long',
|
|
228
|
+
}
|
|
136
229
|
}
|
|
230
|
+
return { block: entry, contextTier: 'short' }
|
|
137
231
|
}
|
|
138
232
|
|
|
139
233
|
/**
|
|
140
234
|
* 在目录中查找某次调用的价格。
|
|
141
235
|
* @param {string} np - 归一化 provider 名
|
|
142
236
|
* @param {string} model - 模型名
|
|
143
|
-
* @param {{mode?:'exact'|'fuzzy', fx?:number
|
|
144
|
-
*
|
|
237
|
+
* @param {{mode?:'exact'|'fuzzy', fx?:number, serviceTier?:'standard'|'fast',
|
|
238
|
+
* tokens?:{input?:number, cacheRead?:number, cacheWrite?:number}}} [opts]
|
|
239
|
+
* serviceTier='fast' 时优先用条目的 serviceTiers.fast(无该块则回落标准价并标 standard-only);
|
|
240
|
+
* tokens 用于长上下文分档(超过 longContext.inputTokensAbove 时按整请求长档价)。
|
|
241
|
+
* @returns {object|null} { provider, catalogModel, rates(CNY), sourceUrl, checkedAt, fx, via,
|
|
242
|
+
* serviceTier, tierPriceSource, contextTier, tierNote? }
|
|
145
243
|
* via: 'exact' | 'fuzzy' | 'cross-provider'
|
|
146
244
|
*/
|
|
147
245
|
export function catalogEntryFor(np, model, opts) {
|
|
@@ -149,7 +247,14 @@ export function catalogEntryFor(np, model, opts) {
|
|
|
149
247
|
const mode = o.mode === 'exact' ? 'exact' : 'fuzzy'
|
|
150
248
|
const fx = Number.isFinite(Number(o.fx)) && Number(o.fx) > 0 ? Number(o.fx) : CATALOG_FX_DEFAULT
|
|
151
249
|
let pid = catalogProviderKey(np)
|
|
152
|
-
|
|
250
|
+
// v1.11.0:中转站 / 聚合网关的 provider 名与厂商无关(如 zhouhy-relay),
|
|
251
|
+
// 按模型名前缀推断厂商;推断不出就照旧返回 null,落到 provider / 通用兜底。
|
|
252
|
+
let viaProvider = 'alias'
|
|
253
|
+
if (!pid) {
|
|
254
|
+
pid = inferProviderFromModel(model)
|
|
255
|
+
if (!pid) return null
|
|
256
|
+
viaProvider = 'model-inferred'
|
|
257
|
+
}
|
|
153
258
|
const models = catalogIndex().get(pid)
|
|
154
259
|
if (!models || models.size === 0) return null
|
|
155
260
|
const n = normalizeModelName(model)
|
|
@@ -179,16 +284,35 @@ export function catalogEntryFor(np, model, opts) {
|
|
|
179
284
|
}
|
|
180
285
|
if (!hit) return null
|
|
181
286
|
const num = (v) => { const c = Number(v); return Number.isFinite(c) ? c : 0 }
|
|
287
|
+
// ---- v1.11.0:服务档位(GPT Fast / priority)----
|
|
288
|
+
// 只有条目显式登记 serviceTiers.fast 才按 Fast 计;没登记的模型一律按标准价,
|
|
289
|
+
// 绝不按「×2」推算 —— 价格必须来自官方或可核对的来源。
|
|
290
|
+
const wantFast = String(o.serviceTier || '').toLowerCase() === 'fast'
|
|
291
|
+
const fastEntry = wantFast && hit.serviceTiers && hit.serviceTiers.fast ? hit.serviceTiers.fast : null
|
|
292
|
+
const priced = fastEntry || hit
|
|
293
|
+
const { block, contextTier } = pickBlock(priced, o.tokens)
|
|
294
|
+
const serviceTier = fastEntry ? 'fast' : 'standard'
|
|
295
|
+
let tierNote = ''
|
|
296
|
+
if (wantFast && !fastEntry) {
|
|
297
|
+
tierNote = '该模型无 Fast 档价,按标准价计'
|
|
298
|
+
} else if (wantFast && fastEntry && block === priced && priced.longContext) {
|
|
299
|
+
tierNote = ''
|
|
300
|
+
}
|
|
182
301
|
return {
|
|
183
302
|
provider: pid,
|
|
184
303
|
catalogModel: hit.__key || undefined,
|
|
185
|
-
rates: toRates(
|
|
186
|
-
sourceUrl: String(hit.sourceUrl || ''),
|
|
187
|
-
checkedAt: String(hit.checkedAt || ''),
|
|
304
|
+
rates: toRates(block, fx),
|
|
305
|
+
sourceUrl: String((fastEntry && hit.sourceUrlFast) || hit.sourceUrl || ''),
|
|
306
|
+
checkedAt: String((fastEntry && hit.checkedAtFast) || hit.checkedAt || ''),
|
|
188
307
|
notes: String(hit.notes || ''),
|
|
189
308
|
fx,
|
|
190
|
-
via,
|
|
191
|
-
|
|
309
|
+
via: viaProvider === 'model-inferred' ? via + '+model-inferred' : via,
|
|
310
|
+
serviceTier,
|
|
311
|
+
tierPriceSource: fastEntry ? 'catalog-fast' : (wantFast ? 'standard-only' : 'standard'),
|
|
312
|
+
...(tierNote ? { tierNote } : {}),
|
|
313
|
+
contextTier,
|
|
314
|
+
longContextThreshold: block && block.inputTokensAbove !== undefined ? num(block.inputTokensAbove) : undefined,
|
|
315
|
+
priceHint: { input: num(block.input), output: num(block.output), cachedInput: num(block.cachedInput), currency: 'USD', serviceTier },
|
|
192
316
|
}
|
|
193
317
|
}
|
|
194
318
|
|
|
@@ -198,3 +322,53 @@ export function catalogModelsFor(np) {
|
|
|
198
322
|
if (!pid) return []
|
|
199
323
|
return [...catalogIndex().get(pid).keys()]
|
|
200
324
|
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* v1.11.0:列出目录中**登记了 Fast 档价**的模型,并给出标准价 / Fast 价对照。
|
|
328
|
+
* 展示层(设置页、cost_prices 工具)据此说明哪些模型走 Fast 会加价,以及具体数额。
|
|
329
|
+
*
|
|
330
|
+
* @param {number} [fx] - USD→CNY 汇率(缺省用 CATALOG_FX_DEFAULT)
|
|
331
|
+
* @returns {{ fx:number, checkedAt:string, models:Array<object> }}
|
|
332
|
+
* 每项:{ provider, model, standard:{input,cachedInput,output}, fast:{...},
|
|
333
|
+
* standardCny, fastCny, longContext?, sourceUrl, checkedAt, confidence }
|
|
334
|
+
*/
|
|
335
|
+
export function catalogFastTiers(fx) {
|
|
336
|
+
const rate = Number.isFinite(Number(fx)) && Number(fx) > 0 ? Number(fx) : CATALOG_FX_DEFAULT
|
|
337
|
+
const out = []
|
|
338
|
+
let checkedAt = ''
|
|
339
|
+
const yen = (v) => Math.round(Number(v) * rate * 10000) / 10000
|
|
340
|
+
for (const [pid, models] of catalogIndex()) {
|
|
341
|
+
for (const [normName, entry] of models) {
|
|
342
|
+
const fast = entry && entry.serviceTiers && entry.serviceTiers.fast
|
|
343
|
+
if (!fast) continue
|
|
344
|
+
const std = { input: Number(entry.input) || 0, cachedInput: Number(entry.cachedInput) || 0, output: Number(entry.output) || 0 }
|
|
345
|
+
const fst = { input: Number(fast.input) || 0, cachedInput: Number(fast.cachedInput) || 0, output: Number(fast.output) || 0 }
|
|
346
|
+
const check = String(entry.checkedAtFast || '')
|
|
347
|
+
if (check && check > checkedAt) checkedAt = check
|
|
348
|
+
out.push({
|
|
349
|
+
provider: pid,
|
|
350
|
+
model: catalogOriginalName(normName),
|
|
351
|
+
standard: std,
|
|
352
|
+
fast: fst,
|
|
353
|
+
standardCny: { input: yen(std.input), cachedInput: yen(std.cachedInput), output: yen(std.output) },
|
|
354
|
+
fastCny: { input: yen(fst.input), cachedInput: yen(fst.cachedInput), output: yen(fst.output) },
|
|
355
|
+
...(entry.longContext ? { longContext: entry.longContext } : {}),
|
|
356
|
+
...(fast.longContext ? { fastLongContext: fast.longContext } : {}),
|
|
357
|
+
sourceUrl: String(entry.sourceUrlFast || ''),
|
|
358
|
+
checkedAt: check,
|
|
359
|
+
confidence: String(fast.confidence || ''),
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
out.sort((a, b) => (a.provider === b.provider ? a.model.localeCompare(b.model) : a.provider.localeCompare(b.provider)))
|
|
364
|
+
return { fx: rate, checkedAt, models: out }
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** v1.11.0:目录中**没有** Fast 档价的模型清单(展示层据此说明「该模型不区分 Fast」) */
|
|
368
|
+
export function catalogModelsWithoutFastTier(np) {
|
|
369
|
+
const pid = catalogProviderKey(np)
|
|
370
|
+
if (!pid) return []
|
|
371
|
+
const models = catalogIndex().get(pid)
|
|
372
|
+
if (!models) return []
|
|
373
|
+
return [...models.entries()].filter(([, e]) => !(e && e.serviceTiers && e.serviceTiers.fast)).map(([k]) => k)
|
|
374
|
+
}
|