opencode-statboard 2.4.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.4.0",
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 };