access.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {
  2. ApiPath,
  3. DEFAULT_API_HOST,
  4. GoogleSafetySettingsThreshold,
  5. ServiceProvider,
  6. StoreKey,
  7. } from "../constant";
  8. import { getHeaders } from "../client/api";
  9. import { getClientConfig } from "../config/client";
  10. import { createPersistStore } from "../utils/store";
  11. import { ensure } from "../utils/clone";
  12. import { DEFAULT_CONFIG } from "./config";
  13. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  14. const isApp = getClientConfig()?.buildMode === "export";
  15. const DEFAULT_OPENAI_URL = isApp
  16. ? DEFAULT_API_HOST + "/api/proxy/openai"
  17. : ApiPath.OpenAI;
  18. const DEFAULT_GOOGLE_URL = isApp
  19. ? DEFAULT_API_HOST + "/api/proxy/google"
  20. : ApiPath.Google;
  21. const DEFAULT_ANTHROPIC_URL = isApp
  22. ? DEFAULT_API_HOST + "/api/proxy/anthropic"
  23. : ApiPath.Anthropic;
  24. const DEFAULT_BAIDU_URL = isApp
  25. ? DEFAULT_API_HOST + "/api/proxy/baidu"
  26. : ApiPath.Baidu;
  27. const DEFAULT_BYTEDANCE_URL = isApp
  28. ? DEFAULT_API_HOST + "/api/proxy/bytedance"
  29. : ApiPath.ByteDance;
  30. const DEFAULT_ALIBABA_URL = isApp
  31. ? DEFAULT_API_HOST + "/api/proxy/alibaba"
  32. : ApiPath.Alibaba;
  33. const DEFAULT_TENCENT_URL = isApp
  34. ? DEFAULT_API_HOST + "/api/proxy/tencent"
  35. : ApiPath.Tencent;
  36. const DEFAULT_MOONSHOT_URL = isApp
  37. ? DEFAULT_API_HOST + "/api/proxy/moonshot"
  38. : ApiPath.Moonshot;
  39. const DEFAULT_STABILITY_URL = isApp
  40. ? DEFAULT_API_HOST + "/api/proxy/stability"
  41. : ApiPath.Stability;
  42. const DEFAULT_IFLYTEK_URL = isApp
  43. ? DEFAULT_API_HOST + "/api/proxy/iflytek"
  44. : ApiPath.Iflytek;
  45. const DEFAULT_ACCESS_STATE = {
  46. accessCode: "",
  47. useCustomConfig: false,
  48. provider: ServiceProvider.OpenAI,
  49. // openai
  50. openaiUrl: DEFAULT_OPENAI_URL,
  51. openaiApiKey: "",
  52. // azure
  53. azureUrl: "",
  54. azureApiKey: "",
  55. azureApiVersion: "2023-08-01-preview",
  56. // google ai studio
  57. googleUrl: DEFAULT_GOOGLE_URL,
  58. googleApiKey: "",
  59. googleApiVersion: "v1",
  60. googleSafetySettings: GoogleSafetySettingsThreshold.BLOCK_ONLY_HIGH,
  61. // anthropic
  62. anthropicUrl: DEFAULT_ANTHROPIC_URL,
  63. anthropicApiKey: "",
  64. anthropicApiVersion: "2023-06-01",
  65. // baidu
  66. baiduUrl: DEFAULT_BAIDU_URL,
  67. baiduApiKey: "",
  68. baiduSecretKey: "",
  69. // bytedance
  70. bytedanceUrl: DEFAULT_BYTEDANCE_URL,
  71. bytedanceApiKey: "",
  72. // alibaba
  73. alibabaUrl: DEFAULT_ALIBABA_URL,
  74. alibabaApiKey: "",
  75. // moonshot
  76. moonshotUrl: DEFAULT_MOONSHOT_URL,
  77. moonshotApiKey: "",
  78. //stability
  79. stabilityUrl: DEFAULT_STABILITY_URL,
  80. stabilityApiKey: "",
  81. // tencent
  82. tencentUrl: DEFAULT_TENCENT_URL,
  83. tencentSecretKey: "",
  84. tencentSecretId: "",
  85. // iflytek
  86. iflytekUrl: DEFAULT_IFLYTEK_URL,
  87. iflytekApiKey: "",
  88. iflytekApiSecret: "",
  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. isAuthorized() {
  142. this.fetch();
  143. // has token or has code or disabled access control
  144. return (
  145. this.isValidOpenAI() ||
  146. this.isValidAzure() ||
  147. this.isValidGoogle() ||
  148. this.isValidAnthropic() ||
  149. this.isValidBaidu() ||
  150. this.isValidByteDance() ||
  151. this.isValidAlibaba() ||
  152. this.isValidTencent() ||
  153. this.isValidMoonshot() ||
  154. this.isValidIflytek() ||
  155. !this.enabledAccessControl() ||
  156. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  157. );
  158. },
  159. fetch() {
  160. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  161. fetchState = 1;
  162. fetch("/api/config", {
  163. method: "post",
  164. body: null,
  165. headers: {
  166. ...getHeaders(),
  167. },
  168. })
  169. .then((res) => res.json())
  170. .then((res) => {
  171. // Set default model from env request
  172. let defaultModel = res.defaultModel ?? "";
  173. DEFAULT_CONFIG.modelConfig.model =
  174. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  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. );