diff --git a/ui/components/MessageBox.tsx b/ui/components/MessageBox.tsx index e32ee0e..c31ac65 100644 --- a/ui/components/MessageBox.tsx +++ b/ui/components/MessageBox.tsx @@ -49,27 +49,37 @@ const MessageBox = ({ useEffect(() => { const regex = /\[(\d+)\]/g; + const thinkRegex = /(.*?)(?:<\/think>|$)(.*)/s; - // First check for thinking content - const match = message.content.match(/(.*?)<\/think>(.*)/s); + // Check for thinking content, including partial tags + const match = message.content.match(thinkRegex); if (match) { const [_, thinkingContent, answerContent] = match; - setThinking(thinkingContent.trim()); - setAnswer(answerContent.trim()); - - // Process the answer part for sources if needed - if (message.role === 'assistant' && message?.sources && message.sources.length > 0) { - setParsedMessage( - answerContent.trim().replace( - regex, - (_, number) => - `${number}`, - ), - ); - } else { - setParsedMessage(answerContent.trim()); + + // Set thinking content even if hasn't appeared yet + if (thinkingContent) { + setThinking(thinkingContent.trim()); + setIsThinkingExpanded(true); // Auto-expand when thinking starts + } + + // Only set answer content if we have it (after ) + if (answerContent) { + setAnswer(answerContent.trim()); + + // Process the answer part for sources if needed + if (message.role === 'assistant' && message?.sources && message.sources.length > 0) { + setParsedMessage( + answerContent.trim().replace( + regex, + (_, number) => + `${number}`, + ), + ); + } else { + setParsedMessage(answerContent.trim()); + } + setSpeechMessage(answerContent.trim().replace(regex, '')); } - setSpeechMessage(answerContent.trim().replace(regex, '')); } else { // No thinking content - process as before if (message.role === 'assistant' && message?.sources && message.sources.length > 0) { @@ -105,37 +115,6 @@ const MessageBox = ({ ref={dividerRef} className="flex flex-col space-y-6 w-full lg:w-9/12" > - {thinking && ( -
- - - {isThinkingExpanded && ( -
- - {thinking} - -
- )} -
- )} {message.sources && message.sources.length > 0 && (
@@ -147,27 +126,71 @@ const MessageBox = ({
)} -
-
- + {thinking && ( +
+ + + {isThinkingExpanded && ( +
+ {thinking.split('\n\n').map((paragraph, index) => { + if (!paragraph.trim()) return null; + + const content = paragraph.replace(/^[•\-\d.]\s*/, ''); + + return ( +
+
+ + +

+ {content} +

+
+
+
+ ); + })} +
)} - size={20} - /> -

- Answer -

+
+ )} + +
+
+ +

+ Answer +

+
+ + {parsedMessage} +
- - {parsedMessage} - {loading && isLast ? null : (
@@ -254,4 +277,4 @@ const MessageBox = ({ ); }; -export default MessageBox; \ No newline at end of file +export default MessageBox;