Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
'use client'
import type { AgentOrchestrateAddActionOptions } from '../add-actions-context'
import type { ToolSettingTarget } from './types'
import type { ToolDefaultValue, ToolValue } from '@/app/components/workflow/block-selector/types'
import type { ToolWithProvider } from '@/app/components/workflow/types'
import type {
AgentCliTool,
AgentProviderTool,
AgentTool,
} from '@/features/agent-v2/agent-composer/form-state'
import type { AgentProviderToolDefaultValue } from '@/features/agent-v2/agent-composer/store-modules/tools'
import { cn } from '@langgenius/dify-ui/cn'
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { noop } from 'es-toolkit/function'
import { useAtomValue, useSetAtom } from 'jotai'
import { memo, useCallback, useId, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { PluginCategoryEnum } from '@/app/components/plugins/types'
import { parseToolProviderType } from '@/app/components/tools/provider-type'
import { CollectionType } from '@/app/components/tools/types'
import { selectToolsWithProvider } from '@/app/components/tools/utils/query-select'
import { ToolPickerContent } from '@/app/components/workflow/block-selector/tool-picker'
import { useGetLanguage } from '@/context/i18n'
import {
addProviderToolsAtom,
agentComposerToolsAtom,
setProviderToolCredentialAtom,
} from '@/features/agent-v2/agent-composer/store-modules/tools'
import { ENABLE_AGENT_CLI_TOOLS } from '@/features/agent-v2/agent-detail/configure/feature-flags'
import { consoleQuery } from '@/service/console'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
import {
createAgentToolProviderCatalog,
getAgentProviderPluginId,
getAgentProviderToolDisplayName,
getAgentProviderToolIcon,
getLocalizedText,
getProviderCredentialType,
getProviderCredentialVariant,
useAgentToolPresentation,
useAgentToolProviderCatalog,
} from '../../../tool-provider-catalog'
import { useRegisterAgentOrchestrateAddAction } from '../add-actions-context'
import { ConfigureSectionAddButton } from '../common/add-button'
import { ConfigureSectionEmpty } from '../common/empty'
import { ConfigureSection } from '../common/section'
import { AgentConfigureTipContent } from '../common/tip-content'
import { useAgentOrchestrateReadOnly } from '../read-only-context'
import { CliToolDialog } from './cli-tool/dialog'
import { AgentCliToolItem } from './cli-tool/item'
import {
useCliToolDialogSurface,
useProviderToolSettingsSurface,
useSelectedProviderTools,
} from './hooks'
import { ProviderToolSettingsDialog } from './provider-tool/dialog'
import { AgentProviderToolItem } from './provider-tool/item'
type DisplayAgentProviderTool = AgentProviderTool & {
isInstalled?: boolean
}
type DisplayAgentTool = AgentCliTool | DisplayAgentProviderTool
const AgentToolItem = memo(
({
tool,
onConfigureAction,
onDeleteCliTool,
onDeleteProviderTool,
onDeleteProviderToolAction,
onEditCliTool,
onCredentialChange,
onPluginInstalled,
defaultExpanded = false,
}: {
tool: DisplayAgentTool
onConfigureAction: (target: ToolSettingTarget) => void
onDeleteCliTool: (toolId: string) => void
onDeleteProviderTool: (toolId: string) => void
onDeleteProviderToolAction: (toolId: string, actionId: string) => void
onEditCliTool: (tool: AgentCliTool) => void
onCredentialChange: (
toolId: string,
credentialId?: string,
credentialType?: AgentProviderTool['credentialType'],
) => void
onPluginInstalled: () => void
defaultExpanded?: boolean
}) => {
const [isExpanded, setIsExpanded] = useState(defaultExpanded)
const handleRemoveProvider = useCallback(() => {
onDeleteProviderTool(tool.id)
}, [onDeleteProviderTool, tool.id])
const handleRemoveProviderAction = useCallback(
(actionId: string) => {
onDeleteProviderToolAction(tool.id, actionId)
},
[onDeleteProviderToolAction, tool.id],
)
const handleDeleteCliTool = useCallback(() => {
onDeleteCliTool(tool.id)
}, [onDeleteCliTool, tool.id])
const handleEditCliTool = useCallback(() => {
if (tool.kind === 'cli') onEditCliTool(tool)
}, [onEditCliTool, tool])
const handleCredentialChange = useCallback(
(credentialId?: string, credentialType?: AgentProviderTool['credentialType']) => {
onCredentialChange(tool.id, credentialId, credentialType)
},
[onCredentialChange, tool.id],
)
if (tool.kind === 'provider') {
return (
<AgentProviderToolItem
tool={tool}
isInstalled={tool.isInstalled}
isExpanded={isExpanded}
onOpenChange={setIsExpanded}
onConfigureAction={onConfigureAction}
onRemoveAction={handleRemoveProviderAction}
onRemoveProvider={handleRemoveProvider}
onCredentialChange={handleCredentialChange}
onInstall={onPluginInstalled}
/>
)
}
return (
<AgentCliToolItem tool={tool} onDelete={handleDeleteCliTool} onEdit={handleEditCliTool} />
)
},
)
function getDisplayCredentialType(
tool: AgentProviderTool,
providerCredentialType: AgentProviderTool['credentialType'],
) {
if (!providerCredentialType) return undefined
if (providerCredentialType === 'oauth2' && tool.credentialType === 'unauthorized')
return 'oauth2' as const
return tool.credentialType ?? providerCredentialType
}
function useDisplayTools(
tools: AgentTool[],
providerById: Map<string, ToolWithProvider>,
resolvedProviderTypes: Set<AgentProviderTool['providerType']>,
toolPresentation: {
language: string
marketplacePluginById?: ReturnType<typeof useAgentToolPresentation>['marketplacePluginById']
},
) {
const { language, marketplacePluginById } = toolPresentation
return useMemo(() => {
return tools.map((tool): DisplayAgentTool => {
if (tool.kind !== 'provider') return tool
const provider = providerById.get(tool.id) ?? providerById.get(tool.name)
if (!provider) {
const providerPluginId = getAgentProviderPluginId(tool)
const marketplacePlugin = marketplacePluginById?.get(providerPluginId)
return {
...tool,
isInstalled: resolvedProviderTypes.has(tool.providerType) ? false : undefined,
pluginId: tool.pluginId ?? providerPluginId,
pluginUniqueIdentifier:
tool.pluginUniqueIdentifier ?? marketplacePlugin?.latest_package_identifier,
displayName: getAgentProviderToolDisplayName({
language,
marketplacePlugin,
tool,
}),
icon: getAgentProviderToolIcon(tool),
}
}
const providerToolByName = new Map(
(provider.tools ?? []).map((providerTool) => [providerTool.name, providerTool]),
)
const providerCredentialType = getProviderCredentialType(provider)
return {
...tool,
isInstalled: true,
displayName: getAgentProviderToolDisplayName({ language, provider, tool }),
icon: getAgentProviderToolIcon(tool, provider),
iconDark: tool.iconDark ?? provider.icon_dark,
providerType: tool.providerType,
allowDelete: tool.allowDelete ?? provider.allow_delete,
credentialKey: providerCredentialType
? (tool.credentialKey ?? 'agentDetail.configure.tools.credential.authOne')
: undefined,
credentialType: getDisplayCredentialType(tool, providerCredentialType),
credentialVariant: getProviderCredentialVariant(tool, provider, providerCredentialType),
actions: tool.actions.map((action) => {
const providerTool = providerToolByName.get(action.toolName)
if (!providerTool) return action
return {
...action,
name:
action.name === action.toolName
? (getLocalizedText(providerTool.label, language) ?? action.name)
: action.name,
description:
action.description || getLocalizedText(providerTool.description, language) || '',
}
}),
} satisfies DisplayAgentProviderTool
})
}, [language, marketplacePluginById, providerById, resolvedProviderTypes, tools])
}
function AddToolMenuItem({
badge,
description,
iconClassName,
label,
onClick,
}: {
badge?: string
description: string
iconClassName: string
label: string
onClick?: () => void
}) {
return (
<button
type="button"
onClick={onClick}
className="flex w-full items-start gap-2 rounded-lg py-2 pr-3 pl-2 text-left hover:bg-state-base-hover focus-visible:bg-state-base-hover focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden"
>
<span
aria-hidden
className={cn('mt-0.5 size-4 shrink-0 text-text-secondary', iconClassName)}
/>
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
<span className="flex min-w-0 items-center gap-1">
<span className="truncate system-sm-semibold text-text-secondary" title={label}>
{label}
</span>
{badge && (
<span className="shrink-0 rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-1 py-0.5 system-2xs-medium-uppercase text-text-tertiary">
{badge}
</span>
)}
</span>
<span className="system-xs-regular text-text-tertiary">{description}</span>
</span>
</button>
)
}
type AddToolMenuView = 'menu' | 'tool-picker'
// CLI tools are not available yet, so open the tool picker directly for now.
// Switch this back to 'menu' when the CLI tool entry returns.
const addToolDefaultView = 'tool-picker' satisfies AddToolMenuView
function AddToolMenu({
onAddCliTool,
onAddTools,
selectedTools,
}: {
onAddCliTool: () => void
onAddTools: (tools: AgentProviderToolDefaultValue[]) => void
selectedTools: ToolValue[]
}) {
const { t } = useTranslation(['agentV2'])
const [open, setOpen] = useState(false)
const [view, setView] = useState<AddToolMenuView>(addToolDefaultView)
const { providerById } = useAgentToolProviderCatalog()
const openToolPicker = useCallback(() => {
setView('tool-picker')
}, [])
const openCliToolDialog = useCallback(() => {
setOpen(false)
onAddCliTool()
}, [onAddCliTool])
const handleOpenChange = useCallback((nextOpen: boolean) => {
setOpen(nextOpen)
if (nextOpen) setView(addToolDefaultView)
}, [])
const toAgentToolDefaultValue = useCallback(
(tool: ToolDefaultValue): AgentProviderToolDefaultValue => ({
...tool,
provider_type: parseToolProviderType(tool.provider_type),
allowDelete: (
providerById.get(tool.provider_id) ??
providerById.get(tool.provider_name) ??
(tool.plugin_id ? providerById.get(tool.plugin_id) : undefined)
)?.allow_delete,
credentialType: getProviderCredentialType(
providerById.get(tool.provider_id) ??
providerById.get(tool.provider_name) ??
(tool.plugin_id ? providerById.get(tool.plugin_id) : undefined),
),
credentialRequired: !!getProviderCredentialType(
providerById.get(tool.provider_id) ??
providerById.get(tool.provider_name) ??
(tool.plugin_id ? providerById.get(tool.plugin_id) : undefined),
),
}),
[providerById],
)
const handleSelectTool = useCallback(
(tool: ToolDefaultValue) => {
onAddTools([toAgentToolDefaultValue(tool)])
},
[onAddTools, toAgentToolDefaultValue],
)
const handleSelectMultipleTools = useCallback(
(tools: ToolDefaultValue[]) => {
onAddTools(tools.map(toAgentToolDefaultValue))
},
[onAddTools, toAgentToolDefaultValue],
)
return (
<Popover open={open} onOpenChange={handleOpenChange}>
<PopoverTrigger
render={
<ConfigureSectionAddButton
ariaLabel={t(($) => $['agentDetail.configure.tools.add'])}
className="data-popup-open:bg-state-base-hover"
/>
}
/>
<PopoverContent
side="top"
align="end"
sideOffset={4}
className={
view === 'menu'
? 'w-70 bg-components-panel-bg-blur p-1 shadow-lg backdrop-blur-[5px]'
: 'w-100 overflow-hidden border-none bg-transparent p-0 shadow-none'
}
>
{view === 'menu' ? (
<>
<AddToolMenuItem
iconClassName="i-ri-box-3-line"
label={t(($) => $['agentDetail.configure.tools.addMenu.tool.label'])}
description={t(($) => $['agentDetail.configure.tools.addMenu.tool.description'])}
onClick={openToolPicker}
/>
{ENABLE_AGENT_CLI_TOOLS && (
<AddToolMenuItem
iconClassName="i-ri-terminal-box-line"
label={t(($) => $['agentDetail.configure.tools.addMenu.cliTool.label'])}
badge={t(($) => $['agentDetail.configure.tools.addMenu.cliTool.badge'])}
description={t(($) => $['agentDetail.configure.tools.addMenu.cliTool.description'])}
onClick={openCliToolDialog}
/>
)}
</>
) : (
<ToolPickerContent
focusSearchOnMount
panelClassName="w-full overflow-hidden"
supportAddCustomTool
selectedTools={selectedTools}
onSelect={handleSelectTool}
onSelectMultiple={handleSelectMultipleTools}
/>
)}
</PopoverContent>
</Popover>
)
}
export function AgentTools() {
const { t } = useTranslation(['agentV2'])
const readOnly = useAgentOrchestrateReadOnly()
const setProviderToolCredential = useSetAtom(setProviderToolCredentialAtom)
const queryClient = useQueryClient()
const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
const { providerById, resolvedProviderTypes } = useAgentToolProviderCatalog()
const tools = useAtomValue(agentComposerToolsAtom)
const selectedTools = useSelectedProviderTools()
const addTools = useSetAtom(addProviderToolsAtom)
const {
settingTarget,
setSettingTarget,
deleteProviderTool,
deleteProviderToolAction,
closeProviderSettingsDialog,
} = useProviderToolSettingsSurface()
const {
isCliToolDialogOpen,
editingCliTool,
deleteCliTool,
openCliToolDialog,
editCliTool,
handleCliDialogSave,
handleCliDialogOpenChange,
} = useCliToolDialogSurface()
const handleProviderCredentialChange = useCallback(
(
toolId: string,
credentialId?: string,
credentialType?: AgentProviderTool['credentialType'],
) => {
setProviderToolCredential({ toolId, credentialId, credentialType })
},
[setProviderToolCredential],
)
const handlePluginInstalled = useCallback(() => {
void Promise.allSettled([
queryClient.invalidateQueries({
queryKey: consoleQuery.workspaces.current.tools.builtin.get.queryKey(),
}),
invalidateInstalledPluginList(PluginCategoryEnum.tool),
])
}, [invalidateInstalledPluginList, queryClient])
const visibleTools = useMemo(
() => (ENABLE_AGENT_CLI_TOOLS ? tools : tools.filter((tool) => tool.kind !== 'cli')),
[tools],
)
const toolPresentation = useAgentToolPresentation(visibleTools, {
providerById,
resolvedProviderTypes,
})
const displayTools = useDisplayTools(
visibleTools,
providerById,
resolvedProviderTypes,
toolPresentation,
)
/*
* knip-ignore-start
* Keep this disabled sync logic while backend credential snapshots are being investigated.
* Re-enabling it writes catalog-derived credential metadata into the composer draft on page entry.
* That can mark a published agent as locally dirty before the user changes anything.
*
* const displayToolById = useMemo(
* () => new Map(displayTools.map(tool => [tool.id, tool])),
* [displayTools],
* )
*
* useEffect(() => {
* if (readOnly)
* return
*
* let shouldSyncCredentials = false
* const nextTools = tools.map((tool) => {
* const displayTool = displayToolById.get(tool.id)
*
* if (tool.kind !== 'provider' || displayTool?.kind !== 'provider')
* return tool
*
* if (
* tool.allowDelete === displayTool.allowDelete
* && tool.credentialKey === displayTool.credentialKey
* && tool.credentialType === displayTool.credentialType
* && tool.credentialVariant === displayTool.credentialVariant
* ) {
* return tool
* }
*
* shouldSyncCredentials = true
* return {
* ...tool,
* allowDelete: displayTool.allowDelete,
* credentialKey: displayTool.credentialKey,
* credentialType: displayTool.credentialType,
* credentialVariant: displayTool.credentialVariant,
* }
* })
*
* if (shouldSyncCredentials)
* setTools(nextTools)
* }, [displayToolById, readOnly, setTools, tools])
* knip-ignore-end
*/
const promptAddCallbackRef = useRef<AgentOrchestrateAddActionOptions['onAdded']>(undefined)
const openCliToolDialogFromPrompt = useCallback(
(options?: AgentOrchestrateAddActionOptions) => {
promptAddCallbackRef.current = options?.onAdded
openCliToolDialog()
},
[openCliToolDialog],
)
const handleCliDialogSaveWithPromptInsert = useCallback(
(tool: AgentCliTool) => {
handleCliDialogSave(tool)
if (!editingCliTool) {
promptAddCallbackRef.current?.([tool])
promptAddCallbackRef.current = undefined
}
},
[editingCliTool, handleCliDialogSave],
)
const handleCliDialogOpenChangeWithPromptInsert = useCallback(
(open: boolean) => {
if (!open) promptAddCallbackRef.current = undefined
handleCliDialogOpenChange(open)
},
[handleCliDialogOpenChange],
)
useRegisterAgentOrchestrateAddAction(
'cli',
ENABLE_AGENT_CLI_TOOLS ? openCliToolDialogFromPrompt : () => {},
)
const toolsListId = 'agent-configure-tools-list'
const settingTargetTool = settingTarget
? tools.find((tool) => tool.kind === 'provider' && tool.id === settingTarget.toolId)
: undefined
const settingTargetCollection = settingTarget
? (providerById.get(settingTarget.toolId) ??
providerById.get(settingTargetTool?.name ?? settingTarget.toolId))
: undefined
return (
<>
<ConfigureSection
label={t(($) => $['agentDetail.configure.tools.label'])}
labelId="agent-configure-tools-label"
panelId={toolsListId}
tip={<AgentConfigureTipContent type="tools" />}
panelContentClassName="flex flex-col gap-1"
actions={
!readOnly ? (
<AddToolMenu
onAddCliTool={openCliToolDialog}
onAddTools={addTools}
selectedTools={selectedTools}
/>
) : undefined
}
>
{displayTools.length === 0 ? (
<ConfigureSectionEmpty
title={t(($) => $['agentDetail.configure.tools.empty.title'])}
description={t(($) => $['agentDetail.configure.tools.empty.description'])}
/>
) : (
displayTools.map((tool) => (
<AgentToolItem
key={tool.id}
tool={tool}
onConfigureAction={setSettingTarget}
onDeleteCliTool={deleteCliTool}
onDeleteProviderTool={deleteProviderTool}
onDeleteProviderToolAction={deleteProviderToolAction}
onEditCliTool={editCliTool}
onCredentialChange={handleProviderCredentialChange}
onPluginInstalled={handlePluginInstalled}
/>
))
)}
</ConfigureSection>
<ProviderToolSettingsDialog
settingTarget={settingTarget}
collection={settingTargetCollection}
onClose={closeProviderSettingsDialog}
/>
{ENABLE_AGENT_CLI_TOOLS && (
<CliToolDialog
key={`${editingCliTool?.id ?? 'add'}:${isCliToolDialogOpen ? 'open' : 'closed'}`}
mode={editingCliTool ? 'edit' : 'add'}
tool={editingCliTool}
onDeleteCliTool={deleteCliTool}
onSaveCliTool={handleCliDialogSaveWithPromptInsert}
open={isCliToolDialogOpen}
onOpenChange={handleCliDialogOpenChangeWithPromptInsert}
/>
)}
</>
)
}
export function AgentTemplateTools() {
const { t } = useTranslation(['agentV2'])
const language = useGetLanguage()
const labelId = useId()
const tools = useAtomValue(agentComposerToolsAtom)
const visibleTools = ENABLE_AGENT_CLI_TOOLS ? tools : tools.filter((tool) => tool.kind !== 'cli')
const providerTypes = new Set(
visibleTools.filter((tool) => tool.kind === 'provider').map((tool) => tool.providerType),
)
const { data: buildInTools } = useQuery(
consoleQuery.workspaces.current.tools.builtin.get.queryOptions({
enabled: providerTypes.has(CollectionType.builtIn) || providerTypes.has('plugin'),
select: selectToolsWithProvider,
}),
)
const { data: customTools } = useQuery(
consoleQuery.workspaces.current.tools.api.get.queryOptions({
enabled: providerTypes.has(CollectionType.custom),
select: selectToolsWithProvider,
}),
)
const { data: workflowTools } = useQuery(
consoleQuery.workspaces.current.tools.workflow.get.queryOptions({
enabled: providerTypes.has(CollectionType.workflow),
select: selectToolsWithProvider,
}),
)
const { data: mcpTools } = useQuery(
consoleQuery.workspaces.current.tools.mcp.get.queryOptions({
enabled: providerTypes.has(CollectionType.mcp),
select: selectToolsWithProvider,
}),
)
const catalog = useMemo(
() => createAgentToolProviderCatalog({ buildInTools, customTools, workflowTools, mcpTools }),
[buildInTools, customTools, workflowTools, mcpTools],
)
const displayTools = useDisplayTools(
visibleTools,
catalog.providerById,
catalog.resolvedProviderTypes,
{ language },
)
return (
<ConfigureSection
label={t(($) => $['agentDetail.configure.tools.label'])}
labelId={labelId}
tip={<AgentConfigureTipContent type="tools" />}
panelContentClassName="flex flex-col gap-1"
>
{displayTools.length === 0 ? (
<ConfigureSectionEmpty
title={t(($) => $['agentDetail.configure.tools.empty.title'])}
description={t(($) => $['agentDetail.configure.tools.empty.description'])}
/>
) : (
displayTools.map((tool, index) => (
<AgentToolItem
key={tool.id}
tool={tool.kind === 'provider' ? { ...tool, isInstalled: undefined } : tool}
defaultExpanded={index === 0}
onConfigureAction={noop}
onDeleteCliTool={noop}
onDeleteProviderTool={noop}
onDeleteProviderToolAction={noop}
onEditCliTool={noop}
onCredentialChange={noop}
onPluginInstalled={noop}
/>
))
)}
</ConfigureSection>
)
}