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.
@@ -16,7 +16,7 @@ permissions:
16
16
 
17
17
  jobs:
18
18
  publish:
19
- runs-on: ubuntu-latest
19
+ runs-on: ubuntu-24.04
20
20
  steps:
21
21
  - uses: actions/checkout@v6
22
22
  - uses: actions/setup-node@v6
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. **Period speed/active/wall (report)** — `buildReportFromDb` runs one extra `GROUP BY session_id` over the merged message view (scoped to `[periodStart, attrSnapshot)`), mapped onto the same session-meta buckets: providers/agents gain period `speed`; agents gain `wallMs` (Σ full session spans — same scope as their token columns) and `activeMs` (Σ in-period message durations); `report.sessions` lists the top 20 sessions by wall time with both durations. Wall includes idle; active counts only model-running time.
45
- 5. **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; `pricedAs` records the entry used per row. Overview and report both use session-meta grouping so the two panels agree (message-level splits would price model-switched sessions differently).
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.3.1",
3
+ "version": "2.5.0",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
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
- // { provider: { models: { name: { cost: { input, output, cache_read } } } } }
5
- // in USD per 1M tokens. Free variants (cost 0) are mapped to a paid basis:
6
- // 1. exact provider/model with nonzero cost
7
- // 2. strip -free / -contributor-free suffix, same provider, nonzero cost
8
- // 3. same base name under provider 'opencode', nonzero cost
9
- // 4. same base name under any provider (sorted, first nonzero)
10
- // 5. proxy fallback: opencode/deepseek-v4-flash rates (marked "proxy:")
11
- // Reasoning tokens have no catalog rate and are billed at the output rate.
12
- // cache_write is always 0 in the DB and contributes nothing.
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 ratesOf(data, provider, model) {
45
- const cost = data[provider] && data[provider].models && data[provider].models[model] && data[provider].models[model].cost;
46
- if (!nonzero(cost)) return null;
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
- // Map a usage model (provider/model as seen in the DB) to paid rates.
51
- // Returns { input, output, cacheRead, source, exact } or null when no catalog.
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
- let r = ratesOf(data, provider, model);
59
- if (r) return { ...r, exact: true };
60
- if (model === 'unknown') {
61
- const fb = ratesOf(data, 'opencode', 'deepseek-v4-flash');
62
- return fb ? { ...fb, source: 'proxy:opencode/deepseek-v4-flash', exact: false } : null;
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 base = String(model).replace(/-contributor-free$/, '').replace(/-free$/, '');
65
- const cands = [];
66
- if (base !== model) cands.push([provider, base]);
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
- for (const [p, m] of cands) {
72
- r = ratesOf(data, p, m);
73
- if (r) return { ...r, exact: false };
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
- const fb = ratesOf(data, 'opencode', 'deepseek-v4-flash');
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 };