access.ts 3.6 KB

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