index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 [toolPageCollectionType, setToolPageCollectionType] = useTabSearchParams({
  65. defaultTab: collectionTypeOptions[0].value,
  66. })
  67. const [appPageCollectionType, setAppPageCollectionType] = useState(collectionTypeOptions[0].value)
  68. const { collectionType, setCollectionType } = (() => {
  69. if (isInToolsPage) {
  70. return {
  71. collectionType: toolPageCollectionType,
  72. setCollectionType: setToolPageCollectionType,
  73. }
  74. }
  75. return {
  76. collectionType: appPageCollectionType,
  77. setCollectionType: setAppPageCollectionType,
  78. }
  79. })()
  80. const showCollectionList = (() => {
  81. let typeFilteredList: Collection[] = []
  82. if (collectionType === CollectionType.all)
  83. typeFilteredList = collectionList
  84. else
  85. typeFilteredList = collectionList.filter(item => item.type === collectionType)
  86. if (query)
  87. return typeFilteredList.filter(item => item.name.includes(query))
  88. return typeFilteredList
  89. })()
  90. const hasNoCustomCollection = !collectionList.find(item => item.type === CollectionType.custom)
  91. useEffect(() => {
  92. setCurrCollectionIndex(0)
  93. }, [collectionType])
  94. const currCollection = (() => {
  95. if (currCollectionIndex === null)
  96. return null
  97. return showCollectionList[currCollectionIndex]
  98. })()
  99. const [currTools, setCurrentTools] = useState<Tool[]>([])
  100. useEffect(() => {
  101. if (!currCollection)
  102. return
  103. (async () => {
  104. setIsDetailLoading(true)
  105. try {
  106. if (currCollection.type === CollectionType.builtIn) {
  107. const list = await fetchBuiltInToolList(currCollection.name)
  108. setCurrentTools(list)
  109. }
  110. else {
  111. const list = await fetchCustomToolList(currCollection.name)
  112. setCurrentTools(list)
  113. }
  114. }
  115. catch (e) { }
  116. setIsDetailLoading(false)
  117. })()
  118. }, [currCollection?.name])
  119. const [isShowEditCollectionToolModal, setIsShowEditCollectionToolModal] = useState(false)
  120. const handleCreateToolCollection = () => {
  121. setIsShowEditCollectionToolModal(true)
  122. }
  123. const doCreateCustomToolCollection = async (data: CustomCollectionBackend) => {
  124. await createCustomCollection(data)
  125. Toast.notify({
  126. type: 'success',
  127. message: t('common.api.actionSuccess'),
  128. })
  129. await fetchCollectionList()
  130. setIsShowEditCollectionToolModal(false)
  131. }
  132. return (
  133. <>
  134. <div className='flex h-full'>
  135. {/* sidebar */}
  136. <div className={cn(isInToolsPage ? 'sm:w-[216px] px-4' : 'sm:w-[256px] px-3', 'flex flex-col w-16 shrink-0 pb-2')}>
  137. {isInToolsPage && (
  138. <Button className='mt-6 flex items-center !h-8 pl-4' type='primary' onClick={handleCreateToolCollection}>
  139. <Plus className='w-4 h-4 mr-1' />
  140. <div className='leading-[18px] text-[13px] font-medium truncate'>{t('tools.createCustomTool')}</div>
  141. </Button>
  142. )}
  143. {isInDebugPage && (
  144. <div className='mt-6 flex space-x-1 items-center'>
  145. <Search
  146. className='grow'
  147. value={query}
  148. onChange={setQuery}
  149. />
  150. <Button className='flex items-center justify-center !w-8 !h-8 !p-0' type='primary'>
  151. <Plus className='w-4 h-4' onClick={handleCreateToolCollection} />
  152. </Button>
  153. </div>
  154. )}
  155. <TabSlider
  156. className='mt-3'
  157. itemWidth={isInToolsPage ? 89 : 75}
  158. value={collectionType}
  159. onChange={v => setCollectionType(v as CollectionType)}
  160. options={collectionTypeOptions}
  161. />
  162. {isInToolsPage && (
  163. <Search
  164. className='mt-5'
  165. value={query}
  166. onChange={setQuery}
  167. />
  168. )}
  169. {(collectionType === CollectionType.custom && hasNoCustomCollection)
  170. ? (
  171. <div className='grow h-0 p-2 pt-8'>
  172. <NoCustomTool onCreateTool={handleCreateToolCollection} />
  173. </div>
  174. )
  175. : (
  176. (showCollectionList.length > 0 || !query)
  177. ? <ToolNavList
  178. className='mt-2 grow height-0 overflow-y-auto'
  179. currentName={currCollection?.name || ''}
  180. list={showCollectionList}
  181. onChosen={setCurrCollectionIndex}
  182. />
  183. : (
  184. <div className='grow h-0 p-2 pt-8'>
  185. <NoSearchRes
  186. onReset={() => { setQuery('') }}
  187. />
  188. </div>
  189. )
  190. )}
  191. {loc === LOC.tools && (
  192. <Contribute />
  193. )}
  194. </div>
  195. {/* tools */}
  196. <div className={cn('grow h-full overflow-hidden p-2')}>
  197. <div className='h-full bg-white rounded-2xl'>
  198. {!(collectionType === CollectionType.custom && hasNoCustomCollection) && showCollectionList.length > 0 && (
  199. <ToolList
  200. collection={currCollection}
  201. list={currTools}
  202. loc={loc}
  203. addedTools={addedTools}
  204. onAddTool={onAddTool}
  205. onRefreshData={fetchCollectionList}
  206. onCollectionRemoved={() => {
  207. setCurrCollectionIndex(0)
  208. fetchCollectionList()
  209. }}
  210. isLoading={isDetailLoading}
  211. />
  212. )}
  213. {collectionType === CollectionType.custom && hasNoCustomCollection && (
  214. <NoCustomToolPlaceholder />
  215. )}
  216. </div>
  217. </div>
  218. </div>
  219. {isShowEditCollectionToolModal && (
  220. <EditCustomToolModal
  221. payload={null}
  222. onHide={() => setIsShowEditCollectionToolModal(false)}
  223. onAdd={doCreateCustomToolCollection}
  224. />
  225. )}
  226. </>
  227. )
  228. }
  229. export default React.memo(Tools)