access.ts 4.9 KB

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