access.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. ApiPath,
  3. DEFAULT_API_HOST,
  4. GoogleSafetySettingsThreshold,
  5. ServiceProvider,
  6. StoreKey,
  7. } from "../constant";
  8. import { getHeaders } from "../client/api";
  9. import { getClientConfig } from "../config/client";
  10. import { createPersistStore } from "../utils/store";
  11. import { ensure } from "../utils/clone";
  12. import { DEFAULT_CONFIG } from "./config";
  13. let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
  14. const DEFAULT_OPENAI_URL =
  15. getClientConfig()?.buildMode === "export"
  16. ? DEFAULT_API_HOST + "/api/proxy/openai"
  17. : ApiPath.OpenAI;
  18. const DEFAULT_ACCESS_STATE = {
  19. accessCode: "",
  20. useCustomConfig: false,
  21. provider: ServiceProvider.OpenAI,
  22. // openai
  23. openaiUrl: DEFAULT_OPENAI_URL,
  24. openaiApiKey: "",
  25. // azure
  26. azureUrl: "",
  27. azureApiKey: "",
  28. azureApiVersion: "2023-08-01-preview",
  29. // google ai studio
  30. googleUrl: "",
  31. googleApiKey: "",
  32. googleApiVersion: "v1",
  33. googleSafetySettings: GoogleSafetySettingsThreshold.BLOCK_ONLY_HIGH,
  34. // anthropic
  35. anthropicApiKey: "",
  36. anthropicApiVersion: "2023-06-01",
  37. anthropicUrl: "",
  38. // server config
  39. needCode: true,
  40. hideUserApiKey: false,
  41. hideBalanceQuery: false,
  42. disableGPT4: false,
  43. disableFastLink: false,
  44. customModels: "",
  45. defaultModel: "",
  46. };
  47. export const useAccessStore = createPersistStore(
  48. { ...DEFAULT_ACCESS_STATE },
  49. (set, get) => ({
  50. enabledAccessControl() {
  51. this.fetch();
  52. return get().needCode;
  53. },
  54. isValidOpenAI() {
  55. return ensure(get(), ["openaiApiKey"]);
  56. },
  57. isValidAzure() {
  58. return ensure(get(), ["azureUrl", "azureApiKey", "azureApiVersion"]);
  59. },
  60. isValidGoogle() {
  61. return ensure(get(), ["googleApiKey"]);
  62. },
  63. isValidAnthropic() {
  64. return ensure(get(), ["anthropicApiKey"]);
  65. },
  66. isAuthorized() {
  67. this.fetch();
  68. // has token or has code or disabled access control
  69. return (
  70. this.isValidOpenAI() ||
  71. this.isValidAzure() ||
  72. this.isValidGoogle() ||
  73. this.isValidAnthropic() ||
  74. !this.enabledAccessControl() ||
  75. (this.enabledAccessControl() && ensure(get(), ["accessCode"]))
  76. );
  77. },
  78. fetch() {
  79. if (fetchState > 0 || getClientConfig()?.buildMode === "export") return;
  80. fetchState = 1;
  81. fetch("/api/config", {
  82. method: "post",
  83. body: null,
  84. headers: {
  85. ...getHeaders(),
  86. },
  87. })
  88. .then((res) => res.json())
  89. .then((res) => {
  90. // Set default model from env request
  91. let defaultModel = res.defaultModel ?? "";
  92. DEFAULT_CONFIG.modelConfig.model =
  93. defaultModel !== "" ? defaultModel : "gpt-3.5-turbo";
  94. return res;
  95. })
  96. .then((res: DangerConfig) => {
  97. console.log("[Config] got config from server", res);
  98. set(() => ({ ...res }));
  99. })
  100. .catch(() => {
  101. console.error("[Config] failed to fetch config");
  102. })
  103. .finally(() => {
  104. fetchState = 2;
  105. });
  106. },
  107. }),
  108. {
  109. name: StoreKey.Access,
  110. version: 2,
  111. migrate(persistedState, version) {
  112. if (version < 2) {
  113. const state = persistedState as {
  114. token: string;
  115. openaiApiKey: string;
  116. azureApiVersion: string;
  117. googleApiKey: string;
  118. };
  119. state.openaiApiKey = state.token;
  120. state.azureApiVersion = "2023-08-01-preview";
  121. }
  122. return persistedState as any;
  123. },
  124. },
  125. );