Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ContextWindowMeter.tsx

137 lines5.3 KB

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