diff --git a/lightrag_webui/src/components/retrieval/ChatMessage.tsx b/lightrag_webui/src/components/retrieval/ChatMessage.tsx index 80956677..f1709004 100644 --- a/lightrag_webui/src/components/retrieval/ChatMessage.tsx +++ b/lightrag_webui/src/components/retrieval/ChatMessage.tsx @@ -116,6 +116,12 @@ const isInlineCode = (node?: Element): boolean => { }; +// Check if it is a large JSON +const isLargeJson = (language: string | undefined, content: string | undefined): boolean => { + if (!content || language !== 'json') return false; + return content.length > 5000; // JSON larger than 5KB is considered large JSON +}; + // Memoize the CodeHighlight component const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false, ...props }: CodeHighlightProps) => { const { theme } = useTheme(); @@ -126,6 +132,10 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false const mermaidRef = useRef(null); const debounceTimerRef = useRef | null>(null); // Use ReturnType for better typing + // Get the content string, check if it is a large JSON + const contentStr = String(children || '').replace(/\n$/, ''); + const isLargeJsonBlock = isLargeJson(language, contentStr); + // Handle Mermaid rendering with debounce useEffect(() => { // Effect should run when renderAsDiagram becomes true or hasRendered changes. @@ -247,11 +257,19 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false } }; // Dependencies: renderAsDiagram ensures effect runs when diagram should be shown. - // children, language, theme trigger re-render if code/context changes. // Dependencies include all values used inside the effect to satisfy exhaustive-deps. // The !hasRendered check prevents re-execution of render logic after success. }, [renderAsDiagram, hasRendered, language, children, theme]); // Add children and theme back + // For large JSON, skip syntax highlighting completely and use a simple pre tag + if (isLargeJsonBlock) { + return ( +
+        {contentStr}
+      
+ ); + } + // Render based on language type // If it's a mermaid language block and rendering as diagram is not requested (e.g., incomplete stream), display as plain text if (language === 'mermaid' && !renderAsDiagram) { @@ -262,7 +280,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false language="text" // Use text as language to avoid syntax highlighting errors {...props} > - {String(children).replace(/\n$/, '')} + {contentStr} ); } @@ -273,6 +291,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false return
; } + // Handle non-Mermaid code blocks return !inline ? ( - {String(children).replace(/\n$/, '')} + {contentStr} ) : ( // Handle inline code {children}