access.ts 4.4 KB

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