import React, { useState } from 'react'; import { Card, Collapse, Tag, Spin, Typography, Button } from 'antd'; import { ToolOutlined, ThunderboltOutlined, CheckCircleOutlined, LoadingOutlined } from '@ant-design/icons'; import type { ThoughtItem } from '../../types/dify_chat'; import Markdown from './markdown'; import '../../styles/components/chat-with-llm/thought-process.css'; const { Panel } = Collapse; const { Text, Paragraph } = Typography; interface ThoughtProcessProps { thought: ThoughtItem; isFinished: boolean; allToolIcons?: Record; } /** * 思考过程组件 * 展示AI的思考过程和工具调用 */ export default function ThoughtProcess({ thought, isFinished, allToolIcons = {} }: ThoughtProcessProps) { const [expanded, setExpanded] = useState(false); const { tool_name, tool_input, tool_output, thought: thoughtText, observation } = thought; /** * 获取工具图标 */ const getToolIcon = (toolName?: string) => { if (!toolName) return ; // 如果有自定义图标映射 if (allToolIcons[toolName]) { return {allToolIcons[toolName]}; } // 根据工具名称返回默认图标 switch (toolName.toLowerCase()) { case 'search': case 'web_search': return '🔍'; case 'calculator': case 'math': return '🧮'; case 'code': case 'python': return '💻'; case 'image': case 'vision': return '👁️'; case 'file': case 'document': return '📄'; default: return ; } }; /** * 获取状态图标和颜色 */ const getStatusInfo = () => { if (isFinished && observation) { return { icon: , color: 'success', text: '已完成' }; } else if (!isFinished) { return { icon: , color: 'processing', text: '执行中' }; } else { return { icon: , color: 'default', text: '等待中' }; } }; const statusInfo = getStatusInfo(); /** * 格式化工具输入 */ const formatToolInput = (input?: string) => { if (!input) return '无输入参数'; try { const parsed = JSON.parse(input); return (
                    {JSON.stringify(parsed, null, 2)}
                
); } catch { return {input}; } }; /** * 格式化工具输出 */ const formatToolOutput = (output?: string) => { if (!output) return '暂无输出'; try { const parsed = JSON.parse(output); return (
                    {JSON.stringify(parsed, null, 2)}
                
); } catch { return ; } }; return (
{getToolIcon(tool_name)} {tool_name || '工具调用'} {statusInfo.text}
{(tool_input || tool_output) && ( )}
{/* 思考内容 */} {thoughtText && (
)} {/* 工具详情 */} {expanded && (tool_input || tool_output) && ( {tool_input && ( 📥 输入参数 } key="input" > {formatToolInput(tool_input)} )} {tool_output && ( 📤 执行结果 } key="output" > {formatToolOutput(tool_output)} )} )} {/* 观察结果 */} {observation && observation !== tool_output && (
💡 观察结果:
)} {/* 加载状态 */} {!isFinished && !observation && (
正在执行工具调用...
)}
); }