'use client' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import cn from 'classnames' import { AuthHeaderPrefix, AuthType, 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 { ConfigurateMethodEnum } from '../../header/account-setting/model-provider-page/declarations' 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' import { useModalContext } from '@/context/modal-context' import { useProviderContext } from '@/context/provider-context' 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 isModel = collection?.type === CollectionType.model const needAuth = collection?.allow_delete const { setShowModelModal } = useModalContext() const [showSettingAuth, setShowSettingAuth] = useState(false) const { modelProviders: providers } = useProviderContext() const showSettingAuthModal = () => { if (isModel) { const provider = providers.find(item => item.provider === collection?.id) if (provider) { setShowModelModal({ payload: { currentProvider: provider, currentConfigurateMethod: ConfigurateMethodEnum.predefinedModel, currentCustomConfigrationModelFixedFields: undefined, }, onSaveCallback: () => { onRefreshData() }, }) } } else { setShowSettingAuth(true) } } const [customCollection, setCustomCollection] = useState(null) useEffect(() => { if (!collection) return (async () => { if (collection.type === CollectionType.custom) { const res = await fetchCustomCollection(collection.name) if (res.credentials.auth_type === AuthType.apiKey && !res.credentials.api_key_header_prefix) { if (res.credentials.api_key_value) res.credentials.api_key_header_prefix = AuthHeaderPrefix.custom } setCustomCollection({ ...res, provider: collection.name, }) } })() }, [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 (
showSettingAuthModal()} onShowEditCustomCollection={() => setIsShowEditCustomCollectionModal(true)} />
{t('tools.includeToolNum', { num: list.length, })}
{needAuth && (isBuiltIn || isModel) && !collection.is_team_authorization && ( <>
ยท
showSettingAuthModal()} >
{t('tools.auth.setup')}
)}
{/* list */}
{list.map(item => ( = MAX_TOOLS_NUM} added={!!addedTools?.find(v => v.provider_id === collection.id && v.provider_type === collection.type && 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)