common.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import type { Fetcher } from 'swr'
  2. import { del, 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. console.log('查询类型列表')
  92. return get(url, { params })
  93. // return new Promise((resolve) => {
  94. // setTimeout(() => {
  95. // const arr: any = []
  96. // for (let i = 1; i < 10; i++) {
  97. // arr.push({
  98. // id: i,
  99. // name: `类型${i}`,
  100. // relation: i % 2,
  101. // })
  102. // }
  103. // resolve({
  104. // data: arr,
  105. // })
  106. // }, 1000)
  107. // })
  108. }
  109. export const fetchKnowledges = ({ url, params }: any) => {
  110. console.log('查询知识服务', params, url)
  111. return get(url, { params })
  112. // return new Promise((resolve) => {
  113. // setTimeout(() => {
  114. // const arr: any = []
  115. // for (let i = 1; i < 10; i++) {
  116. // arr.push({
  117. // id: i,
  118. // serviceType: i % 3 + 1,
  119. // serviceName: `深圳口岸服务网${i}`,
  120. // url: '74.10.28.118',
  121. // status: i % 2,
  122. // time: '2021-09-22 14:22:22',
  123. // })
  124. // }
  125. // resolve({
  126. // data: arr,
  127. // })
  128. // }, 1000)
  129. // })
  130. }
  131. export const fetchMoulds = ({ url, params }: any) => {
  132. console.log('查询知识库模板', params, url)
  133. return get<{ accounts: Member[] | null }>(url, { params })
  134. // return new Promise((resolve) => {
  135. // setTimeout(() => {
  136. // const arr: any = []
  137. // for (let i = 1; i < 2; i++) {
  138. // // /files/7be8a2ca-a633-473c-8bcf-b73ee9fd7738/file-preview?timestamp=1744947616&nonce=a4107f3e163bbb0da255914e2ccabaf3&sign=H82Wk2VbWjXXEyzRUhGyNQlR-PG8NEGy5ijCEF0IKgo=
  139. // arr.push({
  140. // id: '7be8a2ca-a633-473c-8bcf-b73ee9fd7738',
  141. // fileName: `文件${i}.doc`,
  142. // })
  143. // }
  144. // resolve({
  145. // data: arr,
  146. // })
  147. // }, 1000)
  148. // })
  149. }
  150. export const fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  151. return get<Provider[] | null>(url, { params })
  152. }
  153. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  154. return post<ValidateOpenAIKeyResponse>(url, { body })
  155. }
  156. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  157. return post<UpdateOpenAIKeyResponse>(url, { body })
  158. }
  159. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  160. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  161. }
  162. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  163. return post<InvitationResponse>(url, { body })
  164. }
  165. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  166. return put<CommonResponse>(url, { body })
  167. }
  168. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  169. return del<CommonResponse>(url)
  170. }
  171. export const addType = ({ url, body }: any) => {
  172. console.log('新增类型', url, body)
  173. return post(url, { body })
  174. }
  175. export const editType = ({ url, body }: any) => {
  176. console.log('编辑类型', url, body)
  177. return patch(url, { body })
  178. }
  179. export const delType = ({ url, body }: any) => {
  180. console.log('删除类型', url, body)
  181. return del(url, { body })
  182. }
  183. export const addKnowledge = ({ url, body }: any) => {
  184. console.log('新增知识', body, url)
  185. return post(url, { body })
  186. }
  187. export const editKnowledge = ({ url, body }: any) => {
  188. console.log('编辑知识', body, url)
  189. return patch(url, { body })
  190. }
  191. export const delKnowledge = ({ url, body }: any) => {
  192. console.log('删除知识', url, body)
  193. return del(url, { body })
  194. }
  195. export const addMouldFile = ({ url, body }: any) => {
  196. console.log('新增模板文件', body, url)
  197. return upload({
  198. xhr: new XMLHttpRequest(),
  199. data: body,
  200. }, false, url)
  201. }
  202. export const delMouldFile = ({ url, body }: any) => {
  203. console.log('删除模板文件', url)
  204. return del(url)
  205. }
  206. export const handleExamine = ({ url, body }: any) => {
  207. // return post<InvitationResponse>(url, { body })
  208. console.log('处理上下线审核', body)
  209. return new Promise((resolve) => {
  210. setTimeout(() => {
  211. resolve({
  212. result: 'success',
  213. })
  214. }, 1000)
  215. })
  216. }
  217. export const applyExamine = ({ url, body }: any) => {
  218. // return post<InvitationResponse>(url, { body })
  219. console.log('提交上下线审核', body)
  220. return new Promise((resolve) => {
  221. setTimeout(() => {
  222. resolve({
  223. result: 'success',
  224. })
  225. }, 1000)
  226. })
  227. }
  228. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  229. return get<{ content: string }>(`/files/${fileID}/preview`)
  230. }
  231. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  232. return get<ICurrentWorkspace>(url, { params })
  233. }
  234. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  235. return post<ICurrentWorkspace>(url, { body })
  236. }
  237. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  238. return get<{ workspaces: IWorkspace[] }>(url, { params })
  239. }
  240. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  241. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  242. }
  243. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  244. return get<{ data: DataSourceNotion[] }>(url)
  245. }
  246. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  247. return get<CommonResponse>(url)
  248. }
  249. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  250. return patch<CommonResponse>(url)
  251. }
  252. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  253. return get<PluginProvider[] | null>(url)
  254. }
  255. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  256. return post<ValidateOpenAIKeyResponse>(url, { body })
  257. }
  258. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  259. return post<UpdateOpenAIKeyResponse>(url, { body })
  260. }
  261. 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 }) => {
  262. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  263. }
  264. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  265. return post<LoginResponse>(url, { body })
  266. }
  267. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  268. return get<{ data: ModelProvider[] }>(url)
  269. }
  270. export type ModelProviderCredentials = {
  271. credentials?: Record<string, string | undefined | boolean>
  272. load_balancing: ModelLoadBalancingConfig
  273. }
  274. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  275. return get<ModelProviderCredentials>(url)
  276. }
  277. export const fetchModelLoadBalancingConfig: Fetcher<{
  278. credentials?: Record<string, string | undefined | boolean>
  279. load_balancing: ModelLoadBalancingConfig
  280. }, string> = (url) => {
  281. return get<{
  282. credentials?: Record<string, string | undefined | boolean>
  283. load_balancing: ModelLoadBalancingConfig
  284. }>(url)
  285. }
  286. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  287. return get<{ data: ModelItem[] }>(url)
  288. }
  289. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  290. return get<{ data: Model[] }>(url)
  291. }
  292. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  293. return post<ValidateOpenAIKeyResponse>(url, { body })
  294. }
  295. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  296. return post<ValidateOpenAIKeyResponse>(url, { body })
  297. }
  298. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  299. return post<CommonResponse>(url, { body })
  300. }
  301. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  302. return del<CommonResponse>(url, { body })
  303. }
  304. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  305. return post<CommonResponse>(url, { body })
  306. }
  307. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  308. return post<CommonResponse>(url, { body })
  309. }
  310. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  311. return del<CommonResponse>(url)
  312. }
  313. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  314. return get<{ url: string }>(url)
  315. }
  316. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  317. return get<{ data: DefaultModelResponse }>(url)
  318. }
  319. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  320. return post<CommonResponse>(url, { body })
  321. }
  322. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  323. return get<{ data: ModelParameterRule[] }>(url)
  324. }
  325. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  326. return get<FileUploadConfigResponse>(url)
  327. }
  328. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  329. return get(url) as Promise<{ data: string }>
  330. }
  331. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  332. return get(url) as Promise<{ result: string }>
  333. }
  334. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  335. return get(url) as Promise<ApiBasedExtension[]>
  336. }
  337. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  338. return get(url) as Promise<ApiBasedExtension>
  339. }
  340. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  341. return post(url, { body }) as Promise<ApiBasedExtension>
  342. }
  343. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  344. return post(url, { body }) as Promise<ApiBasedExtension>
  345. }
  346. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  347. return del(url) as Promise<{ result: string }>
  348. }
  349. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  350. return get(url) as Promise<CodeBasedExtension>
  351. }
  352. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  353. return post(url, { body }) as Promise<ModerateResponse>
  354. }
  355. type RetrievalMethodsRes = {
  356. retrieval_method: RETRIEVE_METHOD[]
  357. }
  358. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  359. return get<RetrievalMethodsRes>(url)
  360. }
  361. export const getSystemFeatures = () => {
  362. return get<SystemFeatures>('/system-features')
  363. }
  364. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  365. patch<CommonResponse>(url, { body })
  366. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  367. patch<CommonResponse>(url, { body })
  368. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  369. post<CommonResponse & { data: string }>(url, { body })
  370. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  371. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  372. }
  373. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  374. post<CommonResponse>(url, { body })
  375. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean) => {
  376. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic })
  377. }
  378. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  379. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  380. export const emailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  381. post<LoginResponse>('/email-code-login/validity', { body: data })
  382. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  383. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } })
  384. export const verifyResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  385. post<CommonResponse & { is_valid: boolean }>('/forgot-password/validity', { body })
  386. export const sendDeleteAccountCode = () =>
  387. get<CommonResponse & { data: string }>('/account/delete/verify')
  388. export const verifyDeleteAccountCode = (body: { code: string; token: string }) =>
  389. post<CommonResponse & { is_valid: boolean }>('/account/delete', { body })
  390. export const submitDeleteAccountFeedback = (body: { feedback: string; email: string }) =>
  391. post<CommonResponse>('/account/delete/feedback', { body })
  392. export const getDocDownloadUrl = (doc_name: string) =>
  393. get<{ url: string }>('/compliance/download', { params: { doc_name } }, { silent: true })