index.tsx 7.8 KB

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