common.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. DocumentsLimitResponse,
  10. FileUploadConfigResponse,
  11. ICurrentWorkspace,
  12. IWorkspace,
  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 { BackendModel, ProviderMap } from '@/app/components/header/account-setting/model-page/declarations'
  30. import type { RETRIEVE_METHOD } from '@/types/app'
  31. export const login: Fetcher<CommonResponse & { data: string }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  32. return post(url, { body }) as Promise<CommonResponse & { data: string }>
  33. }
  34. export const setup: Fetcher<CommonResponse, { body: Record<string, any> }> = ({ body }) => {
  35. return post<CommonResponse>('/setup', { body })
  36. }
  37. export const fetchSetupStatus = () => {
  38. return get<SetupStatusResponse>('/setup')
  39. }
  40. export const fetchUserProfile: Fetcher<UserProfileOriginResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  41. return get<UserProfileOriginResponse>(url, params, { needAllResponseContent: true })
  42. }
  43. export const updateUserProfile: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  44. return post<CommonResponse>(url, { body })
  45. }
  46. export const logout: Fetcher<CommonResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  47. return get<CommonResponse>(url, params)
  48. }
  49. export const fetchLanggeniusVersion: Fetcher<LangGeniusVersionResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  50. return get<LangGeniusVersionResponse>(url, { params })
  51. }
  52. export const oauth: Fetcher<OauthResponse, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  53. return get<OauthResponse>(url, { params })
  54. }
  55. export const oneMoreStep: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  56. return post<CommonResponse>(url, { body })
  57. }
  58. export const fetchMembers: Fetcher<{ accounts: Member[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  59. return get<{ accounts: Member[] | null }>(url, { params })
  60. }
  61. export const fetchProviders: Fetcher<Provider[] | null, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  62. return get<Provider[] | null>(url, { params })
  63. }
  64. export const validateProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { token: string } }> = ({ url, body }) => {
  65. return post<ValidateOpenAIKeyResponse>(url, { body })
  66. }
  67. export const updateProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { token: string | ProviderAzureToken | ProviderAnthropicToken } }> = ({ url, body }) => {
  68. return post<UpdateOpenAIKeyResponse>(url, { body })
  69. }
  70. export const fetchAccountIntegrates: Fetcher<{ data: AccountIntegrate[] | null }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  71. return get<{ data: AccountIntegrate[] | null }>(url, { params })
  72. }
  73. export const inviteMember: Fetcher<InvitationResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  74. return post<InvitationResponse>(url, { body })
  75. }
  76. export const updateMemberRole: Fetcher<CommonResponse, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  77. return put<CommonResponse>(url, { body })
  78. }
  79. export const deleteMemberOrCancelInvitation: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  80. return del<CommonResponse>(url)
  81. }
  82. export const fetchFilePreview: Fetcher<{ content: string }, { fileID: string }> = ({ fileID }) => {
  83. return get<{ content: string }>(`/files/${fileID}/preview`)
  84. }
  85. export const fetchCurrentWorkspace: Fetcher<ICurrentWorkspace, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  86. return get<ICurrentWorkspace>(url, { params })
  87. }
  88. export const fetchWorkspaces: Fetcher<{ workspaces: IWorkspace[] }, { url: string; params: Record<string, any> }> = ({ url, params }) => {
  89. return get<{ workspaces: IWorkspace[] }>(url, { params })
  90. }
  91. export const switchWorkspace: Fetcher<CommonResponse & { new_tenant: IWorkspace }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
  92. return post<CommonResponse & { new_tenant: IWorkspace }>(url, { body })
  93. }
  94. export const fetchDataSource: Fetcher<{ data: DataSourceNotion[] }, { url: string }> = ({ url }) => {
  95. return get<{ data: DataSourceNotion[] }>(url)
  96. }
  97. export const syncDataSourceNotion: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  98. return get<CommonResponse>(url)
  99. }
  100. export const updateDataSourceNotionAction: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  101. return patch<CommonResponse>(url)
  102. }
  103. export const fetchPluginProviders: Fetcher<PluginProvider[] | null, string> = (url) => {
  104. return get<PluginProvider[] | null>(url)
  105. }
  106. export const validatePluginProviderKey: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  107. return post<ValidateOpenAIKeyResponse>(url, { body })
  108. }
  109. export const updatePluginProviderAIKey: Fetcher<UpdateOpenAIKeyResponse, { url: string; body: { credentials: any } }> = ({ url, body }) => {
  110. return post<UpdateOpenAIKeyResponse>(url, { body })
  111. }
  112. export const invitationCheck: Fetcher<CommonResponse & { is_valid: boolean; workspace_name: string }, { url: string; params: { workspace_id: string; email: string; token: string } }> = ({ url, params }) => {
  113. return get<CommonResponse & { is_valid: boolean; workspace_name: string }>(url, { params })
  114. }
  115. export const activateMember: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  116. return post<CommonResponse>(url, { body })
  117. }
  118. export const fetchModelProviders: Fetcher<ProviderMap, string> = (url) => {
  119. return get<ProviderMap>(url)
  120. }
  121. export const fetchModelList: Fetcher<BackendModel[], string> = (url) => {
  122. return get<BackendModel[]>(url)
  123. }
  124. export const validateModelProvider: Fetcher<ValidateOpenAIKeyResponse, { url: string; body: any }> = ({ url, body }) => {
  125. return post<ValidateOpenAIKeyResponse>(url, { body })
  126. }
  127. export const setModelProvider: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  128. return post<CommonResponse>(url, { body })
  129. }
  130. export const deleteModelProvider: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  131. return del<CommonResponse>(url)
  132. }
  133. export const changeModelProviderPriority: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  134. return post<CommonResponse>(url, { body })
  135. }
  136. export const setModelProviderModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  137. return post<CommonResponse>(url, { body })
  138. }
  139. export const deleteModelProviderModel: Fetcher<CommonResponse, { url: string }> = ({ url }) => {
  140. return del<CommonResponse>(url)
  141. }
  142. export const getPayUrl: Fetcher<{ url: string }, string> = (url) => {
  143. return get<{ url: string }>(url)
  144. }
  145. export const fetchDefaultModal: Fetcher<BackendModel, string> = (url) => {
  146. return get<BackendModel>(url)
  147. }
  148. export const updateDefaultModel: Fetcher<CommonResponse, { url: string; body: any }> = ({ url, body }) => {
  149. return post<CommonResponse>(url, { body })
  150. }
  151. export const submitFreeQuota: Fetcher<{ type: string; redirect_url?: string; result?: string }, string> = (url) => {
  152. return post<{ type: string; redirect_url?: string; result?: string }>(url)
  153. }
  154. export const fetchFileUploadConfig: Fetcher<FileUploadConfigResponse, { url: string }> = ({ url }) => {
  155. return get<FileUploadConfigResponse>(url)
  156. }
  157. export const fetchDocumentsLimit: Fetcher<DocumentsLimitResponse, string> = (url) => {
  158. return get<DocumentsLimitResponse>(url)
  159. }
  160. export const fetchFreeQuotaVerify: Fetcher<{ result: string; flag: boolean; reason: string }, string> = (url) => {
  161. return get(url) as Promise<{ result: string; flag: boolean; reason: string }>
  162. }
  163. export const fetchNotionConnection: Fetcher<{ data: string }, string> = (url) => {
  164. return get(url) as Promise<{ data: string }>
  165. }
  166. export const fetchDataSourceNotionBinding: Fetcher<{ result: string }, string> = (url) => {
  167. return get(url) as Promise<{ result: string }>
  168. }
  169. export const fetchApiBasedExtensionList: Fetcher<ApiBasedExtension[], string> = (url) => {
  170. return get(url) as Promise<ApiBasedExtension[]>
  171. }
  172. export const fetchApiBasedExtensionDetail: Fetcher<ApiBasedExtension, string> = (url) => {
  173. return get(url) as Promise<ApiBasedExtension>
  174. }
  175. export const addApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  176. return post(url, { body }) as Promise<ApiBasedExtension>
  177. }
  178. export const updateApiBasedExtension: Fetcher<ApiBasedExtension, { url: string; body: ApiBasedExtension }> = ({ url, body }) => {
  179. return post(url, { body }) as Promise<ApiBasedExtension>
  180. }
  181. export const deleteApiBasedExtension: Fetcher<{ result: string }, string> = (url) => {
  182. return del(url) as Promise<{ result: string }>
  183. }
  184. export const fetchCodeBasedExtensionList: Fetcher<CodeBasedExtension, string> = (url) => {
  185. return get(url) as Promise<CodeBasedExtension>
  186. }
  187. export const moderate = (url: string, body: { app_id: string; text: string }) => {
  188. return post(url, { body }) as Promise<ModerateResponse>
  189. }
  190. type RetrievalMethodsRes = {
  191. 'retrieval_method': RETRIEVE_METHOD[]
  192. }
  193. export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
  194. return get<RetrievalMethodsRes>(url)
  195. }