dsh-tg-bridge 1.20.4 → 1.20.5
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 +1 -1
- package/handle-message.mjs +3 -3
- package/package.json +1 -1
- package/prices.mjs +65 -105
package/README.md
CHANGED
|
@@ -109,7 +109,7 @@ Optional provider settings live in `config.json`:
|
|
|
109
109
|
| Key | Meaning |
|
|
110
110
|
|---|---|
|
|
111
111
|
| `providerKeys` | extra env var names to pass to a turn, read from `~/.dsh/.credentials.yaml`; default `[]` |
|
|
112
|
-
| `pricesUrl` | public
|
|
112
|
+
| `pricesUrl` | retired: `/prices` always reads OpenRouter's public list and labels the number as a reference for the model |
|
|
113
113
|
| `vision.patch` | opt-in overlay profile for photo turns; default `""` — the turn's own model reads images |
|
|
114
114
|
| `vision.enabled` | set `true` to run photo turns on `vision.patch` instead of the topic profile |
|
|
115
115
|
| `fallbackModels` | free-model fallbacks; default `[]` |
|
package/handle-message.mjs
CHANGED
|
@@ -242,12 +242,12 @@ ${t(lang, 'help.body')}`,
|
|
|
242
242
|
profile: profileOf(key),
|
|
243
243
|
prices: (filter) => {
|
|
244
244
|
const route = modelOf(profileOf(key));
|
|
245
|
-
//
|
|
246
|
-
//
|
|
245
|
+
// One source, always the same one: OpenRouter's public list. The card
|
|
246
|
+
// says in words that the number is a reference for the model, not the
|
|
247
|
+
// bill of the route this install runs on.
|
|
247
248
|
return renderPrices(filter, {
|
|
248
249
|
model: route.model,
|
|
249
250
|
provider: route.provider,
|
|
250
|
-
pageUrl: config.pricesUrl || '',
|
|
251
251
|
lang,
|
|
252
252
|
});
|
|
253
253
|
},
|
package/package.json
CHANGED
package/prices.mjs
CHANGED
|
@@ -1,87 +1,45 @@
|
|
|
1
1
|
// prices.mjs — model prices without a provider-specific plugin.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
3
|
+
// The one source is OpenRouter's public model list: it is JSON, it carries a
|
|
4
|
+
// price for every model it serves, and it is the market rate a person can check
|
|
5
|
+
// in one click. A private route (unorouter, wikivibe, any reseller) rarely
|
|
6
|
+
// publishes its own numbers, so a card built from the route's own page is empty
|
|
7
|
+
// or wrong. Every card therefore says whose price it shows: OpenRouter's rate
|
|
8
|
+
// for the same model, as a reference — not the invoice.
|
|
7
9
|
|
|
8
|
-
const
|
|
10
|
+
const OPENROUTER = 'https://openrouter.ai/api/v1/models';
|
|
9
11
|
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
|
10
12
|
|
|
11
|
-
/** "1.0M", "262K", "1,048,576" -> tokens. */
|
|
12
|
-
export function parseCount(raw) {
|
|
13
|
-
const text = String(raw ?? '').replace(/[,\s]/g, '').trim().toLowerCase();
|
|
14
|
-
const match = /^([0-9]*\.?[0-9]+)([kmbt])?$/.exec(text);
|
|
15
|
-
if (!match) return null;
|
|
16
|
-
const scale = { k: 1e3, m: 1e6, b: 1e9, t: 1e12 }[match[2]] ?? 1;
|
|
17
|
-
const value = Number(match[1]) * scale;
|
|
18
|
-
return Number.isFinite(value) && value > 0 ? value : null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** "$0.30" -> 0.3 (USD per 1M tokens). */
|
|
22
|
-
export function parsePrice(raw) {
|
|
23
|
-
const match = /\$\s*([0-9]+(?:\.[0-9]+)?)/.exec(String(raw ?? ''));
|
|
24
|
-
if (!match) return null;
|
|
25
|
-
const value = Number(match[1]);
|
|
26
|
-
return Number.isFinite(value) ? value : null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
13
|
/**
|
|
30
|
-
*
|
|
14
|
+
* OpenRouter's model list -> the shape the card wants.
|
|
31
15
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
16
|
+
* OpenRouter quotes dollars per token as strings ("0.00000015"); the card speaks
|
|
17
|
+
* per million, which is how every other price in the world is written. A model
|
|
18
|
+
* with no `pricing.prompt` is skipped: a missing price is not a free model.
|
|
35
19
|
*/
|
|
36
|
-
export function
|
|
20
|
+
export function parseOpenRouter(json) {
|
|
21
|
+
const list = Array.isArray(json?.data) ? json.data : Array.isArray(json) ? json : [];
|
|
22
|
+
const perMillion = (raw) => {
|
|
23
|
+
const value = Number(raw);
|
|
24
|
+
return Number.isFinite(value) && value >= 0 ? value * 1e6 : null;
|
|
25
|
+
};
|
|
37
26
|
const models = [];
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const text = row.replace(/<[^>]+>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim();
|
|
44
|
-
const prices = [...text.matchAll(/\$\s*([0-9]+(?:\.[0-9]+)?)/g)].map((m) => Number(m[1]));
|
|
45
|
-
if (prices.length === 0) continue;
|
|
46
|
-
// A model id: a long token with a dash or a slash, never an attribute name.
|
|
47
|
-
const candidates = [...text.matchAll(/\b([a-z][a-z0-9]*(?:[./\-][a-z0-9]+){1,4})\b/gi)]
|
|
48
|
-
.map((m) => m[1])
|
|
49
|
-
.filter((token) => token.length >= 6 && !junk.has(token.toLowerCase()) && !/^data-/i.test(token) && !cssLike.test(token) && /[a-z]/i.test(token) && /\d/.test(token) && /-|\./.test(token));
|
|
50
|
-
const id = candidates.sort((a, b) => b.length - a.length)[0];
|
|
51
|
-
if (!id) continue;
|
|
52
|
-
// Context must be labelled; a bare number is a price or an uptime figure.
|
|
53
|
-
const contextMatch = /(\d+(?:[.,]\d+)?)\s*([KMB])\b\s*(?:tokens|токен|context|контекст|ctx)?/i.exec(text);
|
|
54
|
-
const parsedContext = contextMatch ? parseCount(`${contextMatch[1]}${contextMatch[2]}`) : null;
|
|
55
|
-
// A context window above 4M is a mis-parse of some other number on the row.
|
|
56
|
-
const context = parsedContext && parsedContext <= 4000000 ? parsedContext : null;
|
|
27
|
+
for (const entry of list) {
|
|
28
|
+
const id = String(entry?.id ?? '').trim();
|
|
29
|
+
const inputPrice = perMillion(entry?.pricing?.prompt);
|
|
30
|
+
if (!id || inputPrice === null) continue;
|
|
31
|
+
const outputPrice = perMillion(entry?.pricing?.completion);
|
|
57
32
|
models.push({
|
|
58
|
-
id
|
|
59
|
-
inputPrice
|
|
60
|
-
outputPrice:
|
|
61
|
-
|
|
33
|
+
id,
|
|
34
|
+
inputPrice,
|
|
35
|
+
outputPrice: outputPrice === null ? inputPrice : outputPrice,
|
|
36
|
+
cacheReadPrice: perMillion(entry?.pricing?.input_cache_read),
|
|
37
|
+
context: Number(entry?.context_length) > 0 ? Number(entry.context_length) : null,
|
|
62
38
|
});
|
|
63
39
|
}
|
|
64
40
|
return models;
|
|
65
41
|
}
|
|
66
42
|
|
|
67
|
-
/** models.dev catalog -> the same shape. */
|
|
68
|
-
export function parseModelsDev(catalog) {
|
|
69
|
-
const models = [];
|
|
70
|
-
for (const [provider, entry] of Object.entries(catalog ?? {})) {
|
|
71
|
-
for (const [id, model] of Object.entries(entry?.models ?? {})) {
|
|
72
|
-
const cost = model.cost ?? {};
|
|
73
|
-
if (typeof cost.input !== 'number') continue;
|
|
74
|
-
models.push({
|
|
75
|
-
id: `${provider}/${id}`,
|
|
76
|
-
inputPrice: cost.input,
|
|
77
|
-
outputPrice: typeof cost.output === 'number' ? cost.output : cost.input,
|
|
78
|
-
context: model.limit?.context ?? null,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return models;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
43
|
async function fetchText(url, timeoutMs = 20000) {
|
|
86
44
|
const res = await fetch(url, {
|
|
87
45
|
headers: { 'user-agent': UA },
|
|
@@ -105,11 +63,13 @@ export const WORDS = {
|
|
|
105
63
|
cheap: 'Cheapest with context >= 128k:',
|
|
106
64
|
none: 'No models matched the filter.',
|
|
107
65
|
fail: 'Could not fetch prices',
|
|
108
|
-
notPublic: 'no
|
|
109
|
-
retail: '
|
|
66
|
+
notPublic: 'OpenRouter has no price for this model',
|
|
67
|
+
retail: 'Models it does price: {list}',
|
|
110
68
|
yourProvider: 'price published for your provider',
|
|
111
69
|
exact: 'same model on another gateway',
|
|
112
70
|
similar: 'a similar model, not the same id',
|
|
71
|
+
reference: 'reference: OpenRouter price for the same model, not your route',
|
|
72
|
+
perMillion: 'per 1M tokens',
|
|
113
73
|
},
|
|
114
74
|
ru: {
|
|
115
75
|
prices: 'Цены',
|
|
@@ -117,11 +77,13 @@ export const WORDS = {
|
|
|
117
77
|
cheap: 'Дешёвые модели с контекстом ≥128k:',
|
|
118
78
|
none: 'Ничего не нашлось по фильтру.',
|
|
119
79
|
fail: 'Не смог получить цены',
|
|
120
|
-
notPublic: '
|
|
121
|
-
retail: '
|
|
80
|
+
notPublic: 'у OpenRouter этой модели нет',
|
|
81
|
+
retail: 'Есть цена у: {list}',
|
|
122
82
|
yourProvider: 'цена опубликована для вашего провайдера',
|
|
123
83
|
exact: 'та же модель на другом шлюзе',
|
|
124
84
|
similar: 'похожая модель, не тот же id',
|
|
85
|
+
reference: 'ориентир: цена OpenRouter для той же модели, не вашего маршрута',
|
|
86
|
+
perMillion: 'за 1М токенов',
|
|
125
87
|
},
|
|
126
88
|
zh: {
|
|
127
89
|
prices: '价格',
|
|
@@ -129,11 +91,13 @@ export const WORDS = {
|
|
|
129
91
|
cheap: '上下文 ≥128k 的最便宜模型:',
|
|
130
92
|
none: '没有符合筛选条件的模型。',
|
|
131
93
|
fail: '无法获取价格',
|
|
132
|
-
notPublic: '
|
|
133
|
-
retail: '
|
|
94
|
+
notPublic: 'OpenRouter 没有这个模型的价格',
|
|
95
|
+
retail: '有价格:{list}',
|
|
134
96
|
yourProvider: '你的服务商公布的价格',
|
|
135
97
|
exact: '同一模型,另一家网关',
|
|
136
98
|
similar: '相似的模型,不是同一个 id',
|
|
99
|
+
reference: '参考:同一模型在 OpenRouter 的价格,不是你的线路',
|
|
100
|
+
perMillion: '每百万 token',
|
|
137
101
|
},
|
|
138
102
|
};
|
|
139
103
|
|
|
@@ -198,6 +162,10 @@ function claims(modelId, provider) {
|
|
|
198
162
|
* The two lines that say what the current model costs, and whose price it is.
|
|
199
163
|
* Kept apart from the fetch so the wording can be tested offline: the failure
|
|
200
164
|
* this replaces was a confident "$0.0000", and a wrong price is worse than none.
|
|
165
|
+
*
|
|
166
|
+
* Every number here is OpenRouter's, and the card says so. A private route
|
|
167
|
+
* publishes nothing, so its own number would be either missing or invented; the
|
|
168
|
+
* market rate for the same model is the honest reference, and it is checkable.
|
|
201
169
|
*/
|
|
202
170
|
export function describeCurrent(models, { model, provider } = {}, words) {
|
|
203
171
|
const name = provider ? `${provider}/${model}` : model;
|
|
@@ -215,60 +183,52 @@ export function describeCurrent(models, { model, provider } = {}, words) {
|
|
|
215
183
|
];
|
|
216
184
|
}
|
|
217
185
|
const price = current.inputPrice > 0
|
|
218
|
-
? `${usd(current.inputPrice)} / ${usd(current.outputPrice)}
|
|
186
|
+
? `${usd(current.inputPrice)} / ${usd(current.outputPrice)} ${words.perMillion}`
|
|
219
187
|
: words.notPublic;
|
|
188
|
+
// Whose price this is, on the same line as the id it came from: a reader must
|
|
189
|
+
// be able to check the number in one click, and must not think it is their
|
|
190
|
+
// own bill. A private route publishes nothing, so it never claims one.
|
|
220
191
|
const where = claims(current.id, provider)
|
|
221
|
-
? words.yourProvider
|
|
222
|
-
: `${current.id}
|
|
192
|
+
? `${current.id} — ${words.yourProvider}`
|
|
193
|
+
: `${current.id} — ${words.reference}`;
|
|
223
194
|
return [`${words.now}: ${name} — ${price}`, ` ${where}`];
|
|
224
195
|
}
|
|
225
196
|
|
|
226
197
|
/**
|
|
227
|
-
* Render `/prices [filter]
|
|
228
|
-
*
|
|
229
|
-
* is
|
|
230
|
-
*
|
|
198
|
+
* Render `/prices [filter]`.
|
|
199
|
+
*
|
|
200
|
+
* OpenRouter is the only source: one JSON list, a published price for every
|
|
201
|
+
* model it serves, and a page a person can check. A second catalog would only
|
|
202
|
+
* reintroduce the bug this replaces — two numbers for one model, and a guess
|
|
203
|
+
* about which is the reader's.
|
|
204
|
+
*
|
|
231
205
|
* @param {string} filter optional text to match model ids
|
|
232
|
-
* @param {{ model?: string, provider?: string,
|
|
206
|
+
* @param {{ model?: string, provider?: string, lang?: string }} options
|
|
233
207
|
*/
|
|
234
208
|
export async function renderPrices(filter = '', options = {}) {
|
|
235
209
|
const lang = WORDS[options.lang] ? options.lang : 'en';
|
|
236
210
|
const words = WORDS[lang];
|
|
237
|
-
const
|
|
238
|
-
const jobs = [fetchText(MODELS_DEV, 30000)];
|
|
239
|
-
if (pageUrl) jobs.unshift(fetchText(pageUrl));
|
|
240
|
-
const results = await Promise.allSettled(jobs);
|
|
241
|
-
const page = pageUrl ? results[0] : null;
|
|
242
|
-
const dev = pageUrl ? results[1] : results[0];
|
|
211
|
+
const [openrouter] = await Promise.allSettled([fetchText(OPENROUTER, 30000)]);
|
|
243
212
|
|
|
244
213
|
let models = [];
|
|
245
214
|
const sources = [];
|
|
246
|
-
if (
|
|
247
|
-
const parsed = parseModelsPage(page.value);
|
|
248
|
-
if (parsed.length > 0) {
|
|
249
|
-
models.push(...parsed);
|
|
250
|
-
try { sources.push(new URL(pageUrl).hostname.replace(/^www\./i, '')); } catch { sources.push('provider'); }
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
if (dev?.status === 'fulfilled') {
|
|
215
|
+
if (openrouter?.status === 'fulfilled') {
|
|
254
216
|
try {
|
|
255
|
-
const parsed =
|
|
217
|
+
const parsed = parseOpenRouter(JSON.parse(openrouter.value));
|
|
256
218
|
if (parsed.length > 0) {
|
|
257
219
|
models.push(...parsed);
|
|
258
|
-
sources.push('
|
|
220
|
+
sources.push('OpenRouter');
|
|
259
221
|
}
|
|
260
222
|
} catch {}
|
|
261
223
|
}
|
|
262
224
|
if (models.length === 0) {
|
|
263
|
-
const reason =
|
|
264
|
-
?
|
|
265
|
-
:
|
|
266
|
-
? page.reason?.message
|
|
267
|
-
: 'catalogs parsed to 0 models';
|
|
225
|
+
const reason = openrouter?.status === 'rejected'
|
|
226
|
+
? openrouter.reason?.message
|
|
227
|
+
: 'the OpenRouter list parsed to 0 models';
|
|
268
228
|
return `${words.fail}: ${reason}`;
|
|
269
229
|
}
|
|
270
230
|
|
|
271
|
-
//
|
|
231
|
+
// One catalog, so duplicate ids cannot disagree about a price.
|
|
272
232
|
const seen = new Set();
|
|
273
233
|
models = models.filter((model) => {
|
|
274
234
|
const id = model.id.toLowerCase();
|