import { uuid } from '@/utils/utils'; import { DeleteOutlined, EditOutlined, ToolOutlined } from '@ant-design/icons'; import { Empty, Popconfirm, message } from 'antd'; import { useState, useEffect } from 'react'; import styles from './style.less'; import ToolModal from './ToolModal'; import { getToolTypes } from './service'; import { AgentToolType, AgentType } from './type'; type Props = { currentAgent?: AgentType; onSaveAgent: (agent: AgentType, noTip?: boolean) => Promise; }; const ToolsSection: React.FC = ({ currentAgent, onSaveAgent }) => { const [modalVisible, setModalVisible] = useState(false); const [editTool, setEditTool] = useState(); const [toolTypesOptions, setToolTypesOptions] = useState([]); useEffect(() => { queryToolTypes(); }, []); const toolConfig = currentAgent?.toolConfig ? JSON.parse(currentAgent.toolConfig as any) : {}; const saveAgent = async (agent: AgentType) => { await onSaveAgent(agent); setModalVisible(false); }; const queryToolTypes = async () => { const { code, data } = await getToolTypes(); if (code === 200 && data) { const options = Object.keys(data).map((key: string) => { return { label: data[key], value: key, }; }); setToolTypesOptions(options); } else { message.error('获取工具类型失败'); } }; const onSaveTool = async (tool: AgentToolType) => { const newAgentConfig = toolConfig || ({} as any); if (!newAgentConfig.tools) { newAgentConfig.tools = []; } if (tool.id) { const index = newAgentConfig.tools.findIndex((item: AgentToolType) => item.id === tool.id); newAgentConfig.tools[index] = tool; } else { newAgentConfig.tools.push({ ...tool, id: uuid() }); } await saveAgent({ ...currentAgent, toolConfig: JSON.stringify(newAgentConfig) as any, }); setModalVisible(false); }; const onDeleteTool = async (tool: AgentToolType) => { const newAgentConfig = toolConfig || ({} as any); if (!newAgentConfig.tools) { newAgentConfig.tools = []; } newAgentConfig.tools = newAgentConfig.tools.filter( (item: AgentToolType) => item.id !== tool.id, ); await saveAgent({ ...currentAgent, toolConfig: JSON.stringify(newAgentConfig) as any, }); }; return ( <>
{toolConfig?.tools && toolConfig?.tools?.length > 0 ? (
{toolConfig.tools.map((tool: AgentToolType) => { const toolType = toolTypesOptions.find((item) => item.value === tool.type)?.label; return (
{ setEditTool(tool); setModalVisible(true); }} >
{tool.name || toolType}
{ e.stopPropagation(); setEditTool(tool); setModalVisible(true); }} /> { e?.stopPropagation(); }} onConfirm={(e) => { e?.stopPropagation(); onDeleteTool(tool); }} > { e.stopPropagation(); }} />
{toolType}
); })}
) : (
)}
{modalVisible && ( { setModalVisible(false); }} /> )} ); }; export default ToolsSection;