access.ts 2.7 KB

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