access.ts 6.7 KB

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