跳至正文
获取 Pro
AI 代理A.09

消息气泡

FreeRegistry
components/linyi/message-bubble.tsx
lucide-react
"use client";
 
import * as React from "react";
import { AlertCircle, Check, Copy, RotateCw, Square } from "lucide-react";
import { cn } from "@/lib/cn";
 
export type MessageBubbleRole = "user" | "assistant" | "system";
export type MessageBubbleStatus = "idle" | "streaming" | "done" | "error";
 
export interface MessageBubbleProps {
role: MessageBubbleRole;
content?: string;
children?: React.ReactNode;
status?: MessageBubbleStatus;
errorMessage?: string;
timestamp?: string;
model?: string;
onCopy?: (content: string) => void;
onRetry?: () => void;
onStop?: () => void;
actionsSlot?: React.ReactNode;
compact?: boolean;
className?: string;
}
 
export function MessageBubble({
role,
content = "",
children,
status = "done",
errorMessage,
timestamp,
model,
onCopy,
onRetry,
onStop,
actionsSlot,
compact = false,
className,
}: MessageBubbleProps) {
const [copied, setCopied] = React.useState(false);
const timerRef = React.useRef<number | null>(null);
const isUser = role === "user";
const isSystem = role === "system";
const isStreaming = status === "streaming";
const isError = status === "error";
 
React.useEffect(() => {
return () => {
if (timerRef.current !== null) window.clearTimeout(timerRef.current);
};
}, []);
 
const handleCopy = async () => {
if (!content) return;
try {
await navigator.clipboard?.writeText(content);
} catch {
// Ignore clipboard permission denial in sandboxed contexts
}
onCopy?.(content);
setCopied(true);
if (timerRef.current !== null) window.clearTimeout(timerRef.current);
timerRef.current = window.setTimeout(() => setCopied(false), 1400);
};
 
return (
<div
data-role={role}
data-status={status}
className={cn(
"flex flex-col gap-1 max-w-[88%]",
isUser
? "ml-auto items-end"
: isSystem
? "mx-auto items-center"
: "mr-auto items-start",
className
)}
>
<div
aria-live={isStreaming ? "polite" : undefined}
className={cn(
"rounded-[14px] leading-relaxed whitespace-pre-wrap break-words",
compact ? "px-3 py-2 text-[11px]" : "px-3.5 py-2.5 text-[13px]",
isError
? "bg-danger-soft text-danger border border-danger-border rounded-bl-[4px]"
: isUser
? "bg-inverse text-fg-inverse rounded-br-[4px]"
: isSystem
? "bg-well text-fg-secondary border border-line-well text-xs"
: "bg-surface text-fg border border-line shadow-panel rounded-bl-[4px]"
)}
>
{isError ? (
<div className="flex items-start gap-2">
<AlertCircle className="h-3.5 w-3.5 mt-0.5 flex-shrink-0" aria-hidden="true" />
<span>{errorMessage ?? (content || "生成过程中出现异常")}</span>
</div>
) : (
<>
{children ?? content}
{isStreaming && (
<span
aria-hidden="true"
className="inline-block w-[6px] h-[14px] ml-0.5 align-middle rounded-[1px] bg-accent animate-pulse"
/>
)}
</>
)}
</div>
 
{(timestamp || model || actionsSlot || isStreaming || isError || !compact) && (
<div
className={cn(
"flex items-center flex-wrap gap-2 px-1 font-mono-num text-[10px] text-fg-muted",
isUser && "flex-row-reverse"
)}
>
{model && !isUser && <span>{model}</span>}
{timestamp && <span>{timestamp}</span>}
{isStreaming && onStop && (
<button
type="button"
onClick={onStop}
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded-pill text-danger hover:bg-danger-soft cursor-pointer transition-colors"
>
<Square className="w-2.5 h-2.5 fill-current" aria-hidden="true" />
<span>停止</span>
</button>
)}
{onRetry && !isStreaming && (isError || !isUser) && (
<button
type="button"
onClick={onRetry}
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded-pill hover:bg-well hover:text-fg cursor-pointer transition-colors"
>
<RotateCw className="w-2.5 h-2.5" aria-hidden="true" />
<span>重试</span>
</button>
)}
{content && !isError && (
<button
type="button"
onClick={handleCopy}
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded-pill hover:bg-well hover:text-fg cursor-pointer transition-colors"
aria-label={copied ? "已复制消息内容" : "复制消息内容"}
>
{copied ? (
<Check className="w-2.5 h-2.5 text-success" aria-hidden="true" />
) : (
<Copy className="w-2.5 h-2.5" aria-hidden="true" />
)}
<span>{copied ? "已复制" : "复制"}</span>
</button>
)}
{actionsSlot}
</div>
)}
</div>
);
}