agy-family 0.2.11 → 0.3.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/package.json +1 -1
- package/packages/cli/bin/agy-family.mjs +1 -1
- package/packages/cli/src/hook.mjs +1 -1
- package/packages/core/src/sync.mjs +2 -2
- package/packages/server-core/src/index.mjs +18 -5
- package/packages/server-core/src/stats.mjs +258 -42
- package/packages/server-core/src/ui.mjs +537 -97
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agy-family",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Zero-privacy-leak transparent usage dashboard, /family slash command, and fair-use budget guardrail for Google One / Antigravity family groups.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -47,7 +47,7 @@ import {
|
|
|
47
47
|
writeHostConfig,
|
|
48
48
|
} from '../src/host.mjs';
|
|
49
49
|
|
|
50
|
-
const CLI_VERSION = '0.
|
|
50
|
+
const CLI_VERSION = '0.3.0';
|
|
51
51
|
const THIS_CLI_PATH = fileURLToPath(import.meta.url);
|
|
52
52
|
const PACKAGE_ROOT = resolve(dirname(THIS_CLI_PATH), '..', '..', '..');
|
|
53
53
|
|
|
@@ -716,7 +716,7 @@ export async function runHeadlessSync({ cfg, fullSync = false, home = homedir(),
|
|
|
716
716
|
const { payload, scan, nextSyncedHashes, nextAccountLedger } = await buildSyncPayload({
|
|
717
717
|
deviceId,
|
|
718
718
|
clientKind: 'cli',
|
|
719
|
-
clientVersion: '0.
|
|
719
|
+
clientVersion: '0.3.0',
|
|
720
720
|
home,
|
|
721
721
|
fileCache: cache,
|
|
722
722
|
syncedHashes: prevHashes,
|
|
@@ -82,7 +82,7 @@ export function diffSyncBuckets(buckets, syncedHashes = null, { fullSync = false
|
|
|
82
82
|
export async function buildSyncPayload({
|
|
83
83
|
deviceId,
|
|
84
84
|
clientKind = 'cli',
|
|
85
|
-
clientVersion = '0.
|
|
85
|
+
clientVersion = '0.3.0',
|
|
86
86
|
home,
|
|
87
87
|
roots = null,
|
|
88
88
|
sourceFilter = 'all',
|
|
@@ -155,7 +155,7 @@ export async function buildSyncPayload({
|
|
|
155
155
|
deviceId: String(deviceId || deriveAnonymousDeviceId()).slice(0, 32),
|
|
156
156
|
client: {
|
|
157
157
|
kind: clientKind === 'extension' ? 'extension' : 'cli',
|
|
158
|
-
version: String(clientVersion || '0.
|
|
158
|
+
version: String(clientVersion || '0.3.0').slice(0, 24),
|
|
159
159
|
},
|
|
160
160
|
account: {
|
|
161
161
|
emailHash: effectiveReportedHash,
|
|
@@ -9,6 +9,7 @@ import { ensureSchema, MAX_FAMILY_MEMBERS } from './schema.mjs';
|
|
|
9
9
|
import {
|
|
10
10
|
buildBoardCsv,
|
|
11
11
|
buildBoardPayload,
|
|
12
|
+
buildEffectivePricingTable,
|
|
12
13
|
buildMemberDetailPayload,
|
|
13
14
|
checkOfflineMembersAndAlert,
|
|
14
15
|
computeCreditsReconciliation,
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
import { FAVICON_SVG, renderDashboardHtml } from './ui.mjs';
|
|
24
25
|
import { dispatchWebhook, sendDedupedAlert, validateWebhookUrl } from './webhooks.mjs';
|
|
25
26
|
|
|
26
|
-
export const SERVER_VERSION = '0.
|
|
27
|
+
export const SERVER_VERSION = '0.3.0';
|
|
27
28
|
const MAX_BUCKETS_PER_SYNC = 1000;
|
|
28
29
|
const MAX_DEVICES_PER_MEMBER = 8;
|
|
29
30
|
const MAX_TOKENS_PER_BUCKET = 500_000_000;
|
|
@@ -837,12 +838,21 @@ export function createApp({
|
|
|
837
838
|
} catch {
|
|
838
839
|
integrityEvents = [];
|
|
839
840
|
}
|
|
840
|
-
return adminJsonRes({
|
|
841
|
+
return adminJsonRes({
|
|
842
|
+
settings,
|
|
843
|
+
serverVersion: SERVER_VERSION,
|
|
844
|
+
pricingTable: buildEffectivePricingTable(settings),
|
|
845
|
+
members,
|
|
846
|
+
maxMembers: MAX_FAMILY_MEMBERS,
|
|
847
|
+
snapshots,
|
|
848
|
+
alerts,
|
|
849
|
+
integrityEvents,
|
|
850
|
+
});
|
|
841
851
|
}
|
|
842
852
|
|
|
843
853
|
if (request.method === 'POST' && path === '/api/admin/members') {
|
|
844
|
-
const
|
|
845
|
-
if ((
|
|
854
|
+
const existingRows = await db.all('SELECT color FROM members');
|
|
855
|
+
if ((existingRows || []).length >= MAX_FAMILY_MEMBERS) {
|
|
846
856
|
return adminJsonRes(
|
|
847
857
|
{
|
|
848
858
|
error: `Google 家庭组最多支持 ${MAX_FAMILY_MEMBERS} 位成员(1 位家庭组管理员 + 5 位家庭成员)。`,
|
|
@@ -856,7 +866,10 @@ export function createApp({
|
|
|
856
866
|
const name = String(body.name || '').trim().slice(0, 40);
|
|
857
867
|
if (!name) return adminJsonRes({ error: 'Member name is required.' }, 400);
|
|
858
868
|
const role = body.role === 'manager' ? 'manager' : 'member';
|
|
859
|
-
const
|
|
869
|
+
const defaultPalette = ['#f54e00', '#1f8a65', '#3b7dd8', '#8b5cf6', '#c08532', '#cf2d56'];
|
|
870
|
+
const usedColors = new Set((existingRows || []).map((r) => String(r.color || '').toLowerCase()));
|
|
871
|
+
const nextAutoColor = defaultPalette.find((c) => !usedColors.has(c)) || defaultPalette[(existingRows || []).length % defaultPalette.length];
|
|
872
|
+
const color = /^#[0-9a-fA-F]{6}$/.test(String(body.color || '')) ? body.color : nextAutoColor;
|
|
860
873
|
const quotaPct = parseQuotaPctInput(body.quotaPct);
|
|
861
874
|
const emailParsed = await parseMemberEmailInput(body.email);
|
|
862
875
|
const expectedEmailHash = emailParsed.action === 'set' ? emailParsed.hash : null;
|
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
// 24h activity timeline, member drill-down, CSV settlement export, and shared AI Credits reconciliation.
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
BUILTIN_RATES_MAP,
|
|
5
6
|
estimateCacheSavedUsd,
|
|
6
7
|
estimateUsageUsd,
|
|
7
8
|
fetchRemotePricingCache,
|
|
8
9
|
isKnownModelRate,
|
|
10
|
+
isValidRateEntry,
|
|
11
|
+
lookupRates,
|
|
12
|
+
normalizeModelKey,
|
|
9
13
|
PRICING_RETRY_COOLDOWN_MS,
|
|
10
14
|
PRICING_TTL_MS,
|
|
11
15
|
} from '../../core/src/pricing.mjs';
|
|
@@ -16,24 +20,76 @@ import { sendDedupedAlert } from './webhooks.mjs';
|
|
|
16
20
|
const HOUR_MS = 60 * 60 * 1000;
|
|
17
21
|
const DAY_MS = 24 * HOUR_MS;
|
|
18
22
|
|
|
19
|
-
export function
|
|
20
|
-
const inp = Number(row.input_tokens) || 0;
|
|
21
|
-
const out = Number(row.output_tokens) || 0;
|
|
22
|
-
const cached = Number(row.cache_read_tokens) || 0;
|
|
23
|
-
if (inp + out + cached <= 0) {
|
|
24
|
-
return Number(row.est_usd) || 0;
|
|
25
|
-
}
|
|
23
|
+
export function computeBucketCostParts(row, settings = {}) {
|
|
24
|
+
const inp = Math.max(0, Number(row.input_tokens) || 0);
|
|
25
|
+
const out = Math.max(0, Number(row.output_tokens) || 0);
|
|
26
|
+
const cached = Math.max(0, Number(row.cache_read_tokens) || 0);
|
|
26
27
|
const modName = normalizeModelName(row.model);
|
|
27
|
-
|
|
28
|
-
model: modName,
|
|
29
|
-
input: inp,
|
|
30
|
-
output: out,
|
|
31
|
-
cacheRead: cached,
|
|
28
|
+
const rate = lookupRates(modName, {
|
|
32
29
|
customPricing: settings.customPricing || null,
|
|
33
30
|
pricingCache: settings.pricingCache || null,
|
|
34
31
|
useLocalSources: false,
|
|
35
32
|
fallback: true,
|
|
36
33
|
});
|
|
34
|
+
if (inp + out + cached <= 0) {
|
|
35
|
+
const est = Number(row.est_usd) || 0;
|
|
36
|
+
return {
|
|
37
|
+
usd: est,
|
|
38
|
+
inputUsd: 0,
|
|
39
|
+
outputUsd: 0,
|
|
40
|
+
cacheReadUsd: 0,
|
|
41
|
+
rate: rate
|
|
42
|
+
? { input: rate.input, output: rate.output, cacheRead: rate.cacheRead, source: rate.source, isEstimated: Boolean(rate.isFallback) }
|
|
43
|
+
: null,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const inputUsd = (inp * (rate?.input || 0)) / 1_000_000;
|
|
47
|
+
const outputUsd = (out * (rate?.output || 0)) / 1_000_000;
|
|
48
|
+
const cacheReadUsd = (cached * (rate?.cacheRead || 0)) / 1_000_000;
|
|
49
|
+
const usd = Math.round((inputUsd + outputUsd + cacheReadUsd) * 10_000) / 10_000;
|
|
50
|
+
return {
|
|
51
|
+
usd,
|
|
52
|
+
inputUsd,
|
|
53
|
+
outputUsd,
|
|
54
|
+
cacheReadUsd,
|
|
55
|
+
rate: rate
|
|
56
|
+
? { input: rate.input, output: rate.output, cacheRead: rate.cacheRead, source: rate.source, isEstimated: Boolean(rate.isFallback) }
|
|
57
|
+
: null,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function buildEffectivePricingTable(settings = {}) {
|
|
62
|
+
const merged = new Map();
|
|
63
|
+
const addLayer = (mapObj, sourceLabel, isCustom) => {
|
|
64
|
+
if (!mapObj || typeof mapObj !== 'object') return;
|
|
65
|
+
for (const [rawKey, entry] of Object.entries(mapObj)) {
|
|
66
|
+
const id = normalizeModelKey(rawKey);
|
|
67
|
+
if (!id || !isValidRateEntry(entry)) continue;
|
|
68
|
+
const prev = merged.get(id);
|
|
69
|
+
merged.set(id, {
|
|
70
|
+
id,
|
|
71
|
+
label: (entry.label && String(entry.label).trim()) || prev?.label || rawKey,
|
|
72
|
+
input: Number(entry.input),
|
|
73
|
+
output: Number(entry.output),
|
|
74
|
+
cacheRead: Number(entry.cacheRead ?? 0),
|
|
75
|
+
source: sourceLabel,
|
|
76
|
+
isCustom: Boolean(isCustom),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
addLayer(BUILTIN_RATES_MAP, 'builtin', false);
|
|
81
|
+
const cacheSrc = settings.pricingCache?.source || 'models.dev';
|
|
82
|
+
addLayer(settings.pricingCache?.rates, cacheSrc, false);
|
|
83
|
+
addLayer(settings.customPricing, 'custom', true);
|
|
84
|
+
|
|
85
|
+
return Array.from(merged.values()).sort((a, b) => {
|
|
86
|
+
if (a.isCustom !== b.isCustom) return a.isCustom ? -1 : 1;
|
|
87
|
+
return a.label.localeCompare(b.label);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function computeBucketUsdServer(row, settings = {}) {
|
|
92
|
+
return computeBucketCostParts(row, settings).usd;
|
|
37
93
|
}
|
|
38
94
|
|
|
39
95
|
export async function syncServerPricingCache(
|
|
@@ -703,12 +759,44 @@ export async function buildBoardPayload(
|
|
|
703
759
|
const trendDaysCount = Math.max(1, Math.min(45, Math.floor((trendEndDayStartMs - trendStartMs) / DAY_MS) + 1));
|
|
704
760
|
const trendEndMs = trendStartMs + trendDaysCount * DAY_MS - 1;
|
|
705
761
|
|
|
762
|
+
// Previous comparison window of equal duration for period-over-period delta badge
|
|
763
|
+
let prevStartMs = win.startMs - DAY_MS;
|
|
764
|
+
let prevEndMs = win.startMs - 1;
|
|
765
|
+
let comparisonLabelKey = 'yesterday';
|
|
766
|
+
if (win.period === 'today') {
|
|
767
|
+
prevStartMs = todayStartMs - DAY_MS;
|
|
768
|
+
prevEndMs = todayStartMs - 1;
|
|
769
|
+
comparisonLabelKey = 'yesterday';
|
|
770
|
+
} else if (win.period === 'yesterday') {
|
|
771
|
+
prevStartMs = win.startMs - DAY_MS;
|
|
772
|
+
prevEndMs = win.startMs - 1;
|
|
773
|
+
comparisonLabelKey = 'prevDay';
|
|
774
|
+
} else if (win.period === '7d') {
|
|
775
|
+
prevStartMs = win.startMs - 7 * DAY_MS;
|
|
776
|
+
prevEndMs = win.startMs - 1;
|
|
777
|
+
comparisonLabelKey = 'prev7d';
|
|
778
|
+
} else if (win.period === '30d') {
|
|
779
|
+
prevStartMs = win.startMs - 30 * DAY_MS;
|
|
780
|
+
prevEndMs = win.startMs - 1;
|
|
781
|
+
comparisonLabelKey = 'prev30d';
|
|
782
|
+
} else if (win.period === 'cycle') {
|
|
783
|
+
const span = Math.max(DAY_MS, win.endMs - win.startMs + 1);
|
|
784
|
+
prevStartMs = win.startMs - span;
|
|
785
|
+
prevEndMs = win.startMs - 1;
|
|
786
|
+
comparisonLabelKey = 'prevCycle';
|
|
787
|
+
} else {
|
|
788
|
+
const span = Math.max(DAY_MS, (win.days || 1) * DAY_MS);
|
|
789
|
+
prevStartMs = win.startMs - span;
|
|
790
|
+
prevEndMs = win.startMs - 1;
|
|
791
|
+
comparisonLabelKey = 'prevRange';
|
|
792
|
+
}
|
|
793
|
+
|
|
706
794
|
const membersRaw = await db.all(
|
|
707
795
|
'SELECT id, name, role, color, quota_pct, expected_email_hash, last_account_status, daily_cap_usd, weekly_cap_usd, cycle_cap_usd, active, created_at, last_seen_at, last_client_kind, last_client_version FROM members WHERE active = 1 ORDER BY created_at ASC',
|
|
708
796
|
);
|
|
709
797
|
const quotasMap = resolveFamilyQuotas(membersRaw);
|
|
710
798
|
|
|
711
|
-
const queryStart = Math.min(win.startMs, trendStartMs, cycleInfo.cycleStartMs, now - 24 * HOUR_MS);
|
|
799
|
+
const queryStart = Math.min(win.startMs, trendStartMs, cycleInfo.cycleStartMs, prevStartMs, now - 24 * HOUR_MS);
|
|
712
800
|
const buckets = await db.all(
|
|
713
801
|
'SELECT member_id, device_id, bucket_hour_ms, source, model, input_tokens, output_tokens, cache_read_tokens, thinking_tokens, steps, est_usd FROM usage_buckets WHERE bucket_hour_ms >= ?',
|
|
714
802
|
[queryStart],
|
|
@@ -721,9 +809,13 @@ export async function buildBoardPayload(
|
|
|
721
809
|
name: m.name,
|
|
722
810
|
color: m.color || '#2383e2',
|
|
723
811
|
usedUsd: 0,
|
|
812
|
+
inputUsd: 0,
|
|
813
|
+
outputUsd: 0,
|
|
814
|
+
cacheReadUsd: 0,
|
|
724
815
|
inputTokens: 0,
|
|
725
816
|
outputTokens: 0,
|
|
726
817
|
cacheReadTokens: 0,
|
|
818
|
+
thinkingTokens: 0,
|
|
727
819
|
cacheSavedUsd: 0,
|
|
728
820
|
usedTokens: 0,
|
|
729
821
|
steps: 0,
|
|
@@ -732,6 +824,7 @@ export async function buildBoardPayload(
|
|
|
732
824
|
sources: { desktop: 0, cli: 0 },
|
|
733
825
|
lastSeenAt: m.last_seen_at || null,
|
|
734
826
|
lastClientKind: m.last_client_kind || null,
|
|
827
|
+
lastClientVersion: m.last_client_version || null,
|
|
735
828
|
});
|
|
736
829
|
}
|
|
737
830
|
|
|
@@ -741,12 +834,12 @@ export async function buildBoardPayload(
|
|
|
741
834
|
if (win.period === 'yesterday' || (win.period === 'custom' && win.days === 1)) {
|
|
742
835
|
for (let i = 0; i < 24; i++) {
|
|
743
836
|
const h = win.startMs + i * HOUR_MS;
|
|
744
|
-
hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0 });
|
|
837
|
+
hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0, _members: new Map() });
|
|
745
838
|
}
|
|
746
839
|
} else {
|
|
747
840
|
for (let i = 23; i >= 0; i--) {
|
|
748
841
|
const h = curHour - i * HOUR_MS;
|
|
749
|
-
hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0 });
|
|
842
|
+
hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0, _members: new Map() });
|
|
750
843
|
}
|
|
751
844
|
}
|
|
752
845
|
|
|
@@ -756,20 +849,35 @@ export async function buildBoardPayload(
|
|
|
756
849
|
const dayUtc = trendStartMs + i * DAY_MS;
|
|
757
850
|
const localD = new Date(dayUtc + offsetMs);
|
|
758
851
|
const label = `${String(localD.getUTCMonth() + 1).padStart(2, '0')}-${String(localD.getUTCDate()).padStart(2, '0')}`;
|
|
759
|
-
dailyTrendMap.set(dayUtc, {
|
|
852
|
+
dailyTrendMap.set(dayUtc, {
|
|
853
|
+
dayStartMs: dayUtc,
|
|
854
|
+
label,
|
|
855
|
+
usd: 0,
|
|
856
|
+
tokens: 0,
|
|
857
|
+
steps: 0,
|
|
858
|
+
desktopUsd: 0,
|
|
859
|
+
cliUsd: 0,
|
|
860
|
+
_members: new Map(),
|
|
861
|
+
});
|
|
760
862
|
}
|
|
761
863
|
|
|
762
864
|
const globalModels = new Map();
|
|
763
865
|
let totalUsd = 0;
|
|
866
|
+
let totalInputUsd = 0;
|
|
867
|
+
let totalOutputUsd = 0;
|
|
868
|
+
let totalCacheReadUsd = 0;
|
|
764
869
|
let totalInputTokens = 0;
|
|
765
870
|
let totalOutputTokens = 0;
|
|
766
871
|
let totalCacheReadTokens = 0;
|
|
872
|
+
let totalThinkingTokens = 0;
|
|
767
873
|
let totalCacheSavedUsd = 0;
|
|
768
874
|
let totalTokens = 0;
|
|
769
875
|
let totalSteps = 0;
|
|
770
876
|
let totalDesktopUsd = 0;
|
|
771
877
|
let totalCliUsd = 0;
|
|
772
878
|
let cycleUsedUsd = 0;
|
|
879
|
+
let prevTotalUsd = 0;
|
|
880
|
+
let prevTotalTokens = 0;
|
|
773
881
|
|
|
774
882
|
const rateLookupOpts = {
|
|
775
883
|
customPricing: settings.customPricing || null,
|
|
@@ -777,6 +885,20 @@ export async function buildBoardPayload(
|
|
|
777
885
|
useLocalSources: false,
|
|
778
886
|
};
|
|
779
887
|
|
|
888
|
+
const addSlotMember = (slot, memberObj, usd, tok) => {
|
|
889
|
+
if (!slot || !memberObj || (usd <= 0 && tok <= 0)) return;
|
|
890
|
+
const prev = slot._members.get(memberObj.id) || {
|
|
891
|
+
id: memberObj.id,
|
|
892
|
+
name: memberObj.name,
|
|
893
|
+
color: memberObj.color,
|
|
894
|
+
usd: 0,
|
|
895
|
+
tokens: 0,
|
|
896
|
+
};
|
|
897
|
+
prev.usd = roundUsd(prev.usd + usd);
|
|
898
|
+
prev.tokens += tok;
|
|
899
|
+
slot._members.set(memberObj.id, prev);
|
|
900
|
+
};
|
|
901
|
+
|
|
780
902
|
for (const b of buckets || []) {
|
|
781
903
|
const src = b.source === 'cli' ? 'cli' : 'desktop';
|
|
782
904
|
if (sourceFilter !== 'all' && src !== sourceFilter) continue;
|
|
@@ -784,17 +906,21 @@ export async function buildBoardPayload(
|
|
|
784
906
|
const inp = Number(b.input_tokens) || 0;
|
|
785
907
|
const out = Number(b.output_tokens) || 0;
|
|
786
908
|
const cached = Number(b.cache_read_tokens) || 0;
|
|
909
|
+
const thinking = Number(b.thinking_tokens) || 0;
|
|
787
910
|
const tok = inp + out + cached;
|
|
788
|
-
const
|
|
911
|
+
const parts = computeBucketCostParts(b, settings);
|
|
912
|
+
const usd = parts.usd;
|
|
789
913
|
const steps = Number(b.steps) || 0;
|
|
790
914
|
const modName = normalizeModelName(b.model);
|
|
791
915
|
const savedUsd = estimateCacheSavedUsd({ model: modName, cacheRead: cached, ...rateLookupOpts });
|
|
916
|
+
const memberObj = byMember.get(b.member_id) || null;
|
|
792
917
|
|
|
793
918
|
if (hourlyMap.has(b.bucket_hour_ms)) {
|
|
794
919
|
const slot = hourlyMap.get(b.bucket_hour_ms);
|
|
795
920
|
slot.usd = roundUsd(slot.usd + usd);
|
|
796
921
|
slot.tokens += tok;
|
|
797
922
|
slot.steps += steps;
|
|
923
|
+
addSlotMember(slot, memberObj, usd, tok);
|
|
798
924
|
}
|
|
799
925
|
|
|
800
926
|
if (b.bucket_hour_ms >= trendStartMs && b.bucket_hour_ms <= trendEndMs) {
|
|
@@ -807,41 +933,75 @@ export async function buildBoardPayload(
|
|
|
807
933
|
dSlot.steps += steps;
|
|
808
934
|
if (src === 'cli') dSlot.cliUsd = roundUsd(dSlot.cliUsd + usd);
|
|
809
935
|
else dSlot.desktopUsd = roundUsd(dSlot.desktopUsd + usd);
|
|
936
|
+
addSlotMember(dSlot, memberObj, usd, tok);
|
|
810
937
|
}
|
|
811
938
|
}
|
|
812
939
|
|
|
813
|
-
if (b.bucket_hour_ms >= cycleInfo.cycleStartMs &&
|
|
940
|
+
if (b.bucket_hour_ms >= cycleInfo.cycleStartMs && memberObj) {
|
|
814
941
|
cycleUsedUsd += usd;
|
|
815
942
|
}
|
|
816
943
|
|
|
944
|
+
if (b.bucket_hour_ms >= prevStartMs && b.bucket_hour_ms <= prevEndMs && memberObj) {
|
|
945
|
+
prevTotalUsd += usd;
|
|
946
|
+
prevTotalTokens += tok;
|
|
947
|
+
}
|
|
948
|
+
|
|
817
949
|
if (b.bucket_hour_ms < win.startMs || b.bucket_hour_ms > win.endMs) continue;
|
|
818
950
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
951
|
+
if (!memberObj) continue;
|
|
952
|
+
|
|
953
|
+
memberObj.usedUsd += usd;
|
|
954
|
+
memberObj.inputUsd += parts.inputUsd;
|
|
955
|
+
memberObj.outputUsd += parts.outputUsd;
|
|
956
|
+
memberObj.cacheReadUsd += parts.cacheReadUsd;
|
|
957
|
+
memberObj.inputTokens += inp;
|
|
958
|
+
memberObj.outputTokens += out;
|
|
959
|
+
memberObj.cacheReadTokens += cached;
|
|
960
|
+
memberObj.thinkingTokens += thinking;
|
|
961
|
+
memberObj.cacheSavedUsd += savedUsd;
|
|
962
|
+
memberObj.usedTokens += tok;
|
|
963
|
+
memberObj.steps += steps;
|
|
964
|
+
memberObj.devices.add(b.device_id);
|
|
830
965
|
if (src === 'cli') {
|
|
831
|
-
|
|
966
|
+
memberObj.sources.cli += usd;
|
|
832
967
|
totalCliUsd += usd;
|
|
833
968
|
} else {
|
|
834
|
-
|
|
969
|
+
memberObj.sources.desktop += usd;
|
|
835
970
|
totalDesktopUsd += usd;
|
|
836
971
|
}
|
|
837
972
|
|
|
838
|
-
|
|
839
|
-
|
|
973
|
+
memberObj.models.set(modName, roundUsd((memberObj.models.get(modName) || 0) + usd));
|
|
974
|
+
const gm = globalModels.get(modName) || {
|
|
975
|
+
model: modName,
|
|
976
|
+
usd: 0,
|
|
977
|
+
tokens: 0,
|
|
978
|
+
inputTokens: 0,
|
|
979
|
+
outputTokens: 0,
|
|
980
|
+
cacheReadTokens: 0,
|
|
981
|
+
thinkingTokens: 0,
|
|
982
|
+
steps: 0,
|
|
983
|
+
isEstimated: !isKnownModelRate(modName, rateLookupOpts),
|
|
984
|
+
rate: parts.rate
|
|
985
|
+
? { input: parts.rate.input, output: parts.rate.output, cacheRead: parts.rate.cacheRead, source: parts.rate.source }
|
|
986
|
+
: null,
|
|
987
|
+
};
|
|
988
|
+
gm.usd = roundUsd(gm.usd + usd);
|
|
989
|
+
gm.tokens += tok;
|
|
990
|
+
gm.inputTokens += inp;
|
|
991
|
+
gm.outputTokens += out;
|
|
992
|
+
gm.cacheReadTokens += cached;
|
|
993
|
+
gm.thinkingTokens += thinking;
|
|
994
|
+
gm.steps += steps;
|
|
995
|
+
globalModels.set(modName, gm);
|
|
840
996
|
|
|
841
997
|
totalUsd += usd;
|
|
998
|
+
totalInputUsd += parts.inputUsd;
|
|
999
|
+
totalOutputUsd += parts.outputUsd;
|
|
1000
|
+
totalCacheReadUsd += parts.cacheReadUsd;
|
|
842
1001
|
totalInputTokens += inp;
|
|
843
1002
|
totalOutputTokens += out;
|
|
844
1003
|
totalCacheReadTokens += cached;
|
|
1004
|
+
totalThinkingTokens += thinking;
|
|
845
1005
|
totalCacheSavedUsd += savedUsd;
|
|
846
1006
|
totalTokens += tok;
|
|
847
1007
|
totalSteps += steps;
|
|
@@ -915,10 +1075,14 @@ export async function buildBoardPayload(
|
|
|
915
1075
|
role: raw.role === 'manager' ? 'manager' : 'member',
|
|
916
1076
|
color: raw.color || '#2383e2',
|
|
917
1077
|
usedUsd,
|
|
1078
|
+
inputUsd: roundUsd(agg.inputUsd),
|
|
1079
|
+
outputUsd: roundUsd(agg.outputUsd),
|
|
1080
|
+
cacheReadUsd: roundUsd(agg.cacheReadUsd),
|
|
918
1081
|
usedTokens: agg.usedTokens,
|
|
919
1082
|
inputTokens: agg.inputTokens,
|
|
920
1083
|
outputTokens: agg.outputTokens,
|
|
921
1084
|
cacheReadTokens: agg.cacheReadTokens,
|
|
1085
|
+
thinkingTokens: agg.thinkingTokens,
|
|
922
1086
|
cacheHitPct,
|
|
923
1087
|
cacheSavedUsd: roundUsd(agg.cacheSavedUsd),
|
|
924
1088
|
estPlanSplit,
|
|
@@ -944,6 +1108,7 @@ export async function buildBoardPayload(
|
|
|
944
1108
|
topModels,
|
|
945
1109
|
lastSeenAt: agg.lastSeenAt,
|
|
946
1110
|
lastClientKind: agg.lastClientKind,
|
|
1111
|
+
lastClientVersion: agg.lastClientVersion,
|
|
947
1112
|
online: !isOffline,
|
|
948
1113
|
accountBound: Boolean(raw.expected_email_hash),
|
|
949
1114
|
accountStatus: raw.expected_email_hash ? raw.last_account_status || 'unknown' : 'unbound',
|
|
@@ -959,22 +1124,27 @@ export async function buildBoardPayload(
|
|
|
959
1124
|
const overQuotaCount = members.filter((m) => m.quotaStatus === 'over').length;
|
|
960
1125
|
const warnQuotaCount = members.filter((m) => m.quotaStatus === 'warn').length;
|
|
961
1126
|
const onlineCount = members.filter((m) => m.online).length;
|
|
1127
|
+
const activeUsedCount = members.filter((m) => m.usedUsd > 0).length;
|
|
1128
|
+
const isLowSampleRelative = quotaMode === 'relative' && (totalUsd < 5.0 || activeUsedCount <= 1);
|
|
962
1129
|
|
|
963
|
-
const modelBreakdown = Array.from(globalModels.
|
|
964
|
-
.map((
|
|
965
|
-
|
|
966
|
-
usd,
|
|
967
|
-
isEstimated: !isKnownModelRate(model, rateLookupOpts),
|
|
968
|
-
sharePct: totalUsd > 0 ? Math.round((usd / totalUsd) * 1000) / 10 : 0,
|
|
1130
|
+
const modelBreakdown = Array.from(globalModels.values())
|
|
1131
|
+
.map((m) => ({
|
|
1132
|
+
...m,
|
|
1133
|
+
sharePct: totalUsd > 0 ? Math.round((m.usd / totalUsd) * 1000) / 10 : 0,
|
|
969
1134
|
}))
|
|
970
1135
|
.sort((a, b) => b.usd - a.usd);
|
|
971
1136
|
|
|
972
1137
|
const globalCacheDenom = totalInputTokens + totalCacheReadTokens;
|
|
973
1138
|
const globalCacheHitPct = globalCacheDenom > 0 ? Math.round((totalCacheReadTokens / globalCacheDenom) * 1000) / 10 : 0;
|
|
1139
|
+
const thinkingShareOfOutPct = totalOutputTokens > 0 ? roundPct((totalThinkingTokens / totalOutputTokens) * 100) : 0;
|
|
974
1140
|
const globalSrcTotal = totalDesktopUsd + totalCliUsd;
|
|
975
1141
|
const desktopSharePct = globalSrcTotal > 0 ? roundPct((totalDesktopUsd / globalSrcTotal) * 100) : 0;
|
|
976
1142
|
const cliSharePct = globalSrcTotal > 0 ? roundPct(Math.max(0, 100 - desktopSharePct)) : 0;
|
|
977
1143
|
|
|
1144
|
+
prevTotalUsd = roundUsd(prevTotalUsd);
|
|
1145
|
+
const deltaUsdPct = prevTotalUsd > 0 ? roundPct(((totalUsd - prevTotalUsd) / prevTotalUsd) * 100) : null;
|
|
1146
|
+
const deltaTokensPct = prevTotalTokens > 0 ? roundPct(((totalTokens - prevTotalTokens) / prevTotalTokens) * 100) : null;
|
|
1147
|
+
|
|
978
1148
|
const windowDaysForAvg =
|
|
979
1149
|
win.period === 'today' ? 1 : win.period === '7d' ? 7 : win.period === '30d' ? 30 : Math.max(1, cycleInfo.daysElapsedExact);
|
|
980
1150
|
const dailyAvgUsd = roundUsd(totalUsd / windowDaysForAvg);
|
|
@@ -993,14 +1163,20 @@ export async function buildBoardPayload(
|
|
|
993
1163
|
? roundPct(((poolTotalCredits - Math.max(0, Number(creditsReconciliation.latest.balance))) / poolTotalCredits) * 100)
|
|
994
1164
|
: null;
|
|
995
1165
|
|
|
996
|
-
const
|
|
1166
|
+
const finalizeSlotMembers = (slot) => {
|
|
1167
|
+
const byMem = Array.from(slot._members.values()).sort((a, b) => b.usd - a.usd || b.tokens - a.tokens);
|
|
1168
|
+
const { _members, ...rest } = slot;
|
|
1169
|
+
return { ...rest, byMember: byMem };
|
|
1170
|
+
};
|
|
1171
|
+
|
|
1172
|
+
const hourlyList = Array.from(hourlyMap.values()).map(finalizeSlotMembers);
|
|
997
1173
|
const hourlyTotalUsd = hourlyList.reduce((s, h) => s + (Number(h.usd) || 0), 0);
|
|
998
1174
|
for (const h of hourlyList) {
|
|
999
1175
|
h.sharePct = hourlyTotalUsd > 0 ? roundPct(((Number(h.usd) || 0) / hourlyTotalUsd) * 100) : 0;
|
|
1000
1176
|
h.poolPct = poolTotalCredits > 0 ? roundPct((((Number(h.usd) || 0) * creditsPerUsd) / poolTotalCredits) * 100) : null;
|
|
1001
1177
|
}
|
|
1002
1178
|
|
|
1003
|
-
const dailyTrendList = Array.from(dailyTrendMap.values());
|
|
1179
|
+
const dailyTrendList = Array.from(dailyTrendMap.values()).map(finalizeSlotMembers);
|
|
1004
1180
|
const trendTotalUsd = dailyTrendList.reduce((s, d) => s + (Number(d.usd) || 0), 0);
|
|
1005
1181
|
for (const d of dailyTrendList) {
|
|
1006
1182
|
d.sharePct = trendTotalUsd > 0 ? roundPct(((Number(d.usd) || 0) / trendTotalUsd) * 100) : 0;
|
|
@@ -1060,16 +1236,30 @@ export async function buildBoardPayload(
|
|
|
1060
1236
|
memberCount: members.length,
|
|
1061
1237
|
maxMembers: MAX_FAMILY_MEMBERS,
|
|
1062
1238
|
onlineCount,
|
|
1239
|
+
activeUsedCount,
|
|
1240
|
+
isLowSampleRelative,
|
|
1063
1241
|
warnQuotaCount,
|
|
1064
1242
|
overQuotaCount,
|
|
1065
1243
|
warnCapCount,
|
|
1066
1244
|
overCapCount,
|
|
1067
1245
|
integrityFlags7d: (integrityRows || []).length,
|
|
1068
1246
|
},
|
|
1247
|
+
comparison: {
|
|
1248
|
+
prevTotalUsd,
|
|
1249
|
+
prevTotalTokens,
|
|
1250
|
+
deltaUsdPct,
|
|
1251
|
+
deltaTokensPct,
|
|
1252
|
+
labelKey: comparisonLabelKey,
|
|
1253
|
+
},
|
|
1069
1254
|
tokensBreakdown: {
|
|
1070
1255
|
inputTokens: totalInputTokens,
|
|
1071
1256
|
outputTokens: totalOutputTokens,
|
|
1072
1257
|
cacheReadTokens: totalCacheReadTokens,
|
|
1258
|
+
thinkingTokens: totalThinkingTokens,
|
|
1259
|
+
thinkingShareOfOutPct,
|
|
1260
|
+
inputUsd: roundUsd(totalInputUsd),
|
|
1261
|
+
outputUsd: roundUsd(totalOutputUsd),
|
|
1262
|
+
cacheReadUsd: roundUsd(totalCacheReadUsd),
|
|
1073
1263
|
totalTokens,
|
|
1074
1264
|
cacheHitPct: globalCacheHitPct,
|
|
1075
1265
|
cacheSavedUsd: totalCacheSavedUsd,
|
|
@@ -1242,9 +1432,13 @@ export async function buildMemberDetailPayload(
|
|
|
1242
1432
|
const deviceMap = new Map();
|
|
1243
1433
|
const sources = { desktopUsd: 0, cliUsd: 0 };
|
|
1244
1434
|
let periodUsd = 0;
|
|
1435
|
+
let periodInputUsd = 0;
|
|
1436
|
+
let periodOutputUsd = 0;
|
|
1437
|
+
let periodCacheReadUsd = 0;
|
|
1245
1438
|
let periodInputTokens = 0;
|
|
1246
1439
|
let periodOutputTokens = 0;
|
|
1247
1440
|
let periodCacheReadTokens = 0;
|
|
1441
|
+
let periodThinkingTokens = 0;
|
|
1248
1442
|
let periodCacheSavedUsd = 0;
|
|
1249
1443
|
let periodTokens = 0;
|
|
1250
1444
|
let periodSteps = 0;
|
|
@@ -1259,8 +1453,10 @@ export async function buildMemberDetailPayload(
|
|
|
1259
1453
|
const inp = Number(b.input_tokens) || 0;
|
|
1260
1454
|
const out = Number(b.output_tokens) || 0;
|
|
1261
1455
|
const cached = Number(b.cache_read_tokens) || 0;
|
|
1456
|
+
const thinking = Number(b.thinking_tokens) || 0;
|
|
1262
1457
|
const tok = inp + out + cached;
|
|
1263
|
-
const
|
|
1458
|
+
const parts = computeBucketCostParts(b, settings);
|
|
1459
|
+
const usd = parts.usd;
|
|
1264
1460
|
const steps = Number(b.steps) || 0;
|
|
1265
1461
|
const modName = normalizeModelName(b.model);
|
|
1266
1462
|
const savedUsd = estimateCacheSavedUsd({ model: modName, cacheRead: cached, ...detailRateOpts });
|
|
@@ -1307,9 +1503,13 @@ export async function buildMemberDetailPayload(
|
|
|
1307
1503
|
deviceMap.set(devId, dev);
|
|
1308
1504
|
|
|
1309
1505
|
periodUsd = roundUsd(periodUsd + usd);
|
|
1506
|
+
periodInputUsd += parts.inputUsd;
|
|
1507
|
+
periodOutputUsd += parts.outputUsd;
|
|
1508
|
+
periodCacheReadUsd += parts.cacheReadUsd;
|
|
1310
1509
|
periodInputTokens += inp;
|
|
1311
1510
|
periodOutputTokens += out;
|
|
1312
1511
|
periodCacheReadTokens += cached;
|
|
1512
|
+
periodThinkingTokens += thinking;
|
|
1313
1513
|
periodCacheSavedUsd = roundUsd(periodCacheSavedUsd + savedUsd);
|
|
1314
1514
|
periodTokens += tok;
|
|
1315
1515
|
periodSteps += steps;
|
|
@@ -1318,11 +1518,22 @@ export async function buildMemberDetailPayload(
|
|
|
1318
1518
|
model: modName,
|
|
1319
1519
|
usd: 0,
|
|
1320
1520
|
tokens: 0,
|
|
1521
|
+
inputTokens: 0,
|
|
1522
|
+
outputTokens: 0,
|
|
1523
|
+
cacheReadTokens: 0,
|
|
1524
|
+
thinkingTokens: 0,
|
|
1321
1525
|
steps: 0,
|
|
1322
1526
|
isEstimated: !isKnownModelRate(modName, detailRateOpts),
|
|
1527
|
+
rate: parts.rate
|
|
1528
|
+
? { input: parts.rate.input, output: parts.rate.output, cacheRead: parts.rate.cacheRead, source: parts.rate.source }
|
|
1529
|
+
: null,
|
|
1323
1530
|
};
|
|
1324
1531
|
prev.usd = roundUsd(prev.usd + usd);
|
|
1325
1532
|
prev.tokens += tok;
|
|
1533
|
+
prev.inputTokens += inp;
|
|
1534
|
+
prev.outputTokens += out;
|
|
1535
|
+
prev.cacheReadTokens += cached;
|
|
1536
|
+
prev.thinkingTokens += thinking;
|
|
1326
1537
|
prev.steps += steps;
|
|
1327
1538
|
models.set(modName, prev);
|
|
1328
1539
|
}
|
|
@@ -1405,6 +1616,11 @@ export async function buildMemberDetailPayload(
|
|
|
1405
1616
|
inputTokens: periodInputTokens,
|
|
1406
1617
|
outputTokens: periodOutputTokens,
|
|
1407
1618
|
cacheReadTokens: periodCacheReadTokens,
|
|
1619
|
+
thinkingTokens: periodThinkingTokens,
|
|
1620
|
+
thinkingShareOfOutPct: periodOutputTokens > 0 ? roundPct((periodThinkingTokens / periodOutputTokens) * 100) : 0,
|
|
1621
|
+
inputUsd: roundUsd(periodInputUsd),
|
|
1622
|
+
outputUsd: roundUsd(periodOutputUsd),
|
|
1623
|
+
cacheReadUsd: roundUsd(periodCacheReadUsd),
|
|
1408
1624
|
totalTokens: periodTokens,
|
|
1409
1625
|
cacheHitPct,
|
|
1410
1626
|
cacheSavedUsd: periodCacheSavedUsd,
|