common.ts 10 KB

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