opencode-statboard 2.10.38 → 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 +1 -1
- package/public/index.html +24 -3
- package/server.js +30 -11
package/package.json
CHANGED
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
|
-
|
|
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
|
-
|
|
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 ------------------------------------------------------
|
|
@@ -1069,7 +1089,6 @@ async function poll() {
|
|
|
1069
1089
|
renderTrend(null);
|
|
1070
1090
|
}
|
|
1071
1091
|
renderTools(lastTools);
|
|
1072
|
-
loadQuota(); // refresh quota after data load
|
|
1073
1092
|
const ago = Math.round((Date.now() - (j.generatedAt || 0)) / 1000);
|
|
1074
1093
|
const time = new Date().toLocaleTimeString(lang === 'zh-TW' ? 'zh-TW' : 'en', { hour12: false });
|
|
1075
1094
|
lastUpdatedText = t('lastUpdated') + ' ' + time + ' (' + t('dataAgo') + ' ' + ago + 's)';
|
|
@@ -1250,6 +1269,8 @@ poll();
|
|
|
1250
1269
|
setInterval(poll, 15000);
|
|
1251
1270
|
loadReport(scopeDays);
|
|
1252
1271
|
loadHeatmap().then(renderHeatmap);
|
|
1272
|
+
loadQuota(); // initial quota load
|
|
1273
|
+
setInterval(loadQuota, 5000); // refresh quota every 5s (server caches 5 min)
|
|
1253
1274
|
</script>
|
|
1254
1275
|
</body>
|
|
1255
1276
|
</html>
|
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
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
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
|
|