mirror of
https://github.com/langgenius/dify.git
synced 2026-05-03 08:58:09 +08:00
Feat/plugins (#12547)
Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com>
This commit is contained in:
159
web/app/components/plugins/plugin-item/action.tsx
Normal file
159
web/app/components/plugins/plugin-item/action.tsx
Normal file
@ -0,0 +1,159 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React, { useCallback } from 'react'
|
||||
import { type MetaData, PluginSource } from '../types'
|
||||
import { RiDeleteBinLine, RiInformation2Line, RiLoopLeftLine } from '@remixicon/react'
|
||||
import { useBoolean } from 'ahooks'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import PluginInfo from '../plugin-page/plugin-info'
|
||||
import ActionButton from '../../base/action-button'
|
||||
import Tooltip from '../../base/tooltip'
|
||||
import Confirm from '../../base/confirm'
|
||||
import { uninstallPlugin } from '@/service/plugins'
|
||||
import { useGitHubReleases } from '../install-plugin/hooks'
|
||||
import Toast from '@/app/components/base/toast'
|
||||
import { useModalContext } from '@/context/modal-context'
|
||||
import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
|
||||
|
||||
const i18nPrefix = 'plugin.action'
|
||||
|
||||
type Props = {
|
||||
author: string
|
||||
installationId: string
|
||||
pluginUniqueIdentifier: string
|
||||
pluginName: string
|
||||
usedInApps: number
|
||||
isShowFetchNewVersion: boolean
|
||||
isShowInfo: boolean
|
||||
isShowDelete: boolean
|
||||
onDelete: () => void
|
||||
meta?: MetaData
|
||||
}
|
||||
const Action: FC<Props> = ({
|
||||
author,
|
||||
installationId,
|
||||
pluginUniqueIdentifier,
|
||||
pluginName,
|
||||
isShowFetchNewVersion,
|
||||
isShowInfo,
|
||||
isShowDelete,
|
||||
onDelete,
|
||||
meta,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const [isShowPluginInfo, {
|
||||
setTrue: showPluginInfo,
|
||||
setFalse: hidePluginInfo,
|
||||
}] = useBoolean(false)
|
||||
const [deleting, {
|
||||
setTrue: showDeleting,
|
||||
setFalse: hideDeleting,
|
||||
}] = useBoolean(false)
|
||||
const { checkForUpdates, fetchReleases } = useGitHubReleases()
|
||||
const { setShowUpdatePluginModal } = useModalContext()
|
||||
const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
|
||||
|
||||
const handleFetchNewVersion = async () => {
|
||||
const fetchedReleases = await fetchReleases(author, pluginName)
|
||||
if (fetchedReleases.length === 0) return
|
||||
const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
|
||||
Toast.notify(toastProps)
|
||||
if (needUpdate) {
|
||||
setShowUpdatePluginModal({
|
||||
onSaveCallback: () => {
|
||||
invalidateInstalledPluginList()
|
||||
},
|
||||
payload: {
|
||||
type: PluginSource.github,
|
||||
github: {
|
||||
originalPackageInfo: {
|
||||
id: pluginUniqueIdentifier,
|
||||
repo: meta!.repo,
|
||||
version: meta!.version,
|
||||
package: meta!.package,
|
||||
releases: fetchedReleases,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const [isShowDeleteConfirm, {
|
||||
setTrue: showDeleteConfirm,
|
||||
setFalse: hideDeleteConfirm,
|
||||
}] = useBoolean(false)
|
||||
|
||||
const handleDelete = useCallback(async () => {
|
||||
showDeleting()
|
||||
const res = await uninstallPlugin(installationId)
|
||||
hideDeleting()
|
||||
if (res.success) {
|
||||
hideDeleteConfirm()
|
||||
onDelete()
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [installationId, onDelete])
|
||||
return (
|
||||
<div className='flex space-x-1'>
|
||||
{/* Only plugin installed from GitHub need to check if it's the new version */}
|
||||
{isShowFetchNewVersion
|
||||
&& (
|
||||
<Tooltip popupContent={t(`${i18nPrefix}.checkForUpdates`)}>
|
||||
<ActionButton onClick={handleFetchNewVersion}>
|
||||
<RiLoopLeftLine className='w-4 h-4 text-text-tertiary' />
|
||||
</ActionButton>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
{
|
||||
isShowInfo
|
||||
&& (
|
||||
<Tooltip popupContent={t(`${i18nPrefix}.pluginInfo`)}>
|
||||
<ActionButton onClick={showPluginInfo}>
|
||||
<RiInformation2Line className='w-4 h-4 text-text-tertiary' />
|
||||
</ActionButton>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
{
|
||||
isShowDelete
|
||||
&& (
|
||||
<Tooltip popupContent={t(`${i18nPrefix}.delete`)}>
|
||||
<ActionButton
|
||||
className='hover:bg-state-destructive-hover text-text-tertiary hover:text-text-destructive'
|
||||
onClick={showDeleteConfirm}
|
||||
>
|
||||
<RiDeleteBinLine className='w-4 h-4' />
|
||||
</ActionButton>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
|
||||
{isShowPluginInfo && (
|
||||
<PluginInfo
|
||||
repository={meta!.repo}
|
||||
release={meta!.version}
|
||||
packageName={meta!.package}
|
||||
onHide={hidePluginInfo}
|
||||
/>
|
||||
)}
|
||||
<Confirm
|
||||
isShow={isShowDeleteConfirm}
|
||||
title={t(`${i18nPrefix}.delete`)}
|
||||
content={
|
||||
<div>
|
||||
{t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{pluginName}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
|
||||
{/* // todo: add usedInApps */}
|
||||
{/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
|
||||
</div>
|
||||
}
|
||||
onCancel={hideDeleteConfirm}
|
||||
onConfirm={handleDelete}
|
||||
isLoading={deleting}
|
||||
isDisabled={deleting}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(Action)
|
||||
187
web/app/components/plugins/plugin-item/index.tsx
Normal file
187
web/app/components/plugins/plugin-item/index.tsx
Normal file
@ -0,0 +1,187 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import {
|
||||
RiArrowRightUpLine,
|
||||
RiBugLine,
|
||||
RiHardDrive3Line,
|
||||
RiLoginCircleLine,
|
||||
RiVerifiedBadgeLine,
|
||||
} from '@remixicon/react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { usePluginPageContext } from '../plugin-page/context'
|
||||
import { Github } from '../../base/icons/src/public/common'
|
||||
import Badge from '../../base/badge'
|
||||
import { type PluginDetail, PluginSource, PluginType } from '../types'
|
||||
import CornerMark from '../card/base/corner-mark'
|
||||
import Description from '../card/base/description'
|
||||
import OrgInfo from '../card/base/org-info'
|
||||
import Title from '../card/base/title'
|
||||
import Action from './action'
|
||||
import cn from '@/utils/classnames'
|
||||
import { API_PREFIX, MARKETPLACE_URL_PREFIX } from '@/config'
|
||||
import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
|
||||
import { useInvalidateAllBuiltInTools, useInvalidateAllToolProviders } from '@/service/use-tools'
|
||||
import { useSingleCategories } from '../hooks'
|
||||
import { useProviderContext } from '@/context/provider-context'
|
||||
import { useRenderI18nObject } from '@/hooks/use-i18n'
|
||||
|
||||
type Props = {
|
||||
className?: string
|
||||
plugin: PluginDetail
|
||||
}
|
||||
|
||||
const PluginItem: FC<Props> = ({
|
||||
className,
|
||||
plugin,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const { categoriesMap } = useSingleCategories()
|
||||
const currentPluginID = usePluginPageContext(v => v.currentPluginID)
|
||||
const setCurrentPluginID = usePluginPageContext(v => v.setCurrentPluginID)
|
||||
const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
|
||||
const invalidateAllToolProviders = useInvalidateAllToolProviders()
|
||||
const invalidateAllBuiltinTools = useInvalidateAllBuiltInTools()
|
||||
const { refreshModelProviders } = useProviderContext()
|
||||
|
||||
const {
|
||||
source,
|
||||
tenant_id,
|
||||
installation_id,
|
||||
plugin_unique_identifier,
|
||||
endpoints_active,
|
||||
meta,
|
||||
plugin_id,
|
||||
} = plugin
|
||||
const { category, author, name, label, description, icon, verified } = plugin.declaration
|
||||
|
||||
const orgName = useMemo(() => {
|
||||
return [PluginSource.github, PluginSource.marketplace].includes(source) ? author : ''
|
||||
}, [source, author])
|
||||
|
||||
const handleDelete = () => {
|
||||
invalidateInstalledPluginList()
|
||||
if (PluginType.model.includes(category))
|
||||
refreshModelProviders()
|
||||
if (PluginType.tool.includes(category)) {
|
||||
invalidateAllToolProviders()
|
||||
invalidateAllBuiltinTools()
|
||||
}
|
||||
}
|
||||
const getValueFromI18nObject = useRenderI18nObject()
|
||||
const title = getValueFromI18nObject(label)
|
||||
const descriptionText = getValueFromI18nObject(description)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'p-1 rounded-xl border-[1.5px] border-background-section-burn',
|
||||
currentPluginID === plugin_id && 'border-components-option-card-option-selected-border',
|
||||
source === PluginSource.debugging
|
||||
? 'bg-[repeating-linear-gradient(-45deg,rgba(16,24,40,0.04),rgba(16,24,40,0.04)_5px,rgba(0,0,0,0.02)_5px,rgba(0,0,0,0.02)_10px)]'
|
||||
: 'bg-background-section-burn',
|
||||
)}
|
||||
onClick={() => {
|
||||
setCurrentPluginID(plugin.plugin_id)
|
||||
}}
|
||||
>
|
||||
<div className={cn('relative p-4 pb-3 border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg hover-bg-components-panel-on-panel-item-bg rounded-xl shadow-xs', className)}>
|
||||
<CornerMark text={categoriesMap[category].label} />
|
||||
{/* Header */}
|
||||
<div className="flex">
|
||||
<div className='flex items-center justify-center w-10 h-10 overflow-hidden border-components-panel-border-subtle border-[1px] rounded-xl'>
|
||||
<img
|
||||
className='w-full h-full'
|
||||
src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`}
|
||||
alt={`plugin-${plugin_unique_identifier}-logo`}
|
||||
/>
|
||||
</div>
|
||||
<div className="ml-3 w-0 grow">
|
||||
<div className="flex items-center h-5">
|
||||
<Title title={title} />
|
||||
{verified && <RiVerifiedBadgeLine className="shrink-0 ml-0.5 w-4 h-4 text-text-accent" />}
|
||||
<Badge className='shrink-0 ml-1' text={source === PluginSource.github ? plugin.meta!.version : plugin.version} />
|
||||
</div>
|
||||
<div className='flex items-center justify-between'>
|
||||
<Description text={descriptionText} descriptionLineRows={1}></Description>
|
||||
<div onClick={e => e.stopPropagation()}>
|
||||
<Action
|
||||
pluginUniqueIdentifier={plugin_unique_identifier}
|
||||
installationId={installation_id}
|
||||
author={author}
|
||||
pluginName={name}
|
||||
usedInApps={5}
|
||||
isShowFetchNewVersion={source === PluginSource.github}
|
||||
isShowInfo={source === PluginSource.github}
|
||||
isShowDelete
|
||||
meta={meta}
|
||||
onDelete={handleDelete}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className='mt-1.5 mb-1 flex justify-between items-center h-4 px-4'>
|
||||
<div className='flex items-center'>
|
||||
<OrgInfo
|
||||
className="mt-0.5"
|
||||
orgName={orgName}
|
||||
packageName={name}
|
||||
packageNameClassName='w-auto max-w-[150px]'
|
||||
/>
|
||||
{category === PluginType.extension && (
|
||||
<>
|
||||
<div className='mx-2 text-text-quaternary system-xs-regular'>·</div>
|
||||
<div className='flex text-text-tertiary system-xs-regular space-x-1'>
|
||||
<RiLoginCircleLine className='w-4 h-4' />
|
||||
<span>{t('plugin.endpointsEnabled', { num: endpoints_active })}</span>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='flex items-center'>
|
||||
{source === PluginSource.github
|
||||
&& <>
|
||||
<a href={`https://github.com/${meta!.repo}`} target='_blank' className='flex items-center gap-1'>
|
||||
<div className='text-text-tertiary system-2xs-medium-uppercase'>{t('plugin.from')}</div>
|
||||
<div className='flex items-center space-x-0.5 text-text-secondary'>
|
||||
<Github className='w-3 h-3' />
|
||||
<div className='system-2xs-semibold-uppercase'>GitHub</div>
|
||||
<RiArrowRightUpLine className='w-3 h-3' />
|
||||
</div>
|
||||
</a>
|
||||
</>
|
||||
}
|
||||
{source === PluginSource.marketplace
|
||||
&& <>
|
||||
<a href={`${MARKETPLACE_URL_PREFIX}/plugins/${author}/${name}`} target='_blank' className='flex items-center gap-0.5'>
|
||||
<div className='text-text-tertiary system-2xs-medium-uppercase'>{t('plugin.from')} <span className='text-text-secondary'>marketplace</span></div>
|
||||
<RiArrowRightUpLine className='w-3 h-3 text-text-tertiary' />
|
||||
</a>
|
||||
</>
|
||||
}
|
||||
{source === PluginSource.local
|
||||
&& <>
|
||||
<div className='flex items-center gap-1'>
|
||||
<RiHardDrive3Line className='text-text-tertiary w-3 h-3' />
|
||||
<div className='text-text-tertiary system-2xs-medium-uppercase'>Local Plugin</div>
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
{source === PluginSource.debugging
|
||||
&& <>
|
||||
<div className='flex items-center gap-1'>
|
||||
<RiBugLine className='w-3 h-3 text-text-warning' />
|
||||
<div className='text-text-warning system-2xs-medium-uppercase'>Debugging Plugin</div>
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(PluginItem)
|
||||
Reference in New Issue
Block a user