import { cn } from "~/lib/utils"; import { type ContextWindowSnapshot, formatContextWindowTokens } from "~/lib/contextWindow"; import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; function formatPercentage(value: number | null): string | null { if (value === null || !Number.isFinite(value)) { return null; } if (value < 10) { return `${value.toFixed(1).replace(/\.0$/, "")}%`; } return `${Math.round(value)}%`; } export function ContextWindowMeter(props: { usage: ContextWindowSnapshot; providerDisplayName?: string | null; }) { const { usage, providerDisplayName } = props; const usedPercentage = formatPercentage(usage.usedPercentage); const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0)); const radius = 9.75; const circumference = 2 * Math.PI * radius; const dashOffset = circumference - (normalizedPercentage / 100) * circumference; const totalProcessedTokens = usage.totalProcessedTokens ?? null; const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0; const isOverloaded = normalizedPercentage > 90; const usageColor = isOverloaded ? "var(--color-red-500)" : "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)"; return ( } />
Context Window
{usage.maxTokens !== null && usedPercentage ? (
{usedPercentage} ยท {formatContextWindowTokens(usage.usedTokens)}/ {formatContextWindowTokens(usage.maxTokens ?? null)}
) : (
{formatContextWindowTokens(usage.usedTokens)}
)}
{usage.maxTokens !== null ? (
) : null} {showTotalProcessed ? (
Total processed {formatContextWindowTokens(totalProcessedTokens)}
) : null} {usage.compactsAutomatically ? (
{providerDisplayName ?? "It"} automatically compacts its context when needed.
) : null}
); }