278 lines
10 KiB
TypeScript
278 lines
10 KiB
TypeScript
/**
|
|
* Step 1 / Step 2 消息列表共享的原子件。
|
|
*
|
|
* 两步的消息渲染共用了:头像 + 发送人/徽标/时间的 header 行、流式空内容时的
|
|
* spinner+跳点占位、流式中尾部闪烁竖线、以及 Markdown 紧凑间距渲染配置。
|
|
* 把它们提到这里:
|
|
* - `MessageBubble` 单条消息壳(avatar + header + children slot)
|
|
* - `MessageLoadingPlaceholder` 流式但还没首字节时的 spinner + 跳点 + label
|
|
* - `StreamingCursor` 流式中末尾的小竖线动画
|
|
* - `sharedMarkdownComponents` react-markdown 的紧凑渲染配置
|
|
*
|
|
* 各步特有的内容(Step 1 INTENT_READY 摘要卡 / 澄清卡片;Step 2 ChainOfThought
|
|
* 折叠卡 + 「插话干预」红底容器)仍由对应 Panel 自己组合 —— 它们差异较大,
|
|
* 强行抽象成 props 反而难读。
|
|
*/
|
|
import { useEffect, useRef, useState } from "react";
|
|
import ReactMarkdown from "react-markdown";
|
|
import remarkGfm from "remark-gfm";
|
|
import { BrainIcon, ChevronDownIcon, Loader2 } from "lucide-react";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
export interface MessageBubbleProps {
|
|
/** 左侧头像。caller 决定是 `<Man1Avatar>` / `<RobotAvatar>` / `getAvatar(type)`。 */
|
|
avatar: React.ReactNode;
|
|
/** 发送人显示名。 */
|
|
senderName: string;
|
|
/** 发送人名字额外 className(例如 user 消息用 `text-primary`)。 */
|
|
senderNameClassName?: string;
|
|
/** Header 右上小徽标:Step 1 的 phase 标签、Step 2 的 confidence 标签。 */
|
|
badge?: string;
|
|
/** 时间字符串(`nowTime()` 输出)。 */
|
|
time: string;
|
|
/** 外层容器额外 className —— Step 2「插话干预」需要红底, caller 传入。 */
|
|
containerClassName?: string;
|
|
/** 消息正文区:由 caller 自由组合(MD / 摘要卡 / Steps 卡 / 澄清卡 / cursor 等)。 */
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export function MessageBubble({
|
|
avatar,
|
|
senderName,
|
|
senderNameClassName,
|
|
badge,
|
|
time,
|
|
containerClassName,
|
|
children,
|
|
}: MessageBubbleProps) {
|
|
return (
|
|
<div className={cn("flex gap-4 items-start", containerClassName)}>
|
|
{avatar}
|
|
<div className="flex-1 min-w-0 space-y-1">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<span className={cn("text-xs font-extrabold text-slate-900", senderNameClassName)}>
|
|
{senderName}
|
|
</span>
|
|
{badge && (
|
|
<span className="text-[9px] font-mono font-black uppercase text-[var(--brand-900)] bg-[var(--brand-200)] border border-red-200 px-1.5 py-0.2 rounded">
|
|
{badge}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<span className="text-[10px] text-slate-400 font-mono font-medium shrink-0">{time}</span>
|
|
</div>
|
|
{children}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 流式中、但还没拿到任何 token 时的占位。spinner + label + 三个跳动点,
|
|
* 让用户感受到后端在工作而不是页面卡住。
|
|
*/
|
|
export function MessageLoadingPlaceholder({ label }: { label?: string }) {
|
|
return (
|
|
<div className="flex items-center gap-2 py-1.5 text-xs text-slate-600">
|
|
<Loader2 className="w-4 h-4 animate-spin text-primary" />
|
|
<span className="font-medium">{label ?? "正在连接智能体..."}</span>
|
|
<span className="inline-flex items-center gap-0.5">
|
|
<span className="w-1 h-1 rounded-full bg-primary/70 animate-bounce [animation-delay:-0.3s]" />
|
|
<span className="w-1 h-1 rounded-full bg-primary/70 animate-bounce [animation-delay:-0.15s]" />
|
|
<span className="w-1 h-1 rounded-full bg-primary/70 animate-bounce" />
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** 流式过程中末尾追的小竖线动画。 */
|
|
export function StreamingCursor() {
|
|
return <span className="inline-block w-1 h-3 ml-0.5 bg-primary align-middle animate-pulse" />;
|
|
}
|
|
|
|
/**
|
|
* react-markdown 的紧凑间距配置 —— Step 1 / Step 2 / 主聊天默认气泡都用同一份。
|
|
* 默认 block 边距(`margin: 1em`)在小行高 chat 气泡里太松,这里全压到 my-1。
|
|
* 如果某步需要更多元素(table / blockquote / hr 等),不用单独 override,直接复用本表即可:
|
|
* react-markdown 只会按需启用对应组件,多写无害。
|
|
*/
|
|
export const sharedMarkdownComponents = {
|
|
p: ({ children }: { children?: React.ReactNode }) => (
|
|
<p className="my-1 whitespace-pre-wrap">{children}</p>
|
|
),
|
|
ul: ({ children }: { children?: React.ReactNode }) => (
|
|
<ul className="list-disc pl-5 my-1 space-y-0.5">{children}</ul>
|
|
),
|
|
ol: ({ children }: { children?: React.ReactNode }) => (
|
|
<ol className="list-decimal pl-5 my-1 space-y-0.5">{children}</ol>
|
|
),
|
|
li: ({ children }: { children?: React.ReactNode }) => (
|
|
<li className="leading-snug">{children}</li>
|
|
),
|
|
h1: ({ children }: { children?: React.ReactNode }) => (
|
|
<h1 className="text-sm font-bold mt-2 mb-1">{children}</h1>
|
|
),
|
|
h2: ({ children }: { children?: React.ReactNode }) => (
|
|
<h2 className="text-sm font-bold mt-2 mb-1">{children}</h2>
|
|
),
|
|
h3: ({ children }: { children?: React.ReactNode }) => (
|
|
<h3 className="text-xs font-bold mt-1.5 mb-0.5">{children}</h3>
|
|
),
|
|
h4: ({ children }: { children?: React.ReactNode }) => (
|
|
<h4 className="text-xs font-bold mt-1 mb-0.5">{children}</h4>
|
|
),
|
|
strong: ({ children }: { children?: React.ReactNode }) => (
|
|
<strong className="font-bold text-slate-900">{children}</strong>
|
|
),
|
|
em: ({ children }: { children?: React.ReactNode }) => (
|
|
<em className="italic">{children}</em>
|
|
),
|
|
code: ({
|
|
children,
|
|
className,
|
|
}: {
|
|
children?: React.ReactNode;
|
|
className?: string;
|
|
}) => {
|
|
const isBlock = /language-/.test(className ?? "");
|
|
if (isBlock) {
|
|
return (
|
|
<pre className="bg-slate-100 text-slate-800 rounded-md p-2 my-1 overflow-x-auto text-[11px] font-mono">
|
|
<code>{children}</code>
|
|
</pre>
|
|
);
|
|
}
|
|
return (
|
|
<code className="bg-slate-100 text-slate-800 rounded px-1 py-0.5 text-[11px] font-mono">
|
|
{children}
|
|
</code>
|
|
);
|
|
},
|
|
blockquote: ({ children }: { children?: React.ReactNode }) => (
|
|
<blockquote className="border-l-2 border-slate-300 pl-2 my-1 text-slate-600 italic">
|
|
{children}
|
|
</blockquote>
|
|
),
|
|
a: ({ children, href }: { children?: React.ReactNode; href?: string }) => (
|
|
<a href={href} target="_blank" rel="noopener noreferrer" className="text-primary underline hover:opacity-80">
|
|
{children}
|
|
</a>
|
|
),
|
|
table: ({ children }: { children?: React.ReactNode }) => (
|
|
<table className="my-1 border-collapse text-[11px]">{children}</table>
|
|
),
|
|
th: ({ children }: { children?: React.ReactNode }) => (
|
|
<th className="border border-slate-300 px-1.5 py-0.5 bg-slate-50 font-bold text-left">
|
|
{children}
|
|
</th>
|
|
),
|
|
td: ({ children }: { children?: React.ReactNode }) => (
|
|
<td className="border border-slate-200 px-1.5 py-0.5">{children}</td>
|
|
),
|
|
hr: () => <hr className="my-2 border-slate-200" />,
|
|
};
|
|
|
|
/**
|
|
* 便捷封装:用 sharedMarkdownComponents + remarkGfm 渲染 markdown。
|
|
* 大多数 caller 都不需要再覆盖任何 components,直接用这个就好。
|
|
*/
|
|
export function MarkdownContent({ children }: { children: string }) {
|
|
return (
|
|
<ReactMarkdown remarkPlugins={[remarkGfm]} components={sharedMarkdownComponents}>
|
|
{children}
|
|
</ReactMarkdown>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 「思考过程」折叠卡。展示从 `<think>...</think>` 拆出的 reasoning 文本。
|
|
*
|
|
* 视觉与交互对齐主聊天 `src/components/ai-elements/reasoning.tsx`:
|
|
* - 头部脑图标 + "正在思考..." / "思考完成 (Ns)" 文案
|
|
* - 流式中默认展开,完成后自动延迟收起;用户点击切换后**不再自动收起**(尊重偏好)
|
|
* - 内容区灰底框,用 sharedMarkdownComponents 渲染 markdown
|
|
*/
|
|
export interface ReasoningBlockProps {
|
|
/** Reasoning 文本(已由 `splitInlineReasoning` 提取出来,不含 `<think>` 标签)。 */
|
|
children: string;
|
|
/** 是否仍在流式生成。决定头部 shimmer 与默认展开行为。 */
|
|
streaming?: boolean;
|
|
/** 用于显示「思考用时」的可选时长(秒)。流式期间不显示。 */
|
|
durationSec?: number;
|
|
className?: string;
|
|
/** 展开内容区额外 class,例如 max-h + overflow-y-auto。 */
|
|
contentClassName?: string;
|
|
}
|
|
|
|
const REASONING_AUTO_CLOSE_DELAY_MS = 1000;
|
|
|
|
export function ReasoningBlock({
|
|
children,
|
|
streaming = false,
|
|
durationSec,
|
|
className,
|
|
contentClassName,
|
|
}: ReasoningBlockProps) {
|
|
// open 初始值跟 streaming 同步:流式中默认展开方便观察思考过程,完成后会自动收起一次。
|
|
const [open, setOpen] = useState<boolean>(streaming);
|
|
const userTouchedRef = useRef<boolean>(false);
|
|
const hasAutoClosedRef = useRef<boolean>(false);
|
|
|
|
// 流式开始时若用户没动过开关 → 自动展开;流式结束 → 延迟自动收起(仅一次)。
|
|
useEffect(() => {
|
|
if (userTouchedRef.current) return;
|
|
if (streaming) {
|
|
setOpen(true);
|
|
hasAutoClosedRef.current = false;
|
|
return;
|
|
}
|
|
if (!hasAutoClosedRef.current && open) {
|
|
const timer = setTimeout(() => {
|
|
setOpen(false);
|
|
hasAutoClosedRef.current = true;
|
|
}, REASONING_AUTO_CLOSE_DELAY_MS);
|
|
return () => clearTimeout(timer);
|
|
}
|
|
return undefined;
|
|
}, [streaming, open]);
|
|
|
|
const handleToggle = () => {
|
|
userTouchedRef.current = true;
|
|
setOpen((v) => !v);
|
|
};
|
|
|
|
const headerLabel = streaming
|
|
? "正在思考..."
|
|
: durationSec !== undefined
|
|
? `思考用时 ${durationSec} 秒`
|
|
: "思考过程";
|
|
|
|
return (
|
|
<div className={cn("not-prose space-y-2", className)}>
|
|
<button
|
|
type="button"
|
|
onClick={handleToggle}
|
|
className="inline-flex items-center gap-1.5 text-[11.5px] text-slate-500 hover:text-slate-700 transition-colors cursor-pointer"
|
|
>
|
|
<BrainIcon className={cn("size-3.5", streaming && "text-primary animate-pulse")} />
|
|
<span className={cn(streaming && "animate-pulse")}>{headerLabel}</span>
|
|
<ChevronDownIcon
|
|
className={cn("size-3.5 transition-transform", open ? "rotate-180" : "rotate-0")}
|
|
/>
|
|
</button>
|
|
{open && (
|
|
<div
|
|
className={cn(
|
|
"rounded-md border border-slate-200 bg-slate-50/60 px-3 py-2 text-[11.5px] text-slate-600 leading-relaxed",
|
|
contentClassName,
|
|
)}
|
|
>
|
|
<MarkdownContent>{children}</MarkdownContent>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|