import { apiInterceptors, getAppStrategy, getAppStrategyValues } from '@/client/api'; import { Button, Input, Select } from 'antd'; import { useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import ResourceCard from './resource-card'; interface IProps { resourceTypes: any; updateDetailsByAgentKey: (key: string, data: any) => void; detail: any; editResources?: any; } export default function AgentPanel(props: IProps) { const { resourceTypes, updateDetailsByAgentKey, detail, editResources } = props; const { t } = useTranslation(); const [resources, setResources] = useState([...(editResources ?? [])]); const [agent, setAgent] = useState({ ...detail, resources: [] }); const [strategyOptions, setStrategyOptions] = useState([]); const [strategyValueOptions, setStrategyValueOptions] = useState([]); const updateResourcesByIndex = (data: any, index: number) => { setResources((resources: any) => { const tempResources = [...resources]; if (!data) { return tempResources.filter((_: any, indey) => index !== indey); } return tempResources.map((item: any, indey) => { if (index === indey) { return data; } else { return item; } }); }); }; const getStrategy = async () => { const [_, data] = await apiInterceptors(getAppStrategy()); if (data) { setStrategyOptions(data?.map(item => ({ label: item.name_cn, value: item.value }))); } }; const getStrategyValues = async (type: string) => { const [_, data] = await apiInterceptors(getAppStrategyValues(type)); if (data) { setStrategyValueOptions(data.map(item => ({ label: item, value: item })) ?? []); } }; const formatStrategyValue = (value: string) => { return !value ? [] : value.split(','); }; useEffect(() => { getStrategy(); getStrategyValues(detail.llm_strategy); }, []); useEffect(() => { updateAgent(resources, 'resources'); }, [resources]); const updateAgent = (data: any, type: string) => { const tempAgent = { ...agent }; tempAgent[type] = data; setAgent(tempAgent); updateDetailsByAgentKey(detail.key, tempAgent); }; const handelAdd = () => { setResources([...resources, { name: '', type: '', introduce: '', value: '', is_dynamic: '' }]); }; const resourceTypeOptions = useMemo(() => { return resourceTypes?.map((item: string) => { return { label: item, value: item, }; }); }, [resourceTypes]); return (
{t('Prompt')}:
{ updateAgent(e.target.value, 'prompt_template'); }} />
{t('LLM_strategy')}:
{ if (!value || value?.length === 0) { updateAgent(null, 'llm_strategy_value'); return null; } const curValue = value.reduce((pre: string, cur: string, index: number) => { if (index === 0) { return cur; } else { return `${pre},${cur}`; } }, ''); updateAgent(curValue, 'llm_strategy_value'); }} /> )}
{t('available_resources')}
{resources.map((resource: any, index: number) => { return ( ); })}
); }