access.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import { DEFAULT_CONFIG } from "./config";
  12. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  13. const DEFAULT_OPENAI_URL =
  14. getClientConfig()?.buildMode === "export"
  15. ? DEFAULT_API_HOST + "/api/proxy/openai"
  16. : ApiPath.OpenAI;
  17. const DEFAULT_AZURE_URL =
  18. getClientConfig()?.buildMode === "export"
  19. ? DEFAULT_API_HOST + "/api/proxy/azure/{resource_name}"
  20. : ApiPath.Azure;
  21. const DEFAULT_ACCESS_STATE = {
  22. accessCode: "",
  23. useCustomConfig: false,
  24. provider: ServiceProvider.OpenAI,
  25. // openai
  26. openaiUrl: DEFAULT_OPENAI_URL,
  27. openaiApiKey: "",
  28. // azure
  29. azureUrl: DEFAULT_AZURE_URL,
  30. azureApiKey: "",
  31. azureApiVersion: "2023-08-01-preview",
  32. // google ai studio
  33. googleUrl: "",
  34. googleApiKey: "",
  35. googleApiVersion: "v1",
  36. // anthropic
  37. anthropicApiKey: "",
  38. anthropicApiVersion: "2023-06-01",
  39. anthropicUrl: "",
  40. // baidu
  41. baiduUrl: "",
  42. baiduApiKey: "",
  43. baiduSecretKey: "",
  44. // bytedance
  45. bytedanceApiKey: "",
  46. bytedanceUrl: "",
  47. // server config
  48. needCode: true,
  49. hideUserApiKey: false,
  50. hideBalanceQuery: false,
  51. disableGPT4: false,
  52. disableFastLink: false,
  53. customModels: "",
  54. defaultModel: "",
  55. };
  56. export const useAccessStore = createPersistStore(
  57. { ...DEFAULT_ACCESS_STATE },
  58. (set, get) => ({
  59. enabledAccessControl() {
  60. this.fetch();
  61. return get().needCode;
  62. },
  63. isValidOpenAI() {
  64. return ensure(get(), ["openaiApiKey"]);
  65. },
  66. isValidAzure() {
  67. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  68. },
  69. isValidGoogle() {
  70. return ensure(get(), ["googleApiKey"]);
  71. },
  72. isValidAnthropic() {
  73. return ensure(get(), ["anthropicApiKey"]);
  74. },
  75. isValidBaidu() {
  76. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  77. },
  78. isValidByteDance() {
  79. return ensure(get(), ["bytedanceApiKey"]);
  80. },
  81. isAuthorized() {
  82. this.fetch();
  83. // has token or has code or disabled access control
  84. return (
  85. this.isValidOpenAI() ||
  86. this.isValidAzure() ||
  87. this.isValidGoogle() ||
  88. this.isValidAnthropic() ||
  89. this.isValidBaidu() ||
  90. this.isValidByteDance() ||
  91. !this.enabledAccessControl() ||
  92. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  93. );
  94. },
  95. fetch() {
  96. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  97. fetchState = 1;
  98. fetch("/api/config", {
  99. method: "post",
  100. body: null,
  101. headers: {
  102. ...getHeaders(),
  103. },
  104. })
  105. .then((res) => res.json())
  106. .then((res) => {
  107. // Set default model from env request
  108. let defaultModel = res.defaultModel ?? "";
  109. DEFAULT_CONFIG.modelConfig.model =
  110. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  111. return res;
  112. })
  113. .then((res: DangerConfig) => {
  114. console.log("[Config] got config from server", res);
  115. set(() => ({ ...res }));
  116. })
  117. .catch(() => {
  118. console.error("[Config] failed to fetch config");
  119. })
  120. .finally(() => {
  121. fetchState = 2;
  122. });
  123. },
  124. }),
  125. {
  126. name: StoreKey.Access,
  127. version: 2,
  128. migrate(persistedState, version) {
  129. if (version < 2) {
  130. const state = persistedState as {
  131. token: string;
  132. openaiApiKey: string;
  133. azureApiVersion: string;
  134. googleApiKey: string;
  135. };
  136. state.openaiApiKey = state.token;
  137. state.azureApiVersion = "2023-08-01-preview";
  138. }
  139. return persistedState as any;
  140. },
  141. },
  142. );