opencode-statboard 2.10.39 → 2.10.40

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.39",
3
+ "version": "2.10.40",
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
@@ -791,8 +791,26 @@ function renderQuota(q) {
791
791
  }).join('');
792
792
  body.innerHTML = rows + '<div style="color:var(--muted);font-size:10px;margin-top:8px;text-align:center">' + t('quotaReset') + ' · ' + t('cached') + '</div>';
793
793
  }
794
+ // Single-flight: concurrent callers share one in-flight request instead of
795
+ // each firing 3 live HTTPS calls to openrouter.ai (that pile-up is what made
796
+ // the panel stay empty and the page feel slow).
797
+ let quotaInFlight = null;
794
798
  async function loadQuota() {
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; }
799
+ if (quotaInFlight) return quotaInFlight;
800
+ quotaInFlight = (async () => {
801
+ try {
802
+ const r = await fetch('/api/openrouter-quota?_=' + Date.now(), { cache: 'no-store' });
803
+ const j = await r.json();
804
+ renderQuota(j);
805
+ return j;
806
+ } catch (e) {
807
+ $('quota-body').textContent = 'load failed: ' + e.message;
808
+ return null;
809
+ } finally {
810
+ quotaInFlight = null;
811
+ }
812
+ })();
813
+ return quotaInFlight;
796
814
  }
797
815
 
798
816
  // ---- trend (buckets follow scope; newest day leftmost) -----------------
@@ -1033,7 +1051,9 @@ function renderAll() {
1033
1051
  renderTrend(tr || null);
1034
1052
  renderHeatmap(heatmapTrend);
1035
1053
  renderTools(lastTools);
1036
- loadQuota(); // always refresh quota on render (server caches 5 min, so this is cheap)
1054
+ // Quota is refreshed by its own 5s interval, not on every render: renderAll
1055
+ // runs on each poll/scope change, and firing a quota fetch from all of them
1056
+ // piled up live HTTPS calls and left the panel blank.
1037
1057
  }
1038
1058
 
1039
1059
  // ---- data loading ------------------------------------------------------
package/server.js CHANGED
@@ -1566,6 +1566,7 @@ const server = http.createServer(async (req, res) => {
1566
1566
  const OR_KEY_FILE = path.join(process.env.USERPROFILE || process.env.HOME || '', '.thumb-secrets', 'openrouter-keys.txt');
1567
1567
  const OR_QUOTA_TTL = 5 * 60 * 1000;
1568
1568
  let orQuotaCache = { at: 0, data: null };
1569
+ let orQuotaInFlight = null; // single-flight: concurrent callers share one round
1569
1570
 
1570
1571
  function readOpenRouterKeys() {
1571
1572
  const keys = [];
@@ -1589,14 +1590,26 @@ function readOpenRouterKeys() {
1589
1590
  async function fetchOpenRouterQuota() {
1590
1591
  const now = Date.now();
1591
1592
  if (orQuotaCache.data && now - orQuotaCache.at < OR_QUOTA_TTL) return orQuotaCache.data;
1592
- const keys = readOpenRouterKeys();
1593
- if (keys.length === 0) return { ok: false, error: 'no keys found', accounts: [] };
1594
- const accounts = [];
1595
- for (const { label, key } of keys) {
1596
- try {
1597
- const r = await fetch('https://openrouter.ai/api/v1/key', { headers: { Authorization: 'Bearer ' + key } });
1598
- if (!r.ok) { accounts.push({ label, error: 'HTTP ' + r.status }); continue; }
1599
- const j = await r.json();
1593
+ // Single-flight: if a round is already running, wait for it instead of
1594
+ // starting a second one (each round is 3 live HTTPS calls).
1595
+ if (orQuotaInFlight) return orQuotaInFlight;
1596
+ orQuotaInFlight = (async () => {
1597
+ const keys = readOpenRouterKeys();
1598
+ if (keys.length === 0) return { ok: false, error: 'no keys found', accounts: [] };
1599
+ const accounts = [];
1600
+ for (const { label, key } of keys) {
1601
+ try {
1602
+ // Per-key timeout so one hanging network call cannot stall the panel.
1603
+ const ac = new AbortController();
1604
+ const timer = setTimeout(() => ac.abort(), 6000);
1605
+ let r;
1606
+ try {
1607
+ r = await fetch('https://openrouter.ai/api/v1/key', { headers: { Authorization: 'Bearer ' + key }, signal: ac.signal });
1608
+ } finally {
1609
+ clearTimeout(timer);
1610
+ }
1611
+ if (!r.ok) { accounts.push({ label, error: 'HTTP ' + r.status }); continue; }
1612
+ const j = await r.json();
1600
1613
  const d = j.data || {};
1601
1614
  const eb = d.effective_budget || {};
1602
1615
  // free_model_daily_requests is account-level (shared across all keys of the same account).
@@ -1615,10 +1628,16 @@ async function fetchOpenRouterQuota() {
1615
1628
  fetchedAt: now,
1616
1629
  });
1617
1630
  } catch (e) { accounts.push({ label, error: String(e && e.message || e) }); }
1631
+ }
1632
+ const result = { ok: true, accounts, fetchedAt: Date.now(), resetUtc: '00:00 UTC (08:00 TW)' };
1633
+ orQuotaCache = { at: Date.now(), data: result };
1634
+ return result;
1635
+ })();
1636
+ try {
1637
+ return await orQuotaInFlight;
1638
+ } finally {
1639
+ orQuotaInFlight = null;
1618
1640
  }
1619
- const result = { ok: true, accounts, fetchedAt: now, resetUtc: '00:00 UTC (08:00 TW)' };
1620
- orQuotaCache = { at: now, data: result };
1621
- return result;
1622
1641
  }
1623
1642
 
1624
1643