"use client" import type React from "react" import { useRef, useEffect, useState } from "react" import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { ScrollArea } from "@/components/ui/scroll-area" import { Button } from "@/components/ui/button" import { useChat } from '@ai-sdk/react'; import { ChatInput } from "@/components/chat-input" interface ChatPanelProps { onDisplayChart: (xml: string) => void; onFetchChart: () => Promise; } export default function ChatPanel({ onDisplayChart, onFetchChart }: ChatPanelProps) { const [previousXml, setPreviousXml] = useState(""); const { messages, input, handleInputChange, handleSubmit, status, error, setInput, setMessages, data } = useChat({ maxSteps: 5, async onToolCall({ toolCall }) { console.log("Tool call:", toolCall); console.log("Tool call name:", toolCall.toolName); console.log("Tool call arguments:", toolCall.args); if (toolCall.toolName === "display_diagram") { const { xml } = toolCall.args as { xml: string }; onDisplayChart(xml); return "Successfully displayed the flowchart."; } }, onError: (error) => { console.error("Chat error:", error); } }) const messagesEndRef = useRef(null) // Scroll to bottom when messages change useEffect(() => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: "smooth" }) } console.log("Data updated:", data); }, [messages]) const onFormSubmit = (e: React.FormEvent) => { e.preventDefault() if (input.trim() && status !== "streaming") { setInput( ` Current diagram XML: """xml ${onFetchChart()} """ User input: """md ${input} """ ` ) handleSubmit(e) } } // Helper function to render tool invocations const renderToolInvocation = (toolInvocation: any) => { const callId = toolInvocation.toolCallId; switch (toolInvocation.toolName) { case 'display_diagram': { switch (toolInvocation.state) { case 'partial-call': { const currentXml = toolInvocation.args?.xml || ""; const shouldShowPartialArgs = currentXml && (!previousXml || Math.abs(currentXml.length - previousXml.length) > 50); // Update previous XML for next comparison if (currentXml) { setPreviousXml(currentXml); } return (
Preparing to display diagram...
Tool: display_diagram {shouldShowPartialArgs && toolInvocation.args && (
Partial args: {JSON.stringify(toolInvocation.args, null, 2)}
)}
); } case 'call': return (
Displaying diagram...
Tool: display_diagram
Args: {JSON.stringify(toolInvocation.args, null, 2)}
); case 'result': return (
Diagram displayed
Result: {toolInvocation.result}
); } break; } default: return null; } }; return ( Chat with Diagram Generator {messages.length === 0 ? (

Start a conversation to generate or modify diagrams.

Try: "Create a flowchart for user authentication"

) : ( messages.map((message) => (
{/* Render message content based on parts if available */} {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; } }) ) : ( // Fallback to simple content for older format message.content )}
{/* Legacy support for function_call format */} {(message as any).function_call && (
Using tool: {(message as any).function_call.name}...
)}
)) )} {error && (
Error: {error.message}
)}
) }