access.ts 6.0 KB

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