Repository navigation
fix(web): thread Limits row shows hub limits for an instance behind that hub #39333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import type { | ||
| EnvironmentId, | ||
| ProviderInstanceId, | ||
| ServerProvider, | ||
| ServerProviderUsageWindow, | ||
| UsageLimitSourceSnapshots, | ||
| } from "@t3tools/contracts"; | ||
| import { | ||
| collectProviderUsageLimits, | ||
| displayLimitWindows, | ||
| endpointHubLimitPool, | ||
| type LimitPace, | ||
| paceOf, | ||
| paceRemainingPercent, | ||
|
|
@@ -33,6 +35,7 @@ const PACE_LABEL: Record<LimitPace, string> = { | |
| }; | ||
|
|
||
| export interface ThreadDetailsUsageInput { | ||
| readonly environmentId: EnvironmentId; | ||
| readonly providerInstanceId: ProviderInstanceId | null; | ||
| readonly providers: readonly ServerProvider[]; | ||
| readonly sources: UsageLimitSourceSnapshots; | ||
|
|
@@ -43,6 +46,7 @@ type UsageTone = "error" | "warning" | null; | |
|
|
||
| /** The selected provider's limit windows. Each row reads as room left and opens the usage page. */ | ||
| export function ThreadDetailsUsage({ | ||
| environmentId, | ||
| providerInstanceId, | ||
| providers, | ||
| sources, | ||
|
|
@@ -57,11 +61,14 @@ export function ThreadDetailsUsage({ | |
| : null; | ||
| // Only this instance's data: the report keeps one instance per login, and any other entry may be | ||
| // a different login or workspace, so fall back to the instance's own snapshot. | ||
| const provider = providers.find((entry) => entry.instanceId === providerInstanceId); | ||
| const limits = | ||
| report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ?? | ||
| providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits; | ||
| if (!limits) return null; | ||
| provider?.usageLimits; | ||
| if (!limits || !provider) return null; | ||
| const notice = limitsNotice(limits, m); | ||
| const hubPool = notice ? endpointHubLimitPool(environmentId, provider, sources, now) : null; | ||
| const hubWindows = hubPool ? displayLimitWindows(hubPool) : []; | ||
|
|
||
| return ( | ||
| <ThreadDetailsSection | ||
|
|
@@ -70,7 +77,23 @@ export function ThreadDetailsUsage({ | |
| showHeading={false} | ||
| > | ||
| <div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs"> | ||
| {notice ? ( | ||
| {hubWindows.length > 0 ? ( | ||
| hubWindows.map((window) => { | ||
| const reset = window.resets.find((entry) => entry.at > now); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine resets that occur at the same timestamp. If two equal-capacity accounts are exhausted and reset together, Group entries at the earliest future timestamp and calculate their combined restored share. Preserve the existing share when only one account resets.
|
||
| return ( | ||
| <LimitRow | ||
| key={window.id} | ||
| label={window.label} | ||
| left={window.remainingPercent} | ||
| pace={window.pace} | ||
| timeLeft={null} | ||
| resetsAt={reset ? new Date(reset.at).toISOString() : undefined} | ||
| restoresPercent={reset?.restoresPercent} | ||
| now={now} | ||
| /> | ||
| ); | ||
| }) | ||
| ) : notice ? ( | ||
| <UsageRow | ||
| label={gt("Limits", { $context: "usage limits row label" })} | ||
| fill={null} | ||
|
|
@@ -80,7 +103,17 @@ export function ThreadDetailsUsage({ | |
| opensUsage | ||
| /> | ||
| ) : ( | ||
| limits.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />) | ||
| limits.windows.map((window) => ( | ||
| <LimitRow | ||
| key={window.id} | ||
| label={window.label} | ||
| left={remainingPercent(window)} | ||
| pace={paceOf(window, now)} | ||
| timeLeft={paceRemainingPercent(window, now)} | ||
| resetsAt={window.resetsAt} | ||
| now={now} | ||
| /> | ||
| )) | ||
| )} | ||
| </div> | ||
| </ThreadDetailsSection> | ||
|
|
@@ -155,22 +188,30 @@ export function ThreadDetailsContext({ usage }: { readonly usage: ContextWindowS | |
| } | ||
|
|
||
| function LimitRow({ | ||
| window, | ||
| label, | ||
| left, | ||
| pace, | ||
| timeLeft, | ||
| resetsAt: resetsAtIso, | ||
| restoresPercent, | ||
| now, | ||
| }: { | ||
| readonly window: ServerProviderUsageWindow; | ||
| readonly label: string; | ||
| readonly left: number; | ||
| readonly pace: LimitPace | null; | ||
| readonly timeLeft: number | null; | ||
| readonly resetsAt: string | undefined; | ||
| /** For a pooled window: the share of the pool its next reset restores. */ | ||
| readonly restoresPercent?: number | undefined; | ||
| readonly now: number; | ||
| }) { | ||
| const gt = useGT(); | ||
| const m = useMessages(); | ||
| const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); | ||
| const left = remainingPercent(window); | ||
| const pace = paceOf(window, now); | ||
| const timeLeft = paceRemainingPercent(window, now); | ||
| const resetsAt = window.resetsAt ? Date.parse(window.resetsAt) : Number.NaN; | ||
| const resetsAt = resetsAtIso ? Date.parse(resetsAtIso) : Number.NaN; | ||
| return ( | ||
| <UsageRow | ||
| label={window.label} | ||
| label={label} | ||
| fill={left} | ||
| tone={left <= 10 ? "error" : left <= 20 ? "warning" : null} | ||
| tick={timeLeft} | ||
|
|
@@ -179,22 +220,28 @@ function LimitRow({ | |
| opensUsage | ||
| tooltip={ | ||
| <> | ||
| <span className="text-foreground"> | ||
| {gt("{label}: {left}% left", { label: window.label, left })} | ||
| </span> | ||
| {pace && timeLeft !== null ? ( | ||
| <span className="text-foreground">{gt("{label}: {left}% left", { label, left })}</span> | ||
| {pace ? ( | ||
| <span className="text-muted-foreground"> | ||
| {m(PACE_LABEL[pace])} · {gt("{timeLeft}% of the window left", { timeLeft })} | ||
| {m(PACE_LABEL[pace])} | ||
| {timeLeft !== null | ||
| ? ` · ${gt("{timeLeft}% of the window left", { timeLeft })}` | ||
| : null} | ||
| </span> | ||
| ) : null} | ||
| {window.resetsAt && resetsAt > now ? ( | ||
| {resetsAtIso && resetsAt > now ? ( | ||
| <span className="text-muted-foreground"> | ||
| {gt("Resets in {duration} · {time}", { | ||
| duration: formatDuration(resetsAt - now, m), | ||
| time: formatUpcomingTimestamp(window.resetsAt, timestampFormat, now, undefined, m), | ||
| time: formatUpcomingTimestamp(resetsAtIso, timestampFormat, now, undefined, m), | ||
| })} | ||
| </span> | ||
| ) : null} | ||
| {restoresPercent ? ( | ||
| <span className="text-muted-foreground"> | ||
| {gt("+{percent}% of pool", { percent: restoresPercent })} | ||
| </span> | ||
| ) : null} | ||
| </> | ||
| } | ||
| /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -541,6 +541,37 @@ export function collectLimitPools( | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * A custom endpoint that is a usage hub routes each turn to any of the hub's accounts, so the | ||
| * instance's own limits are the hub's accounts for its driver, pooled. | ||
| */ | ||
| export function endpointHubLimitPool( | ||
| environmentId: EnvironmentId, | ||
| provider: ServerProvider, | ||
| sources: UsageLimitSourceSnapshots, | ||
| now: number, | ||
| ): LimitPool | null { | ||
| const endpoint = provider.auth.endpoint; | ||
| if (!endpoint) return null; | ||
| // Both sides are server-canonical: the instance reports an origin, a hub its origin and path. | ||
| const hubs = sources.filter( | ||
| (source) => source.endpoint === endpoint || source.endpoint?.startsWith(`${endpoint}/`), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pool only hubs whose base path matches the instance route.
Require the pool to come from the hub that the instance route identifies. If the route cannot be identified, do not show a pooled value from sibling hubs on the same origin.
|
||
| ); | ||
| if (hubs.length === 0) return null; | ||
| const accounts = collectLimitAccounts( | ||
| new Map([ | ||
| [ | ||
| environmentId, | ||
| { | ||
| entry: { target: { label: "" } }, | ||
| serverConfig: { providers: [], usageLimitSources: hubs }, | ||
| }, | ||
| ], | ||
| ]), | ||
| ).filter((account) => account.driver === provider.driver); | ||
| return collectLimitPools(accounts, now)[0] ?? null; | ||
| } | ||
|
|
||
| function accountSortName(account: LimitAccount): string { | ||
| return (account.displayName ?? account.email ?? account.key).toLowerCase(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow hub fallback when
usageLimitsis absent.If the provider omits its optional
usageLimits, this return prevents hub pooling. Hub entries incollectProviderUsageLimitshave noinstanceId, so they cannot supplylimitsthrough the preceding lookup either.Require
providerfirst. Then attempt hub pooling when native limits are absent or unavailable. Returnnullonly when neither native limits nor usable pooled windows exist. Keep the current native display when native limits are usable.