2025-09-21 11:02:31 -07:00
|
|
|
"use client";
|
|
|
|
|
import type { UseChatHelpers } from "@ai-sdk/react";
|
|
|
|
|
import equal from "fast-deep-equal";
|
|
|
|
|
import { memo, useState } from "react";
|
|
|
|
|
import type { Vote } from "@/lib/db/schema";
|
|
|
|
|
import type { ChatMessage } from "@/lib/types";
|
|
|
|
|
import { cn, sanitizeText } from "@/lib/utils";
|
|
|
|
|
import { useDataStream } from "./data-stream-provider";
|
|
|
|
|
import { DocumentToolResult } from "./document";
|
|
|
|
|
import { DocumentPreview } from "./document-preview";
|
|
|
|
|
import { MessageContent } from "./elements/message";
|
|
|
|
|
import { Response } from "./elements/response";
|
2025-08-28 14:15:36 +01:00
|
|
|
import {
|
|
|
|
|
Tool,
|
2025-09-21 12:03:29 +01:00
|
|
|
ToolContent,
|
2025-09-21 11:02:31 -07:00
|
|
|
ToolHeader,
|
2025-08-28 14:15:36 +01:00
|
|
|
ToolInput,
|
|
|
|
|
ToolOutput,
|
2025-09-21 11:02:31 -07:00
|
|
|
} from "./elements/tool";
|
|
|
|
|
import { SparklesIcon } from "./icons";
|
|
|
|
|
import { MessageActions } from "./message-actions";
|
|
|
|
|
import { MessageEditor } from "./message-editor";
|
|
|
|
|
import { MessageReasoning } from "./message-reasoning";
|
|
|
|
|
import { PreviewAttachment } from "./preview-attachment";
|
|
|
|
|
import { Weather } from "./weather";
|
2025-07-03 02:26:34 -07:00
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
const PurePreviewMessage = ({
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId,
|
|
|
|
|
message,
|
|
|
|
|
vote,
|
|
|
|
|
isLoading,
|
2024-12-05 15:29:33 +03:00
|
|
|
setMessages,
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate,
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly,
|
2025-11-29 13:02:24 +00:00
|
|
|
requiresScrollPadding: _requiresScrollPadding,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId: string;
|
2025-07-03 02:26:34 -07:00
|
|
|
message: ChatMessage;
|
2024-11-05 17:15:51 +03:00
|
|
|
vote: Vote | undefined;
|
|
|
|
|
isLoading: boolean;
|
2025-09-21 11:02:31 -07:00
|
|
|
setMessages: UseChatHelpers<ChatMessage>["setMessages"];
|
|
|
|
|
regenerate: UseChatHelpers<ChatMessage>["regenerate"];
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2025-05-01 02:28:24 -07:00
|
|
|
requiresScrollPadding: boolean;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) => {
|
2025-09-21 11:02:31 -07:00
|
|
|
const [mode, setMode] = useState<"view" | "edit">("view");
|
2024-12-05 15:29:33 +03:00
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
const attachmentsFromMessage = message.parts.filter(
|
2025-09-21 11:02:31 -07:00
|
|
|
(part) => part.type === "file"
|
2025-07-03 02:26:34 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useDataStream();
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
2025-11-29 13:02:24 +00:00
|
|
|
<div
|
|
|
|
|
className="group/message fade-in w-full animate-in duration-200"
|
2025-09-21 12:03:29 +01:00
|
|
|
data-role={message.role}
|
2025-09-21 11:02:31 -07:00
|
|
|
data-testid={`message-${message.role}`}
|
2025-09-07 00:04:51 +01:00
|
|
|
>
|
|
|
|
|
<div
|
2025-09-21 11:02:31 -07:00
|
|
|
className={cn("flex w-full items-start gap-2 md:gap-3", {
|
|
|
|
|
"justify-end": message.role === "user" && mode !== "edit",
|
|
|
|
|
"justify-start": message.role === "assistant",
|
2025-09-07 00:04:51 +01:00
|
|
|
})}
|
2024-10-30 16:01:24 +05:30
|
|
|
>
|
2025-09-21 11:02:31 -07:00
|
|
|
{message.role === "assistant" && (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="-mt-1 flex size-8 shrink-0 items-center justify-center rounded-full bg-background ring-1 ring-border">
|
2025-09-07 00:04:51 +01:00
|
|
|
<SparklesIcon size={14} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
<div
|
2025-09-21 11:02:31 -07:00
|
|
|
className={cn("flex flex-col", {
|
|
|
|
|
"gap-2 md:gap-4": message.parts?.some(
|
|
|
|
|
(p) => p.type === "text" && p.text?.trim()
|
2025-09-07 00:04:51 +01:00
|
|
|
),
|
2025-09-21 11:02:31 -07:00
|
|
|
"w-full":
|
|
|
|
|
(message.role === "assistant" &&
|
2025-09-07 00:04:51 +01:00
|
|
|
message.parts?.some(
|
2025-09-21 11:02:31 -07:00
|
|
|
(p) => p.type === "text" && p.text?.trim()
|
2025-09-07 00:04:51 +01:00
|
|
|
)) ||
|
2025-09-21 11:02:31 -07:00
|
|
|
mode === "edit",
|
|
|
|
|
"max-w-[calc(100%-2.5rem)] sm:max-w-[min(fit-content,80%)]":
|
|
|
|
|
message.role === "user" && mode !== "edit",
|
2025-09-01 11:07:07 +01:00
|
|
|
})}
|
2024-12-19 17:05:04 +05:30
|
|
|
>
|
2025-09-07 00:04:51 +01:00
|
|
|
{attachmentsFromMessage.length > 0 && (
|
|
|
|
|
<div
|
2025-09-09 15:44:07 -04:00
|
|
|
className="flex flex-row justify-end gap-2"
|
2025-09-21 11:02:31 -07:00
|
|
|
data-testid={"message-attachments"}
|
2025-09-07 00:04:51 +01:00
|
|
|
>
|
|
|
|
|
{attachmentsFromMessage.map((attachment) => (
|
|
|
|
|
<PreviewAttachment
|
|
|
|
|
attachment={{
|
2025-09-21 11:02:31 -07:00
|
|
|
name: attachment.filename ?? "file",
|
2025-09-07 00:04:51 +01:00
|
|
|
contentType: attachment.mediaType,
|
|
|
|
|
url: attachment.url,
|
|
|
|
|
}}
|
2025-09-21 11:02:31 -07:00
|
|
|
key={attachment.url}
|
2025-09-07 00:04:51 +01:00
|
|
|
/>
|
|
|
|
|
))}
|
2024-12-05 15:29:33 +03:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
{message.parts?.map((part, index) => {
|
|
|
|
|
const { type } = part;
|
|
|
|
|
const key = `message-${message.id}-part-${index}`;
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "reasoning" && part.text?.trim().length > 0) {
|
2025-09-07 00:04:51 +01:00
|
|
|
return (
|
|
|
|
|
<MessageReasoning
|
2025-09-21 12:03:29 +01:00
|
|
|
isLoading={isLoading}
|
2025-09-21 11:02:31 -07:00
|
|
|
key={key}
|
2025-09-07 00:04:51 +01:00
|
|
|
reasoning={part.text}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-02-03 20:33:15 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "text") {
|
|
|
|
|
if (mode === "view") {
|
2025-03-16 18:42:29 -07:00
|
|
|
return (
|
2025-09-07 00:04:51 +01:00
|
|
|
<div key={key}>
|
|
|
|
|
<MessageContent
|
|
|
|
|
className={cn({
|
2025-09-21 11:02:31 -07:00
|
|
|
"w-fit break-words rounded-2xl px-3 py-2 text-right text-white":
|
|
|
|
|
message.role === "user",
|
|
|
|
|
"bg-transparent px-0 py-0 text-left":
|
|
|
|
|
message.role === "assistant",
|
2025-09-07 00:04:51 +01:00
|
|
|
})}
|
2025-09-21 11:02:31 -07:00
|
|
|
data-testid="message-content"
|
2025-09-07 00:04:51 +01:00
|
|
|
style={
|
2025-09-21 11:02:31 -07:00
|
|
|
message.role === "user"
|
|
|
|
|
? { backgroundColor: "#006cff" }
|
2025-09-07 00:04:51 +01:00
|
|
|
: undefined
|
|
|
|
|
}
|
2025-09-01 11:07:07 +01:00
|
|
|
>
|
2025-09-07 00:04:51 +01:00
|
|
|
<Response>{sanitizeText(part.text)}</Response>
|
|
|
|
|
</MessageContent>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-03-16 18:42:29 -07:00
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (mode === "edit") {
|
2025-08-28 14:15:36 +01:00
|
|
|
return (
|
2025-09-07 00:04:51 +01:00
|
|
|
<div
|
2025-09-21 12:03:29 +01:00
|
|
|
className="flex w-full flex-row items-start gap-3"
|
2025-09-21 11:02:31 -07:00
|
|
|
key={key}
|
2025-09-07 00:04:51 +01:00
|
|
|
>
|
|
|
|
|
<div className="size-8" />
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="min-w-0 flex-1">
|
2025-09-07 00:04:51 +01:00
|
|
|
<MessageEditor
|
|
|
|
|
key={message.id}
|
|
|
|
|
message={message}
|
2025-09-21 12:03:29 +01:00
|
|
|
regenerate={regenerate}
|
2025-09-21 11:02:31 -07:00
|
|
|
setMessages={setMessages}
|
|
|
|
|
setMode={setMode}
|
2025-09-07 00:04:51 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-08-28 14:15:36 +01:00
|
|
|
);
|
2025-07-03 02:26:34 -07:00
|
|
|
}
|
2025-09-07 00:04:51 +01:00
|
|
|
}
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "tool-getWeather") {
|
2025-09-07 00:04:51 +01:00
|
|
|
const { toolCallId, state } = part;
|
2025-09-01 11:07:07 +01:00
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
return (
|
2025-09-21 11:02:31 -07:00
|
|
|
<Tool defaultOpen={true} key={toolCallId}>
|
|
|
|
|
<ToolHeader state={state} type="tool-getWeather" />
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolContent>
|
2025-09-21 11:02:31 -07:00
|
|
|
{state === "input-available" && (
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolInput input={part.input} />
|
|
|
|
|
)}
|
2025-09-21 11:02:31 -07:00
|
|
|
{state === "output-available" && (
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolOutput
|
2025-09-21 12:03:29 +01:00
|
|
|
errorText={undefined}
|
2025-09-21 11:02:31 -07:00
|
|
|
output={<Weather weatherAtLocation={part.output} />}
|
2025-09-07 00:04:51 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</ToolContent>
|
|
|
|
|
</Tool>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "tool-createDocument") {
|
2025-09-07 00:04:51 +01:00
|
|
|
const { toolCallId } = part;
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (part.output && "error" in part.output) {
|
2025-08-28 14:15:36 +01:00
|
|
|
return (
|
2025-09-07 00:04:51 +01:00
|
|
|
<div
|
2025-09-21 12:03:29 +01:00
|
|
|
className="rounded-lg border border-red-200 bg-red-50 p-4 text-red-500 dark:bg-red-950/50"
|
2025-09-21 11:02:31 -07:00
|
|
|
key={toolCallId}
|
2025-09-07 00:04:51 +01:00
|
|
|
>
|
|
|
|
|
Error creating document: {String(part.output.error)}
|
|
|
|
|
</div>
|
2025-08-28 14:15:36 +01:00
|
|
|
);
|
2025-07-03 02:26:34 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
return (
|
|
|
|
|
<DocumentPreview
|
2025-09-21 12:03:29 +01:00
|
|
|
isReadonly={isReadonly}
|
2025-09-21 11:02:31 -07:00
|
|
|
key={toolCallId}
|
2025-09-07 00:04:51 +01:00
|
|
|
result={part.output}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-07-03 02:26:34 -07:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "tool-updateDocument") {
|
2025-09-07 00:04:51 +01:00
|
|
|
const { toolCallId } = part;
|
2025-09-01 16:36:28 +01:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (part.output && "error" in part.output) {
|
2025-08-28 14:15:36 +01:00
|
|
|
return (
|
2025-09-07 00:04:51 +01:00
|
|
|
<div
|
2025-09-21 12:03:29 +01:00
|
|
|
className="rounded-lg border border-red-200 bg-red-50 p-4 text-red-500 dark:bg-red-950/50"
|
2025-09-21 11:02:31 -07:00
|
|
|
key={toolCallId}
|
2025-09-07 00:04:51 +01:00
|
|
|
>
|
|
|
|
|
Error updating document: {String(part.output.error)}
|
2025-09-01 11:07:07 +01:00
|
|
|
</div>
|
2025-08-28 14:15:36 +01:00
|
|
|
);
|
2025-07-03 02:26:34 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
return (
|
2025-09-21 11:02:31 -07:00
|
|
|
<div className="relative" key={toolCallId}>
|
2025-09-07 00:04:51 +01:00
|
|
|
<DocumentPreview
|
2025-09-21 11:02:31 -07:00
|
|
|
args={{ ...part.output, isUpdate: true }}
|
2025-09-07 00:04:51 +01:00
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
result={part.output}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-07-03 02:26:34 -07:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (type === "tool-requestSuggestions") {
|
2025-09-07 00:04:51 +01:00
|
|
|
const { toolCallId, state } = part;
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
return (
|
2025-09-21 11:02:31 -07:00
|
|
|
<Tool defaultOpen={true} key={toolCallId}>
|
|
|
|
|
<ToolHeader state={state} type="tool-requestSuggestions" />
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolContent>
|
2025-09-21 11:02:31 -07:00
|
|
|
{state === "input-available" && (
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolInput input={part.input} />
|
|
|
|
|
)}
|
2025-09-21 11:02:31 -07:00
|
|
|
{state === "output-available" && (
|
2025-09-07 00:04:51 +01:00
|
|
|
<ToolOutput
|
2025-09-21 11:02:31 -07:00
|
|
|
errorText={undefined}
|
2025-09-07 00:04:51 +01:00
|
|
|
output={
|
2025-09-21 11:02:31 -07:00
|
|
|
"error" in part.output ? (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="rounded border p-2 text-red-500">
|
2025-09-07 00:04:51 +01:00
|
|
|
Error: {String(part.output.error)}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<DocumentToolResult
|
2025-09-21 12:03:29 +01:00
|
|
|
isReadonly={isReadonly}
|
2025-09-21 11:02:31 -07:00
|
|
|
result={part.output}
|
|
|
|
|
type="request-suggestions"
|
2025-09-07 00:04:51 +01:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</ToolContent>
|
|
|
|
|
</Tool>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-21 11:02:31 -07:00
|
|
|
|
|
|
|
|
return null;
|
2025-09-07 00:04:51 +01:00
|
|
|
})}
|
|
|
|
|
|
|
|
|
|
{!isReadonly && (
|
|
|
|
|
<MessageActions
|
2025-09-21 12:03:29 +01:00
|
|
|
chatId={chatId}
|
|
|
|
|
isLoading={isLoading}
|
2025-09-21 11:02:31 -07:00
|
|
|
key={`action-${message.id}`}
|
|
|
|
|
message={message}
|
2025-09-21 12:03:29 +01:00
|
|
|
setMode={setMode}
|
2025-09-21 11:02:31 -07:00
|
|
|
vote={vote}
|
2025-09-07 00:04:51 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
2024-10-24 16:35:51 -04:00
|
|
|
</div>
|
2025-09-07 00:04:51 +01:00
|
|
|
</div>
|
2025-11-29 13:02:24 +00:00
|
|
|
</div>
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
};
|
2024-11-06 15:21:28 +03:00
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
export const PreviewMessage = memo(
|
|
|
|
|
PurePreviewMessage,
|
|
|
|
|
(prevProps, nextProps) => {
|
2025-09-21 11:02:31 -07:00
|
|
|
if (prevProps.isLoading !== nextProps.isLoading) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (prevProps.message.id !== nextProps.message.id) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (prevProps.requiresScrollPadding !== nextProps.requiresScrollPadding) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!equal(prevProps.message.parts, nextProps.message.parts)) {
|
2025-05-01 02:28:24 -07:00
|
|
|
return false;
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
|
|
|
|
if (!equal(prevProps.vote, nextProps.vote)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-12-10 17:54:10 +05:30
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
return false;
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
2024-12-03 17:49:38 +03:00
|
|
|
);
|
|
|
|
|
|
2024-11-06 15:21:28 +03:00
|
|
|
export const ThinkingMessage = () => {
|
|
|
|
|
return (
|
2025-11-29 13:02:24 +00:00
|
|
|
<div
|
|
|
|
|
className="group/message fade-in w-full animate-in duration-300"
|
|
|
|
|
data-role="assistant"
|
2025-09-21 11:02:31 -07:00
|
|
|
data-testid="message-assistant-loading"
|
2024-11-06 15:21:28 +03:00
|
|
|
>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex items-start justify-start gap-3">
|
|
|
|
|
<div className="-mt-1 flex size-8 shrink-0 items-center justify-center rounded-full bg-background ring-1 ring-border">
|
2025-11-29 13:02:24 +00:00
|
|
|
<div className="animate-pulse">
|
|
|
|
|
<SparklesIcon size={14} />
|
|
|
|
|
</div>
|
2024-11-06 15:21:28 +03:00
|
|
|
</div>
|
|
|
|
|
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex w-full flex-col gap-2 md:gap-4">
|
2025-11-29 13:02:24 +00:00
|
|
|
<div className="flex items-center gap-1 p-0 text-muted-foreground text-sm">
|
|
|
|
|
<span className="animate-pulse">Thinking</span>
|
|
|
|
|
<span className="inline-flex">
|
|
|
|
|
<span className="animate-bounce [animation-delay:0ms]">.</span>
|
|
|
|
|
<span className="animate-bounce [animation-delay:150ms]">.</span>
|
|
|
|
|
<span className="animate-bounce [animation-delay:300ms]">.</span>
|
|
|
|
|
</span>
|
2025-09-07 00:04:51 +01:00
|
|
|
</div>
|
2024-11-06 15:21:28 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-29 13:02:24 +00:00
|
|
|
</div>
|
2024-11-06 15:21:28 +03:00
|
|
|
);
|
|
|
|
|
};
|