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

ContextWindowMeter.tsx

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