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
29 ? "var(--color-red-500)"
30 : "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)";
31
32 return (
33 <Popover>
34 <PopoverTrigger
35 openOnHover
36 delay={150}
37 closeDelay={0}
38 render={
39 <button
40 type="button"
41 className={cn(
42 "inline-flex size-7 cursor-pointer items-center justify-center rounded-full border border-transparent text-muted-foreground outline-none transition-colors",
43 "hover:bg-accent data-[pressed]:bg-accent",
44 "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background",
45 )}
46 aria-label={
47 usage.maxTokens !== null && usedPercentage
48 ? `Context window ${usedPercentage} used`
49 : `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
50 }
51 >
52 <span className="relative flex size-5 items-center justify-center">
53 <svg
54 viewBox="0 0 24 24"
55 className="-rotate-90 absolute inset-0 size-full transform-gpu"
56 aria-hidden="true"
57 >
58 <circle
59 cx="12"
60 cy="12"
61 r={radius}
62 fill="none"
63 stroke="color-mix(in oklab, var(--color-muted-foreground) 24%, transparent)"
64 strokeWidth="3"
65 />
66 <circle
67 cx="12"
68 cy="12"
69 r={radius}
70 fill="none"
71 stroke={usageColor}
72 strokeWidth="3"
73 strokeLinecap="round"
74 strokeDasharray={circumference}
75 strokeDashoffset={dashOffset}
76 className="transition-[stroke-dashoffset] duration-500 ease-out motion-reduce:transition-none"
77 />
78 </svg>
79 </span>
80 </button>
81 }
82 />
83 <PopoverPopup
84 tooltipStyle
85 side="top"
86 align="end"
87 className="dropdown-glass w-64 max-w-none border-0! bg-secondary! p-0 shadow-none! before:hidden"
88 >
89 <div className="flex flex-col gap-2 p-3">
90 <div className="flex items-center justify-between gap-3">
91 <div className="font-medium text-muted-foreground text-xs">Context Window</div>
92 {usage.maxTokens !== null && usedPercentage ? (
93 <div className="text-[11px] tabular-nums text-muted-foreground/70">
94 <span>{usedPercentage}</span>
95 <span className="mx-1">·</span>
96 <span>
97 {formatContextWindowTokens(usage.usedTokens)}/
98 {formatContextWindowTokens(usage.maxTokens ?? null)}
99 </span>
100 </div>
101 ) : (
102 <div className="text-[11px] tabular-nums text-muted-foreground/70">
103 {formatContextWindowTokens(usage.usedTokens)}
104 </div>
105 )}
106 </div>
107 {usage.maxTokens !== null ? (
108 <div
109 className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
110 role="progressbar"
111 aria-valuemin={0}
112 aria-valuemax={100}
113 aria-valuenow={Math.round(normalizedPercentage)}
114 aria-label="Context window usage"
115 >
116 <div
117 className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
118 style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
119 />
120 </div>
121 ) : null}
122 {showTotalProcessed ? (
123 <div className="flex items-center justify-between gap-3 text-[11px] leading-4">
124 <span className="text-muted-foreground/60">Total processed</span>
125 <span className="font-medium tabular-nums text-muted-foreground/80">
126 {formatContextWindowTokens(totalProcessedTokens)}
127 </span>
128 </div>
129 ) : null}
130 {usage.compactsAutomatically ? (
131 <div className="mt-1 text-pretty text-[11px] font-medium text-muted-foreground/70">
132 {providerDisplayName ?? "It"} automatically compacts its context when needed.
133 </div>
134 ) : null}
135 </div>
136 </PopoverPopup>
137 </Popover>
138 );
139}