access.ts 7.0 KB

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