magpie-page-token-widget 0.1.0 → 0.2.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/README.md +150 -14
- package/lib/atomic.mjs +66 -0
- package/lib/collector.mjs +431 -123
- package/lib/forward.mjs +40 -19
- package/lib/normalize.mjs +66 -12
- package/lib/server.mjs +375 -58
- package/lib/state.mjs +197 -10
- package/lib/tailnet.mjs +140 -11
- package/mobile/app.js +242 -0
- package/mobile/index.html +66 -0
- package/mobile/style.css +9 -0
- package/package.json +2 -1
- package/scriptable/SetupUsageWidget.js +58 -58
- package/scriptable/UsageWidget.js +11 -2
- package/ui/app.js +292 -28
- package/ui/index.html +42 -1
- package/ui/style.css +81 -0
package/lib/normalize.mjs
CHANGED
|
@@ -366,6 +366,45 @@ export function normalizeUsage(raw) {
|
|
|
366
366
|
computers: ranking(raw.computers, 'computer'),
|
|
367
367
|
};
|
|
368
368
|
}
|
|
369
|
+
|
|
370
|
+
// The summary of the gateway carries the totals of the day, the hours, the
|
|
371
|
+
// vendors and the accounts in one answer. The vendors are gathered from the
|
|
372
|
+
// keys of the vendors and from the accounts, so a vendor whose calls come
|
|
373
|
+
// from an account it holds is counted once.
|
|
374
|
+
export function normalizeUsageSummary(raw) {
|
|
375
|
+
if (!raw || !['input', 'output', 'cache_read', 'cache_write', 'calls'].every(key =>
|
|
376
|
+
typeof raw[key] === 'number' && Number.isFinite(raw[key]) && raw[key] >= 0)) throw new Error('Invalid usage response');
|
|
377
|
+
const groups = new Map();
|
|
378
|
+
const gather = (provider, row) => {
|
|
379
|
+
if (typeof provider !== 'string' || !provider) return;
|
|
380
|
+
const family = PROVIDERS[provider]?.id || provider;
|
|
381
|
+
const group = groups.get(family) || {
|
|
382
|
+
id: provider, calls: 0, errors: 0, input: 0, output: 0, cache_read: 0, cache_write: 0, reasoning: 0,
|
|
383
|
+
};
|
|
384
|
+
for (const field of ['calls', 'errors', 'input', 'output', 'cache_read', 'cache_write', 'reasoning']) {
|
|
385
|
+
group[field] += number(row?.[field]);
|
|
386
|
+
}
|
|
387
|
+
groups.set(family, group);
|
|
388
|
+
};
|
|
389
|
+
for (const key of Array.isArray(raw.providerKeys) ? raw.providerKeys : []) gather(key?.provider, key);
|
|
390
|
+
for (const account of Array.isArray(raw.accounts) ? raw.accounts : []) gather(account?.provider, account);
|
|
391
|
+
const accounts = (Array.isArray(raw.accounts) ? raw.accounts : [])
|
|
392
|
+
.filter(row => row && typeof row.provider === 'string' && typeof row.account === 'string' && row.account.includes('@'))
|
|
393
|
+
.map(row => ({
|
|
394
|
+
provider: row.provider,
|
|
395
|
+
family: PROVIDERS[row.provider]?.id || null,
|
|
396
|
+
account: row.account.trim(),
|
|
397
|
+
...metrics(row),
|
|
398
|
+
}));
|
|
399
|
+
return {
|
|
400
|
+
metrics: metrics(raw),
|
|
401
|
+
trend: (Array.isArray(raw.series) ? raw.series : []).map((item, index) => ({
|
|
402
|
+
at: iso(item.time), label: iso(item.time) ? shanghaiDay(Date.parse(item.time)) : 'Day ' + (index + 1), ...metrics(item),
|
|
403
|
+
})),
|
|
404
|
+
providers: ranking([...groups.values()], 'provider'),
|
|
405
|
+
accounts,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
369
408
|
export function sanitizeCachedLabels(data, key) {
|
|
370
409
|
if (key === 'quotas') return normalizeQuotas({ data: data.map(card => ({
|
|
371
410
|
provider: card.provider === 'glm' ? 'zhipu' : card.provider, plan: card.plan, readAt: card.updatedAt, stale: card.stale === true,
|
|
@@ -377,11 +416,23 @@ export function sanitizeCachedLabels(data, key) {
|
|
|
377
416
|
})) });
|
|
378
417
|
const cleanRows = (rows, type) => (rows || []).map((row, index) => ({ ...row, label: typedLabel(row.label, type, index) }));
|
|
379
418
|
return { ...data,
|
|
380
|
-
trend: data.trend.map((row, index) => ({ ...row, label: iso(row.at) ? shanghaiDay(Date.parse(row.at)) : 'Day ' + (index + 1) })),
|
|
419
|
+
trend: (data.trend || []).map((row, index) => ({ ...row, label: iso(row.at) ? shanghaiDay(Date.parse(row.at)) : 'Day ' + (index + 1) })),
|
|
381
420
|
models: cleanRows(data.models, 'model'), providers: cleanRows(data.providers, 'provider'),
|
|
382
421
|
agents: cleanRows(data.agents, 'agent'), computers: cleanRows(data.computers, 'computer'),
|
|
383
422
|
};
|
|
384
423
|
}
|
|
424
|
+
|
|
425
|
+
// The summary of today has its own shape: the vendors and the accounts, and
|
|
426
|
+
// no models, agents or computers.
|
|
427
|
+
export function sanitizeCachedSummary(data) {
|
|
428
|
+
if (!data || typeof data !== 'object') return data;
|
|
429
|
+
const cleanRows = (rows, type) => (rows || []).map((row, index) => ({ ...row, label: typedLabel(row.label, type, index) }));
|
|
430
|
+
return { ...data,
|
|
431
|
+
trend: (data.trend || []).map((row, index) => ({ ...row, label: iso(row.at) ? shanghaiDay(Date.parse(row.at)) : 'Day ' + (index + 1) })),
|
|
432
|
+
providers: cleanRows(data.providers, 'provider'),
|
|
433
|
+
accounts: (data.accounts || []).map(row => ({ ...row })),
|
|
434
|
+
};
|
|
435
|
+
}
|
|
385
436
|
export const shanghaiDay = value => new Date(value + 8 * 3600_000).toISOString().slice(0, 10);
|
|
386
437
|
// Display choices live in the PC-side configuration; the phone and the dashboard
|
|
387
438
|
// only render what the service projects.
|
|
@@ -389,7 +440,7 @@ export const DEFAULT_DISPLAY = {
|
|
|
389
440
|
modelLimit: 8, agentLimit: 6, providerLimit: 20, computerLimit: 20, subscriptions: ['codex', 'minimax', 'glm'],
|
|
390
441
|
};
|
|
391
442
|
// A subscription is a vendor family (codex) or one of its accounts
|
|
392
|
-
// (codex@
|
|
443
|
+
// (codex@alice). The family's case does not matter; the account's does.
|
|
393
444
|
const splitSubscription = id => {
|
|
394
445
|
const at = typeof id === 'string' ? id.indexOf('@') : -1;
|
|
395
446
|
const family = (at < 0 ? String(id) : id.slice(0, at)).toLowerCase();
|
|
@@ -444,8 +495,8 @@ export function displayOverrides(argv = []) {
|
|
|
444
495
|
}
|
|
445
496
|
return overrides;
|
|
446
497
|
}
|
|
447
|
-
export function project(snapshot, now = Date.now(), display = {}) {
|
|
448
|
-
const
|
|
498
|
+
export function project(snapshot, now = Date.now(), display = {}, settings = {}) {
|
|
499
|
+
const choices = displayOptions(display);
|
|
449
500
|
const sources = {};
|
|
450
501
|
for (const key of ['quotas', 'today', 'week']) {
|
|
451
502
|
const part = snapshot.parts[key];
|
|
@@ -454,6 +505,9 @@ export function project(snapshot, now = Date.now(), display = {}) {
|
|
|
454
505
|
observedAt: part?.observedAt || null,
|
|
455
506
|
};
|
|
456
507
|
}
|
|
508
|
+
// A reading that ran out of time is given back marked as old, so the reader
|
|
509
|
+
// knows the numbers are not of this moment.
|
|
510
|
+
if (settings.expired) for (const key of Object.keys(sources)) if (sources[key].state === 'ok') sources[key].state = 'stale';
|
|
457
511
|
const todayPart = snapshot.parts.today;
|
|
458
512
|
const outdated = !!todayPart && (todayPart.periodDay || shanghaiDay(Date.parse(todayPart.observedAt))) !== shanghaiDay(now);
|
|
459
513
|
if (outdated) { sources.today.state = 'stale'; sources.today.outdated = true; }
|
|
@@ -496,10 +550,10 @@ export function project(snapshot, now = Date.now(), display = {}) {
|
|
|
496
550
|
expired: !!window.resetsAt && Date.parse(window.resetsAt) <= now,
|
|
497
551
|
})),
|
|
498
552
|
}));
|
|
499
|
-
const showAll =
|
|
553
|
+
const showAll = choices.subscriptions.includes('*');
|
|
500
554
|
// Without an explicit list the canonical family order is used; a provider that
|
|
501
555
|
// is not listed sorts last instead of first.
|
|
502
|
-
const order = showAll ? PROVIDER_ORDER :
|
|
556
|
+
const order = showAll ? PROVIDER_ORDER : choices.subscriptions;
|
|
503
557
|
// A card is ranked by the first entry that names it: its family, or the one
|
|
504
558
|
// account of that family the list picked.
|
|
505
559
|
const rank = card => {
|
|
@@ -507,20 +561,20 @@ export function project(snapshot, now = Date.now(), display = {}) {
|
|
|
507
561
|
return index < 0 ? order.length : index;
|
|
508
562
|
};
|
|
509
563
|
const shown = subscriptions
|
|
510
|
-
.filter(card => showAll ||
|
|
564
|
+
.filter(card => showAll || choices.subscriptions.includes(card.provider) || choices.subscriptions.includes(card.id))
|
|
511
565
|
.sort((a, b) => rank(a) - rank(b));
|
|
512
566
|
const week = snapshot.parts.week?.data;
|
|
513
567
|
return {
|
|
514
568
|
schemaVersion: 1, generatedAt: new Date(now).toISOString(), timezone: 'Asia/Shanghai', sources,
|
|
515
|
-
display:
|
|
569
|
+
display: choices,
|
|
516
570
|
subscriptions: shown,
|
|
517
571
|
usage: { today: outdated ? null : snapshot.parts.today?.data.metrics || null, week: week?.metrics || null },
|
|
518
572
|
trend: week?.trend || [],
|
|
519
573
|
rankings: {
|
|
520
|
-
models: (week?.models || []).slice(0,
|
|
521
|
-
agents: (week?.agents || []).slice(0,
|
|
522
|
-
providers: (week?.providers || []).slice(0,
|
|
523
|
-
computers: (week?.computers || []).slice(0,
|
|
574
|
+
models: (week?.models || []).slice(0, choices.modelLimit),
|
|
575
|
+
agents: (week?.agents || []).slice(0, choices.agentLimit),
|
|
576
|
+
providers: (week?.providers || []).slice(0, choices.providerLimit),
|
|
577
|
+
computers: (week?.computers || []).slice(0, choices.computerLimit),
|
|
524
578
|
},
|
|
525
579
|
};
|
|
526
580
|
}
|