-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathassistant.tsx
More file actions
170 lines (153 loc) · 5.36 KB
/
assistant.tsx
File metadata and controls
170 lines (153 loc) · 5.36 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
import { cn, Tooltip } from "@heroui/react";
import { GeneralToolCard } from "./tools/general";
import { useCallback, useEffect, useMemo, useState } from "react";
import googleAnalytics from "@/libs/google-analytics";
import { getProjectId } from "@/libs/helpers";
import MarkdownComponent from "../markdown";
import { TextPatches } from "../text-patches";
import { useAuthStore } from "@/stores/auth-store";
import { Icon } from "@iconify/react/dist/iconify.js";
// Helper functions
interface ParsedMessage {
regularContent: string;
paperDebuggerContent: string[];
}
const parseMessage = (message: string): ParsedMessage => {
const regex = /<PaperDebugger>([\s\S]*?)<\/PaperDebugger>/g;
const paperDebuggerContents: string[] = [];
let regularContent = message;
// Extract all PaperDebugger blocks
regularContent = message.replace(regex, (_, content) => {
const processedContent = content.replace(/\n/g, "§NEWLINE§");
paperDebuggerContents.push(processedContent);
return ""; // Remove the tag from regular content
});
return {
regularContent: regularContent.trim(),
paperDebuggerContent: paperDebuggerContents,
};
};
export const AssistantMessageContainer = ({
message,
reasoning,
messageId,
animated,
prevAttachment,
stale,
preparing,
}: {
message: string;
reasoning?: string;
messageId: string;
animated: boolean;
prevAttachment: string;
stale: boolean;
preparing: boolean;
}) => {
const parsedMessage = useMemo(() => parseMessage(message), [message]);
const { user } = useAuthStore();
const projectId = getProjectId();
const [copySuccess, setCopySuccess] = useState(false);
// Auto-collapse reasoning when message content arrives
const [isReasoningCollapsed, setIsReasoningCollapsed] = useState(true);
useEffect(() => {
const hasReasoning = (reasoning?.length ?? 0) > 0;
const hasMessage = (parsedMessage.regularContent?.length ?? 0) > 0 || parsedMessage.paperDebuggerContent.length > 0;
// Auto-expand when reasoning arrives
if (hasReasoning && !hasMessage) {
setIsReasoningCollapsed(false);
}
// Auto-collapse when message content arrives
if (hasReasoning && hasMessage) {
setIsReasoningCollapsed(true);
}
}, [reasoning, parsedMessage]);
const handleCopy = useCallback(() => {
if (message) {
googleAnalytics.fireEvent(user?.id, "messagecard_copy_message", {
projectId,
messageId: messageId,
});
navigator.clipboard.writeText(message);
setCopySuccess(true);
setTimeout(() => {
setCopySuccess(false);
}, 2000);
}
}, [user?.id, projectId, message, messageId]);
const showMessage =
(parsedMessage.regularContent?.length ?? 0) > 0 ||
parsedMessage.paperDebuggerContent.length > 0 ||
(reasoning?.length ?? 0) > 0;
const staleComponent = stale && <div className="message-box-stale-description">This message is stale.</div>;
const writingIndicator =
stale || !showMessage ? null : (
<Icon
icon="tabler:pencil"
className={cn(
"w-4! h-4! text-[14px]! text-gray-400! animate-bounce!",
"transition-all! duration-300! ease-in-out!",
"inline-block! align-middle! ml-1!",
preparing && "opacity-100!",
!preparing && "opacity-0! hidden!",
)}
/>
);
const reasoningComponent = reasoning && (
<GeneralToolCard
functionName="reasoning"
message={reasoning}
animated={animated}
isCollapsed={isReasoningCollapsed}
onToggleCollapse={() => setIsReasoningCollapsed(!isReasoningCollapsed)}
isLoading={preparing}
/>
);
return (
showMessage && (
<div className="chat-message-entry noselect">
<div className={cn("message-box-assistant rnd-cancel", messageId.startsWith("error-") && "text-red-500!")}>
{/* Reasoning content */}
{reasoningComponent}
{/* Message content */}
<div className="canselect">
{/* Regular markdown content */}
{parsedMessage.regularContent && (
<MarkdownComponent prevAttachment={prevAttachment} animated={animated}>
{parsedMessage.regularContent}
</MarkdownComponent>
)}
{/* PaperDebugger blocks */}
{parsedMessage.paperDebuggerContent.map((content) => (
<TextPatches key={content} attachment={prevAttachment}>
{content}
</TextPatches>
))}
</div>
{writingIndicator}
{/* Stale message */}
{staleComponent}
{((parsedMessage.regularContent?.length || 0) > 0 || parsedMessage.paperDebuggerContent.length > 0) && (
<div className="actions rnd-cancel noselect">
<Tooltip content="Copy" placement="bottom" size="sm" delay={1000}>
<span
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
handleCopy();
}
}}
tabIndex={0}
role="button"
aria-label="Copy message"
>
<Icon icon={copySuccess ? "tabler:copy-check" : "tabler:copy"} className="icon" />
</span>
</Tooltip>
</div>
)}
</div>
</div>
)
);
};