2026-05-13 06:27:53 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
import { Check, ChevronRight, Copy, FileIcon, ImageIcon, PlaySquare, Sparkles, Wrench } from "lucide-react";
|
2026-04-19 06:39:06 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-04-21 10:29:47 +00:00
|
|
|
import { ImageLightbox } from "@/components/ImageLightbox";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { MarkdownText } from "@/components/MarkdownText";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-04-24 19:17:58 +00:00
|
|
|
import type { UIImage, UIMediaAttachment, UIMessage } from "@/lib/types";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
|
|
|
|
interface MessageBubbleProps {
|
|
|
|
|
message: UIMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render a single message. Following agent-chat-ui: user turns are a rounded
|
|
|
|
|
* "pill" right-aligned with a muted fill; assistant turns render as bare
|
|
|
|
|
* markdown so prose/code read like a document rather than a chat bubble.
|
|
|
|
|
* Each turn fades+slides in for a touch of motion polish.
|
|
|
|
|
*
|
|
|
|
|
* Trace rows (tool-call hints, progress breadcrumbs) render as a subdued
|
|
|
|
|
* collapsible group so intermediate steps never masquerade as replies.
|
|
|
|
|
*/
|
|
|
|
|
export function MessageBubble({ message }: MessageBubbleProps) {
|
2026-05-06 14:15:36 +00:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
|
const copyResetRef = useRef<number | null>(null);
|
2026-04-18 18:51:53 +00:00
|
|
|
const baseAnim = "animate-in fade-in-0 slide-in-from-bottom-1 duration-300";
|
|
|
|
|
|
2026-05-06 14:15:36 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
if (copyResetRef.current !== null) {
|
|
|
|
|
window.clearTimeout(copyResetRef.current);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onCopyAssistantReply = useCallback(() => {
|
|
|
|
|
if (!navigator.clipboard) return;
|
|
|
|
|
void navigator.clipboard.writeText(message.content).then(() => {
|
|
|
|
|
setCopied(true);
|
|
|
|
|
if (copyResetRef.current !== null) {
|
|
|
|
|
window.clearTimeout(copyResetRef.current);
|
|
|
|
|
}
|
|
|
|
|
copyResetRef.current = window.setTimeout(() => {
|
|
|
|
|
setCopied(false);
|
|
|
|
|
copyResetRef.current = null;
|
|
|
|
|
}, 1_500);
|
|
|
|
|
});
|
|
|
|
|
}, [message.content]);
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
if (message.kind === "trace") {
|
|
|
|
|
return <TraceGroup message={message} animClass={baseAnim} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message.role === "user") {
|
2026-04-21 10:29:47 +00:00
|
|
|
const images = message.images ?? [];
|
2026-04-24 19:17:58 +00:00
|
|
|
const media = message.media ?? [];
|
2026-04-21 10:29:47 +00:00
|
|
|
const hasImages = images.length > 0;
|
2026-04-24 19:17:58 +00:00
|
|
|
const hasMedia = media.length > 0;
|
2026-04-21 10:29:47 +00:00
|
|
|
const hasText = message.content.trim().length > 0;
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2026-04-21 10:29:47 +00:00
|
|
|
"group ml-auto flex max-w-[min(85%,36rem)] flex-col items-end gap-1.5",
|
2026-04-18 18:51:53 +00:00
|
|
|
baseAnim,
|
|
|
|
|
)}
|
|
|
|
|
>
|
2026-04-24 19:17:58 +00:00
|
|
|
{hasImages ? <UserImages images={images} align="right" /> : null}
|
|
|
|
|
{!hasImages && hasMedia ? (
|
|
|
|
|
<MessageMedia media={media} align="right" />
|
|
|
|
|
) : null}
|
2026-04-21 10:29:47 +00:00
|
|
|
{hasText ? (
|
|
|
|
|
<p
|
|
|
|
|
className={cn(
|
2026-04-23 07:43:21 +00:00
|
|
|
"ml-auto w-fit rounded-[18px] bg-secondary/70 px-4 py-2",
|
2026-05-08 15:31:52 +00:00
|
|
|
"text-left text-[16px]/[1.75] whitespace-pre-wrap break-words",
|
2026-04-21 10:29:47 +00:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{message.content}
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
2026-04-18 18:51:53 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const empty = message.content.trim().length === 0;
|
2026-04-24 19:17:58 +00:00
|
|
|
const media = message.media ?? [];
|
2026-05-13 06:27:53 +00:00
|
|
|
const reasoning = message.role === "assistant" ? message.reasoning ?? [] : [];
|
2026-05-06 14:15:36 +00:00
|
|
|
const showAssistantActions = message.role === "assistant" && !message.isStreaming && !empty;
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
2026-05-08 15:31:52 +00:00
|
|
|
<div className={cn("w-full text-[15px]", baseAnim)} style={{ lineHeight: "var(--cjk-line-height)" }}>
|
2026-05-13 06:27:53 +00:00
|
|
|
{reasoning.length > 0 ? <ReasoningBubble lines={reasoning} /> : null}
|
|
|
|
|
{empty && message.isStreaming && reasoning.length === 0 ? (
|
2026-04-18 18:51:53 +00:00
|
|
|
<TypingDots />
|
2026-05-13 06:27:53 +00:00
|
|
|
) : empty && message.isStreaming ? null : (
|
2026-04-18 18:51:53 +00:00
|
|
|
<>
|
|
|
|
|
<MarkdownText>{message.content}</MarkdownText>
|
|
|
|
|
{message.isStreaming && <StreamCursor />}
|
2026-04-24 19:17:58 +00:00
|
|
|
{media.length > 0 ? <MessageMedia media={media} align="left" /> : null}
|
2026-05-06 14:15:36 +00:00
|
|
|
{showAssistantActions ? (
|
|
|
|
|
<div className="mt-2 flex items-center gap-1 text-muted-foreground">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onCopyAssistantReply}
|
|
|
|
|
aria-label={copied ? t("message.copiedReply") : t("message.copyReply")}
|
|
|
|
|
title={copied ? t("message.copiedReply") : t("message.copyReply")}
|
|
|
|
|
className={cn(
|
|
|
|
|
"inline-flex h-8 w-8 items-center justify-center rounded-full",
|
|
|
|
|
"transition-colors hover:bg-muted/55 hover:text-foreground",
|
|
|
|
|
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{copied ? (
|
|
|
|
|
<Check className="h-4 w-4" aria-hidden />
|
|
|
|
|
) : (
|
|
|
|
|
<Copy className="h-4 w-4" aria-hidden />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-04-18 18:51:53 +00:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 19:17:58 +00:00
|
|
|
function MessageMedia({
|
|
|
|
|
media,
|
|
|
|
|
align,
|
|
|
|
|
}: {
|
|
|
|
|
media: UIMediaAttachment[];
|
|
|
|
|
align: "left" | "right";
|
|
|
|
|
}) {
|
|
|
|
|
if (media.length === 0) return null;
|
|
|
|
|
const images = media
|
|
|
|
|
.filter((item) => item.kind === "image")
|
|
|
|
|
.map(({ url, name }) => ({ url, name }));
|
|
|
|
|
const nonImages = media.filter((item) => item.kind !== "image");
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"mt-2 flex flex-wrap gap-2",
|
|
|
|
|
align === "right" ? "justify-end" : "justify-start",
|
|
|
|
|
)}
|
|
|
|
|
>
|
2026-05-08 09:40:15 +00:00
|
|
|
{images.length > 0 ? (
|
|
|
|
|
<UserImages images={images} align={align} size={align === "left" ? "large" : "compact"} />
|
|
|
|
|
) : null}
|
2026-04-24 19:17:58 +00:00
|
|
|
{nonImages.map((item, i) => (
|
|
|
|
|
<MediaCell key={`${item.url ?? item.name ?? item.kind}-${i}`} media={item} />
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function MediaCell({ media }: { media: UIMediaAttachment }) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const hasUrl = typeof media.url === "string" && media.url.length > 0;
|
|
|
|
|
|
|
|
|
|
if (media.kind === "video" && hasUrl) {
|
|
|
|
|
return (
|
|
|
|
|
<figure className="max-w-[min(100%,32rem)] overflow-hidden rounded-[14px] border border-border/60 bg-muted/40">
|
|
|
|
|
<video
|
|
|
|
|
src={media.url}
|
|
|
|
|
controls
|
|
|
|
|
preload="metadata"
|
|
|
|
|
className="block max-h-[26rem] w-full bg-black"
|
|
|
|
|
aria-label={media.name ? `${t("message.videoAttachment", { defaultValue: "Video attachment" })}: ${media.name}` : t("message.videoAttachment", { defaultValue: "Video attachment" })}
|
|
|
|
|
/>
|
|
|
|
|
{media.name ? (
|
|
|
|
|
<figcaption className="truncate px-3 py-1.5 text-[11.5px] text-muted-foreground">
|
|
|
|
|
{media.name}
|
|
|
|
|
</figcaption>
|
|
|
|
|
) : null}
|
|
|
|
|
</figure>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const label =
|
|
|
|
|
media.kind === "video"
|
|
|
|
|
? t("message.videoAttachment", { defaultValue: "Video attachment" })
|
|
|
|
|
: t("message.fileAttachment", { defaultValue: "File attachment" });
|
|
|
|
|
const Icon = media.kind === "video" ? PlaySquare : FileIcon;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className="flex max-w-[18rem] items-center gap-2 rounded-[14px] border border-border/60 bg-muted/40 px-3 py-2 text-xs text-muted-foreground"
|
|
|
|
|
title={media.name ?? undefined}
|
|
|
|
|
aria-label={label}
|
|
|
|
|
>
|
|
|
|
|
<Icon className="h-4 w-4 flex-none" aria-hidden />
|
|
|
|
|
<span className="truncate">{media.name ?? label}</span>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 10:29:47 +00:00
|
|
|
/**
|
|
|
|
|
* Right-aligned preview row for images attached to a user turn.
|
|
|
|
|
*
|
|
|
|
|
* Visual follows agent-chat-ui: a single wrapping row of fixed-size square
|
|
|
|
|
* thumbnails that stay modest next to the text pill regardless of how many
|
|
|
|
|
* images are attached.
|
|
|
|
|
*
|
|
|
|
|
* The URL is expected to be a self-contained ``data:`` URL (the Composer
|
|
|
|
|
* hands the normalized base64 payload to the optimistic bubble so that the
|
|
|
|
|
* preview survives React StrictMode double-mount — blob URLs would be
|
|
|
|
|
* revoked by the Composer's cleanup before remount). Historical replays
|
|
|
|
|
* have no URL (the backend strips data URLs before persisting), so we
|
|
|
|
|
* render a labelled placeholder tile instead of a broken ``<img>``.
|
|
|
|
|
*/
|
2026-04-24 19:17:58 +00:00
|
|
|
function UserImages({
|
|
|
|
|
images,
|
|
|
|
|
align = "right",
|
2026-05-08 09:40:15 +00:00
|
|
|
size = "compact",
|
2026-04-24 19:17:58 +00:00
|
|
|
}: {
|
|
|
|
|
images: UIImage[];
|
|
|
|
|
align?: "left" | "right";
|
2026-05-08 09:40:15 +00:00
|
|
|
size?: "compact" | "large";
|
2026-04-24 19:17:58 +00:00
|
|
|
}) {
|
2026-04-21 10:29:47 +00:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
// Only real-URL images can open in the lightbox; historical-replay
|
|
|
|
|
// placeholders (no URL) have nothing to zoom into.
|
|
|
|
|
const viewable = images
|
|
|
|
|
.map((img, i) => ({ img, i }))
|
|
|
|
|
.filter(({ img }) => typeof img.url === "string" && img.url.length > 0);
|
|
|
|
|
const viewableImages = viewable.map(({ img }) => img);
|
|
|
|
|
const originalToViewable = new Map<number, number>(
|
|
|
|
|
viewable.map(({ i }, v) => [i, v]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [lightboxIndex, setLightboxIndex] = useState<number | null>(null);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-04-24 19:17:58 +00:00
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex flex-wrap items-end gap-2",
|
2026-05-08 09:40:15 +00:00
|
|
|
size === "large" && "gap-3",
|
2026-04-24 19:17:58 +00:00
|
|
|
align === "right" ? "ml-auto justify-end" : "mr-auto justify-start",
|
|
|
|
|
)}
|
|
|
|
|
>
|
2026-04-21 10:29:47 +00:00
|
|
|
{images.map((img, i) => (
|
|
|
|
|
<UserImageCell
|
|
|
|
|
key={`${img.url ?? "placeholder"}-${i}`}
|
|
|
|
|
image={img}
|
2026-05-08 09:40:15 +00:00
|
|
|
size={size}
|
2026-04-21 10:29:47 +00:00
|
|
|
placeholderLabel={t("message.imageAttachment")}
|
|
|
|
|
openLabel={t("lightbox.open")}
|
|
|
|
|
onOpen={
|
|
|
|
|
originalToViewable.has(i)
|
|
|
|
|
? () => setLightboxIndex(originalToViewable.get(i)!)
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
<ImageLightbox
|
|
|
|
|
images={viewableImages}
|
|
|
|
|
index={lightboxIndex}
|
|
|
|
|
onIndexChange={setLightboxIndex}
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
if (!open) setLightboxIndex(null);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function UserImageCell({
|
|
|
|
|
image,
|
2026-05-08 09:40:15 +00:00
|
|
|
size,
|
2026-04-21 10:29:47 +00:00
|
|
|
placeholderLabel,
|
|
|
|
|
openLabel,
|
|
|
|
|
onOpen,
|
|
|
|
|
}: {
|
|
|
|
|
image: UIImage;
|
2026-05-08 09:40:15 +00:00
|
|
|
size: "compact" | "large";
|
2026-04-21 10:29:47 +00:00
|
|
|
placeholderLabel: string;
|
|
|
|
|
openLabel: string;
|
|
|
|
|
onOpen?: () => void;
|
|
|
|
|
}) {
|
|
|
|
|
const hasUrl = typeof image.url === "string" && image.url.length > 0;
|
|
|
|
|
const tileClasses = cn(
|
2026-05-08 09:40:15 +00:00
|
|
|
"relative overflow-hidden border border-border/60 bg-muted/40",
|
|
|
|
|
size === "large"
|
2026-05-08 15:39:37 +00:00
|
|
|
? "w-[min(100%,34rem)] rounded-[20px] bg-transparent"
|
2026-05-08 09:40:15 +00:00
|
|
|
: "h-24 w-24 rounded-[14px]",
|
2026-04-21 10:29:47 +00:00
|
|
|
"shadow-[0_6px_18px_-14px_rgba(0,0,0,0.45)]",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (hasUrl && onOpen) {
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onOpen}
|
|
|
|
|
aria-label={image.name ? `${openLabel}: ${image.name}` : openLabel}
|
|
|
|
|
className={cn(
|
|
|
|
|
tileClasses,
|
2026-05-08 15:39:37 +00:00
|
|
|
"block cursor-zoom-in p-0 transition-transform duration-150 motion-reduce:transition-none",
|
|
|
|
|
"hover:scale-[1.01] hover:ring-2 hover:ring-primary/25",
|
2026-04-21 10:29:47 +00:00
|
|
|
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/50",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
src={image.url}
|
|
|
|
|
alt={image.name ?? ""}
|
|
|
|
|
loading="lazy"
|
|
|
|
|
decoding="async"
|
|
|
|
|
draggable={false}
|
2026-05-08 15:39:37 +00:00
|
|
|
className={cn(
|
|
|
|
|
"block",
|
|
|
|
|
size === "large"
|
|
|
|
|
? "h-auto max-h-[36rem] w-full rounded-[inherit] object-contain"
|
|
|
|
|
: "h-full w-full object-cover",
|
|
|
|
|
)}
|
2026-04-21 10:29:47 +00:00
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={tileClasses} title={image.name ?? undefined}>
|
|
|
|
|
<div
|
|
|
|
|
className="flex h-full w-full flex-col items-center justify-center gap-1 px-2 text-[11px] text-muted-foreground"
|
|
|
|
|
aria-label={placeholderLabel}
|
|
|
|
|
>
|
|
|
|
|
<ImageIcon className="h-4 w-4 flex-none" aria-hidden />
|
|
|
|
|
<span className="line-clamp-2 text-center leading-tight">
|
|
|
|
|
{image.name ?? placeholderLabel}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
/** Blinking cursor appended at the end of streaming text. */
|
|
|
|
|
function StreamCursor() {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
|
|
|
|
<span
|
2026-04-19 06:39:06 +00:00
|
|
|
aria-label={t("message.streaming")}
|
2026-04-18 18:51:53 +00:00
|
|
|
className={cn(
|
|
|
|
|
"ml-0.5 inline-block h-[1em] w-[3px] translate-y-[2px] align-middle",
|
|
|
|
|
"rounded-sm bg-foreground/70 animate-pulse",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Pre-token-arrival placeholder: three bouncing dots. */
|
|
|
|
|
function TypingDots() {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
|
|
|
|
<span
|
2026-04-19 06:39:06 +00:00
|
|
|
aria-label={t("message.assistantTyping")}
|
2026-04-18 18:51:53 +00:00
|
|
|
className="inline-flex items-center gap-1 py-1"
|
|
|
|
|
>
|
|
|
|
|
<Dot delay="0ms" />
|
|
|
|
|
<Dot delay="150ms" />
|
|
|
|
|
<Dot delay="300ms" />
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Dot({ delay }: { delay: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<span
|
|
|
|
|
style={{ animationDelay: delay }}
|
|
|
|
|
className={cn(
|
|
|
|
|
"inline-block h-1.5 w-1.5 rounded-full bg-muted-foreground/60",
|
|
|
|
|
"animate-bounce",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface TraceGroupProps {
|
|
|
|
|
message: UIMessage;
|
|
|
|
|
animClass: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Collapsible group of tool-call / progress breadcrumbs. Defaults to
|
|
|
|
|
* expanded for discoverability; a single click on the header folds the
|
|
|
|
|
* group down to a one-line summary so it never dominates the thread.
|
|
|
|
|
*/
|
|
|
|
|
function TraceGroup({ message, animClass }: TraceGroupProps) {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
const lines = message.traces ?? [message.content];
|
|
|
|
|
const count = lines.length;
|
|
|
|
|
const [open, setOpen] = useState(true);
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("w-full", animClass)}>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setOpen((v) => !v)}
|
|
|
|
|
className={cn(
|
|
|
|
|
"group flex w-full items-center gap-2 rounded-md px-2 py-1.5",
|
|
|
|
|
"text-xs text-muted-foreground transition-colors hover:bg-muted/45",
|
|
|
|
|
)}
|
|
|
|
|
aria-expanded={open}
|
|
|
|
|
>
|
|
|
|
|
<Wrench className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
<span className="font-medium">
|
2026-04-19 06:39:06 +00:00
|
|
|
{count === 1
|
|
|
|
|
? t("message.toolSingle")
|
|
|
|
|
: t("message.toolMany", { count })}
|
2026-04-18 18:51:53 +00:00
|
|
|
</span>
|
|
|
|
|
<ChevronRight
|
|
|
|
|
aria-hidden
|
|
|
|
|
className={cn(
|
|
|
|
|
"ml-auto h-3.5 w-3.5 transition-transform duration-200",
|
|
|
|
|
open && "rotate-90",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
{open && (
|
|
|
|
|
<ul
|
|
|
|
|
className={cn(
|
|
|
|
|
"mt-1 space-y-0.5 border-l border-muted-foreground/20 pl-3",
|
|
|
|
|
"animate-in fade-in-0 slide-in-from-top-1 duration-200",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{lines.map((line, i) => (
|
|
|
|
|
<li
|
|
|
|
|
key={i}
|
|
|
|
|
className="whitespace-pre-wrap break-words font-mono text-[11.5px] leading-relaxed text-muted-foreground/90"
|
|
|
|
|
>
|
|
|
|
|
{line}
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-05-13 06:27:53 +00:00
|
|
|
|
|
|
|
|
interface ReasoningBubbleProps {
|
|
|
|
|
lines: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subordinate "thinking" trace shown above an assistant turn. Mirrors the
|
|
|
|
|
* CLI's italic dim ``ChevronRight`` row visually; collapsible because
|
|
|
|
|
* reasoning from models like DeepSeek-R1 / o-series can run long. Defaults
|
|
|
|
|
* to expanded while the answer is still streaming (so the user sees the
|
|
|
|
|
* model "thinking out loud"), but the toggle persists across rerenders.
|
|
|
|
|
*/
|
|
|
|
|
function ReasoningBubble({ lines }: ReasoningBubbleProps) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [open, setOpen] = useState(true);
|
|
|
|
|
const text = useMemo(() => lines.join("\n\n"), [lines]);
|
|
|
|
|
return (
|
|
|
|
|
<div className="mb-2 w-full animate-in fade-in-0 slide-in-from-top-1 duration-200">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setOpen((v) => !v)}
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex w-full items-center gap-2 rounded-md px-2 py-1.5",
|
|
|
|
|
"text-xs text-muted-foreground transition-colors hover:bg-muted/45",
|
|
|
|
|
)}
|
|
|
|
|
aria-expanded={open}
|
|
|
|
|
>
|
|
|
|
|
<Sparkles className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
<span className="font-medium">{t("message.reasoning", { defaultValue: "Thinking" })}</span>
|
|
|
|
|
<ChevronRight
|
|
|
|
|
aria-hidden
|
|
|
|
|
className={cn(
|
|
|
|
|
"ml-auto h-3.5 w-3.5 transition-transform duration-200",
|
|
|
|
|
open && "rotate-90",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
{open && (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"mt-1 whitespace-pre-wrap break-words border-l border-muted-foreground/20 pl-3",
|
|
|
|
|
"text-[12.5px] italic leading-relaxed text-muted-foreground/85",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|