common.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 }) => {
  91. // return get<{ accounts: Member[] | null }>(url, { params })
  92. console.log('查询类型列表')
  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 }) => {
  110. // return get<{ accounts: Member[] | null }>(url, { params })
  111. console.log('查询知识服务')
  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 fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  132. return get<Provider[] | null>(url, { params })
  133. }
  134. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  135. return post<ValidateOpenAIKeyResponse>(url, { body })
  136. }
  137. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  138. return post<UpdateOpenAIKeyResponse>(url, { body })
  139. }
  140. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  141. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  142. }
  143. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  144. return post<InvitationResponse>(url, { body })
  145. }
  146. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  147. return put<CommonResponse>(url, { body })
  148. }
  149. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  150. return del<CommonResponse>(url)
  151. }
  152. export const addType = ({ url, body }) => {
  153. // return post<InvitationResponse>(url, { body })
  154. console.log('新增类型', body)
  155. return new Promise((resolve) => {
  156. setTimeout(() => {
  157. resolve({
  158. result: 'success',
  159. })
  160. }, 1000)
  161. })
  162. }
  163. export const editType = ({ url, body }) => {
  164. // return post<InvitationResponse>(url, { body })
  165. console.log('编辑类型', body)
  166. return new Promise((resolve) => {
  167. setTimeout(() => {
  168. resolve({
  169. result: 'success',
  170. })
  171. }, 1000)
  172. })
  173. }
  174. export const addKnowledge = ({ url, body }) => {
  175. // return post<InvitationResponse>(url, { body })
  176. console.log('新增知识', body)
  177. return new Promise((resolve) => {
  178. setTimeout(() => {
  179. resolve({
  180. result: 'success',
  181. })
  182. }, 1000)
  183. })
  184. }
  185. export const editKnowledge = ({ url, body }) => {
  186. // return post<InvitationResponse>(url, { body })
  187. console.log('编辑知识', body)
  188. return new Promise((resolve) => {
  189. setTimeout(() => {
  190. resolve({
  191. result: 'success',
  192. })
  193. }, 1000)
  194. })
  195. }
  196. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  197. return get<{ content: string }>(`/files/${fileID}/preview`)
  198. }
  199. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  200. return get<ICurrentWorkspace>(url, { params })
  201. }
  202. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  203. return post<ICurrentWorkspace>(url, { body })
  204. }
  205. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  206. return get<{ workspaces: IWorkspace[] }>(url, { params })
  207. }
  208. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  209. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  210. }
  211. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  212. return get<{ data: DataSourceNotion[] }>(url)
  213. }
  214. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  215. return get<CommonResponse>(url)
  216. }
  217. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  218. return patch<CommonResponse>(url)
  219. }
  220. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  221. return get<PluginProvider[] | null>(url)
  222. }
  223. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  224. return post<ValidateOpenAIKeyResponse>(url, { body })
  225. }
  226. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  227. return post<UpdateOpenAIKeyResponse>(url, { body })
  228. }
  229. 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 }) => {
  230. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  231. }
  232. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  233. return post<LoginResponse>(url, { body })
  234. }
  235. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  236. return get<{ data: ModelProvider[] }>(url)
  237. }
  238. export type ModelProviderCredentials = {
  239. credentials?: Record<string, string | undefined | boolean>
  240. load_balancing: ModelLoadBalancingConfig
  241. }
  242. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  243. return get<ModelProviderCredentials>(url)
  244. }
  245. export const fetchModelLoadBalancingConfig: Fetcher<{
  246. credentials?: Record<string, string | undefined | boolean>
  247. load_balancing: ModelLoadBalancingConfig
  248. }, string> = (url) => {
  249. return get<{
  250. credentials?: Record<string, string | undefined | boolean>
  251. load_balancing: ModelLoadBalancingConfig
  252. }>(url)
  253. }
  254. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  255. return get<{ data: ModelItem[] }>(url)
  256. }
  257. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  258. return get<{ data: Model[] }>(url)
  259. }
  260. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  261. return post<ValidateOpenAIKeyResponse>(url, { body })
  262. }
  263. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  264. return post<ValidateOpenAIKeyResponse>(url, { body })
  265. }
  266. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  267. return post<CommonResponse>(url, { body })
  268. }
  269. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  270. return del<CommonResponse>(url, { body })
  271. }
  272. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  273. return post<CommonResponse>(url, { body })
  274. }
  275. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  276. return post<CommonResponse>(url, { body })
  277. }
  278. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  279. return del<CommonResponse>(url)
  280. }
  281. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  282. return get<{ url: string }>(url)
  283. }
  284. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  285. return get<{ data: DefaultModelResponse }>(url)
  286. }
  287. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  288. return post<CommonResponse>(url, { body })
  289. }
  290. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  291. return get<{ data: ModelParameterRule[] }>(url)
  292. }
  293. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  294. return get<FileUploadConfigResponse>(url)
  295. }
  296. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  297. return get(url) as Promise<{ data: string }>
  298. }
  299. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  300. return get(url) as Promise<{ result: string }>
  301. }
  302. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  303. return get(url) as Promise<ApiBasedExtension[]>
  304. }
  305. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  306. return get(url) as Promise<ApiBasedExtension>
  307. }
  308. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  309. return post(url, { body }) as Promise<ApiBasedExtension>
  310. }
  311. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  312. return post(url, { body }) as Promise<ApiBasedExtension>
  313. }
  314. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  315. return del(url) as Promise<{ result: string }>
  316. }
  317. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  318. return get(url) as Promise<CodeBasedExtension>
  319. }
  320. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  321. return post(url, { body }) as Promise<ModerateResponse>
  322. }
  323. type RetrievalMethodsRes = {
  324. retrieval_method: RETRIEVE_METHOD[]
  325. }
  326. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  327. return get<RetrievalMethodsRes>(url)
  328. }
  329. export const getSystemFeatures = () => {
  330. return get<SystemFeatures>('/system-features')
  331. }
  332. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  333. patch<CommonResponse>(url, { body })
  334. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  335. patch<CommonResponse>(url, { body })
  336. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  337. post<CommonResponse & { data: string }>(url, { body })
  338. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  339. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  340. }
  341. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  342. post<CommonResponse>(url, { body })
  343. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean) => {
  344. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic })
  345. }
  346. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  347. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  348. export const emailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  349. post<LoginResponse>('/email-code-login/validity', { body: data })
  350. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  351. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } })
  352. export const verifyResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  353. post<CommonResponse & { is_valid: boolean }>('/forgot-password/validity', { body })
  354. export const sendDeleteAccountCode = () =>
  355. get<CommonResponse & { data: string }>('/account/delete/verify')
  356. export const verifyDeleteAccountCode = (body: { code: string; token: string }) =>
  357. post<CommonResponse & { is_valid: boolean }>('/account/delete', { body })
  358. export const submitDeleteAccountFeedback = (body: { feedback: string; email: string }) =>
  359. post<CommonResponse>('/account/delete/feedback', { body })
  360. export const getDocDownloadUrl = (doc_name: string) =>
  361. get<{ url: string }>('/compliance/download', { params: { doc_name } }, { silent: true })