Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tools.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {
  2. memo,
  3. useMemo,
  4. useRef,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import type { BlockEnum, ToolWithProvider } from '../types'
  8. import IndexBar, { groupItems } from './index-bar'
  9. import type { ToolDefaultValue, ToolValue } from './types'
  10. import type { ToolTypeEnum } from './types'
  11. import { ViewType } from './view-type-select'
  12. import Empty from '@/app/components/tools/add-tool-modal/empty'
  13. import { useGetLanguage } from '@/context/i18n'
  14. import ToolListTreeView from './tool/tool-list-tree-view/list'
  15. import ToolListFlatView from './tool/tool-list-flat-view/list'
  16. import classNames from '@/utils/classnames'
  17. type ToolsProps = {
  18. onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
  19. canNotSelectMultiple?: boolean
  20. onSelectMultiple?: (type: BlockEnum, tools: ToolDefaultValue[]) => void
  21. tools: ToolWithProvider[]
  22. viewType: ViewType
  23. hasSearchText: boolean
  24. toolType?: ToolTypeEnum
  25. isAgent?: boolean
  26. className?: string
  27. indexBarClassName?: string
  28. selectedTools?: ToolValue[]
  29. canChooseMCPTool?: boolean
  30. }
  31. const Blocks = ({
  32. onSelect,
  33. canNotSelectMultiple,
  34. onSelectMultiple,
  35. tools,
  36. viewType,
  37. hasSearchText,
  38. toolType,
  39. isAgent,
  40. className,
  41. indexBarClassName,
  42. selectedTools,
  43. canChooseMCPTool,
  44. }: ToolsProps) => {
  45. // const tools: any = []
  46. const { t } = useTranslation()
  47. const language = useGetLanguage()
  48. const isFlatView = viewType === ViewType.flat
  49. const isShowLetterIndex = isFlatView && tools.length > 10
  50. /*
  51. treeViewToolsData:
  52. {
  53. A: {
  54. 'google': [ // plugin organize name
  55. ...tools
  56. ],
  57. 'custom': [ // custom tools
  58. ...tools
  59. ],
  60. 'workflow': [ // workflow as tools
  61. ...tools
  62. ]
  63. }
  64. }
  65. */
  66. const { letters, groups: withLetterAndGroupViewToolsData } = groupItems(tools, tool => tool.label[language][0])
  67. const treeViewToolsData = useMemo(() => {
  68. const result: Record<string, ToolWithProvider[]> = {}
  69. Object.keys(withLetterAndGroupViewToolsData).forEach((letter) => {
  70. Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
  71. if (!result[groupName])
  72. result[groupName] = []
  73. result[groupName].push(...withLetterAndGroupViewToolsData[letter][groupName])
  74. })
  75. })
  76. return result
  77. }, [withLetterAndGroupViewToolsData])
  78. const listViewToolData = useMemo(() => {
  79. const result: ToolWithProvider[] = []
  80. letters.forEach((letter) => {
  81. Object.keys(withLetterAndGroupViewToolsData[letter]).forEach((groupName) => {
  82. result.push(...withLetterAndGroupViewToolsData[letter][groupName].map((item) => {
  83. return {
  84. ...item,
  85. letter,
  86. }
  87. }))
  88. })
  89. })
  90. return result
  91. }, [withLetterAndGroupViewToolsData, letters])
  92. const toolRefs = useRef({})
  93. return (
  94. <div className={classNames('max-w-[100%] p-1', className)}>
  95. {
  96. !tools.length && hasSearchText && (
  97. <div className='mt-2 flex h-[22px] items-center px-3 text-xs font-medium text-text-secondary'>{t('workflow.tabs.noResult')}</div>
  98. )
  99. }
  100. {!tools.length && !hasSearchText && (
  101. <div className='py-10'>
  102. <Empty type={toolType!} isAgent={isAgent}/>
  103. </div>
  104. )}
  105. {!!tools.length && (
  106. isFlatView ? (
  107. <ToolListFlatView
  108. toolRefs={toolRefs}
  109. letters={letters}
  110. payload={listViewToolData}
  111. isShowLetterIndex={isShowLetterIndex}
  112. hasSearchText={hasSearchText}
  113. onSelect={onSelect}
  114. canNotSelectMultiple={canNotSelectMultiple}
  115. onSelectMultiple={onSelectMultiple}
  116. selectedTools={selectedTools}
  117. canChooseMCPTool={canChooseMCPTool}
  118. indexBar={<IndexBar letters={letters} itemRefs={toolRefs} className={indexBarClassName} />}
  119. />
  120. ) : (
  121. <ToolListTreeView
  122. payload={treeViewToolsData}
  123. hasSearchText={hasSearchText}
  124. onSelect={onSelect}
  125. canNotSelectMultiple={canNotSelectMultiple}
  126. onSelectMultiple={onSelectMultiple}
  127. selectedTools={selectedTools}
  128. canChooseMCPTool={canChooseMCPTool}
  129. />
  130. )
  131. )}
  132. </div>
  133. )
  134. }
  135. export default memo(Blocks)