common.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import type { Fetcher } from 'swr'
  2. import { del, get, patch, post, put } from './base'
  3. import type {
  4. AccountIntegrate,
  5. ApiBasedExtension,
  6. CodeBasedExtension,
  7. CommonResponse,
  8. DataSourceNotion,
  9. FileUploadConfigResponse,
  10. ICurrentWorkspace,
  11. IWorkspace,
  12. InitValidateStatusResponse,
  13. InvitationResponse,
  14. LangGeniusVersionResponse,
  15. Member,
  16. ModerateResponse,
  17. OauthResponse,
  18. PluginProvider,
  19. Provider,
  20. ProviderAnthropicToken,
  21. ProviderAzureToken,
  22. SetupStatusResponse,
  23. UserProfileOriginResponse,
  24. } from '@/models/common'
  25. import type {
  26. UpdateOpenAIKeyResponse,
  27. ValidateOpenAIKeyResponse,
  28. } from '@/models/app'
  29. import type {
  30. DefaultModelResponse,
  31. Model,
  32. ModelItem,
  33. ModelLoadBalancingConfig,
  34. ModelParameterRule,
  35. ModelProvider,
  36. ModelTypeEnum,
  37. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  38. import type { RETRIEVE_METHOD } from '@/types/app'
  39. import type { SystemFeatures } from '@/types/feature'
  40. type LoginSuccess = {
  41. result: 'success'
  42. data: { access_token: string;refresh_token: string }
  43. }
  44. type LoginFail = {
  45. result: 'fail'
  46. data: string
  47. code: string
  48. message: string
  49. }
  50. type LoginResponse = LoginSuccess | LoginFail
  51. export const login: Fetcher<LoginResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  52. return post(url, { body }) as Promise<LoginResponse>
  53. }
  54. export const fetchNewToken: Fetcher<CommonResponse & { data: { access_token: string; refresh_token: string } }, { body: Record<string, any> }> = ({ body }) => {
  55. return post('/refresh-token', { body }) as Promise<CommonResponse & { data: { access_token: string; refresh_token: string } }>
  56. }
  57. export const setup: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {
  58. return post<CommonResponse>('/setup', { body })
  59. }
  60. export const initValidate: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {
  61. return post<CommonResponse>('/init', { body })
  62. }
  63. export const fetchInitValidateStatus = () => {
  64. return get<InitValidateStatusResponse>('/init')
  65. }
  66. export const fetchSetupStatus = () => {
  67. return get<SetupStatusResponse>('/setup')
  68. }
  69. export const fetchUserProfile: Fetcher<UserProfileOriginResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  70. return get<UserProfileOriginResponse>(url, params, { needAllResponseContent: true })
  71. }
  72. export const updateUserProfile: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  73. return post<CommonResponse>(url, { body })
  74. }
  75. export const logout: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  76. return get<CommonResponse>(url, params)
  77. }
  78. export const fetchLanggeniusVersion: Fetcher<LangGeniusVersionResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  79. return get<LangGeniusVersionResponse>(url, { params })
  80. }
  81. export const oauth: Fetcher<OauthResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  82. return get<OauthResponse>(url, { params })
  83. }
  84. export const oneMoreStep: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  85. return post<CommonResponse>(url, { body })
  86. }
  87. export const fetchMembers: Fetcher<{ accounts: Member[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  88. return get<{ accounts: Member[] | null }>(url, { params })
  89. }
  90. export const fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  91. return get<Provider[] | null>(url, { params })
  92. }
  93. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  94. return post<ValidateOpenAIKeyResponse>(url, { body })
  95. }
  96. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  97. return post<UpdateOpenAIKeyResponse>(url, { body })
  98. }
  99. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  100. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  101. }
  102. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  103. return post<InvitationResponse>(url, { body })
  104. }
  105. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  106. return put<CommonResponse>(url, { body })
  107. }
  108. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  109. return del<CommonResponse>(url)
  110. }
  111. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  112. return get<{ content: string }>(`/files/${fileID}/preview`)
  113. }
  114. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  115. return get<ICurrentWorkspace>(url, { params })
  116. }
  117. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  118. return post<ICurrentWorkspace>(url, { body })
  119. }
  120. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  121. return get<{ workspaces: IWorkspace[] }>(url, { params })
  122. }
  123. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  124. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  125. }
  126. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  127. return get<{ data: DataSourceNotion[] }>(url)
  128. }
  129. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  130. return get<CommonResponse>(url)
  131. }
  132. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  133. return patch<CommonResponse>(url)
  134. }
  135. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  136. return get<PluginProvider[] | null>(url)
  137. }
  138. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  139. return post<ValidateOpenAIKeyResponse>(url, { body })
  140. }
  141. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  142. return post<UpdateOpenAIKeyResponse>(url, { body })
  143. }
  144. export const invitationCheck: Fetcher<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }, { url: string; params: { workspace_id?: string; email?: string; token: string } }> = ({ url, params }) => {
  145. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  146. }
  147. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  148. return post<LoginResponse>(url, { body })
  149. }
  150. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  151. return get<{ data: ModelProvider[] }>(url)
  152. }
  153. export type ModelProviderCredentials = {
  154. credentials?: Record<string, string | undefined | boolean>
  155. load_balancing: ModelLoadBalancingConfig
  156. }
  157. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  158. return get<ModelProviderCredentials>(url)
  159. }
  160. export const fetchModelLoadBalancingConfig: Fetcher<{
  161. credentials?: Record<string, string | undefined | boolean>
  162. load_balancing: ModelLoadBalancingConfig
  163. }, string> = (url) => {
  164. return get<{
  165. credentials?: Record<string, string | undefined | boolean>
  166. load_balancing: ModelLoadBalancingConfig
  167. }>(url)
  168. }
  169. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  170. return get<{ data: ModelItem[] }>(url)
  171. }
  172. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  173. return get<{ data: Model[] }>(url)
  174. }
  175. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  176. return post<ValidateOpenAIKeyResponse>(url, { body })
  177. }
  178. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  179. return post<ValidateOpenAIKeyResponse>(url, { body })
  180. }
  181. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  182. return post<CommonResponse>(url, { body })
  183. }
  184. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  185. return del<CommonResponse>(url, { body })
  186. }
  187. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  188. return post<CommonResponse>(url, { body })
  189. }
  190. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  191. return post<CommonResponse>(url, { body })
  192. }
  193. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  194. return del<CommonResponse>(url)
  195. }
  196. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  197. return get<{ url: string }>(url)
  198. }
  199. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  200. return get<{ data: DefaultModelResponse }>(url)
  201. }
  202. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  203. return post<CommonResponse>(url, { body })
  204. }
  205. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  206. return get<{ data: ModelParameterRule[] }>(url)
  207. }
  208. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  209. return get<FileUploadConfigResponse>(url)
  210. }
  211. export const fetchFreeQuotaVerify: Fetcher<{ result: string; flag: boolean; reason: string }, string> = (url) => {
  212. return get(url) as Promise<{ result: string; flag: boolean; reason: string }>
  213. }
  214. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  215. return get(url) as Promise<{ data: string }>
  216. }
  217. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  218. return get(url) as Promise<{ result: string }>
  219. }
  220. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  221. return get(url) as Promise<ApiBasedExtension[]>
  222. }
  223. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  224. return get(url) as Promise<ApiBasedExtension>
  225. }
  226. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  227. return post(url, { body }) as Promise<ApiBasedExtension>
  228. }
  229. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  230. return post(url, { body }) as Promise<ApiBasedExtension>
  231. }
  232. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  233. return del(url) as Promise<{ result: string }>
  234. }
  235. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  236. return get(url) as Promise<CodeBasedExtension>
  237. }
  238. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  239. return post(url, { body }) as Promise<ModerateResponse>
  240. }
  241. type RetrievalMethodsRes = {
  242. 'retrieval_method': RETRIEVE_METHOD[]
  243. }
  244. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  245. return get<RetrievalMethodsRes>(url)
  246. }
  247. export const getSystemFeatures = () => {
  248. return get<SystemFeatures>('/system-features')
  249. }
  250. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  251. patch<CommonResponse>(url, { body })
  252. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  253. patch<CommonResponse>(url, { body })
  254. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  255. post<CommonResponse & { data: string }>(url, { body })
  256. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  257. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  258. }
  259. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  260. post<CommonResponse>(url, { body })
  261. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean) => {
  262. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic })
  263. }
  264. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  265. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  266. export const emailLoginWithCode = (data: { email: string;code: string;token: string }) =>
  267. post<LoginResponse>('/email-code-login/validity', { body: data })
  268. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  269. post<CommonResponse & { data: string;message?: string ;code?: string }>('/forgot-password', { body: { email, language } })
  270. export const verifyResetPasswordCode = (body: { email: string;code: string;token: string }) =>
  271. post<CommonResponse & { is_valid: boolean }>('/forgot-password/validity', { body })