route.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { getServerSideConfig } from "@/app/config/server";
  2. import { TENCENT_BASE_URL, ModelProvider } from "@/app/constant";
  3. import { prettyObject } from "@/app/utils/format";
  4. import { NextRequest, NextResponse } from "next/server";
  5. import { auth } from "@/app/api/auth";
  6. import { getHeader } from "@/app/utils/tencent";
  7. const serverConfig = getServerSideConfig();
  8. async function handle(
  9. req: NextRequest,
  10. { params }: { params: { path: string[] } },
  11. ) {
  12. console.log("[Tencent Route] params ", params);
  13. if (req.method === "OPTIONS") {
  14. return NextResponse.json({ body: "OK" }, { status: 200 });
  15. }
  16. const authResult = auth(req, ModelProvider.Hunyuan);
  17. if (authResult.error) {
  18. return NextResponse.json(authResult, {
  19. status: 401,
  20. });
  21. }
  22. try {
  23. const response = await request(req);
  24. return response;
  25. } catch (e) {
  26. console.error("[Tencent] ", e);
  27. return NextResponse.json(prettyObject(e));
  28. }
  29. }
  30. export const GET = handle;
  31. export const POST = handle;
  32. export const runtime = "edge";
  33. export const preferredRegion = [
  34. "arn1",
  35. "bom1",
  36. "cdg1",
  37. "cle1",
  38. "cpt1",
  39. "dub1",
  40. "fra1",
  41. "gru1",
  42. "hnd1",
  43. "iad1",
  44. "icn1",
  45. "kix1",
  46. "lhr1",
  47. "pdx1",
  48. "sfo1",
  49. "sin1",
  50. "syd1",
  51. ];
  52. async function request(req: NextRequest) {
  53. const controller = new AbortController();
  54. let baseUrl = serverConfig.tencentUrl || TENCENT_BASE_URL;
  55. if (!baseUrl.startsWith("http")) {
  56. baseUrl = `https://${baseUrl}`;
  57. }
  58. if (baseUrl.endsWith("/")) {
  59. baseUrl = baseUrl.slice(0, -1);
  60. }
  61. console.log("[Base Url]", baseUrl);
  62. const timeoutId = setTimeout(
  63. () => {
  64. controller.abort();
  65. },
  66. 10 * 60 * 1000,
  67. );
  68. const fetchUrl = baseUrl;
  69. const body = await req.text();
  70. const headers = await getHeader(
  71. body,
  72. serverConfig.tencentSecretId as string,
  73. serverConfig.tencentSecretKey as string,
  74. );
  75. const fetchOptions: RequestInit = {
  76. headers,
  77. method: req.method,
  78. body,
  79. redirect: "manual",
  80. // @ts-ignore
  81. duplex: "half",
  82. signal: controller.signal,
  83. };
  84. try {
  85. const res = await fetch(fetchUrl, fetchOptions);
  86. // to prevent browser prompt for credentials
  87. const newHeaders = new Headers(res.headers);
  88. newHeaders.delete("www-authenticate");
  89. // to disable nginx buffering
  90. newHeaders.set("X-Accel-Buffering", "no");
  91. return new Response(res.body, {
  92. status: res.status,
  93. statusText: res.statusText,
  94. headers: newHeaders,
  95. });
  96. } finally {
  97. clearTimeout(timeoutId);
  98. }
  99. }