2024-11-03 17:02:13 +08:00
|
|
|
import { useState, useRef, useEffect } from "react";
|
2024-11-20 10:08:08 +08:00
|
|
|
import { PanelRightClose, PanelRightOpen, X } from "lucide-react";
|
2024-11-03 17:02:13 +08:00
|
|
|
|
|
|
|
|
import { ChatMessage } from "./ChatMessage";
|
|
|
|
|
import { ChatInput } from "./ChatInput";
|
|
|
|
|
import { Sidebar } from "./Sidebar";
|
2024-11-09 11:30:36 +08:00
|
|
|
import type { Chat, Message } from "./types";
|
|
|
|
|
import { tauriFetch } from "../../api/tauriFetchClient";
|
|
|
|
|
import { useWebSocket } from "../../hooks/useWebSocket";
|
2024-11-20 10:08:08 +08:00
|
|
|
import useWindows from "../../hooks/useWindows";
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-20 10:08:08 +08:00
|
|
|
interface ChatAIProps {}
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-20 10:08:08 +08:00
|
|
|
export default function ChatAI({}: ChatAIProps) {
|
2024-11-09 11:30:36 +08:00
|
|
|
const [chats, setChats] = useState<Chat[]>([]);
|
|
|
|
|
const [activeChat, setActiveChat] = useState<Chat>();
|
2024-11-20 10:08:08 +08:00
|
|
|
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
|
2024-11-03 17:02:13 +08:00
|
|
|
const [isTyping, setIsTyping] = useState(false);
|
|
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
2024-11-20 10:08:08 +08:00
|
|
|
const { closeWin } = useWindows();
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
const [websocketId, setWebsocketId] = useState("");
|
|
|
|
|
const [curMessage, setCurMessage] = useState("");
|
|
|
|
|
const [curChatEnd, setCurChatEnd] = useState(true);
|
2024-11-27 19:41:54 +08:00
|
|
|
|
|
|
|
|
const [curId, setCurId] = useState("");
|
|
|
|
|
|
|
|
|
|
const curChatEndRef = useRef(curChatEnd);
|
|
|
|
|
curChatEndRef.current = curChatEnd;
|
|
|
|
|
|
|
|
|
|
const curIdRef = useRef(curId);
|
|
|
|
|
curIdRef.current = curId;
|
2024-11-09 11:30:36 +08:00
|
|
|
const { messages, setMessages } = useWebSocket(
|
|
|
|
|
"ws://localhost:2900/ws",
|
|
|
|
|
(msg) => {
|
|
|
|
|
if (msg.includes("websocket_session_id")) {
|
|
|
|
|
const array = msg.split(" ");
|
|
|
|
|
setWebsocketId(array[2]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.includes("PRIVATE")) {
|
2024-11-27 19:41:54 +08:00
|
|
|
if (
|
|
|
|
|
msg.includes("assistant finished output") ||
|
|
|
|
|
curChatEndRef.current
|
|
|
|
|
) {
|
2024-11-09 11:30:36 +08:00
|
|
|
setCurChatEnd(true);
|
|
|
|
|
} else {
|
|
|
|
|
const cleanedData = msg.replace(/^PRIVATE /, "");
|
|
|
|
|
try {
|
|
|
|
|
const chunkData = JSON.parse(cleanedData);
|
2024-11-27 19:41:54 +08:00
|
|
|
if (chunkData.reply_to_message === curIdRef.current) {
|
|
|
|
|
setCurMessage((prev) => prev + chunkData.message_chunk);
|
|
|
|
|
return chunkData.message_chunk;
|
|
|
|
|
}
|
2024-11-09 11:30:36 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("JSON Parse error:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// websocket
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (messages.length === 0 || !activeChat?._id) return;
|
|
|
|
|
|
|
|
|
|
const simulateAssistantResponse = () => {
|
|
|
|
|
console.log("messages", messages);
|
|
|
|
|
|
|
|
|
|
const assistantMessage: Message = {
|
|
|
|
|
_id: activeChat._id,
|
|
|
|
|
_source: {
|
|
|
|
|
type: "assistant",
|
|
|
|
|
message: messages,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updatedChat = {
|
|
|
|
|
...activeChat,
|
|
|
|
|
messages: [...(activeChat.messages || []), assistantMessage],
|
|
|
|
|
};
|
|
|
|
|
setMessages("");
|
|
|
|
|
setCurMessage("");
|
|
|
|
|
setActiveChat(updatedChat);
|
|
|
|
|
setTimeout(() => setIsTyping(false), 1000);
|
|
|
|
|
};
|
|
|
|
|
if (curChatEnd) {
|
|
|
|
|
simulateAssistantResponse();
|
|
|
|
|
}
|
|
|
|
|
}, [messages, isTyping, curChatEnd]);
|
|
|
|
|
|
|
|
|
|
// getChatHistory
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getChatHistory();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const getChatHistory = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: "/chat/_history",
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
console.log("_history", response);
|
|
|
|
|
const hits = response.data?.hits?.hits || [];
|
|
|
|
|
setChats(hits);
|
|
|
|
|
if (hits[0]) {
|
|
|
|
|
onSelectChat(hits[0]);
|
|
|
|
|
} else {
|
|
|
|
|
createNewChat();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 17:02:13 +08:00
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({
|
|
|
|
|
behavior: "smooth",
|
|
|
|
|
block: "end",
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
scrollToBottom();
|
2024-11-09 11:30:36 +08:00
|
|
|
}, [activeChat?.messages, isTyping, curMessage]);
|
|
|
|
|
|
|
|
|
|
const createNewChat = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: "/chat/_new",
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
console.log("_new", response);
|
|
|
|
|
const newChat: Chat = response.data;
|
|
|
|
|
setChats((prev) => [newChat, ...prev]);
|
|
|
|
|
setActiveChat(newChat);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
2024-11-03 17:02:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deleteChat = (chatId: string) => {
|
2024-11-09 11:30:36 +08:00
|
|
|
setChats((prev) => prev.filter((chat) => chat._id !== chatId));
|
|
|
|
|
if (activeChat?._id === chatId) {
|
|
|
|
|
const remainingChats = chats.filter((chat) => chat._id !== chatId);
|
2024-11-03 17:02:13 +08:00
|
|
|
if (remainingChats.length > 0) {
|
|
|
|
|
setActiveChat(remainingChats[0]);
|
|
|
|
|
} else {
|
|
|
|
|
createNewChat();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
const handleSendMessage = async (content: string) => {
|
|
|
|
|
if (!activeChat?._id) return;
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: `/chat/${activeChat?._id}/_send`,
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
WEBSOCKET_SESSION_ID: websocketId,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ message: content }),
|
|
|
|
|
});
|
|
|
|
|
console.log("_send", response, websocketId);
|
2024-11-27 19:41:54 +08:00
|
|
|
setCurId(response.data[0]?._id);
|
2024-11-09 11:30:36 +08:00
|
|
|
const updatedChat: Chat = {
|
|
|
|
|
...activeChat,
|
|
|
|
|
messages: [...(activeChat?.messages || []), ...(response.data || [])],
|
|
|
|
|
};
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
setActiveChat(updatedChat);
|
|
|
|
|
setIsTyping(true);
|
|
|
|
|
setCurChatEnd(false);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
const chatHistory = async (chat: Chat) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: `/chat/${chat._id}/_history`,
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
console.log("id_history", response);
|
|
|
|
|
const hits = response.data?.hits?.hits || [];
|
|
|
|
|
const updatedChat: Chat = {
|
|
|
|
|
...chat,
|
|
|
|
|
messages: hits,
|
2024-11-03 17:02:13 +08:00
|
|
|
};
|
2024-11-09 11:30:36 +08:00
|
|
|
setActiveChat(updatedChat);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
const chatClose = async () => {
|
|
|
|
|
if (!activeChat?._id) return;
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: `/chat/${activeChat._id}/_close`,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
console.log("_close", response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-03 17:02:13 +08:00
|
|
|
|
2024-11-09 11:30:36 +08:00
|
|
|
const onSelectChat = async (chat: any) => {
|
|
|
|
|
chatClose();
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: `/chat/${chat._id}/_open`,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
console.log("_open", response);
|
|
|
|
|
chatHistory(response.data);
|
2024-11-20 10:08:08 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-24 19:25:47 +08:00
|
|
|
|
2024-11-20 10:08:08 +08:00
|
|
|
const cancelChat = async () => {
|
|
|
|
|
if (!activeChat?._id) return;
|
|
|
|
|
try {
|
|
|
|
|
const response = await tauriFetch({
|
|
|
|
|
url: `/chat/${activeChat._id}/_cancel`,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
console.log("_cancel", response);
|
2024-11-09 11:30:36 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to fetch user data:", error);
|
|
|
|
|
}
|
2024-11-03 17:02:13 +08:00
|
|
|
};
|
|
|
|
|
|
2024-11-20 10:08:08 +08:00
|
|
|
async function closeWindow() {
|
|
|
|
|
await closeWin("chat");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 17:02:13 +08:00
|
|
|
return (
|
2024-11-27 19:41:54 +08:00
|
|
|
<div className="h-screen">
|
2024-11-03 17:02:13 +08:00
|
|
|
<div className="h-[100%] flex">
|
|
|
|
|
{/* Sidebar */}
|
2024-11-12 09:44:49 +08:00
|
|
|
{isSidebarOpen ? (
|
|
|
|
|
<div
|
|
|
|
|
className={`fixed inset-y-0 left-0 z-50 w-64 transform ${
|
|
|
|
|
isSidebarOpen ? "translate-x-0" : "-translate-x-full"
|
2024-11-27 19:41:54 +08:00
|
|
|
} transition-transform duration-300 ease-in-out md:translate-x-0 md:static md:block bg-gray-100 dark:bg-gray-800`}
|
2024-11-12 09:44:49 +08:00
|
|
|
>
|
|
|
|
|
{activeChat ? (
|
|
|
|
|
<Sidebar
|
|
|
|
|
chats={chats}
|
|
|
|
|
activeChat={activeChat}
|
|
|
|
|
onNewChat={createNewChat}
|
|
|
|
|
onSelectChat={onSelectChat}
|
|
|
|
|
onDeleteChat={deleteChat}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2024-11-03 17:02:13 +08:00
|
|
|
|
|
|
|
|
{/* Main content */}
|
2024-11-27 19:41:54 +08:00
|
|
|
<div className={`flex-1 flex flex-col bg-white dark:bg-gray-900`}>
|
2024-11-24 19:25:47 +08:00
|
|
|
<header
|
|
|
|
|
className={`flex items-center justify-between p-2 border-b border-gray-200 dark:border-gray-800`}
|
2024-11-03 17:02:13 +08:00
|
|
|
>
|
2024-11-24 19:25:47 +08:00
|
|
|
<button
|
|
|
|
|
onClick={() => setIsSidebarOpen(!isSidebarOpen)}
|
|
|
|
|
className={`rounded-lg transition-colors hover:bg-gray-100 text-gray-600 dark:hover:bg-gray-800 dark:text-gray-300`}
|
2024-11-03 17:02:13 +08:00
|
|
|
>
|
2024-11-24 19:25:47 +08:00
|
|
|
{isSidebarOpen ? (
|
|
|
|
|
<PanelRightClose className="h-6 w-6" />
|
|
|
|
|
) : (
|
|
|
|
|
<PanelRightOpen className="h-6 w-6" />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
2024-11-14 09:39:22 +08:00
|
|
|
|
2024-11-24 19:25:47 +08:00
|
|
|
<X className="cursor-pointer" onClick={closeWindow} />
|
|
|
|
|
</header>
|
2024-11-03 17:02:13 +08:00
|
|
|
|
|
|
|
|
{/* Chat messages */}
|
2024-11-24 19:25:47 +08:00
|
|
|
<div className="flex-1 overflow-y-auto custom-scrollbar">
|
2024-11-09 11:30:36 +08:00
|
|
|
{activeChat?.messages?.map((message, index) => (
|
2024-11-03 17:02:13 +08:00
|
|
|
<ChatMessage
|
2024-11-09 11:30:36 +08:00
|
|
|
key={message._id + index}
|
2024-11-03 17:02:13 +08:00
|
|
|
message={message}
|
|
|
|
|
isTyping={
|
|
|
|
|
isTyping &&
|
2024-11-09 11:30:36 +08:00
|
|
|
index === (activeChat.messages?.length || 0) - 1 &&
|
|
|
|
|
message._source?.type === "assistant"
|
2024-11-03 17:02:13 +08:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2024-11-09 11:30:36 +08:00
|
|
|
{!curChatEnd && activeChat?._id ? (
|
|
|
|
|
<ChatMessage
|
|
|
|
|
key={"last"}
|
|
|
|
|
message={{
|
|
|
|
|
_id: activeChat?._id,
|
|
|
|
|
_source: {
|
|
|
|
|
type: "assistant",
|
|
|
|
|
message: curMessage,
|
|
|
|
|
},
|
|
|
|
|
}}
|
2024-11-14 09:39:22 +08:00
|
|
|
isTyping={!curChatEnd}
|
2024-11-09 11:30:36 +08:00
|
|
|
/>
|
|
|
|
|
) : null}
|
2024-11-03 17:02:13 +08:00
|
|
|
<div ref={messagesEndRef} />
|
2024-11-24 19:25:47 +08:00
|
|
|
</div>
|
2024-11-03 17:02:13 +08:00
|
|
|
|
|
|
|
|
{/* Input area */}
|
2024-11-24 19:25:47 +08:00
|
|
|
<div className={`border-t p-4 border-gray-200 dark:border-gray-800`}>
|
2024-11-09 11:30:36 +08:00
|
|
|
<ChatInput
|
|
|
|
|
onSend={handleSendMessage}
|
|
|
|
|
disabled={isTyping}
|
2024-11-27 19:41:54 +08:00
|
|
|
curChatEnd={curChatEnd}
|
|
|
|
|
disabledChange={() => {
|
2024-11-24 19:25:47 +08:00
|
|
|
cancelChat();
|
2024-11-27 19:41:54 +08:00
|
|
|
setCurChatEnd(true);
|
|
|
|
|
setIsTyping(false);
|
2024-11-14 09:39:22 +08:00
|
|
|
}}
|
2024-11-09 11:30:36 +08:00
|
|
|
/>
|
2024-11-24 19:25:47 +08:00
|
|
|
</div>
|
2024-11-03 17:02:13 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-11-24 19:25:47 +08:00
|
|
|
</div>
|
2024-11-03 17:02:13 +08:00
|
|
|
);
|
|
|
|
|
}
|