From 285ad05c475e9db09a0bf1c51c0f5d1c33b19735 Mon Sep 17 00:00:00 2001 From: Fionn Gartlan <96302767+fionn77@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:13:43 +0100 Subject: [PATCH] fix(web): thread Limits row shows hub limits for an instance behind that hub An instance whose custom endpoint is a CLIProxyAPI hub has no subscription limits of its own, so the thread details Limits row read "Unavailable" even when the same hub was added as a usage source. The row now pools the hub's accounts for the instance's driver when the hub's endpoint matches the instance's. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/ChatView.tsx | 1 + .../components/chat/ThreadDetailsUsage.tsx | 85 ++++++++++++++----- packages/shared/src/usageLimits.test.ts | 59 +++++++++++++ packages/shared/src/usageLimits.ts | 31 +++++++ 4 files changed, 157 insertions(+), 19 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index a731a5798e191..48e50b1d87fea 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3996,6 +3996,7 @@ export default function ChatView(props: ChatViewProps) { onUpdateProjectScript={updateProjectScriptStable} onDeleteProjectScript={deleteProjectScriptStable} usage={{ + environmentId: activeThread.environmentId, providerInstanceId: activeProviderInstanceId, providers: providerStatuses, sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES, diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index b2e09a35c5264..8478e391e0a9a 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -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 = { }; 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 (
- {notice ? ( + {hubWindows.length > 0 ? ( + hubWindows.map((window) => { + const reset = window.resets.find((entry) => entry.at > now); + return ( + + ); + }) + ) : notice ? ( ) : ( - limits.windows.map((window) => ) + limits.windows.map((window) => ( + + )) )}
@@ -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 ( - - {gt("{label}: {left}% left", { label: window.label, left })} - - {pace && timeLeft !== null ? ( + {gt("{label}: {left}% left", { label, left })} + {pace ? ( - {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} ) : null} - {window.resetsAt && resetsAt > now ? ( + {resetsAtIso && resetsAt > now ? ( {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), })} ) : null} + {restoresPercent ? ( + + {gt("+{percent}% of pool", { percent: restoresPercent })} + + ) : null} } /> diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 0078c21b566b7..e2cf389383d2c 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -19,6 +19,7 @@ import { collectExternalUsageLinks, collectLimitPools, elapsedShare, + endpointHubLimitPool, exhaustedUsageWindow, paceOf, paceRemainingPercent, @@ -1376,6 +1377,64 @@ describe("pooled account columns", () => { }); }); +describe("endpointHubLimitPool", () => { + const environmentId = EnvironmentId.make("server"); + const claude = ProviderDriverKind.make("claudeAgent"); + const proxied = provider({ + instanceId: ProviderInstanceId.make("claude_pool"), + driver: claude, + auth: { status: "authenticated", type: "customEndpoint", endpoint: "http://127.0.0.1:8317" }, + usageLimits: { checkedAt: "2026-09-03T11:00:00.000Z", windows: [] }, + }); + const read = (usedPercent: number, resetsAt: string) => ({ + checkedAt: "2026-09-03T11:00:00.000Z", + windows: [{ ...window, usedPercent, resetsAt }], + }); + const hub = (endpoint: string) => ({ + id: UsageLimitSourceId.make("hub"), + kind: "cliproxy" as const, + label: "CLIProxy", + endpoint, + checkedAt: "2026-09-03T11:00:00.000Z", + accounts: [ + { id: "john.json", driver: claude, usageLimits: read(20, "2026-09-03T13:00:00.000Z") }, + { id: "gmail.json", driver: claude, usageLimits: read(60, "2026-09-03T14:00:00.000Z") }, + { + id: "codex.json", + driver: ProviderDriverKind.make("codex"), + usageLimits: read(100, "2026-09-03T13:00:00.000Z"), + }, + ], + }); + + it("pools the driver's accounts of the hub the instance's endpoint points at", () => { + const pool = endpointHubLimitPool(environmentId, proxied, [hub("http://127.0.0.1:8317")], now); + expect(pool?.accounts.map((account) => account.displayName)).toEqual(["john", "gmail"]); + expect(pool?.windows.map((entry) => entry.remainingPercent)).toEqual([60]); + expect(pool?.windows[0]?.resets[0]?.restoresPercent).toBe(10); + }); + + it("matches a hub served below the endpoint's origin", () => { + expect( + endpointHubLimitPool(environmentId, proxied, [hub("http://127.0.0.1:8317/hub")], now), + ).not.toBeNull(); + }); + + it("ignores hubs at other endpoints and instances without one", () => { + expect( + endpointHubLimitPool(environmentId, proxied, [hub("http://127.0.0.1:83170")], now), + ).toBeNull(); + expect( + endpointHubLimitPool( + environmentId, + { ...proxied, auth: { status: "authenticated" } }, + [hub("http://127.0.0.1:8317")], + now, + ), + ).toBeNull(); + }); +}); + describe("/usage-limits", () => { const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] }; const selected = provider({ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 693d82b956971..d5bd9a9756726 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -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}/`), + ); + 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(); }