Files
t3code/apps/web/src/components/chat/ContextWindowMeter.tsx
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

181 lines
6.8 KiB
TypeScript

import { Button } from "../ui/button";
import { type ContextWindowSnapshot, formatContextWindowTokens } from "~/lib/contextWindow";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import {
formatContextWindowCompactionMessage,
formatContextWindowCost,
} from "./ContextWindowMeter.logic";
import { Minimize2Icon } from "lucide-react";
import { composerFloatingLayerProps } from "./composerEventScope";
function formatPercentage(value: number | null): string | null {
if (value === null || !Number.isFinite(value)) {
return null;
}
if (value < 10) {
return `${value.toFixed(1).replace(/\.0$/, "")}%`;
}
return `${Math.round(value)}%`;
}
export function ContextWindowMeter(props: {
usage: ContextWindowSnapshot;
modelDisplayName?: string | null;
onCompact?: (() => void) | undefined;
compactDisabled?: boolean | undefined;
compactDisabledReason?: string | null | undefined;
}) {
const { usage, modelDisplayName, onCompact, compactDisabled, compactDisabledReason } = props;
const usedPercentage = formatPercentage(usage.usedPercentage);
const normalizedPercentage = Math.max(0, Math.min(100, usage.usedPercentage ?? 0));
const radius = 9.75;
const circumference = 2 * Math.PI * radius;
const dashOffset = circumference * (1 - normalizedPercentage / 100);
const totalProcessedTokens = usage.totalProcessedTokens ?? null;
const showTotalProcessed = totalProcessedTokens !== null && totalProcessedTokens > 0;
const isOverloaded = normalizedPercentage > 90;
const usageColor = isOverloaded
? "var(--color-error)"
: "color-mix(in oklab, var(--color-muted-foreground) 72%, transparent)";
return (
<Popover>
<PopoverTrigger
openOnHover
delay={150}
closeDelay={onCompact ? 150 : 0}
render={
<Button
size="icon-sm"
variant="ghost-muted"
className="size-7"
aria-label={
usage.maxTokens !== null && usedPercentage
? `Context window ${usedPercentage} used`
: `Context window ${formatContextWindowTokens(usage.usedTokens)} tokens used`
}
>
<span className="relative flex size-5 items-center justify-center">
<svg
viewBox="0 0 24 24"
className="-rotate-90 absolute inset-0 size-full transform-gpu mx-0!"
aria-hidden="true"
>
<circle
cx="12"
cy="12"
r={radius}
fill="none"
className="stroke-muted-foreground/24"
strokeWidth="3"
/>
<circle
cx="12"
cy="12"
r={radius}
fill="none"
stroke={usageColor}
strokeWidth="3"
strokeLinecap="round"
strokeDasharray={circumference}
strokeDashoffset={dashOffset}
className="transition-[stroke-dashoffset,stroke] duration-500 ease-out motion-reduce:transition-none"
/>
</svg>
</span>
</Button>
}
/>
<PopoverPopup
{...composerFloatingLayerProps}
tooltipStyle
side="top"
align="end"
padding="none"
width="sm"
className="text-left whitespace-normal"
>
<div className="flex flex-col gap-2 p-(--floating-content-inset)">
<div className="flex items-center justify-between gap-3">
<div className="font-medium text-muted-foreground text-xs">Context Window</div>
{usage.maxTokens !== null && usedPercentage ? (
<div className="text-secondary-label text-2xs tabular-nums">
<span>{usedPercentage}</span>
<span className="mx-1">·</span>
<span>
{formatContextWindowTokens(usage.usedTokens)}/
{formatContextWindowTokens(usage.maxTokens ?? null)}
</span>
</div>
) : (
<div className="text-secondary-label text-2xs tabular-nums">
{formatContextWindowTokens(usage.usedTokens)}
</div>
)}
</div>
{usage.maxTokens !== null ? (
<div
className="h-1.5 w-full overflow-hidden rounded-full bg-muted/60"
role="progressbar"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(normalizedPercentage)}
aria-label="Context window usage"
>
<div
className="h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none"
style={{ width: `${normalizedPercentage}%`, backgroundColor: usageColor }}
/>
</div>
) : null}
{showTotalProcessed ? (
<div className="flex items-center justify-between gap-3 text-2xs leading-4">
<span className="text-secondary-label">Total processed</span>
<span className="font-medium tabular-nums text-secondary-label">
{formatContextWindowTokens(totalProcessedTokens)}
</span>
</div>
) : null}
{usage.cost != null ? (
<div className="flex items-center justify-between gap-3 text-2xs leading-4">
<span className="text-secondary-label">Cost</span>
<span className="font-medium tabular-nums text-secondary-label">
{formatContextWindowCost(usage.cost)}
</span>
</div>
) : null}
{usage.compactsAutomatically ? (
<div className="mt-1 text-pretty text-secondary-label text-2xs font-medium">
{formatContextWindowCompactionMessage(modelDisplayName, usage.autoCompactThreshold)}
</div>
) : null}
{onCompact ? (
<>
<Button
size="xs"
variant="outline"
className="mt-1 w-full justify-center"
disabled={compactDisabled}
onClick={onCompact}
>
<Minimize2Icon aria-hidden="true" />
Compact context
</Button>
{compactDisabled && compactDisabledReason ? (
<div className="text-pretty text-secondary-label text-2xs">
{compactDisabledReason}
</div>
) : null}
</>
) : null}
</div>
</PopoverPopup>
</Popover>
);
}
/** Holds the meter's footprint while a thread's activities are still loading. */
export function ContextWindowMeterPlaceholder() {
return <span aria-hidden="true" className="size-7 shrink-0" />;
}