index.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import {
  4. RiAddLine,
  5. RiArrowDropDownLine,
  6. RiQuestionLine,
  7. } from '@remixicon/react'
  8. import ToolSelector from '@/app/components/plugins/plugin-detail-panel/tool-selector'
  9. import ActionButton from '@/app/components/base/action-button'
  10. import Tooltip from '@/app/components/base/tooltip'
  11. import Divider from '@/app/components/base/divider'
  12. import type { ToolValue } from '@/app/components/workflow/block-selector/types'
  13. import type { Node } from 'reactflow'
  14. import type { NodeOutPutVar } from '@/app/components/workflow/types'
  15. import cn from '@/utils/classnames'
  16. type Props = {
  17. disabled?: boolean
  18. value: ToolValue[]
  19. label: string
  20. required?: boolean
  21. tooltip?: any
  22. supportCollapse?: boolean
  23. scope?: string
  24. onChange: (value: ToolValue[]) => void
  25. nodeOutputVars: NodeOutPutVar[],
  26. availableNodes: Node[],
  27. nodeId?: string
  28. }
  29. const MultipleToolSelector = ({
  30. disabled,
  31. value = [],
  32. label,
  33. required,
  34. tooltip,
  35. supportCollapse,
  36. scope,
  37. onChange,
  38. nodeOutputVars,
  39. availableNodes,
  40. nodeId,
  41. }: Props) => {
  42. const { t } = useTranslation()
  43. const enabledCount = value.filter(item => item.enabled).length
  44. // collapse control
  45. const [collapse, setCollapse] = React.useState(false)
  46. const handleCollapse = () => {
  47. if (supportCollapse)
  48. setCollapse(!collapse)
  49. }
  50. // add tool
  51. const [open, setOpen] = React.useState(false)
  52. const [panelShowState, setPanelShowState] = React.useState(true)
  53. const handleAdd = (val: ToolValue) => {
  54. const newValue = [...value, val]
  55. // deduplication
  56. const deduplication = newValue.reduce((acc, cur) => {
  57. if (!acc.find(item => item.provider_name === cur.provider_name && item.tool_name === cur.tool_name))
  58. acc.push(cur)
  59. return acc
  60. }, [] as ToolValue[])
  61. // update value
  62. onChange(deduplication)
  63. setOpen(false)
  64. }
  65. // delete tool
  66. const handleDelete = (index: number) => {
  67. const newValue = [...value]
  68. newValue.splice(index, 1)
  69. onChange(newValue)
  70. }
  71. // configure tool
  72. const handleConfigure = (val: ToolValue, index: number) => {
  73. const newValue = [...value]
  74. newValue[index] = val
  75. onChange(newValue)
  76. }
  77. return (
  78. <>
  79. <div className='flex items-center mb-1'>
  80. <div
  81. className={cn('relative grow flex items-center gap-0.5', supportCollapse && 'cursor-pointer')}
  82. onClick={handleCollapse}
  83. >
  84. <div className='h-6 flex items-center text-text-secondary system-sm-semibold-uppercase'>{label}</div>
  85. {required && <div className='text-red-500'>*</div>}
  86. {tooltip && (
  87. <Tooltip
  88. popupContent={tooltip}
  89. needsDelay
  90. >
  91. <div><RiQuestionLine className='w-3.5 h-3.5 text-text-quaternary hover:text-text-tertiary' /></div>
  92. </Tooltip>
  93. )}
  94. {supportCollapse && (
  95. <div className='absolute -left-4 top-1'>
  96. <RiArrowDropDownLine
  97. className={cn(
  98. 'w-4 h-4 text-text-tertiary',
  99. collapse && 'transform -rotate-90',
  100. )}
  101. />
  102. </div>
  103. )}
  104. </div>
  105. {value.length > 0 && (
  106. <>
  107. <div className='flex items-center gap-1 text-text-tertiary system-xs-medium'>
  108. <span>{`${enabledCount}/${value.length}`}</span>
  109. <span>{t('appDebug.agent.tools.enabled')}</span>
  110. </div>
  111. <Divider type='vertical' className='ml-3 mr-1 h-3' />
  112. </>
  113. )}
  114. {!disabled && (
  115. <ActionButton className='mx-1' onClick={() => {
  116. setOpen(!open)
  117. setPanelShowState(true)
  118. }}>
  119. <RiAddLine className='w-4 h-4' />
  120. </ActionButton>
  121. )}
  122. </div>
  123. {!collapse && (
  124. <>
  125. <ToolSelector
  126. nodeId={nodeId}
  127. nodeOutputVars={nodeOutputVars}
  128. availableNodes={availableNodes}
  129. scope={scope}
  130. value={undefined}
  131. selectedTools={value}
  132. onSelect={handleAdd}
  133. controlledState={open}
  134. onControlledStateChange={setOpen}
  135. trigger={
  136. <div className=''></div>
  137. }
  138. panelShowState={panelShowState}
  139. onPanelShowStateChange={setPanelShowState}
  140. />
  141. {value.length === 0 && (
  142. <div className='p-3 flex justify-center rounded-[10px] bg-background-section text-text-tertiary system-xs-regular'>{t('plugin.detailPanel.toolSelector.empty')}</div>
  143. )}
  144. {value.length > 0 && value.map((item, index) => (
  145. <div className='mb-1' key={index}>
  146. <ToolSelector
  147. nodeId={nodeId}
  148. nodeOutputVars={nodeOutputVars}
  149. availableNodes={availableNodes}
  150. scope={scope}
  151. value={item}
  152. selectedTools={value}
  153. onSelect={item => handleConfigure(item, index)}
  154. onDelete={() => handleDelete(index)}
  155. supportEnableSwitch
  156. />
  157. </div>
  158. ))}
  159. </>
  160. )}
  161. </>
  162. )
  163. }
  164. export default MultipleToolSelector