type RecordValue = Record; export function record(value: unknown): RecordValue { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Invalid upstream schema'); return value as RecordValue; } function count(value: unknown): number | null { return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : null; } function identifier(value: unknown): string | null { return typeof value === 'string' && /^[a-zA-Z0-9_./:@()+ -]{1,180}$/.test(value) && !value.includes('@') && !/^(sk-|Bearer )/i.test(value) ? value : null; } function metrics(row: RecordValue, prefix = '') { const key = (name: string) => prefix ? prefix + name[0].toUpperCase() + name.slice(1) : name; return { requests: count(row[key('requests')]), inputTokens: count(row[key('promptTokens')]), outputTokens: count(row[key('completionTokens')]), cacheReadTokens: count(row[key('cachedTokens')]), cacheWriteTokens: null, reasoningTokens: null, errors: null, }; } export function normalizeStats(value: unknown) { const stats = record(value); if (count(stats.totalRequests) === null) throw new Error('Invalid stats'); const rows = (key: string) => { const entries = Object.entries(record(stats[key])); if (entries.length > 2000) throw new Error('Too many aggregates'); return entries; }; return { totals: metrics(stats, 'total'), providers: rows('byProvider').map(([id, row]) => ({provider: identifier(id), ...metrics(record(row))})), models: rows('byModel').map(([, row]) => { const r = record(row); return {model: identifier(r.rawModel), provider: identifier(r.provider), ...metrics(r)}; }), accounts: rows('byAccount').map(([, row]) => { const r = record(row); return { accountId: identifier(r.connectionId), model: identifier(r.rawModel), provider: identifier(r.provider), ...metrics(r), quota: {status: 'unknown' as const, remaining: null, resetAt: null, reason: 'upstream_quota_get_mutates_credentials'}, }; }), }; }