index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import Button from '../base/button'
  7. import { Plus } from '../base/icons/src/vender/line/general'
  8. import Toast from '../base/toast'
  9. import type { Collection, CustomCollectionBackend, Tool } from './types'
  10. import { CollectionType, LOC } from './types'
  11. import ToolNavList from './tool-nav-list'
  12. import Search from './search'
  13. import Contribute from './contribute'
  14. import ToolList from './tool-list'
  15. import EditCustomToolModal from './edit-custom-collection-modal'
  16. import NoCustomTool from './info/no-custom-tool'
  17. import NoSearchRes from './info/no-search-res'
  18. import TabSlider from '@/app/components/base/tab-slider'
  19. import { createCustomCollection, fetchCollectionList as doFetchCollectionList, fetchBuiltInToolList, fetchCustomToolList } from '@/service/tools'
  20. import type { AgentTool } from '@/types/app'
  21. type Props = {
  22. loc: LOC
  23. addedTools?: AgentTool[]
  24. onAddTool?: (collection: Collection, payload: Tool) => void
  25. selectedProviderId?: string
  26. }
  27. const Tools: FC<Props> = ({
  28. loc,
  29. addedTools,
  30. onAddTool,
  31. selectedProviderId,
  32. }) => {
  33. const { t } = useTranslation()
  34. const isInToolsPage = loc === LOC.tools
  35. const isInDebugPage = !isInToolsPage
  36. const [collectionList, setCollectionList] = useState<Collection[]>([])
  37. const [currCollectionIndex, setCurrCollectionIndex] = useState<number | null>(null)
  38. const [isDetailLoading, setIsDetailLoading] = useState(false)
  39. const fetchCollectionList = async () => {
  40. const list = await doFetchCollectionList() as Collection[]
  41. setCollectionList(list)
  42. if (list.length > 0 && currCollectionIndex === null) {
  43. let index = 0
  44. if (selectedProviderId)
  45. index = list.findIndex(item => item.id === selectedProviderId)
  46. setCurrCollectionIndex(index || 0)
  47. }
  48. }
  49. useEffect(() => {
  50. fetchCollectionList()
  51. }, [])
  52. const collectionTypeOptions = (() => {
  53. const res = [
  54. { value: CollectionType.builtIn, text: t('tools.type.builtIn') },
  55. { value: CollectionType.custom, text: t('tools.type.custom') },
  56. ]
  57. if (!isInToolsPage)
  58. res.unshift({ value: CollectionType.all, text: t('tools.type.all') })
  59. return res
  60. })()
  61. const [query, setQuery] = useState('')
  62. const [collectionType, setCollectionType] = useState<CollectionType>(collectionTypeOptions[0].value)
  63. const showCollectionList = (() => {
  64. let typeFilteredList: Collection[] = []
  65. if (collectionType === CollectionType.all)
  66. typeFilteredList = collectionList
  67. else
  68. typeFilteredList = collectionList.filter(item => item.type === collectionType)
  69. if (query)
  70. return typeFilteredList.filter(item => item.name.includes(query))
  71. return typeFilteredList
  72. })()
  73. const hasNoCustomCollection = !collectionList.find(item => item.type === CollectionType.custom)
  74. useEffect(() => {
  75. setCurrCollectionIndex(0)
  76. }, [collectionType])
  77. const currCollection = (() => {
  78. if (currCollectionIndex === null)
  79. return null
  80. return showCollectionList[currCollectionIndex]
  81. })()
  82. const [currTools, setCurrentTools] = useState<Tool[]>([])
  83. useEffect(() => {
  84. if (!currCollection)
  85. return
  86. (async () => {
  87. setIsDetailLoading(true)
  88. try {
  89. if (currCollection.type === CollectionType.builtIn) {
  90. const list = await fetchBuiltInToolList(currCollection.name) as Tool[]
  91. setCurrentTools(list)
  92. }
  93. else {
  94. const list = await fetchCustomToolList(currCollection.name) as Tool[]
  95. setCurrentTools(list)
  96. }
  97. }
  98. catch (e) { }
  99. setIsDetailLoading(false)
  100. })()
  101. }, [currCollection?.name])
  102. const [isShowEditCollectionToolModal, setIsShowEditCollectionToolModal] = useState(false)
  103. const handleCreateToolCollection = () => {
  104. setIsShowEditCollectionToolModal(true)
  105. }
  106. const doCreateCustomToolCollection = async (data: CustomCollectionBackend) => {
  107. await createCustomCollection(data)
  108. Toast.notify({
  109. type: 'success',
  110. message: t('common.api.actionSuccess'),
  111. })
  112. await fetchCollectionList()
  113. setIsShowEditCollectionToolModal(false)
  114. }
  115. return (
  116. <>
  117. <div className='flex h-full'>
  118. {/* sidebar */}
  119. <div className={cn(isInToolsPage ? 'sm:w-[216px] px-4' : 'sm:w-[256px] px-3', 'flex flex-col w-16 shrink-0 pb-2')}>
  120. {isInToolsPage && (
  121. <Button className='mt-6 flex items-center !h-8 pl-4' type='primary' onClick={handleCreateToolCollection}>
  122. <Plus className='w-4 h-4 mr-1' />
  123. <div className='leading-[18px] text-[13px] font-medium truncate'>{t('tools.createCustomTool')}</div>
  124. </Button>
  125. )}
  126. {isInDebugPage && (
  127. <div className='mt-6 flex space-x-1 items-center'>
  128. <Search
  129. className='grow'
  130. value={query}
  131. onChange={setQuery}
  132. />
  133. <Button className='flex items-center justify-center !w-8 !h-8 !p-0' type='primary'>
  134. <Plus className='w-4 h-4' onClick={handleCreateToolCollection} />
  135. </Button>
  136. </div>
  137. )}
  138. <TabSlider
  139. className='mt-3'
  140. itemWidth={isInToolsPage ? 89 : 75}
  141. value={collectionType}
  142. onChange={v => setCollectionType(v as CollectionType)}
  143. options={collectionTypeOptions}
  144. />
  145. {isInToolsPage && (
  146. <Search
  147. className='mt-5'
  148. value={query}
  149. onChange={setQuery}
  150. />
  151. )}
  152. {(collectionType === CollectionType.custom && hasNoCustomCollection)
  153. ? (
  154. <div className='grow h-0 p-2 pt-8'>
  155. <NoCustomTool onCreateTool={handleCreateToolCollection} />
  156. </div>
  157. )
  158. : (
  159. (showCollectionList.length > 0 || !query)
  160. ? <ToolNavList
  161. className='mt-2 grow height-0 overflow-y-auto'
  162. currentName={currCollection?.name || ''}
  163. list={showCollectionList}
  164. onChosen={setCurrCollectionIndex}
  165. />
  166. : (
  167. <div className='grow h-0 p-2 pt-8'>
  168. <NoSearchRes
  169. onReset={() => { setQuery('') }}
  170. />
  171. </div>
  172. )
  173. )}
  174. {loc === LOC.tools && (
  175. <Contribute />
  176. )}
  177. </div>
  178. {/* tools */}
  179. <div className={cn('grow h-full overflow-hidden p-2')}>
  180. <div className='h-full bg-white rounded-2xl'>
  181. {!(collectionType === CollectionType.custom && hasNoCustomCollection) && showCollectionList.length > 0 && (
  182. <ToolList
  183. collection={currCollection}
  184. list={currTools}
  185. loc={loc}
  186. addedTools={addedTools}
  187. onAddTool={onAddTool}
  188. onRefreshData={fetchCollectionList}
  189. onCollectionRemoved={() => {
  190. setCurrCollectionIndex(0)
  191. fetchCollectionList()
  192. }}
  193. isLoading={isDetailLoading}
  194. />
  195. )}
  196. </div>
  197. </div>
  198. </div>
  199. {isShowEditCollectionToolModal && (
  200. <EditCustomToolModal
  201. payload={null}
  202. onHide={() => setIsShowEditCollectionToolModal(false)}
  203. onAdd={doCreateCustomToolCollection}
  204. />
  205. )}
  206. </>
  207. )
  208. }
  209. export default React.memo(Tools)