aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/app/api/ask
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-05-25 18:41:26 -0500
committerDhravya <[email protected]>2024-05-25 18:41:26 -0500
commit075f45986fd4d198292226e64afb71b3515576b4 (patch)
tree5c728356cd0310f1c1c012fd6618c72a836c314b /apps/web/src/app/api/ask
parentadded social material (diff)
downloadarchived-supermemory-075f45986fd4d198292226e64afb71b3515576b4.tar.xz
archived-supermemory-075f45986fd4d198292226e64afb71b3515576b4.zip
refactored UI, with shared components and UI, better rules and million lint
Diffstat (limited to 'apps/web/src/app/api/ask')
-rw-r--r--apps/web/src/app/api/ask/route.ts78
1 files changed, 0 insertions, 78 deletions
diff --git a/apps/web/src/app/api/ask/route.ts b/apps/web/src/app/api/ask/route.ts
deleted file mode 100644
index 17b24b3e..00000000
--- a/apps/web/src/app/api/ask/route.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-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 ", "");
-
- if (process.env.RATELIMITER) {
- const { success } = await process.env.RATELIMITER.limit({ key: token });
-
- if (!success) {
- return new Response(JSON.stringify({ message: "Rate limit exceeded" }), {
- status: 429,
- });
- }
- }
-
- 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 });
-}