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 * (1 - normalizedPercentage / 100);
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-error)"
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,stroke] 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 viewportClassName="p-0"
88 className="w-64 max-w-none text-left whitespace-normal"
89 >
90 <div className="flex flex-col gap-2 p-[var(--floating-content-inset)]">
91 <div className="flex items-center justify-between gap-3">
92 <div className="font-medium text-muted-foreground text-xs">Context Window</div>
93 {usage.maxTokens !== null && usedPercentage ? (
94 <div className="text-secondary-label text-[11px] tabular-nums">
95 <span>{usedPercentage}</span>
96 <span className="mx-1">·</span>
97 <span>
98 {formatContextWindowTokens(usage.usedTokens)}/
99 {formatContextWindowTokens(usage.maxTokens ?? null)}
100 </span>
101 </div>
102 ) : (
103 <div className="text-secondary-label text-[11px] tabular-nums">
104 {formatContextWindowTokens(usage.usedTokens)}
105 </div>
106 )}
107 </div>
108 {usage.maxTokens !== null ? (
109 <div
110 className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
111 role="progressbar"
112 aria-valuemin={0}
113 aria-valuemax={100}
114 aria-valuenow={Math.round(normalizedPercentage)}
115 aria-label="Context window usage"
116 >
117 <div
118 className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
119 style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
120 />
121 </div>
122 ) : null}
123 {showTotalProcessed ? (
124 <div className="flex items-center justify-between gap-3 text-[11px] leading-4">
125 <span className="text-secondary-label">Total processed</span>
126 <span className="font-medium tabular-nums text-secondary-label">
127 {formatContextWindowTokens(totalProcessedTokens)}
128 </span>
129 </div>
130 ) : null}
131 {usage.compactsAutomatically ? (
132 <div className="mt-1 text-pretty text-secondary-label text-[11px] font-medium">
133 {providerDisplayName ?? "It"} automatically compacts its context when needed.
134 </div>
135 ) : null}
136 </div>
137 </PopoverPopup>
138 </Popover>
139 );
140}