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/match.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-name matching: pure functions over catalog entries.
|
|
3
|
+
* Knows nothing about providers, credentials or overrides — those belong to
|
|
4
|
+
* `pricing.js` (which has the provider string) and `core.js` (which decides
|
|
5
|
+
* whether a row is billable at all).
|
|
6
|
+
*/
|
|
7
|
+
import { normalizeModel } from './providers.js'
|
|
8
|
+
|
|
9
|
+
/** Words that change the price tier: never stripped, never matched loosely. */
|
|
10
|
+
const SIGNIFICANT = new Set([
|
|
11
|
+
'flash', 'pro', 'max', 'mini', 'air', 'plus', 'turbo', 'lite',
|
|
12
|
+
'coder', 'code', 'reasoner', 'chat', 'vision',
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
/** Deployment noise: stripped for fuzzy matching, kept as evidence in logs. */
|
|
16
|
+
const NOISE = new Set([
|
|
17
|
+
'ioa', 'exp', 'experimental', 'preview', 'beta', 'alpha', 'rc', 'dev', 'test',
|
|
18
|
+
'latest', 'stable', 'prod', 'production', 'free', 'ga',
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
/** A bare `v` left over after removing the version segment is a marker, not a family word. */
|
|
22
|
+
const VERSION_MARK = 'v'
|
|
23
|
+
|
|
24
|
+
/** Non-leading digit runs of 3+ chars are dates / build ids, not family names. */
|
|
25
|
+
function isNumericNoise(part) {
|
|
26
|
+
return /^\d{3,}$/.test(part)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Split a model id into `{ version, family, tags }`.
|
|
31
|
+
* `deepseek-v4.1-flash-ioa` → version `4.1`, family `deepseek`, tags `{flash, ioa}`.
|
|
32
|
+
*/
|
|
33
|
+
export function parseModelId(raw) {
|
|
34
|
+
const id = normalizeModel(raw)
|
|
35
|
+
if (id === 'unknown') return null
|
|
36
|
+
const version = id.match(/\d+(?:\.\d+)*/)?.[0] ?? null
|
|
37
|
+
const rest = version === null ? id : id.replace(version, '|')
|
|
38
|
+
const parts = rest.split(/[-_|]/).filter((part) => part !== '')
|
|
39
|
+
const family = []
|
|
40
|
+
const tags = new Set()
|
|
41
|
+
parts.forEach((part) => {
|
|
42
|
+
if (part === VERSION_MARK) return
|
|
43
|
+
if (SIGNIFICANT.has(part) || NOISE.has(part) || isNumericNoise(part)) {
|
|
44
|
+
tags.add(part)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
family.push(part)
|
|
48
|
+
})
|
|
49
|
+
return { version, family: family.join('-'), tags }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Table-provided regexes are pre-compiled in `catalog.js`. */
|
|
53
|
+
function patternByModel(modelId, entries) {
|
|
54
|
+
for (const entry of entries) {
|
|
55
|
+
for (const regexp of entry.regexps) {
|
|
56
|
+
if (regexp.test(modelId)) return entry
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parsedOf(entry) {
|
|
63
|
+
entry.parsed ??= parseModelId(entry.modelId)
|
|
64
|
+
return entry.parsed
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Fuzzy candidates: same version segment, same family, candidate tags covered by
|
|
69
|
+
* the request, every extra request tag either significant-free or known noise.
|
|
70
|
+
*/
|
|
71
|
+
function fuzzyCandidates(modelId, entries) {
|
|
72
|
+
const req = parseModelId(modelId)
|
|
73
|
+
if (req === null) return []
|
|
74
|
+
return entries.filter((entry) => {
|
|
75
|
+
const parsed = parsedOf(entry)
|
|
76
|
+
if (parsed === null) return false
|
|
77
|
+
if (parsed.version !== req.version || parsed.family !== req.family) return false
|
|
78
|
+
for (const tag of parsed.tags) {
|
|
79
|
+
if (!req.tags.has(tag)) return false
|
|
80
|
+
}
|
|
81
|
+
for (const tag of req.tags) {
|
|
82
|
+
if (!parsed.tags.has(tag) && !NOISE.has(tag)) return false
|
|
83
|
+
}
|
|
84
|
+
return true
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Four-level short-circuit: exact → alias → pattern → fuzzy.
|
|
90
|
+
* Returns `{ entry, confidence, deltaTags }` or null; never guesses on ties.
|
|
91
|
+
*/
|
|
92
|
+
export function resolveMatch(model, entries) {
|
|
93
|
+
const modelId = normalizeModel(model)
|
|
94
|
+
if (modelId === 'unknown') return null
|
|
95
|
+
|
|
96
|
+
const exact = entries.find((entry) => entry.modelId === modelId)
|
|
97
|
+
if (exact !== undefined) return { entry: exact, confidence: 'exact', deltaTags: [] }
|
|
98
|
+
|
|
99
|
+
const alias = entries.find((entry) => entry.aliases.includes(modelId))
|
|
100
|
+
if (alias !== undefined) return { entry: alias, confidence: 'alias', deltaTags: [] }
|
|
101
|
+
|
|
102
|
+
const pattern = patternByModel(modelId, entries)
|
|
103
|
+
if (pattern !== null) return { entry: pattern, confidence: 'pattern', deltaTags: [] }
|
|
104
|
+
|
|
105
|
+
const candidates = fuzzyCandidates(modelId, entries)
|
|
106
|
+
if (candidates.length !== 1) return null
|
|
107
|
+
const entry = candidates[0]
|
|
108
|
+
const reqTags = parseModelId(modelId)?.tags ?? new Set()
|
|
109
|
+
const entryTags = parsedOf(entry)?.tags ?? new Set()
|
|
110
|
+
const deltaTags = [...reqTags].filter((tag) => !entryTags.has(tag))
|
|
111
|
+
return { entry, confidence: 'fuzzy', deltaTags }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Why did `resolveMatch` fail? `ambiguous` vs `no-model-price` (for `reason`). */
|
|
115
|
+
export function explainMiss(model, entries) {
|
|
116
|
+
const modelId = normalizeModel(model)
|
|
117
|
+
if (modelId === 'unknown') return 'invalid-model'
|
|
118
|
+
return fuzzyCandidates(modelId, entries).length > 1 ? 'ambiguous' : 'no-model-price'
|
|
119
|
+
}
|
package/src/pricing.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Pricing: catalog I/O + table selection + peak/off-peak + override short-circuit.
|
|
3
|
+
* Matching itself lives in `match.js`; shape normalization lives in `catalog.js`.
|
|
4
|
+
* This module never sees credentials and never asks who the provider is.
|
|
4
5
|
*/
|
|
5
6
|
import { readFileSync } from 'node:fs'
|
|
7
|
+
import { loadCatalog, routerModels } from './catalog.js'
|
|
8
|
+
import { explainMiss, resolveMatch } from './match.js'
|
|
6
9
|
import { normalizeModel, normalizeProvider, providerCurrency } from './providers.js'
|
|
7
10
|
|
|
8
11
|
const BUNDLED = JSON.parse(readFileSync(new URL('./catalog.json', import.meta.url), 'utf8'))
|
|
@@ -12,84 +15,19 @@ const GITEE_PAGE = 'https://gitee.com/oyw125/model-price-table'
|
|
|
12
15
|
const GITHUB_PAGE = 'https://github.com/LaoYueHanNi/model-price-table'
|
|
13
16
|
const ROUTER = 'https://openrouter.ai/api/v1/models'
|
|
14
17
|
|
|
15
|
-
let catalog =
|
|
16
|
-
let
|
|
17
|
-
let catalogSource = 'bundled'
|
|
18
|
+
let catalog = loadCatalog(BUNDLED, 'bundled')
|
|
19
|
+
let routerCatalog = { entries: [], currency: 'USD', source: 'router', updatedAt: null }
|
|
18
20
|
let catalogUrl = null
|
|
19
|
-
let routerPrices = null
|
|
20
21
|
let communityCache = { at: 0, value: null }
|
|
22
|
+
/** `provider:model` / `model` → normalized override quote (set by `configurePricing`). */
|
|
23
|
+
let overrides = new Map()
|
|
24
|
+
/** `(providerId) => 'CNY' | 'USD'`; declared prefs first, registry as fallback. */
|
|
25
|
+
let currencyOf = (providerId) => providerCurrency(providerId)
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function pricesOf(raw) {
|
|
27
|
-
return {
|
|
28
|
-
input: finite(raw?.input),
|
|
29
|
-
cacheRead: finite(raw?.cacheRead) ?? 0,
|
|
30
|
-
cacheWrite: finite(raw?.cacheWrite) ?? 0,
|
|
31
|
-
output: finite(raw?.output),
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function normalizeEntry(raw) {
|
|
36
|
-
const provider = normalizeProvider(raw?.provider)
|
|
37
|
-
const model = normalizeModel(raw?.model)
|
|
38
|
-
if (provider === 'unknown' || model === 'unknown') return null
|
|
39
|
-
const currency = raw?.currency === 'USD' ? 'USD' : (raw?.currency === 'CNY' || raw?.currency === 'RMB' ? 'CNY' : providerCurrency(provider))
|
|
40
|
-
const prices = raw?.prices !== undefined ? pricesOf(raw.prices) : pricesOf(raw)
|
|
41
|
-
// 二次归一化会显式传 null:null 必须等同于"没有峰谷价目"
|
|
42
|
-
const peakRaw = raw?.peak != null ? pricesOf(raw.peak) : null
|
|
43
|
-
const offPeakRaw = raw?.offPeak != null ? pricesOf(raw.offPeak) : null
|
|
44
|
-
const hasFlat = prices.input !== null && prices.output !== null
|
|
45
|
-
const hasTiers = peakRaw !== null && offPeakRaw !== null && peakRaw.input !== null && peakRaw.output !== null && offPeakRaw.input !== null && offPeakRaw.output !== null
|
|
46
|
-
if (!hasFlat && !hasTiers) return null
|
|
47
|
-
const peak = hasTiers ? peakRaw : null
|
|
48
|
-
const offPeak = hasTiers ? offPeakRaw : null
|
|
49
|
-
return {
|
|
50
|
-
provider,
|
|
51
|
-
model,
|
|
52
|
-
displayName: String(raw?.displayName ?? model),
|
|
53
|
-
family: String(raw?.family ?? model),
|
|
54
|
-
releasedAt: String(raw?.releasedAt ?? raw?.updatedAt ?? ''),
|
|
55
|
-
currency,
|
|
56
|
-
aliases: Array.isArray(raw?.aliases) ? raw.aliases.map((alias) => normalizeModel(alias)) : [],
|
|
57
|
-
prices: hasTiers ? null : prices,
|
|
58
|
-
peak,
|
|
59
|
-
offPeak,
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function normalizeCatalog(raw) {
|
|
64
|
-
const models = []
|
|
65
|
-
for (const item of Array.isArray(raw?.models) ? raw.models : []) {
|
|
66
|
-
const entry = normalizeEntry(item)
|
|
67
|
-
if (entry !== null) models.push(entry)
|
|
68
|
-
}
|
|
69
|
-
return { models }
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function allEntries() {
|
|
73
|
-
return [...catalog.models, ...(routerPrices?.models ?? [])]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function exactEntry(provider, model) {
|
|
77
|
-
return allEntries().find((entry) => entry.provider === provider && entry.model === model) ?? null
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function aliasEntry(provider, model) {
|
|
81
|
-
return allEntries().find((entry) => entry.provider === provider && entry.aliases.includes(model)) ?? null
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function familyEntry(provider, model) {
|
|
85
|
-
const candidates = allEntries().filter((entry) => entry.provider === provider && entry.family !== '' && model.startsWith(entry.family))
|
|
86
|
-
if (candidates.length === 0) return null
|
|
87
|
-
return candidates.sort((left, right) => String(right.releasedAt).localeCompare(String(left.releasedAt)))[0]
|
|
88
|
-
}
|
|
89
|
-
|
|
27
|
+
/** Prices are per million tokens in both the community feed and the bundled catalog. */
|
|
90
28
|
function effectivePrices(entry, ts) {
|
|
91
29
|
if (entry.peak !== null && entry.offPeak !== null) {
|
|
92
|
-
//
|
|
30
|
+
// Beijing workdays 09:00-12:00 / 14:00-18:00 are peak; weekends are off-peak.
|
|
93
31
|
const date = new Date(ts + 8 * 60 * 60 * 1000)
|
|
94
32
|
const hour = date.getUTCHours()
|
|
95
33
|
const weekday = date.getUTCDay()
|
|
@@ -100,180 +38,184 @@ function effectivePrices(entry, ts) {
|
|
|
100
38
|
return entry.prices
|
|
101
39
|
}
|
|
102
40
|
|
|
103
|
-
function
|
|
41
|
+
function overrideQuote(modelId, providerId) {
|
|
42
|
+
if (overrides.size === 0) return null
|
|
43
|
+
const byProvider = overrides.get(`${providerId}:${modelId}`)
|
|
44
|
+
if (byProvider !== undefined) return byProvider
|
|
45
|
+
return overrides.get(modelId) ?? null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Only `USD` providers may use router prices; everything else stays on the
|
|
50
|
+
* community table so a CNY number never wears a `$`.
|
|
51
|
+
*/
|
|
52
|
+
function selectTable(entry, currency) {
|
|
53
|
+
if (currency === 'USD') {
|
|
54
|
+
const routed = routerCatalog.entries.find((item) => item.modelId === entry.modelId)
|
|
55
|
+
if (routed !== undefined) {
|
|
56
|
+
return { name: 'router', entry: routed, currency: 'USD' }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { name: entry.source === 'bundled' ? 'bundled' : 'community', entry, currency: entry.currency }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Resolve one line item. Override wins and short-circuits table selection: the
|
|
64
|
+
* price and the currency must come from the same place.
|
|
65
|
+
*/
|
|
66
|
+
export function resolvePrice(model, provider, ts = Date.now()) {
|
|
67
|
+
const modelId = normalizeModel(model)
|
|
68
|
+
if (modelId === 'unknown') return null
|
|
69
|
+
const providerId = normalizeProvider(provider)
|
|
70
|
+
/** provider 应使用的币种;与技术表币种不同即 mismatch(判定权在 core,这里只标注)。 */
|
|
71
|
+
const currency = currencyOf(providerId)
|
|
72
|
+
|
|
73
|
+
const override = overrideQuote(modelId, providerId)
|
|
74
|
+
if (override !== null) {
|
|
75
|
+
return {
|
|
76
|
+
input: override.input,
|
|
77
|
+
cacheRead: override.cacheRead,
|
|
78
|
+
cacheWrite: override.cacheWrite,
|
|
79
|
+
output: override.output,
|
|
80
|
+
currency: override.currency,
|
|
81
|
+
mismatch: override.currency !== currency,
|
|
82
|
+
confidence: 'override',
|
|
83
|
+
approximate: false,
|
|
84
|
+
catalogModel: modelId,
|
|
85
|
+
table: 'override',
|
|
86
|
+
source: 'override',
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const matched = resolveMatch(modelId, catalog.entries)
|
|
91
|
+
if (matched === null) return null
|
|
92
|
+
const table = selectTable(matched.entry, currency)
|
|
93
|
+
const prices = effectivePrices(table.entry, ts)
|
|
94
|
+
if (prices === null || prices.input === null || prices.output === null) return null
|
|
104
95
|
return {
|
|
105
|
-
input:
|
|
106
|
-
cacheRead:
|
|
107
|
-
cacheWrite:
|
|
108
|
-
output:
|
|
96
|
+
input: prices.input,
|
|
97
|
+
cacheRead: prices.cacheRead,
|
|
98
|
+
cacheWrite: prices.cacheWrite,
|
|
99
|
+
output: prices.output,
|
|
100
|
+
currency: table.currency,
|
|
101
|
+
mismatch: table.currency !== currency,
|
|
102
|
+
confidence: matched.confidence,
|
|
103
|
+
approximate: matched.confidence === 'fuzzy',
|
|
104
|
+
catalogModel: table.entry.modelId,
|
|
105
|
+
table: table.name,
|
|
106
|
+
source: catalog.source,
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
/**
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
const slot = slots.find((item) => Array.isArray(item?.windows) && item.windows.length === 2)
|
|
116
|
-
if (slot === undefined) return null
|
|
117
|
-
const windows = slot.windows.map((item) => [Number(item?.startMinute), Number(item?.endMinute)])
|
|
118
|
-
const standard = windows[0][0] === 540 && windows[0][1] === 720 && windows[1][0] === 840 && windows[1][1] === 1080
|
|
119
|
-
const days = Array.isArray(slot.daysOfWeek) ? slot.daysOfWeek.map(Number) : [1, 2, 3, 4, 5]
|
|
120
|
-
const weekdays = days.length === 5 && [1, 2, 3, 4, 5].every((day) => days.includes(day))
|
|
121
|
-
if (!standard || !weekdays) return null
|
|
122
|
-
const peak = communityPrices(slot)
|
|
123
|
-
const offPeak = communityPrices(raw)
|
|
124
|
-
if (peak.input === null || peak.output === null || offPeak.input === null || offPeak.output === null) return null
|
|
125
|
-
return { peak, offPeak }
|
|
110
|
+
/** Same failure taxonomy as the plan's `reason` field, for unpriced rows. */
|
|
111
|
+
export function explainPriceMiss(model) {
|
|
112
|
+
return explainMiss(model, catalog.entries)
|
|
126
113
|
}
|
|
127
114
|
|
|
128
115
|
/**
|
|
129
|
-
*
|
|
130
|
-
* `
|
|
116
|
+
* Inject prefs-derived inputs. `overrides` keys may be `provider:model` or
|
|
117
|
+
* `model`; both sides are normalized here so callers stay dumb.
|
|
131
118
|
*/
|
|
132
|
-
export function
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
119
|
+
export function configurePricing(input = {}) {
|
|
120
|
+
const next = new Map()
|
|
121
|
+
for (const [key, value] of Object.entries(input.overrides ?? {})) {
|
|
122
|
+
const quote = normalizeOverride(value)
|
|
123
|
+
if (quote === null) continue
|
|
124
|
+
const [left, right] = String(key).split(':')
|
|
125
|
+
const normalized = right === undefined
|
|
126
|
+
? normalizeModel(left)
|
|
127
|
+
: `${normalizeProvider(left)}:${normalizeModel(right)}`
|
|
128
|
+
if (normalized === 'unknown' || normalized.endsWith(':unknown')) continue
|
|
129
|
+
next.set(normalized, quote)
|
|
139
130
|
}
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
131
|
+
overrides = next
|
|
132
|
+
if (typeof input.currencyOf === 'function') currencyOf = input.currencyOf
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function normalizeOverride(raw) {
|
|
136
|
+
const input = Number(raw?.input)
|
|
137
|
+
const output = Number(raw?.output)
|
|
138
|
+
if (!Number.isFinite(input) || input < 0) return null
|
|
139
|
+
if (!Number.isFinite(output) || output < 0) return null
|
|
140
|
+
const optional = (value) => {
|
|
141
|
+
const parsed = Number(value ?? 0)
|
|
142
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0
|
|
144
143
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
return {
|
|
145
|
+
input,
|
|
146
|
+
cacheRead: optional(raw?.cacheRead),
|
|
147
|
+
cacheWrite: optional(raw?.cacheWrite),
|
|
148
|
+
output,
|
|
149
|
+
currency: raw?.currency === 'USD' ? 'USD' : 'CNY',
|
|
149
150
|
}
|
|
150
|
-
return null
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
export function catalogInfo() {
|
|
154
|
-
return {
|
|
154
|
+
return {
|
|
155
|
+
version: catalog.version ?? null,
|
|
156
|
+
source: catalog.source,
|
|
157
|
+
updatedAt: catalog.updatedAt,
|
|
158
|
+
url: catalogUrl,
|
|
159
|
+
models: catalog.entries.length,
|
|
160
|
+
routerModels: routerCatalog.entries.length,
|
|
161
|
+
overrides: overrides.size,
|
|
162
|
+
}
|
|
155
163
|
}
|
|
156
164
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
165
|
+
/** Tests: replace the community catalog wholesale (provider fields are ignored). */
|
|
166
|
+
export function setCatalogForTests(models, source = 'test') {
|
|
167
|
+
catalog = loadCatalog({ models }, source)
|
|
168
|
+
routerCatalog = { entries: [], currency: 'USD', source: 'router', updatedAt: null }
|
|
169
|
+
catalogUrl = null
|
|
162
170
|
communityCache = { at: 0, value: null }
|
|
163
171
|
}
|
|
164
172
|
|
|
165
|
-
function
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
model: raw.modelId ?? raw.model,
|
|
170
|
-
displayName: raw.displayName ?? raw.modelId ?? raw.model,
|
|
171
|
-
family: raw.family ?? '',
|
|
172
|
-
releasedAt: raw.releasedAt ?? raw.updatedAt ?? '',
|
|
173
|
-
currency: raw.currency === 'RMB' ? 'CNY' : raw.currency,
|
|
174
|
-
input: raw.input ?? raw.inputCostPerMillion,
|
|
175
|
-
cacheRead: raw.cacheRead ?? raw.cacheReadCostPerMillion,
|
|
176
|
-
cacheWrite: raw.cacheWrite ?? raw.cacheCreationCostPerMillion,
|
|
177
|
-
output: raw.output ?? raw.outputCostPerMillion,
|
|
178
|
-
aliases: Array.isArray(raw.aliases) ? raw.aliases : [],
|
|
179
|
-
...(tier === null ? {} : tier),
|
|
173
|
+
function fetchJson(url, timeoutMs = 6000) {
|
|
174
|
+
return fetch(url, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(timeoutMs) }).then((response) => {
|
|
175
|
+
if (!response.ok) throw new Error(`http-${response.status}`)
|
|
176
|
+
return response.json()
|
|
180
177
|
})
|
|
181
178
|
}
|
|
182
179
|
|
|
183
|
-
|
|
184
|
-
const response = await fetch(url, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(timeoutMs) })
|
|
185
|
-
if (!response.ok) throw new Error(`http-${response.status}`)
|
|
186
|
-
return response.json()
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** Load community catalog; Gitee first, GitHub second. Never throws. */
|
|
180
|
+
/** Community catalog: Gitee first, GitHub second, bundled as last resort. Never throws. */
|
|
190
181
|
export async function loadCommunityPricing({ force = false } = {}) {
|
|
191
182
|
if (!force && communityCache.value !== null && Date.now() - communityCache.at < 24 * 3600 * 1000) return communityCache.value
|
|
192
|
-
for (const url of [GITEE, GITHUB]) {
|
|
183
|
+
for (const [url, source, page] of [[GITEE, 'community-gitee', GITEE_PAGE], [GITHUB, 'community-github', GITHUB_PAGE]]) {
|
|
193
184
|
try {
|
|
194
185
|
const raw = await fetchJson(url)
|
|
195
|
-
const parsed =
|
|
196
|
-
|
|
197
|
-
const normalized = remoteEntry({ ...item, provider: item.provider ?? inferProvider(item.modelId) })
|
|
198
|
-
return normalized === null ? null : {
|
|
199
|
-
provider: normalized.provider,
|
|
200
|
-
model: normalized.model,
|
|
201
|
-
displayName: normalized.displayName,
|
|
202
|
-
family: normalized.family,
|
|
203
|
-
releasedAt: normalized.releasedAt,
|
|
204
|
-
currency: normalized.currency,
|
|
205
|
-
aliases: normalized.aliases,
|
|
206
|
-
prices: normalized.prices,
|
|
207
|
-
peak: normalized.peak,
|
|
208
|
-
offPeak: normalized.offPeak,
|
|
209
|
-
}
|
|
210
|
-
}).filter(Boolean),
|
|
211
|
-
})
|
|
212
|
-
if (parsed.models.length === 0) throw new Error('empty-catalog')
|
|
186
|
+
const parsed = loadCatalog(raw, source)
|
|
187
|
+
if (parsed.entries.length === 0) throw new Error('empty-catalog')
|
|
213
188
|
catalog = parsed
|
|
214
|
-
|
|
215
|
-
catalogUrl = url === GITEE ? GITEE_PAGE : GITHUB_PAGE
|
|
216
|
-
catalogUpdatedAt = raw?.updatedAt === undefined ? null : String(raw.updatedAt)
|
|
189
|
+
catalogUrl = page
|
|
217
190
|
communityCache = { at: Date.now(), value: catalogInfo() }
|
|
218
191
|
return catalogInfo()
|
|
219
192
|
} catch {
|
|
220
|
-
// try next mirror, then bundled
|
|
193
|
+
// try next mirror, then keep the bundled catalog
|
|
221
194
|
}
|
|
222
195
|
}
|
|
223
196
|
return catalogInfo()
|
|
224
197
|
}
|
|
225
198
|
|
|
226
|
-
|
|
227
|
-
const id = String(modelId ?? '').toLowerCase()
|
|
228
|
-
if (id.includes('glm')) return 'zhipu'
|
|
229
|
-
if (id.includes('kimi')) return 'moonshot'
|
|
230
|
-
if (id.includes('qwen')) return 'qwen'
|
|
231
|
-
if (id.includes('deepseek')) return 'deepseek-official'
|
|
232
|
-
return 'unknown'
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** OpenRouter public model prices; used only for provider=openrouter. */
|
|
199
|
+
/** OpenRouter public prices; only consulted for providers declared as USD. */
|
|
236
200
|
export async function loadRouterPricing() {
|
|
237
201
|
try {
|
|
238
202
|
const raw = await fetchJson(ROUTER)
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const input = Number(prices?.prompt)
|
|
243
|
-
const output = Number(prices?.completion)
|
|
244
|
-
if (!Number.isFinite(input) || !Number.isFinite(output)) continue
|
|
245
|
-
models.push({
|
|
246
|
-
provider: 'openrouter',
|
|
247
|
-
model: normalizeModel(item.id),
|
|
248
|
-
displayName: String(item.name ?? item.id),
|
|
249
|
-
family: String(item.id).split('/')[0] ?? '',
|
|
250
|
-
releasedAt: '',
|
|
251
|
-
currency: 'USD',
|
|
252
|
-
aliases: [],
|
|
253
|
-
prices: {
|
|
254
|
-
input: input * 1_000_000,
|
|
255
|
-
cacheRead: Number(prices?.input_cache_read ?? 0) * 1_000_000,
|
|
256
|
-
cacheWrite: Number(prices?.input_cache_write ?? 0) * 1_000_000,
|
|
257
|
-
output: output * 1_000_000,
|
|
258
|
-
},
|
|
259
|
-
peak: null,
|
|
260
|
-
offPeak: null,
|
|
261
|
-
})
|
|
262
|
-
}
|
|
263
|
-
if (models.length > 0) {
|
|
264
|
-
routerPrices = { models }
|
|
203
|
+
const parsed = loadCatalog({ models: routerModels(raw), currency: 'USD' }, 'router')
|
|
204
|
+
if (parsed.entries.length > 0) {
|
|
205
|
+
routerCatalog = parsed
|
|
265
206
|
return true
|
|
266
207
|
}
|
|
267
208
|
} catch {
|
|
268
|
-
// keep
|
|
209
|
+
// keep whatever we had
|
|
269
210
|
}
|
|
270
211
|
return false
|
|
271
212
|
}
|
|
272
213
|
|
|
273
214
|
export function resetPricingForTests() {
|
|
274
|
-
catalog =
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
routerPrices = null
|
|
215
|
+
catalog = loadCatalog(BUNDLED, 'bundled')
|
|
216
|
+
routerCatalog = { entries: [], currency: 'USD', source: 'router', updatedAt: null }
|
|
217
|
+
catalogUrl = null
|
|
278
218
|
communityCache = { at: 0, value: null }
|
|
219
|
+
overrides = new Map()
|
|
220
|
+
currencyOf = (providerId) => providerCurrency(providerId)
|
|
279
221
|
}
|
package/src/providers.js
CHANGED
|
@@ -24,10 +24,12 @@ const ALIASES = {
|
|
|
24
24
|
'opencode-go': 'opencode-go',
|
|
25
25
|
commandcode: 'command-code',
|
|
26
26
|
'command-code': 'command-code',
|
|
27
|
+
'ollama-cloud': 'ollama',
|
|
28
|
+
ollama: 'ollama',
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/** Display order is also the P3 pricing-link order. */
|
|
30
|
-
export const PROVIDER_ORDER = ['deepseek-official', 'zhipu', 'moonshot', 'openrouter', 'qwen', 'opencode-go', 'command-code']
|
|
32
|
+
export const PROVIDER_ORDER = ['deepseek-official', 'zhipu', 'moonshot', 'openrouter', 'qwen', 'opencode-go', 'command-code', 'ollama']
|
|
31
33
|
|
|
32
34
|
const PROVIDERS = {
|
|
33
35
|
'deepseek-official': {
|
|
@@ -116,6 +118,16 @@ const PROVIDERS = {
|
|
|
116
118
|
modelLabels: {},
|
|
117
119
|
pricingUrl: 'https://commandcode.ai/',
|
|
118
120
|
},
|
|
121
|
+
// 额度 + 逐笔按量(USD):不补默认模型行(模型名不固定,补 0 值行反而误导)
|
|
122
|
+
ollama: {
|
|
123
|
+
id: 'ollama',
|
|
124
|
+
label: 'Ollama',
|
|
125
|
+
currency: 'USD',
|
|
126
|
+
subscription: false,
|
|
127
|
+
prefixes: [],
|
|
128
|
+
modelLabels: {},
|
|
129
|
+
pricingUrl: 'https://ollama.com/pricing',
|
|
130
|
+
},
|
|
119
131
|
}
|
|
120
132
|
|
|
121
133
|
export function normalizeProvider(raw) {
|