Просмотр исходного кода

fix: handle null/undefined token values in ContextCondenseRow to prevent UI crash (#6916)

* fix: handle null/undefined token values in ContextCondenseRow to prevent UI crash

- Added null/undefined checks for prevContextTokens, newContextTokens, and cost
- Default to 0 when values are null or undefined
- Added comprehensive test coverage for edge cases
- Fixes #6914

* Delete webview-ui/src/components/chat/__tests__/ContextCondenseRow.spec.tsx

---------

Co-authored-by: Roo Code <[email protected]>
Co-authored-by: Daniel <[email protected]>
roomote[bot] 4 месяцев назад
Родитель
Сommit
b2fdb9ac57
1 измененных файлов с 9 добавлено и 2 удалено
  1. 9 2
      webview-ui/src/components/chat/ContextCondenseRow.tsx

+ 9 - 2
webview-ui/src/components/chat/ContextCondenseRow.tsx

@@ -11,6 +11,11 @@ export const ContextCondenseRow = ({ cost, prevContextTokens, newContextTokens,
 	const { t } = useTranslation()
 	const [isExpanded, setIsExpanded] = useState(false)
 
+	// Handle null/undefined token values to prevent crashes
+	const prevTokens = prevContextTokens ?? 0
+	const newTokens = newContextTokens ?? 0
+	const displayCost = cost ?? 0
+
 	return (
 		<div className="mb-2">
 			<div
@@ -33,9 +38,11 @@ export const ContextCondenseRow = ({ cost, prevContextTokens, newContextTokens,
 					<span className="codicon codicon-compress text-blue-400" />
 					<span className="font-bold text-vscode-foreground">{t("chat:contextCondense.title")}</span>
 					<span className="text-vscode-descriptionForeground text-sm">
-						{prevContextTokens.toLocaleString()} → {newContextTokens.toLocaleString()} {t("tokens")}
+						{prevTokens.toLocaleString()} → {newTokens.toLocaleString()} {t("tokens")}
 					</span>
-					<VSCodeBadge className={cost > 0 ? "opacity-100" : "opacity-0"}>${cost.toFixed(2)}</VSCodeBadge>
+					<VSCodeBadge className={displayCost > 0 ? "opacity-100" : "opacity-0"}>
+						${displayCost.toFixed(2)}
+					</VSCodeBadge>
 				</div>
 				<span className={`codicon codicon-chevron-${isExpanded ? "up" : "down"}`}></span>
 			</div>