diff options
| author | Dhravya <[email protected]> | 2024-04-08 18:12:16 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-08 18:12:16 -0700 |
| commit | f04fa3faf75c1b2c63f094632c15a528a98932c5 (patch) | |
| tree | f2831ca93153a0b7698cb0517a35c0601f1b5ed3 /apps/web/src/app/api | |
| parent | made it functional (diff) | |
| download | supermemory-f04fa3faf75c1b2c63f094632c15a528a98932c5.tar.xz supermemory-f04fa3faf75c1b2c63f094632c15a528a98932c5.zip | |
setup for multi chat
Diffstat (limited to 'apps/web/src/app/api')
| -rw-r--r-- | apps/web/src/app/api/chat/route.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/apps/web/src/app/api/chat/route.ts b/apps/web/src/app/api/chat/route.ts new file mode 100644 index 00000000..2cb03186 --- /dev/null +++ b/apps/web/src/app/api/chat/route.ts @@ -0,0 +1,62 @@ +import { db } from "@/server/db"; +import { eq } from "drizzle-orm"; +import { sessions, users } from "@/server/db/schema"; +import { type NextRequest, NextResponse } from "next/server"; +import { env } from "@/env"; +import { ChatHistory } from "../../../../types/memory"; + +export const runtime = "edge"; + +export async function POST(req: NextRequest) { + const token = req.cookies.get("next-auth.session-token")?.value ?? req.cookies.get("__Secure-authjs.session-token")?.value ?? req.cookies.get("authjs.session-token")?.value ?? req.headers.get("Authorization")?.replace("Bearer ", ""); + + const sessionData = await db.select().from(sessions).where(eq(sessions.sessionToken, token!)) + + if (!sessionData || sessionData.length === 0) { + return new Response(JSON.stringify({ message: "Invalid Key, session not found." }), { status: 404 }); + } + + const user = await db.select().from(users).where(eq(users.id, sessionData[0].userId)).limit(1) + + if (!user || user.length === 0) { + return NextResponse.json({ message: "Invalid Key, session not found." }, { status: 404 }); + } + + const session = { session: sessionData[0], user: user[0] } + + const query = new URL(req.url).searchParams.get("q"); + const sourcesOnly = new URL(req.url).searchParams.get("sourcesOnly") ?? "false"; + + const chatHistory = await req.json() as { + chatHistory: ChatHistory[] + }; + + + if (!query) { + return new Response(JSON.stringify({ message: "Invalid query" }), { status: 400 }); + } + + const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}`, { + headers: { + "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, + }, + method: "POST", + body: JSON.stringify({ + chatHistory + }) + }) + + console.log(resp.status) + + if (resp.status !== 200 || !resp.ok) { + const errorData = await resp.json(); + console.log(errorData) + return new Response(JSON.stringify({ message: "Error in CF function", error: errorData }), { status: resp.status }); + } + + // Stream the response back to the client + const { readable, writable } = new TransformStream(); + resp && resp.body!.pipeTo(writable); + + return new Response(readable, { status: 200 }); +}
\ No newline at end of file |