forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserMessage.tsx
More file actions
88 lines (76 loc) · 2.29 KB
/
Copy pathUserMessage.tsx
File metadata and controls
88 lines (76 loc) · 2.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
import { UserMessageProps } from "../props";
import { AttachmentRenderer } from "../AttachmentRenderer";
type UserMessageContent = NonNullable<UserMessageProps["message"]>["content"];
const getTextContent = (
content: UserMessageContent | undefined,
): string | undefined => {
if (typeof content === "undefined") {
return undefined;
}
if (typeof content === "string") {
return content;
}
return (
content
.map((part) => {
if (part.type === "text") {
return part.text;
}
return undefined;
})
.filter(
(value): value is string =>
typeof value === "string" && value.length > 0,
)
.join(" ")
.trim() || undefined
);
};
const getMediaParts = (content: UserMessageContent | undefined) => {
if (!content || typeof content === "string") return [];
return content.filter(
(part) =>
part.type === "image" ||
part.type === "audio" ||
part.type === "video" ||
part.type === "document",
) as Array<{
type: "image" | "audio" | "video" | "document";
source:
| { type: "data"; value: string; mimeType: string }
| { type: "url"; value: string; mimeType?: string };
}>;
};
export const UserMessage = (props: UserMessageProps) => {
const { message, ImageRenderer } = props;
const content = message?.content;
// Legacy path: old-style image field on message
const isLegacyImageMessage =
message && "image" in message && Boolean((message as any).image);
if (isLegacyImageMessage) {
const legacyImage = (message as any).image;
const textContent = getTextContent(content);
return (
<div className="copilotKitMessage copilotKitUserMessage">
<ImageRenderer image={legacyImage} content={textContent} />
</div>
);
}
const textContent = getTextContent(content);
const mediaParts = getMediaParts(content);
if (mediaParts.length === 0) {
return (
<div className="copilotKitMessage copilotKitUserMessage">
{textContent}
</div>
);
}
return (
<div className="copilotKitMessage copilotKitUserMessage">
{textContent && <div>{textContent}</div>}
{mediaParts.map((part, index) => (
<AttachmentRenderer key={index} type={part.type} source={part.source} />
))}
</div>
);
};