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

ContextWindowMeter.tsx

179 lines7.0 KB

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