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

ContextWindowMeter.tsx

138 lines5.3 KB

1import { Button } from "../ui/button";
2import { type ContextWindowSnapshot, formatContextWindowTokens } from "~/lib/contextWindow";
3import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
4import { formatContextWindowCompactionMessage } from "./ContextWindowMeter.logic";
5​
6function formatPercentage(value: number | null): string | null {
7 if (value === null || !Number.isFinite(value)) {
8 return null;
9 }
10 if (value < 10) {
11 return `${value.toFixed(1).replace(/\.0$/, "")}%`;
12 }
13 return `${Math.round(value)}%`;
15​
16export function ContextWindowMeter(props: {
17 usage: ContextWindowSnapshot;
18 modelDisplayName?: string | null;
19}) {
20 const { usage, modelDisplayName } = props;
21 const usedPercentage = formatPercentage(usage.usedPercentage);
22 const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
23 const radius = 9.75;
24 const circumference = 2 * Math.PI * radius;
25 const dashOffset = circumference * (1 - normalizedPercentage / 100);
26 const totalProcessedTokens = usage.totalProcessedTokens ?? null;
27 const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0;
28 const isOverloaded = normalizedPercentage > 90;
29 const usageColor = isOverloaded
30 ? "var(--color-error)"
31 : "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)";
32​
33 return (
34 <Popover>
35 <PopoverTrigger
36 openOnHover
37 delay={150}
38 closeDelay={0}
39 render={
40 <Button
41 size="icon-sm"
42 variant="ghost-muted"
43 className="size-7 rounded-full hover:text-muted-foreground data-pressed:text-muted-foreground"
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-5 items-center justify-center">
51 <svg
52 viewBox="0 0 24 24"
53 className="-rotate-90 absolute inset-0 size-full transform-gpu mx-0!"
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) 24%, 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,stroke] duration-500 ease-out motion-reduce:transition-none"
75 />
76 </svg>
77 </span>
78 </Button>
79 }
80 />
81 <PopoverPopup
82 tooltipStyle
83 side="top"
84 align="end"
85 viewportClassName="p-0"
86 className="w-64 max-w-none text-left whitespace-normal"
87 >
88 <div className="flex flex-col gap-2 p-[var(--floating-content-inset)]">
89 <div className="flex items-center justify-between gap-3">
90 <div className="font-medium text-muted-foreground text-xs">Context Window</div>
91 {usage.maxTokens !== null && usedPercentage ? (
92 <div className="text-secondary-label text-[11px] tabular-nums">
93 <span>{usedPercentage}</span>
94 <span className="mx-1">·</span>
95 <span>
96 {formatContextWindowTokens(usage.usedTokens)}/
97 {formatContextWindowTokens(usage.maxTokens ?? null)}
98 </span>
99 </div>
100 ) : (
101 <div className="text-secondary-label text-[11px] tabular-nums">
102 {formatContextWindowTokens(usage.usedTokens)}
103 </div>
104 )}
105 </div>
106 {usage.maxTokens !== null ? (
107 <div
108 className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
109 role="progressbar"
110 aria-valuemin={0}
111 aria-valuemax={100}
112 aria-valuenow={Math.round(normalizedPercentage)}
113 aria-label="Context window usage"
114 >
115 <div
116 className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
117 style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
118 />
119 </div>
120 ) : null}
121 {showTotalProcessed ? (
122 <div className="flex items-center justify-between gap-3 text-[11px] leading-4">
123 <span className="text-secondary-label">Total processed</span>
124 <span className="font-medium tabular-nums text-secondary-label">
125 {formatContextWindowTokens(totalProcessedTokens)}
126 </span>
127 </div>
128 ) : null}
129 {usage.compactsAutomatically ? (
130 <div className="mt-1 text-pretty text-secondary-label text-[11px] font-medium">
131 {formatContextWindowCompactionMessage(modelDisplayName)}
132 </div>
133 ) : null}
134 </div>
135 </PopoverPopup>
136 </Popover>
137 );