access.ts 6.3 KB

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