"use client"; import { useRef, useEffect, useState, useCallback } from "react"; import Image from "next/image"; import { ScrollArea } from "@/components/ui/scroll-area"; import ExamplePanel from "./chat-example-panel"; import { UIMessage } from "ai"; import { convertToLegalXml, replaceNodes } from "@/lib/utils"; import { Copy, Check, X } from "lucide-react"; import { useDiagram } from "@/contexts/diagram-context"; const getMessageTextContent = (message: UIMessage): string => { if (!message.parts) return ""; return message.parts .filter((part: any) => part.type === "text") .map((part: any) => part.text) .join("\n"); }; interface ChatMessageDisplayProps { messages: UIMessage[]; error?: Error | null; setInput: (input: string) => void; setFiles: (files: File[]) => void; } export function ChatMessageDisplay({ messages, error, setInput, setFiles, }: ChatMessageDisplayProps) { const { chartXML, loadDiagram: onDisplayChart } = useDiagram(); const messagesEndRef = useRef(null); const previousXML = useRef(""); const processedToolCalls = useRef>(new Set()); const [expandedTools, setExpandedTools] = useState>( {} ); const [copiedMessageId, setCopiedMessageId] = useState(null); const [copyFailedMessageId, setCopyFailedMessageId] = useState(null); const copyMessageToClipboard = async (messageId: string, text: string) => { try { await navigator.clipboard.writeText(text); setCopiedMessageId(messageId); setTimeout(() => setCopiedMessageId(null), 2000); } catch (err) { console.error("Failed to copy message:", err); setCopyFailedMessageId(messageId); setTimeout(() => setCopyFailedMessageId(null), 2000); } }; const handleDisplayChart = useCallback( (xml: string) => { const currentXml = xml || ""; const convertedXml = convertToLegalXml(currentXml); if (convertedXml !== previousXML.current) { previousXML.current = convertedXml; const replacedXML = replaceNodes(chartXML, convertedXml); onDisplayChart(replacedXML); } }, [chartXML, onDisplayChart] ); useEffect(() => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: "smooth" }); } }, [messages]); // Handle tool invocations and update diagram when needed useEffect(() => { messages.forEach((message) => { if (message.parts) { message.parts.forEach((part: any) => { if (part.type?.startsWith("tool-")) { const { toolCallId, state } = part; // Auto-collapse args when diagrams are generated if (state === "output-available") { setExpandedTools((prev) => ({ ...prev, [toolCallId]: false, })); } // Handle diagram updates for display_diagram tool if ( part.type === "tool-display_diagram" && part.input?.xml ) { // For streaming input, always update to show streaming if ( state === "input-streaming" || state === "input-available" ) { handleDisplayChart(part.input.xml); } // For completed calls, only update if not processed yet else if ( state === "output-available" && !processedToolCalls.current.has(toolCallId) ) { handleDisplayChart(part.input.xml); processedToolCalls.current.add(toolCallId); } } } }); } }); }, [messages, handleDisplayChart]); const renderToolPart = (part: any) => { const callId = part.toolCallId; const { state, input, output } = part; const isExpanded = expandedTools[callId] ?? true; const toolName = part.type?.replace("tool-", ""); const toggleExpanded = () => { setExpandedTools((prev) => ({ ...prev, [callId]: !isExpanded, })); }; return (
Tool: {toolName}
{input && Object.keys(input).length > 0 && ( )}
{input && isExpanded && (
{typeof input === "object" && Object.keys(input).length > 0 && `Input: ${JSON.stringify(input, null, 2)}`}
)}
{state === "input-streaming" ? (
) : state === "output-available" ? (
{output || (toolName === "display_diagram" ? "Diagram generated" : toolName === "edit_diagram" ? "Diagram edited" : "Tool executed")}
) : state === "output-error" ? (
{output || (toolName === "display_diagram" ? "Error generating diagram" : toolName === "edit_diagram" ? "Error editing diagram" : "Tool error")}
) : null}
); }; return ( {messages.length === 0 ? ( ) : ( messages.map((message) => { const userMessageText = message.role === "user" ? getMessageTextContent(message) : ""; return (
{message.role === "user" && userMessageText && ( )}
{message.parts?.map((part: any, index: number) => { switch (part.type) { case "text": return (
{part.text}
); case "file": return (
{`Uploaded
); default: if (part.type?.startsWith("tool-")) { return renderToolPart(part); } return null; } })}
); }) )} {error && (
Error: {error.message}
)}
); }