'use client' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import cn from 'classnames' import { CollectionType, LOC } from '../types' import type { Collection, CustomCollectionBackend, Tool } from '../types' import Loading from '../../base/loading' import { ArrowNarrowRight } from '../../base/icons/src/vender/line/arrows' import Toast from '../../base/toast' import Header from './header' import Item from './item' import AppIcon from '@/app/components/base/app-icon' import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials' import { fetchCustomCollection, removeBuiltInToolCredential, removeCustomCollection, updateBuiltInToolCredential, updateCustomCollection } from '@/service/tools' import EditCustomToolModal from '@/app/components/tools/edit-custom-collection-modal' import type { AgentTool } from '@/types/app' import { MAX_TOOLS_NUM } from '@/config' type Props = { collection: Collection | null list: Tool[] // onToolListChange: () => void // custom tools change loc: LOC addedTools?: AgentTool[] onAddTool?: (collection: Collection, payload: Tool) => void onRefreshData: () => void onCollectionRemoved: () => void isLoading: boolean } const ToolList: FC = ({ collection, list, loc, addedTools, onAddTool, onRefreshData, onCollectionRemoved, isLoading, }) => { const { t } = useTranslation() const isInToolsPage = loc === LOC.tools const isBuiltIn = collection?.type === CollectionType.builtIn const needAuth = collection?.allow_delete const [showSettingAuth, setShowSettingAuth] = useState(false) const [customCollection, setCustomCollection] = useState(null) useEffect(() => { if (!collection) return (async () => { if (collection.type === CollectionType.custom) { const res = await fetchCustomCollection(collection.name) as any setCustomCollection({ ...res, provider: collection.name, } as CustomCollectionBackend) } })() }, [collection]) const [isShowEditCollectionToolModal, setIsShowEditCustomCollectionModal] = useState(false) const doUpdateCustomToolCollection = async (data: CustomCollectionBackend) => { await updateCustomCollection(data) onRefreshData() Toast.notify({ type: 'success', message: t('common.api.actionSuccess'), }) setIsShowEditCustomCollectionModal(false) } const doRemoveCustomToolCollection = async () => { await removeCustomCollection(collection?.name as string) onCollectionRemoved() Toast.notify({ type: 'success', message: t('common.api.actionSuccess'), }) setIsShowEditCustomCollectionModal(false) } if (!collection || isLoading) return const icon = <>{typeof collection.icon === 'string' ? (
) : ( )} return (
setShowSettingAuth(true)} onShowEditCustomCollection={() => setIsShowEditCustomCollectionModal(true)} />
{t('tools.includeToolNum', { num: list.length, })}
{needAuth && isBuiltIn && !collection.is_team_authorization && ( <>
ยท
setShowSettingAuth(true)} >
{t('tools.auth.setup')}
)}
{/* list */}
{list.map(item => ( = MAX_TOOLS_NUM} added={!!addedTools?.find(v => v.provider_id === collection.id && v.tool_name === item.name)} onAdd={!isInToolsPage ? tool => onAddTool?.(collection as Collection, tool) : undefined} /> ))}
{showSettingAuth && ( setShowSettingAuth(false)} onSaved={async (value) => { await updateBuiltInToolCredential(collection.name, value) Toast.notify({ type: 'success', message: t('common.api.actionSuccess'), }) await onRefreshData() setShowSettingAuth(false) }} onRemove={async () => { await removeBuiltInToolCredential(collection.name) Toast.notify({ type: 'success', message: t('common.api.actionSuccess'), }) await onRefreshData() setShowSettingAuth(false) }} /> )} {isShowEditCollectionToolModal && ( setIsShowEditCustomCollectionModal(false)} onEdit={doUpdateCustomToolCollection} onRemove={doRemoveCustomToolCollection} /> )}
) } export default React.memo(ToolList)