access.ts 4.7 KB

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