-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathassistant-message.tsx
More file actions
178 lines (173 loc) · 5.25 KB
/
Copy pathassistant-message.tsx
File metadata and controls
178 lines (173 loc) · 5.25 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
import { GitBranch } from "lucide-react";
import { motion } from "motion/react";
import dynamic from "next/dynamic";
import DogIcon from "@/components/icons/dog-icon";
const MarkdownRenderer = dynamic(
() =>
import("@/components/markdown-renderer").then((m) => ({
default: m.MarkdownRenderer,
})),
{ ssr: false },
);
import { trackButton } from "@/lib/analytics";
import { cn } from "@/lib/utils";
import type {
FunctionCall,
TokenUsage as TokenUsageType,
} from "../_types/types";
import { FunctionCalls } from "./function-calls";
import { Message } from "./message";
import MessageActions from "./message-actions";
import { TokenUsage } from "./token-usage";
interface AssistantMessageProps {
content: string;
functionCalls?: FunctionCall[];
messageIndex?: number;
expandedFunctionCalls: Set<string>;
onToggle: (functionCallId: string) => void;
isStreaming?: boolean;
showForkButton?: boolean;
onFork?: (e: React.MouseEvent) => void;
isCompleted?: boolean;
isInactive?: boolean;
animate?: boolean;
delay?: number;
isInitialGreeting?: boolean;
usage?: TokenUsageType;
timestamp?: Date;
showFeedback?: boolean;
}
export function AssistantMessage({
content,
functionCalls = [],
messageIndex,
expandedFunctionCalls,
onToggle,
isStreaming = false,
showForkButton = false,
onFork,
isCompleted = false,
isInactive = false,
animate = true,
delay = 0.2,
isInitialGreeting = false,
usage,
timestamp,
showFeedback = true,
}: AssistantMessageProps) {
const trackFeedback = (feedback: "like" | "dislike") => {
trackButton({
action: feedback,
elementId: "message-feedback",
namespace: "chat",
CTA: feedback === "like" ? "Like Message" : "Dislike Message",
timestamp: timestamp?.getTime(),
});
};
return (
<motion.div
initial={animate ? { opacity: 0, y: -20 } : { opacity: 1, y: 0 }}
animate={{ opacity: 1, y: 0 }}
transition={
animate
? { duration: 0.4, delay: delay, ease: "easeOut" }
: { duration: 0 }
}
className={isCompleted ? "opacity-50" : ""}
>
<Message
icon={
<div className="w-8 h-8 flex items-center justify-center flex-shrink-0 select-none">
{/* Dog icon with bark animation when greeting */}
<motion.div
initial={isInitialGreeting ? { rotate: -5, y: -1 } : false}
animate={
isInitialGreeting
? {
rotate: [-5, -8, -5, 0],
y: [-1, -2, -1, 0],
}
: {}
}
transition={
isInitialGreeting
? {
duration: 0.8,
times: [0, 0.4, 0.7, 1],
ease: "easeInOut",
}
: {}
}
>
<DogIcon
className="h-6 w-6 transition-colors duration-300"
disabled={isCompleted || isInactive}
/>
</motion.div>
</div>
}
actions={
showForkButton && onFork ? (
<button
type="button"
onClick={onFork}
className="opacity-0 group-hover:opacity-100 transition-opacity p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground"
title="Fork conversation from here"
>
<GitBranch className="h-3 w-3" />
</button>
) : undefined
}
>
<FunctionCalls
functionCalls={functionCalls}
messageIndex={messageIndex}
expandedFunctionCalls={expandedFunctionCalls}
onToggle={onToggle}
/>
<div className="relative">
{/* Slide animation for initial greeting */}
<motion.div
initial={isInitialGreeting ? { opacity: 0, x: -16 } : false}
animate={
isInitialGreeting
? {
opacity: [0, 0, 1, 1],
x: [-16, -8, 0, 0],
}
: {}
}
transition={
isInitialGreeting
? {
duration: 0.8,
times: [0, 0.3, 0.6, 1],
ease: "easeOut",
}
: {}
}
>
<MarkdownRenderer
className={cn(
"text-sm py-1.5 transition-colors duration-300",
isCompleted ? "text-placeholder-foreground" : "text-foreground",
)}
chatMessage={
isStreaming
? content.trim()
? content +
' <span class="inline-block w-1 h-4 bg-primary ml-1 animate-pulse"></span>'
: '<span class="text-muted-foreground italic">Thinking<span class="thinking-dots"></span></span>'
: content
}
/>
{usage && !isStreaming && <TokenUsage usage={usage} />}
{!isInitialGreeting && showFeedback && (
<MessageActions trackFeedback={trackFeedback} />
)}
</motion.div>
</div>
</Message>
</motion.div>
);
}