aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/chat/route.ts
blob: 340998481e4607354f827a8d2ba52de4edb7406e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { type NextRequest } from "next/server";
import { ChatHistory } from "@repo/shared-types";
import { ensureAuth } from "../ensureAuth";

export const runtime = "edge";

export async function POST(req: NextRequest) {
  const session = await ensureAuth(req);

  if (!session) {
    return new Response("Unauthorized", { status: 401 });
  }

  if (!process.env.BACKEND_SECURITY_KEY) {
    return new Response("Missing BACKEND_SECURITY_KEY", { status: 500 });
  }

  const query = new URL(req.url).searchParams.get("q");
  const spaces = new URL(req.url).searchParams.get("spaces");

  const sourcesOnly =
    new URL(req.url).searchParams.get("sourcesOnly") ?? "false";

  const chatHistory = (await req.json()) as {
    chatHistory: ChatHistory[];
  };

  console.log("CHathistory", chatHistory);

  if (!query) {
    return new Response(JSON.stringify({ message: "Invalid query" }), {
      status: 400,
    });
  }

  try {
    const resp = await fetch(
      `https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
      {
        headers: {
          "X-Custom-Auth-Key": process.env.BACKEND_SECURITY_KEY!,
        },
        method: "POST",
        body: JSON.stringify({
          chatHistory: chatHistory.chatHistory ?? [],
        }),
      },
    );

    console.log("sourcesOnly", sourcesOnly);

    if (sourcesOnly == "true") {
      const data = await resp.json();
      console.log("data", data);
      return new Response(JSON.stringify(data), { status: 200 });
    }

    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 });
  } catch {}
}