MessageBubble.tsx
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import React, { useMemo, useState, useEffect } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { cn } from '@/lib/utils';
import {
ChevronDown, ChevronRight,
CheckCircle2, Loader2, Lightbulb, Terminal,
BookOpen, FileText
} from 'lucide-react';
export type ReasoningStep = {
id: string;
type: 'status' | 'thought' | 'call' | 'response' | 'sources';
content: string;
name?: string;
data?: {
original: string[];
wiki: string[];
};
time?: string;
};
interface MessageProps {
role: 'user' | 'ai' | 'error';
content: string;
reasoning?: ReasoningStep[];
isStreaming?: boolean;
startTime?: number;
endTime?: number;
serverDurationMs?: number;
}
export function MessageBubble({ role, content, reasoning, isStreaming, startTime, endTime, serverDurationMs }: MessageProps) {
// 1. 默认折叠思考过程
const [isReasoningExpanded, setIsReasoningExpanded] = useState(false);
const [elapsedMs, setElapsedMs] = useState<number>(0);
const isUser = role === 'user';
useEffect(() => {
if (isStreaming && startTime && !endTime) {
const interval = setInterval(() => {
setElapsedMs(Date.now() - startTime);
}, 100);
return () => clearInterval(interval);
} else if (startTime && endTime) {
Promise.resolve().then(() => {
setElapsedMs(endTime - startTime);
});
} else if (startTime && !isStreaming) {
// If it stopped streaming but endTime wasn't set, just freeze it
Promise.resolve().then(() => {
setElapsedMs(Date.now() - startTime);
});
}
}, [isStreaming, startTime, endTime]);
// 结束后优先用服务器权威耗时(真实秒数);流式中用本地 tick 实时展示
const effectiveMs = (!isStreaming && typeof serverDurationMs === 'number') ? serverDurationMs : elapsedMs;
const displayTime = effectiveMs > 0 ? Math.round(effectiveMs / 1000) + 's' : null;
// 提取 interactive json 并剔除
const displayContent = useMemo(() => {
if (!content) return '';
const regex = /```json_interactive\s*(\{[\s\S]*?\})\s*```/;
let newContent = content;
if (content.match(regex)) {
newContent = content.replace(regex, '');
}
newContent = newContent.replace(/`+$/, '').trim();
return newContent;
}, [content]);
const hasMeaningfulReasoning = useMemo(() => {
if (!reasoning || reasoning.length === 0) return false;
return true;
}, [reasoning]);
if (role === 'error') {
return (
<div className="flex justify-center my-4 w-full">
<div className="bg-red-50 border border-red-100 text-red-600 px-4 py-2 rounded-lg text-sm flex items-center space-x-2">
<span className="font-bold">错误:</span>
<span>{content}</span>
</div>
</div>
);
}
return (
<div className={cn("flex mb-6 w-full", isUser ? "justify-end" : "justify-start")}>
<div className={cn(
"flex items-start space-x-3 w-full max-w-[95%] md:max-w-[90%]",
isUser && "flex-row-reverse space-x-reverse"
)}>
<div className={cn(
"w-9 h-9 rounded-xl flex items-center justify-center flex-shrink-0 mt-0.5 shadow-sm font-bold text-xs text-white",
isUser ? "bg-slate-700" : "bg-blue-600"
)}>
{isUser ? "我" : "AI"}
</div>
<div className="flex flex-col space-y-2 flex-1 min-w-0">
{/* Reasoning Block */}
{!isUser && hasMeaningfulReasoning && (
<div className="w-full">
<div className={cn(
"bg-slate-50 border border-slate-200 rounded-xl overflow-hidden transition-all duration-200",
!isReasoningExpanded && "max-h-12"
)}>
<button
onClick={() => setIsReasoningExpanded(!isReasoningExpanded)}
className="w-full px-4 py-3 flex items-center justify-between hover:bg-slate-100/50 transition-colors"
>
<div className="flex items-center space-x-2 text-xs font-bold text-slate-500 tracking-wider">
{isReasoningExpanded ? <ChevronDown className="w-3.5 h-3.5" /> : <ChevronRight className="w-3.5 h-3.5" />}
{isStreaming ? (
<Loader2 className="w-3.5 h-3.5 animate-spin text-blue-500" />
) : (
<CheckCircle2 className="w-3.5 h-3.5 text-green-500" />
)}
<span>查看思考过程 ({reasoning!.length} 步{displayTime ? `, ${displayTime}` : ''})</span>
</div>
</button>
{isReasoningExpanded && (
<div className="px-4 pb-4 space-y-3 border-t border-slate-100 pt-3 bg-white/50">
{reasoning!.map((step, idx) => (
<div key={step.id || idx} className="text-xs flex flex-col space-y-1 mt-2 first:mt-0">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-1.5 text-slate-400 font-semibold uppercase tracking-wider text-[9px]">
{step.type === 'thought' && <Lightbulb className="w-3 h-3 text-yellow-500" />}
{step.type === 'call' && <Terminal className="w-3 h-3 text-blue-500" />}
{step.type === 'response' && <CheckCircle2 className="w-3 h-3 text-green-500" />}
{step.type === 'sources' && <BookOpen className="w-3 h-3 text-indigo-500" />}
<span>
{step.type === 'call' ? '工具调用' :
step.type === 'response' ? '工具返回' :
step.type === 'sources' ? '引用来源' : '思考'}
{step.name ? ` | ${step.name}` : ''}
</span>
</div>
{step.time && (
<span className="text-[9px] text-slate-400 font-medium tracking-wide tabular-nums">
{step.time}
</span>
)}
</div>
{/* 工具详情折叠 */}
{(step.type === 'call' || step.type === 'response') ? (
<details className="group ml-4.5 border-l-2 border-slate-100 pl-3 py-1">
<summary className="text-[10px] text-slate-500 cursor-pointer hover:text-blue-600 transition-colors list-none flex items-center gap-1">
<ChevronRight className="w-2.5 h-2.5 transition-transform group-open:rotate-90" />
<span>点击查看数据详情</span>
</summary>
<pre className={cn(
"mt-2 p-2 leading-relaxed font-mono whitespace-pre-wrap break-all max-h-60 overflow-y-auto rounded-md shadow-inner",
step.type === 'response' && step.content.includes('记忆检索服务暂时异常')
? "bg-amber-50 border border-amber-200 text-amber-700"
: "bg-slate-100/50 border border-slate-200/50 text-slate-600"
)}>
{step.content}
</pre>
</details>
) : step.type === 'sources' && step.data ? (
/* 增强参考资料显示 */
<div className="ml-4.5 border-l-2 border-indigo-100 pl-3 py-1 space-y-2">
{step.data.original && step.data.original.length > 0 && (
<div className="space-y-1">
<div className="text-[9px] text-slate-400 font-bold uppercase tracking-tight flex items-center gap-1">
<FileText className="w-2.5 h-2.5" /> 原始文档来源
</div>
<div className="flex flex-wrap gap-1.5">
{step.data.original.map(s => (
<span key={s} className="px-2 py-0.5 bg-blue-50 text-blue-600 rounded-md text-[10px] font-semibold border border-blue-100 shadow-sm">
{s}
</span>
))}
</div>
</div>
)}
{step.data.wiki && step.data.wiki.length > 0 && (
<div className="space-y-1">
<div className="text-[9px] text-slate-400 font-bold uppercase tracking-tight flex items-center gap-1">
<BookOpen className="w-2.5 h-2.5" /> 关联 Wiki 知识点
</div>
<div className="space-y-0.5">
{step.data.wiki.map(s => (
<div key={s} className="text-[10px] text-slate-600 flex items-center gap-1.5 py-0.5 px-2 bg-slate-100/30 rounded border border-transparent hover:border-slate-200 hover:bg-slate-100/60 transition-all">
<div className="w-1 h-1 rounded-full bg-indigo-300" />
{s}
</div>
))}
</div>
</div>
)}
</div>
) : (
<div className="ml-4.5 border-l-2 border-slate-100 pl-3 py-1 text-slate-600 leading-relaxed font-sans whitespace-pre-wrap">
{step.content}
</div>
)}
</div>
))}
</div>
)}
</div>
</div>
)}
{/* Main Content Bubble */}
{(displayContent || isUser) && (
<div className={cn(
"px-5 py-3.5 shadow-sm text-[15px] leading-relaxed break-words",
isUser
? "bg-blue-600 text-white rounded-2xl rounded-tr-sm self-end"
: "bg-white border border-slate-100 text-slate-800 rounded-2xl rounded-tl-sm"
)}>
{isUser ? (
<div className="whitespace-pre-wrap">{displayContent}</div>
) : (
<div className="prose prose-sm max-w-none prose-p:leading-relaxed prose-pre:bg-slate-900 prose-pre:text-slate-100 prose-slate prose-headings:text-slate-900 prose-a:text-blue-600">
<ReactMarkdown remarkPlugins={[remarkGfm]}>
{displayContent}
</ReactMarkdown>
</div>
)}
</div>
)}
{/* Interactive UI Cards have been moved to Dashboard in page.tsx */}
</div>
</div>
</div>
);
}