| 1 | import { NextRequest, NextResponse } from "next/server"; |
| 2 | |
| 3 | export async function POST(req: NextRequest) { |
| 4 | try { |
| 5 | const body = await req.json(); |
| 6 | const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:3001"; |
| 7 | |
| 8 | const response = await fetch(`${apiUrl}/api/chat`, { |
| 9 | method: "POST", |
| 10 | headers: { |
| 11 | "Content-Type": "application/json", |
| 12 | ...(process.env.ANTHROPIC_API_KEY |
| 13 | ? { Authorization: `Bearer ${process.env.ANTHROPIC_API_KEY}` } |
| 14 | : {}), |
| 15 | }, |
| 16 | body: JSON.stringify(body), |
| 17 | }); |
| 18 | |
| 19 | if (!response.ok) { |
| 20 | return NextResponse.json( |
| 21 | { error: "Backend request failed" }, |
| 22 | { status: response.status } |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | // Stream the response through |
| 27 | return new NextResponse(response.body, { |
| 28 | headers: { |
| 29 | "Content-Type": response.headers.get("Content-Type") ?? "application/json", |
| 30 | }, |
| 31 | }); |
| 32 | } catch (error) { |
| 33 | console.error("Chat API error:", error); |
| 34 | return NextResponse.json({ error: "Internal server error" }, { status: 500 }); |
| 35 | } |
| 36 | } |