import type { ProviderInstanceId, ServerProvider, ServerProviderUsageWindow, UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { collectProviderUsageLimits, formatDuration, type LimitPace, limitsNotice, paceOf, remainingPercent, } from "@t3tools/shared/usageLimits"; import type { ReactNode } from "react"; import { usePrimarySettings } from "../../hooks/useSettings"; import { useNowMinute } from "../../hooks/useNowMinute"; import { type ContextWindowSnapshot, formatContextWindowTokens } from "../../lib/contextWindow"; import { cn } from "../../lib/utils"; import { formatUpcomingTimestamp } from "../../timestampFormat"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { ThreadDetailsSection } from "./ThreadDetailsSection"; const PACE_LABEL: Record = { ahead: "Ahead of pace", on: "On pace", under: "Under pace", }; export interface ThreadDetailsUsageInput { readonly providerInstanceId: ProviderInstanceId | null; readonly providers: readonly ServerProvider[]; readonly sources: UsageLimitSourceSnapshots; readonly contextWindow: ContextWindowSnapshot | null; } /** * Labs readout of the selected provider's limit windows and the thread's context window. * Every row reads as room left, so a longer bar always means more headroom. */ export function ThreadDetailsUsage({ providerInstanceId, providers, sources, contextWindow, }: ThreadDetailsUsageInput) { const nowMinute = useNowMinute(); const now = Date.parse(`${nowMinute}:00Z`); const report = providerInstanceId !== null ? collectProviderUsageLimits(providerInstanceId, providers, sources, now) : null; // Match the thread's own instance only; any other entry may be a different login or workspace. const account = report?.accounts.find((entry) => entry.instanceId === providerInstanceId); const notice = account ? limitsNotice(account.limits) : null; if (!account && !contextWindow) return null; return (
{notice ? ( {notice}} /> ) : null} {account && !notice ? account.limits.windows.map((window) => ( )) : null} {contextWindow ? : null}
); } function LimitRow({ window, now, }: { readonly window: ServerProviderUsageWindow; readonly now: number; }) { const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); const left = remainingPercent(window); const pace = paceOf(window, now); const resetsAt = window.resetsAt ? Date.parse(window.resetsAt) : Number.NaN; return ( {window.label}: {left}% left {window.resetsAt && resetsAt > now ? ( Resets in {formatDuration(resetsAt - now)} ยท{" "} {formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)} ) : null} {pace ? {PACE_LABEL[pace]} : null} } /> ); } function ContextRow({ usage, spaced, }: { readonly usage: ContextWindowSnapshot; readonly spaced: boolean; }) { const maxTokens = usage.maxTokens ?? null; const left = maxTokens !== null && usage.usedPercentage !== null ? Math.round(100 - Math.max(0, Math.min(100, usage.usedPercentage))) : null; const used = formatContextWindowTokens(usage.usedTokens); return ( {left !== null ? Context: {left}% left : null} {maxTokens !== null ? `${used} of ${formatContextWindowTokens(maxTokens)} tokens used` : `${used} tokens used`} } value={left === null ? used : undefined} /> ); } /** One label, hairline, and value row; the whole row opens the details tooltip. */ function UsageRow({ label, left, tooltip, value, muted = false, className, }: { readonly label: string; readonly left: number | null; readonly tooltip: ReactNode; /** Replaces the percentage when there is none. */ readonly value?: string | undefined; readonly muted?: boolean; readonly className?: string | undefined; }) { const tone = left === null ? null : left <= 10 ? "error" : left <= 20 ? "warning" : null; return ( } > {label} {left !== null ? ( {left > 0 ? ( ) : null} ) : null} {value ?? `${left}%`} {/* Rows sit 8px inside the card; clear its edge instead of overlapping it. */}
{tooltip}
); }