server.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import md5 from "spark-md5";
  2. import { DEFAULT_MODELS } from "../constant";
  3. declare global {
  4. namespace NodeJS {
  5. interface ProcessEnv {
  6. OPENAI_API_KEY?: string;
  7. CODE?: string;
  8. BASE_URL?: string;
  9. PROXY_URL?: string;
  10. OPENAI_ORG_ID?: string;
  11. VERCEL?: string;
  12. HIDE_USER_API_KEY?: string; // disable user's api key input
  13. DISABLE_GPT4?: string; // allow user to use gpt-4 or not
  14. BUILD_MODE?: "standalone" | "export";
  15. BUILD_APP?: string; // is building desktop app
  16. ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
  17. DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
  18. CUSTOM_MODELS?: string; // to control custom models
  19. }
  20. }
  21. }
  22. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  23. const code = process.env.CODE;
  24. try {
  25. const codes = (code?.split(",") ?? [])
  26. .filter((v) => !!v)
  27. .map((v) => md5.hash(v.trim()));
  28. return new Set(codes);
  29. } catch (e) {
  30. return new Set();
  31. }
  32. })();
  33. export const getServerSideConfig = () => {
  34. if (typeof process === "undefined") {
  35. throw Error(
  36. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  37. );
  38. }
  39. let disableGPT4 = !!process.env.DISABLE_GPT4;
  40. let customModels = process.env.CUSTOM_MODELS ?? "";
  41. if (disableGPT4) {
  42. if (customModels) customModels += ",";
  43. customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
  44. .map((m) => "-" + m.name)
  45. .join(",");
  46. }
  47. return {
  48. apiKey: process.env.OPENAI_API_KEY,
  49. code: process.env.CODE,
  50. codes: ACCESS_CODES,
  51. needCode: ACCESS_CODES.size > 0,
  52. baseUrl: process.env.BASE_URL,
  53. proxyUrl: process.env.PROXY_URL,
  54. openaiOrgId: process.env.OPENAI_ORG_ID,
  55. isVercel: !!process.env.VERCEL,
  56. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  57. disableGPT4,
  58. hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
  59. disableFastLink: !!process.env.DISABLE_FAST_LINK,
  60. customModels,
  61. };
  62. };