common.ts 11 KB

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