opencode-statboard 2.10.33 → 2.10.35

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.10.33",
3
+ "version": "2.10.35",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/public/index.html CHANGED
@@ -62,6 +62,7 @@
62
62
  .scrollx table { min-width: 1080px; }
63
63
  td.clip { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
64
64
  .track { background: #1a1f2a; border-radius: 6px; height: 14px; overflow: hidden; }
65
+ .track .fill { height: 100%; border-radius: 6px; transition: width 0.3s ease; }
65
66
  .track.share { min-width: 90px; height: 12px; }
66
67
  /* horizontal bar list (tools). The container must NOT be a .bar-row — each
67
68
  child is a grid item, so a grid container here collapses all rows into
@@ -423,6 +424,8 @@ const STRINGS = {
423
424
  statsFail: 'Failed to read opencode stats', connFail: 'Connection failed', reportFail: 'Failed to read usage report',
424
425
  langBtn: '繁中',
425
426
  backup: 'Backup DB', backingUp: 'Backing up…', backupFail: 'Backup failed',
427
+ unlimited: 'unlimited', freeRpd: 'free RPD', left: 'left', budgetValue: 'budget', freeTier: '[free tier]',
428
+ remaining: 'remaining', used: 'used', quotaReset: 'reset 00:00 UTC = 08:00 TW', cached: 'cached 5 min',
426
429
  },
427
430
  'zh-TW': {
428
431
  title: 'opencode 用量儀表板',
@@ -472,6 +475,8 @@ const STRINGS = {
472
475
  statsFail: 'opencode stats 讀取失敗', connFail: '連線失敗', reportFail: '用量報告讀取失敗',
473
476
  langBtn: 'EN',
474
477
  backup: '備份 DB', backingUp: '備份中…', backupFail: '備份失敗',
478
+ unlimited: '無限制', freeRpd: '免費 RPD', left: '剩', budgetValue: '預算剩', freeTier: '[免費層]',
479
+ remaining: '剩餘', used: '已用', quotaReset: '重置 00:00 UTC = 08:00 台灣', cached: '快取 5 分鐘',
475
480
  }
476
481
  };
477
482
 
@@ -761,30 +766,30 @@ function renderQuota(q) {
761
766
  const free = a.freeModelDaily || {};
762
767
  const rem = a.remainingUsd != null ? fmtUsd(a.remainingUsd) : '—';
763
768
  const used = fmtUsd(a.usedUsd || 0);
764
- const lim = a.limitUsd != null ? fmtUsd(a.limitUsd) : 'unlimited';
769
+ const lim = a.limitUsd != null ? fmtUsd(a.limitUsd) : t('unlimited');
765
770
  const pct = (a.limitUsd && a.limitUsd > 0) ? Math.round(((a.usedUsd || 0) / a.limitUsd) * 100) : null;
766
771
  const freePct = (free.limit && free.limit > 0) ? Math.round((free.used / free.limit) * 100) : 0;
767
772
  const freeLine = free.limit != null
768
- ? '<div style="margin-top:4px">free RPD ' + fmt(free.used) + '/' + fmt(free.limit) + ' (left ' + fmt(free.remaining) + ')</div>'
773
+ ? '<div style="margin-top:4px">' + t('freeRpd') + ' ' + fmt(free.used) + '/' + fmt(free.limit) + ' (' + t('left') + ' ' + fmt(free.remaining) + ')</div>'
769
774
  + '<div class="track" style="margin-top:2px"><div class="fill" style="width:' + freePct + '%;background:' + (freePct > 80 ? 'var(--out)' : 'var(--in)') + '"></div></div>'
770
775
  : '';
771
- const bud = a.budgetRemaining != null ? '<div style="margin-top:4px;color:var(--muted)">budget left ' + fmtUsd(a.budgetRemaining) + '</div>' : '';
776
+ const bud = a.budgetRemaining != null ? '<div style="margin-top:4px;color:var(--muted)">' + t('budgetValue') + ' ' + fmtUsd(a.budgetRemaining) + '</div>' : '';
772
777
  const barColor = pct != null && pct > 80 ? 'var(--out)' : pct != null && pct > 50 ? 'var(--cache)' : 'var(--in)';
773
778
  return '<div style="margin-bottom:12px;padding:12px;background:var(--bg);border:1px solid var(--border);border-radius:8px">'
774
779
  + '<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px">'
775
780
  + '<b style="color:var(--text)">' + esc(a.label) + '</b>'
776
- + '<span style="font-size:11px;color:var(--muted)">' + (a.isFreeTier ? '[free tier]' : '') + '</span>'
781
+ + '<span style="font-size:11px;color:var(--muted)">' + (a.isFreeTier ? t('freeTier') : '') + '</span>'
777
782
  + '</div>'
778
783
  + '<div style="display:flex;justify-content:space-between;font-size:12px;margin-bottom:4px">'
779
- + '<span>remaining <b>' + rem + '</b> / ' + lim + '</span>'
780
- + '<span style="color:var(--muted)">used ' + used + '</span>'
784
+ + '<span>' + t('remaining') + ' <b>' + rem + '</b> / ' + lim + '</span>'
785
+ + '<span style="color:var(--muted)">' + t('used') + ' ' + used + '</span>'
781
786
  + '</div>'
782
787
  + (pct != null ? '<div class="track"><div class="fill" style="width:' + pct + '%;background:' + barColor + '"></div></div>' : '')
783
788
  + freeLine
784
789
  + bud
785
790
  + '</div>';
786
791
  }).join('');
787
- body.innerHTML = rows + '<div style="color:var(--muted);font-size:10px;margin-top:8px;text-align:center">reset 00:00 UTC = 08:00 TW · cached 5 min</div>';
792
+ body.innerHTML = rows + '<div style="color:var(--muted);font-size:10px;margin-top:8px;text-align:center">' + t('quotaReset') + ' · ' + t('cached') + '</div>';
788
793
  }
789
794
  async function loadQuota() {
790
795
  try { const r = await fetch('/api/openrouter-quota?_=' + Date.now(), { cache: 'no-store' }); const j = await r.json(); renderQuota(j); } catch (e) { $('quota-body').textContent = 'load failed: ' + e.message; }
@@ -1069,6 +1074,7 @@ async function poll() {
1069
1074
  lastUpdatedText = t('lastUpdated') + ' ' + time + ' (' + t('dataAgo') + ' ' + ago + 's)';
1070
1075
  $('updated').textContent = lastUpdatedText;
1071
1076
  refreshCurrentReport(); // auto-refresh report panels
1077
+ loadQuota(); // keep quota/RPD fresh on a 24/7 dashboard (server caches 5 min, so this is cheap)
1072
1078
  return j;
1073
1079
  } catch (e) {
1074
1080
  $('updated').textContent = t('connFail') + ': ' + e.message;
package/server.js CHANGED
@@ -14,6 +14,21 @@ let Database;
14
14
  try { Database = require('better-sqlite3'); } catch (_) { Database = null; }
15
15
  const pricing = require('./pricing');
16
16
 
17
+ // The report/trend endpoints only ever have four real scopes (the four pills in
18
+ // the UI). `days` used to be taken straight from the query string, so
19
+ // `?days=abc` fell through `parseInt(...) || 0` to 0 (= Daily) and every distinct
20
+ // value minted its own reportCache/trendCache entry that nothing ever evicted
21
+ // (cleanup runs every CACHE_CLEANUP_MS = 6h by default). Normalize to the known
22
+ // set so an arbitrary query param cannot pick a scope or grow the cache.
23
+ const SCOPE_DAYS = [-1, 1, 7, 30];
24
+ const DEFAULT_SCOPE = 7;
25
+ // Upper bound on reportCache/trendCache entries (see cleanExpiredCache).
26
+ const CACHE_MAX_KEYS = 16;
27
+ function normalizeScope(days) {
28
+ const n = parseInt(days, 10);
29
+ return SCOPE_DAYS.includes(n) ? n : DEFAULT_SCOPE;
30
+ }
31
+
17
32
  const PORT = parseInt(process.env.OC_PORT || '4868', 10);
18
33
  const REFRESH_MS = 15000;
19
34
  const TREND_MS = 60 * 1000;
@@ -214,6 +229,14 @@ function cleanExpiredCache(cache, ttl) {
214
229
  delete cache[key];
215
230
  }
216
231
  }
232
+ // Hard cap as a backstop: expiry alone is not a bound if a caller keeps
233
+ // minting keys faster than the (6h by default) cleanup interval. Drop the
234
+ // oldest entries first.
235
+ const keys = Object.keys(cache);
236
+ if (keys.length > CACHE_MAX_KEYS) {
237
+ keys.sort((a, b) => (cache[a].generatedAt || 0) - (cache[b].generatedAt || 0));
238
+ for (const k of keys.slice(0, keys.length - CACHE_MAX_KEYS)) delete cache[k];
239
+ }
217
240
  }
218
241
 
219
242
  async function generateTrend(days) {
@@ -1427,7 +1450,7 @@ const server = http.createServer(async (req, res) => {
1427
1450
  };
1428
1451
 
1429
1452
  if (u === '/api/stats') {
1430
- const days = parseInt(url.searchParams.get('days') || '7', 10) || 7;
1453
+ const days = normalizeScope(url.searchParams.get('days') || DEFAULT_SCOPE);
1431
1454
  const r = refreshTrend(days);
1432
1455
  respond({
1433
1456
  ok: !!state.data,
@@ -1441,7 +1464,7 @@ const server = http.createServer(async (req, res) => {
1441
1464
  });
1442
1465
  } else if (u === '/api/report') {
1443
1466
  const daysParam = url.searchParams.get('days');
1444
- const days = daysParam !== null ? (parseInt(daysParam, 10) || 0) : 0;
1467
+ const days = daysParam !== null ? normalizeScope(daysParam) : 0;
1445
1468
  const r = refreshReport(days);
1446
1469
  respond({
1447
1470
  ok: true,
@@ -1576,12 +1599,15 @@ async function fetchOpenRouterQuota() {
1576
1599
  const j = await r.json();
1577
1600
  const d = j.data || {};
1578
1601
  const eb = d.effective_budget || {};
1602
+ // free_model_daily_requests is account-level (shared across all keys of the same account).
1603
+ // Only show RPD for the free tier key; paid keys don't have their own RPD.
1604
+ const isFree = !!d.is_free_tier;
1579
1605
  accounts.push({
1580
1606
  label, limitUsd: d.limit != null ? d.limit : null, usedUsd: d.usage || 0,
1581
1607
  remainingUsd: d.limit_remaining != null ? d.limit_remaining : null,
1582
1608
  dailyUsed: d.usage_daily || 0, weeklyUsed: d.usage_weekly || 0, monthlyUsed: d.usage_monthly || 0,
1583
- isFreeTier: !!d.is_free_tier, expiresAt: d.expires_at || null,
1584
- freeModelDaily: d.free_model_daily_requests || null,
1609
+ isFreeTier: isFree, expiresAt: d.expires_at || null,
1610
+ freeModelDaily: isFree ? (d.free_model_daily_requests || null) : null,
1585
1611
  budgetScope: eb.scope || null, budgetLimit: eb.limit_usd != null ? eb.limit_usd : null,
1586
1612
  budgetSpend: eb.spend_usd != null ? eb.spend_usd : null,
1587
1613
  budgetRemaining: eb.remaining_usd != null ? eb.remaining_usd : null,