access.ts 5.7 KB

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