tools.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. memo,
  3. useCallback,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiAddLine,
  8. } from '@remixicon/react'
  9. import cn from '@/utils/classnames'
  10. import { ArrowUpRight } from '@/app/components/base/icons/src/vender/line/arrows'
  11. import { Check } from '@/app/components/base/icons/src/vender/line/general'
  12. import { Tag01 } from '@/app/components/base/icons/src/vender/line/financeAndECommerce'
  13. import type { ToolWithProvider } from '@/app/components/workflow/types'
  14. import { BlockEnum } from '@/app/components/workflow/types'
  15. import BlockIcon from '@/app/components/workflow/block-icon'
  16. import Tooltip from '@/app/components/base/tooltip'
  17. import Button from '@/app/components/base/button'
  18. import { useGetLanguage } from '@/context/i18n'
  19. import { useStore as useLabelStore } from '@/app/components/tools/labels/store'
  20. import Empty from '@/app/components/tools/add-tool-modal/empty'
  21. import type { Tool } from '@/app/components/tools/types'
  22. import { CollectionType } from '@/app/components/tools/types'
  23. import type { AgentTool } from '@/types/app'
  24. import { MAX_TOOLS_NUM } from '@/config'
  25. type ToolsProps = {
  26. showWorkflowEmpty: boolean
  27. tools: ToolWithProvider[]
  28. addedTools: AgentTool[]
  29. onSelect: (provider: ToolWithProvider, tool: Tool) => void
  30. onAuthSetup: (provider: ToolWithProvider) => void
  31. }
  32. const Blocks = ({
  33. showWorkflowEmpty,
  34. tools,
  35. addedTools,
  36. onSelect,
  37. onAuthSetup,
  38. }: ToolsProps) => {
  39. const { t } = useTranslation()
  40. const language = useGetLanguage()
  41. const labelList = useLabelStore(s => s.labelList)
  42. const addable = addedTools.length < MAX_TOOLS_NUM
  43. const renderGroup = useCallback((toolWithProvider: ToolWithProvider) => {
  44. const list = toolWithProvider.tools
  45. const needAuth = toolWithProvider.allow_delete && !toolWithProvider.is_team_authorization && toolWithProvider.type === CollectionType.builtIn
  46. return (
  47. <div
  48. key={toolWithProvider.id}
  49. className='group mb-1 last-of-type:mb-0'
  50. >
  51. <div className='flex items-center justify-between w-full pl-3 pr-1 h-[22px] text-xs font-medium text-gray-500'>
  52. {toolWithProvider.label[language]}
  53. <a className='hidden cursor-pointer items-center group-hover:flex' href={`/tools?category=${toolWithProvider.type}`} target='_blank'>{t('tools.addToolModal.manageInTools')}<ArrowUpRight className='ml-0.5 w-3 h-3' /></a>
  54. </div>
  55. {list.map((tool) => {
  56. const labelContent = (() => {
  57. if (!tool.labels)
  58. return ''
  59. return tool.labels.map((name) => {
  60. const label = labelList.find(item => item.name === name)
  61. return label?.label[language]
  62. }).filter(Boolean).join(', ')
  63. })()
  64. const added = !!addedTools?.find(v => v.provider_id === toolWithProvider.id && v.provider_type === toolWithProvider.type && v.tool_name === tool.name)
  65. return (
  66. <Tooltip
  67. key={tool.name}
  68. selector={`workflow-block-tool-${tool.name}`}
  69. position='bottom'
  70. className='!p-0 !px-3 !py-2.5 !w-[210px] !leading-[18px] !text-xs !text-gray-700 !border-[0.5px] !border-black/5 !bg-transparent !rounded-xl !shadow-lg translate-x-[108px]'
  71. htmlContent={(
  72. <div>
  73. <BlockIcon
  74. size='md'
  75. className='mb-2'
  76. type={BlockEnum.Tool}
  77. toolIcon={toolWithProvider.icon}
  78. />
  79. <div className='mb-1 text-sm leading-5 text-gray-900'>{tool.label[language]}</div>
  80. <div className='text-xs text-gray-700 leading-[18px]'>{tool.description[language]}</div>
  81. {tool.labels?.length > 0 && (
  82. <div className='flex items-center shrink-0 mt-1'>
  83. <div className='relative w-full flex items-center gap-1 py-1 rounded-md text-gray-500' title={labelContent}>
  84. <Tag01 className='shrink-0 w-3 h-3 text-gray-500' />
  85. <div className='grow text-xs text-start leading-[18px] font-normal truncate'>{labelContent}</div>
  86. </div>
  87. </div>
  88. )}
  89. </div>
  90. )}
  91. noArrow
  92. >
  93. <div className='group/item flex items-center w-full pl-3 pr-1 h-8 rounded-lg hover:bg-gray-50 cursor-pointer'>
  94. <BlockIcon
  95. className={cn('mr-2 shrink-0', needAuth && 'opacity-30')}
  96. type={BlockEnum.Tool}
  97. toolIcon={toolWithProvider.icon}
  98. />
  99. <div className={cn('grow text-sm text-gray-900 truncate', needAuth && 'opacity-30')}>{tool.label[language]}</div>
  100. {!needAuth && added && (
  101. <div className='flex items-center gap-1 rounded-[6px] border border-gray-100 px-2 py-[3px] bg-white text-gray-300 text-xs font-medium leading-[18px]'>
  102. <Check className='w-3 h-3' />
  103. {t('tools.addToolModal.added').toLocaleUpperCase()}
  104. </div>
  105. )}
  106. {!needAuth && !added && addable && (
  107. <Button
  108. variant='secondary-accent'
  109. size='small'
  110. className={cn('hidden shrink-0 items-center group-hover/item:flex')}
  111. onClick={() => onSelect(toolWithProvider, tool)}
  112. >
  113. <RiAddLine className='w-3 h-3' />
  114. {t('tools.addToolModal.add').toLocaleUpperCase()}
  115. </Button>
  116. )}
  117. {needAuth && (
  118. <Button
  119. variant='secondary-accent'
  120. size='small'
  121. className={cn('hidden shrink-0 group-hover/item:flex')}
  122. onClick={() => onAuthSetup(toolWithProvider)}
  123. >{t('tools.auth.setup')}</Button>
  124. )}
  125. </div>
  126. </Tooltip>
  127. )
  128. })}
  129. </div>
  130. )
  131. }, [addable, language, t, labelList, addedTools, onAuthSetup, onSelect])
  132. return (
  133. <div className='p-1 pb-6 max-w-[440px]'>
  134. {!tools.length && !showWorkflowEmpty && (
  135. <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>{t('workflow.tabs.noResult')}</div>
  136. )}
  137. {!tools.length && showWorkflowEmpty && (
  138. <div className='pt-[280px]'>
  139. <Empty />
  140. </div>
  141. )}
  142. {!!tools.length && tools.map(renderGroup)}
  143. </div>
  144. )
  145. }
  146. export default memo(Blocks)