Skip to content

Commit ade1425

Browse files
authored
Merge pull request HKUDS#1539 from danielaskdd/json-code-block-freezing
Resolve json code block freezing issue in WebUI
2 parents 7ff23dd + ee420f1 commit ade1425

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

‎lightrag/api/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__api_version__ = "0167"
1+
__api_version__ = "0168"

‎lightrag/api/webui/assets/feature-retrieval-C3RSdrRs.js‎ renamed to ‎lightrag/api/webui/assets/feature-retrieval-BWPImNx0.js‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lightrag/api/webui/assets/index-DPk-nJzB.js‎ renamed to ‎lightrag/api/webui/assets/index-CvkPVz55.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lightrag/api/webui/index.html‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lightrag_webui/src/components/retrieval/ChatMessage.tsx‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ const isInlineCode = (node?: Element): boolean => {
116116
};
117117

118118

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+
119125
// Memoize the CodeHighlight component
120126
const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false, ...props }: CodeHighlightProps) => {
121127
const { theme } = useTheme();
@@ -126,6 +132,10 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
126132
const mermaidRef = useRef<HTMLDivElement>(null);
127133
const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); // Use ReturnType for better typing
128134

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+
129139
// Handle Mermaid rendering with debounce
130140
useEffect(() => {
131141
// Effect should run when renderAsDiagram becomes true or hasRendered changes.
@@ -247,11 +257,19 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
247257
}
248258
};
249259
// Dependencies: renderAsDiagram ensures effect runs when diagram should be shown.
250-
// children, language, theme trigger re-render if code/context changes.
251260
// Dependencies include all values used inside the effect to satisfy exhaustive-deps.
252261
// The !hasRendered check prevents re-execution of render logic after success.
253262
}, [renderAsDiagram, hasRendered, language, children, theme]); // Add children and theme back
254263

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+
255273
// Render based on language type
256274
// If it's a mermaid language block and rendering as diagram is not requested (e.g., incomplete stream), display as plain text
257275
if (language === 'mermaid' && !renderAsDiagram) {
@@ -262,7 +280,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
262280
language="text" // Use text as language to avoid syntax highlighting errors
263281
{...props}
264282
>
265-
{String(children).replace(/\n$/, '')}
283+
{contentStr}
266284
</SyntaxHighlighter>
267285
);
268286
}
@@ -273,6 +291,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
273291
return <div className="mermaid-diagram-container my-4 overflow-x-auto" ref={mermaidRef}></div>;
274292
}
275293

294+
276295
// Handle non-Mermaid code blocks
277296
return !inline ? (
278297
<SyntaxHighlighter
@@ -281,12 +300,12 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
281300
language={language}
282301
{...props}
283302
>
284-
{String(children).replace(/\n$/, '')}
303+
{contentStr}
285304
</SyntaxHighlighter>
286305
) : (
287306
// Handle inline code
288307
<code
289-
className={cn(className, 'mx-1 rounded-sm bg-muted px-1 py-0.5 font-mono text-sm')} // 添加 font-mono 确保使用等宽字体
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
290309
{...props}
291310
>
292311
{children}

0 commit comments

Comments
 (0)