2025-03-19 07:20:22 +00:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import type React from "react"
|
|
|
|
|
import { useRef, useEffect } from "react"
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-19 07:20:22 +00:00
|
|
|
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
|
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
2025-03-22 14:28:55 +00:00
|
|
|
import { Button } from "@/components/ui/button"
|
2025-03-19 08:16:44 +00:00
|
|
|
import { useChat } from '@ai-sdk/react';
|
2025-03-22 13:15:51 +00:00
|
|
|
import { ChatInput } from "@/components/chat-input"
|
2025-03-19 07:20:22 +00:00
|
|
|
|
2025-03-19 08:16:44 +00:00
|
|
|
interface ChatPanelProps {
|
|
|
|
|
onDisplayChart: (xml: string) => void;
|
2025-03-19 12:14:09 +00:00
|
|
|
onFetchChart: () => Promise<string>;
|
2025-03-19 08:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function ChatPanel({ onDisplayChart, onFetchChart }: ChatPanelProps) {
|
2025-03-22 15:00:40 +00:00
|
|
|
const { messages, input, handleInputChange, handleSubmit, status, error, setInput, setMessages, data } = useChat({
|
2025-03-22 14:28:55 +00:00
|
|
|
maxSteps: 5,
|
2025-03-19 08:16:44 +00:00
|
|
|
async onToolCall({ toolCall }) {
|
|
|
|
|
console.log("Tool call:", toolCall);
|
2025-03-19 11:03:37 +00:00
|
|
|
console.log("Tool call name:", toolCall.toolName);
|
|
|
|
|
console.log("Tool call arguments:", toolCall.args);
|
2025-03-22 14:28:55 +00:00
|
|
|
if (toolCall.toolName === "display_diagram") {
|
2025-03-19 08:16:44 +00:00
|
|
|
const { xml } = toolCall.args as { xml: string };
|
|
|
|
|
onDisplayChart(xml);
|
2025-03-22 13:26:14 +00:00
|
|
|
return "Successfully displayed the flowchart.";
|
2025-03-19 08:16:44 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
console.error("Chat error:", error);
|
|
|
|
|
}
|
2025-03-19 07:20:22 +00:00
|
|
|
})
|
|
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
// Scroll to bottom when messages change
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (messagesEndRef.current) {
|
|
|
|
|
messagesEndRef.current.scrollIntoView({ behavior: "smooth" })
|
|
|
|
|
}
|
2025-03-22 15:00:40 +00:00
|
|
|
console.log("Data updated:", data);
|
2025-03-19 07:20:22 +00:00
|
|
|
}, [messages])
|
|
|
|
|
|
|
|
|
|
const onFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
e.preventDefault()
|
2025-03-22 13:26:14 +00:00
|
|
|
if (input.trim() && status !== "streaming") {
|
2025-03-22 14:28:55 +00:00
|
|
|
setInput(
|
|
|
|
|
`
|
|
|
|
|
Current diagram XML:
|
|
|
|
|
"""xml
|
|
|
|
|
${onFetchChart()}
|
|
|
|
|
"""
|
|
|
|
|
User input:
|
|
|
|
|
"""md
|
|
|
|
|
${input}
|
|
|
|
|
"""
|
|
|
|
|
`
|
|
|
|
|
)
|
2025-03-19 07:20:22 +00:00
|
|
|
handleSubmit(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-19 12:14:09 +00:00
|
|
|
// Helper function to render tool invocations
|
|
|
|
|
const renderToolInvocation = (toolInvocation: any) => {
|
|
|
|
|
const callId = toolInvocation.toolCallId;
|
|
|
|
|
|
|
|
|
|
switch (toolInvocation.toolName) {
|
2025-03-22 15:00:40 +00:00
|
|
|
case 'display_diagram': {
|
2025-03-19 12:14:09 +00:00
|
|
|
switch (toolInvocation.state) {
|
|
|
|
|
case 'call':
|
|
|
|
|
case 'partial-call':
|
|
|
|
|
return (
|
|
|
|
|
<div key={callId} className="mt-2 text-sm bg-yellow-50 p-2 rounded border border-yellow-200">
|
2025-03-22 15:00:40 +00:00
|
|
|
<div className="font-medium">Displaying diagram...</div>
|
2025-03-19 12:14:09 +00:00
|
|
|
<div className="text-xs text-gray-500 mt-1">
|
2025-03-22 15:00:40 +00:00
|
|
|
Tool: display_diagram
|
2025-03-19 12:14:09 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
case 'result':
|
|
|
|
|
return (
|
|
|
|
|
<div key={callId} className="mt-2 text-sm bg-green-50 p-2 rounded border border-green-200">
|
2025-03-22 15:00:40 +00:00
|
|
|
<div className="font-medium">Diagram displayed</div>
|
2025-03-19 12:14:09 +00:00
|
|
|
<div className="text-xs text-gray-500 mt-1">
|
|
|
|
|
Result: {toolInvocation.result}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-19 07:20:22 +00:00
|
|
|
return (
|
2025-03-22 13:15:51 +00:00
|
|
|
<Card className="h-full flex flex-col rounded-none py-0">
|
|
|
|
|
<CardHeader className="p-2 text-center">
|
2025-03-19 07:20:22 +00:00
|
|
|
<CardTitle>Chat with Diagram Generator</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="flex-grow overflow-hidden p-4">
|
|
|
|
|
<ScrollArea className="h-full pr-4">
|
|
|
|
|
{messages.length === 0 ? (
|
|
|
|
|
<div className="text-center text-gray-500 mt-8">
|
2025-03-19 08:16:44 +00:00
|
|
|
<p>Start a conversation to generate or modify diagrams.</p>
|
2025-03-19 07:20:22 +00:00
|
|
|
<p className="text-sm mt-2">Try: "Create a flowchart for user authentication"</p>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
messages.map((message) => (
|
|
|
|
|
<div key={message.id} className={`mb-4 ${message.role === "user" ? "text-right" : "text-left"}`}>
|
|
|
|
|
<div
|
2025-03-22 15:00:40 +00:00
|
|
|
className={`inline-block px-4 py-2 whitespace-pre-wrap text-sm rounded-lg max-w-[85%] break-words ${message.role === "user" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"
|
2025-03-19 07:20:22 +00:00
|
|
|
}`}
|
|
|
|
|
>
|
2025-03-19 12:14:09 +00:00
|
|
|
{/* Render message content based on parts if available */}
|
|
|
|
|
{message.parts ? (
|
|
|
|
|
message.parts.map((part, index) => {
|
|
|
|
|
switch (part.type) {
|
|
|
|
|
case 'text':
|
|
|
|
|
return <div key={index}>{part.text}</div>;
|
|
|
|
|
case 'tool-invocation':
|
|
|
|
|
return renderToolInvocation(part.toolInvocation);
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
) : (
|
|
|
|
|
// Fallback to simple content for older format
|
|
|
|
|
message.content
|
|
|
|
|
)}
|
2025-03-19 07:20:22 +00:00
|
|
|
</div>
|
2025-03-19 12:14:09 +00:00
|
|
|
{/* Legacy support for function_call format */}
|
2025-03-19 08:16:44 +00:00
|
|
|
{(message as any).function_call && (
|
|
|
|
|
<div className="mt-2 text-left">
|
2025-03-19 07:20:22 +00:00
|
|
|
<div className="text-xs text-gray-500">
|
2025-03-19 08:16:44 +00:00
|
|
|
Using tool: {(message as any).function_call.name}...
|
2025-03-19 07:20:22 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-03-19 08:16:44 +00:00
|
|
|
)}
|
2025-03-19 07:20:22 +00:00
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)}
|
2025-03-19 08:16:44 +00:00
|
|
|
{error && (
|
|
|
|
|
<div className="text-red-500 text-sm mt-2">
|
|
|
|
|
Error: {error.message}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-03-19 07:20:22 +00:00
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
</CardContent>
|
2025-03-22 13:15:51 +00:00
|
|
|
<CardFooter className="p-2">
|
|
|
|
|
<ChatInput
|
|
|
|
|
input={input}
|
2025-03-22 13:26:14 +00:00
|
|
|
status={status}
|
2025-03-22 13:15:51 +00:00
|
|
|
onSubmit={onFormSubmit}
|
|
|
|
|
onChange={handleInputChange}
|
2025-03-22 14:28:55 +00:00
|
|
|
setMessages={setMessages}
|
2025-03-22 13:15:51 +00:00
|
|
|
/>
|
2025-03-19 07:20:22 +00:00
|
|
|
</CardFooter>
|
|
|
|
|
</Card>
|
|
|
|
|
)
|
|
|
|
|
}
|