Skip to content

Commit d5c55e4

Browse files
feat(web): show the selected account's remaining quota on the model picker
After switching accounts the composer showed only the model and a two-letter badge, so neither the account nor how much quota it has left was visible. The picker trigger now shows the remaining percent of the account's tightest limit, and its tooltip names the account and the limit. Accounts that report no limits show nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3c2b347 commit d5c55e4

3 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎apps/web/src/components/chat/ProviderModelPicker.tsx‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getTriggerDisplayModelLabel,
1818
getTriggerDisplayModelName,
1919
} from "./providerIconUtils";
20+
import { tightestUsageWindow } from "@t3tools/client-runtime/state/limit-switch-targets";
2021
import { shouldShowInstanceBadge, type ProviderInstanceEntry } from "../../providerInstances";
2122
import {
2223
ComposerControl,
@@ -94,6 +95,11 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
9495
: triggerTitle;
9596
const showInstanceBadge =
9697
activeEntry !== null && shouldShowInstanceBadge(activeEntry, props.instanceEntries);
98+
// Where the selected account stands, so a switch is visible without opening Usage.
99+
const usageWindow =
100+
activeEntry !== null && props.triggerLabel === undefined
101+
? tightestUsageWindow(activeEntry.snapshot)
102+
: null;
97103

98104
const setIsMenuOpen = (open: boolean) => {
99105
props.onOpenChange?.(open);
@@ -187,9 +193,18 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
187193
const allModelNames = selectedEntries
188194
? selectedEntries.map((selection) => selection.label).join(", ") || "Choose models"
189195
: undefined;
196+
const accountTooltipPrefix =
197+
showInstanceBadge && activeEntry && !selectedEntries && props.triggerLabel === undefined
198+
? `${activeEntry.displayName} · `
199+
: "";
200+
const usageTooltipSuffix =
201+
usageWindow && !selectedEntries
202+
? ` · ${usageWindow.label} limit: ${Math.round(usageWindow.remainingPercent)}% left`
203+
: "";
204+
const triggerTooltipLabel = `${accountTooltipPrefix}${props.triggerLabel ?? allModelNames ?? triggerLabel}${usageTooltipSuffix}`;
190205
const triggerTooltipContent = shortcutLabel
191-
? `${props.triggerLabel ?? allModelNames ?? triggerLabel} · ${shortcutLabel}`
192-
: (props.triggerLabel ?? allModelNames ?? triggerLabel);
206+
? `${triggerTooltipLabel} · ${shortcutLabel}`
207+
: triggerTooltipLabel;
193208

194209
return (
195210
<Popover
@@ -274,6 +289,18 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
274289
</TooltipTrigger>
275290
<TooltipPopup side="top">{triggerTooltipContent}</TooltipPopup>
276291
</Tooltip>
292+
{usageWindow && !selectedEntries && !props.compact ? (
293+
<span
294+
className={cn(
295+
"shrink-0 tabular-nums",
296+
usageWindow.remainingPercent <= 10
297+
? "text-warning-foreground"
298+
: "text-muted-foreground",
299+
)}
300+
>
301+
{Math.round(usageWindow.remainingPercent)}%
302+
</span>
303+
) : null}
277304
{selectedModel?.isUnavailable && !selectedEntries && props.triggerLabel === undefined ? (
278305
<Badge variant="outline" size="sm">
279306
Unavailable

‎packages/client-runtime/src/state/limitSwitchTargets.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
} from "@t3tools/contracts";
77
import { describe, expect, it } from "vite-plus/test";
88

9-
import { listLimitSwitchTargets, suggestLimitSwitch } from "./limitSwitchTargets.ts";
9+
import {
10+
listLimitSwitchTargets,
11+
suggestLimitSwitch,
12+
tightestUsageWindow,
13+
} from "./limitSwitchTargets.ts";
1014

1115
const NOW = Date.parse("2026-10-10T12:00:00.000Z");
1216

@@ -181,3 +185,33 @@ describe("suggestLimitSwitch", () => {
181185
).toBeNull();
182186
});
183187
});
188+
189+
describe("tightestUsageWindow", () => {
190+
it("reads the window closest to running out", () => {
191+
expect(
192+
tightestUsageWindow({
193+
usageLimits: {
194+
checkedAt: "2026-10-10T11:59:00.000Z",
195+
windows: [
196+
{ id: "primary", kind: "session", label: "Session", usedPercent: 20 },
197+
{ id: "secondary", kind: "weekly", label: "Weekly", usedPercent: 63 },
198+
],
199+
},
200+
}),
201+
).toEqual({ label: "Weekly", remainingPercent: 37 });
202+
});
203+
204+
it("reports nothing for an account without usable windows", () => {
205+
expect(tightestUsageWindow({})).toBeNull();
206+
expect(
207+
tightestUsageWindow({
208+
usageLimits: {
209+
checkedAt: "2026-10-10T11:59:00.000Z",
210+
windows: [],
211+
unavailable: { reason: "probeFailed" },
212+
},
213+
}),
214+
).toBeNull();
215+
expect(tightestUsageWindow({ usageLimits: usage(100, "2026-10-10T11:00:00.000Z") })).toBeNull();
216+
});
217+
});

‎packages/client-runtime/src/state/limitSwitchTargets.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ function remainingPercent(provider: ServerProvider): number | null {
2121
return Math.max(0, 100 - Math.max(...usage.windows.map((window) => window.usedPercent)));
2222
}
2323

24+
/**
25+
* The window an account is closest to running out of, for a compact "where do I
26+
* stand" readout. Null when the account reports no windows, so the caller shows
27+
* nothing rather than a full bar.
28+
*/
29+
export function tightestUsageWindow(
30+
provider: Pick<ServerProvider, "usageLimits">,
31+
): { readonly label: string; readonly remainingPercent: number } | null {
32+
const usage = provider.usageLimits;
33+
if (!usage || usage.unavailable) return null;
34+
// Judged at the read itself, so rendering stays pure; the next probe replaces it.
35+
const readAtMs = Date.parse(usage.checkedAt);
36+
const tightest = usage.windows
37+
// A window whose reset has passed describes the previous period.
38+
.filter((window) => window.resetsAt === undefined || Date.parse(window.resetsAt) > readAtMs)
39+
.reduce<ServerProviderUsageWindow | undefined>(
40+
(current, window) =>
41+
current === undefined || window.usedPercent > current.usedPercent ? window : current,
42+
undefined,
43+
);
44+
return tightest
45+
? { label: tightest.label, remainingPercent: Math.max(0, 100 - tightest.usedPercent) }
46+
: null;
47+
}
48+
2449
/** The used-up window that reopens last, which is the one the account is waiting on. */
2550
function exhaustedWindow(
2651
provider: ServerProvider,

0 commit comments

Comments
 (0)