access.ts 4.0 KB

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