Open History to see the last commit
1import { Button } from "../ui/button";
2import type { ContextWindowSnapshot } from "@t3tools/client-runtime/state/context-window";
3import { formatContextWindowTokens } from "~/lib/contextWindow";
4import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
5import {
6 formatContextWindowCompactionMessage,
7 formatContextWindowCost,
8} from "./ContextWindowMeter.logic";
9import { Minimize2Icon } from "lucide-react";
10import { composerFloatingLayerProps } from "./composerEventScope";
11
12function formatPercentage(value: number | null): string | null {
13 if (value === null || !Number.isFinite(value)) {
14 return null;
15 }
16 if (value < 10) {
17 return `${value.toFixed(1).replace(/\.0$/, "")}%`;
18 }
19 return `${Math.round(value)}%`;
20}
21
22export function ContextWindowMeter(props: {
23 usage: ContextWindowSnapshot;
24 modelDisplayName?: string | null;
25 onCompact?: (() => void) | undefined;
26 compactDisabled?: boolean | undefined;
27 compactDisabledReason?: string | null | undefined;
28}) {
29 const { usage, modelDisplayName, onCompact, compactDisabled, compactDisabledReason } = props;
30 const usedPercentage = formatPercentage(usage.usedPercentage);
31 const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
32 const radius = 9.75;
33 const circumference = 2 * Math.PI * radius;
34 const dashOffset = circumference * (1 - normalizedPercentage / 100);
35 const totalProcessedTokens = usage.totalProcessedTokens ?? null;
36 const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0;
37 const isOverloaded = normalizedPercentage > 90;
38 const usageColor = isOverloaded
39 ? "var(--color-error)"
40 : "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)";
41
42 return (
43 <Popover>
44 <PopoverTrigger
45 openOnHover
46 delay={150}
47 closeDelay={onCompact ? 150 : 0}
48 render={
49 <Button
50 size="icon-sm"
51 variant="ghost-muted"
52 className="size-7"
53 aria-label={
54 usage.maxTokens !== null && usedPercentage
55 ? `Context window ${usedPercentage} used`
56 : `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
57 }
58 >
59 <span className="relative flex size-5 items-center justify-center">
60 <svg
61 viewBox="0 0 24 24"
62 className="-rotate-90 absolute inset-0 size-full transform-gpu mx-0!"
63 aria-hidden="true"
64 >
65 <circle
66 cx="12"
67 cy="12"
68 r={radius}
69 fill="none"
70 className="stroke-muted-foreground/24"
71 strokeWidth="3"
72 />
73 <circle
74 cx="12"
75 cy="12"
76 r={radius}
77 fill="none"
78 stroke={usageColor}
79 strokeWidth="3"
80 strokeLinecap="round"
81 strokeDasharray={circumference}
82 strokeDashoffset={dashOffset}
83 className="transition-[stroke-dashoffset,stroke] duration-500 ease-out motion-reduce:transition-none"
84 />
85 </svg>
86 </span>
87 </Button>
88 }
89 />
90 <PopoverPopup
91 {...composerFloatingLayerProps}
92 tooltipStyle
93 side="top"
94 align="end"
95 padding="none"
96 width="sm"
97 className="text-left whitespace-normal"
98 >
99 <div className="flex flex-col gap-2 p-(--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-2xs 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-2xs 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-2xs 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 {usage.cost != null ? (
141 <div className="flex items-center justify-between gap-3 text-2xs leading-4">
142 <span className="text-secondary-label">Cost</span>
143 <span className="font-medium tabular-nums text-secondary-label">
144 {formatContextWindowCost(usage.cost)}
145 </span>
146 </div>
147 ) : null}
148 {usage.compactsAutomatically ? (
149 <div className="mt-1 text-pretty text-secondary-label text-2xs font-medium">
150 {formatContextWindowCompactionMessage(modelDisplayName, usage.autoCompactThreshold)}
151 </div>
152 ) : null}
153 {onCompact ? (
154 <>
155 <Button
156 size="xs"
157 variant="outline"
158 className="mt-1 w-full justify-center"
159 disabled={compactDisabled}
160 onClick={onCompact}
161 >
162 <Minimize2Icon aria-hidden="true" />
163 Compact context
164 </Button>
165 {compactDisabled && compactDisabledReason ? (
166 <div className="text-pretty text-secondary-label text-2xs">
167 {compactDisabledReason}
168 </div>
169 ) : null}
170 </>
171 ) : null}
172 </div>
173 </PopoverPopup>
174 </Popover>
175 );
176}
177
178export function ContextWindowMeterPlaceholder() {
179 return <span aria-hidden="true" className="size-7 shrink-0" />;
180}