You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import { useContext } from 'use-context-selector'
  7. import produce from 'immer'
  8. import { useFormattingChangedDispatcher } from '../../../debug/hooks'
  9. import SettingBuiltInTool from './setting-built-in-tool'
  10. import Panel from '@/app/components/app/configuration/base/feature-panel'
  11. import Tooltip from '@/app/components/base/tooltip'
  12. import { HelpCircle, InfoCircle, Trash03 } from '@/app/components/base/icons/src/vender/line/general'
  13. import OperationBtn from '@/app/components/app/configuration/base/operation-btn'
  14. import { ToolsActive } from '@/app/components/base/icons/src/public/header-nav/tools'
  15. import AppIcon from '@/app/components/base/app-icon'
  16. import Switch from '@/app/components/base/switch'
  17. import ConfigContext from '@/context/debug-configuration'
  18. import type { AgentTool } from '@/types/app'
  19. import { type Collection, CollectionType } from '@/app/components/tools/types'
  20. import { MAX_TOOLS_NUM } from '@/config'
  21. import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  22. import TooltipPlus from '@/app/components/base/tooltip-plus'
  23. import { DefaultToolIcon } from '@/app/components/base/icons/src/public/other'
  24. import AddToolModal from '@/app/components/tools/add-tool-modal'
  25. type AgentToolWithMoreInfo = AgentTool & { icon: any; collection?: Collection } | null
  26. const AgentTools: FC = () => {
  27. const { t } = useTranslation()
  28. const [isShowChooseTool, setIsShowChooseTool] = useState(false)
  29. const { modelConfig, setModelConfig, collectionList } = useContext(ConfigContext)
  30. const formattingChangedDispatcher = useFormattingChangedDispatcher()
  31. const [currentTool, setCurrentTool] = useState<AgentToolWithMoreInfo>(null)
  32. const [isShowSettingTool, setIsShowSettingTool] = useState(false)
  33. const tools = (modelConfig?.agentConfig?.tools as AgentTool[] || []).map((item) => {
  34. const collection = collectionList.find(collection => collection.id === item.provider_id && collection.type === item.provider_type)
  35. const icon = collection?.icon
  36. return {
  37. ...item,
  38. icon,
  39. collection,
  40. }
  41. })
  42. const handleToolSettingChange = (value: Record<string, any>) => {
  43. const newModelConfig = produce(modelConfig, (draft) => {
  44. const tool = (draft.agentConfig.tools).find((item: any) => item.provider_id === currentTool?.collection?.id && item.tool_name === currentTool?.tool_name)
  45. if (tool)
  46. (tool as AgentTool).tool_parameters = value
  47. })
  48. setModelConfig(newModelConfig)
  49. setIsShowSettingTool(false)
  50. formattingChangedDispatcher()
  51. }
  52. return (
  53. <>
  54. <Panel
  55. className="mt-4"
  56. noBodySpacing={tools.length === 0}
  57. headerIcon={
  58. <ToolsActive className='w-4 h-4 text-primary-500' />
  59. }
  60. title={
  61. <div className='flex items-center'>
  62. <div className='mr-1'>{t('appDebug.agent.tools.name')}</div>
  63. <Tooltip htmlContent={<div className='w-[180px]'>
  64. {t('appDebug.agent.tools.description')}
  65. </div>} selector='config-tools-tooltip'>
  66. <HelpCircle className='w-[14px] h-[14px] text-gray-400' />
  67. </Tooltip>
  68. </div>
  69. }
  70. headerRight={
  71. <div className='flex items-center'>
  72. <div className='leading-[18px] text-xs font-normal text-gray-500'>{tools.filter((item: any) => !!item.enabled).length}/{tools.length}&nbsp;{t('appDebug.agent.tools.enabled')}</div>
  73. {tools.length < MAX_TOOLS_NUM && (
  74. <>
  75. <div className='ml-3 mr-1 h-3.5 w-px bg-gray-200'></div>
  76. <OperationBtn type="add" onClick={() => setIsShowChooseTool(true)} />
  77. </>
  78. )}
  79. </div>
  80. }
  81. >
  82. <div className='grid gap-1 grid-cols-1 2xl:grid-cols-2 items-center flex-wrap justify-between'>
  83. {tools.map((item: AgentTool & { icon: any; collection?: Collection }, index) => (
  84. <div key={index}
  85. className={cn((item.isDeleted || item.notAuthor) ? 'bg-white/50' : 'bg-white', (item.enabled && !item.isDeleted && !item.notAuthor) && 'shadow-xs', index > 1 && 'mt-1', 'group relative flex justify-between items-center last-of-type:mb-0 pl-2.5 py-2 pr-3 w-full rounded-lg border-[0.5px] border-gray-200 ')}
  86. >
  87. <div className='grow w-0 flex items-center'>
  88. {(item.isDeleted || item.notAuthor)
  89. ? (
  90. <DefaultToolIcon className='w-6 h-6' />
  91. )
  92. : (
  93. typeof item.icon === 'string'
  94. ? (
  95. <div
  96. className='w-6 h-6 bg-cover bg-center rounded-md'
  97. style={{
  98. backgroundImage: `url(${item.icon})`,
  99. }}
  100. ></div>
  101. )
  102. : (
  103. <AppIcon
  104. className='rounded-md'
  105. size='tiny'
  106. icon={item.icon?.content}
  107. background={item.icon?.background}
  108. />
  109. ))}
  110. <div
  111. className={cn((item.isDeleted || item.notAuthor) ? 'line-through opacity-50' : '', 'grow w-0 ml-2 leading-[18px] text-[13px] font-medium text-gray-800 truncate')}
  112. >
  113. <span className='text-gray-800 pr-2'>{item.provider_type === CollectionType.builtIn ? item.provider_name : item.tool_label}</span>
  114. <TooltipPlus
  115. popupContent={t('tools.toolNameUsageTip')}
  116. >
  117. <span className='text-gray-500'>{item.tool_name}</span>
  118. </TooltipPlus>
  119. </div>
  120. </div>
  121. <div className='shrink-0 ml-1 flex items-center'>
  122. {(item.isDeleted || item.notAuthor)
  123. ? (
  124. <div className='flex items-center'>
  125. <TooltipPlus
  126. popupContent={t(`tools.${item.isDeleted ? 'toolRemoved' : 'notAuthorized'}`)}
  127. >
  128. <div className='mr-1 p-1 rounded-md hover:bg-black/5 cursor-pointer' onClick={() => {
  129. if (item.notAuthor)
  130. setIsShowChooseTool(true)
  131. }}>
  132. <AlertTriangle className='w-4 h-4 text-[#F79009]' />
  133. </div>
  134. </TooltipPlus>
  135. <div className='p-1 rounded-md hover:bg-black/5 cursor-pointer' onClick={() => {
  136. const newModelConfig = produce(modelConfig, (draft) => {
  137. draft.agentConfig.tools.splice(index, 1)
  138. })
  139. setModelConfig(newModelConfig)
  140. formattingChangedDispatcher()
  141. }}>
  142. <Trash03 className='w-4 h-4 text-gray-500' />
  143. </div>
  144. <div className='ml-2 mr-3 w-px h-3.5 bg-gray-200'></div>
  145. </div>
  146. )
  147. : (
  148. <div className='hidden group-hover:flex items-center'>
  149. <TooltipPlus
  150. popupContent={t('tools.setBuiltInTools.infoAndSetting')}
  151. >
  152. <div className='mr-1 p-1 rounded-md hover:bg-black/5 cursor-pointer' onClick={() => {
  153. setCurrentTool(item)
  154. setIsShowSettingTool(true)
  155. }}>
  156. <InfoCircle className='w-4 h-4 text-gray-500' />
  157. </div>
  158. </TooltipPlus>
  159. <div className='p-1 rounded-md hover:bg-black/5 cursor-pointer' onClick={() => {
  160. const newModelConfig = produce(modelConfig, (draft) => {
  161. draft.agentConfig.tools.splice(index, 1)
  162. })
  163. setModelConfig(newModelConfig)
  164. formattingChangedDispatcher()
  165. }}>
  166. <Trash03 className='w-4 h-4 text-gray-500' />
  167. </div>
  168. <div className='ml-2 mr-3 w-px h-3.5 bg-gray-200'></div>
  169. </div>
  170. )}
  171. <div className={cn((item.isDeleted || item.notAuthor) && 'opacity-50')}>
  172. <Switch
  173. defaultValue={(item.isDeleted || item.notAuthor) ? false : item.enabled}
  174. disabled={(item.isDeleted || item.notAuthor)}
  175. size='md'
  176. onChange={(enabled) => {
  177. const newModelConfig = produce(modelConfig, (draft) => {
  178. (draft.agentConfig.tools[index] as any).enabled = enabled
  179. })
  180. setModelConfig(newModelConfig)
  181. formattingChangedDispatcher()
  182. }} />
  183. </div>
  184. </div>
  185. </div>
  186. ))}
  187. </div >
  188. </Panel >
  189. {isShowChooseTool && (
  190. <AddToolModal onHide={() => setIsShowChooseTool(false)} />
  191. )}
  192. {
  193. isShowSettingTool && (
  194. <SettingBuiltInTool
  195. toolName={currentTool?.tool_name as string}
  196. setting={currentTool?.tool_parameters as any}
  197. collection={currentTool?.collection as Collection}
  198. isBuiltIn={currentTool?.collection?.type === CollectionType.builtIn}
  199. isModel={currentTool?.collection?.type === CollectionType.model}
  200. onSave={handleToolSettingChange}
  201. onHide={() => setIsShowSettingTool(false)}
  202. />)
  203. }
  204. </>
  205. )
  206. }
  207. export default React.memo(AgentTools)