File size: 1,546 Bytes
4b81334
6be14c8
4b81334
 
 
 
6be14c8
 
 
 
 
 
 
4b81334
6be14c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b81334
6be14c8
4b81334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { type NextRequest, NextResponse } from "next/server";
import { backendUrl } from "@/lib/backend-url";

export const dynamic = "force-dynamic";
export const runtime = "nodejs";

const BACKEND_RETRY_ATTEMPTS = 20;
const BACKEND_RETRY_DELAY_MS = 500;

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function POST(request: NextRequest) {
  const backend = backendUrl();
  const body = await request.text();

  let response: Response | undefined;
  for (let attempt = 1; ; attempt += 1) {
    try {
      response = await fetch(`${backend}/chat`, {
        method: "POST",
        body,
        headers: {
          "content-type": request.headers.get("content-type") ?? "application/json",
        },
      });
      break;
    } catch {
      if (attempt >= BACKEND_RETRY_ATTEMPTS) {
        return NextResponse.json(
          { detail: `Could not reach the backend chat service at ${backend}.` },
          { status: 502 }
        );
      }
      await delay(BACKEND_RETRY_DELAY_MS);
    }
  }

  if (!response) {
    return NextResponse.json(
      { detail: `Could not reach the backend chat service at ${backend}.` },
      { status: 502 }
    );
  }

  const headers = new Headers();
  headers.set(
    "content-type",
    response.headers.get("content-type") ?? "application/x-ndjson"
  );
  headers.set("cache-control", "no-cache, no-transform");

  return new Response(response.body, {
    status: response.status,
    statusText: response.statusText,
    headers,
  });
}