forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChatUserMessage.tsx
More file actions
445 lines (412 loc) · 11.8 KB
/
Copy pathCopilotChatUserMessage.tsx
File metadata and controls
445 lines (412 loc) · 11.8 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import { useMemo, useState } from "react";
import { Copy, Check, Edit, ChevronLeft, ChevronRight } from "lucide-react";
import {
useCopilotChatConfiguration,
CopilotChatDefaultLabels,
} from "../../providers/CopilotChatConfigurationProvider";
import { twMerge } from "tailwind-merge";
import { Button } from "../../components/ui/button";
import { UserMessage } from "@ag-ui/core";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "../../components/ui/tooltip";
import { renderSlot, WithSlots } from "../../lib/slots";
import {
type ImageInputPart,
type AudioInputPart,
type VideoInputPart,
type DocumentInputPart,
copyToClipboard,
} from "@copilotkit/shared";
import { CopilotChatAttachmentRenderer } from "./CopilotChatAttachmentRenderer";
function flattenUserMessageContent(content?: UserMessage["content"]): string {
if (!content) {
return "";
}
if (typeof content === "string") {
return content;
}
return content
.map((part) => {
if (
part &&
typeof part === "object" &&
"type" in part &&
(part as { type?: unknown }).type === "text" &&
typeof (part as { text?: unknown }).text === "string"
) {
return (part as { text: string }).text;
}
return "";
})
.filter((text) => text.length > 0)
.join("\n");
}
type MediaPart =
| ImageInputPart
| AudioInputPart
| VideoInputPart
| DocumentInputPart;
function getMediaParts(content: UserMessage["content"]): MediaPart[] {
if (!content || typeof content === "string") return [];
return content.filter(
(part): part is MediaPart =>
part.type === "image" ||
part.type === "audio" ||
part.type === "video" ||
part.type === "document",
);
}
function getFilename(part: MediaPart): string | undefined {
const meta = part.metadata;
if (
meta != null &&
typeof meta === "object" &&
"filename" in meta &&
typeof meta.filename === "string"
) {
return meta.filename;
}
return undefined;
}
export interface CopilotChatUserMessageOnEditMessageProps {
message: UserMessage;
}
export interface CopilotChatUserMessageOnSwitchToBranchProps {
message: UserMessage;
branchIndex: number;
numberOfBranches: number;
}
export type CopilotChatUserMessageProps = WithSlots<
{
messageRenderer: typeof CopilotChatUserMessage.MessageRenderer;
toolbar: typeof CopilotChatUserMessage.Toolbar;
copyButton: typeof CopilotChatUserMessage.CopyButton;
editButton: typeof CopilotChatUserMessage.EditButton;
branchNavigation: typeof CopilotChatUserMessage.BranchNavigation;
},
{
onEditMessage?: (props: CopilotChatUserMessageOnEditMessageProps) => void;
onSwitchToBranch?: (
props: CopilotChatUserMessageOnSwitchToBranchProps,
) => void;
message: UserMessage;
branchIndex?: number;
numberOfBranches?: number;
additionalToolbarItems?: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>
>;
export function CopilotChatUserMessage({
message,
onEditMessage,
branchIndex,
numberOfBranches,
onSwitchToBranch,
additionalToolbarItems,
messageRenderer,
toolbar,
copyButton,
editButton,
branchNavigation,
children,
className,
...props
}: CopilotChatUserMessageProps) {
const flattenedContent = useMemo(
() => flattenUserMessageContent(message.content),
[message.content],
);
const mediaParts = useMemo(
() => getMediaParts(message.content),
[message.content],
);
const BoundMessageRenderer = renderSlot(
messageRenderer,
CopilotChatUserMessage.MessageRenderer,
{
content: flattenedContent,
},
);
const BoundCopyButton = renderSlot(
copyButton,
CopilotChatUserMessage.CopyButton,
{
onClick: async () => {
if (flattenedContent) {
return await copyToClipboard(flattenedContent);
}
return false;
},
},
);
const BoundEditButton = renderSlot(
editButton,
CopilotChatUserMessage.EditButton,
{
onClick: () => onEditMessage?.({ message }),
},
);
const BoundBranchNavigation = renderSlot(
branchNavigation,
CopilotChatUserMessage.BranchNavigation,
{
currentBranch: branchIndex,
numberOfBranches,
onSwitchToBranch,
message,
},
);
const showBranchNavigation =
numberOfBranches && numberOfBranches > 1 && onSwitchToBranch;
const BoundToolbar = renderSlot(toolbar, CopilotChatUserMessage.Toolbar, {
children: (
<div className="cpk:flex cpk:items-center cpk:gap-1 cpk:justify-end">
{additionalToolbarItems}
{BoundCopyButton}
{onEditMessage && BoundEditButton}
{showBranchNavigation && BoundBranchNavigation}
</div>
),
});
if (children) {
return (
<div data-copilotkit style={{ display: "contents" }}>
{children({
messageRenderer: BoundMessageRenderer,
toolbar: BoundToolbar,
copyButton: BoundCopyButton,
editButton: BoundEditButton,
branchNavigation: BoundBranchNavigation,
message,
branchIndex,
numberOfBranches,
additionalToolbarItems,
})}
</div>
);
}
return (
<div
data-copilotkit
data-testid="copilot-user-message"
className={twMerge(
"copilotKitMessage copilotKitUserMessage cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10",
className,
)}
data-message-id={message.id}
{...props}
>
{mediaParts.length > 0 && (
<div className="cpk:flex cpk:flex-row cpk:flex-wrap cpk:justify-end cpk:gap-2 cpk:mb-2">
{mediaParts.map((part, index) => (
<CopilotChatAttachmentRenderer
key={index}
type={part.type}
source={part.source}
filename={getFilename(part)}
/>
))}
</div>
)}
{BoundMessageRenderer}
{BoundToolbar}
</div>
);
}
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CopilotChatUserMessage {
export const Container: React.FC<
React.PropsWithChildren<React.HTMLAttributes<HTMLDivElement>>
> = ({ children, className, ...props }) => (
<div
className={twMerge(
"cpk:flex cpk:flex-col cpk:items-end cpk:group",
className,
)}
{...props}
>
{children}
</div>
);
export const MessageRenderer: React.FC<{
content: string;
className?: string;
}> = ({ content, className }) => (
<div
className={twMerge(
"cpk:prose cpk:dark:prose-invert cpk:bg-muted cpk:relative cpk:max-w-[80%] cpk:rounded-[18px] cpk:px-4 cpk:py-1.5 cpk:data-[multiline]:py-3 cpk:inline-block cpk:whitespace-pre-wrap",
className,
)}
>
{content}
</div>
);
export const Toolbar: React.FC<React.HTMLAttributes<HTMLDivElement>> = ({
className,
...props
}) => (
<div
data-testid="copilot-user-toolbar"
className={twMerge(
"cpk:w-full cpk:bg-transparent cpk:flex cpk:items-center cpk:justify-end cpk:-mr-[5px] cpk:mt-[4px] cpk:invisible cpk:group-hover:visible",
className,
)}
{...props}
/>
);
export const ToolbarButton: React.FC<
React.ButtonHTMLAttributes<HTMLButtonElement> & {
title: string;
children: React.ReactNode;
}
> = ({ title, children, className, ...props }) => {
return (
<Tooltip>
<TooltipTrigger asChild>
<Button
type="button"
variant="assistantMessageToolbarButton"
aria-label={title}
className={twMerge(className)}
{...props}
>
{children}
</Button>
</TooltipTrigger>
<TooltipContent side="bottom">
<p>{title}</p>
</TooltipContent>
</Tooltip>
);
};
export const CopyButton: React.FC<
React.ButtonHTMLAttributes<HTMLButtonElement> & { copied?: boolean }
> = ({ className, title, onClick, ...props }) => {
const config = useCopilotChatConfiguration();
const labels = config?.labels ?? CopilotChatDefaultLabels;
const [copied, setCopied] = useState(false);
const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
let success = false;
if (onClick) {
// onClick may return a boolean indicating copy success
const result = await Promise.resolve(onClick(event));
success = result === true;
}
if (success) {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
return (
<ToolbarButton
data-testid="copilot-user-copy-button"
title={title || labels.userMessageToolbarCopyMessageLabel}
onClick={handleClick}
className={className}
{...props}
>
{copied ? (
<Check className="cpk:size-[18px]" />
) : (
<Copy className="cpk:size-[18px]" />
)}
</ToolbarButton>
);
};
export const EditButton: React.FC<
React.ButtonHTMLAttributes<HTMLButtonElement>
> = ({ className, title, ...props }) => {
const config = useCopilotChatConfiguration();
const labels = config?.labels ?? CopilotChatDefaultLabels;
return (
<ToolbarButton
data-testid="copilot-edit-button"
title={title || labels.userMessageToolbarEditMessageLabel}
className={className}
{...props}
>
<Edit className="cpk:size-[18px]" />
</ToolbarButton>
);
};
export const BranchNavigation: React.FC<
React.HTMLAttributes<HTMLDivElement> & {
currentBranch?: number;
numberOfBranches?: number;
onSwitchToBranch?: (
props: CopilotChatUserMessageOnSwitchToBranchProps,
) => void;
message: UserMessage;
}
> = ({
className,
currentBranch = 0,
numberOfBranches = 1,
onSwitchToBranch,
message,
...props
}) => {
if (!numberOfBranches || numberOfBranches <= 1 || !onSwitchToBranch) {
return null;
}
const canGoPrev = currentBranch > 0;
const canGoNext = currentBranch < numberOfBranches - 1;
return (
<div
data-testid="copilot-branch-navigation"
className={twMerge("cpk:flex cpk:items-center cpk:gap-1", className)}
{...props}
>
<Button
type="button"
variant="assistantMessageToolbarButton"
onClick={() =>
onSwitchToBranch?.({
branchIndex: currentBranch - 1,
numberOfBranches,
message,
})
}
disabled={!canGoPrev}
className="cpk:h-6 cpk:w-6 cpk:p-0"
>
<ChevronLeft className="cpk:size-[20px]" />
</Button>
<span className="cpk:text-sm cpk:text-muted-foreground cpk:px-0 cpk:font-medium">
{currentBranch + 1}/{numberOfBranches}
</span>
<Button
type="button"
variant="assistantMessageToolbarButton"
onClick={() =>
onSwitchToBranch?.({
branchIndex: currentBranch + 1,
numberOfBranches,
message,
})
}
disabled={!canGoNext}
className="cpk:h-6 cpk:w-6 cpk:p-0"
>
<ChevronRight className="cpk:size-[20px]" />
</Button>
</div>
);
};
}
CopilotChatUserMessage.Container.displayName =
"CopilotChatUserMessage.Container";
CopilotChatUserMessage.MessageRenderer.displayName =
"CopilotChatUserMessage.MessageRenderer";
CopilotChatUserMessage.Toolbar.displayName = "CopilotChatUserMessage.Toolbar";
CopilotChatUserMessage.ToolbarButton.displayName =
"CopilotChatUserMessage.ToolbarButton";
CopilotChatUserMessage.CopyButton.displayName =
"CopilotChatUserMessage.CopyButton";
CopilotChatUserMessage.EditButton.displayName =
"CopilotChatUserMessage.EditButton";
CopilotChatUserMessage.BranchNavigation.displayName =
"CopilotChatUserMessage.BranchNavigation";
export default CopilotChatUserMessage;