Merge pull request #1539 from danielaskdd/json-code-block-freezing
Browse files
lightrag/api/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1 |
-
__api_version__ = "
|
|
|
1 |
+
__api_version__ = "0168"
|
lightrag/api/webui/assets/{feature-retrieval-C3RSdrRs.js → feature-retrieval-BWPImNx0.js}
RENAMED
Binary files a/lightrag/api/webui/assets/feature-retrieval-C3RSdrRs.js and b/lightrag/api/webui/assets/feature-retrieval-BWPImNx0.js differ
|
|
lightrag/api/webui/assets/{index-DPk-nJzB.js → index-CvkPVz55.js}
RENAMED
Binary files a/lightrag/api/webui/assets/index-DPk-nJzB.js and b/lightrag/api/webui/assets/index-CvkPVz55.js differ
|
|
lightrag/api/webui/index.html
CHANGED
Binary files a/lightrag/api/webui/index.html and b/lightrag/api/webui/index.html differ
|
|
lightrag_webui/src/components/retrieval/ChatMessage.tsx
CHANGED
@@ -116,6 +116,12 @@ const isInlineCode = (node?: Element): boolean => {
|
|
116 |
};
|
117 |
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
// Memoize the CodeHighlight component
|
120 |
const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false, ...props }: CodeHighlightProps) => {
|
121 |
const { theme } = useTheme();
|
@@ -126,6 +132,10 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
|
|
126 |
const mermaidRef = useRef<HTMLDivElement>(null);
|
127 |
const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); // Use ReturnType for better typing
|
128 |
|
|
|
|
|
|
|
|
|
129 |
// Handle Mermaid rendering with debounce
|
130 |
useEffect(() => {
|
131 |
// Effect should run when renderAsDiagram becomes true or hasRendered changes.
|
@@ -247,11 +257,19 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
|
|
247 |
}
|
248 |
};
|
249 |
// Dependencies: renderAsDiagram ensures effect runs when diagram should be shown.
|
250 |
-
// children, language, theme trigger re-render if code/context changes.
|
251 |
// Dependencies include all values used inside the effect to satisfy exhaustive-deps.
|
252 |
// The !hasRendered check prevents re-execution of render logic after success.
|
253 |
}, [renderAsDiagram, hasRendered, language, children, theme]); // Add children and theme back
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
// Render based on language type
|
256 |
// If it's a mermaid language block and rendering as diagram is not requested (e.g., incomplete stream), display as plain text
|
257 |
if (language === 'mermaid' && !renderAsDiagram) {
|
@@ -262,7 +280,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
|
|
262 |
language="text" // Use text as language to avoid syntax highlighting errors
|
263 |
{...props}
|
264 |
>
|
265 |
-
{
|
266 |
</SyntaxHighlighter>
|
267 |
);
|
268 |
}
|
@@ -273,6 +291,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
|
|
273 |
return <div className="mermaid-diagram-container my-4 overflow-x-auto" ref={mermaidRef}></div>;
|
274 |
}
|
275 |
|
|
|
276 |
// Handle non-Mermaid code blocks
|
277 |
return !inline ? (
|
278 |
<SyntaxHighlighter
|
@@ -281,12 +300,12 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
|
|
281 |
language={language}
|
282 |
{...props}
|
283 |
>
|
284 |
-
{
|
285 |
</SyntaxHighlighter>
|
286 |
) : (
|
287 |
// Handle inline code
|
288 |
<code
|
289 |
-
className={cn(className, 'mx-1 rounded-sm bg-muted px-1 py-0.5 font-mono text-sm')} //
|
290 |
{...props}
|
291 |
>
|
292 |
{children}
|
|
|
116 |
};
|
117 |
|
118 |
|
119 |
+
// Check if it is a large JSON
|
120 |
+
const isLargeJson = (language: string | undefined, content: string | undefined): boolean => {
|
121 |
+
if (!content || language !== 'json') return false;
|
122 |
+
return content.length > 5000; // JSON larger than 5KB is considered large JSON
|
123 |
+
};
|
124 |
+
|
125 |
// Memoize the CodeHighlight component
|
126 |
const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false, ...props }: CodeHighlightProps) => {
|
127 |
const { theme } = useTheme();
|
|
|
132 |
const mermaidRef = useRef<HTMLDivElement>(null);
|
133 |
const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); // Use ReturnType for better typing
|
134 |
|
135 |
+
// Get the content string, check if it is a large JSON
|
136 |
+
const contentStr = String(children || '').replace(/\n$/, '');
|
137 |
+
const isLargeJsonBlock = isLargeJson(language, contentStr);
|
138 |
+
|
139 |
// Handle Mermaid rendering with debounce
|
140 |
useEffect(() => {
|
141 |
// Effect should run when renderAsDiagram becomes true or hasRendered changes.
|
|
|
257 |
}
|
258 |
};
|
259 |
// Dependencies: renderAsDiagram ensures effect runs when diagram should be shown.
|
|
|
260 |
// Dependencies include all values used inside the effect to satisfy exhaustive-deps.
|
261 |
// The !hasRendered check prevents re-execution of render logic after success.
|
262 |
}, [renderAsDiagram, hasRendered, language, children, theme]); // Add children and theme back
|
263 |
|
264 |
+
// For large JSON, skip syntax highlighting completely and use a simple pre tag
|
265 |
+
if (isLargeJsonBlock) {
|
266 |
+
return (
|
267 |
+
<pre className="whitespace-pre-wrap break-words bg-muted p-4 rounded-md overflow-x-auto text-sm font-mono">
|
268 |
+
{contentStr}
|
269 |
+
</pre>
|
270 |
+
);
|
271 |
+
}
|
272 |
+
|
273 |
// Render based on language type
|
274 |
// If it's a mermaid language block and rendering as diagram is not requested (e.g., incomplete stream), display as plain text
|
275 |
if (language === 'mermaid' && !renderAsDiagram) {
|
|
|
280 |
language="text" // Use text as language to avoid syntax highlighting errors
|
281 |
{...props}
|
282 |
>
|
283 |
+
{contentStr}
|
284 |
</SyntaxHighlighter>
|
285 |
);
|
286 |
}
|
|
|
291 |
return <div className="mermaid-diagram-container my-4 overflow-x-auto" ref={mermaidRef}></div>;
|
292 |
}
|
293 |
|
294 |
+
|
295 |
// Handle non-Mermaid code blocks
|
296 |
return !inline ? (
|
297 |
<SyntaxHighlighter
|
|
|
300 |
language={language}
|
301 |
{...props}
|
302 |
>
|
303 |
+
{contentStr}
|
304 |
</SyntaxHighlighter>
|
305 |
) : (
|
306 |
// Handle inline code
|
307 |
<code
|
308 |
+
className={cn(className, 'mx-1 rounded-sm bg-muted px-1 py-0.5 font-mono text-sm')} // Add font-mono to ensure monospaced font is used
|
309 |
{...props}
|
310 |
>
|
311 |
{children}
|