-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage-bubble.tsx
More file actions
196 lines (183 loc) · 6.29 KB
/
message-bubble.tsx
File metadata and controls
196 lines (183 loc) · 6.29 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
'use client';
import { useState } from 'react';
import type { UIMessage, DynamicToolUIPart } from 'ai';
import { ToolInvocation } from './tool-invocation';
interface MessageBubbleProps {
message: UIMessage;
headers: Record<string, string> | undefined;
onRegenerate?: () => void;
onEdit?: (newText: string) => void;
}
function Badge({ label, value, color }: { label: string; value: string; color: string }) {
return (
<span className={`inline-flex items-center gap-1 rounded px-1.5 py-0.5 text-[10px] leading-tight ${color}`}>
<span className="text-zinc-600">{label}</span>
<span>{value}</span>
</span>
);
}
function StatusBadge({ status }: { status: string }) {
const color =
status === 'finished'
? 'bg-emerald-950 text-emerald-400'
: status === 'streaming'
? 'bg-amber-950 text-amber-400'
: status === 'aborted'
? 'bg-red-950 text-red-400'
: 'bg-zinc-900 text-zinc-500';
return (
<Badge
label="status"
value={status}
color={color}
/>
);
}
function bubbleClasses(isUser: boolean, status: string | undefined): string {
const base = 'rounded-lg px-3 py-2 text-sm leading-relaxed whitespace-pre-wrap';
if (isUser) {
return `${base} bg-zinc-800 text-zinc-200`;
}
if (status === 'streaming') {
return `${base} bg-zinc-900 text-zinc-300 border border-amber-900/40`;
}
if (status === 'finished') {
return `${base} bg-zinc-900 text-zinc-300 border border-emerald-900/40`;
}
if (status === 'aborted') {
return `${base} bg-zinc-900 text-zinc-300 border border-red-900/40`;
}
return `${base} bg-zinc-900 text-zinc-300 border border-zinc-800`;
}
export function MessageBubble({ message, headers, onRegenerate, onEdit }: MessageBubbleProps) {
const isUser = message.role === 'user';
const [editing, setEditing] = useState(false);
const textContent = message.parts
.filter((p): p is Extract<typeof p, { type: 'text' }> => p.type === 'text')
.map((p) => p.text)
.join('');
const [editText, setEditText] = useState(textContent);
const handleEditSubmit = (e: React.FormEvent) => {
e.preventDefault();
const trimmed = editText.trim();
if (!trimmed || !onEdit) return;
setEditing(false);
onEdit(trimmed);
};
const role = headers?.['x-ably-role'] ?? message.role;
const clientId = headers?.['x-ably-turn-client-id'];
const turnId = headers?.['x-ably-turn-id'];
const status = headers?.['x-ably-status'];
return (
<div className={`flex ${isUser ? 'justify-end' : 'justify-start'}`}>
<div className="max-w-[75%]">
{editing ? (
<form
onSubmit={handleEditSubmit}
className="flex flex-col gap-2"
>
<textarea
value={editText}
onChange={(e) => setEditText(e.target.value)}
className="rounded-md bg-zinc-900 border border-zinc-600 px-3 py-2 text-sm text-zinc-200 outline-none focus:border-zinc-400 min-w-[300px] resize-y"
rows={3}
autoFocus
onKeyDown={(e) => {
if (e.key === 'Escape') {
setEditing(false);
setEditText(textContent);
}
}}
/>
<div className="flex gap-2 justify-end">
<button
type="button"
onClick={() => {
setEditing(false);
setEditText(textContent);
}}
className="text-xs text-zinc-500 hover:text-zinc-300 transition-colors rounded bg-zinc-800/60 px-2 py-1"
>
Cancel
</button>
<button
type="submit"
disabled={!editText.trim()}
className="text-xs text-zinc-200 hover:bg-zinc-600 transition-colors rounded bg-zinc-700 px-2 py-1 disabled:opacity-40"
>
Save & send
</button>
</div>
</form>
) : (
<div className={bubbleClasses(isUser, status)}>
{message.parts.map((part, i) => {
if (part.type === 'text') return <span key={i}>{part.text}</span>;
if (part.type === 'dynamic-tool')
return (
<ToolInvocation
key={i}
part={part as DynamicToolUIPart}
/>
);
return null;
})}
{!isUser && status === 'streaming' && (
<span className="inline-block w-1.5 h-3.5 ml-0.5 bg-amber-500/60 animate-pulse rounded-sm align-text-bottom" />
)}
</div>
)}
<div className="mt-1 flex items-center gap-1.5 flex-wrap">
{/* Edit button (user messages) */}
{onEdit && !editing && (
<button
onClick={() => {
setEditText(textContent);
setEditing(true);
}}
className="text-[10px] text-zinc-500 hover:text-zinc-200 transition-colors rounded bg-zinc-800/60 px-1.5 py-0.5"
title="Edit message"
>
edit
</button>
)}
{/* Regenerate button (assistant messages) */}
{onRegenerate && status !== 'streaming' && (
<button
onClick={onRegenerate}
className="text-[10px] text-zinc-500 hover:text-zinc-200 transition-colors rounded bg-zinc-800/60 px-1.5 py-0.5"
title="Regenerate response"
>
regenerate
</button>
)}
{/* Debug badges */}
{headers && (
<>
<Badge
label="role"
value={role}
color="bg-zinc-900 text-zinc-500"
/>
{clientId && (
<Badge
label="client"
value={clientId}
color="bg-zinc-900 text-zinc-500"
/>
)}
{turnId && (
<Badge
label="turn"
value={turnId.slice(0, 8)}
color="bg-zinc-900 text-zinc-500"
/>
)}
{status && <StatusBadge status={status} />}
</>
)}
</div>
</div>
</div>
);
}