access.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_ACCESS_STATE = {
  43. accessCode: "",
  44. useCustomConfig: false,
  45. provider: ServiceProvider.OpenAI,
  46. // openai
  47. openaiUrl: DEFAULT_OPENAI_URL,
  48. openaiApiKey: "",
  49. // azure
  50. azureUrl: "",
  51. azureApiKey: "",
  52. azureApiVersion: "2023-08-01-preview",
  53. // google ai studio
  54. googleUrl: DEFAULT_GOOGLE_URL,
  55. googleApiKey: "",
  56. googleApiVersion: "v1",
  57. googleSafetySettings: GoogleSafetySettingsThreshold.BLOCK_ONLY_HIGH,
  58. // anthropic
  59. anthropicUrl: DEFAULT_ANTHROPIC_URL,
  60. anthropicApiKey: "",
  61. anthropicApiVersion: "2023-06-01",
  62. // baidu
  63. baiduUrl: DEFAULT_BAIDU_URL,
  64. baiduApiKey: "",
  65. baiduSecretKey: "",
  66. // bytedance
  67. bytedanceUrl: DEFAULT_BYTEDANCE_URL,
  68. bytedanceApiKey: "",
  69. // alibaba
  70. alibabaUrl: DEFAULT_ALIBABA_URL,
  71. alibabaApiKey: "",
  72. // moonshot
  73. moonshotUrl: DEFAULT_MOONSHOT_URL,
  74. moonshotApiKey: "",
  75. //stability
  76. stabilityUrl: DEFAULT_STABILITY_URL,
  77. stabilityApiKey: "",
  78. // tencent
  79. tencentUrl: DEFAULT_TENCENT_URL,
  80. tencentSecretKey: "",
  81. tencentSecretId: "",
  82. // server config
  83. needCode: true,
  84. hideUserApiKey: false,
  85. hideBalanceQuery: false,
  86. disableGPT4: false,
  87. disableFastLink: false,
  88. customModels: "",
  89. defaultModel: "",
  90. };
  91. export const useAccessStore = createPersistStore(
  92. { ...DEFAULT_ACCESS_STATE },
  93. (set, get) => ({
  94. enabledAccessControl() {
  95. this.fetch();
  96. return get().needCode;
  97. },
  98. isValidOpenAI() {
  99. return ensure(get(), ["openaiApiKey"]);
  100. },
  101. isValidAzure() {
  102. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  103. },
  104. isValidGoogle() {
  105. return ensure(get(), ["googleApiKey"]);
  106. },
  107. isValidAnthropic() {
  108. return ensure(get(), ["anthropicApiKey"]);
  109. },
  110. isValidBaidu() {
  111. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  112. },
  113. isValidByteDance() {
  114. return ensure(get(), ["bytedanceApiKey"]);
  115. },
  116. isValidAlibaba() {
  117. return ensure(get(), ["alibabaApiKey"]);
  118. },
  119. isValidTencent() {
  120. return ensure(get(), ["tencentSecretKey", "tencentSecretId"]);
  121. },
  122. isValidMoonshot() {
  123. return ensure(get(), ["moonshotApiKey"]);
  124. },
  125. isAuthorized() {
  126. this.fetch();
  127. // has token or has code or disabled access control
  128. return (
  129. this.isValidOpenAI() ||
  130. this.isValidAzure() ||
  131. this.isValidGoogle() ||
  132. this.isValidAnthropic() ||
  133. this.isValidBaidu() ||
  134. this.isValidByteDance() ||
  135. this.isValidAlibaba() ||
  136. this.isValidTencent ||
  137. this.isValidMoonshot() ||
  138. !this.enabledAccessControl() ||
  139. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  140. );
  141. },
  142. fetch() {
  143. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  144. fetchState = 1;
  145. fetch("/api/config", {
  146. method: "post",
  147. body: null,
  148. headers: {
  149. ...getHeaders(),
  150. },
  151. })
  152. .then((res) => res.json())
  153. .then((res) => {
  154. // Set default model from env request
  155. let defaultModel = res.defaultModel ?? "";
  156. DEFAULT_CONFIG.modelConfig.model =
  157. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  158. return res;
  159. })
  160. .then((res: DangerConfig) => {
  161. console.log("[Config] got config from server", res);
  162. set(() => ({ ...res }));
  163. })
  164. .catch(() => {
  165. console.error("[Config] failed to fetch config");
  166. })
  167. .finally(() => {
  168. fetchState = 2;
  169. });
  170. },
  171. }),
  172. {
  173. name: StoreKey.Access,
  174. version: 2,
  175. migrate(persistedState, version) {
  176. if (version < 2) {
  177. const state = persistedState as {
  178. token: string;
  179. openaiApiKey: string;
  180. azureApiVersion: string;
  181. googleApiKey: string;
  182. };
  183. state.openaiApiKey = state.token;
  184. state.azureApiVersion = "2023-08-01-preview";
  185. }
  186. return persistedState as any;
  187. },
  188. },
  189. );