access.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // tts config
  103. edgeTTSVoiceName: "zh-CN-YunxiNeural",
  104. };
  105. export const useAccessStore = createPersistStore(
  106. { ...DEFAULT_ACCESS_STATE },
  107. (set, get) => ({
  108. enabledAccessControl() {
  109. this.fetch();
  110. return get().needCode;
  111. },
  112. edgeVoiceName() {
  113. this.fetch();
  114. return get().edgeTTSVoiceName;
  115. },
  116. isValidOpenAI() {
  117. return ensure(get(), ["openaiApiKey"]);
  118. },
  119. isValidAzure() {
  120. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  121. },
  122. isValidGoogle() {
  123. return ensure(get(), ["googleApiKey"]);
  124. },
  125. isValidAnthropic() {
  126. return ensure(get(), ["anthropicApiKey"]);
  127. },
  128. isValidBaidu() {
  129. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  130. },
  131. isValidByteDance() {
  132. return ensure(get(), ["bytedanceApiKey"]);
  133. },
  134. isValidAlibaba() {
  135. return ensure(get(), ["alibabaApiKey"]);
  136. },
  137. isValidTencent() {
  138. return ensure(get(), ["tencentSecretKey", "tencentSecretId"]);
  139. },
  140. isValidMoonshot() {
  141. return ensure(get(), ["moonshotApiKey"]);
  142. },
  143. isValidIflytek() {
  144. return ensure(get(), ["iflytekApiKey"]);
  145. },
  146. isValidDeepSeek() {
  147. return ensure(get(), ["deepseekApiKey"]);
  148. },
  149. isValidXAI() {
  150. return ensure(get(), ["xaiApiKey"]);
  151. },
  152. isValidChatGLM() {
  153. return ensure(get(), ["chatglmApiKey"]);
  154. },
  155. isAuthorized() {
  156. this.fetch();
  157. // has token or has code or disabled access control
  158. return (
  159. this.isValidOpenAI() ||
  160. this.isValidAzure() ||
  161. this.isValidGoogle() ||
  162. this.isValidAnthropic() ||
  163. this.isValidBaidu() ||
  164. this.isValidByteDance() ||
  165. this.isValidAlibaba() ||
  166. this.isValidTencent() ||
  167. this.isValidMoonshot() ||
  168. this.isValidIflytek() ||
  169. this.isValidDeepSeek() ||
  170. this.isValidXAI() ||
  171. this.isValidChatGLM() ||
  172. !this.enabledAccessControl() ||
  173. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  174. );
  175. },
  176. fetch() {
  177. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  178. fetchState = 1;
  179. fetch("/api/config", {
  180. method: "post",
  181. body: null,
  182. headers: {
  183. ...getHeaders(),
  184. },
  185. })
  186. .then((res) => res.json())
  187. .then((res) => {
  188. const defaultModel = res.defaultModel ?? "";
  189. if (defaultModel !== "") {
  190. const [model, providerName] = getModelProvider(defaultModel);
  191. DEFAULT_CONFIG.modelConfig.model = model;
  192. DEFAULT_CONFIG.modelConfig.providerName = providerName as any;
  193. }
  194. return res;
  195. })
  196. .then((res: DangerConfig) => {
  197. console.log("[Config] got config from server", res);
  198. set(() => ({ ...res }));
  199. })
  200. .catch(() => {
  201. console.error("[Config] failed to fetch config");
  202. })
  203. .finally(() => {
  204. fetchState = 2;
  205. });
  206. },
  207. }),
  208. {
  209. name: StoreKey.Access,
  210. version: 2,
  211. migrate(persistedState, version) {
  212. if (version < 2) {
  213. const state = persistedState as {
  214. token: string;
  215. openaiApiKey: string;
  216. azureApiVersion: string;
  217. googleApiKey: string;
  218. };
  219. state.openaiApiKey = state.token;
  220. state.azureApiVersion = "2023-08-01-preview";
  221. }
  222. return persistedState as any;
  223. },
  224. },
  225. );