access.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. ApiPath,
  3. DEFAULT_API_HOST,
  4. ServiceProvider,
  5. StoreKey,
  6. } from "../constant";
  7. import { getHeaders } from "../client/api";
  8. import { getClientConfig } from "../config/client";
  9. import { createPersistStore } from "../utils/store";
  10. import { ensure } from "../utils/clone";
  11. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  12. const DEFAULT_OPENAI_URL =
  13. getClientConfig()?.buildMode === "export"
  14. ? DEFAULT_API_HOST + "/api/proxy/openai"
  15. : ApiPath.OpenAI;
  16. const DEFAULT_ACCESS_STATE = {
  17. accessCode: "",
  18. useCustomConfig: false,
  19. provider: ServiceProvider.OpenAI,
  20. // openai
  21. openaiUrl: DEFAULT_OPENAI_URL,
  22. openaiApiKey: "",
  23. // azure
  24. azureUrl: "",
  25. azureApiKey: "",
  26. azureApiVersion: "2023-08-01-preview",
  27. // google ai studio
  28. googleUrl: "",
  29. googleApiKey: "",
  30. googleApiVersion: "v1",
  31. // server config
  32. needCode: true,
  33. hideUserApiKey: false,
  34. hideBalanceQuery: false,
  35. disableGPT4: false,
  36. disableFastLink: false,
  37. customModels: "",
  38. };
  39. export const useAccessStore = createPersistStore(
  40. { ...DEFAULT_ACCESS_STATE },
  41. (set, get) => ({
  42. enabledAccessControl() {
  43. this.fetch();
  44. return get().needCode;
  45. },
  46. isValidOpenAI() {
  47. return ensure(get(), ["openaiApiKey"]);
  48. },
  49. isValidAzure() {
  50. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  51. },
  52. isValidGoogle() {
  53. return ensure(get(), ["googleApiKey"]);
  54. },
  55. isAuthorized() {
  56. this.fetch();
  57. // has token or has code or disabled access control
  58. return (
  59. this.isValidOpenAI() ||
  60. this.isValidAzure() ||
  61. this.isValidGoogle() ||
  62. !this.enabledAccessControl() ||
  63. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  64. );
  65. },
  66. fetch() {
  67. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  68. fetchState = 1;
  69. fetch("/api/config", {
  70. method: "post",
  71. body: null,
  72. headers: {
  73. ...getHeaders(),
  74. },
  75. })
  76. .then((res) => res.json())
  77. .then((res: DangerConfig) => {
  78. console.log("[Config] got config from server", res);
  79. set(() => ({ ...res }));
  80. })
  81. .catch(() => {
  82. console.error("[Config] failed to fetch config");
  83. })
  84. .finally(() => {
  85. fetchState = 2;
  86. });
  87. },
  88. }),
  89. {
  90. name: StoreKey.Access,
  91. version: 2,
  92. migrate(persistedState, version) {
  93. if (version < 2) {
  94. const state = persistedState as {
  95. token: string;
  96. openaiApiKey: string;
  97. azureApiVersion: string;
  98. googleApiKey: string;
  99. };
  100. state.openaiApiKey = state.token;
  101. state.azureApiVersion = "2023-08-01-preview";
  102. }
  103. return persistedState as any;
  104. },
  105. },
  106. );