2025-03-23 12:48:31 +00:00
|
|
|
"use client";
|
2025-03-19 07:20:22 +00:00
|
|
|
|
2025-03-23 12:48:31 +00:00
|
|
|
import type React from "react";
|
|
|
|
|
import { useRef, useEffect, useState } from "react";
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-23 12:48:31 +00:00
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardFooter,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "@/components/ui/card";
|
|
|
|
|
import { useChat } from "@ai-sdk/react";
|
|
|
|
|
import { ChatInput } from "@/components/chat-input";
|
2025-03-25 02:58:11 +00:00
|
|
|
import { ChatMessageDisplay } from "./chat-message-display";
|
2025-03-26 00:30:00 +00:00
|
|
|
import { useDiagram } from "@/contexts/diagram-context";
|
2025-04-03 15:10:53 +00:00
|
|
|
import { replaceNodes } from "@/lib/utils";
|
2025-03-26 00:30:00 +00:00
|
|
|
|
|
|
|
|
export default function ChatPanel() {
|
|
|
|
|
const {
|
|
|
|
|
loadDiagram: onDisplayChart,
|
|
|
|
|
handleExport: onExport,
|
|
|
|
|
resolverRef,
|
2025-04-03 15:10:53 +00:00
|
|
|
chartXML,
|
2025-03-27 08:09:22 +00:00
|
|
|
clearDiagram,
|
2025-03-26 00:30:00 +00:00
|
|
|
} = useDiagram();
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-26 00:30:00 +00:00
|
|
|
const onFetchChart = () => {
|
|
|
|
|
return new Promise<string>((resolve) => {
|
2025-03-26 08:51:21 +00:00
|
|
|
if (resolverRef && "current" in resolverRef) {
|
|
|
|
|
resolverRef.current = resolve; // Store the resolver
|
|
|
|
|
}
|
2025-03-26 00:30:00 +00:00
|
|
|
onExport(); // Trigger the export
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-03-22 16:03:03 +00:00
|
|
|
// Add a step counter to track updates
|
2025-03-25 04:23:38 +00:00
|
|
|
|
2025-03-23 11:03:25 +00:00
|
|
|
// Add state for file attachments
|
2025-03-27 08:02:03 +00:00
|
|
|
const [files, setFiles] = useState<File[]>([]);
|
2025-03-23 13:54:21 +00:00
|
|
|
// Add state for showing the history dialog
|
|
|
|
|
const [showHistory, setShowHistory] = useState(false);
|
2025-03-23 11:03:25 +00:00
|
|
|
|
2025-03-27 08:02:03 +00:00
|
|
|
// Convert File[] to FileList for experimental_attachments
|
|
|
|
|
const createFileList = (files: File[]): FileList => {
|
|
|
|
|
const dt = new DataTransfer();
|
|
|
|
|
files.forEach((file) => dt.items.add(file));
|
|
|
|
|
return dt.files;
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-22 16:03:03 +00:00
|
|
|
// Remove the currentXmlRef and related useEffect
|
2025-03-23 12:48:31 +00:00
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
input,
|
|
|
|
|
handleInputChange,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
status,
|
|
|
|
|
error,
|
|
|
|
|
setInput,
|
|
|
|
|
setMessages,
|
|
|
|
|
} = useChat({
|
2025-03-22 14:28:55 +00:00
|
|
|
maxSteps: 5,
|
2025-03-19 08:16:44 +00:00
|
|
|
async onToolCall({ toolCall }) {
|
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 };
|
2025-03-25 08:56:24 +00:00
|
|
|
// do nothing because we will handle this streamingly in the ChatMessageDisplay component
|
2025-04-03 15:10:53 +00:00
|
|
|
onDisplayChart(replaceNodes(chartXML, 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-23 12:48:31 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
2025-03-19 07:20:22 +00:00
|
|
|
// Scroll to bottom when messages change
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (messagesEndRef.current) {
|
2025-03-23 12:48:31 +00:00
|
|
|
messagesEndRef.current.scrollIntoView({ behavior: "smooth" });
|
2025-03-19 07:20:22 +00:00
|
|
|
}
|
2025-03-23 12:48:31 +00:00
|
|
|
}, [messages]);
|
2025-03-19 07:20:22 +00:00
|
|
|
|
2025-03-22 16:03:03 +00:00
|
|
|
const onFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
2025-03-23 12:48:31 +00:00
|
|
|
e.preventDefault();
|
2025-03-22 13:26:14 +00:00
|
|
|
if (input.trim() && status !== "streaming") {
|
2025-03-22 16:03:03 +00:00
|
|
|
try {
|
|
|
|
|
// Fetch chart data before setting input
|
|
|
|
|
const chartXml = await onFetchChart();
|
2025-03-23 11:03:25 +00:00
|
|
|
handleSubmit(e, {
|
2025-03-24 02:38:27 +00:00
|
|
|
data: {
|
|
|
|
|
xml: chartXml,
|
|
|
|
|
},
|
2025-03-27 08:02:03 +00:00
|
|
|
experimental_attachments:
|
|
|
|
|
files.length > 0 ? createFileList(files) : undefined,
|
2025-03-23 12:48:31 +00:00
|
|
|
});
|
2025-03-23 11:03:25 +00:00
|
|
|
|
|
|
|
|
// Clear files after submission
|
2025-03-27 08:02:03 +00:00
|
|
|
setFiles([]);
|
2025-03-22 16:03:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching chart data:", error);
|
|
|
|
|
}
|
2025-03-19 07:20:22 +00:00
|
|
|
}
|
2025-03-23 12:48:31 +00:00
|
|
|
};
|
2025-03-19 07:20:22 +00:00
|
|
|
|
2025-03-23 11:03:25 +00:00
|
|
|
// Helper function to handle file changes
|
2025-03-27 08:02:03 +00:00
|
|
|
const handleFileChange = (newFiles: File[]) => {
|
2025-03-23 11:03:25 +00:00
|
|
|
setFiles(newFiles);
|
2025-03-23 12:48:31 +00:00
|
|
|
};
|
2025-03-23 13:54:21 +00:00
|
|
|
|
2025-03-19 07:20:22 +00:00
|
|
|
return (
|
2025-03-23 14:36:21 +00:00
|
|
|
<Card className="h-full flex flex-col rounded-none py-0 gap-0">
|
2025-03-23 12:48:31 +00:00
|
|
|
<CardHeader className="p-4 text-center">
|
|
|
|
|
<CardTitle>Next-AI-Drawio</CardTitle>
|
2025-03-19 07:20:22 +00:00
|
|
|
</CardHeader>
|
2025-03-23 14:36:21 +00:00
|
|
|
<CardContent className="flex-grow overflow-hidden px-2">
|
2025-03-25 02:58:11 +00:00
|
|
|
<ChatMessageDisplay
|
|
|
|
|
messages={messages}
|
|
|
|
|
error={error}
|
|
|
|
|
setInput={setInput}
|
|
|
|
|
setFiles={handleFileChange}
|
|
|
|
|
/>
|
2025-03-19 07:20:22 +00:00
|
|
|
</CardContent>
|
2025-03-23 13:15:28 +00:00
|
|
|
|
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-27 08:09:22 +00:00
|
|
|
onClearChat={() => {
|
|
|
|
|
setMessages([]);
|
|
|
|
|
clearDiagram();
|
|
|
|
|
}}
|
2025-03-23 11:03:25 +00:00
|
|
|
files={files}
|
|
|
|
|
onFileChange={handleFileChange}
|
2025-03-23 13:54:21 +00:00
|
|
|
showHistory={showHistory}
|
2025-03-27 07:48:19 +00:00
|
|
|
onToggleHistory={setShowHistory}
|
2025-03-22 13:15:51 +00:00
|
|
|
/>
|
2025-03-19 07:20:22 +00:00
|
|
|
</CardFooter>
|
|
|
|
|
</Card>
|
2025-03-23 12:48:31 +00:00
|
|
|
);
|
2025-03-19 07:20:22 +00:00
|
|
|
}
|