From c1efb2d5cb9cd5665fe49a9439c908bc086401d1 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Wed, 7 Feb 2024 00:48:59 +0530 Subject: [PATCH] Update PlaygroundForm and Sidebar components --- .../Option/Playground/PlaygroundForm.tsx | 19 ++++++++--- src/components/Option/Sidebar.tsx | 24 ++++++++++--- src/hooks/useMessageOption.tsx | 2 +- src/libs/db.ts | 34 +++++++++++++++++-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/components/Option/Playground/PlaygroundForm.tsx b/src/components/Option/Playground/PlaygroundForm.tsx index faff95f..dd6b96c 100644 --- a/src/components/Option/Playground/PlaygroundForm.tsx +++ b/src/components/Option/Playground/PlaygroundForm.tsx @@ -1,5 +1,5 @@ import { useForm } from "@mantine/form" -import { useMutation } from "@tanstack/react-query" +import { useMutation, useQueryClient } from "@tanstack/react-query" import React from "react" import useDynamicTextareaSize from "~hooks/useDynamicTextareaSize" import PhotoIcon from "@heroicons/react/24/outline/PhotoIcon" @@ -50,12 +50,19 @@ export const PlaygroundForm = ({ dropedFile }: Props) => { } }, [dropedFile]) - useDynamicTextareaSize(textareaRef, form.values.message, 120) + useDynamicTextareaSize(textareaRef, form.values.message, 300) - const { onSubmit, selectedModel, chatMode } = useMessageOption() + const { onSubmit, selectedModel, chatMode, clearChat } = useMessageOption() + + const queryClient = useQueryClient() const { mutateAsync: sendMessage, isPending: isSending } = useMutation({ - mutationFn: onSubmit + mutationFn: onSubmit, + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["fetchChatHistory"] + }) + } }) return ( @@ -82,7 +89,9 @@ export const PlaygroundForm = ({ dropedFile }: Props) => {
- diff --git a/src/components/Option/Sidebar.tsx b/src/components/Option/Sidebar.tsx index 9d74808..650d8f2 100644 --- a/src/components/Option/Sidebar.tsx +++ b/src/components/Option/Sidebar.tsx @@ -1,10 +1,17 @@ import { useQuery } from "@tanstack/react-query" -import { PageAssitDatabase } from "~libs/db" +import { + PageAssitDatabase, + formatToChatHistory, + formatToMessage +} from "~libs/db" import { Empty, Skeleton } from "antd" +import { useMessageOption } from "~hooks/useMessageOption" type Props = {} export const Sidebar = ({}: Props) => { + const { setMessages, setHistory, setHistoryId } = useMessageOption() + const { data: chatHistories, status } = useQuery({ queryKey: ["fetchChatHistory"], queryFn: async () => { @@ -15,7 +22,7 @@ export const Sidebar = ({}: Props) => { }) return ( -
+
{status === "success" && chatHistories.length === 0 && (
@@ -34,11 +41,18 @@ export const Sidebar = ({}: Props) => { {status === "success" && chatHistories.length > 0 && (
{chatHistories.map((chat, index) => ( -
{ + const db = new PageAssitDatabase() + const history = await db.getChatHistory(chat.id) + setHistoryId(chat.id) + setHistory(formatToChatHistory(history)) + setMessages(formatToMessage(history)) + }} key={index} - className="flex py-2 px-2 cursor-pointer items-center gap-3 relative rounded-md truncate hover:pr-4 group transition-opacity duration-300 ease-in-out bg-gray-100 dark:bg-[#232222] dark:text-gray-100 text-gray-800 border hover:bg-gray-200 dark:hover:bg-[#2d2d2d] dark:border-gray-800"> + className="flex text-start py-2 px-2 cursor-pointer items-start gap-3 relative rounded-md truncate hover:pr-4 group transition-opacity duration-300 ease-in-out bg-gray-100 dark:bg-[#232222] dark:text-gray-100 text-gray-800 border hover:bg-gray-200 dark:hover:bg-[#2d2d2d] dark:border-gray-800"> {chat.title} -
+ ))}
)} diff --git a/src/hooks/useMessageOption.tsx b/src/hooks/useMessageOption.tsx index 30cd4bd..1dcac17 100644 --- a/src/hooks/useMessageOption.tsx +++ b/src/hooks/useMessageOption.tsx @@ -95,7 +95,7 @@ export const useMessageOption = () => { const abortControllerRef = React.useRef(null) const clearChat = () => { - stopStreamingRequest() + // stopStreamingRequest() setMessages([]) setHistory([]) setHistoryId(null) diff --git a/src/libs/db.ts b/src/libs/db.ts index d51829a..51a207d 100644 --- a/src/libs/db.ts +++ b/src/libs/db.ts @@ -1,4 +1,7 @@ -im +import { + type ChatHistory as ChatHistoryType, + type Message as MessageType +} from "~store/option" type HistoryInfo = { id: string @@ -13,6 +16,7 @@ type Message = { role: string content: string images?: string[] + sources?: string[] createdAt: number } @@ -84,8 +88,6 @@ const generateID = () => { }) } - - export const saveHistory = async (title: string) => { const id = generateID() const createdAt = Date.now() @@ -109,3 +111,29 @@ export const saveMessage = async ( await db.addMessage(message) return message } + +export const formatToChatHistory = ( + messages: MessageHistory +): ChatHistoryType => { + messages.sort((a, b) => a.createdAt - b.createdAt) + return messages.map((message) => { + return { + content: message.content, + role: message.role as "user" | "assistant" | "system", + images: message.images + } + }) +} + +export const formatToMessage = (messages: MessageHistory): MessageType[] => { + messages.sort((a, b) => a.createdAt - b.createdAt) + return messages.map((message) => { + return { + isBot: message.role === "assistant", + message: message.content, + name: message.name, + sources: message?.sources || [], + images: message.images || [] + } + }) +}