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: { usage: ContextWindowSnapshot }) {
16 const { usage } = props;
17 const usedPercentage = formatPercentage(usage.usedPercentage);
18 const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
19 const radius = 9.75;
20 const circumference = 2 * Math.PI * radius;
21 const dashOffset = circumference - (normalizedPercentage / 100) * circumference;
22
23 return (
24 <Popover>
25 <PopoverTrigger
26 openOnHover
27 delay={150}
28 closeDelay={0}
29 render={
30 <button
31 type="button"
32 className="group inline-flex items-center justify-center rounded-full transition-opacity hover:opacity-85"
33 aria-label={
34 usage.maxTokens !== null && usedPercentage
35 ? `Context window ${usedPercentage} used`
36 : `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
37 }
38 >
39 <span className="relative flex h-6 w-6 items-center justify-center">
40 <svg
41 viewBox="0 0 24 24"
42 className="-rotate-90 absolute inset-0 h-full w-full transform-gpu"
43 aria-hidden="true"
44 >
45 <circle
46 cx="12"
47 cy="12"
48 r={radius}
49 fill="none"
50 stroke="color-mix(in oklab, var(--color-muted) 70%, transparent)"
51 strokeWidth="3"
52 />
53 <circle
54 cx="12"
55 cy="12"
56 r={radius}
57 fill="none"
58 stroke="var(--color-muted-foreground)"
59 strokeWidth="3"
60 strokeLinecap="round"
61 strokeDasharray={circumference}
62 strokeDashoffset={dashOffset}
63 className="transition-[stroke-dashoffset] duration-500 ease-out motion-reduce:transition-none"
64 />
65 </svg>
66 <span
67 className={cn(
68 "relative flex h-[15px] w-[15px] items-center justify-center rounded-full bg-background text-[8px] font-medium",
69 "text-muted-foreground",
70 )}
71 >
72 {usage.usedPercentage !== null
73 ? Math.round(usage.usedPercentage)
74 : formatContextWindowTokens(usage.usedTokens)}
75 </span>
76 </span>
77 </button>
78 }
79 />
80 <PopoverPopup tooltipStyle side="top" align="end" className="w-max max-w-none px-3 py-2">
81 <div className="space-y-1.5 leading-tight">
82 <div className="text-[11px] font-medium uppercase tracking-[0.08em] text-muted-foreground">
83 Context window
84 </div>
85 {usage.maxTokens !== null && usedPercentage ? (
86 <div className="whitespace-nowrap text-xs font-medium text-foreground">
87 <span>{usedPercentage}</span>
88 <span className="mx-1">⋅</span>
89 <span>{formatContextWindowTokens(usage.usedTokens)}</span>
90 <span>/</span>
91 <span>{formatContextWindowTokens(usage.maxTokens ?? null)} context used</span>
92 </div>
93 ) : (
94 <div className="text-sm text-foreground">
95 {formatContextWindowTokens(usage.usedTokens)} tokens used so far
96 </div>
97 )}
98 {(usage.totalProcessedTokens ?? null) !== null &&
99 (usage.totalProcessedTokens ?? 0) > usage.usedTokens ? (
100 <div className="text-xs text-muted-foreground">
101 Total processed: {formatContextWindowTokens(usage.totalProcessedTokens ?? null)}{" "}
102 tokens
103 </div>
104 ) : null}
105 {usage.compactsAutomatically ? (
106 <div className="text-xs text-muted-foreground">
107 Automatically compacts its context when needed.
108 </div>
109 ) : null}
110 </div>
111 </PopoverPopup>
112 </Popover>
113 );
114}