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