common.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 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('查询知识服务')
  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 }) => {
  132. // return get<{ accounts: Member[] | null }>(url, { params })
  133. console.log('查询知识库模板')
  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. // return post<InvitationResponse>(url, { body })
  197. console.log('新增模板文件', body)
  198. return new Promise((resolve) => {
  199. setTimeout(() => {
  200. resolve({
  201. result: 'success',
  202. })
  203. }, 1000)
  204. })
  205. }
  206. export const delMouldFile = ({ 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 handleExamine = ({ 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 applyExamine = ({ url, body }: any) => {
  229. // return post<InvitationResponse>(url, { body })
  230. console.log('提交上下线审核', body)
  231. return new Promise((resolve) => {
  232. setTimeout(() => {
  233. resolve({
  234. result: 'success',
  235. })
  236. }, 1000)
  237. })
  238. }
  239. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  240. return get<{ content: string }>(`/files/${fileID}/preview`)
  241. }
  242. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  243. return get<ICurrentWorkspace>(url, { params })
  244. }
  245. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  246. return post<ICurrentWorkspace>(url, { body })
  247. }
  248. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  249. return get<{ workspaces: IWorkspace[] }>(url, { params })
  250. }
  251. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  252. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  253. }
  254. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  255. return get<{ data: DataSourceNotion[] }>(url)
  256. }
  257. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  258. return get<CommonResponse>(url)
  259. }
  260. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  261. return patch<CommonResponse>(url)
  262. }
  263. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  264. return get<PluginProvider[] | null>(url)
  265. }
  266. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  267. return post<ValidateOpenAIKeyResponse>(url, { body })
  268. }
  269. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  270. return post<UpdateOpenAIKeyResponse>(url, { body })
  271. }
  272. 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 }) => {
  273. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  274. }
  275. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  276. return post<LoginResponse>(url, { body })
  277. }
  278. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  279. return get<{ data: ModelProvider[] }>(url)
  280. }
  281. export type ModelProviderCredentials = {
  282. credentials?: Record<string, string | undefined | boolean>
  283. load_balancing: ModelLoadBalancingConfig
  284. }
  285. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  286. return get<ModelProviderCredentials>(url)
  287. }
  288. export const fetchModelLoadBalancingConfig: Fetcher<{
  289. credentials?: Record<string, string | undefined | boolean>
  290. load_balancing: ModelLoadBalancingConfig
  291. }, string> = (url) => {
  292. return get<{
  293. credentials?: Record<string, string | undefined | boolean>
  294. load_balancing: ModelLoadBalancingConfig
  295. }>(url)
  296. }
  297. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  298. return get<{ data: ModelItem[] }>(url)
  299. }
  300. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  301. return get<{ data: Model[] }>(url)
  302. }
  303. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  304. return post<ValidateOpenAIKeyResponse>(url, { body })
  305. }
  306. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  307. return post<ValidateOpenAIKeyResponse>(url, { body })
  308. }
  309. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  310. return post<CommonResponse>(url, { body })
  311. }
  312. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  313. return del<CommonResponse>(url, { body })
  314. }
  315. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  316. return post<CommonResponse>(url, { body })
  317. }
  318. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  319. return post<CommonResponse>(url, { body })
  320. }
  321. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  322. return del<CommonResponse>(url)
  323. }
  324. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  325. return get<{ url: string }>(url)
  326. }
  327. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  328. return get<{ data: DefaultModelResponse }>(url)
  329. }
  330. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  331. return post<CommonResponse>(url, { body })
  332. }
  333. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  334. return get<{ data: ModelParameterRule[] }>(url)
  335. }
  336. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  337. return get<FileUploadConfigResponse>(url)
  338. }
  339. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  340. return get(url) as Promise<{ data: string }>
  341. }
  342. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  343. return get(url) as Promise<{ result: string }>
  344. }
  345. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  346. return get(url) as Promise<ApiBasedExtension[]>
  347. }
  348. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  349. return get(url) as Promise<ApiBasedExtension>
  350. }
  351. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  352. return post(url, { body }) as Promise<ApiBasedExtension>
  353. }
  354. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  355. return post(url, { body }) as Promise<ApiBasedExtension>
  356. }
  357. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  358. return del(url) as Promise<{ result: string }>
  359. }
  360. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  361. return get(url) as Promise<CodeBasedExtension>
  362. }
  363. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  364. return post(url, { body }) as Promise<ModerateResponse>
  365. }
  366. type RetrievalMethodsRes = {
  367. retrieval_method: RETRIEVE_METHOD[]
  368. }
  369. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  370. return get<RetrievalMethodsRes>(url)
  371. }
  372. export const getSystemFeatures = () => {
  373. return get<SystemFeatures>('/system-features')
  374. }
  375. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  376. patch<CommonResponse>(url, { body })
  377. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  378. patch<CommonResponse>(url, { body })
  379. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  380. post<CommonResponse & { data: string }>(url, { body })
  381. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  382. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  383. }
  384. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  385. post<CommonResponse>(url, { body })
  386. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean) => {
  387. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic })
  388. }
  389. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  390. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  391. export const emailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  392. post<LoginResponse>('/email-code-login/validity', { body: data })
  393. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  394. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } })
  395. export const verifyResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  396. post<CommonResponse & { is_valid: boolean }>('/forgot-password/validity', { body })
  397. export const sendDeleteAccountCode = () =>
  398. get<CommonResponse & { data: string }>('/account/delete/verify')
  399. export const verifyDeleteAccountCode = (body: { code: string; token: string }) =>
  400. post<CommonResponse & { is_valid: boolean }>('/account/delete', { body })
  401. export const submitDeleteAccountFeedback = (body: { feedback: string; email: string }) =>
  402. post<CommonResponse>('/account/delete/feedback', { body })
  403. export const getDocDownloadUrl = (doc_name: string) =>
  404. get<{ url: string }>('/compliance/download', { params: { doc_name } }, { silent: true })