opencode-statboard 2.3.1 → 2.5.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/.github/workflows/publish.yml +1 -1
- package/AGENTS.md +4 -2
- package/package.json +1 -1
- package/pricing.js +67 -33
- package/public/index.html +673 -456
- package/server.js +254 -108
package/AGENTS.md
CHANGED
|
@@ -41,8 +41,10 @@ Token/cost attribution uses a hybrid model:
|
|
|
41
41
|
1. **Message-level** — Each assistant message's `tokens` and `cost` from `message.data` JSON are attributed to its `time_created` local day
|
|
42
42
|
2. **Session residual** — `session.tokens_* - sum(message.tokens_*)` is spread across the session's full local-day span, weighted by per-day message volume
|
|
43
43
|
3. **Speed (message-level only)** — Per-model `speed` (output tok/s) = Σ completed-assistant-message `tokens.output` ÷ Σ(`time.completed - time.created`). Session residuals carry no duration and are excluded; models without usable durations report `null` (shown as —). Tracked in the same message scan, no extra query.
|
|
44
|
-
4. **
|
|
45
|
-
5. **
|
|
44
|
+
4. **Per-message authority** — When a session's message sums exceed its session-table counters (V2 migration artifact: 455 sessions were recomputed from surviving messages and dropped pruned history; one session reports 52M input where the table says 7.3M), the measured message values win. Overview therefore reads the attribution totals, not `SUM(session.tokens_*)`, so Overview and Report(All) cannot disagree.
|
|
45
|
+
5. **Period scope (report)** — `attributeUsage(start, end, periodStartMs)` slices EVERY total into `*Period` buckets alongside the all-time ones (one scan serves both views; `getAttrAll()` passes `periodStartMs = 0`). Report providers/agents/sessions are built from those period buckets — message tokens under the MESSAGE's model/agent, residual under the SESSION's — so every table column sums exactly to the summary card. `savedTotal` is Σ provider `saved`, never `lifetime market − period cost`.
|
|
46
|
+
6. **Speed / active / wall** — `speed` (output tok/s) and `activeMs` come from in-period message durations; `wallMs` is the per-session span **on the message clock** (`MIN(time_created)`→`MAX(time.completed)`), NOT `session.time_updated − time_created`: subagent/fork sessions are written with both timestamps in the same millisecond, so session-table spans report ~0 wall for hours of real work. Wall includes idle; active is model-running time only, so `wall >= active` always holds (asserted in `check-report-verify.js`).
|
|
47
|
+
7. **Savings estimate (pricing.js, shared with checks)** — Market value at paid rates from the local opencode catalog (`~/.cache/opencode/models.json`, USD/1M). Free models map to a paid basis in order: exact provider/model → strip `-free`/`-contributor-free`, same provider → same base under `opencode` → same base anywhere (sorted) → `proxy:opencode/deepseek-v4-flash` fallback. Reasoning bills at the output rate (verified: `tokens.output` does NOT include reasoning — 8,764 messages have reasoning > output). Pricing is per MESSAGE model, so real paid usage is no longer filed under the session's free-model label. `pricedAs` records the entry used per row.
|
|
46
48
|
|
|
47
49
|
This ensures multi-day sessions (e.g. a session created Monday, updated Thursday) don't dump all tokens onto Thursday.
|
|
48
50
|
|
package/package.json
CHANGED
package/pricing.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
// Shared market-price engine for the "how much did I save" feature.
|
|
2
2
|
// Pricing comes from the user's local opencode models catalog
|
|
3
|
-
// (~/.cache/opencode/models.json):
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
3
|
+
// (~/.cache/opencode/models.json): { provider: { models: { name: { cost: { input,
|
|
4
|
+
// output, cache_read } } } } } in USD per 1M tokens.
|
|
5
|
+
//
|
|
6
|
+
// A free variant is mapped to a paid basis, in order:
|
|
7
|
+
// 1. exact provider/model with a nonzero cost -> kind 'exact'
|
|
8
|
+
// 2. same provider, free suffix stripped (:free / -free /
|
|
9
|
+
// -contributor-free) or duplicated provider prefix -> kind 'sibling'
|
|
10
|
+
// 3. same base name under provider 'opencode' -> kind 'proxy'
|
|
11
|
+
// 4. same base name anywhere else (cheapest nonzero) -> kind 'proxy'
|
|
12
|
+
// 5. nothing in the catalog -> null (NOT priced;
|
|
13
|
+
// inventing a number here is how a $0 model turned into $212 of fake
|
|
14
|
+
// savings). Callers must show '—' and can disclose the unpriced rows.
|
|
15
|
+
//
|
|
16
|
+
// Reasoning has no catalog rate and bills at the output rate (verified:
|
|
17
|
+
// tokens.output does not include reasoning — 8,764 messages have
|
|
18
|
+
// reasoning > output). cache_write is always 0 in the DB.
|
|
13
19
|
const fs = require('fs');
|
|
14
20
|
const path = require('path');
|
|
15
21
|
|
|
@@ -41,39 +47,67 @@ function nonzero(cost) {
|
|
|
41
47
|
return (cost.input || 0) > 0 || (cost.output || 0) > 0 || (cost.cache_read || 0) > 0;
|
|
42
48
|
}
|
|
43
49
|
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
return { input: cost.input || 0, output: cost.output || 0, cacheRead: cost.cache_read || 0, source: `${provider}/${model}` };
|
|
50
|
+
function entry(data, provider, model) {
|
|
51
|
+
const c = data[provider] && data[provider].models && data[provider].models[model] && data[provider].models[model].cost;
|
|
52
|
+
return nonzero(c) ? { input: c.input || 0, output: c.output || 0, cacheRead: c.cache_read || 0 } : null;
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
const stripFree = (m) => String(m).replace(/-contributor-free$/, '').replace(/-free$/, '').replace(/:free$/, '');
|
|
56
|
+
|
|
57
|
+
// Map a usage model (provider/model as recorded in the DB) to paid rates.
|
|
58
|
+
// Returns { input, output, cacheRead, source, kind } or null when the catalog
|
|
59
|
+
// has no comparable paid listing at all.
|
|
52
60
|
function priceFor(provider, model) {
|
|
53
61
|
const cat = loadCatalog();
|
|
54
62
|
if (!cat.data) return null;
|
|
55
63
|
const data = cat.data;
|
|
56
64
|
provider = provider || 'unknown';
|
|
57
65
|
model = model || 'unknown';
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
return
|
|
66
|
+
if (model === 'unknown') return null; // unattributed usage: never invent a rate
|
|
67
|
+
|
|
68
|
+
const mk = (p, m, kind) => {
|
|
69
|
+
const r = entry(data, p, m);
|
|
70
|
+
return r ? { ...r, source: `${p}/${m}`, kind } : null;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
let r = mk(provider, model, 'exact');
|
|
74
|
+
if (r) return r;
|
|
75
|
+
|
|
76
|
+
// Free suffixes and a provider prefix duplicated inside the model id
|
|
77
|
+
// (opencode records nvidia/nvidia/nemotron-... on some sessions). The plain
|
|
78
|
+
// base is always kept: a model that is free at ITS provider (cost 0) may
|
|
79
|
+
// still have a paid listing elsewhere (e.g. nvidia/z-ai/glm-5.3-flash).
|
|
80
|
+
const bases = [];
|
|
81
|
+
const deDup = model.startsWith(`${provider}/`) ? model.slice(provider.length + 1) : null;
|
|
82
|
+
for (const b of [stripFree(model), deDup && stripFree(deDup), deDup, model]) {
|
|
83
|
+
if (b && !bases.includes(b)) bases.push(b);
|
|
84
|
+
}
|
|
85
|
+
for (const b of bases) {
|
|
86
|
+
if (b === model) continue;
|
|
87
|
+
r = mk(provider, b, 'sibling');
|
|
88
|
+
if (r) return r;
|
|
63
89
|
}
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
cands.push(['opencode', base]);
|
|
68
|
-
for (const pid of Object.keys(data).sort()) {
|
|
69
|
-
if (pid !== provider && pid !== 'opencode') cands.push([pid, base]);
|
|
90
|
+
for (const b of bases) {
|
|
91
|
+
r = mk('opencode', b, 'proxy');
|
|
92
|
+
if (r) return r;
|
|
70
93
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
94
|
+
// Any provider, cheapest nonzero listing for the same base name. Cheapest
|
|
95
|
+
// keeps the estimate conservative (it understates savings, never inflates).
|
|
96
|
+
for (const b of bases) {
|
|
97
|
+
let best = null;
|
|
98
|
+
for (const pid of Object.keys(data).sort()) {
|
|
99
|
+
const e = entry(data, pid, b);
|
|
100
|
+
if (!e) continue;
|
|
101
|
+
const score = e.input + e.output + e.cacheRead;
|
|
102
|
+
if (!best || score < best.score) best = { ...e, score, pid };
|
|
103
|
+
}
|
|
104
|
+
if (best) {
|
|
105
|
+
const { score, pid, ...rates } = best;
|
|
106
|
+
void score;
|
|
107
|
+
return { ...rates, source: `${pid}/${b}`, kind: 'proxy' };
|
|
108
|
+
}
|
|
74
109
|
}
|
|
75
|
-
|
|
76
|
-
return fb ? { ...fb, source: 'proxy:opencode/deepseek-v4-flash', exact: false } : null;
|
|
110
|
+
return null;
|
|
77
111
|
}
|
|
78
112
|
|
|
79
113
|
// Market value (USD) of token usage at the given rates.
|
|
@@ -91,4 +125,4 @@ function catalogInfo() {
|
|
|
91
125
|
return cat.data ? { ok: true, path: cat.path, mtimeMs: cat.mtimeMs } : { ok: false };
|
|
92
126
|
}
|
|
93
127
|
|
|
94
|
-
module.exports = { priceFor, marketOf, catalogInfo };
|
|
128
|
+
module.exports = { priceFor, marketOf, catalogInfo, loadCatalog };
|