server.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import md5 from "spark-md5";
  2. import { DEFAULT_MODELS } from "../constant";
  3. declare global {
  4. namespace NodeJS {
  5. interface ProcessEnv {
  6. BASE_PATH?: string;
  7. PROXY_URL?: string; // docker only
  8. OPENAI_API_KEY?: string;
  9. CODE?: string;
  10. BASE_URL?: string;
  11. OPENAI_ORG_ID?: string; // openai only
  12. VERCEL?: string;
  13. BUILD_MODE?: "standalone" | "export";
  14. BUILD_APP?: string; // is building desktop app
  15. HIDE_USER_API_KEY?: string; // disable user's api key input
  16. DISABLE_GPT4?: string; // allow user to use gpt-4 or not
  17. ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
  18. DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
  19. CUSTOM_MODELS?: string; // to control custom models
  20. // azure only
  21. AZURE_URL?: string; // https://{azure-url}/openai/deployments/{deploy-name}
  22. AZURE_API_KEY?: string;
  23. AZURE_API_VERSION?: string;
  24. // oauth2
  25. OAUTH_CLIENT_ID?: string;
  26. OAUTH_CLIENT_SECRET?: string;
  27. OAUTH_REDIRECT_URI?: string;
  28. OAUTH_AUTHORIZE_ENDPOINT?: string;
  29. OAUTH_USERINFO?: string;
  30. }
  31. }
  32. }
  33. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  34. const code = process.env.CODE;
  35. try {
  36. const codes = (code?.split(",") ?? [])
  37. .filter((v) => !!v)
  38. .map((v) => md5.hash(v.trim()));
  39. return new Set(codes);
  40. } catch (e) {
  41. return new Set();
  42. }
  43. })();
  44. export const getServerSideConfig = () => {
  45. if (typeof process === "undefined") {
  46. throw Error(
  47. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  48. );
  49. }
  50. const disableGPT4 = !!process.env.DISABLE_GPT4;
  51. let customModels = process.env.CUSTOM_MODELS ?? "";
  52. if (disableGPT4) {
  53. if (customModels) customModels += ",";
  54. customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
  55. .map((m) => "-" + m.name)
  56. .join(",");
  57. }
  58. const isAzure = !!process.env.AZURE_URL;
  59. const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
  60. const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
  61. const randomIndex = Math.floor(Math.random() * apiKeys.length);
  62. const apiKey = apiKeys[randomIndex];
  63. console.log(
  64. `[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
  65. );
  66. return {
  67. baseApi: process.env.BASE_PATH,
  68. baseUrl: process.env.BASE_URL,
  69. apiKey,
  70. openaiOrgId: process.env.OPENAI_ORG_ID,
  71. isAzure,
  72. azureUrl: process.env.AZURE_URL,
  73. azureApiKey: process.env.AZURE_API_KEY,
  74. azureApiVersion: process.env.AZURE_API_VERSION,
  75. needCode: ACCESS_CODES.size > 0,
  76. code: process.env.CODE,
  77. codes: ACCESS_CODES,
  78. proxyUrl: process.env.PROXY_URL,
  79. isVercel: !!process.env.VERCEL,
  80. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  81. disableGPT4,
  82. hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
  83. disableFastLink: !!process.env.DISABLE_FAST_LINK,
  84. customModels,
  85. // oauth2
  86. OAUTH_CLIENT_ID: process.env.OAUTH_CLIENT_ID,
  87. OAUTH_CLIENT_SECRET: process.env.OAUTH_CLIENT_SECRET,
  88. OAUTH_REDIRECT_URI: process.env.OAUTH_REDIRECT_URI,
  89. OAUTH_AUTHORIZE_ENDPOINT: process.env.OAUTH_AUTHORIZE_ENDPOINT,
  90. OAUTH_USERINFO: process.env.OAUTH_USERINFO,
  91. };
  92. };