'use client'; /* eslint-disable @next/next/no-img-element */ import React, { MutableRefObject, useEffect, useState } from 'react'; import { Message } from './ChatWindow'; import { cn } from '@/lib/utils'; import { BookCopy, Disc3, Volume2, StopCircle, Layers3, Plus, } from 'lucide-react'; import Markdown, { MarkdownToJSX } from 'markdown-to-jsx'; import Copy from './MessageActions/Copy'; import Rewrite from './MessageActions/Rewrite'; import MessageSources from './MessageSources'; import SearchImages from './SearchImages'; import SearchVideos from './SearchVideos'; import { useSpeech } from 'react-text-to-speech'; import ThinkBox from './ThinkBox'; const ThinkTagProcessor = ({ children }: { children: React.ReactNode }) => { return ; }; const MessageBox = ({ message, messageIndex, history, loading, dividerRef, isLast, rewrite, sendMessage, }: { message: Message; messageIndex: number; history: Message[]; loading: boolean; dividerRef?: MutableRefObject; isLast: boolean; rewrite: (messageId: string) => void; sendMessage: (message: string) => void; }) => { const [parsedMessage, setParsedMessage] = useState(message.content); const [speechMessage, setSpeechMessage] = useState(message.content); useEffect(() => { const citationRegex = /\[([^\]]+)\]/g; const regex = /\[(\d+)\]/g; let processedMessage = message.content; if (message.role === 'assistant' && message.content.includes('')) { const openThinkTag = processedMessage.match(//g)?.length || 0; const closeThinkTag = processedMessage.match(/<\/think>/g)?.length || 0; if (openThinkTag > closeThinkTag) { processedMessage += ' '; // The extra is to prevent the the think component from looking bad } } if ( message.role === 'assistant' && message?.sources && message.sources.length > 0 ) { setParsedMessage( processedMessage.replace( citationRegex, // Use the updated regex (match, capturedContent) => { // match is the full "[1,2,3]", capturedContent is "1,2,3" // Split the captured content by comma, trim whitespace, and filter out non-digits const numbers = capturedContent .split(',') .map(numStr => numStr.trim()) .filter(numStr => /^\d+$/.test(numStr)); // Ensure it's only digits // If no valid numbers found after split/filter (e.g., "[]" or "[abc]"), return original match if (numbers.length === 0) { return match; } // Generate an HTML link for each valid number found const linksHtml = numbers.map(numStr => { const number = parseInt(numStr, 10); // Convert string to integer for array indexing // Basic validation: Ensure it's a positive number if (isNaN(number) || number <= 0) { // Return the original number part as text if invalid for lookup return `[${numStr}]`; } // Get the corresponding source, adjusting for 0-based index const source = message.sources?.[number - 1]; const url = source?.metadata?.url; // If URL exists, create the link if (url) { return `${numStr}`; } else { // If no URL found for this number, return the number styled similarly but red return `${numStr}`; } }).join(''); // Join the generated links (or fallback text) together without separators return linksHtml; } ) ); return; } setSpeechMessage(message.content.replace(regex, '')); setParsedMessage(processedMessage); }, [message.content, message.sources, message.role]); const { speechStatus, start, stop } = useSpeech({ text: speechMessage }); const markdownOverrides: MarkdownToJSX.Options = { overrides: { think: { component: ThinkTagProcessor, }, }, }; return (
{message.role === 'user' && (

{message.content}

)} {message.role === 'assistant' && (
{message.sources && message.sources.length > 0 && (

Sources

)}

Answer

{parsedMessage} {loading && isLast ? null : (
{/* */}
)} {isLast && message.suggestions && message.suggestions.length > 0 && message.role === 'assistant' && !loading && ( <>

Related

{message.suggestions.map((suggestion, i) => (
{ sendMessage(suggestion); }} className="cursor-pointer flex flex-row justify-between font-medium space-x-2 items-center" >

{suggestion}

))}
)}
)}
); }; export default MessageBox;