Skip to content
1 change: 1 addition & 0 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3997,6 +3997,7 @@ export default function ChatView(props: ChatViewProps) {
providers: providerStatuses,
sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES,
contextWindow: activeContextWindow,
onOpenUsageLimits: composerMounted ? openUsageLimits : undefined,
}}
/>

Expand Down
132 changes: 132 additions & 0 deletions apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import {
ProviderDriverKind,
ProviderInstanceId,
UsageLimitSourceId,
type ServerProvider,
type UsageLimitSourceSnapshots,
} from "@t3tools/contracts";
import { expect, it, vi } from "vite-plus/test";
import { render } from "../../../test/render";
import { ThreadDetailsUsage } from "./ThreadDetailsUsage";

const checkedAt = "2026-11-04T12:00:00.000Z";
const provider: ServerProvider = {
instanceId: ProviderInstanceId.make("claude-proxy"),
driver: ProviderDriverKind.make("claudeAgent"),
enabled: true,
installed: true,
version: null,
status: "ready",
auth: { status: "authenticated" },
checkedAt,
models: [],
slashCommands: [],
skills: [],
usageLimits: { checkedAt, windows: [], unavailable: { reason: "unsupported" } },
};
const sources: UsageLimitSourceSnapshots = [
{
id: UsageLimitSourceId.make("hub"),
kind: "cliproxy",
label: "Claude pool",
checkedAt,
accounts: [
{
id: "claude-account",
driver: provider.driver,
usageLimits: {
checkedAt,
windows: [{ id: "session", kind: "session", label: "Session", usedPercent: 17 }],
},
},
],
},
];

it("opens available hub limits without attributing a hub account's quota to the endpoint", async () => {
const onOpenUsageLimits = vi.fn();
await using screen = await render(
<ThreadDetailsUsage
providerInstanceId={provider.instanceId}
providers={[provider]}
sources={sources}
onOpenUsageLimits={onOpenUsageLimits}
/>,
);
await expect.element(screen.getByRole("button", { name: "View hub limits" })).toBeVisible();
await expect.element(screen.getByText("Unavailable", { exact: true })).not.toBeInTheDocument();
await screen.getByRole("button", { name: "View hub limits" }).click();
expect(onOpenUsageLimits).toHaveBeenCalledOnce();

await screen.rerender(
<ThreadDetailsUsage
providerInstanceId={provider.instanceId}
providers={[provider]}
sources={sources}
onOpenUsageLimits={undefined}
/>,
);
await expect.element(screen.getByText("Unavailable", { exact: true })).toBeVisible();
await expect
.element(screen.getByRole("button", { name: "View hub limits" }))
.not.toBeInTheDocument();

for (const unavailableSources of [
[],
[
{
...sources[0]!,
accounts: [{ ...sources[0]!.accounts[0]!, driver: ProviderDriverKind.make("codex") }],
},
],
[
{
...sources[0]!,
accounts: [
{
...sources[0]!.accounts[0]!,
usageLimits: {
checkedAt,
windows: [],
unavailable: { reason: "probeFailed" as const },
},
},
],
},
],
]) {
await screen.rerender(
<ThreadDetailsUsage
providerInstanceId={provider.instanceId}
providers={[provider]}
sources={unavailableSources}
onOpenUsageLimits={onOpenUsageLimits}
/>,
);
await expect.element(screen.getByText("Unavailable", { exact: true })).toBeVisible();
await expect
.element(screen.getByRole("button", { name: "View hub limits" }))
.not.toBeInTheDocument();
}

await screen.rerender(
<ThreadDetailsUsage
providerInstanceId={provider.instanceId}
providers={[
{
...provider,
usageLimits: {
checkedAt,
windows: [{ id: "session", kind: "session", label: "Session", usedPercent: 60 }],
},
},
]}
sources={sources}
onOpenUsageLimits={onOpenUsageLimits}
/>,
);
await expect.element(screen.getByText("40%", { exact: true })).toBeVisible();
await expect
.element(screen.getByRole("button", { name: "View hub limits" }))
.not.toBeInTheDocument();
});
31 changes: 26 additions & 5 deletions apps/web/src/components/chat/ThreadDetailsUsage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { ContextWindowSnapshot } from "@t3tools/client-runtime/state/contex
import { formatContextWindowTokens } from "../../lib/contextWindow";
import { cn } from "../../lib/utils";
import { formatUpcomingTimestamp } from "../../timestampFormat";
import { Button } from "../ui/button";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { formatContextWindowCompactionMessage } from "./compaction.logic";
import { ThreadDetailsSection } from "./ThreadDetailsSection";
Expand All @@ -35,6 +36,7 @@ export interface ThreadDetailsUsageInput {
readonly providers: readonly ServerProvider[];
readonly sources: UsageLimitSourceSnapshots;
readonly contextWindow: ContextWindowSnapshot | null;
readonly onOpenUsageLimits: (() => void) | undefined;
}

type UsageTone = "error" | "warning" | null;
Expand All @@ -44,6 +46,7 @@ export function ThreadDetailsUsage({
providerInstanceId,
providers,
sources,
onOpenUsageLimits,
}: Omit<ThreadDetailsUsageInput, "contextWindow">) {
const gt = useGT();
const m = useMessages();
Expand All @@ -55,11 +58,20 @@ 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 selected = providers.find((provider) => provider.instanceId === providerInstanceId);
const limits =
report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ??
providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits;
if (!limits) return null;
const notice = limitsNotice(limits, m);
selected?.usageLimits;
const hasHubLimits = sources.some((source) =>
source.accounts.some(
(account) =>
account.driver === selected?.driver && limitsNotice(account.usageLimits, m) === null,
),
);
if (!limits && !hasHubLimits) return null;
const notice = limits ? limitsNotice(limits, m) : null;
const showHubLimits =
onOpenUsageLimits !== undefined && hasHubLimits && (!limits || notice !== null);

return (
<ThreadDetailsSection
Expand All @@ -68,7 +80,16 @@ export function ThreadDetailsUsage({
showHeading={false}
>
<div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
{notice ? (
{showHubLimits ? (
<div className="col-span-3 flex h-6 items-center justify-between gap-2 px-2.5">
<span className="text-muted-foreground">
{gt("Limits", { $context: "usage limits row label" })}
</span>
<Button variant="link" size="micro" onClick={onOpenUsageLimits}>
{gt("View hub limits")}
</Button>
</div>
) : notice ? (
<UsageRow
label={gt("Limits", { $context: "usage limits row label" })}
fill={null}
Expand All @@ -77,7 +98,7 @@ export function ThreadDetailsUsage({
tooltip={<span className="text-foreground">{notice}</span>}
/>
) : (
limits.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />)
limits?.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />)
)}
</div>
</ThreadDetailsSection>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -4029,6 +4029,7 @@
"b16fdf56b16d99c9": "Im Tempo",
"90f977e97ae8b6c6": "Unter dem Tempo",
"b560c47847c88b2e": "Limits",
"9ce17cbde3a324c7": "Hub-Limits anzeigen",
"42a02a4b27828027": "Nicht verfügbar",
"16fb3b76d0abbb2e": "Kontextfenster",
"83bbceecef0f6872": "Kontext",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,7 @@
"b16fdf56b16d99c9": "On pace",
"90f977e97ae8b6c6": "Under pace",
"b560c47847c88b2e": "Limits",
"9ce17cbde3a324c7": "View hub limits",
"42a02a4b27828027": "Unavailable",
"16fb3b76d0abbb2e": "Context window",
"83bbceecef0f6872": "Context",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -4026,6 +4026,7 @@
"b16fdf56b16d99c9": "Al ritmo",
"90f977e97ae8b6c6": "Por debajo del ritmo",
"b560c47847c88b2e": "Límites",
"9ce17cbde3a324c7": "Ver límites del hub",
"42a02a4b27828027": "No disponible",
"16fb3b76d0abbb2e": "Ventana de contexto",
"83bbceecef0f6872": "Contexto",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,7 @@
"b16fdf56b16d99c9": "Dans le rythme",
"90f977e97ae8b6c6": "En dessous du rythme",
"b560c47847c88b2e": "Limites",
"9ce17cbde3a324c7": "Voir les limites du hub",
"42a02a4b27828027": "Indisponible",
"16fb3b76d0abbb2e": "Fenêtre de contexte",
"83bbceecef0f6872": "Contexte",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -4017,6 +4017,7 @@
"b16fdf56b16d99c9": "ペースどおり",
"90f977e97ae8b6c6": "ペースより遅い",
"b560c47847c88b2e": "上限",
"9ce17cbde3a324c7": "ハブの上限を表示",
"42a02a4b27828027": "利用不可",
"16fb3b76d0abbb2e": "コンテキストウィンドウ",
"83bbceecef0f6872": "コンテキスト",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -4014,6 +4014,7 @@
"b16fdf56b16d99c9": "예상대로",
"90f977e97ae8b6c6": "예상보다 느림",
"b560c47847c88b2e": "한도",
"9ce17cbde3a324c7": "허브 한도 보기",
"42a02a4b27828027": "사용할 수 없음",
"16fb3b76d0abbb2e": "컨텍스트 창",
"83bbceecef0f6872": "컨텍스트",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -4026,6 +4026,7 @@
"b16fdf56b16d99c9": "No ritmo",
"90f977e97ae8b6c6": "Abaixo do ritmo",
"b560c47847c88b2e": "Limites",
"9ce17cbde3a324c7": "Ver limites do hub",
"42a02a4b27828027": "Indisponível",
"16fb3b76d0abbb2e": "Janela de contexto",
"83bbceecef0f6872": "Contexto",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -4026,6 +4026,7 @@
"b16fdf56b16d99c9": "В темпе",
"90f977e97ae8b6c6": "Медленнее темпа",
"b560c47847c88b2e": "Лимиты",
"9ce17cbde3a324c7": "Посмотреть лимиты хаба",
"42a02a4b27828027": "Недоступно",
"16fb3b76d0abbb2e": "Контекстное окно",
"83bbceecef0f6872": "Контекст",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,7 @@
"b16fdf56b16d99c9": "I takt",
"90f977e97ae8b6c6": "Under takten",
"b560c47847c88b2e": "Gränser",
"9ce17cbde3a324c7": "Visa hubbens gränser",
"42a02a4b27828027": "Inte tillgänglig",
"16fb3b76d0abbb2e": "Kontextfönster",
"83bbceecef0f6872": "Kontext",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/translations/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -4033,6 +4033,7 @@
"b16fdf56b16d99c9": "用量消耗适中",
"90f977e97ae8b6c6": "用量消耗偏慢",
"b560c47847c88b2e": "限制",
"9ce17cbde3a324c7": "查看中心限额",
"42a02a4b27828027": "不可用",
"16fb3b76d0abbb2e": "上下文窗口",
"83bbceecef0f6872": "上下文",
Expand Down
Loading