baidu.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { getServerSideConfig } from "@/app/config/server";
  2. import {
  3. BAIDU_BASE_URL,
  4. ApiPath,
  5. ModelProvider,
  6. ServiceProvider,
  7. } from "@/app/constant";
  8. import { prettyObject } from "@/app/utils/format";
  9. import { NextRequest, NextResponse } from "next/server";
  10. import { auth } from "@/app/api/auth";
  11. import { isModelNotavailableInServer } from "@/app/utils/model";
  12. import { getAccessToken } from "@/app/utils/baidu";
  13. const serverConfig = getServerSideConfig();
  14. export async function handle(
  15. req: NextRequest,
  16. { params }: { params: { path: string[] } },
  17. ) {
  18. console.log("[Baidu Route] params ", params);
  19. if (req.method === "OPTIONS") {
  20. return NextResponse.json({ body: "OK" }, { status: 200 });
  21. }
  22. const authResult = auth(req, ModelProvider.Ernie);
  23. if (authResult.error) {
  24. return NextResponse.json(authResult, {
  25. status: 401,
  26. });
  27. }
  28. if (!serverConfig.baiduApiKey || !serverConfig.baiduSecretKey) {
  29. return NextResponse.json(
  30. {
  31. error: true,
  32. message: `missing BAIDU_API_KEY or BAIDU_SECRET_KEY in server env vars`,
  33. },
  34. {
  35. status: 401,
  36. },
  37. );
  38. }
  39. try {
  40. const response = await request(req);
  41. return response;
  42. } catch (e) {
  43. console.error("[Baidu] ", e);
  44. return NextResponse.json(prettyObject(e));
  45. }
  46. }
  47. async function request(req: NextRequest) {
  48. const controller = new AbortController();
  49. let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Baidu, "");
  50. let baseUrl = serverConfig.baiduUrl || BAIDU_BASE_URL;
  51. if (!baseUrl.startsWith("http")) {
  52. baseUrl = `https://${baseUrl}`;
  53. }
  54. if (baseUrl.endsWith("/")) {
  55. baseUrl = baseUrl.slice(0, -1);
  56. }
  57. console.log("[Proxy] ", path);
  58. console.log("[Base Url]", baseUrl);
  59. const timeoutId = setTimeout(
  60. () => {
  61. controller.abort();
  62. },
  63. 10 * 60 * 1000,
  64. );
  65. const { access_token } = await getAccessToken(
  66. serverConfig.baiduApiKey as string,
  67. serverConfig.baiduSecretKey as string,
  68. );
  69. const fetchUrl = `${baseUrl}${path}?access_token=${access_token}`;
  70. const fetchOptions: RequestInit = {
  71. headers: {
  72. "Content-Type": "application/json",
  73. },
  74. method: req.method,
  75. body: req.body,
  76. redirect: "manual",
  77. // @ts-ignore
  78. duplex: "half",
  79. signal: controller.signal,
  80. };
  81. // #1815 try to refuse some request to some models
  82. if (serverConfig.customModels && req.body) {
  83. try {
  84. const clonedBody = await req.text();
  85. fetchOptions.body = clonedBody;
  86. const jsonBody = JSON.parse(clonedBody) as { model?: string };
  87. // not undefined and is false
  88. if (
  89. isModelNotavailableInServer(
  90. serverConfig.customModels,
  91. jsonBody?.model as string,
  92. ServiceProvider.Baidu as string,
  93. )
  94. ) {
  95. return NextResponse.json(
  96. {
  97. error: true,
  98. message: `you are not allowed to use ${jsonBody?.model} model`,
  99. },
  100. {
  101. status: 403,
  102. },
  103. );
  104. }
  105. } catch (e) {
  106. console.error(`[Baidu] filter`, e);
  107. }
  108. }
  109. try {
  110. const res = await fetch(fetchUrl, fetchOptions);
  111. // to prevent browser prompt for credentials
  112. const newHeaders = new Headers(res.headers);
  113. newHeaders.delete("www-authenticate");
  114. // to disable nginx buffering
  115. newHeaders.set("X-Accel-Buffering", "no");
  116. return new Response(res.body, {
  117. status: res.status,
  118. statusText: res.statusText,
  119. headers: newHeaders,
  120. });
  121. } finally {
  122. clearTimeout(timeoutId);
  123. }
  124. }