import React, { useState } from 'react'; import { Box, Paper, Typography, Alert, useTheme, IconButton, Collapse } from '@mui/material'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ExpandLessIcon from '@mui/icons-material/ExpandLess'; import PsychologyIcon from '@mui/icons-material/Psychology'; import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh'; import { useTranslation } from 'react-i18next'; /** * 聊天消息组件 * @param {Object} props * @param {Object} props.message - 消息对象 * @param {string} props.message.role - 消息角色:'user'、'assistant' 或 'error' * @param {string} props.message.content - 消息内容 * @param {string} props.modelName - 模型名称(仅在 assistant 或 error 类型消息中显示) */ export default function ChatMessage({ message, modelName }) { const theme = useTheme(); const { t } = useTranslation(); // 用户消息 if (message.role === 'user') { return ( {typeof message.content === 'string' ? ( {message.content} ) : ( // 如果是数组类型(用于视觉模型的用户输入) <> {Array.isArray(message.content) && message.content.map((item, i) => { if (item.type === 'text') { return ( {item.text} ); } else if (item.type === 'image_url') { return ( 上传图片 ); } return null; })} )} ); } // 助手消息 if (message.role === 'assistant') { // 处理推理过程的展示状态 const [showThinking, setShowThinking] = useState(message.showThinking || false); const hasThinking = message.thinking && message.thinking.trim().length > 0; return ( {modelName && ( {modelName} )} {/* 推理过程显示区域 */} {hasThinking && ( {message.isStreaming ? ( ) : ( )} {t('playground.reasoningProcess', '推理过程')} setShowThinking(!showThinking)} sx={{ p: 0 }}> {showThinking ? : } {message.thinking} )} {/* 回答内容 */} {typeof message.content === 'string' ? ( <> {message.content} {message.isStreaming && |} ) : ( // 如果是数组类型(用于视觉模型的响应) <> {Array.isArray(message.content) && message.content.map((item, i) => { if (item.type === 'text') { return {item.text}; } else if (item.type === 'image_url') { return ( 图片 ); } return null; })} {message.isStreaming && |} )} ); } // 错误消息 if (message.role === 'error') { return ( {modelName && ( {modelName} )} {message.content} ); } return null; }