access.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // server config
  45. needCode: true,
  46. hideUserApiKey: false,
  47. hideBalanceQuery: false,
  48. disableGPT4: false,
  49. disableFastLink: false,
  50. customModels: "",
  51. defaultModel: "",
  52. };
  53. export const useAccessStore = createPersistStore(
  54. { ...DEFAULT_ACCESS_STATE },
  55. (set, get) => ({
  56. enabledAccessControl() {
  57. this.fetch();
  58. return get().needCode;
  59. },
  60. isValidOpenAI() {
  61. return ensure(get(), ["openaiApiKey"]);
  62. },
  63. isValidAzure() {
  64. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  65. },
  66. isValidGoogle() {
  67. return ensure(get(), ["googleApiKey"]);
  68. },
  69. isValidAnthropic() {
  70. return ensure(get(), ["anthropicApiKey"]);
  71. },
  72. isValidBaidu() {
  73. return ensure(get(), ["baiduApiKey", "baiduSecretKey"]);
  74. },
  75. isAuthorized() {
  76. this.fetch();
  77. // has token or has code or disabled access control
  78. return (
  79. this.isValidOpenAI() ||
  80. this.isValidAzure() ||
  81. this.isValidGoogle() ||
  82. this.isValidAnthropic() ||
  83. this.isValidBaidu() ||
  84. !this.enabledAccessControl() ||
  85. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  86. );
  87. },
  88. fetch() {
  89. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  90. fetchState = 1;
  91. fetch("/api/config", {
  92. method: "post",
  93. body: null,
  94. headers: {
  95. ...getHeaders(),
  96. },
  97. })
  98. .then((res) => res.json())
  99. .then((res) => {
  100. // Set default model from env request
  101. let defaultModel = res.defaultModel ?? "";
  102. DEFAULT_CONFIG.modelConfig.model =
  103. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  104. return res;
  105. })
  106. .then((res: DangerConfig) => {
  107. console.log("[Config] got config from server", res);
  108. set(() => ({ ...res }));
  109. })
  110. .catch(() => {
  111. console.error("[Config] failed to fetch config");
  112. })
  113. .finally(() => {
  114. fetchState = 2;
  115. });
  116. },
  117. }),
  118. {
  119. name: StoreKey.Access,
  120. version: 2,
  121. migrate(persistedState, version) {
  122. if (version < 2) {
  123. const state = persistedState as {
  124. token: string;
  125. openaiApiKey: string;
  126. azureApiVersion: string;
  127. googleApiKey: string;
  128. };
  129. state.openaiApiKey = state.token;
  130. state.azureApiVersion = "2023-08-01-preview";
  131. }
  132. return persistedState as any;
  133. },
  134. },
  135. );