-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathMessageBubble.tsx
More file actions
422 lines (393 loc) · 15 KB
/
MessageBubble.tsx
File metadata and controls
422 lines (393 loc) · 15 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import { useState, useMemo, type ComponentProps } from "react";
import Markdown from "react-markdown";
import remarkGfm from "remark-gfm";
import type { ChatMessage, ContentBlock } from "../types.js";
import { ToolBlock, getToolIcon, getToolLabel, getPreview, ToolIcon } from "./ToolBlock.js";
export function MessageBubble({ message }: { message: ChatMessage }) {
if (message.role === "system") {
return (
<div className="flex items-center gap-3 py-2 min-w-0">
<div className="shrink-0 flex-1 h-px bg-cc-border" />
<span className="text-[11px] text-cc-muted italic font-mono-code px-2 min-w-0 break-words text-center tracking-wide">
{message.content}
</span>
<div className="shrink-0 flex-1 h-px bg-cc-border" />
</div>
);
}
if (message.role === "user") {
return (
<div className="flex justify-end animate-[userSlideIn_0.3s_ease-out]">
<div className="max-w-[85%] sm:max-w-[76%] px-3.5 sm:px-4 py-2.5 rounded-[14px] rounded-br-[5px] user-bubble-gradient text-cc-fg border border-cc-border/50 shadow-[0_4px_16px_rgba(20,25,36,0.08)]">
{message.images && message.images.length > 0 && (
<div className="flex gap-2 flex-wrap mb-2">
{message.images.map((img, i) => (
<img
key={i}
src={`data:${img.media_type};base64,${img.data}`}
alt="attachment"
className="max-w-[150px] sm:max-w-[200px] max-h-[120px] sm:max-h-[150px] rounded-xl object-cover border border-cc-border/30"
/>
))}
</div>
)}
<div className="text-[13px] sm:text-[14px] leading-relaxed break-words">
<MarkdownContent text={message.content} />
</div>
</div>
</div>
);
}
// Assistant message
return (
<div className="animate-[fadeSlideIn_0.3s_ease-out] py-0.5">
<AssistantMessage message={message} />
</div>
);
}
interface ToolGroupItem {
id: string;
name: string;
input: Record<string, unknown>;
}
interface ToolUseInfo {
name: string;
input: Record<string, unknown>;
}
type GroupedBlock =
| { kind: "content"; block: ContentBlock }
| { kind: "tool_group"; name: string; items: ToolGroupItem[] };
function groupContentBlocks(blocks: ContentBlock[]): GroupedBlock[] {
const groups: GroupedBlock[] = [];
for (const block of blocks) {
if (block.type === "tool_use") {
const last = groups[groups.length - 1];
if (last?.kind === "tool_group" && last.name === block.name) {
last.items.push({ id: block.id, name: block.name, input: block.input });
} else {
groups.push({
kind: "tool_group",
name: block.name,
items: [{ id: block.id, name: block.name, input: block.input }],
});
}
} else {
groups.push({ kind: "content", block });
}
}
return groups;
}
function mapToolUsesById(blocks: ContentBlock[]): Map<string, ToolUseInfo> {
const map = new Map<string, ToolUseInfo>();
for (const block of blocks) {
if (block.type === "tool_use") {
map.set(block.id, { name: block.name, input: block.input });
}
}
return map;
}
function AssistantMessage({ message }: { message: ChatMessage }) {
const blocks = message.contentBlocks || [];
const grouped = useMemo(() => groupContentBlocks(blocks), [blocks]);
const toolUseById = useMemo(() => mapToolUsesById(blocks), [blocks]);
if (blocks.length === 0 && message.content) {
// During streaming thinking phase, render as faded italic inline text
const isThinkingPhase = message.isStreaming && message.streamingPhase === "thinking";
return (
<div className="flex items-start gap-3">
<AssistantAvatar />
<div className="flex-1 min-w-0">
{isThinkingPhase ? (
<ThinkingBlock text={message.content} />
) : (
<MarkdownContent text={message.content} showCursor={!!message.isStreaming} />
)}
</div>
</div>
);
}
return (
<div className="flex items-start gap-3">
<AssistantAvatar />
<div className="flex-1 min-w-0 space-y-3">
{grouped.map((group, i) => {
if (group.kind === "content") {
return <ContentBlockRenderer key={i} block={group.block} toolUseById={toolUseById} />;
}
// Single tool_use renders as before
if (group.items.length === 1) {
const item = group.items[0];
return <ToolBlock key={i} name={item.name} input={item.input} toolUseId={item.id} />;
}
// Grouped tool_uses
return <ToolGroupBlock key={i} name={group.name} items={group.items} />;
})}
</div>
</div>
);
}
function AssistantAvatar() {
return (
<div className="w-6 h-6 rounded-full avatar-ring flex items-center justify-center shrink-0 mt-0.5">
<div className="avatar-inner w-full h-full rounded-full flex items-center justify-center">
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5 text-cc-primary">
<path d="M8 2.2l1.9 3.8L14 7l-3 2.9.7 4.1L8 12.2 4.3 14l.7-4.1L2 7l4.1-1L8 2.2z" />
</svg>
</div>
</div>
);
}
function MarkdownContent({ text, showCursor = false }: { text: string; showCursor?: boolean }) {
return (
<div className="markdown-body text-[14px] sm:text-[15px] text-cc-fg/95 leading-relaxed overflow-hidden">
<Markdown
remarkPlugins={[remarkGfm]}
components={{
p: ({ children }) => (
<p className="mb-3 last:mb-0">{children}</p>
),
strong: ({ children }) => (
<strong className="font-semibold text-cc-fg">{children}</strong>
),
em: ({ children }) => (
<em className="italic">{children}</em>
),
h1: ({ children }) => (
<h1 className="text-xl font-bold text-cc-fg mt-4 mb-2">{children}</h1>
),
h2: ({ children }) => (
<h2 className="text-lg font-bold text-cc-fg mt-3 mb-2">{children}</h2>
),
h3: ({ children }) => (
<h3 className="text-base font-semibold text-cc-fg mt-3 mb-1">{children}</h3>
),
ul: ({ children }) => (
<ul className="list-disc pl-5 mb-3 space-y-1">{children}</ul>
),
ol: ({ children }) => (
<ol className="list-decimal pl-5 mb-3 space-y-1">{children}</ol>
),
li: ({ children }) => (
<li className="text-cc-fg">{children}</li>
),
a: ({ href, children }) => (
<a href={href} target="_blank" rel="noopener noreferrer" className="text-cc-primary hover:underline">
{children}
</a>
),
blockquote: ({ children }) => (
<blockquote className="border-l-2 border-cc-primary/30 pl-3 my-2 text-cc-muted italic">
{children}
</blockquote>
),
hr: () => (
<hr className="border-cc-border my-4" />
),
code: (props: ComponentProps<"code">) => {
const { children, className } = props;
const match = /language-(\w+)/.exec(className || "");
const isBlock = match || (typeof children === "string" && children.includes("\n"));
if (isBlock) {
const lang = match?.[1] || "";
return (
<div className="my-2.5 rounded-xl overflow-hidden border border-cc-border shadow-[0_2px_8px_rgba(0,0,0,0.04)]">
{lang && (
<div className="px-3 py-1.5 bg-cc-code-bg border-b border-cc-border flex items-center gap-2">
<div className="flex gap-1">
<span className="w-2 h-2 rounded-full bg-cc-muted/20" />
<span className="w-2 h-2 rounded-full bg-cc-muted/20" />
<span className="w-2 h-2 rounded-full bg-cc-muted/20" />
</div>
<span className="text-[10px] text-cc-muted/70 font-mono-code uppercase tracking-wider">
{lang}
</span>
</div>
)}
<pre className="px-3 sm:px-4 py-2.5 sm:py-3 bg-cc-code-bg text-cc-code-fg text-[12px] sm:text-[13px] font-mono-code leading-relaxed overflow-x-auto">
<code>{children}</code>
</pre>
</div>
);
}
return (
<code className="px-1.5 py-0.5 rounded-md bg-cc-fg/[0.06] text-[12.5px] font-mono-code text-cc-fg/80 border border-cc-border/40">
{children}
</code>
);
},
pre: ({ children }) => <>{children}</>,
table: ({ children }) => (
<div className="overflow-x-auto my-2">
<table className="min-w-full text-sm border border-cc-border rounded-lg overflow-hidden">
{children}
</table>
</div>
),
thead: ({ children }) => (
<thead className="bg-cc-code-bg/50">{children}</thead>
),
th: ({ children }) => (
<th className="px-3 py-1.5 text-left text-xs font-semibold text-cc-fg border-b border-cc-border">
{children}
</th>
),
td: ({ children }) => (
<td className="px-3 py-1.5 text-xs text-cc-fg border-b border-cc-border">
{children}
</td>
),
}}
>
{text}
</Markdown>
{showCursor && (
<span
data-testid="assistant-stream-cursor"
className="inline-block w-[3px] h-[18px] bg-cc-primary rounded-full ml-0.5 align-middle animate-[pulse-dot_1s_ease-in-out_infinite]"
/>
)}
</div>
);
}
function ContentBlockRenderer({
block,
toolUseById,
}: {
block: ContentBlock;
toolUseById: Map<string, ToolUseInfo>;
}) {
if (block.type === "text") {
return <MarkdownContent text={block.text} />;
}
if (block.type === "thinking") {
return <ThinkingBlock text={block.thinking} />;
}
if (block.type === "tool_use") {
return <ToolBlock name={block.name} input={block.input} toolUseId={block.id} />;
}
if (block.type === "tool_result") {
const content = typeof block.content === "string" ? block.content : JSON.stringify(block.content);
const linkedTool = toolUseById.get(block.tool_use_id);
const toolName = linkedTool?.name;
const isError = block.is_error ?? false;
if (toolName === "Bash") {
return <BashResultBlock text={content} isError={isError} />;
}
return (
<div className="rounded-lg bg-cc-code-bg overflow-hidden">
<pre className={`text-[12px] font-mono-code px-3 py-2 whitespace-pre-wrap leading-relaxed max-h-60 overflow-y-auto ${
isError ? "text-cc-error" : "text-cc-code-fg/60"
}`}>
{content}
</pre>
</div>
);
}
return null;
}
function BashResultBlock({ text, isError }: { text: string; isError: boolean }) {
const lines = text.split(/\r?\n/);
const hasMore = lines.length > 20;
const [showFull, setShowFull] = useState(false);
const rendered = showFull || !hasMore ? text : lines.slice(-20).join("\n");
return (
<div className="rounded-lg bg-cc-code-bg overflow-hidden">
<pre className={`text-[12px] font-mono-code px-3 py-2 whitespace-pre-wrap leading-relaxed ${
isError ? "text-cc-error" : "text-cc-code-fg/60"
}`}>
{rendered}
</pre>
{hasMore && (
<div className="px-3 pb-1.5 flex items-center justify-between">
<span className={`text-[10px] ${isError ? "text-cc-error/50" : "text-cc-muted/40"}`}>
{showFull ? `${lines.length} lines` : `last 20 of ${lines.length}`}
</span>
<button
onClick={() => setShowFull(!showFull)}
className="text-[10px] text-cc-muted/40 hover:text-cc-muted/70 transition-colors cursor-pointer"
>
{showFull ? "Show tail" : "Show all"}
</button>
</div>
)}
</div>
);
}
function ToolGroupBlock({ name, items }: { name: string; items: ToolGroupItem[] }) {
const [open, setOpen] = useState(false);
const iconType = getToolIcon(name);
const label = getToolLabel(name);
return (
<div className="border border-cc-border rounded-[10px] overflow-hidden bg-cc-card tool-card">
<button
onClick={() => setOpen(!open)}
className="w-full flex items-center gap-2.5 px-3 py-2 text-left hover:bg-cc-hover transition-colors cursor-pointer"
>
<svg
viewBox="0 0 16 16"
fill="currentColor"
className={`w-3 h-3 text-cc-muted transition-transform shrink-0 ${open ? "rotate-90" : ""}`}
>
<path d="M6 4l4 4-4 4" />
</svg>
<ToolIcon type={iconType} />
<span className="text-xs font-medium text-cc-fg">{label}</span>
<span className="text-[10px] text-cc-muted bg-cc-hover rounded-full px-1.5 py-0.5 tabular-nums">
{items.length}
</span>
</button>
{open && (
<div className="border-t border-cc-border px-3 py-1.5">
{items.map((item, i) => {
const preview = getPreview(item.name, item.input);
return (
<div key={item.id || i} className="flex items-center gap-2 py-1 text-xs text-cc-muted font-mono-code truncate">
<span className="w-1 h-1 rounded-full bg-cc-muted/40 shrink-0" />
<span className="truncate">{preview || JSON.stringify(item.input).slice(0, 80)}</span>
</div>
);
})}
</div>
)}
</div>
);
}
function ThinkingBlock({ text }: { text: string }) {
const normalized = text.trim();
const [expanded, setExpanded] = useState(false);
const lines = normalized.split("\n");
const isLong = lines.length > 8 || normalized.length > 600;
const displayed = isLong && !expanded
? lines.slice(0, 8).join("\n")
: normalized;
return (
<div>
<div className="markdown-body text-[13px] text-cc-fg/55 leading-relaxed italic">
<Markdown
remarkPlugins={[remarkGfm]}
components={{
p: ({ children }) => <p className="mb-2 last:mb-0">{children}</p>,
ul: ({ children }) => <ul className="list-disc pl-4 mb-2 space-y-0.5">{children}</ul>,
ol: ({ children }) => <ol className="list-decimal pl-4 mb-2 space-y-0.5">{children}</ol>,
li: ({ children }) => <li>{children}</li>,
code: ({ children }) => (
<code className="px-1 py-0.5 rounded bg-cc-fg/[0.03] text-cc-fg/55 font-mono-code text-[12px] not-italic">
{children}
</code>
),
}}
>
{displayed || "No thinking text captured."}
</Markdown>
</div>
{isLong && !expanded && (
<button
onClick={() => setExpanded(true)}
className="text-[11px] text-cc-muted/60 hover:text-cc-muted/85 cursor-pointer transition-colors"
>
Show more
</button>
)}
</div>
);
}