import ModelIcon from '@/new-components/chat/content/ModelIcon'; import MarkdownContext from '@/new-components/common/MarkdownContext'; import { IChatDialogueMessageSchema } from '@/types/chat'; import { Divider } from 'antd'; import cls from 'classnames'; import React, { memo, useContext, useMemo, useRef } from 'react'; import { MobileChatContext } from '../'; import Feedback from './Feedback'; type DBGPTView = { name: string; status: 'todo' | 'runing' | 'failed' | 'completed' | (string & {}); result?: string; err_msg?: string; }; // 对话气泡 const ChatDialog: React.FC<{ message: IChatDialogueMessageSchema; index: number; }> = ({ message, index }) => { const { scene } = useContext(MobileChatContext); const { context, model_name, role, thinking } = message; // GPT回复 const isRobot = useMemo(() => role === 'view', [role]); const chatDialogRef = useRef(null); const { value } = useMemo<{ relations: string[]; value: string; cachePluginContext: DBGPTView[]; }>(() => { if (typeof context !== 'string') { return { relations: [], value: '', cachePluginContext: [], }; } const [value, relation] = context.split('\trelations:'); const relations = relation ? relation.split(',') : []; const cachePluginContext: DBGPTView[] = []; let cacheIndex = 0; const result = value.replace(/]*>[^<]*<\/dbgpt-view>/gi, matchVal => { try { const pluginVal = matchVal.replaceAll('\n', '\\n').replace(/<[^>]*>|<\/[^>]*>/gm, ''); const pluginContext = JSON.parse(pluginVal) as DBGPTView; const replacement = `${cacheIndex}`; cachePluginContext.push({ ...pluginContext, result: formatMarkdownVal(pluginContext.result ?? ''), }); cacheIndex++; return replacement; } catch (e) { console.log((e as any).message, e); return matchVal; } }); return { relations, cachePluginContext, value: result, }; }, [context]); const formatMarkdownVal = (val: string) => { return val .replaceAll('\\n', '\n') .replace(/]+)>/gi, '') .replace(/]+)>/gi, ''); }; const formatMarkdownValForAgent = (val: string) => { return val?.replace(/]+)>/gi, '
').replace(/]+)>/gi, ''); }; return (
{/* 用户提问 */} {!isRobot &&
{context}
} {isRobot && (
{typeof context === 'string' && scene === 'chat_agent' && ( {formatMarkdownValForAgent(value)} )} {typeof context === 'string' && scene !== 'chat_agent' && ( {formatMarkdownVal(value)} )} {/* 正在思考 */} {thinking && !context && (
思考中
)} {!thinking && }
{/* 用户反馈 */} {scene !== 'chat_agent' && (
{model_name}
)}
)}
); }; export default memo(ChatDialog);