opencode-statboard 2.10.29 → 2.10.31
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/move-quota2.py +29 -0
- package/move-quota3.py +34 -0
- package/move-quota4.py +31 -0
- package/move-quota5.py +26 -0
- package/package.json +1 -1
- package/public/index.html +37 -0
- package/server.js +63 -0
- package/tui-dashboard.js +22 -1
package/move-quota2.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
p = r'C:\Users\CPXru\Desktop\thumb\01_Active\oc-stats\server.js'
|
|
2
|
+
s = open(p, encoding='utf-8').read()
|
|
3
|
+
|
|
4
|
+
guard = "if (require.main !== module) {\n"
|
|
5
|
+
assert guard in s, 'guard not found'
|
|
6
|
+
guard_idx = s.index(guard)
|
|
7
|
+
|
|
8
|
+
# The quota block starts at '// ---- OpenRouter quota'
|
|
9
|
+
quota_start = s.index('// ---- OpenRouter quota')
|
|
10
|
+
# It ends right before 'module.exports = {'
|
|
11
|
+
me_start = s.index('module.exports = {')
|
|
12
|
+
quota_block = s[quota_start:me_start]
|
|
13
|
+
|
|
14
|
+
# Remove it from current location
|
|
15
|
+
s = s[:quota_start] + s[me_start:]
|
|
16
|
+
|
|
17
|
+
# Find the guard again (position may have shifted)
|
|
18
|
+
guard_idx = s.index(guard)
|
|
19
|
+
|
|
20
|
+
# Insert quota block BEFORE the guard
|
|
21
|
+
s = s[:guard_idx] + quota_block + '\n' + s[guard_idx:]
|
|
22
|
+
|
|
23
|
+
open(p, 'w', encoding='utf-8').write(s)
|
|
24
|
+
print('moved quota block before guard')
|
|
25
|
+
|
|
26
|
+
# Verify
|
|
27
|
+
gi = s.index(guard)
|
|
28
|
+
qi = s.index('// ---- OpenRouter quota')
|
|
29
|
+
print('guard at', gi, 'quota at', qi, 'quota before guard:', qi < gi)
|
package/move-quota3.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
p = r'C:\Users\CPXru\Desktop\thumb\01_Active\oc-stats\server.js'
|
|
2
|
+
s = open(p, encoding='utf-8').read()
|
|
3
|
+
|
|
4
|
+
# The quota block is currently inside the guard block.
|
|
5
|
+
# Find guard line
|
|
6
|
+
guard_line = 'if (require.main !== module) {'
|
|
7
|
+
assert guard_line in s
|
|
8
|
+
guard_idx = s.index(guard_line)
|
|
9
|
+
|
|
10
|
+
# Find quota block
|
|
11
|
+
quota_marker = '// ---- OpenRouter quota'
|
|
12
|
+
assert quota_marker in s
|
|
13
|
+
quota_start = s.index(quota_marker)
|
|
14
|
+
|
|
15
|
+
# The quota block ends at module.exports
|
|
16
|
+
me_marker = 'module.exports = {'
|
|
17
|
+
assert me_marker in s
|
|
18
|
+
me_start = s.index(me_marker)
|
|
19
|
+
|
|
20
|
+
# Extract quota block
|
|
21
|
+
quota_block = s[quota_start:me_start]
|
|
22
|
+
|
|
23
|
+
# Remove from current location
|
|
24
|
+
s = s[:quota_start] + s[me_start:]
|
|
25
|
+
|
|
26
|
+
# Recompute guard position after removal
|
|
27
|
+
guard_idx = s.index(guard_line)
|
|
28
|
+
|
|
29
|
+
# Insert before guard
|
|
30
|
+
s = s[:guard_idx] + quota_block + '\n' + s[guard_idx:]
|
|
31
|
+
|
|
32
|
+
open(p, 'w', encoding='utf-8').write(s)
|
|
33
|
+
print('moved quota block before guard')
|
|
34
|
+
print('quota before guard:', s.index(quota_marker) < s.index(guard_line))
|
package/move-quota4.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
p = r'C:\Users\CPXru\Desktop\thumb\01_Active\oc-stats\server.js'
|
|
2
|
+
s = open(p, encoding='utf-8').read()
|
|
3
|
+
|
|
4
|
+
guard_marker = 'if (require.main !== module) {'
|
|
5
|
+
assert guard_marker in s
|
|
6
|
+
guard_idx = s.index(guard_marker)
|
|
7
|
+
|
|
8
|
+
quota_marker = '// ---- OpenRouter quota'
|
|
9
|
+
assert quota_marker in s
|
|
10
|
+
quota_start = s.index(quota_marker)
|
|
11
|
+
|
|
12
|
+
# The quota block ends at 'module.exports = {'
|
|
13
|
+
me_marker = 'module.exports = {'
|
|
14
|
+
assert me_marker in s
|
|
15
|
+
me_start = s.index(me_marker)
|
|
16
|
+
|
|
17
|
+
# Extract quota block
|
|
18
|
+
quota_block = s[quota_start:me_start]
|
|
19
|
+
|
|
20
|
+
# Remove from current location
|
|
21
|
+
s = s[:quota_start] + s[me_start:]
|
|
22
|
+
|
|
23
|
+
# Recompute guard position
|
|
24
|
+
guard_idx = s.index(guard_marker)
|
|
25
|
+
|
|
26
|
+
# Insert before guard
|
|
27
|
+
s = s[:guard_idx] + quota_block + '\n' + s[guard_idx:]
|
|
28
|
+
|
|
29
|
+
open(p, 'w', encoding='utf-8').write(s)
|
|
30
|
+
print('moved')
|
|
31
|
+
print('quota before guard:', s.index(quota_marker) < s.index(guard_marker))
|
package/move-quota5.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
p = r'C:\Users\CPXru\Desktop\thumb\01_Active\oc-stats\server.js'
|
|
2
|
+
s = open(p, encoding='utf-8').read()
|
|
3
|
+
|
|
4
|
+
guard_marker = 'if (require.main !== module) {'
|
|
5
|
+
assert guard_marker in s
|
|
6
|
+
guard_idx = s.index(guard_marker)
|
|
7
|
+
|
|
8
|
+
quota_marker = '// ---- OpenRouter quota'
|
|
9
|
+
assert quota_marker in s
|
|
10
|
+
quota_start = s.index(quota_marker)
|
|
11
|
+
|
|
12
|
+
# quota block is from quota_marker to guard_marker (exclusive)
|
|
13
|
+
quota_block = s[quota_start:guard_idx]
|
|
14
|
+
|
|
15
|
+
# Remove it
|
|
16
|
+
s = s[:quota_start] + s[guard_idx:]
|
|
17
|
+
|
|
18
|
+
# guard is now at the start of what was after quota_block
|
|
19
|
+
guard_idx = s.index(guard_marker)
|
|
20
|
+
|
|
21
|
+
# Insert before guard
|
|
22
|
+
s = s[:guard_idx] + quota_block + '\n' + s[guard_idx:]
|
|
23
|
+
|
|
24
|
+
open(p, 'w', encoding='utf-8').write(s)
|
|
25
|
+
print('moved')
|
|
26
|
+
print('quota before guard:', s.index(quota_marker) < s.index(guard_marker))
|
package/package.json
CHANGED
package/public/index.html
CHANGED
|
@@ -230,6 +230,12 @@
|
|
|
230
230
|
</div>
|
|
231
231
|
</details>
|
|
232
232
|
|
|
233
|
+
<!-- 1b. OpenRouter quota (key-level remaining credits & free-tier RPD) -->
|
|
234
|
+
<details class="tool-section" open id="sec-quota">
|
|
235
|
+
<summary><h2 data-i18n="openrouterQuota">OpenRouter Quota</h2><span class="scope-tag" id="tag-quota"></span></summary>
|
|
236
|
+
<div id="quota-body" style="padding:8px 0;font-size:12px;color:var(--muted)"></div>
|
|
237
|
+
</details>
|
|
238
|
+
|
|
233
239
|
<!-- 2. TREND — bucket count follows the scope -->
|
|
234
240
|
<details class="tool-section" open id="sec-trend">
|
|
235
241
|
<summary><h2 data-i18n="trend">TREND</h2><span class="scope-tag" id="tag-trend"></span></summary>
|
|
@@ -402,6 +408,7 @@ const STRINGS = {
|
|
|
402
408
|
kindProxyTip: 'borrowed price from another provider (estimate)',
|
|
403
409
|
kindOpenrouter: 'openrouter',
|
|
404
410
|
kindOpenrouterTip: 'live OpenRouter list price for the paid twin',
|
|
411
|
+
openrouterQuota: 'OpenRouter Quota',
|
|
405
412
|
kindPartialTip: 'part of the usage priced, rest not in catalog',
|
|
406
413
|
kindUnpricedTip: 'not in catalog — market/saved unknown',
|
|
407
414
|
legacyAgent: 'legacy',
|
|
@@ -450,6 +457,7 @@ const STRINGS = {
|
|
|
450
457
|
kindProxyTip: '借用其他供應商牌價(代估)',
|
|
451
458
|
kindOpenrouter: 'openrouter',
|
|
452
459
|
kindOpenrouterTip: 'OpenRouter 即時牌價(同級付費版)',
|
|
460
|
+
openrouterQuota: 'OpenRouter 額度',
|
|
453
461
|
kindPartialTip: '部分用量有牌價,其餘查無',
|
|
454
462
|
kindUnpricedTip: '目錄查無 — 市價/節省未知',
|
|
455
463
|
legacyAgent: '舊版',
|
|
@@ -742,6 +750,34 @@ function renderPricing(rep) {
|
|
|
742
750
|
$('pricing-empty').textContent = prov.length ? '' : t('noData');
|
|
743
751
|
}
|
|
744
752
|
|
|
753
|
+
function fmtUsd(v) { if (v == null) return EM; return '$' + Number(v).toFixed(4); }
|
|
754
|
+
function renderQuota(q) {
|
|
755
|
+
const body = $('quota-body');
|
|
756
|
+
if (!q || !q.ok) { body.textContent = q && q.error ? q.error : t('noData'); return; }
|
|
757
|
+
const rows = (q.accounts || []).map((a) => {
|
|
758
|
+
if (a.error) return '<div style="color:var(--err)">' + esc(a.label) + ': ' + esc(a.error) + '</div>';
|
|
759
|
+
const free = a.freeModelDaily || {};
|
|
760
|
+
const rem = a.remainingUsd != null ? fmtUsd(a.remainingUsd) : EM;
|
|
761
|
+
const used = fmtUsd(a.usedUsd || 0);
|
|
762
|
+
const lim = a.limitUsd != null ? fmtUsd(a.limitUsd) : 'unlimited';
|
|
763
|
+
const pct = (a.limitUsd && a.limitUsd > 0) ? Math.round(((a.usedUsd || 0) / a.limitUsd) * 100) : null;
|
|
764
|
+
const freeLine = free.limit != null
|
|
765
|
+
? 'free RPD ' + fmt(free.used) + '/' + fmt(free.limit) + ' (left ' + fmt(free.remaining) + ')'
|
|
766
|
+
: '';
|
|
767
|
+
const bud = a.budgetRemaining != null ? 'budget left ' + fmtUsd(a.budgetRemaining) + (a.budgetResetInterval ? ' (resets ' + a.budgetResetInterval + ')' : '') : '';
|
|
768
|
+
return '<div style="margin-bottom:6px"><b>' + esc(a.label) + '</b>'
|
|
769
|
+
+ ' remaining ' + rem + ' / ' + lim + (pct != null ? ' (' + pct + '% used)' : '')
|
|
770
|
+
+ ' used ' + used + (a.isFreeTier ? ' [free tier]' : '')
|
|
771
|
+
+ (freeLine ? '<br> ' + freeLine : '')
|
|
772
|
+
+ (bud ? '<br> ' + bud : '')
|
|
773
|
+
+ '</div>';
|
|
774
|
+
}).join('');
|
|
775
|
+
body.innerHTML = rows + '<div style="color:var(--muted);font-size:10px;margin-top:4px">reset 00:00 UTC = 08:00 TW · cached 5 min</div>';
|
|
776
|
+
}
|
|
777
|
+
async function loadQuota() {
|
|
778
|
+
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; }
|
|
779
|
+
}
|
|
780
|
+
|
|
745
781
|
// ---- trend (buckets follow scope; newest day leftmost) -----------------
|
|
746
782
|
function renderTrend(data) {
|
|
747
783
|
if (!data || !data.labels || !data.labels.length) {
|
|
@@ -980,6 +1016,7 @@ function renderAll() {
|
|
|
980
1016
|
renderTrend(tr || null);
|
|
981
1017
|
renderHeatmap(heatmapTrend);
|
|
982
1018
|
renderTools(lastTools);
|
|
1019
|
+
if (!window.__quotaLoaded) { window.__quotaLoaded = true; loadQuota(); }
|
|
983
1020
|
}
|
|
984
1021
|
|
|
985
1022
|
// ---- data loading ------------------------------------------------------
|
package/server.js
CHANGED
|
@@ -1513,6 +1513,11 @@ const server = http.createServer(async (req, res) => {
|
|
|
1513
1513
|
}
|
|
1514
1514
|
respond({ ok: true, path: dbPath, files, totalSize, pragma });
|
|
1515
1515
|
} catch (e) { respond({ ok: false, error: e.message }); }
|
|
1516
|
+
} else if (u === '/api/openrouter-quota') {
|
|
1517
|
+
try {
|
|
1518
|
+
const q = await fetchOpenRouterQuota();
|
|
1519
|
+
respond(q);
|
|
1520
|
+
} catch (e) { respond({ ok: false, error: e.message, accounts: [] }); }
|
|
1516
1521
|
} else if (u === '/') {
|
|
1517
1522
|
loadIndexHtml((err, data) => {
|
|
1518
1523
|
if (err) {
|
|
@@ -1534,6 +1539,64 @@ const server = http.createServer(async (req, res) => {
|
|
|
1534
1539
|
// helpers and bail out before any server-side side effect (listening, timers,
|
|
1535
1540
|
// signal handlers). `bin` points straight at this file, so a normal
|
|
1536
1541
|
// `opencode-dashboard` run always has require.main === module.
|
|
1542
|
+
// ---- OpenRouter quota (key-level remaining credits & free-tier RPD) ------
|
|
1543
|
+
const OR_KEY_FILE = path.join(process.env.USERPROFILE || process.env.HOME || '', '.thumb-secrets', 'openrouter-keys.txt');
|
|
1544
|
+
const OR_QUOTA_TTL = 5 * 60 * 1000;
|
|
1545
|
+
let orQuotaCache = { at: 0, data: null };
|
|
1546
|
+
|
|
1547
|
+
function readOpenRouterKeys() {
|
|
1548
|
+
const keys = [];
|
|
1549
|
+
for (const name of ['OPENROUTER_API_KEY', 'OPENROUTER_API_KEY_PRIMARY', 'OPENROUTER_API_KEY_FREE']) {
|
|
1550
|
+
const v = process.env[name];
|
|
1551
|
+
if (v) keys.push({ label: name.replace('OPENROUTER_API_KEY_', '').replace('OPENROUTER_API_KEY', 'PRIMARY'), key: v.trim() });
|
|
1552
|
+
}
|
|
1553
|
+
if (keys.length === 0) {
|
|
1554
|
+
try {
|
|
1555
|
+
const lines = fs.readFileSync(OR_KEY_FILE, 'utf8').split(/\r?\n/);
|
|
1556
|
+
for (const line of lines) {
|
|
1557
|
+
const m = line.match(/^OPENROUTER_KEY_(\w+)\s*=\s*(sk-or-v1-[a-f0-9]+)/);
|
|
1558
|
+
if (m) keys.push({ label: m[1], key: m[2] });
|
|
1559
|
+
}
|
|
1560
|
+
} catch (_) { /* no file: skip */ }
|
|
1561
|
+
}
|
|
1562
|
+
const seen = new Set();
|
|
1563
|
+
return keys.filter((k) => { if (seen.has(k.key)) return false; seen.add(k.key); return true; });
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
async function fetchOpenRouterQuota() {
|
|
1567
|
+
const now = Date.now();
|
|
1568
|
+
if (orQuotaCache.data && now - orQuotaCache.at < OR_QUOTA_TTL) return orQuotaCache.data;
|
|
1569
|
+
const keys = readOpenRouterKeys();
|
|
1570
|
+
if (keys.length === 0) return { ok: false, error: 'no keys found', accounts: [] };
|
|
1571
|
+
const accounts = [];
|
|
1572
|
+
for (const { label, key } of keys) {
|
|
1573
|
+
try {
|
|
1574
|
+
const r = await fetch('https://openrouter.ai/api/v1/key', { headers: { Authorization: 'Bearer ' + key } });
|
|
1575
|
+
if (!r.ok) { accounts.push({ label, error: 'HTTP ' + r.status }); continue; }
|
|
1576
|
+
const j = await r.json();
|
|
1577
|
+
const d = j.data || {};
|
|
1578
|
+
const eb = d.effective_budget || {};
|
|
1579
|
+
accounts.push({
|
|
1580
|
+
label, limitUsd: d.limit != null ? d.limit : null, usedUsd: d.usage || 0,
|
|
1581
|
+
remainingUsd: d.limit_remaining != null ? d.limit_remaining : null,
|
|
1582
|
+
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,
|
|
1585
|
+
budgetScope: eb.scope || null, budgetLimit: eb.limit_usd != null ? eb.limit_usd : null,
|
|
1586
|
+
budgetSpend: eb.spend_usd != null ? eb.spend_usd : null,
|
|
1587
|
+
budgetRemaining: eb.remaining_usd != null ? eb.remaining_usd : null,
|
|
1588
|
+
budgetResetInterval: eb.reset_interval || null, budgetResetsAt: eb.resets_at || null,
|
|
1589
|
+
fetchedAt: now,
|
|
1590
|
+
});
|
|
1591
|
+
} catch (e) { accounts.push({ label, error: String(e && e.message || e) }); }
|
|
1592
|
+
}
|
|
1593
|
+
const result = { ok: true, accounts, fetchedAt: now, resetUtc: '00:00 UTC (08:00 TW)' };
|
|
1594
|
+
orQuotaCache = { at: now, data: result };
|
|
1595
|
+
return result;
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
|
|
1599
|
+
|
|
1537
1600
|
if (require.main !== module) {
|
|
1538
1601
|
module.exports = {
|
|
1539
1602
|
buildAttrBundle,
|
package/tui-dashboard.js
CHANGED
|
@@ -40,6 +40,7 @@ const I18N = {
|
|
|
40
40
|
cost: 'Cost',
|
|
41
41
|
marketValue: 'Market Value',
|
|
42
42
|
saved: 'Saved',
|
|
43
|
+
openrouterQuota: 'OpenRouter Quota',
|
|
43
44
|
avgCostDay: 'Avg Cost/Day',
|
|
44
45
|
avgTokensDay: 'Avg Tokens/Day',
|
|
45
46
|
projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
|
|
@@ -77,7 +78,8 @@ const I18N = {
|
|
|
77
78
|
total: '總計',
|
|
78
79
|
cost: '成本',
|
|
79
80
|
marketValue: '市場價值',
|
|
80
|
-
saved: '
|
|
81
|
+
saved: 'Saved',
|
|
82
|
+
openrouterQuota: 'OpenRouter 額度',
|
|
81
83
|
avgCostDay: '日均成本',
|
|
82
84
|
avgTokensDay: '日均Token',
|
|
83
85
|
projectHeader: '{專案:<45} {會話:>10} {Token:>18} {成本:>14}',
|
|
@@ -117,6 +119,7 @@ const I18N = {
|
|
|
117
119
|
cost: 'Cost',
|
|
118
120
|
marketValue: 'Market Value',
|
|
119
121
|
saved: 'Saved',
|
|
122
|
+
openrouterQuota: 'OpenRouter Quota',
|
|
120
123
|
avgCostDay: 'Avg Cost/Day',
|
|
121
124
|
avgTokensDay: 'Avg Tokens/Day',
|
|
122
125
|
projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
|
|
@@ -259,6 +262,9 @@ class TUIServer {
|
|
|
259
262
|
// Parse trend
|
|
260
263
|
const trendData = trend.trend || trend;
|
|
261
264
|
|
|
265
|
+
// Quota (fire and forget, cached server-side 5 min)
|
|
266
|
+
this.fetchData('/api/openrouter-quota').then(q => { this.quota = q; }).catch(() => {});
|
|
267
|
+
|
|
262
268
|
// Handle overview.sessions being a number or object
|
|
263
269
|
const sessionsValue = overview.sessions;
|
|
264
270
|
const sessionsObj = typeof sessionsValue === 'number'
|
|
@@ -530,6 +536,21 @@ class TUIServer {
|
|
|
530
536
|
'{bold}' + t('avgCostDay') + ':{/bold} ' + this.formatCost(cost.avgCostPerDay || 0),
|
|
531
537
|
'{bold}' + t('avgTokensDay') + ':{/bold} ' + this.formatNumber(cost.avgTokensPerDay || 0)
|
|
532
538
|
];
|
|
539
|
+
if (this.quota && this.quota.ok && this.quota.accounts && this.quota.accounts.length) {
|
|
540
|
+
lines.push('');
|
|
541
|
+
lines.push('{bold}' + t('openrouterQuota') + ':{/bold}');
|
|
542
|
+
for (const a of this.quota.accounts) {
|
|
543
|
+
if (a.error) { lines.push(' ' + a.label + ': ' + a.error); continue; }
|
|
544
|
+
const free = a.freeModelDaily || {};
|
|
545
|
+
const rem = a.remainingUsd != null ? this.formatCost(a.remainingUsd) : '—';
|
|
546
|
+
const lim = a.limitUsd != null ? this.formatCost(a.limitUsd) : 'unlimited';
|
|
547
|
+
const used = this.formatCost(a.usedUsd || 0);
|
|
548
|
+
const freeLine = free.limit != null ? ('free RPD ' + free.used + '/' + free.limit + ' (left ' + free.remaining + ')') : '';
|
|
549
|
+
lines.push(' ' + a.label + ': ' + rem + ' / ' + lim + ' (used ' + used + ')' + (a.isFreeTier ? ' [free]' : ''));
|
|
550
|
+
if (freeLine) lines.push(' ' + freeLine);
|
|
551
|
+
}
|
|
552
|
+
lines.push(' {gray-fg}reset 00:00 UTC = 08:00 TW{/gray-fg}');
|
|
553
|
+
}
|
|
533
554
|
return this.fillScreen(lines);
|
|
534
555
|
}
|
|
535
556
|
|