TypeScript
fix(usage): label a proxied Claude account by its e-mail, and drop .json from hub rows (#28065)
Julius Marminge58cf69c835bc
diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.tsindex 23447ae7930..72a17323386 100644--- a/packages/shared/src/usageLimits.test.ts+++ b/packages/shared/src/usageLimits.test.ts@@ -672,6 +672,57 @@ describe("pools", () => {expect(accounts[0]?.displayName).toBe("claude-team-seat");});+ it("labels a default-named instance merged with a hub account by e-mail, keeping a chosen name", () => {+ const native = provider({+ driver: claude,+ instanceId: ProviderInstanceId.make("claudeAgent"),+ displayName: "Claude",+ auth: { status: "authenticated", email: "[email protected]" },+ usageLimits: { checkedAt, windows: [window] },+ });+ const named = provider({+ driver: claude,+ instanceId: ProviderInstanceId.make("claude-work"),+ displayName: "Work",+ auth: { status: "authenticated", email: "[email protected]" },+ usageLimits: { checkedAt, windows: [window] },+ });+ const hubAccount = (email: string) => ({+ id: `claude-${email}.json`,+ driver: claude,+ email,+ usageLimits: { checkedAt, windows: [window] },+ });+ const input = new Map([+ [+ EnvironmentId.make("env-a"),+ {+ ...laptop,+ serverConfig: {+ providers: [native, named],+ usageLimitSources: [+ {+ ...source,+ accounts: [+ hubAccount("[email protected]"),+ hubAccount("[email protected]"),+ hubAccount("[email protected]"),+ ],+ },+ ],+ },+ },+ ],+ ]);+ expect(+ collectLimitAccounts(input).map(({ displayName, email }) => ({ displayName, email })),+ ).toEqual([+ { displayName: null, email: "[email protected]" },+ { displayName: "Work", email: "[email protected]" },+ { displayName: null, email: "[email protected]" },+ ]);+ });+it("pools Codex windows by kind, whichever slot reported them", () => {const codexAccount = (id: string, slots: readonly string[]) => ({id,@@ -1138,6 +1189,28 @@ describe("/usage-limits", () => {expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);});+ it("labels hub rows by account id without .json", () => {+ const hub = [+ {+ ...sources[0]!,+ label: "CLIProxyAPI",+ accounts: [+ {+ id: "[email protected]",+ driver: selected.driver,+ email: "[email protected]",+ usageLimits: limits,+ },+ { id: "team-seat.json", driver: selected.driver, usageLimits: limits },+ ],+ },+ ];+ const report = collectProviderUsageLimits(selected.instanceId, [selected], hub, now);+ expect(+ report?.accounts.filter((account) => account.sourceLabel).map((account) => account.label),+ ).toEqual(["CLIProxyAPI · [email protected]", "CLIProxyAPI · team-seat"]);+ });+it("keeps unidentified custom instances separate, filtering by driver", () => {const report = collectProviderUsageLimits(selected.instanceId,diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.tsindex bf7dfea4e04..869f676acae 100644--- a/packages/shared/src/usageLimits.ts+++ b/packages/shared/src/usageLimits.ts@@ -9,6 +9,7 @@ import {type ProviderConsumeResetCreditInput,type ServerProviderSlashCommand,isProviderAvailable,+ PROVIDER_DISPLAY_NAMES,type ServerProvider,type ServerProviderUsageLimits,type ServerProviderUsageWindow,@@ -232,6 +233,8 @@ function accountKeyResolver(reports: Iterable<AccountReport>) {};}+const hubAccountName = (account: { readonly id: string }) => account.id.replace(/\.json$/i, "");+export interface LimitAccount {readonly key: string;readonly driver: ServerProvider["driver"];@@ -297,9 +300,18 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl];const winner = fresher ? next : previous;const creditSource = creditSources.get(key);+ // A hub e-mail names the account better than the driver's default instance name.+ const hubEmail = [previous, next].some(+ (account) => account.environments.length === 0 && account.email,+ );+ const displayName = previous.displayName ?? next.displayName;accounts.set(key, {...previous,- displayName: previous.displayName ?? next.displayName,+ displayName:+ hubEmail &&+ displayName?.toLowerCase() === PROVIDER_DISPLAY_NAMES[previous.driver]?.toLowerCase()+ ? null+ : displayName,plan: planSources.get(key)?.plan ?? previous.plan ?? next.plan,accentColor: previous.accentColor ?? next.accentColor,environments,@@ -353,7 +365,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonlmerge(resolveKey(account) ?? key, {key,driver: account.driver,- displayName: account.email ? null : account.id.replace(/\.json$/i, ""),+ displayName: account.email ? null : hubAccountName(account),email: account.email,plan: account.plan,accentColor: undefined,@@ -843,7 +855,7 @@ export function collectProviderUsageLimits(accounts.push({id: `${source.id}:${account.id}`,driver: account.driver,- label: `${source.label} · ${account.id}`,+ label: `${source.label} · ${hubAccountName(account)}`,sourceLabel: "CLI Proxy",...(account.usageLimits.resetCredits?.nextCreditId? {