yangdx commited on
Commit
ac8ad08
·
1 Parent(s): ba1d878

Postpond mermaid rendering untill streaming streaming respond stopped.

Browse files
lightrag_webui/src/components/retrieval/ChatMessage.tsx CHANGED
@@ -22,7 +22,7 @@ export type MessageWithError = Message & {
22
  isError?: boolean
23
  }
24
 
25
- export const ChatMessage = ({ message }: { message: MessageWithError }) => {
26
  const { t } = useTranslation()
27
  const handleCopyMarkdown = useCallback(async () => {
28
  if (message.content) {
@@ -51,7 +51,7 @@ export const ChatMessage = ({ message }: { message: MessageWithError }) => {
51
  rehypePlugins={[rehypeReact]}
52
  skipHtml={false}
53
  components={{
54
- code: CodeHighlight,
55
  p: ({ children }) => <p className="my-2">{children}</p>,
56
  h1: ({ children }) => <h1 className="text-xl font-bold mt-4 mb-2">{children}</h1>,
57
  h2: ({ children }) => <h2 className="text-lg font-bold mt-4 mb-2">{children}</h2>,
@@ -86,6 +86,7 @@ interface CodeHighlightProps {
86
  className?: string
87
  children?: ReactNode
88
  node?: Element // Keep node for inline check
 
89
  }
90
 
91
  // Helper function remains the same
@@ -100,7 +101,7 @@ const isInlineCode = (node?: Element): boolean => {
100
  };
101
 
102
 
103
- const CodeHighlight = ({ className, children, node, ...props }: CodeHighlightProps) => {
104
  const { theme } = useTheme();
105
  const match = className?.match(/language-(\w+)/);
106
  const language = match ? match[1] : undefined;
@@ -233,6 +234,21 @@ const CodeHighlight = ({ className, children, node, ...props }: CodeHighlightPro
233
  }, [language, children, theme]); // Dependencies
234
 
235
  // Render based on language type
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  if (language === 'mermaid') {
237
  // Container for Mermaid diagram
238
  return <div className="mermaid-diagram-container my-4 overflow-x-auto" ref={mermaidRef}></div>;
 
22
  isError?: boolean
23
  }
24
 
25
+ export const ChatMessage = ({ message, isComplete = true }: { message: MessageWithError, isComplete?: boolean }) => {
26
  const { t } = useTranslation()
27
  const handleCopyMarkdown = useCallback(async () => {
28
  if (message.content) {
 
51
  rehypePlugins={[rehypeReact]}
52
  skipHtml={false}
53
  components={{
54
+ code: (props) => <CodeHighlight {...props} isComplete={isComplete} />,
55
  p: ({ children }) => <p className="my-2">{children}</p>,
56
  h1: ({ children }) => <h1 className="text-xl font-bold mt-4 mb-2">{children}</h1>,
57
  h2: ({ children }) => <h2 className="text-lg font-bold mt-4 mb-2">{children}</h2>,
 
86
  className?: string
87
  children?: ReactNode
88
  node?: Element // Keep node for inline check
89
+ isComplete?: boolean // Flag to indicate if the message is complete
90
  }
91
 
92
  // Helper function remains the same
 
101
  };
102
 
103
 
104
+ const CodeHighlight = ({ className, children, node, isComplete = true, ...props }: CodeHighlightProps) => {
105
  const { theme } = useTheme();
106
  const match = className?.match(/language-(\w+)/);
107
  const language = match ? match[1] : undefined;
 
234
  }, [language, children, theme]); // Dependencies
235
 
236
  // Render based on language type
237
+ // If it's a mermaid language block and the message is not complete, display as plain text
238
+ if (language === 'mermaid' && !isComplete) {
239
+ return (
240
+ <SyntaxHighlighter
241
+ style={theme === 'dark' ? oneDark : oneLight}
242
+ PreTag="div"
243
+ language="text" // Use text as language to avoid syntax highlighting errors
244
+ {...props}
245
+ >
246
+ {String(children).replace(/\n$/, '')}
247
+ </SyntaxHighlighter>
248
+ );
249
+ }
250
+
251
+ // If it's a mermaid language block and the message is complete, render as diagram
252
  if (language === 'mermaid') {
253
  // Container for Mermaid diagram
254
  return <div className="mermaid-diagram-container my-4 overflow-x-auto" ref={mermaidRef}></div>;
lightrag_webui/src/features/RetrievalTesting.tsx CHANGED
@@ -279,14 +279,23 @@ export default function RetrievalTesting() {
279
  {t('retrievePanel.retrieval.startPrompt')}
280
  </div>
281
  ) : (
282
- messages.map((message, idx) => (
283
- <div
284
- key={idx}
285
- className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
286
- >
287
- {<ChatMessage message={message} />}
288
- </div>
289
- ))
 
 
 
 
 
 
 
 
 
290
  )}
291
  <div ref={messagesEndRef} className="pb-1" />
292
  </div>
 
279
  {t('retrievePanel.retrieval.startPrompt')}
280
  </div>
281
  ) : (
282
+ messages.map((message, idx) => {
283
+ // Determine if this message is complete:
284
+ // 1. If it's not the last message, it's complete
285
+ // 2. If it's the last message but we're not receiving a streaming response, it's complete
286
+ // 3. If it's the last message and we're receiving a streaming response, it's not complete
287
+ const isLastMessage = idx === messages.length - 1;
288
+ const isMessageComplete = !isLastMessage || !isReceivingResponseRef.current;
289
+
290
+ return (
291
+ <div
292
+ key={idx}
293
+ className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
294
+ >
295
+ {<ChatMessage message={message} isComplete={isMessageComplete} />}
296
+ </div>
297
+ );
298
+ })
299
  )}
300
  <div ref={messagesEndRef} className="pb-1" />
301
  </div>