diff --git a/package.json b/package.json index 59b8e18f..cfea9908 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.0.0", "cmdk": "^0.2.0", - "drizzle-orm": "0.26.5", + "drizzle-orm": "0.30.9", "drizzle-zod": "0.4.2", "encoding": "^0.1.13", "eslint": "8.37.0", diff --git a/src/app/api/chats/[chatid]/route.tsx b/src/app/api/chats/[chatid]/route.tsx index f05f96c7..4057b9e1 100644 --- a/src/app/api/chats/[chatid]/route.tsx +++ b/src/app/api/chats/[chatid]/route.tsx @@ -18,6 +18,6 @@ export async function GET( const msg = fetchedChat[0].messages; const chatlog = JSON.parse(msg as string) as ChatLog; - console.log("fetchedChat route", chatlog); + // console.log("fetchedChat route", chatlog); return NextResponse.json({ chats: chatlog ? chatlog : [] }); } diff --git a/src/app/api/patentsearch/route.ts b/src/app/api/patentsearch/route.ts index d0ea241e..390b1c95 100644 --- a/src/app/api/patentsearch/route.ts +++ b/src/app/api/patentsearch/route.ts @@ -27,7 +27,6 @@ export async function POST(request: Request) { console.log("chatId", chatId); console.log("lastMessageIndex", lastMessageIndex); - // console.log("messages", msgs) const query = msgs.map((msg) => msg.content).join(" "); console.log("query", query); @@ -56,8 +55,6 @@ export async function POST(request: Request) { chatlog.log.length, ); - console.log("prevChatData", nextChatData); - const res = await axios.get(`${baseUrl}/search/102`, { params: { q: query, @@ -71,6 +68,9 @@ export async function POST(request: Request) { console.log("data", data); + if (data?.length === 0) { + return NextResponse.json({ data: null }, { status: 404 }); + } const patentMessage = { role: "assistant", subRole: "patent-search", @@ -90,7 +90,5 @@ export async function POST(request: Request) { .where(eq(chats.id, Number(chatId))) .run(); - // console.log(data) - return NextResponse.json({ data: updatedChatLog }); } diff --git a/src/components/chat.tsx b/src/components/chat.tsx index d6781465..a940d291 100644 --- a/src/components/chat.tsx +++ b/src/components/chat.tsx @@ -3,7 +3,6 @@ import { useState, useEffect, useCallback, useRef } from "react"; import { AIType, ChatType } from "@/lib/types"; import InputBar from "@/components/inputBar"; import { Message, useChat } from "ai/react"; -import Startnewchatbutton from "@/components/startnewchatbutton"; import ChatMessageCombinator from "@/components/chatmessagecombinator"; import { useMutation, useQuery } from "@tanstack/react-query"; import axios from "axios"; @@ -41,7 +40,6 @@ export default function Chat(props: ChatProps) { onClickOpenChatSheet, } = useImageState(); const [choosenAI, setChoosenAI] = useState("universal"); - const [isChatCompleted, setIsChatCompleted] = useState(false); const [calculatedMessages, setCalculatedMessages] = useState([]); // const { presenceData, updateStatus } = usePresence(`channel_${props.chatId}`); const [dropZoneActive, setDropzoneActive] = useState(false); @@ -143,8 +141,7 @@ export default function Chat(props: ChatProps) { chattype: props.type, }, onError: (error) => { - console.log("got the error", error); - setIsChatCompleted(true); + console.log("got the error from api", error); }, onResponse(response) { console.log("got the response", response); @@ -154,7 +151,6 @@ export default function Chat(props: ChatProps) { }, sendExtraMessageFields: true, }); - console.log("messages", messages); useEffect(() => { let mainArray: Message[][] = []; @@ -270,8 +266,6 @@ export default function Chat(props: ChatProps) { setMessages={setMessages} chatTitle={props.chatTitle} imageUrl={props.imageUrl} - isChatCompleted={isChatCompleted} - setIsChatCompleted={setIsChatCompleted} append={append} isLoading={isLoading} shouldShowConfidentialToggler={userIds.includes(props.uid)} @@ -304,15 +298,6 @@ export default function Chat(props: ChatProps) { ) : null} - - {isChatCompleted && ( -
- -
- )} diff --git a/src/components/chatcard.tsx b/src/components/chatcard.tsx index 3448c3c9..2a43369a 100644 --- a/src/components/chatcard.tsx +++ b/src/components/chatcard.tsx @@ -44,7 +44,6 @@ const Chatcard = ({ chat, uid, org_id, org_slug, priority, type }: Props) => { const chatlog = JSON.parse(chat.messages as string) as ChatLog; console.log("chatlog", chatlog.log); const msgs = chatlog.log as ChatEntry[]; - console.log("messages", msgs); const chats = msgs.slice(0, 2); const res = await fetch(`/api/generateTitle/${chat.id}/${org_id}`, { method: "POST", diff --git a/src/components/chatmessagecombinator.tsx b/src/components/chatmessagecombinator.tsx index a89f8af5..d160c7d2 100644 --- a/src/components/chatmessagecombinator.tsx +++ b/src/components/chatmessagecombinator.tsx @@ -1,8 +1,7 @@ -import React, { SetStateAction, useEffect, useRef } from "react"; +import React, { useEffect, useRef } from "react"; import Image from "next/image"; import { ContextWrapper } from "@/components/contextwrapper"; import { ChatRequestOptions, CreateMessage, Message } from "ai"; -import { CHAT_COMPLETION_CONTENT } from "@/lib/types"; import ChatMessage from "@/components/chatmessage"; import { cn } from "@/lib/utils"; import { Button } from "@/components/button"; @@ -24,8 +23,6 @@ type Props = { chatTitle: string; imageUrl: string; setMessages: (messages: Message[]) => void; - isChatCompleted: boolean; - setIsChatCompleted: React.Dispatch>; append: ( message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions | undefined, @@ -42,8 +39,6 @@ const ChatMessageCombinator = ({ calculatedMessages, messages, name, - isChatCompleted, - setIsChatCompleted, append, setMessages, chatId, @@ -65,28 +60,33 @@ const ChatMessageCombinator = ({ const queryClient = useQueryClient(); const sheetContentRef = useRef(null); // Specify the type as HTMLDivElement - const mutation = useMutation( - async (data: { id: string; msgs: Message[]; lastMessageIndex: number }) => { + const mutation = useMutation< + Message[] | null, + unknown, + { msgs: Message[]; id: string; lastMessageIndex: number } + >({ + mutationFn: async (data) => { const res = await axios.post(`/api/patentsearch`, { msgs: data.msgs, orgId: orgId, chatId: chatId, lastMessageIndex: data.lastMessageIndex, }); - const result = res.data; - console.log("patentresult", result); + console.log("patentresult", res.data); + return res.data; }, - { - onSuccess: () => { - // Invalidate and refetch - queryClient.invalidateQueries(["chats", chatId]); - }, + onSuccess: (data, variables) => { + if (!data) { + console.log("no patients found"); + } + queryClient.invalidateQueries({ queryKey: ["chats", chatId] }); }, - ); + onError: (_error: any) => {}, + }); - const titleSplit = chatTitle.replaceAll('"', "").split(":"); - const chat_title = titleSplit[0]; - const chat_sub_title = titleSplit.length > 1 ? titleSplit[1] : ""; + const titleSplit = chatTitle?.replaceAll('"', "").split(":"); + const chat_title = titleSplit?.[0]; + const chat_sub_title = titleSplit?.length > 1 ? titleSplit?.[1] : ""; const scrollToBottom = () => { if (sheetContentRef.current) { sheetContentRef.current.scrollIntoView({ behavior: "smooth" }); @@ -201,11 +201,6 @@ const ChatMessageCombinator = ({ )} > {msgs.map((msg, idx) => { - if (index === messages.length - 1 && !isChatCompleted) { - if (messages[index].content === CHAT_COMPLETION_CONTENT) { - setIsChatCompleted(true); - } - } messageIndex++; const msgIdx = messageIndex; if (msg.subRole === "patent-search") return null; @@ -246,6 +241,14 @@ const ChatMessageCombinator = ({ preferences.showSubRoll ? ( patentMessage ? ( + ) : mutation.isLoading && + mutation.variables?.id === msg.id ? ( +
+
+ +
+ +
) : ( ) ) : null diff --git a/src/components/inputBar.tsx b/src/components/inputBar.tsx index b90fe2e5..4fb293bf 100644 --- a/src/components/inputBar.tsx +++ b/src/components/inputBar.tsx @@ -75,7 +75,6 @@ interface InputBarProps { chatRequestOptions?: ChatRequestOptions | undefined, ) => Promise; setInput: Dispatch>; - isChatCompleted: boolean; chatId: string; messages: Message[]; orgId: string; @@ -498,7 +497,6 @@ const InputBar = (props: InputBarProps) => { > {/* { )} {