common.ts 12 KB

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