aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/app/api
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-03-01 11:44:23 -0700
committerDhravya <[email protected]>2024-03-01 11:44:23 -0700
commitdc7e35e76a49ebe7101c93411a511ff659f9b1fa (patch)
treec70db0ef4bd14dc7d63c4f9219a1e38ff07e1c0a /apps/web/src/app/api
parentfeat: Added AI query UI (diff)
downloadsupermemory-dc7e35e76a49ebe7101c93411a511ff659f9b1fa.tar.xz
supermemory-dc7e35e76a49ebe7101c93411a511ff659f9b1fa.zip
feat: added and fixed query and search interface
Diffstat (limited to 'apps/web/src/app/api')
-rw-r--r--apps/web/src/app/api/ask/route.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/apps/web/src/app/api/ask/route.ts b/apps/web/src/app/api/ask/route.ts
new file mode 100644
index 00000000..cad7a671
--- /dev/null
+++ b/apps/web/src/app/api/ask/route.ts
@@ -0,0 +1,48 @@
+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";
+
+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 body = await req.json() as {
+ query: string;
+ }
+
+ const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/ask`, {
+ headers: {
+ "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
+ },
+ method: "POST",
+ body: JSON.stringify({
+ query: body.query,
+ }),
+ })
+
+ if (resp.status !== 200 || !resp.ok) {
+ const errorData = await resp.json();
+ 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