You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.ts 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 webAppLogin: Fetcher<LoginResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  55. return post(url, { body }, { isPublicAPI: true }) as Promise<LoginResponse>
  56. }
  57. export const fetchNewToken: Fetcher<CommonResponse & { data: { access_token: string; refresh_token: string } }, { body: Record<string, any> }> = ({ body }) => {
  58. return post('/refresh-token', { body }) as Promise<CommonResponse & { data: { access_token: string; refresh_token: string } }>
  59. }
  60. export const setup: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {
  61. return post<CommonResponse>('/setup', { body })
  62. }
  63. export const initValidate: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {
  64. return post<CommonResponse>('/init', { body })
  65. }
  66. export const fetchInitValidateStatus = () => {
  67. return get<InitValidateStatusResponse>('/init')
  68. }
  69. export const fetchSetupStatus = () => {
  70. return get<SetupStatusResponse>('/setup')
  71. }
  72. export const fetchUserProfile: Fetcher<UserProfileOriginResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  73. return get<UserProfileOriginResponse>(url, params, { needAllResponseContent: true })
  74. }
  75. export const updateUserProfile: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  76. return post<CommonResponse>(url, { body })
  77. }
  78. export const logout: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  79. return get<CommonResponse>(url, params)
  80. }
  81. export const fetchLangGeniusVersion: Fetcher<LangGeniusVersionResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  82. return get<LangGeniusVersionResponse>(url, { params })
  83. }
  84. export const oauth: Fetcher<OauthResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  85. return get<OauthResponse>(url, { params })
  86. }
  87. export const oneMoreStep: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  88. return post<CommonResponse>(url, { body })
  89. }
  90. export const fetchMembers: Fetcher<{ accounts: Member[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  91. return get<{ accounts: Member[] | null }>(url, { params })
  92. }
  93. export const fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  94. return get<Provider[] | null>(url, { params })
  95. }
  96. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  97. return post<ValidateOpenAIKeyResponse>(url, { body })
  98. }
  99. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  100. return post<UpdateOpenAIKeyResponse>(url, { body })
  101. }
  102. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  103. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  104. }
  105. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  106. return post<InvitationResponse>(url, { body })
  107. }
  108. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  109. return put<CommonResponse>(url, { body })
  110. }
  111. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  112. return del<CommonResponse>(url)
  113. }
  114. export const sendOwnerEmail = (body: { language?: string }) =>
  115. post<CommonResponse & { data: string }>('/workspaces/current/members/send-owner-transfer-confirm-email', { body })
  116. export const verifyOwnerEmail = (body: { code: string; token: string }) =>
  117. post<CommonResponse & { is_valid: boolean; email: string; token: string }>('/workspaces/current/members/owner-transfer-check', { body })
  118. export const ownershipTransfer = (memberID: string, body: { token: string }) =>
  119. post<CommonResponse & { is_valid: boolean; email: string; token: string }>(`/workspaces/current/members/${memberID}/owner-transfer`, { body })
  120. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  121. return get<{ content: string }>(`/files/${fileID}/preview`)
  122. }
  123. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  124. return get<ICurrentWorkspace>(url, { params })
  125. }
  126. export const updateCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  127. return post<ICurrentWorkspace>(url, { body })
  128. }
  129. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  130. return get<{ workspaces: IWorkspace[] }>(url, { params })
  131. }
  132. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  133. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  134. }
  135. export const updateWorkspaceInfo: Fetcher<ICurrentWorkspace, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  136. return post<ICurrentWorkspace>(url, { body })
  137. }
  138. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  139. return get<{ data: DataSourceNotion[] }>(url)
  140. }
  141. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  142. return get<CommonResponse>(url)
  143. }
  144. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  145. return patch<CommonResponse>(url)
  146. }
  147. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  148. return get<PluginProvider[] | null>(url)
  149. }
  150. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  151. return post<ValidateOpenAIKeyResponse>(url, { body })
  152. }
  153. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  154. return post<UpdateOpenAIKeyResponse>(url, { body })
  155. }
  156. 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 }) => {
  157. return get<CommonResponse & { is_valid: boolean; data: { workspace_name: string; email: string; workspace_id: string } }>(url, { params })
  158. }
  159. export const activateMember: Fetcher<LoginResponse, { url: string; body: any }> = ({ url, body }) => {
  160. return post<LoginResponse>(url, { body })
  161. }
  162. export const fetchModelProviders: Fetcher<{ data: ModelProvider[] }, string> = (url) => {
  163. return get<{ data: ModelProvider[] }>(url)
  164. }
  165. export type ModelProviderCredentials = {
  166. credentials?: Record<string, string | undefined | boolean>
  167. load_balancing: ModelLoadBalancingConfig
  168. }
  169. export const fetchModelProviderCredentials: Fetcher<ModelProviderCredentials, string> = (url) => {
  170. return get<ModelProviderCredentials>(url)
  171. }
  172. export const fetchModelLoadBalancingConfig: Fetcher<{
  173. credentials?: Record<string, string | undefined | boolean>
  174. load_balancing: ModelLoadBalancingConfig
  175. }, string> = (url) => {
  176. return get<{
  177. credentials?: Record<string, string | undefined | boolean>
  178. load_balancing: ModelLoadBalancingConfig
  179. }>(url)
  180. }
  181. export const fetchModelProviderModelList: Fetcher<{ data: ModelItem[] }, string> = (url) => {
  182. return get<{ data: ModelItem[] }>(url)
  183. }
  184. export const fetchModelList: Fetcher<{ data: Model[] }, string> = (url) => {
  185. return get<{ data: Model[] }>(url)
  186. }
  187. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  188. return post<ValidateOpenAIKeyResponse>(url, { body })
  189. }
  190. export const validateModelLoadBalancingCredentials: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  191. return post<ValidateOpenAIKeyResponse>(url, { body })
  192. }
  193. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  194. return post<CommonResponse>(url, { body })
  195. }
  196. export const deleteModelProvider: Fetcher<CommonResponse, { url: string; body?: any }> = ({ url, body }) => {
  197. return del<CommonResponse>(url, { body })
  198. }
  199. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  200. return post<CommonResponse>(url, { body })
  201. }
  202. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  203. return post<CommonResponse>(url, { body })
  204. }
  205. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  206. return del<CommonResponse>(url)
  207. }
  208. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  209. return get<{ url: string }>(url)
  210. }
  211. export const fetchDefaultModal: Fetcher<{ data: DefaultModelResponse }, string> = (url) => {
  212. return get<{ data: DefaultModelResponse }>(url)
  213. }
  214. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  215. return post<CommonResponse>(url, { body })
  216. }
  217. export const fetchModelParameterRules: Fetcher<{ data: ModelParameterRule[] }, string> = (url) => {
  218. return get<{ data: ModelParameterRule[] }>(url)
  219. }
  220. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  221. return get<FileUploadConfigResponse>(url)
  222. }
  223. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  224. return get(url) as Promise<{ data: string }>
  225. }
  226. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  227. return get(url) as Promise<{ result: string }>
  228. }
  229. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  230. return get(url) as Promise<ApiBasedExtension[]>
  231. }
  232. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  233. return get(url) as Promise<ApiBasedExtension>
  234. }
  235. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  236. return post(url, { body }) as Promise<ApiBasedExtension>
  237. }
  238. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  239. return post(url, { body }) as Promise<ApiBasedExtension>
  240. }
  241. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  242. return del(url) as Promise<{ result: string }>
  243. }
  244. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  245. return get(url) as Promise<CodeBasedExtension>
  246. }
  247. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  248. return post(url, { body }) as Promise<ModerateResponse>
  249. }
  250. type RetrievalMethodsRes = {
  251. retrieval_method: RETRIEVE_METHOD[]
  252. }
  253. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  254. return get<RetrievalMethodsRes>(url)
  255. }
  256. export const getSystemFeatures = () => {
  257. return get<SystemFeatures>('/system-features')
  258. }
  259. export const enableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  260. patch<CommonResponse>(url, { body })
  261. export const disableModel = (url: string, body: { model: string; model_type: ModelTypeEnum }) =>
  262. patch<CommonResponse>(url, { body })
  263. export const sendForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  264. post<CommonResponse & { data: string }>(url, { body })
  265. export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  266. return post(url, { body }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  267. }
  268. export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  269. post<CommonResponse>(url, { body })
  270. export const sendWebAppForgotPasswordEmail: Fetcher<CommonResponse & { data: string }, { url: string; body: { email: string } }> = ({ url, body }) =>
  271. post<CommonResponse & { data: string }>(url, { body }, { isPublicAPI: true })
  272. export const verifyWebAppForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boolean; email: string }, { url: string; body: { token: string } }> = ({ url, body }) => {
  273. return post(url, { body }, { isPublicAPI: true }) as Promise<CommonResponse & { is_valid: boolean; email: string }>
  274. }
  275. export const changeWebAppPasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
  276. post<CommonResponse>(url, { body }, { isPublicAPI: true })
  277. export const uploadRemoteFileInfo = (url: string, isPublic?: boolean, silent?: boolean) => {
  278. return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } }, { isPublicAPI: isPublic, silent })
  279. }
  280. export const sendEMailLoginCode = (email: string, language = 'en-US') =>
  281. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })
  282. export const emailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  283. post<LoginResponse>('/email-code-login/validity', { body: data })
  284. export const sendResetPasswordCode = (email: string, language = 'en-US') =>
  285. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } })
  286. export const verifyResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  287. post<CommonResponse & { is_valid: boolean; token: string }>('/forgot-password/validity', { body })
  288. export const sendWebAppEMailLoginCode = (email: string, language = 'en-US') =>
  289. post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } }, { isPublicAPI: true })
  290. export const webAppEmailLoginWithCode = (data: { email: string; code: string; token: string }) =>
  291. post<LoginResponse>('/email-code-login/validity', { body: data }, { isPublicAPI: true })
  292. export const sendWebAppResetPasswordCode = (email: string, language = 'en-US') =>
  293. post<CommonResponse & { data: string; message?: string; code?: string }>('/forgot-password', { body: { email, language } }, { isPublicAPI: true })
  294. export const verifyWebAppResetPasswordCode = (body: { email: string; code: string; token: string }) =>
  295. post<CommonResponse & { is_valid: boolean; token: string }>('/forgot-password/validity', { body }, { isPublicAPI: true })
  296. export const sendDeleteAccountCode = () =>
  297. get<CommonResponse & { data: string }>('/account/delete/verify')
  298. export const verifyDeleteAccountCode = (body: { code: string; token: string }) =>
  299. post<CommonResponse & { is_valid: boolean }>('/account/delete', { body })
  300. export const submitDeleteAccountFeedback = (body: { feedback: string; email: string }) =>
  301. post<CommonResponse>('/account/delete/feedback', { body })
  302. export const getDocDownloadUrl = (doc_name: string) =>
  303. get<{ url: string }>('/compliance/download', { params: { doc_name } }, { silent: true })
  304. export const sendVerifyCode = (body: { email: string; phase: string; token?: string }) =>
  305. post<CommonResponse & { data: string }>('/account/change-email', { body })
  306. export const verifyEmail = (body: { email: string; code: string; token: string }) =>
  307. post<CommonResponse & { is_valid: boolean; email: string; token: string }>('/account/change-email/validity', { body })
  308. export const resetEmail = (body: { new_email: string; token: string }) =>
  309. post<CommonResponse>('/account/change-email/reset', { body })
  310. export const checkEmailExisted = (body: { email: string }) =>
  311. post<CommonResponse>('/account/change-email/check-email-unique', { body }, { silent: true })