aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-06-23 17:54:05 -0500
committerDhravya <[email protected]>2024-06-23 17:54:05 -0500
commit9f751ba89cc37516daa261bbae8723d5566a6602 (patch)
tree3e7533aefbf7931c966c930349263ae248cc1135 /apps/web/app
parentadded indexes and stuff (diff)
downloadsupermemory-9f751ba89cc37516daa261bbae8723d5566a6602.tar.xz
supermemory-9f751ba89cc37516daa261bbae8723d5566a6602.zip
feat: vector lookup and chat is twice as fast now
Diffstat (limited to 'apps/web/app')
-rw-r--r--apps/web/app/(dash)/chat/chatWindow.tsx17
-rw-r--r--apps/web/app/api/chat/route.ts24
2 files changed, 18 insertions, 23 deletions
diff --git a/apps/web/app/(dash)/chat/chatWindow.tsx b/apps/web/app/(dash)/chat/chatWindow.tsx
index 8485d0b2..9a18cfe7 100644
--- a/apps/web/app/(dash)/chat/chatWindow.tsx
+++ b/apps/web/app/(dash)/chat/chatWindow.tsx
@@ -6,7 +6,7 @@ import QueryInput from "../home/queryinput";
import { cn } from "@repo/ui/lib/utils";
import { motion } from "framer-motion";
import { useRouter } from "next/navigation";
-import { ChatHistory } from "@repo/shared-types";
+import { ChatHistory, sourcesZod } from "@repo/shared-types";
import {
Accordion,
AccordionContent,
@@ -20,15 +20,10 @@ import rehypeKatex from "rehype-katex";
import rehypeHighlight from "rehype-highlight";
import { code, p } from "./markdownRenderHelpers";
import { codeLanguageSubset } from "@/lib/constants";
-import { z } from "zod";
import { toast } from "sonner";
import Link from "next/link";
import { createChatObject } from "@/app/actions/doers";
-import {
- ClipboardIcon,
- ShareIcon,
- SpeakerWaveIcon,
-} from "@heroicons/react/24/outline";
+import { ClipboardIcon } from "@heroicons/react/24/outline";
import { SendIcon } from "lucide-react";
function ChatWindow({
@@ -83,11 +78,6 @@ function ChatWindow({
// TODO: handle this properly
const sources = await sourcesFetch.json();
- const sourcesZod = z.object({
- ids: z.array(z.string()),
- metadata: z.array(z.any()),
- });
-
const sourcesParsed = sourcesZod.safeParse(sources);
if (!sourcesParsed.success) {
@@ -100,7 +90,6 @@ function ChatWindow({
behavior: "smooth",
});
- // Assuming this is part of a larger function within a React component
const updateChatHistoryAndFetch = async () => {
// Step 1: Update chat history with the assistant's response
await new Promise((resolve) => {
@@ -143,7 +132,7 @@ function ChatWindow({
`/api/chat?q=${query}&spaces=${spaces}&threadId=${threadId}`,
{
method: "POST",
- body: JSON.stringify({ chatHistory }),
+ body: JSON.stringify({ chatHistory, sources: sourcesParsed.data }),
},
);
diff --git a/apps/web/app/api/chat/route.ts b/apps/web/app/api/chat/route.ts
index d0e53066..d1730baa 100644
--- a/apps/web/app/api/chat/route.ts
+++ b/apps/web/app/api/chat/route.ts
@@ -1,5 +1,10 @@
import { type NextRequest } from "next/server";
-import { ChatHistoryZod, convertChatHistoryList } from "@repo/shared-types";
+import {
+ ChatHistory,
+ ChatHistoryZod,
+ convertChatHistoryList,
+ SourcesFromApi,
+} from "@repo/shared-types";
import { ensureAuth } from "../ensureAuth";
import { z } from "zod";
@@ -23,7 +28,11 @@ export async function POST(req: NextRequest) {
const sourcesOnly = url.searchParams.get("sourcesOnly") ?? "false";
- const chatHistory = await req.json();
+ const jsonRequest = (await req.json()) as {
+ chatHistory: ChatHistory[];
+ sources: SourcesFromApi[] | undefined;
+ };
+ const { chatHistory, sources } = jsonRequest;
if (!query || query.trim.length < 0) {
return new Response(JSON.stringify({ message: "Invalid query" }), {
@@ -31,9 +40,7 @@ export async function POST(req: NextRequest) {
});
}
- const validated = z
- .object({ chatHistory: z.array(ChatHistoryZod) })
- .safeParse(chatHistory ?? []);
+ const validated = z.array(ChatHistoryZod).safeParse(chatHistory ?? []);
if (!validated.success) {
return new Response(
@@ -45,9 +52,7 @@ export async function POST(req: NextRequest) {
);
}
- const modelCompatible = await convertChatHistoryList(
- validated.data.chatHistory,
- );
+ const modelCompatible = await convertChatHistoryList(validated.data);
const resp = await fetch(
`${process.env.BACKEND_BASE_URL}/api/chat?query=${query}&user=${session.user.id}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
@@ -59,12 +64,13 @@ export async function POST(req: NextRequest) {
method: "POST",
body: JSON.stringify({
chatHistory: modelCompatible,
+ sources,
}),
},
);
if (sourcesOnly == "true") {
- const data = await resp.json();
+ const data = (await resp.json()) as SourcesFromApi;
return new Response(JSON.stringify(data), { status: 200 });
}