Skip to content

armgit / dify

Public Imported from GitHub
0

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.

8.0 KB
Raw Blame
import type { RagPipelineDatasourceProviderResponse } from '@dify/contracts/api/console/rag/types.gen'
import type { TriggerWithProvider } from '../block-selector/types'
import type { DataSourceNodeType } from '../nodes/data-source/types'
import type { ToolNodeType } from '../nodes/tool/types'
import type { PluginTriggerNodeType } from '../nodes/trigger-plugin/types'
import type { Node, ToolWithProvider } from '../types'
import { useQuery } from '@tanstack/react-query'
import { useCallback, useMemo } from 'react'
import { resolveDatasourceIcon } from '@/app/components/rag-pipeline/utils/datasource-icon'
import { CollectionType } from '@/app/components/tools/types'
import { selectToolsWithProvider } from '@/app/components/tools/utils/query-select'
import { useResolvedTheme } from '@/features/theme/hooks'
import { consoleQuery } from '@/service/console'
import { useAllTriggerPlugins } from '@/service/use-triggers'
import { canFindTool } from '@/utils'
import { matchesProviderReference } from '@/utils/provider-reference'
import { useStore, useWorkflowStore } from '../store'
import { BlockEnum } from '../types'
import { matchDataSource } from '../utils/plugin-install-check'

const isTriggerPluginNode = (data: Node['data']): data is PluginTriggerNodeType =>
  data.type === BlockEnum.TriggerPlugin

const isToolNode = (data: Node['data']): data is ToolNodeType => data.type === BlockEnum.Tool

const isDataSourceNode = (data: Node['data']): data is DataSourceNodeType =>
  data.type === BlockEnum.DataSource

type IconValue = ToolWithProvider['icon']
export type ToolIconCollections = {
  buildInTools?: ToolWithProvider[]
  customTools?: ToolWithProvider[]
  workflowTools?: ToolWithProvider[]
  mcpTools?: ToolWithProvider[]
}

const resolveIconByTheme = (
  currentTheme: string | undefined,
  icon?: IconValue,
  iconDark?: IconValue,
) => {
  if (currentTheme === 'dark' && iconDark) return iconDark
  return icon
}

const findTriggerPluginIcon = (
  identifiers: (string | undefined)[],
  triggers: TriggerWithProvider[] | undefined,
  currentTheme?: string,
) => {
  const targetTriggers = triggers || []
  for (const identifier of identifiers) {
    if (!identifier) continue
    const matched = targetTriggers.find(
      (trigger) => trigger.id === identifier || canFindTool(trigger.id, identifier),
    )
    if (matched) return resolveIconByTheme(currentTheme, matched.icon, matched.icon_dark)
  }
  return undefined
}

const getPrimaryToolCollection = (
  providerType: CollectionType | undefined,
  collections: ToolIconCollections,
) => {
  switch (providerType) {
    case CollectionType.custom:
      return collections.customTools
    case CollectionType.mcp:
      return collections.mcpTools
    case CollectionType.workflow:
      return collections.workflowTools
    case CollectionType.builtIn:
    default:
      return collections.buildInTools
  }
}

const getCollectionsToSearch = (
  providerType: CollectionType | undefined,
  collections: ToolIconCollections,
) => {
  return [
    getPrimaryToolCollection(providerType, collections),
    collections.buildInTools,
    collections.customTools,
    collections.workflowTools,
    collections.mcpTools,
  ] as Array<ToolWithProvider[] | undefined>
}

const findToolInCollections = (
  collections: Array<ToolWithProvider[] | undefined>,
  data: ToolNodeType,
) => {
  const seen = new Set<ToolWithProvider[]>()

  for (const collection of collections) {
    if (!collection || seen.has(collection)) continue

    seen.add(collection)
    const matched = collection.find((toolWithProvider) => {
      if (matchesProviderReference(toolWithProvider, data.provider_id)) return true
      if (data.plugin_id && toolWithProvider.plugin_id === data.plugin_id) return true
      return data.provider_name === toolWithProvider.name
    })

    if (matched) return matched
  }

  return undefined
}

const findToolNodeIcon = ({
  data,
  collections,
  theme,
}: {
  data: ToolNodeType
  collections: ToolIconCollections
  theme?: string
}) => {
  const matched = findToolInCollections(
    getCollectionsToSearch(data.provider_type, collections),
    data,
  )
  if (matched) {
    const matchedIcon = resolveIconByTheme(theme, matched.icon, matched.icon_dark)
    if (matchedIcon) return matchedIcon
  }

  return resolveIconByTheme(theme, data.provider_icon, data.provider_icon_dark)
}

const findDataSourceIcon = (
  data: DataSourceNodeType,
  dataSourceList?: RagPipelineDatasourceProviderResponse[],
) => {
  const provider = matchDataSource(dataSourceList ?? [], data)
  return provider ? resolveDatasourceIcon(provider.declaration.identity.icon) : undefined
}

export const findNodeIcon = ({
  data,
  collections,
  dataSourceList,
  triggerPlugins,
  theme,
}: {
  data?: Node['data']
  collections: ToolIconCollections
  dataSourceList?: RagPipelineDatasourceProviderResponse[]
  triggerPlugins?: TriggerWithProvider[]
  theme?: string
}) => {
  if (!data) return undefined

  if (isTriggerPluginNode(data)) {
    return findTriggerPluginIcon(
      [data.plugin_id, data.provider_id, data.provider_name],
      triggerPlugins,
      theme,
    )
  }

  if (isToolNode(data)) return findToolNodeIcon({ data, collections, theme })

  if (isDataSourceNode(data)) return findDataSourceIcon(data, dataSourceList)

  return undefined
}

export const useToolIcon = (data?: Node['data']) => {
  const { data: buildInTools } = useQuery(
    consoleQuery.workspaces.current.tools.builtin.get.queryOptions({
      select: selectToolsWithProvider,
    }),
  )
  const { data: customTools } = useQuery(
    consoleQuery.workspaces.current.tools.api.get.queryOptions({ select: selectToolsWithProvider }),
  )
  const { data: workflowTools } = useQuery(
    consoleQuery.workspaces.current.tools.workflow.get.queryOptions({
      select: selectToolsWithProvider,
    }),
  )
  const { data: mcpTools } = useQuery(
    consoleQuery.workspaces.current.tools.mcp.get.queryOptions({ select: selectToolsWithProvider }),
  )
  const dataSourceList = useStore((s) => s.dataSourceList)
  const { data: triggerPlugins } = useAllTriggerPlugins()
  const theme = useResolvedTheme()

  const toolIcon = useMemo(() => {
    return (
      findNodeIcon({
        data,
        collections: {
          buildInTools,
          customTools,
          workflowTools,
          mcpTools,
        },
        dataSourceList,
        triggerPlugins,
        theme,
      }) || ''
    )
  }, [
    data,
    dataSourceList,
    buildInTools,
    customTools,
    workflowTools,
    mcpTools,
    triggerPlugins,
    theme,
  ])

  return toolIcon
}

export const useGetToolIcon = () => {
  const { data: buildInTools } = useQuery(
    consoleQuery.workspaces.current.tools.builtin.get.queryOptions({
      select: selectToolsWithProvider,
    }),
  )
  const { data: customTools } = useQuery(
    consoleQuery.workspaces.current.tools.api.get.queryOptions({ select: selectToolsWithProvider }),
  )
  const { data: workflowTools } = useQuery(
    consoleQuery.workspaces.current.tools.workflow.get.queryOptions({
      select: selectToolsWithProvider,
    }),
  )
  const { data: mcpTools } = useQuery(
    consoleQuery.workspaces.current.tools.mcp.get.queryOptions({ select: selectToolsWithProvider }),
  )
  const { data: triggerPlugins } = useAllTriggerPlugins()
  const workflowStore = useWorkflowStore()
  const theme = useResolvedTheme()

  const getToolIcon = useCallback(
    (data: Node['data']) => {
      const {
        buildInTools: storeBuiltInTools,
        customTools: storeCustomTools,
        workflowTools: storeWorkflowTools,
        mcpTools: storeMcpTools,
        dataSourceList,
      } = workflowStore.getState()

      return findNodeIcon({
        data,
        collections: {
          buildInTools: storeBuiltInTools ?? buildInTools,
          customTools: storeCustomTools ?? customTools,
          workflowTools: storeWorkflowTools ?? workflowTools,
          mcpTools: storeMcpTools ?? mcpTools,
        },
        dataSourceList,
        triggerPlugins,
        theme,
      })
    },
    [workflowStore, triggerPlugins, buildInTools, customTools, workflowTools, mcpTools, theme],
  )

  return getToolIcon
}