You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-plugins.ts 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import { useCallback, useEffect } from 'react'
  2. import type {
  3. ModelProvider,
  4. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  5. import { fetchModelProviderModelList } from '@/service/common'
  6. import { fetchPluginInfoFromMarketPlace } from '@/service/plugins'
  7. import type {
  8. DebugInfo as DebugInfoTypes,
  9. Dependency,
  10. GitHubItemAndMarketPlaceDependency,
  11. InstallPackageResponse,
  12. InstalledLatestVersionResponse,
  13. InstalledPluginListResponse,
  14. PackageDependency,
  15. Permissions,
  16. Plugin,
  17. PluginDeclaration,
  18. PluginDetail,
  19. PluginInfoFromMarketPlace,
  20. PluginTask,
  21. PluginType,
  22. PluginsFromMarketplaceByInfoResponse,
  23. PluginsFromMarketplaceResponse,
  24. VersionInfo,
  25. VersionListResponse,
  26. uploadGitHubResponse,
  27. } from '@/app/components/plugins/types'
  28. import { TaskStatus } from '@/app/components/plugins/types'
  29. import { PluginType as PluginTypeEnum } from '@/app/components/plugins/types'
  30. import type {
  31. PluginsSearchParams,
  32. } from '@/app/components/plugins/marketplace/types'
  33. import { get, getMarketplace, post, postMarketplace } from './base'
  34. import type { MutateOptions, QueryOptions } from '@tanstack/react-query'
  35. import {
  36. useMutation,
  37. useQuery,
  38. useQueryClient,
  39. } from '@tanstack/react-query'
  40. import { useInvalidateAllBuiltInTools } from './use-tools'
  41. import usePermission from '@/app/components/plugins/plugin-page/use-permission'
  42. import { uninstallPlugin } from '@/service/plugins'
  43. import useRefreshPluginList from '@/app/components/plugins/install-plugin/hooks/use-refresh-plugin-list'
  44. import { cloneDeep } from 'lodash-es'
  45. const NAME_SPACE = 'plugins'
  46. const useInstalledPluginListKey = [NAME_SPACE, 'installedPluginList']
  47. export const useCheckInstalled = ({
  48. pluginIds,
  49. enabled,
  50. }: {
  51. pluginIds: string[],
  52. enabled: boolean
  53. }) => {
  54. return useQuery<{ plugins: PluginDetail[] }>({
  55. queryKey: [NAME_SPACE, 'checkInstalled', pluginIds],
  56. queryFn: () => post<{ plugins: PluginDetail[] }>('/workspaces/current/plugin/list/installations/ids', {
  57. body: {
  58. plugin_ids: pluginIds,
  59. },
  60. }),
  61. enabled,
  62. staleTime: 0, // always fresh
  63. })
  64. }
  65. export const useInstalledPluginList = (disable?: boolean) => {
  66. return useQuery<InstalledPluginListResponse>({
  67. queryKey: useInstalledPluginListKey,
  68. queryFn: () => get<InstalledPluginListResponse>('/workspaces/current/plugin/list'),
  69. enabled: !disable,
  70. initialData: !disable ? undefined : { plugins: [] },
  71. })
  72. }
  73. export const useInstalledLatestVersion = (pluginIds: string[]) => {
  74. return useQuery<InstalledLatestVersionResponse>({
  75. queryKey: [NAME_SPACE, 'installedLatestVersion', pluginIds],
  76. queryFn: () => post<InstalledLatestVersionResponse>('/workspaces/current/plugin/list/latest-versions', {
  77. body: {
  78. plugin_ids: pluginIds,
  79. },
  80. }),
  81. enabled: !!pluginIds.length,
  82. initialData: pluginIds.length ? undefined : { versions: {} },
  83. })
  84. }
  85. export const useInvalidateInstalledPluginList = () => {
  86. const queryClient = useQueryClient()
  87. const invalidateAllBuiltInTools = useInvalidateAllBuiltInTools()
  88. return () => {
  89. queryClient.invalidateQueries(
  90. {
  91. queryKey: useInstalledPluginListKey,
  92. })
  93. invalidateAllBuiltInTools()
  94. }
  95. }
  96. export const useInstallPackageFromMarketPlace = (options?: MutateOptions<InstallPackageResponse, Error, string>) => {
  97. return useMutation({
  98. ...options,
  99. mutationFn: (uniqueIdentifier: string) => {
  100. return post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', { body: { plugin_unique_identifiers: [uniqueIdentifier] } })
  101. },
  102. })
  103. }
  104. export const useUpdatePackageFromMarketPlace = (options?: MutateOptions<InstallPackageResponse, Error, object>) => {
  105. return useMutation({
  106. ...options,
  107. mutationFn: (body: object) => {
  108. return post<InstallPackageResponse>('/workspaces/current/plugin/upgrade/marketplace', {
  109. body,
  110. })
  111. },
  112. })
  113. }
  114. export const usePluginDeclarationFromMarketPlace = (pluginUniqueIdentifier: string) => {
  115. return useQuery({
  116. queryKey: [NAME_SPACE, 'pluginDeclaration', pluginUniqueIdentifier],
  117. queryFn: () => get<{ manifest: PluginDeclaration }>('/workspaces/current/plugin/marketplace/pkg', { params: { plugin_unique_identifier: pluginUniqueIdentifier } }),
  118. enabled: !!pluginUniqueIdentifier,
  119. })
  120. }
  121. export const useVersionListOfPlugin = (pluginID: string) => {
  122. return useQuery<{ data: VersionListResponse }>({
  123. enabled: !!pluginID,
  124. queryKey: [NAME_SPACE, 'versions', pluginID],
  125. queryFn: () => getMarketplace<{ data: VersionListResponse }>(`/plugins/${pluginID}/versions`, { params: { page: 1, page_size: 100 } }),
  126. })
  127. }
  128. export const useInvalidateVersionListOfPlugin = () => {
  129. const queryClient = useQueryClient()
  130. return (pluginID: string) => {
  131. queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'versions', pluginID] })
  132. }
  133. }
  134. export const useInstallPackageFromLocal = () => {
  135. return useMutation({
  136. mutationFn: (uniqueIdentifier: string) => {
  137. return post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
  138. body: { plugin_unique_identifiers: [uniqueIdentifier] },
  139. })
  140. },
  141. })
  142. }
  143. export const useInstallPackageFromGitHub = () => {
  144. return useMutation({
  145. mutationFn: ({ repoUrl, selectedVersion, selectedPackage, uniqueIdentifier }: {
  146. repoUrl: string
  147. selectedVersion: string
  148. selectedPackage: string
  149. uniqueIdentifier: string
  150. }) => {
  151. return post<InstallPackageResponse>('/workspaces/current/plugin/install/github', {
  152. body: {
  153. repo: repoUrl,
  154. version: selectedVersion,
  155. package: selectedPackage,
  156. plugin_unique_identifier: uniqueIdentifier,
  157. },
  158. })
  159. },
  160. })
  161. }
  162. export const useUploadGitHub = (payload: {
  163. repo: string
  164. version: string
  165. package: string
  166. }) => {
  167. return useQuery({
  168. queryKey: [NAME_SPACE, 'uploadGitHub', payload],
  169. queryFn: () => post<uploadGitHubResponse>('/workspaces/current/plugin/upload/github', {
  170. body: payload,
  171. }),
  172. retry: 0,
  173. })
  174. }
  175. export const useInstallOrUpdate = ({
  176. onSuccess,
  177. }: {
  178. onSuccess?: (res: { success: boolean }[]) => void
  179. }) => {
  180. const { mutateAsync: updatePackageFromMarketPlace } = useUpdatePackageFromMarketPlace()
  181. return useMutation({
  182. mutationFn: (data: {
  183. payload: Dependency[],
  184. plugin: Plugin[],
  185. installedInfo: Record<string, VersionInfo>
  186. }) => {
  187. const { payload, plugin, installedInfo } = data
  188. return Promise.all(payload.map(async (item, i) => {
  189. try {
  190. const orgAndName = `${plugin[i]?.org || plugin[i]?.author}/${plugin[i]?.name}`
  191. const installedPayload = installedInfo[orgAndName]
  192. const isInstalled = !!installedPayload
  193. let uniqueIdentifier = ''
  194. if (item.type === 'github') {
  195. const data = item as GitHubItemAndMarketPlaceDependency
  196. // From local bundle don't have data.value.github_plugin_unique_identifier
  197. uniqueIdentifier = data.value.github_plugin_unique_identifier!
  198. if (!uniqueIdentifier) {
  199. const { unique_identifier } = await post<uploadGitHubResponse>('/workspaces/current/plugin/upload/github', {
  200. body: {
  201. repo: data.value.repo!,
  202. version: data.value.release! || data.value.version!,
  203. package: data.value.packages! || data.value.package!,
  204. },
  205. })
  206. uniqueIdentifier = data.value.github_plugin_unique_identifier! || unique_identifier
  207. // has the same version, but not installed
  208. if (uniqueIdentifier === installedPayload?.uniqueIdentifier) {
  209. return {
  210. success: true,
  211. }
  212. }
  213. }
  214. if (!isInstalled) {
  215. await post<InstallPackageResponse>('/workspaces/current/plugin/install/github', {
  216. body: {
  217. repo: data.value.repo!,
  218. version: data.value.release! || data.value.version!,
  219. package: data.value.packages! || data.value.package!,
  220. plugin_unique_identifier: uniqueIdentifier,
  221. },
  222. })
  223. }
  224. }
  225. if (item.type === 'marketplace') {
  226. const data = item as GitHubItemAndMarketPlaceDependency
  227. uniqueIdentifier = data.value.marketplace_plugin_unique_identifier! || plugin[i]?.plugin_id
  228. if (uniqueIdentifier === installedPayload?.uniqueIdentifier) {
  229. return {
  230. success: true,
  231. }
  232. }
  233. if (!isInstalled) {
  234. await post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', {
  235. body: {
  236. plugin_unique_identifiers: [uniqueIdentifier],
  237. },
  238. })
  239. }
  240. }
  241. if (item.type === 'package') {
  242. const data = item as PackageDependency
  243. uniqueIdentifier = data.value.unique_identifier
  244. if (uniqueIdentifier === installedPayload?.uniqueIdentifier) {
  245. return {
  246. success: true,
  247. }
  248. }
  249. if (!isInstalled) {
  250. await post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
  251. body: {
  252. plugin_unique_identifiers: [uniqueIdentifier],
  253. },
  254. })
  255. }
  256. }
  257. if (isInstalled) {
  258. if (item.type === 'package') {
  259. await uninstallPlugin(installedPayload.installedId)
  260. await post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
  261. body: {
  262. plugin_unique_identifiers: [uniqueIdentifier],
  263. },
  264. })
  265. }
  266. else {
  267. await updatePackageFromMarketPlace({
  268. original_plugin_unique_identifier: installedPayload?.uniqueIdentifier,
  269. new_plugin_unique_identifier: uniqueIdentifier,
  270. })
  271. }
  272. }
  273. return ({ success: true })
  274. }
  275. // eslint-disable-next-line unused-imports/no-unused-vars
  276. catch (e) {
  277. return Promise.resolve({ success: false })
  278. }
  279. }))
  280. },
  281. onSuccess,
  282. })
  283. }
  284. export const useDebugKey = () => {
  285. return useQuery({
  286. queryKey: [NAME_SPACE, 'debugKey'],
  287. queryFn: () => get<DebugInfoTypes>('/workspaces/current/plugin/debugging-key'),
  288. })
  289. }
  290. const usePermissionsKey = [NAME_SPACE, 'permissions']
  291. export const usePermissions = () => {
  292. return useQuery({
  293. queryKey: usePermissionsKey,
  294. queryFn: () => get<Permissions>('/workspaces/current/plugin/permission/fetch'),
  295. })
  296. }
  297. export const useInvalidatePermissions = () => {
  298. const queryClient = useQueryClient()
  299. return () => {
  300. queryClient.invalidateQueries(
  301. {
  302. queryKey: usePermissionsKey,
  303. })
  304. }
  305. }
  306. export const useMutationPermissions = ({
  307. onSuccess,
  308. }: {
  309. onSuccess?: () => void
  310. }) => {
  311. return useMutation({
  312. mutationFn: (payload: Permissions) => {
  313. return post('/workspaces/current/plugin/permission/change', { body: payload })
  314. },
  315. onSuccess,
  316. })
  317. }
  318. export const useMutationPluginsFromMarketplace = () => {
  319. return useMutation({
  320. mutationFn: (pluginsSearchParams: PluginsSearchParams) => {
  321. const {
  322. query,
  323. sortBy,
  324. sortOrder,
  325. category,
  326. tags,
  327. exclude,
  328. type,
  329. page = 1,
  330. pageSize = 40,
  331. } = pluginsSearchParams
  332. const pluginOrBundle = type === 'bundle' ? 'bundles' : 'plugins'
  333. return postMarketplace<{ data: PluginsFromMarketplaceResponse }>(`/${pluginOrBundle}/search/advanced`, {
  334. body: {
  335. page,
  336. page_size: pageSize,
  337. query,
  338. sort_by: sortBy,
  339. sort_order: sortOrder,
  340. category: category !== 'all' ? category : '',
  341. tags,
  342. exclude,
  343. type,
  344. },
  345. })
  346. },
  347. })
  348. }
  349. export const useFetchPluginsInMarketPlaceByIds = (unique_identifiers: string[], options?: QueryOptions<{ data: PluginsFromMarketplaceResponse }>) => {
  350. return useQuery({
  351. ...options,
  352. queryKey: [NAME_SPACE, 'fetchPluginsInMarketPlaceByIds', unique_identifiers],
  353. queryFn: () => postMarketplace<{ data: PluginsFromMarketplaceResponse }>('/plugins/identifier/batch', {
  354. body: {
  355. unique_identifiers,
  356. },
  357. }),
  358. enabled: unique_identifiers?.filter(i => !!i).length > 0,
  359. retry: 0,
  360. })
  361. }
  362. export const useFetchPluginsInMarketPlaceByInfo = (infos: Record<string, any>[]) => {
  363. return useQuery({
  364. queryKey: [NAME_SPACE, 'fetchPluginsInMarketPlaceByInfo', infos],
  365. queryFn: () => postMarketplace<{ data: PluginsFromMarketplaceByInfoResponse }>('/plugins/versions/batch', {
  366. body: {
  367. plugin_tuples: infos.map(info => ({
  368. org: info.organization,
  369. name: info.plugin,
  370. version: info.version,
  371. })),
  372. },
  373. }),
  374. enabled: infos?.filter(i => !!i).length > 0,
  375. retry: 0,
  376. })
  377. }
  378. const usePluginTaskListKey = [NAME_SPACE, 'pluginTaskList']
  379. export const usePluginTaskList = (category?: PluginType) => {
  380. const {
  381. canManagement,
  382. } = usePermission()
  383. const { refreshPluginList } = useRefreshPluginList()
  384. const {
  385. data,
  386. isFetched,
  387. isRefetching,
  388. refetch,
  389. ...rest
  390. } = useQuery({
  391. enabled: canManagement,
  392. queryKey: usePluginTaskListKey,
  393. queryFn: () => get<{ tasks: PluginTask[] }>('/workspaces/current/plugin/tasks?page=1&page_size=100'),
  394. refetchInterval: (lastQuery) => {
  395. const lastData = lastQuery.state.data
  396. const taskDone = lastData?.tasks.every(task => task.status === TaskStatus.success || task.status === TaskStatus.failed)
  397. return taskDone ? false : 5000
  398. },
  399. })
  400. useEffect(() => {
  401. // After first fetch, refresh plugin list each time all tasks are done
  402. if (!isRefetching) {
  403. const lastData = cloneDeep(data)
  404. const taskDone = lastData?.tasks.every(task => task.status === TaskStatus.success || task.status === TaskStatus.failed)
  405. const taskAllFailed = lastData?.tasks.every(task => task.status === TaskStatus.failed)
  406. if (taskDone) {
  407. if (lastData?.tasks.length && !taskAllFailed)
  408. refreshPluginList(category ? { category } as any : undefined, !category)
  409. }
  410. }
  411. // eslint-disable-next-line react-hooks/exhaustive-deps
  412. }, [isRefetching])
  413. const handleRefetch = useCallback(() => {
  414. refetch()
  415. }, [refetch])
  416. return {
  417. data,
  418. pluginTasks: data?.tasks || [],
  419. isFetched,
  420. handleRefetch,
  421. ...rest,
  422. }
  423. }
  424. export const useMutationClearTaskPlugin = () => {
  425. return useMutation({
  426. mutationFn: ({ taskId, pluginId }: { taskId: string; pluginId: string }) => {
  427. return post<{ success: boolean }>(`/workspaces/current/plugin/tasks/${taskId}/delete/${pluginId}`)
  428. },
  429. })
  430. }
  431. export const useMutationClearAllTaskPlugin = () => {
  432. return useMutation({
  433. mutationFn: () => {
  434. return post<{ success: boolean }>('/workspaces/current/plugin/tasks/delete_all')
  435. },
  436. })
  437. }
  438. export const usePluginManifestInfo = (pluginUID: string) => {
  439. return useQuery({
  440. enabled: !!pluginUID,
  441. queryKey: [[NAME_SPACE, 'manifest', pluginUID]],
  442. queryFn: () => getMarketplace<{ data: { plugin: PluginInfoFromMarketPlace, version: { version: string } } }>(`/plugins/${pluginUID}`),
  443. retry: 0,
  444. })
  445. }
  446. export const useDownloadPlugin = (info: { organization: string; pluginName: string; version: string }, needDownload: boolean) => {
  447. return useQuery({
  448. queryKey: [NAME_SPACE, 'downloadPlugin', info],
  449. queryFn: () => getMarketplace<Blob>(`/plugins/${info.organization}/${info.pluginName}/${info.version}/download`),
  450. enabled: needDownload,
  451. retry: 0,
  452. })
  453. }
  454. export const useMutationCheckDependencies = () => {
  455. return useMutation({
  456. mutationFn: (appId: string) => {
  457. return get<{ leaked_dependencies: Dependency[] }>(`/apps/imports/${appId}/check-dependencies`)
  458. },
  459. })
  460. }
  461. export const useModelInList = (currentProvider?: ModelProvider, modelId?: string) => {
  462. return useQuery({
  463. queryKey: ['modelInList', currentProvider?.provider, modelId],
  464. queryFn: async () => {
  465. if (!modelId || !currentProvider) return false
  466. try {
  467. const modelsData = await fetchModelProviderModelList(`/workspaces/current/model-providers/${currentProvider?.provider}/models`)
  468. return !!modelId && !!modelsData.data.find(item => item.model === modelId)
  469. }
  470. catch {
  471. return false
  472. }
  473. },
  474. enabled: !!modelId && !!currentProvider,
  475. })
  476. }
  477. export const usePluginInfo = (providerName?: string) => {
  478. return useQuery({
  479. queryKey: ['pluginInfo', providerName],
  480. queryFn: async () => {
  481. if (!providerName) return null
  482. const parts = providerName.split('/')
  483. const org = parts[0]
  484. const name = parts[1]
  485. try {
  486. const response = await fetchPluginInfoFromMarketPlace({ org, name })
  487. return response.data.plugin.category === PluginTypeEnum.model ? response.data.plugin : null
  488. }
  489. catch {
  490. return null
  491. }
  492. },
  493. enabled: !!providerName,
  494. })
  495. }