access.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_MOONSHOT_URL = isApp
  34. ? DEFAULT_API_HOST + "/api/proxy/moonshot"
  35. : ApiPath.Moonshot;
  36. const DEFAULT_STABILITY_URL = isApp
  37. ? DEFAULT_API_HOST + "/api/proxy/stability"
  38. : ApiPath.Stability;
  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. // server config
  76. needCode: true,
  77. hideUserApiKey: false,
  78. hideBalanceQuery: false,
  79. disableGPT4: false,
  80. disableFastLink: false,
  81. customModels: "",
  82. defaultModel: "",
  83. };
  84. export const useAccessStore = createPersistStore(
  85. { ...DEFAULT_ACCESS_STATE },
  86. (set, get) => ({
  87. enabledAccessControl() {
  88. this.fetch();
  89. return get().needCode;
  90. },
  91. isValidOpenAI() {
  92. return ensure(get(), ["openaiApiKey"]);
  93. },
  94. isValidAzure() {
  95. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  96. },
  97. isValidGoogle() {
  98. return ensure(get(), ["googleApiKey"]);
  99. },
  100. isValidAnthropic() {
  101. return ensure(get(), ["anthropicApiKey"]);
  102. },
  103. isValidBaidu() {
  104. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  105. },
  106. isValidByteDance() {
  107. return ensure(get(), ["bytedanceApiKey"]);
  108. },
  109. isValidAlibaba() {
  110. return ensure(get(), ["alibabaApiKey"]);
  111. },
  112. isValidMoonshot() {
  113. return ensure(get(), ["moonshotApiKey"]);
  114. },
  115. isAuthorized() {
  116. this.fetch();
  117. // has token or has code or disabled access control
  118. return (
  119. this.isValidOpenAI() ||
  120. this.isValidAzure() ||
  121. this.isValidGoogle() ||
  122. this.isValidAnthropic() ||
  123. this.isValidBaidu() ||
  124. this.isValidByteDance() ||
  125. this.isValidAlibaba() ||
  126. this.isValidMoonshot() ||
  127. !this.enabledAccessControl() ||
  128. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  129. );
  130. },
  131. fetch() {
  132. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  133. fetchState = 1;
  134. fetch("/api/config", {
  135. method: "post",
  136. body: null,
  137. headers: {
  138. ...getHeaders(),
  139. },
  140. })
  141. .then((res) => res.json())
  142. .then((res) => {
  143. // Set default model from env request
  144. let defaultModel = res.defaultModel ?? "";
  145. DEFAULT_CONFIG.modelConfig.model =
  146. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  147. return res;
  148. })
  149. .then((res: DangerConfig) => {
  150. console.log("[Config] got config from server", res);
  151. set(() => ({ ...res }));
  152. })
  153. .catch(() => {
  154. console.error("[Config] failed to fetch config");
  155. })
  156. .finally(() => {
  157. fetchState = 2;
  158. });
  159. },
  160. }),
  161. {
  162. name: StoreKey.Access,
  163. version: 2,
  164. migrate(persistedState, version) {
  165. if (version < 2) {
  166. const state = persistedState as {
  167. token: string;
  168. openaiApiKey: string;
  169. azureApiVersion: string;
  170. googleApiKey: string;
  171. };
  172. state.openaiApiKey = state.token;
  173. state.azureApiVersion = "2023-08-01-preview";
  174. }
  175. return persistedState as any;
  176. },
  177. },
  178. );