|
| 1 | +import { useMemo } from "react" |
| 2 | +import { useTranslation } from "react-i18next" |
| 3 | + |
| 4 | +import { formatLargeNumber } from "@/utils/format" |
| 5 | +import { calculateTokenDistribution } from "@/utils/model-utils" |
| 6 | + |
| 7 | +interface ContextWindowProgressProps { |
| 8 | + contextWindow: number |
| 9 | + contextTokens: number |
| 10 | + maxTokens?: number |
| 11 | +} |
| 12 | + |
| 13 | +export const ContextWindowProgress = ({ contextWindow, contextTokens, maxTokens }: ContextWindowProgressProps) => { |
| 14 | + const { t } = useTranslation() |
| 15 | + // Use the shared utility function to calculate all token distribution values |
| 16 | + const tokenDistribution = useMemo( |
| 17 | + () => calculateTokenDistribution(contextWindow, contextTokens, maxTokens), |
| 18 | + [contextWindow, contextTokens, maxTokens], |
| 19 | + ) |
| 20 | + |
| 21 | + // Destructure the values we need |
| 22 | + const { currentPercent, reservedPercent, availableSize, reservedForOutput, availablePercent } = tokenDistribution |
| 23 | + |
| 24 | + // For display purposes |
| 25 | + const safeContextWindow = Math.max(0, contextWindow) |
| 26 | + const safeContextTokens = Math.max(0, contextTokens) |
| 27 | + |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <div className="flex items-center gap-2 flex-1 whitespace-nowrap px-2"> |
| 31 | + <div data-testid="context-tokens-count">{formatLargeNumber(safeContextTokens)}</div> |
| 32 | + <div className="flex-1 relative"> |
| 33 | + {/* Invisible overlay for hover area */} |
| 34 | + <div |
| 35 | + className="absolute w-full h-4 -top-[7px] z-5" |
| 36 | + title={t("chat:tokenProgress.availableSpace", { amount: formatLargeNumber(availableSize) })} |
| 37 | + data-testid="context-available-space" |
| 38 | + /> |
| 39 | + |
| 40 | + {/* Main progress bar container */} |
| 41 | + <div className="flex items-center h-1 rounded-[2px] overflow-hidden w-full bg-[color-mix(in_srgb,var(--vscode-foreground)_20%,transparent)]"> |
| 42 | + {/* Current tokens container */} |
| 43 | + <div className="relative h-full" style={{ width: `${currentPercent}%` }}> |
| 44 | + {/* Invisible overlay for current tokens section */} |
| 45 | + <div |
| 46 | + className="absolute h-4 -top-[7px] w-full z-6" |
| 47 | + title={t("chat:tokenProgress.tokensUsed", { |
| 48 | + used: formatLargeNumber(safeContextTokens), |
| 49 | + total: formatLargeNumber(safeContextWindow), |
| 50 | + })} |
| 51 | + data-testid="context-tokens-used" |
| 52 | + /> |
| 53 | + {/* Current tokens used - darkest */} |
| 54 | + <div className="h-full w-full bg-[var(--vscode-foreground)] transition-width duration-300 ease-out" /> |
| 55 | + </div> |
| 56 | + |
| 57 | + {/* Container for reserved tokens */} |
| 58 | + <div className="relative h-full" style={{ width: `${reservedPercent}%` }}> |
| 59 | + {/* Invisible overlay for reserved section */} |
| 60 | + <div |
| 61 | + className="absolute h-4 -top-[7px] w-full z-6" |
| 62 | + title={t("chat:tokenProgress.reservedForResponse", { |
| 63 | + amount: formatLargeNumber(reservedForOutput), |
| 64 | + })} |
| 65 | + data-testid="context-reserved-tokens" |
| 66 | + /> |
| 67 | + {/* Reserved for output section - medium gray */} |
| 68 | + <div className="h-full w-full bg-[color-mix(in_srgb,var(--vscode-foreground)_30%,transparent)] transition-width duration-300 ease-out" /> |
| 69 | + </div> |
| 70 | + |
| 71 | + {/* Empty section (if any) */} |
| 72 | + {availablePercent > 0 && ( |
| 73 | + <div className="relative h-full" style={{ width: `${availablePercent}%` }}> |
| 74 | + {/* Invisible overlay for available space */} |
| 75 | + <div |
| 76 | + className="absolute h-4 -top-[7px] w-full z-6" |
| 77 | + title={t("chat:tokenProgress.availableSpace", { |
| 78 | + amount: formatLargeNumber(availableSize), |
| 79 | + })} |
| 80 | + data-testid="context-available-space-section" |
| 81 | + /> |
| 82 | + </div> |
| 83 | + )} |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + <div data-testid="context-window-size">{formatLargeNumber(safeContextWindow)}</div> |
| 87 | + </div> |
| 88 | + </> |
| 89 | + ) |
| 90 | +} |
0 commit comments