route.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { NextRequest, NextResponse } from "next/server";
  2. const BACKEND_HOST = process.env.NEXT_PUBLIC_BACKEND_HOST || "localhost";
  3. const BACKEND_PORT = process.env.NEXT_PUBLIC_BACKEND_PORT || "8080";
  4. const BACKEND_BASE = `http://${BACKEND_HOST}:${BACKEND_PORT}`;
  5. /** 开发环境:将 /api/agent/prompts 代理到后端,避免 rewrites 在 Turbopack 下不稳定 */
  6. export async function GET(request: NextRequest) {
  7. const { searchParams } = new URL(request.url);
  8. const backendUrl = `${BACKEND_BASE}/agent/prompts?${searchParams.toString()}`;
  9. try {
  10. const res = await fetch(backendUrl, { cache: "no-store" });
  11. const body = await res.text();
  12. return new NextResponse(body, {
  13. status: res.status,
  14. headers: { "Content-Type": res.headers.get("content-type") || "application/json" },
  15. });
  16. } catch (e) {
  17. return NextResponse.json(
  18. { error: "无法连接后端,请确认后端已启动且端口一致(默认 8080)" },
  19. { status: 502 }
  20. );
  21. }
  22. }
  23. export async function PUT(request: NextRequest) {
  24. const backendUrl = `${BACKEND_BASE}/agent/prompts`;
  25. try {
  26. const body = await request.text();
  27. const res = await fetch(backendUrl, {
  28. method: "PUT",
  29. headers: { "Content-Type": "application/json" },
  30. body,
  31. });
  32. const resBody = await res.text();
  33. return new NextResponse(resBody, {
  34. status: res.status,
  35. headers: { "Content-Type": res.headers.get("content-type") || "application/json" },
  36. });
  37. } catch (e) {
  38. return NextResponse.json(
  39. { error: "无法连接后端,请确认后端已启动且端口一致(默认 8080)" },
  40. { status: 502 }
  41. );
  42. }
  43. }