access.ts 5.0 KB

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