'use client'; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { cn } from '@/lib/utils'; import type { ComponentProps } from 'react'; import { Separator } from '@/components/ui/separator'; import { Progress } from '@/components/ui/progress'; import type { AppUsage } from '@/lib/usage'; export type ContextProps = ComponentProps<'button'> & { /** Optional full usage payload to enable breakdown view */ usage?: AppUsage; }; const THOUSAND = 1000; const MILLION = 1_000_000; const BILLION = 1_000_000_000; const PERCENT_MAX = 100; // Lucide CircleIcon geometry const ICON_VIEWBOX = 24; const ICON_CENTER = 12; const ICON_RADIUS = 10; const ICON_STROKE_WIDTH = 2; type ContextIconProps = { percent: number; // 0 - 100 }; export const ContextIcon = ({ percent }: ContextIconProps) => { const radius = ICON_RADIUS; const circumference = 2 * Math.PI * radius; const dashOffset = circumference * (1 - percent / PERCENT_MAX); return ( ); }; function InfoRow({ label, tokens, costText, }: { label: string; tokens?: number; costText?: string; }) { return (
{label}
{tokens === undefined ? '—' : tokens.toLocaleString()} {costText !== undefined && costText !== null && !isNaN(parseFloat(costText)) && ( ${parseFloat(costText).toFixed(6)} )}
); } export const Context = ({ className, usage, ...props }: ContextProps) => { const used = usage?.totalTokens ?? 0; const max = usage?.context?.totalMax ?? usage?.context?.combinedMax ?? usage?.context?.inputMax; const hasMax = typeof max === 'number' && Number.isFinite(max) && max > 0; const usedPercent = hasMax ? Math.min(100, (used / max) * 100) : 0; return (
{usedPercent.toFixed(1)}% {hasMax ? `${used} / ${max} tokens` : `${used} tokens`}
{usage?.cachedInputTokens && usage.cachedInputTokens > 0 && ( )} 0 ? usage.reasoningTokens : undefined } costText={usage?.costUSD?.reasoningUSD?.toString()} /> {usage?.costUSD?.totalUSD !== undefined && ( <>
Total cost
{!isNaN(parseFloat(usage.costUSD.totalUSD.toString())) ? `$${parseFloat(usage.costUSD.totalUSD.toString()).toFixed(6)}` : '—' }
)}
); };