access.ts 4.6 KB

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