Open History to see the last commit
1import { cn } from "~/lib/utils";
2import { type ContextWindowSnapshot, formatContextWindowTokens } from "~/lib/contextWindow";
3import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
4
5function formatPercentage(value: number | null): string | null {
6 if (value === null || !Number.isFinite(value)) {
7 return null;
8 }
9 if (value < 10) {
10 return `${value.toFixed(1).replace(/\.0$/, "")}%`;
11 }
12 return `${Math.round(value)}%`;
13}
14
15export function ContextWindowMeter(props: {
16 usage: ContextWindowSnapshot;
17 providerDisplayName?: string | null;
18}) {
19 const { usage, providerDisplayName } = props;
20 const usedPercentage = formatPercentage(usage.usedPercentage);
21 const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
22 const radius = 9.75;
23 const circumference = 2 * Math.PI * radius;
24 const dashOffset = circumference - (normalizedPercentage / 100) * circumference;
25 const totalProcessedTokens = usage.totalProcessedTokens ?? null;
26 const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0;
27 const isOverloaded = normalizedPercentage > 90;
28 const usageColor = isOverloaded ? "var(--color-red-500)" : "var(--color-blue-500)";
29
30 return (
31 <Popover>
32 <PopoverTrigger
33 openOnHover
34 delay={150}
35 closeDelay={0}
36 render={
37 <button
38 type="button"
39 className={cn(
40 "inline-flex size-6 cursor-pointer items-center justify-center rounded-full border border-transparent text-muted-foreground outline-none transition-colors",
41 "hover:bg-accent data-[pressed]:bg-accent",
42 "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background",
43 )}
44 aria-label={
45 usage.maxTokens !== null && usedPercentage
46 ? `Context window ${usedPercentage} used`
47 : `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
48 }
49 >
50 <span className="relative flex size-4 items-center justify-center">
51 <svg
52 viewBox="0 0 24 24"
53 className="-rotate-90 absolute inset-0 size-full transform-gpu"
54 aria-hidden="true"
55 >
56 <circle
57 cx="12"
58 cy="12"
59 r={radius}
60 fill="none"
61 stroke="color-mix(in oklab, var(--color-muted-foreground) 35%, transparent)"
62 strokeWidth="3"
63 />
64 <circle
65 cx="12"
66 cy="12"
67 r={radius}
68 fill="none"
69 stroke={usageColor}
70 strokeWidth="3"
71 strokeLinecap="round"
72 strokeDasharray={circumference}
73 strokeDashoffset={dashOffset}
74 className="transition-[stroke-dashoffset] duration-500 ease-out motion-reduce:transition-none"
75 />
76 </svg>
77 </span>
78 </button>
79 }
80 />
81 <PopoverPopup tooltipStyle side="top" align="end" className="w-64 max-w-none p-0">
82 <div className="flex flex-col gap-2 p-3">
83 <div className="flex items-center justify-between gap-3">
84 <div className="font-medium text-muted-foreground text-xs">Context Window</div>
85 {usage.maxTokens !== null && usedPercentage ? (
86 <div className="text-[11px] tabular-nums text-muted-foreground/70">
87 <span>{usedPercentage}</span>
88 <span className="mx-1">·</span>
89 <span>
90 {formatContextWindowTokens(usage.usedTokens)}/
91 {formatContextWindowTokens(usage.maxTokens ?? null)}
92 </span>
93 </div>
94 ) : (
95 <div className="text-[11px] tabular-nums text-muted-foreground/70">
96 {formatContextWindowTokens(usage.usedTokens)}
97 </div>
98 )}
99 </div>
100 {usage.maxTokens !== null ? (
101 <div
102 className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
103 role="progressbar"
104 aria-valuemin={0}
105 aria-valuemax={100}
106 aria-valuenow={Math.round(normalizedPercentage)}
107 aria-label="Context window usage"
108 >
109 <div
110 className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
111 style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
112 />
113 </div>
114 ) : null}
115 {showTotalProcessed ? (
116 <div className="flex items-center justify-between gap-3 text-[11px] leading-4">
117 <span className="text-muted-foreground/60">Total processed</span>
118 <span className="font-medium tabular-nums text-muted-foreground/80">
119 {formatContextWindowTokens(totalProcessedTokens)}
120 </span>
121 </div>
122 ) : null}
123 {usage.compactsAutomatically ? (
124 <div className="mt-1 text-pretty text-[11px] font-medium text-muted-foreground/70">
125 {providerDisplayName ?? "It"} automatically compacts its context when needed.
126 </div>
127 ) : null}
128 </div>
129 </PopoverPopup>
130 </Popover>
131 );
132}