access.ts 7.1 KB

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