import { Form, Modal, Input, Select, Button, TreeSelect } from 'antd'; import { AgentToolType, AgentToolTypeEnum, AGENT_TOOL_TYPE_LIST, MetricOptionType, QUERY_MODE_LIST, } from './type'; import { useEffect, useState } from 'react'; import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; import styles from './style.less'; import { traverseTree, uuid } from '@/utils/utils'; import { getModelList } from './service'; import { PluginType } from '../ChatPlugin/type'; import { getPluginList } from '../ChatPlugin/service'; const FormItem = Form.Item; type Props = { editTool?: AgentToolType; onSaveTool: (tool: AgentToolType) => Promise; onCancel: () => void; }; const ToolModal: React.FC = ({ editTool, onSaveTool, onCancel }) => { const [toolType, setToolType] = useState(); const [modelList, setModelList] = useState([]); const [saveLoading, setSaveLoading] = useState(false); const [examples, setExamples] = useState<{ id: string; question?: string }[]>([]); const [metricOptions, setMetricOptions] = useState([]); const [plugins, setPlugins] = useState([]); const [form] = Form.useForm(); const initModelList = async () => { const res = await getModelList(); const treeData = traverseTree(res.data, (node: any) => { node.title = node.name; node.value = node.type === 'DOMAIN' ? `DOMAIN_${node.id}` : node.id; node.checkable = node.type === 'DATASET' || (node.type === 'DOMAIN' && node.children?.length > 0); }); setModelList([{ title: '默认', value: -1, type: 'DATASET' }, ...treeData]); }; const initPluginList = async () => { const res = await getPluginList({}); setPlugins(res.data || []); }; useEffect(() => { initModelList(); initPluginList(); }, []); useEffect(() => { if (editTool) { form.setFieldsValue({ ...editTool, plugins: editTool.plugins?.[0] }); setToolType(editTool.type); setExamples( (editTool.exampleQuestions || []).map((item) => ({ id: uuid(), question: item })), ); setMetricOptions(editTool.metricOptions || []); } else { form.resetFields(); } }, [editTool]); const layout = { labelCol: { span: 6 }, wrapperCol: { span: 14 }, }; const onOk = async () => { const values = await form.validateFields(); setSaveLoading(true); await onSaveTool({ id: editTool?.id, ...values, exampleQuestions: examples.map((item) => item.question).filter((item) => item), plugins: values.plugins ? [values.plugins] : undefined, metricOptions: metricOptions.map((item) => ({ ...item, modelId: values.modelId })), }); setSaveLoading(false); }; return (
{(toolType === AgentToolTypeEnum.NL2SQL_RULE || toolType === AgentToolTypeEnum.NL2SQL_LLM) && ( )} {toolType === AgentToolTypeEnum.NL2SQL_LLM && (
{examples.map((example) => { const { id, question } = example; return (
{ example.question = e.target.value; setExamples([...examples]); }} allowClear /> { setExamples(examples.filter((item) => item.id !== id)); }} />
); })}
)} {toolType === AgentToolTypeEnum.PLUGIN && ( ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase()) } /> )}
); }; export default ToolModal;