route.ts 2.6 KB

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