"use client"; import type React from "react"; 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 { Message } from "ai"; import { convertToLegalXml, replaceNodes } from "@/lib/utils"; import { useDiagram } from "@/contexts/diagram-context"; interface ChatMessageDisplayProps { messages: Message[]; 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 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) => { if (part.type === "tool-invocation") { const { toolCallId, state, args, toolName } = part.toolInvocation; // Auto-collapse args when diagrams are generated if (state === "result") { setExpandedTools((prev) => ({ ...prev, [toolCallId]: false, })); } // Handle diagram updates for display_diagram tool if (toolName === "display_diagram" && args?.xml) { // For partial calls, always update to show streaming if (state === "partial-call") { handleDisplayChart(args.xml); } // For completed calls, only update if not processed yet else if (state === "result" && !processedToolCalls.current.has(toolCallId)) { handleDisplayChart(args.xml); processedToolCalls.current.add(toolCallId); } } } }); } }); }, [messages, handleDisplayChart]); const renderToolInvocation = (toolInvocation: any) => { const callId = toolInvocation.toolCallId; const { toolName, args, state } = toolInvocation; const isExpanded = expandedTools[callId] ?? true; const toggleExpanded = () => { setExpandedTools((prev) => ({ ...prev, [callId]: !isExpanded, })); }; return (
Tool: display_diagram
{args && Object.keys(args).length > 0 && ( )}
{args && isExpanded && (
{typeof args === "object" && Object.keys(args).length > 0 && `Args: ${JSON.stringify(args, null, 2)}`}
)}
{state === "partial-call" ? (
) : state === "result" ? (
Diagram generated
) : null}
); }; return ( {messages.length === 0 ? ( ) : ( messages.map((message) => (
{message.parts ? message.parts.map((part, index) => { switch (part.type) { case "text": return (
{part.text}
); case "tool-invocation": return renderToolInvocation( part.toolInvocation ); default: return null; } }) : message.content}
{message?.experimental_attachments ?.filter((attachment) => attachment?.contentType?.startsWith("image/") ) .map((attachment, index) => (
{
))} {(message as any).function_call && (
Using tool:{" "} {(message as any).function_call.name} ...
)}
)) )} {error && (
Error: {error.message}
)}
); }