/** * 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 { desensitize } from "@/lib/desensitize"; import { cn } from "@/lib/utils"; export interface MessageBubbleProps { /** 左侧头像。caller 决定是 `` / `` / `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 (
{avatar}
{/* 智能体名称需脱敏(消息列表里唯一要脱敏的就是名称)。 */} {desensitize(senderName)} {badge && ( {badge} )}
{time}
{children}
); } /** * 流式中、但还没拿到任何 token 时的占位。spinner + label + 三个跳动点, * 让用户感受到后端在工作而不是页面卡住。 */ export function MessageLoadingPlaceholder({ label }: { label?: string }) { return (
{label ?? "正在连接智能体..."}
); } /** 流式过程中末尾追的小竖线动画。 */ export function StreamingCursor() { return ; } /** * 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 }) => (

{children}

), ul: ({ children }: { children?: React.ReactNode }) => (
    {children}
), ol: ({ children }: { children?: React.ReactNode }) => (
    {children}
), li: ({ children }: { children?: React.ReactNode }) => (
  • {children}
  • ), h1: ({ children }: { children?: React.ReactNode }) => (

    {children}

    ), h2: ({ children }: { children?: React.ReactNode }) => (

    {children}

    ), h3: ({ children }: { children?: React.ReactNode }) => (

    {children}

    ), h4: ({ children }: { children?: React.ReactNode }) => (

    {children}

    ), strong: ({ children }: { children?: React.ReactNode }) => ( {children} ), em: ({ children }: { children?: React.ReactNode }) => ( {children} ), code: ({ children, className, }: { children?: React.ReactNode; className?: string; }) => { const isBlock = /language-/.test(className ?? ""); if (isBlock) { return (
              {children}
            
    ); } return ( {children} ); }, blockquote: ({ children }: { children?: React.ReactNode }) => (
    {children}
    ), a: ({ children, href }: { children?: React.ReactNode; href?: string }) => ( {children} ), // 宽表在并行两列 / 窄气泡里会超出容器(模型常输出多列表格,如「法律免责条款」表)。 // 套一层 max-w-full + overflow-x-auto 的块级容器:表格保持自然宽度,超出时容器内**横向 // 滚动**而不是撑破列宽 / 盖到相邻列。table 不设 max-w,否则单元格会被挤到错乱换行。 table: ({ children }: { children?: React.ReactNode }) => (
    {children}
    ), th: ({ children }: { children?: React.ReactNode }) => ( {children} ), td: ({ children }: { children?: React.ReactNode }) => ( {children} ), hr: () =>
    , }; /** * 便捷封装:用 sharedMarkdownComponents + remarkGfm 渲染 markdown。 * 大多数 caller 都不需要再覆盖任何 components,直接用这个就好。 */ // 消息列表正文**不脱敏**(仅名称脱敏);MarkdownContent 是会商对话气泡正文渲染器,故原文渲染。 export function MarkdownContent({ children }: { children: string }) { return ( {children} ); } /** * 「思考过程」折叠卡。展示从 `...` 拆出的 reasoning 文本。 * * 视觉与交互对齐主聊天 `src/components/ai-elements/reasoning.tsx`: * - 头部脑图标 + "正在思考..." / "思考完成 (Ns)" 文案 * - 流式中默认展开,完成后自动延迟收起;用户点击切换后**不再自动收起**(尊重偏好) * - 内容区灰底框,用 sharedMarkdownComponents 渲染 markdown */ export interface ReasoningBlockProps { /** Reasoning 文本(已由 `splitInlineReasoning` 提取出来,不含 `` 标签)。 */ 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(streaming); const userTouchedRef = useRef(false); const hasAutoClosedRef = useRef(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 (
    {open && (
    {children}
    )}
    ); }