import { apiInterceptors, getAgents, getAppStrategy, getPromptList, getResourceType } from '@/client/api'; import { IAgent, IDetail } from '@/types/app'; import { QuestionCircleOutlined } from '@ant-design/icons'; import { useRequest } from 'ahooks'; import { Checkbox, Form, Space, Tooltip } from 'antd'; import cls from 'classnames'; import { concat } from 'lodash'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { agentIcon, resourceTypeIcon } from '../../config'; import DetailsCard from './DetailsCard'; interface AgentSelectProps { agents: IAgent[]; selectedTab: string; setSelectedTab: React.Dispatch>; value?: any; onChange?: (value: any) => void; } // 自定义agent选择 const AgentSelect: React.FC = ({ value, onChange, agents, selectedTab, setSelectedTab }) => { return ( { onChange?.(value); }} value={value} > {agents.map(item => { return (
{ setSelectedTab(item.name || ''); }} >
{agentIcon[item.name || '']} {item.label}
); })}
); }; const AutoPlan: React.FC<{ initValue: any; updateData: (data: any) => void; classNames?: string; }> = ({ initValue, updateData, classNames }) => { const { t, i18n } = useTranslation(); const [form] = Form.useForm(); const agentName = Form.useWatch('agent_name', form); // 选中的agent const [selectedTab, setSelectedTab] = useState(''); const details = useRef([]); const language = i18n.language === 'en'; // 获取agents, strategy, sourceType; const { data, loading } = useRequest(async () => { const res = await Promise.all([ apiInterceptors(getAgents()), apiInterceptors(getAppStrategy()), apiInterceptors(getResourceType()), ]); const [, agents] = res?.[0] || []; details.current = agents?.map(item => { return { agent_name: item.name, llm_strategy: '', llm_strategy_value: '', prompt_template: '', resources: [], }; }) || []; form.setFieldsValue({ agent_name: initValue?.map((item: any) => item.agent_name), }); setSelectedTab(initValue?.map((item: any) => item.agent_name)?.[0] || agents?.[0]?.name || ''); return res ?? []; }); // 获取prompt提示语列表 const { data: promptData } = useRequest(async () => { const [, res] = await apiInterceptors( getPromptList({ page: 1, page_size: 100000, }), ); return res ?? { items: [] }; }); // 模型策略options const modelStrategyOptions: any[] = useMemo(() => { const [, strategy] = data?.[1] || []; if (strategy?.length) { return strategy.map(item => { return { label: language ? item.name : item.name_cn, value: item.value, }; }); } return []; }, [data]); // 资源类型options const resourceTypeOptions: Record[] = useMemo(() => { const [, sourceType] = data?.[2] || []; if (sourceType?.length) { const formatterSourceType = sourceType.map(item => { return { label: ( {resourceTypeIcon[item]} {item} ), value: item, }; }); return concat( [ { label: (
{resourceTypeIcon['all']} {t('All')}
), value: 'all', }, ], formatterSourceType, ); } return []; }, [data]); // 实时返回数据给消费组件 useEffect(() => { updateData([loading, details.current.filter(detail => agentName?.includes(detail.agent_name))]); }, [loading, agentName, updateData]); return (
{data?.[0]?.[1]?.map(item => ( { details.current = details.current.map(detail => { if (detail.agent_name === data?.agent_name) { return { ...detail, ...data, }; } return { ...detail, }; }); updateData([loading, details.current.filter(detail => agentName?.includes(detail.agent_name))]); }} initValue={initValue} name={item.name} modelStrategyOptions={modelStrategyOptions} resourceTypeOptions={resourceTypeOptions} promptList={promptData?.items || []} /> ))}
); }; export default AutoPlan;