您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 9.8KB

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