access.ts 4.6 KB

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