access.ts 3.5 KB

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