diff --git a/apps/web/app/(dash)/chat/chatWindow.tsx b/apps/web/app/(dash)/chat/chatWindow.tsx index 77c1f32b..17c415e9 100644 --- a/apps/web/app/(dash)/chat/chatWindow.tsx +++ b/apps/web/app/(dash)/chat/chatWindow.tsx @@ -23,7 +23,6 @@ import { codeLanguageSubset } from "@/lib/constants"; import { z } from "zod"; import { toast } from "sonner"; import Link from "next/link"; -import { sources } from "next/dist/compiled/webpack/webpack"; function ChatWindow({ q, @@ -47,6 +46,20 @@ function ChatWindow({ }, ]); + const removeJustificationFromText = (text: string) => { + // remove everything after the first "" word + const justificationLine = text.indexOf(""); + if (justificationLine !== -1) { + // Add that justification to the last chat message + const lastChatMessage = chatHistory[chatHistory.length - 1]; + if (lastChatMessage) { + lastChatMessage.answer.justification = text.slice(justificationLine); + } + return text.slice(0, justificationLine); + } + return text; + }; + const router = useRouter(); const getAnswer = async (query: string, spaces: string[]) => { @@ -55,7 +68,7 @@ function ChatWindow({ { method: "POST", body: JSON.stringify({ chatHistory }), - }, + } ); // TODO: handle this properly @@ -80,7 +93,7 @@ function ChatWindow({ const lastAnswer = newChatHistory[newChatHistory.length - 1]; if (!lastAnswer) return prevChatHistory; const filteredSourceUrls = new Set( - sourcesParsed.data.metadata.map((source) => source.url), + sourcesParsed.data.metadata.map((source) => source.url) ); const uniqueSources = sourcesParsed.data.metadata.filter((source) => { if (filteredSourceUrls.has(source.url)) { @@ -95,7 +108,7 @@ function ChatWindow({ source: source.url ?? "https://supermemory.ai", content: source.content ?? "No content available", numChunks: sourcesParsed.data.metadata.filter( - (f) => f.url === source.url, + (f) => f.url === source.url ).length, })); return newChatHistory; @@ -129,7 +142,7 @@ function ChatWindow({ if (q.trim().length > 0) { getAnswer( q, - spaces.map((s) => s.id), + spaces.map((s) => s.id) ); setTimeout(() => { setLayout("chat"); @@ -164,7 +177,7 @@ function ChatWindow({ >

{chat.question} @@ -262,10 +275,38 @@ function ChatWindow({ }} className="flex flex-col gap-2" > - {chat.answer.parts.map((part) => part.text).join("")} + {removeJustificationFromText( + chat.answer.parts.map((part) => part.text).join("") + )} + + {/* Justification */} + {chat.answer.justification && + chat.answer.justification.length && ( +
0 ? "flex" : "hidden"}`} + > + + + + Justification + + + {chat.answer.justification.length > 0 + ? chat.answer.justification + .replaceAll("", "") + .replaceAll("", "") + : "No justification provided."} + + + +
+ )} ))} diff --git a/packages/shared-types/index.ts b/packages/shared-types/index.ts index b8792369..d3f466e1 100644 --- a/packages/shared-types/index.ts +++ b/packages/shared-types/index.ts @@ -13,6 +13,7 @@ export const ChatHistoryZod = z.object({ numChunks: z.number().optional().default(1), }), ), + justification: z.string().optional(), }), });