common.ts 10 KB

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