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

ContextWindowMeter.tsx

161 lines6.2 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";
5import { Minimize2Icon } from "lucide-react";
6​
7function formatPercentage(value: number | null): string | null {
8 if (value === null || !Number.isFinite(value)) {
9 return null;
10 }
11 if (value < 10) {
12 return `${value.toFixed(1).replace(/\.0$/, "")}%`;
13 }
14 return `${Math.round(value)}%`;
16​
17export function ContextWindowMeter(props: {
18 usage: ContextWindowSnapshot;
19 modelDisplayName?: string | null;
20 onCompact?: (() => void) | undefined;
21 compactDisabled?: boolean | undefined;
22 compactDisabledReason?: string | null | undefined;
23}) {
24 const { usage, modelDisplayName, onCompact, compactDisabled, compactDisabledReason } = props;
25 const usedPercentage = formatPercentage(usage.usedPercentage);
26 const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
27 const radius = 9.75;
28 const circumference = 2 * Math.PI * radius;
29 const dashOffset = circumference * (1 - normalizedPercentage / 100);
30 const totalProcessedTokens = usage.totalProcessedTokens ?? null;
31 const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0;
32 const isOverloaded = normalizedPercentage > 90;
33 const usageColor = isOverloaded
34 ? "var(--color-error)"
35 : "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)";
36​
37 return (
38 <Popover>
39 <PopoverTrigger
40 openOnHover
41 delay={150}
42 closeDelay={onCompact ? 150 : 0}
43 render={
44 <Button
45 size="icon-sm"
46 variant="ghost-muted"
47 className="size-7 rounded-full hover:text-muted-foreground data-pressed:text-muted-foreground"
48 aria-label={
49 usage.maxTokens !== null && usedPercentage
50 ? `Context window ${usedPercentage} used`
51 : `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
52 }
53 >
54 <span className="relative flex size-5 items-center justify-center">
55 <svg
56 viewBox="0 0 24 24"
57 className="-rotate-90 absolute inset-0 size-full transform-gpu mx-0!"
58 aria-hidden="true"
59 >
60 <circle
61 cx="12"
62 cy="12"
63 r={radius}
64 fill="none"
65 stroke="color-mix(in oklab, var(--color-muted-foreground) 24%, transparent)"
66 strokeWidth="3"
67 />
68 <circle
69 cx="12"
70 cy="12"
71 r={radius}
72 fill="none"
73 stroke={usageColor}
74 strokeWidth="3"
75 strokeLinecap="round"
76 strokeDasharray={circumference}
77 strokeDashoffset={dashOffset}
78 className="transition-[stroke-dashoffset,stroke] duration-500 ease-out motion-reduce:transition-none"
79 />
80 </svg>
81 </span>
82 </Button>
83 }
84 />
85 <PopoverPopup
86 tooltipStyle
87 side="top"
88 align="end"
89 viewportClassName="p-0"
90 className="w-64 max-w-none text-left whitespace-normal"
91 >
92 <div className="flex flex-col gap-2 p-[var(--floating-content-inset)]">
93 <div className="flex items-center justify-between gap-3">
94 <div className="font-medium text-muted-foreground text-xs">Context Window</div>
95 {usage.maxTokens !== null && usedPercentage ? (
96 <div className="text-secondary-label text-[11px] tabular-nums">
97 <span>{usedPercentage}</span>
98 <span className="mx-1">·</span>
99 <span>
100 {formatContextWindowTokens(usage.usedTokens)}/
101 {formatContextWindowTokens(usage.maxTokens ?? null)}
102 </span>
103 </div>
104 ) : (
105 <div className="text-secondary-label text-[11px] tabular-nums">
106 {formatContextWindowTokens(usage.usedTokens)}
107 </div>
108 )}
109 </div>
110 {usage.maxTokens !== null ? (
111 <div
112 className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
113 role="progressbar"
114 aria-valuemin={0}
115 aria-valuemax={100}
116 aria-valuenow={Math.round(normalizedPercentage)}
117 aria-label="Context window usage"
118 >
119 <div
120 className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
121 style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
122 />
123 </div>
124 ) : null}
125 {showTotalProcessed ? (
126 <div className="flex items-center justify-between gap-3 text-[11px] leading-4">
127 <span className="text-secondary-label">Total processed</span>
128 <span className="font-medium tabular-nums text-secondary-label">
129 {formatContextWindowTokens(totalProcessedTokens)}
130 </span>
131 </div>
132 ) : null}
133 {usage.compactsAutomatically ? (
134 <div className="mt-1 text-pretty text-secondary-label text-[11px] font-medium">
135 {formatContextWindowCompactionMessage(modelDisplayName, usage.autoCompactThreshold)}
136 </div>
137 ) : null}
138 {onCompact ? (
139 <>
140 <Button
141 size="xs"
142 variant="outline"
143 className="mt-1 w-full justify-center"
144 disabled={compactDisabled}
145 onClick={onCompact}
146 >
147 <Minimize2Icon aria-hidden="true" />
148 Compact context
149 </Button>
150 {compactDisabled && compactDisabledReason ? (
151 <div className="text-pretty text-secondary-label text-[11px]">
152 {compactDisabledReason}
153 </div>
154 ) : null}
155 </>
156 ) : null}
157 </div>
158 </PopoverPopup>
159 </Popover>
160 );