mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 21:23:22 +08:00
feat: can install bundle from local
This commit is contained in:
parent
77d6dbb3d0
commit
4ef0a3818f
@ -4,7 +4,7 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
|||||||
import type { Dependency, GitHubItemAndMarketPlaceDependency, PackageDependency, Plugin } from '../../../types'
|
import type { Dependency, GitHubItemAndMarketPlaceDependency, PackageDependency, Plugin } from '../../../types'
|
||||||
import MarketplaceItem from '../item/marketplace-item'
|
import MarketplaceItem from '../item/marketplace-item'
|
||||||
import GithubItem from '../item/github-item'
|
import GithubItem from '../item/github-item'
|
||||||
import { useFetchPluginsInMarketPlaceByIds } from '@/service/use-plugins'
|
import { useFetchPluginsInMarketPlaceByIds, useFetchPluginsInMarketPlaceByInfo } from '@/service/use-plugins'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import { useGetState } from 'ahooks'
|
import { useGetState } from 'ahooks'
|
||||||
import PackageItem from '../item/package-item'
|
import PackageItem from '../item/package-item'
|
||||||
@ -23,8 +23,8 @@ const InstallByDSLList: FC<Props> = ({
|
|||||||
onSelect,
|
onSelect,
|
||||||
onLoadedAllPlugin,
|
onLoadedAllPlugin,
|
||||||
}) => {
|
}) => {
|
||||||
const { isLoading: isFetchingMarketplaceData, data: marketplaceRes } = useFetchPluginsInMarketPlaceByIds(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value.plugin_unique_identifier!))
|
const { isLoading: isFetchingMarketplaceDataFromDSL, data: marketplaceFromDSLRes } = useFetchPluginsInMarketPlaceByIds(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value.plugin_unique_identifier!))
|
||||||
// console.log(allPlugins)
|
const { isLoading: isFetchingMarketplaceDataFromLocal, data: marketplaceResFromLocalRes } = useFetchPluginsInMarketPlaceByInfo(allPlugins.filter(d => d.type === 'marketplace').map(d => (d as GitHubItemAndMarketPlaceDependency).value!))
|
||||||
const [plugins, setPlugins, getPlugins] = useGetState<(Plugin | undefined)[]>((() => {
|
const [plugins, setPlugins, getPlugins] = useGetState<(Plugin | undefined)[]>((() => {
|
||||||
const hasLocalPackage = allPlugins.some(d => d.type === 'package')
|
const hasLocalPackage = allPlugins.some(d => d.type === 'package')
|
||||||
if (!hasLocalPackage)
|
if (!hasLocalPackage)
|
||||||
@ -70,8 +70,8 @@ const InstallByDSLList: FC<Props> = ({
|
|||||||
}, [allPlugins])
|
}, [allPlugins])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isFetchingMarketplaceData && marketplaceRes?.data.plugins) {
|
if (!isFetchingMarketplaceDataFromDSL && marketplaceFromDSLRes?.data.plugins) {
|
||||||
const payloads = marketplaceRes?.data.plugins
|
const payloads = marketplaceFromDSLRes?.data.plugins
|
||||||
const failedIndex: number[] = []
|
const failedIndex: number[] = []
|
||||||
const nextPlugins = produce(getPlugins(), (draft) => {
|
const nextPlugins = produce(getPlugins(), (draft) => {
|
||||||
marketPlaceInDSLIndex.forEach((index, i) => {
|
marketPlaceInDSLIndex.forEach((index, i) => {
|
||||||
@ -84,10 +84,34 @@ const InstallByDSLList: FC<Props> = ({
|
|||||||
setPlugins(nextPlugins)
|
setPlugins(nextPlugins)
|
||||||
if (failedIndex.length > 0)
|
if (failedIndex.length > 0)
|
||||||
setErrorIndexes([...errorIndexes, ...failedIndex])
|
setErrorIndexes([...errorIndexes, ...failedIndex])
|
||||||
// marketplaceRes?.data.plugins
|
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [isFetchingMarketplaceData])
|
}, [isFetchingMarketplaceDataFromDSL])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isFetchingMarketplaceDataFromLocal && marketplaceResFromLocalRes?.data.versions) {
|
||||||
|
const payloads = marketplaceResFromLocalRes?.data.versions
|
||||||
|
const failedIndex: number[] = []
|
||||||
|
const nextPlugins = produce(getPlugins(), (draft) => {
|
||||||
|
marketPlaceInDSLIndex.forEach((index, i) => {
|
||||||
|
if (payloads[i]) {
|
||||||
|
const item = payloads[i]
|
||||||
|
draft[index] = { // TODO: wait for api change
|
||||||
|
name: 'xxx',
|
||||||
|
plugin_id: item.unique_identifier,
|
||||||
|
} as Plugin
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
failedIndex.push(index)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
setPlugins(nextPlugins)
|
||||||
|
if (failedIndex.length > 0)
|
||||||
|
setErrorIndexes([...errorIndexes, ...failedIndex])
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [isFetchingMarketplaceDataFromLocal])
|
||||||
|
|
||||||
const isLoadedAllData = (plugins.filter(p => !!p).length + errorIndexes.length) === allPlugins.length
|
const isLoadedAllData = (plugins.filter(p => !!p).length + errorIndexes.length) === allPlugins.length
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@ -58,7 +58,10 @@ const Install: FC<Props> = ({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
const handleInstall = () => {
|
const handleInstall = () => {
|
||||||
installFromMarketplaceAndGitHub(allPlugins.filter((_d, index) => selectedIndexes.includes(index)))
|
installFromMarketplaceAndGitHub({
|
||||||
|
payload: allPlugins.filter((_d, index) => selectedIndexes.includes(index)),
|
||||||
|
plugin: selectedPlugins,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@ -310,6 +310,13 @@ export type UninstallPluginResponse = {
|
|||||||
export type PluginsFromMarketplaceResponse = {
|
export type PluginsFromMarketplaceResponse = {
|
||||||
plugins: Plugin[]
|
plugins: Plugin[]
|
||||||
}
|
}
|
||||||
|
export type PluginsFromMarketplaceByInfoResponse = {
|
||||||
|
versions: {
|
||||||
|
plugin_name: string
|
||||||
|
plugin_org: string
|
||||||
|
unique_identifier: string
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
export type GitHubItemAndMarketPlaceDependency = {
|
export type GitHubItemAndMarketPlaceDependency = {
|
||||||
type: 'github' | 'marketplace' | 'package'
|
type: 'github' | 'marketplace' | 'package'
|
||||||
|
|||||||
@ -7,7 +7,9 @@ import type {
|
|||||||
InstalledPluginListResponse,
|
InstalledPluginListResponse,
|
||||||
PackageDependency,
|
PackageDependency,
|
||||||
Permissions,
|
Permissions,
|
||||||
|
Plugin,
|
||||||
PluginTask,
|
PluginTask,
|
||||||
|
PluginsFromMarketplaceByInfoResponse,
|
||||||
PluginsFromMarketplaceResponse,
|
PluginsFromMarketplaceResponse,
|
||||||
VersionListResponse,
|
VersionListResponse,
|
||||||
uploadGitHubResponse,
|
uploadGitHubResponse,
|
||||||
@ -117,8 +119,12 @@ export const useInstallFromMarketplaceAndGitHub = ({
|
|||||||
onSuccess?: (res: { success: boolean }[]) => void
|
onSuccess?: (res: { success: boolean }[]) => void
|
||||||
}) => {
|
}) => {
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: (payload: Dependency[]) => {
|
mutationFn: (data: {
|
||||||
return Promise.all(payload.map(async (item) => {
|
payload: Dependency[],
|
||||||
|
plugin: Plugin[],
|
||||||
|
}) => {
|
||||||
|
const { payload, plugin } = data
|
||||||
|
return Promise.all(payload.map(async (item, i) => {
|
||||||
try {
|
try {
|
||||||
if (item.type === 'github') {
|
if (item.type === 'github') {
|
||||||
const data = item as GitHubItemAndMarketPlaceDependency
|
const data = item as GitHubItemAndMarketPlaceDependency
|
||||||
@ -136,7 +142,7 @@ export const useInstallFromMarketplaceAndGitHub = ({
|
|||||||
|
|
||||||
await post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', {
|
await post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', {
|
||||||
body: {
|
body: {
|
||||||
plugin_unique_identifiers: [data.value.plugin_unique_identifier!],
|
plugin_unique_identifiers: [data.value.plugin_unique_identifier! || plugin[i]?.plugin_id],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -231,7 +237,25 @@ export const useFetchPluginsInMarketPlaceByIds = (unique_identifiers: string[])
|
|||||||
unique_identifiers,
|
unique_identifiers,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
enabled: unique_identifiers.filter(i => !!i).length > 0, // loaded then fetch
|
enabled: unique_identifiers?.filter(i => !!i).length > 0,
|
||||||
|
retry: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useFetchPluginsInMarketPlaceByInfo = (infos: Record<string, any>[]) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [NAME_SPACE, 'fetchPluginsInMarketPlaceByInfo', infos],
|
||||||
|
queryFn: () => postMarketplace<{ data: PluginsFromMarketplaceByInfoResponse }>('/plugins/versions', {
|
||||||
|
body: {
|
||||||
|
plugin_tuples: infos.map(info => ({
|
||||||
|
org: info.organization,
|
||||||
|
name: info.plugin,
|
||||||
|
version: info.version,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
enabled: infos?.filter(i => !!i).length > 0,
|
||||||
|
retry: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user