access.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {
  2. ApiPath,
  3. DEFAULT_API_HOST,
  4. ServiceProvider,
  5. StoreKey,
  6. } from "../constant";
  7. import { getHeaders } from "../client/api";
  8. import { getClientConfig } from "../config/client";
  9. import { createPersistStore } from "../utils/store";
  10. import { ensure } from "../utils/clone";
  11. import { DEFAULT_CONFIG } from "./config";
  12. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  13. const isApp = getClientConfig()?.buildMode === "export";
  14. const DEFAULT_OPENAI_URL = isApp
  15. ? DEFAULT_API_HOST + "/api/proxy/openai"
  16. : ApiPath.OpenAI;
  17. const DEFAULT_GOOGLE_URL = isApp
  18. ? DEFAULT_API_HOST + "/api/proxy/google"
  19. : ApiPath.Google;
  20. const DEFAULT_ANTHROPIC_URL = isApp
  21. ? DEFAULT_API_HOST + "/api/proxy/anthropic"
  22. : ApiPath.Anthropic;
  23. const DEFAULT_BAIDU_URL = isApp
  24. ? DEFAULT_API_HOST + "/api/proxy/baidu"
  25. : ApiPath.Baidu;
  26. const DEFAULT_BYTEDANCE_URL = isApp
  27. ? DEFAULT_API_HOST + "/api/proxy/bytedance"
  28. : ApiPath.ByteDance;
  29. const DEFAULT_ALIBABA_URL = isApp
  30. ? DEFAULT_API_HOST + "/api/proxy/alibaba"
  31. : ApiPath.Alibaba;
  32. console.log("DEFAULT_ANTHROPIC_URL", DEFAULT_ANTHROPIC_URL);
  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. // anthropic
  49. anthropicUrl: DEFAULT_ANTHROPIC_URL,
  50. anthropicApiKey: "",
  51. anthropicApiVersion: "2023-06-01",
  52. // baidu
  53. baiduUrl: DEFAULT_BAIDU_URL,
  54. baiduApiKey: "",
  55. baiduSecretKey: "",
  56. // bytedance
  57. bytedanceUrl: DEFAULT_BYTEDANCE_URL,
  58. bytedanceApiKey: "",
  59. // alibaba
  60. alibabaUrl: DEFAULT_ALIBABA_URL,
  61. alibabaApiKey: "",
  62. // server config
  63. needCode: true,
  64. hideUserApiKey: false,
  65. hideBalanceQuery: false,
  66. disableGPT4: false,
  67. disableFastLink: false,
  68. customModels: "",
  69. defaultModel: "",
  70. };
  71. export const useAccessStore = createPersistStore(
  72. { ...DEFAULT_ACCESS_STATE },
  73. (set, get) => ({
  74. enabledAccessControl() {
  75. this.fetch();
  76. return get().needCode;
  77. },
  78. isValidOpenAI() {
  79. return ensure(get(), ["openaiApiKey"]);
  80. },
  81. isValidAzure() {
  82. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  83. },
  84. isValidGoogle() {
  85. return ensure(get(), ["googleApiKey"]);
  86. },
  87. isValidAnthropic() {
  88. return ensure(get(), ["anthropicApiKey"]);
  89. },
  90. isValidBaidu() {
  91. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  92. },
  93. isValidByteDance() {
  94. return ensure(get(), ["bytedanceApiKey"]);
  95. },
  96. isValidAlibaba() {
  97. return ensure(get(), ["alibabaApiKey"]);
  98. },
  99. isAuthorized() {
  100. this.fetch();
  101. // has token or has code or disabled access control
  102. return (
  103. this.isValidOpenAI() ||
  104. this.isValidAzure() ||
  105. this.isValidGoogle() ||
  106. this.isValidAnthropic() ||
  107. this.isValidBaidu() ||
  108. this.isValidByteDance() ||
  109. this.isValidAlibaba() ||
  110. !this.enabledAccessControl() ||
  111. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  112. );
  113. },
  114. fetch() {
  115. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  116. fetchState = 1;
  117. fetch("/api/config", {
  118. method: "post",
  119. body: null,
  120. headers: {
  121. ...getHeaders(),
  122. },
  123. })
  124. .then((res) => res.json())
  125. .then((res) => {
  126. // Set default model from env request
  127. let defaultModel = res.defaultModel ?? "";
  128. DEFAULT_CONFIG.modelConfig.model =
  129. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  130. return res;
  131. })
  132. .then((res: DangerConfig) => {
  133. console.log("[Config] got config from server", res);
  134. set(() => ({ ...res }));
  135. })
  136. .catch(() => {
  137. console.error("[Config] failed to fetch config");
  138. })
  139. .finally(() => {
  140. fetchState = 2;
  141. });
  142. },
  143. }),
  144. {
  145. name: StoreKey.Access,
  146. version: 2,
  147. migrate(persistedState, version) {
  148. if (version < 2) {
  149. const state = persistedState as {
  150. token: string;
  151. openaiApiKey: string;
  152. azureApiVersion: string;
  153. googleApiKey: string;
  154. };
  155. state.openaiApiKey = state.token;
  156. state.azureApiVersion = "2023-08-01-preview";
  157. }
  158. return persistedState as any;
  159. },
  160. },
  161. );