common.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. import type { Fetcher } from 'swr'
  2. import { del, download, get, patch, post, put, upload } 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 fetchTypes = ({ url, params }: any) => {
  91. return get(url, { params })
  92. }
  93. export const fetchKnowledges = ({ url, params }: any) => {
  94. return get(url, { params })
  95. }
  96. export const fetchMoulds = ({ url, params }: any) => {
  97. return get<{ accounts: Member[] | null }>(url, { params })
  98. }
  99. export const fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  100. return get<Provider[] | null>(url, { params })
  101. }
  102. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  103. return post<ValidateOpenAIKeyResponse>(url, { body })
  104. }
  105. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  106. return post<UpdateOpenAIKeyResponse>(url, { body })
  107. }
  108. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  109. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  110. }
  111. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  112. return post<InvitationResponse>(url, { body })
  113. }
  114. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  115. return put<CommonResponse>(url, { body })
  116. }
  117. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  118. return del<CommonResponse>(url)
  119. }
  120. export const addType = ({ url, body }: any) => {
  121. return post(url, { body })
  122. }
  123. export const editType = ({ url, body }: any) => {
  124. return patch(url, { body })
  125. }
  126. export const delType = ({ url, body }: any) => {
  127. return del(url, { body })
  128. }
  129. export const addKnowledge = ({ url, body }: any) => {
  130. return post(url, { body })
  131. }
  132. export const editKnowledge = ({ url, body }: any) => {
  133. return patch(url, { body })
  134. }
  135. export const delKnowledge = ({ url, body }: any) => {
  136. return del(url, { body })
  137. }
  138. export const addMouldFile = ({ url, body }: any) => {
  139. return upload({
  140. xhr: new XMLHttpRequest(),
  141. data: body,
  142. }, false, url)
  143. }
  144. export const delMouldFile = ({ url, body }: any) => {
  145. return del(url)
  146. }
  147. export const downloadMouldFile = ({ url, fileName }: any) => {
  148. return download(url, {}, { fileName })
  149. }
  150. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  151. return get<{ content: string }>(`/files/${fileID}/preview`)
  152. }
  153. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  154. return get<ICurrentWorkspace>(url, { params })
  155. }
  156. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  157. return post<ICurrentWorkspace>(url, { body })
  158. }
  159. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  160. return get<{ workspaces: IWorkspace[] }>(url, { params })
  161. }
  162. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  163. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  164. }
  165. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  166. return get<{ data: DataSourceNotion[] }>(url)
  167. }
  168. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  169. return get<CommonResponse>(url)
  170. }
  171. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  172. return patch<CommonResponse>(url)
  173. }
  174. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  175. return get<PluginProvider[] | null>(url)
  176. }
  177. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  178. return post<ValidateOpenAIKeyResponse>(url, { body })
  179. }
  180. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  181. return post<UpdateOpenAIKeyResponse>(url, { body })
  182. }
  183. 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 }) => {
  184. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  185. }
  186. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  187. return post<LoginResponse>(url, { body })
  188. }
  189. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  190. return get<{ data: ModelProvider[] }>(url)
  191. }
  192. export type ModelProviderCredentials = {
  193. credentials?: Record<string, string | undefined | boolean>
  194. load_balancing: ModelLoadBalancingConfig
  195. }
  196. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  197. return get<ModelProviderCredentials>(url)
  198. }
  199. export const fetchModelLoadBalancingConfig: Fetcher<{
  200. credentials?: Record<string, string | undefined | boolean>
  201. load_balancing: ModelLoadBalancingConfig
  202. }, string> = (url) => {
  203. return get<{
  204. credentials?: Record<string, string | undefined | boolean>
  205. load_balancing: ModelLoadBalancingConfig
  206. }>(url)
  207. }
  208. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  209. return get<{ data: ModelItem[] }>(url)
  210. }
  211. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  212. return get<{ data: Model[] }>(url)
  213. }
  214. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  215. return post<ValidateOpenAIKeyResponse>(url, { body })
  216. }
  217. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  218. return post<ValidateOpenAIKeyResponse>(url, { body })
  219. }
  220. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  221. return post<CommonResponse>(url, { body })
  222. }
  223. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  224. return del<CommonResponse>(url, { body })
  225. }
  226. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  227. return post<CommonResponse>(url, { body })
  228. }
  229. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  230. return post<CommonResponse>(url, { body })
  231. }
  232. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  233. return del<CommonResponse>(url)
  234. }
  235. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  236. return get<{ url: string }>(url)
  237. }
  238. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  239. return get<{ data: DefaultModelResponse }>(url)
  240. }
  241. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  242. return post<CommonResponse>(url, { body })
  243. }
  244. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  245. return get<{ data: ModelParameterRule[] }>(url)
  246. }
  247. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  248. return get<FileUploadConfigResponse>(url)
  249. }
  250. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  251. return get(url) as Promise<{ data: string }>
  252. }
  253. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  254. return get(url) as Promise<{ result: string }>
  255. }
  256. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  257. return get(url) as Promise<ApiBasedExtension[]>
  258. }
  259. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  260. return get(url) as Promise<ApiBasedExtension>
  261. }
  262. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  263. return post(url, { body }) as Promise<ApiBasedExtension>
  264. }
  265. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  266. return post(url, { body }) as Promise<ApiBasedExtension>
  267. }
  268. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  269. return del(url) as Promise<{ result: string }>
  270. }
  271. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  272. return get(url) as Promise<CodeBasedExtension>
  273. }
  274. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  275. return post(url, { body }) as Promise<ModerateResponse>
  276. }
  277. type RetrievalMethodsRes = {
  278. retrieval_method: RETRIEVE_METHOD[]
  279. }
  280. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  281. return get<RetrievalMethodsRes>(url)
  282. }
  283. export const getSystemFeatures = () => {
  284. return get<SystemFeatures>('/system-features')
  285. }
  286. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  287. patch<CommonResponse>(url, { body })
  288. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  289. patch<CommonResponse>(url, { body })
  290. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  291. post<CommonResponse & { data: string }>(url, { body })
  292. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  293. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  294. }
  295. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  296. post<CommonResponse>(url, { body })
  297. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean) => {
  298. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic })
  299. }
  300. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  301. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  302. export const emailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  303. post<LoginResponse>('/email-code-login/validity', { body: data })
  304. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  305. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } })
  306. export const verifyResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  307. post<CommonResponse & { is_valid: boolean }>('/forgot-password/validity', { body })
  308. export const sendDeleteAccountCode = () =>
  309. get<CommonResponse & { data: string }>('/account/delete/verify')
  310. export const verifyDeleteAccountCode = (body: { code: string; token: string }) =>
  311. post<CommonResponse & { is_valid: boolean }>('/account/delete', { body })
  312. export const submitDeleteAccountFeedback = (body: { feedback: string; email: string }) =>
  313. post<CommonResponse>('/account/delete/feedback', { body })
  314. export const getDocDownloadUrl = (doc_name: string) =>
  315. get<{ url: string }>('/compliance/download', { params: { doc_name } }, { silent: true })
  316. export const fetchStatistic = ({ url, params }: any) => {
  317. return get(url, { params })
  318. }
  319. export const fetchLog = ({ url, params }: any) => {
  320. return get(url, { params })
  321. }
  322. export const downloadLogFile = ({ url, fileName }: any) => {
  323. return download(url, {}, { fileName })
  324. }
  325. export const fetchIntentType = ({ url, params }: any) => {
  326. return get(url, { params })
  327. }
  328. export const addIntentType = ({ url, body }: any) => {
  329. return post(url, { body })
  330. }
  331. export const editIntentType = ({ url, body }: any) => {
  332. return patch(url, { body })
  333. }
  334. export const delIntentType = ({ url, body }: any) => {
  335. return del(url, { body })
  336. }
  337. export const fetchIntent = ({ url, params }: any) => {
  338. return get(url, { params })
  339. }
  340. export const addIntent = ({ url, body }: any) => {
  341. return post(url, { body })
  342. }
  343. export const editIntent = ({ url, body }: any) => {
  344. return patch(url, { body })
  345. }
  346. export const delIntent = ({ url, body }: any) => {
  347. return del(url, { body })
  348. }
  349. export const getIntent = ({ url, body }: any) => {
  350. return get(url, { body })
  351. }
  352. export const fetchIntentKeyword = ({ url, params }: any) => {
  353. return get(url, { params })
  354. }
  355. export const addIntentKeyword = ({ url, body }: any) => {
  356. return post(url, { body })
  357. }
  358. export const editIntentKeyword = ({ url, body }: any) => {
  359. return patch(url, { body })
  360. }
  361. export const delBatchIntentKeyword = ({ url, body }: any) => {
  362. return post(url, { body })
  363. }
  364. export const fetchCorpus = ({ url, params }: any) => {
  365. return get(url, { params })
  366. }
  367. export const addCorpus = ({ url, body }: any) => {
  368. return post(url, { body })
  369. }
  370. export const editCorpus = ({ url, body }: any) => {
  371. return patch(url, { body })
  372. }
  373. export const delCorpus = ({ url, body }: any) => {
  374. return del(url, { body })
  375. }
  376. export const getCorpus = ({ url, params }: any) => {
  377. return get(url, { params })
  378. }
  379. export const fetchCorpusQuestion = ({ url, params }: any) => {
  380. return get(url, { params })
  381. }
  382. export const addCorpusQuestion = ({ url, body }: any) => {
  383. return post(url, { body })
  384. }
  385. export const editCorpusQuestion = ({ url, body }: any) => {
  386. return patch(url, { body })
  387. }
  388. export const delBatchCorpusQuestion = ({ url, body }: any) => {
  389. return post(url, { body })
  390. }
  391. export const fetchDepts = ({ url, params }: any) => {
  392. return get(url, { params })
  393. }
  394. export const addDept = ({ url, body }: any) => {
  395. return post(url, { body })
  396. }
  397. export const editDept = ({ url, body }: any) => {
  398. return post(url, { body })
  399. }
  400. export const delDept = ({ url, body }: any) => {
  401. return del(url, { body })
  402. }
  403. export const fetchDeptUsers = ({ url, params }: any) => {
  404. return get(url, { params })
  405. }
  406. export const fetchNoDeptUsers = ({ url, params }: any) => {
  407. return get(url, { params })
  408. }
  409. export const deptAddUsers = ({ url, body }: any) => {
  410. return post(url, { body })
  411. }
  412. export const deptDelUsers = ({ url, body }: any) => {
  413. return del(url, { body })
  414. }
  415. export const fetchDeptUserTree = ({ url, params }: any) => {
  416. return get(url, { params })
  417. }
  418. export const fetchDatasetsPermission = ({ url, params }: any) => {
  419. return get(url, { params })
  420. }
  421. export const setDatasetsPermission = ({ url, body }: any) => {
  422. return post(url, { body })
  423. }
  424. export const fetchAppsPermission = ({ url, params }: any) => {
  425. return get(url, { params })
  426. }
  427. export const setAppsPermission = ({ url, body }: any) => {
  428. return post(url, { body })
  429. }