'use client' import type { FC } from 'react' import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' import Item from './item' import FeaturePanel from '@/app/components/app/configuration/base/feature-panel' import { Google, WebReader, Wikipedia } from '@/app/components/base/icons/src/public/plugins' import { getToolProviders } from '@/service/explore' import Loading from '@/app/components/base/loading' import AccountSetting from '@/app/components/header/account-setting' export type IPluginsProps = { readonly?: boolean config: Record onChange?: (key: string, value: boolean) => void } const plugins = [ { key: 'google_search', icon: }, { key: 'web_reader', icon: }, { key: 'wikipedia', icon: }, ] const Plugins: FC = ({ readonly, config, onChange, }) => { const { t } = useTranslation() const [isLoading, setIsLoading] = React.useState(!readonly) const [isSerpApiValid, setIsSerpApiValid] = React.useState(false) const checkSerpApiKey = async () => { if (readonly) return const provides: any = await getToolProviders() const isSerpApiValid = !!provides.find((v: any) => v.tool_name === 'serpapi' && v.is_enabled) setIsSerpApiValid(isSerpApiValid) setIsLoading(false) } useEffect(() => { checkSerpApiKey() }, []) const [showSetSerpAPIKeyModal, setShowSetAPIKeyModal] = React.useState(false) const itemConfigs = plugins.map((plugin) => { const res: Record = { ...plugin } const { key } = plugin res.name = t(`explore.universalChat.plugins.${key}.name`) if (key === 'web_reader') res.description = t(`explore.universalChat.plugins.${key}.description`) if (key === 'google_search' && !isSerpApiValid && !readonly) { res.readonly = true res.more = (
{t('explore.universalChat.plugins.google_search.more.left')} setShowSetAPIKeyModal(true)}>{t('explore.universalChat.plugins.google_search.more.link')} {t('explore.universalChat.plugins.google_search.more.right')}
) } return res }) const enabledPluginNum = Object.values(config).filter(v => v).length return ( <>
{t('explore.universalChat.plugins.name')}
({enabledPluginNum}/{plugins.length})
} hasHeaderBottomBorder={false} > {isLoading ? (
) : (
{itemConfigs.map(item => ( onChange?.(item.key, enabled)} readonly={readonly || item.readonly} /> ))}
)}
{ showSetSerpAPIKeyModal && ( { setShowSetAPIKeyModal(false) await checkSerpApiKey() }} /> ) } ) } export default React.memo(Plugins)