dify/web/app/components/plugins/marketplace/utils.ts

122 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-12-03 14:34:23 +08:00
import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
2024-10-29 14:44:30 +08:00
import type { Plugin } from '@/app/components/plugins/types'
2024-11-25 12:03:49 +08:00
import { PluginType } from '@/app/components/plugins/types'
2024-10-29 14:44:30 +08:00
import type {
2024-10-30 15:15:53 +08:00
CollectionsAndPluginsSearchParams,
2024-10-29 14:44:30 +08:00
MarketplaceCollection,
} from '@/app/components/plugins/marketplace/types'
2024-12-03 14:34:23 +08:00
import {
MARKETPLACE_API_PREFIX,
MARKETPLACE_URL_PREFIX,
} from '@/config'
2024-10-29 14:44:30 +08:00
2024-10-30 15:15:53 +08:00
export const getPluginIconInMarketplace = (plugin: Plugin) => {
2024-12-03 14:34:23 +08:00
if (plugin.type === 'bundle')
return `${MARKETPLACE_API_PREFIX}/bundles/${plugin.org}/${plugin.name}/icon`
2024-10-30 15:15:53 +08:00
return `${MARKETPLACE_API_PREFIX}/plugins/${plugin.org}/${plugin.name}/icon`
}
2024-12-03 14:34:23 +08:00
export const getFormattedPlugin = (bundle: any) => {
if (bundle.type === 'bundle') {
return {
...bundle,
icon: getPluginIconInMarketplace(bundle),
brief: bundle.description,
label: bundle.labels,
}
}
return {
...bundle,
icon: getPluginIconInMarketplace(bundle),
}
}
export const getPluginLinkInMarketplace = (plugin: Plugin) => {
if (plugin.type === 'bundle')
return `${MARKETPLACE_URL_PREFIX}/bundles/${plugin.org}/${plugin.name}`
return `${MARKETPLACE_URL_PREFIX}/plugins/${plugin.org}/${plugin.name}`
}
2024-12-10 15:51:55 +08:00
export const getMarketplacePluginsByCollectionId = async (collectionId: string, query?: CollectionsAndPluginsSearchParams) => {
let plugins = [] as Plugin[]
try {
const url = `${MARKETPLACE_API_PREFIX}/collections/${collectionId}/plugins`
const marketplaceCollectionPluginsData = await globalThis.fetch(
url,
{
cache: 'no-store',
method: 'POST',
body: JSON.stringify({
category: query?.category,
exclude: query?.exclude,
type: query?.type,
}),
},
)
const marketplaceCollectionPluginsDataJson = await marketplaceCollectionPluginsData.json()
plugins = marketplaceCollectionPluginsDataJson.data.plugins.map((plugin: Plugin) => {
return getFormattedPlugin(plugin)
})
}
// eslint-disable-next-line unused-imports/no-unused-vars
catch (e) {
plugins = []
}
return plugins
}
2024-10-30 15:15:53 +08:00
export const getMarketplaceCollectionsAndPlugins = async (query?: CollectionsAndPluginsSearchParams) => {
2024-10-29 14:44:30 +08:00
let marketplaceCollections = [] as MarketplaceCollection[]
let marketplaceCollectionPluginsMap = {} as Record<string, Plugin[]>
try {
2024-11-25 12:03:49 +08:00
let marketplaceUrl = `${MARKETPLACE_API_PREFIX}/collections?page=1&page_size=100`
if (query?.condition)
marketplaceUrl += `&condition=${query.condition}`
2024-12-03 14:34:23 +08:00
if (query?.type)
marketplaceUrl += `&type=${query.type}`
2024-11-25 12:03:49 +08:00
const marketplaceCollectionsData = await globalThis.fetch(marketplaceUrl, { cache: 'no-store' })
2024-10-29 14:44:30 +08:00
const marketplaceCollectionsDataJson = await marketplaceCollectionsData.json()
marketplaceCollections = marketplaceCollectionsDataJson.data.collections
await Promise.all(marketplaceCollections.map(async (collection: MarketplaceCollection) => {
2024-12-10 15:51:55 +08:00
const plugins = await getMarketplacePluginsByCollectionId(collection.name, query)
2024-10-29 14:44:30 +08:00
marketplaceCollectionPluginsMap[collection.name] = plugins
}))
}
// eslint-disable-next-line unused-imports/no-unused-vars
catch (e) {
marketplaceCollections = []
marketplaceCollectionPluginsMap = {}
}
return {
marketplaceCollections,
marketplaceCollectionPluginsMap,
}
}
2024-11-25 12:03:49 +08:00
export const getMarketplaceListCondition = (pluginType: string) => {
if (pluginType === PluginType.tool)
return 'category=tool'
if (pluginType === PluginType.model)
return 'category=model'
if (pluginType === PluginType.extension)
return 'category=endpoint'
return ''
}
2024-12-03 14:34:23 +08:00
export const getMarketplaceListFilterType = (category: string) => {
if (category === PLUGIN_TYPE_SEARCH_MAP.all)
return undefined
if (category === PLUGIN_TYPE_SEARCH_MAP.bundle)
return 'bundle'
return 'plugin'
}