access.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {
  2. GoogleSafetySettingsThreshold,
  3. ServiceProvider,
  4. StoreKey,
  5. ApiPath,
  6. OPENAI_BASE_URL,
  7. ANTHROPIC_BASE_URL,
  8. GEMINI_BASE_URL,
  9. BAIDU_BASE_URL,
  10. BYTEDANCE_BASE_URL,
  11. ALIBABA_BASE_URL,
  12. TENCENT_BASE_URL,
  13. MOONSHOT_BASE_URL,
  14. STABILITY_BASE_URL,
  15. IFLYTEK_BASE_URL,
  16. XAI_BASE_URL,
  17. CHATGLM_BASE_URL,
  18. } from "../constant";
  19. import { getHeaders } from "../client/api";
  20. import { getClientConfig } from "../config/client";
  21. import { createPersistStore } from "../utils/store";
  22. import { ensure } from "../utils/clone";
  23. import { DEFAULT_CONFIG } from "./config";
  24. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  25. const isApp = getClientConfig()?.buildMode === "export";
  26. const DEFAULT_OPENAI_URL = isApp ? OPENAI_BASE_URL : ApiPath.OpenAI;
  27. const DEFAULT_GOOGLE_URL = isApp ? GEMINI_BASE_URL : ApiPath.Google;
  28. const DEFAULT_ANTHROPIC_URL = isApp ? ANTHROPIC_BASE_URL : ApiPath.Anthropic;
  29. const DEFAULT_BAIDU_URL = isApp ? BAIDU_BASE_URL : ApiPath.Baidu;
  30. const DEFAULT_BYTEDANCE_URL = isApp ? BYTEDANCE_BASE_URL : ApiPath.ByteDance;
  31. const DEFAULT_ALIBABA_URL = isApp ? ALIBABA_BASE_URL : ApiPath.Alibaba;
  32. const DEFAULT_TENCENT_URL = isApp ? TENCENT_BASE_URL : ApiPath.Tencent;
  33. const DEFAULT_MOONSHOT_URL = isApp ? MOONSHOT_BASE_URL : ApiPath.Moonshot;
  34. const DEFAULT_STABILITY_URL = isApp ? STABILITY_BASE_URL : ApiPath.Stability;
  35. const DEFAULT_IFLYTEK_URL = isApp ? IFLYTEK_BASE_URL : ApiPath.Iflytek;
  36. const DEFAULT_XAI_URL = isApp ? XAI_BASE_URL : ApiPath.XAI;
  37. const DEFAULT_CHATGLM_URL = isApp ? CHATGLM_BASE_URL : ApiPath.ChatGLM;
  38. const DEFAULT_ACCESS_STATE = {
  39. accessCode: "",
  40. useCustomConfig: false,
  41. provider: ServiceProvider.OpenAI,
  42. // openai
  43. openaiUrl: DEFAULT_OPENAI_URL,
  44. openaiApiKey: "",
  45. // azure
  46. azureUrl: "",
  47. azureApiKey: "",
  48. azureApiVersion: "2023-08-01-preview",
  49. // google ai studio
  50. googleUrl: DEFAULT_GOOGLE_URL,
  51. googleApiKey: "",
  52. googleApiVersion: "v1",
  53. googleSafetySettings: GoogleSafetySettingsThreshold.BLOCK_ONLY_HIGH,
  54. // anthropic
  55. anthropicUrl: DEFAULT_ANTHROPIC_URL,
  56. anthropicApiKey: "",
  57. anthropicApiVersion: "2023-06-01",
  58. // baidu
  59. baiduUrl: DEFAULT_BAIDU_URL,
  60. baiduApiKey: "",
  61. baiduSecretKey: "",
  62. // bytedance
  63. bytedanceUrl: DEFAULT_BYTEDANCE_URL,
  64. bytedanceApiKey: "",
  65. // alibaba
  66. alibabaUrl: DEFAULT_ALIBABA_URL,
  67. alibabaApiKey: "",
  68. // moonshot
  69. moonshotUrl: DEFAULT_MOONSHOT_URL,
  70. moonshotApiKey: "",
  71. //stability
  72. stabilityUrl: DEFAULT_STABILITY_URL,
  73. stabilityApiKey: "",
  74. // tencent
  75. tencentUrl: DEFAULT_TENCENT_URL,
  76. tencentSecretKey: "",
  77. tencentSecretId: "",
  78. // iflytek
  79. iflytekUrl: DEFAULT_IFLYTEK_URL,
  80. iflytekApiKey: "",
  81. iflytekApiSecret: "",
  82. // xai
  83. xaiUrl: DEFAULT_XAI_URL,
  84. xaiApiKey: "",
  85. // chatglm
  86. chatglmUrl: DEFAULT_CHATGLM_URL,
  87. chatglmApiKey: "",
  88. // server config
  89. needCode: true,
  90. hideUserApiKey: false,
  91. hideBalanceQuery: false,
  92. disableGPT4: false,
  93. disableFastLink: false,
  94. customModels: "",
  95. defaultModel: "",
  96. // tts config
  97. edgeTTSVoiceName: "zh-CN-YunxiNeural",
  98. };
  99. export const useAccessStore = createPersistStore(
  100. { ...DEFAULT_ACCESS_STATE },
  101. (set, get) => ({
  102. enabledAccessControl() {
  103. this.fetch();
  104. return get().needCode;
  105. },
  106. edgeVoiceName() {
  107. this.fetch();
  108. return get().edgeTTSVoiceName;
  109. },
  110. isValidOpenAI() {
  111. return ensure(get(), ["openaiApiKey"]);
  112. },
  113. isValidAzure() {
  114. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  115. },
  116. isValidGoogle() {
  117. return ensure(get(), ["googleApiKey"]);
  118. },
  119. isValidAnthropic() {
  120. return ensure(get(), ["anthropicApiKey"]);
  121. },
  122. isValidBaidu() {
  123. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  124. },
  125. isValidByteDance() {
  126. return ensure(get(), ["bytedanceApiKey"]);
  127. },
  128. isValidAlibaba() {
  129. return ensure(get(), ["alibabaApiKey"]);
  130. },
  131. isValidTencent() {
  132. return ensure(get(), ["tencentSecretKey", "tencentSecretId"]);
  133. },
  134. isValidMoonshot() {
  135. return ensure(get(), ["moonshotApiKey"]);
  136. },
  137. isValidIflytek() {
  138. return ensure(get(), ["iflytekApiKey"]);
  139. },
  140. isValidXAI() {
  141. return ensure(get(), ["xaiApiKey"]);
  142. },
  143. isValidChatGLM() {
  144. return ensure(get(), ["chatglmApiKey"]);
  145. },
  146. isAuthorized() {
  147. this.fetch();
  148. // has token or has code or disabled access control
  149. return (
  150. this.isValidOpenAI() ||
  151. this.isValidAzure() ||
  152. this.isValidGoogle() ||
  153. this.isValidAnthropic() ||
  154. this.isValidBaidu() ||
  155. this.isValidByteDance() ||
  156. this.isValidAlibaba() ||
  157. this.isValidTencent() ||
  158. this.isValidMoonshot() ||
  159. this.isValidIflytek() ||
  160. this.isValidXAI() ||
  161. this.isValidChatGLM() ||
  162. !this.enabledAccessControl() ||
  163. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  164. );
  165. },
  166. fetch() {
  167. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  168. fetchState = 1;
  169. fetch("/api/config", {
  170. method: "post",
  171. body: null,
  172. headers: {
  173. ...getHeaders(),
  174. },
  175. })
  176. .then((res) => res.json())
  177. .then((res) => {
  178. const defaultModel = res.defaultModel ?? "";
  179. if (defaultModel !== "") {
  180. const [model, providerName] = defaultModel.split("@");
  181. DEFAULT_CONFIG.modelConfig.model = model;
  182. DEFAULT_CONFIG.modelConfig.providerName = providerName;
  183. }
  184. return res;
  185. })
  186. .then((res: DangerConfig) => {
  187. console.log("[Config] got config from server", res);
  188. set(() => ({ ...res }));
  189. })
  190. .catch(() => {
  191. console.error("[Config] failed to fetch config");
  192. })
  193. .finally(() => {
  194. fetchState = 2;
  195. });
  196. },
  197. }),
  198. {
  199. name: StoreKey.Access,
  200. version: 2,
  201. migrate(persistedState, version) {
  202. if (version < 2) {
  203. const state = persistedState as {
  204. token: string;
  205. openaiApiKey: string;
  206. azureApiVersion: string;
  207. googleApiKey: string;
  208. };
  209. state.openaiApiKey = state.token;
  210. state.azureApiVersion = "2023-08-01-preview";
  211. }
  212. return persistedState as any;
  213. },
  214. },
  215. );