From 6e02db4bd7a9a74b64acd0a58437114bdee4e5a1 Mon Sep 17 00:00:00 2001 From: reanon <> Date: Sun, 20 Jul 2025 04:03:11 +0200 Subject: [PATCH] another firebase fix? --- src/shared/users/user-store.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/shared/users/user-store.ts b/src/shared/users/user-store.ts index 43e58a5..276d376 100644 --- a/src/shared/users/user-store.ts +++ b/src/shared/users/user-store.ts @@ -238,13 +238,23 @@ export function upsertUser(user: UserUpdate) { if (updates.tokenCounts) { for (const family of MODEL_FAMILIES) { - updates.tokenCounts[family] ??= { input: 0, output: 0 }; + // Preserve existing legacy_total when creating default token counts + const existingCounts = existing.tokenCounts[family]; + const defaultCounts: { input: number; output: number; legacy_total?: number } = { input: 0, output: 0 }; + if (existingCounts?.legacy_total !== undefined) { + defaultCounts.legacy_total = existingCounts.legacy_total; + } + updates.tokenCounts[family] ??= defaultCounts; + // The property is now guaranteed to be an object, so the 'number' check is removed. // Defaulting individual fields if they are missing. const counts = updates.tokenCounts[family]!; // Should not be undefined here counts.input ??= 0; counts.output ??= 0; - // legacy_total is optional and not defaulted here if missing + // Preserve legacy_total from existing data if not already set in updates + if (counts.legacy_total === undefined && existingCounts?.legacy_total !== undefined) { + counts.legacy_total = existingCounts.legacy_total; + } } } if (updates.tokenLimits) {