From ac1c2ce0442fc79a4331268ab973ceaaed2ba25e Mon Sep 17 00:00:00 2001 From: Dayuan Jiang <34411969+DayuanJiang@users.noreply.github.com> Date: Sun, 14 Dec 2025 21:49:08 +0900 Subject: [PATCH] fix: remove overly aggressive message filtering on restore (#263) The hasValidDiagramXml filter was deleting valid messages that had minor XML issues. Error handling in handleDisplayChart now catches all errors, so filtering is no longer needed - invalid XML just won't load the diagram but the conversation is preserved. --- components/chat-panel.tsx | 59 +-------------------------------------- 1 file changed, 1 insertion(+), 58 deletions(-) diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx index 7ed54a8..17ee579 100644 --- a/components/chat-panel.tsx +++ b/components/chat-panel.tsx @@ -89,37 +89,6 @@ function hasToolErrors(messages: ChatMessage[]): boolean { return lastToolPart?.state === TOOL_ERROR_STATE } -/** - * Check if a message contains valid diagram XML. - * Used to filter out corrupted messages when restoring from localStorage. - * Validates both display_diagram and append_diagram tool calls. - */ -function hasValidDiagramXml(message: { - parts?: Array<{ type?: string; input?: unknown }> -}): boolean { - if (!message.parts) return true // No parts = valid (user messages, text-only) - - const parser = new DOMParser() - for (const part of message.parts) { - // Check both display_diagram and append_diagram tools - const isDiagramTool = - part.type === "tool-display_diagram" || - part.type === "tool-append_diagram" - const input = part.input as { xml?: string } | undefined - if (isDiagramTool && input?.xml) { - try { - const doc = parser.parseFromString(input.xml, "text/xml") - if (doc.querySelector("parsererror")) { - return false - } - } catch { - return false - } - } - } - return true -} - export default function ChatPanel({ isVisible, onToggleVisibility, @@ -639,7 +608,6 @@ Continue from EXACTLY where you stopped.`, const messagesEndRef = useRef(null) // Restore messages and XML snapshots from localStorage on mount - // Validates and filters out corrupted messages to prevent crash loops useEffect(() => { if (hasRestoredRef.current) return hasRestoredRef.current = true @@ -650,32 +618,7 @@ Continue from EXACTLY where you stopped.`, if (savedMessages) { const parsed = JSON.parse(savedMessages) if (Array.isArray(parsed) && parsed.length > 0) { - // Filter out messages with invalid XML to prevent crash loops - const validMessages = parsed.filter((msg: ChatMessage) => { - try { - return hasValidDiagramXml(msg) - } catch { - return false - } - }) - - if (validMessages.length < parsed.length) { - const removedCount = - parsed.length - validMessages.length - console.warn( - `[ChatPanel] Filtered ${removedCount} corrupted message(s) from storage`, - ) - toast.warning( - `Removed ${removedCount} message(s) with invalid diagrams to recover session.`, - ) - // Update storage with cleaned messages - localStorage.setItem( - STORAGE_MESSAGES_KEY, - JSON.stringify(validMessages), - ) - } - - setMessages(validMessages) + setMessages(parsed) } }