access.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // anthropic
  32. anthropicApiKey: "",
  33. anthropicApiVersion: "2023-06-01",
  34. anthropicUrl: "",
  35. // server config
  36. needCode: true,
  37. hideUserApiKey: false,
  38. hideBalanceQuery: false,
  39. disableGPT4: false,
  40. disableFastLink: false,
  41. customModels: "",
  42. };
  43. export const useAccessStore = createPersistStore(
  44. { ...DEFAULT_ACCESS_STATE },
  45. (set, get) => ({
  46. enabledAccessControl() {
  47. this.fetch();
  48. return get().needCode;
  49. },
  50. isValidOpenAI() {
  51. return ensure(get(), ["openaiApiKey"]);
  52. },
  53. isValidAzure() {
  54. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  55. },
  56. isValidGoogle() {
  57. return ensure(get(), ["googleApiKey"]);
  58. },
  59. isValidAnthropic() {
  60. return ensure(get(), ["anthropicApiKey"]);
  61. },
  62. isAuthorized() {
  63. this.fetch();
  64. // has token or has code or disabled access control
  65. return (
  66. this.isValidOpenAI() ||
  67. this.isValidAzure() ||
  68. this.isValidGoogle() ||
  69. this.isValidAnthropic() ||
  70. !this.enabledAccessControl() ||
  71. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  72. );
  73. },
  74. fetch() {
  75. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  76. fetchState = 1;
  77. fetch("/api/config", {
  78. method: "post",
  79. body: null,
  80. headers: {
  81. ...getHeaders(),
  82. },
  83. })
  84. .then((res) => res.json())
  85. .then((res: DangerConfig) => {
  86. console.log("[Config] got config from server", res);
  87. set(() => ({ ...res }));
  88. })
  89. .catch(() => {
  90. console.error("[Config] failed to fetch config");
  91. })
  92. .finally(() => {
  93. fetchState = 2;
  94. });
  95. },
  96. }),
  97. {
  98. name: StoreKey.Access,
  99. version: 2,
  100. migrate(persistedState, version) {
  101. if (version < 2) {
  102. const state = persistedState as {
  103. token: string;
  104. openaiApiKey: string;
  105. azureApiVersion: string;
  106. googleApiKey: string;
  107. };
  108. state.openaiApiKey = state.token;
  109. state.azureApiVersion = "2023-08-01-preview";
  110. }
  111. return persistedState as any;
  112. },
  113. },
  114. );