opencode-statboard 2.10.19 → 2.10.22
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/attr-worker.js +4 -2
- package/package.json +1 -1
- package/pricing.js +71 -0
- package/public/index.html +6 -0
- package/run-check.bat +3 -0
- package/server.js +26 -9
package/attr-worker.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const { parentPort, workerData } = require('worker_threads');
|
|
2
|
+
if (workerData && workerData.dbPath && !process.env.OPENCODE_DATA) {
|
|
3
|
+
process.env.OPENCODE_DATA = workerData.dbPath;
|
|
4
|
+
}
|
|
2
5
|
const srv = require('./server.js');
|
|
3
6
|
|
|
4
|
-
// I/O throttling parameters (from env, with defaults)
|
|
5
7
|
const ITHROTTLE_BATCH = parseInt(process.env.OPENCODE_ITHROTTLE_BATCH || '1000', 10);
|
|
6
8
|
const ITHROTTLE_DELAY_MS = parseInt(process.env.OPENCODE_ITHROTTLE_DELAY_MS || '5', 10);
|
|
7
9
|
|
|
@@ -25,4 +27,4 @@ function throttle(iteration) {
|
|
|
25
27
|
});
|
|
26
28
|
})().catch((e) => {
|
|
27
29
|
parentPort.postMessage({ ok: false, error: (e && e.message) || String(e) });
|
|
28
|
-
});
|
|
30
|
+
});
|
package/package.json
CHANGED
package/pricing.js
CHANGED
|
@@ -130,6 +130,73 @@ function entry(data, provider, model) {
|
|
|
130
130
|
return nonzero(c) ? { input: c.input || 0, output: c.output || 0, cacheRead: c.cache_read || 0 } : null;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
// ---- live OpenRouter list-price fallback -------------------------------
|
|
134
|
+
// The local opencode catalog (models.json) is a snapshot and misses plenty of
|
|
135
|
+
// paid listings (OpenRouter alone lists 477 models). When nothing local matches
|
|
136
|
+
// we consult OpenRouter's public model list, cached on disk for 12h and
|
|
137
|
+
// refreshed in the background so no request ever waits on the network.
|
|
138
|
+
// Rates are USD per token there, so ×1e6 to match the local catalog's unit.
|
|
139
|
+
const OR_URL = 'https://openrouter.ai/api/v1/models';
|
|
140
|
+
const OR_TTL = 12 * 60 * 60 * 1000;
|
|
141
|
+
const OR_DIR = path.join(process.env.USERPROFILE || process.env.HOME || process.cwd(), '.cache', 'opencode-statboard');
|
|
142
|
+
const OR_FILE = path.join(OR_DIR, 'openrouter-models.json');
|
|
143
|
+
let orCache = { at: 0, models: null, refreshing: false };
|
|
144
|
+
|
|
145
|
+
function orLoad() {
|
|
146
|
+
if (orCache.models) return orCache;
|
|
147
|
+
try {
|
|
148
|
+
const o = JSON.parse(fs.readFileSync(OR_FILE, 'utf8'));
|
|
149
|
+
if (o && o.models && o.fetchedAt) orCache = { at: o.fetchedAt, models: o.models, refreshing: false };
|
|
150
|
+
} catch (_) { /* first run: no cache yet */ }
|
|
151
|
+
return orCache;
|
|
152
|
+
}
|
|
153
|
+
function orRefresh() {
|
|
154
|
+
if (orCache.refreshing) return;
|
|
155
|
+
orCache.refreshing = true;
|
|
156
|
+
const run = async () => {
|
|
157
|
+
try {
|
|
158
|
+
const res = await fetch(OR_URL, { headers: { 'user-agent': 'opencode-statboard' } });
|
|
159
|
+
if (!res.ok) return;
|
|
160
|
+
const j = await res.json();
|
|
161
|
+
const models = {};
|
|
162
|
+
for (const m of (j.data || [])) {
|
|
163
|
+
const p = m.pricing || {};
|
|
164
|
+
const input = Number(p.prompt || 0) * 1e6;
|
|
165
|
+
const output = Number(p.completion || 0) * 1e6;
|
|
166
|
+
const cacheRead = Number(p.input_cache_read || 0) * 1e6;
|
|
167
|
+
if (!(input > 0) && !(output > 0)) continue; // free listings carry no paid basis
|
|
168
|
+
models[m.id] = { input, output, cacheRead };
|
|
169
|
+
}
|
|
170
|
+
orCache = { at: Date.now(), models, refreshing: false };
|
|
171
|
+
try { fs.mkdirSync(OR_DIR, { recursive: true }); fs.writeFileSync(OR_FILE, JSON.stringify({ fetchedAt: orCache.at, models })); } catch (_) {}
|
|
172
|
+
} catch (_) { orCache.refreshing = false; }
|
|
173
|
+
};
|
|
174
|
+
run();
|
|
175
|
+
}
|
|
176
|
+
function orEntry(provider, model) {
|
|
177
|
+
const c = orLoad();
|
|
178
|
+
if (!c.models) {
|
|
179
|
+
if (Date.now() - c.at > OR_TTL) orRefresh();
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
if (Date.now() - c.at > OR_TTL) orRefresh();
|
|
183
|
+
const bare = stripFree(model);
|
|
184
|
+
const cands = [];
|
|
185
|
+
const push = (s) => { if (s && !cands.includes(s)) cands.push(s); };
|
|
186
|
+
push(`${provider}/${model}`); push(`${provider}/${bare}`);
|
|
187
|
+
push(model); push(bare);
|
|
188
|
+
if (model.includes('/')) { const rest = model.split('/').slice(1).join('/'); push(rest); push(stripFree(rest)); }
|
|
189
|
+
for (const id of cands) if (c.models[id]) return { ...c.models[id], id };
|
|
190
|
+
// Last resort: unique match on the final path segment (deepseek-ai/deepseek-v4.1-flash
|
|
191
|
+
// → deepseek/deepseek-v4.1-flash). Only when exactly one paid listing matches.
|
|
192
|
+
const last = String(bare).split('/').pop();
|
|
193
|
+
if (last && last !== 'unknown') {
|
|
194
|
+
const hits = Object.keys(c.models).filter((id) => id.split('/').pop() === last);
|
|
195
|
+
if (hits.length === 1) return { ...c.models[hits[0]], id: hits[0] };
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
133
200
|
// Map a usage model (provider/model as recorded in the DB) to paid rates.
|
|
134
201
|
function priceFor(provider, model) {
|
|
135
202
|
const cat = loadCatalog();
|
|
@@ -203,6 +270,10 @@ function priceFor(provider, model) {
|
|
|
203
270
|
if (r2) return r2;
|
|
204
271
|
}
|
|
205
272
|
|
|
273
|
+
// Live OpenRouter list price (paid twin of a free/unknown model).
|
|
274
|
+
const or = orEntry(provider, model);
|
|
275
|
+
if (or) return { input: or.input, output: or.output, cacheRead: or.cacheRead, source: `openrouter:${or.id}`, kind: 'openrouter' };
|
|
276
|
+
|
|
206
277
|
return null;
|
|
207
278
|
}
|
|
208
279
|
|
package/public/index.html
CHANGED
|
@@ -150,6 +150,7 @@
|
|
|
150
150
|
.badge.exact { color: #3fb950; border-color: rgba(63,185,80,.4); }
|
|
151
151
|
.badge.sibling { color: #58a6ff; border-color: rgba(88,166,255,.4); }
|
|
152
152
|
.badge.proxy { color: #d29922; border-color: rgba(210,153,34,.4); }
|
|
153
|
+
.badge.openrouter { color: #a371f7; border-color: rgba(163,113,247,.4); }
|
|
153
154
|
.badge.partial { color: #f778ba; border-color: rgba(247,120,186,.4); }
|
|
154
155
|
.badge.unpriced { color: #f85149; border-color: rgba(248,81,73,.4); }
|
|
155
156
|
.empty { color: var(--muted); font-size: 13px; padding: 12px 0; }
|
|
@@ -342,6 +343,7 @@
|
|
|
342
343
|
<span><span class="badge exact" data-i18n="kindExact">exact</span> <span data-i18n="kindExactTip">own provider + own list price</span></span>
|
|
343
344
|
<span><span class="badge sibling" data-i18n="kindSibling">sibling</span> <span data-i18n="kindSiblingTip">same provider, -free variant stripped</span></span>
|
|
344
345
|
<span><span class="badge proxy" data-i18n="kindProxy">proxy</span> <span data-i18n="kindProxyTip">borrowed price from another provider (estimate)</span></span>
|
|
346
|
+
<span><span class="badge openrouter" data-i18n="kindOpenrouter">openrouter</span> <span data-i18n="kindOpenrouterTip">live OpenRouter list price for the paid twin</span></span>
|
|
345
347
|
<span><span class="badge partial" data-i18n="kindPartial">partial</span> <span data-i18n="kindPartialTip">part priced, part not in catalog</span></span>
|
|
346
348
|
<span><span class="badge unpriced" data-i18n="kindUnpriced">unpriced</span> <span data-i18n="kindUnpricedTip">not in catalog — market/saved unknown</span></span>
|
|
347
349
|
</div>
|
|
@@ -398,6 +400,8 @@ const STRINGS = {
|
|
|
398
400
|
kindExactTip: 'own provider + own list price',
|
|
399
401
|
kindSiblingTip: 'same provider, -free variant stripped',
|
|
400
402
|
kindProxyTip: 'borrowed price from another provider (estimate)',
|
|
403
|
+
kindOpenrouter: 'openrouter',
|
|
404
|
+
kindOpenrouterTip: 'live OpenRouter list price for the paid twin',
|
|
401
405
|
kindPartialTip: 'part of the usage priced, rest not in catalog',
|
|
402
406
|
kindUnpricedTip: 'not in catalog — market/saved unknown',
|
|
403
407
|
legacyAgent: 'legacy',
|
|
@@ -444,6 +448,8 @@ const STRINGS = {
|
|
|
444
448
|
kindExactTip: '同供應商同名牌價',
|
|
445
449
|
kindSiblingTip: '同供應商,去掉 -free 變體',
|
|
446
450
|
kindProxyTip: '借用其他供應商牌價(代估)',
|
|
451
|
+
kindOpenrouter: 'openrouter',
|
|
452
|
+
kindOpenrouterTip: 'OpenRouter 即時牌價(同級付費版)',
|
|
447
453
|
kindPartialTip: '部分用量有牌價,其餘查無',
|
|
448
454
|
kindUnpricedTip: '目錄查無 — 市價/節省未知',
|
|
449
455
|
legacyAgent: '舊版',
|
package/run-check.bat
ADDED
package/server.js
CHANGED
|
@@ -294,9 +294,10 @@ function splitModelName(name) {
|
|
|
294
294
|
function marketOf(tok, provider, model) {
|
|
295
295
|
const pr = pricing.priceFor(provider, model);
|
|
296
296
|
if (!pr) return { market: null, saved: null, pricedAs: null, priced: false, kind: 'unpriced' };
|
|
297
|
-
const
|
|
297
|
+
const marketRaw = pricing.marketOf(tok, pr);
|
|
298
298
|
const r4m = (v) => Math.round(v * 10000) / 10000;
|
|
299
|
-
|
|
299
|
+
// Calculate saved from unrounded market to avoid double-rounding error.
|
|
300
|
+
return { market: r4m(marketRaw), saved: r4m(marketRaw - (tok.cost || 0)), pricedAs: pr.source, priced: true, kind: pr.kind };
|
|
300
301
|
}
|
|
301
302
|
|
|
302
303
|
// Hybrid usage attribution over [startMs, endMs).
|
|
@@ -740,7 +741,20 @@ function getAttrAll() {
|
|
|
740
741
|
function getAttrPeriod(startMs) {
|
|
741
742
|
if (!attrAllCache) refreshAttrBundle();
|
|
742
743
|
if (!attrAllCache) return EMPTY_ATTR;
|
|
743
|
-
|
|
744
|
+
const periods = attrAllCache.a.periods;
|
|
745
|
+
const hit = periods.get(startMs);
|
|
746
|
+
if (hit) return hit;
|
|
747
|
+
// Key miss must NEVER fall back to the all-time buckets: that silently
|
|
748
|
+
// rendered lifetime tokens inside a Daily/Weekly card (the "7 days shows
|
|
749
|
+
// all-time models" bug). Pick the deepest slice that starts at or before
|
|
750
|
+
// the requested boundary; only a boundary older than every slice (or a
|
|
751
|
+
// bundle with no slices) legitimately means "all time".
|
|
752
|
+
let best = null;
|
|
753
|
+
for (const [k, sl] of periods) {
|
|
754
|
+
if (k === 0) continue;
|
|
755
|
+
if (k <= startMs && (!best || k > best.start)) best = sl;
|
|
756
|
+
}
|
|
757
|
+
return best || attrAllCache.a;
|
|
744
758
|
}
|
|
745
759
|
// Kick a fresh scan. The scan runs in a WORKER THREAD because it is
|
|
746
760
|
// synchronous better-sqlite3 + JSON work: inline it froze the event loop for
|
|
@@ -803,7 +817,7 @@ function spawnAttrWorker(now, rev) {
|
|
|
803
817
|
}
|
|
804
818
|
let w;
|
|
805
819
|
try {
|
|
806
|
-
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0, dbRevision: rev } });
|
|
820
|
+
w = new Worker(ATTR_WORKER, { workerData: { now, toolsStartMs: 0, dbRevision: rev, dbPath: process.env.OPENCODE_DATA || '' } });
|
|
807
821
|
} catch (e) {
|
|
808
822
|
// Worker unavailable (very old Node / restricted env): build inline.
|
|
809
823
|
console.error('attr worker spawn failed, building inline:', e.message);
|
|
@@ -966,6 +980,7 @@ function getStatsFromDb(days) {
|
|
|
966
980
|
if (info.ok) {
|
|
967
981
|
priceAsOf = new Date(info.mtimeMs).toISOString();
|
|
968
982
|
marketTotal = 0;
|
|
983
|
+
savedTotal = 0;
|
|
969
984
|
for (const [name, m] of Object.entries(attr.perModel)) {
|
|
970
985
|
const { provider, model } = splitModelName(name);
|
|
971
986
|
const tok = { input: m.input, output: m.output, reasoning: m.reasoning, cacheRead: m.cacheRead, cost: m.cost };
|
|
@@ -976,10 +991,11 @@ function getStatsFromDb(days) {
|
|
|
976
991
|
continue;
|
|
977
992
|
}
|
|
978
993
|
marketTotal += mk.market;
|
|
979
|
-
|
|
994
|
+
savedTotal += mk.saved;
|
|
995
|
+
if (mk.kind === 'proxy' || mk.kind === 'openrouter') proxyMarket += mk.market;
|
|
980
996
|
}
|
|
981
997
|
marketTotal = Math.round(marketTotal * 10000) / 10000;
|
|
982
|
-
savedTotal = Math.round(
|
|
998
|
+
savedTotal = Math.round(savedTotal * 10000) / 10000;
|
|
983
999
|
proxyMarket = Math.round(proxyMarket * 10000) / 10000;
|
|
984
1000
|
}
|
|
985
1001
|
}
|
|
@@ -1142,7 +1158,7 @@ function periodStartMs(days, now) {
|
|
|
1142
1158
|
}
|
|
1143
1159
|
|
|
1144
1160
|
function buildReportFromDb(days) {
|
|
1145
|
-
const now = Date.now();
|
|
1161
|
+
const now = attrAllCache ? attrAllCache.at : Date.now(); // use bundle time for consistency
|
|
1146
1162
|
const startMs = periodStartMs(days, now);
|
|
1147
1163
|
const label = days === -1 ? 'All' : days <= 1 ? 'Daily' : days <= 7 ? 'Weekly' : 'Monthly';
|
|
1148
1164
|
|
|
@@ -1318,12 +1334,13 @@ function buildReportFromDb(days) {
|
|
|
1318
1334
|
const sumMkt = (rows) => rows.reduce((s, r) => s + (r.market || 0), 0);
|
|
1319
1335
|
const sumSaved = (rows) => rows.reduce((s, r) => s + (r.saved || 0), 0);
|
|
1320
1336
|
statsR.cost.marketTotal = Math.round(sumMkt(pProviderRows) * 10000) / 10000;
|
|
1337
|
+
// savedTotal = sum of per-model saved so it always matches the model table.
|
|
1321
1338
|
statsR.cost.savedTotal = Math.round(sumSaved(pProviderRows) * 10000) / 10000;
|
|
1322
1339
|
statsR.cost.savedPct = statsR.cost.marketTotal > 0 ? Math.round((statsR.cost.savedTotal / statsR.cost.marketTotal) * 1000) / 10 : null;
|
|
1323
1340
|
statsR.cost.priceAsOf = pricing.catalogInfo().ok ? new Date(pricing.catalogInfo().mtimeMs).toISOString() : null;
|
|
1324
1341
|
statsR.cost.unpricedRows = pProviderRows.filter((r) => !r.priced).length;
|
|
1325
1342
|
statsR.cost.unpricedTokens = pProviderRows.filter((r) => !r.priced).reduce((s, r) => s + r.tok_in + r.tok_out + r.tok_reasoning + r.cache_read, 0);
|
|
1326
|
-
const proxyMkt = pProviderRows.filter((r) => r.kind === 'proxy').reduce((s, r) => s + (r.market || 0), 0);
|
|
1343
|
+
const proxyMkt = pProviderRows.filter((r) => r.kind === 'proxy' || r.kind === 'openrouter').reduce((s, r) => s + (r.market || 0), 0);
|
|
1327
1344
|
statsR.cost.proxyShare = statsR.cost.marketTotal > 0 ? Math.round((proxyMkt / statsR.cost.marketTotal) * 1000) / 10 : null;
|
|
1328
1345
|
|
|
1329
1346
|
return {
|
|
@@ -1334,7 +1351,7 @@ function buildReportFromDb(days) {
|
|
|
1334
1351
|
providers: pProviderRows,
|
|
1335
1352
|
sessions: pSessionRows,
|
|
1336
1353
|
priceAsOf: statsR.cost.priceAsOf,
|
|
1337
|
-
generatedAt:
|
|
1354
|
+
generatedAt: aNow, // bundle build time (consistent with period bucket boundaries)
|
|
1338
1355
|
};
|
|
1339
1356
|
}
|
|
1340
1357
|
|