"use client" import type React from "react" import { useRef, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { ScrollArea } from "@/components/ui/scroll-area" import { Loader2, Send } from "lucide-react" import { useChat } from '@ai-sdk/react'; interface ChatPanelProps { onDisplayChart: (xml: string) => void; onFetchChart: () => Promise; } export default function ChatPanel({ onDisplayChart, onFetchChart }: ChatPanelProps) { const { messages, input, handleInputChange, handleSubmit, isLoading, error, addToolResult } = useChat({ 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_flow_chart") { const { xml } = toolCall.args as { xml: string }; onDisplayChart(xml); return "Displaying the flowchart..."; } else if (toolCall.toolName === "fetch_flow_chart") { const currentXML = await onFetchChart(); console.log("Current XML:", currentXML); return currentXML; } }, 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" }) } }, [messages]) const onFormSubmit = (e: React.FormEvent) => { e.preventDefault() if (input.trim() && !isLoading) { handleSubmit(e) } } // Helper function to render tool invocations const renderToolInvocation = (toolInvocation: any) => { const callId = toolInvocation.toolCallId; switch (toolInvocation.toolName) { case 'display_flow_chart': { switch (toolInvocation.state) { case 'call': case 'partial-call': return (
Displaying flowchart...
Tool: display_flow_chart
); case 'result': return (
Flowchart displayed
Result: {toolInvocation.result}
); } break; } case 'fetch_flow_chart': { switch (toolInvocation.state) { case 'call': case 'partial-call': return (
Fetching current flowchart...
Tool: fetch_flow_chart
); case 'result': return (
Flowchart fetched
Successfully retrieved current diagram
); } 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}
)}
) }