import { useEffect, useRef, useState } from 'react'; import type { ReactNode } from 'react'; import { ArrowRight, BrainCircuit, ChevronRight, CircleAlert, Database, Layers, Lightbulb, ListChecks, Sparkles, Table2 } from 'lucide-react'; import type { ThemeTokens } from '../../context/ThemeContext'; import type { I18n } from '../../i18n/locales'; import type { AskAnswer, AskStep, AskTone } from '../../lib/ibpAsk'; /** * 履约风险回答卡:思考过程(逐条揭示,揭示完折叠成一行)+ 规范六段式正文 * 结论 / 关键数字 / 风险明细 / 依据与口径 / 建议动作 / 数据来源。 * 只负责渲染,所有数字与文案由 lib/ibpAsk.ts 从业务本体层现算。 */ const toneColor = (tone: AskTone | undefined, tk: ThemeTokens) => tone === 'danger' ? tk.danger : tone === 'warning' ? tk.warning : tone === 'success' ? tk.success : tone === 'info' ? tk.info : tk.accent; /** 小节标题:细线 + 图标 + 文案,让六段结构一眼可辨 */ function SecHead({ icon, text, tk, hint }: { icon: ReactNode; text: string; tk: ThemeTokens; hint?: string }) { return (
{icon} {text} {hint && · {hint}}
); } /** 打字点:思考进行中用,揭示一条停一条 */ function Dots({ tk }: { tk: ThemeTokens }) { return ( {[0, 1, 2].map(i => ( ))} ); } /** 小节思考提示:斜体 + info 色,让各区块的生成有呼吸感 */ function BlockThink({ text, tk }: { text: string; tk: ThemeTokens }) { return (
{text}
); } type BlockKey = 'metrics' | 'table' | 'points' | 'actions' | 'source'; const BLOCKS: BlockKey[] = ['metrics', 'table', 'points', 'actions', 'source']; type Phase = 'hidden' | 'thinking' | 'ready'; const THINK_MS = 1600; const GAP_MS = 600; const PRE_CONCLUSION_MS = 900; const CHAR_MS = 65; const TICK_MS = 80; function blockHasContent(answer: AskAnswer, key: BlockKey) { switch (key) { case 'metrics': return answer.metrics.length > 0; case 'table': return !!answer.table; case 'points': return answer.points.length > 0; case 'actions': return answer.actions.length > 0; case 'source': return true; } } /** 每个正文块的揭示时间点:用于按 elapsed-time 推进,避免后台 tab 的 setTimeout 被节流后卡住 */ interface BlockSchedule { key: BlockKey; thinkAt: number; readyAt: number } /** * 思考过程轨:running 时逐条出现(由父级推进 revealed),出答案后折叠成 * 「思考过程 · 6 步 · 耗时 2.8s」一行,点开可复查每一步做了什么、依据哪张表。 */ export function StepTrace({ steps, revealed, running, elapsed, tk, t, }: { steps: AskStep[]; revealed: number; running: boolean; elapsed?: number; tk: ThemeTokens; t: I18n; }) { const [open, setOpen] = useState(running); const shown = steps.slice(0, Math.max(0, revealed)); const label = t.agent.thinkSteps.replace('{n}', String(steps.length)); const time = elapsed !== undefined ? t.agent.thinkElapsed.replace('{s}', elapsed.toFixed(1)) : t.agent.thinkHint; return (
{open && (
{shown.map((s, i) => (
{i + 1} {s.act} · {s.detail}
))} {running && revealed < steps.length && (
{steps[revealed]?.act ?? t.agent.thinkHint}
)}
)}
); } interface CardProps { answer: AskAnswer; elapsed: number; tk: ThemeTokens; t: I18n; time: string; /** 派发给哪位数字员工(回答卡头部标注,保留数字员工协同设定) */ staff?: string; onLocate: (so: string) => void; onAsk: (q: string) => void; onOntology: () => void; } export default function AskAnswerCard({ answer, elapsed, tk, t, time, staff, onLocate, onAsk, onOntology }: CardProps) { const sev = toneColor(answer.severity, tk); const tone = (x?: AskTone) => toneColor(x, tk); const iconOf = (a: AskAnswer['actions'][number]) => a.locateSO ? : a.ontology ? : ; const run = (a: AskAnswer['actions'][number]) => { if (a.locateSO) onLocate(a.locateSO); else if (a.ontology) onOntology(); else if (a.ask) onAsk(a.ask); }; /* 结论生成节奏:先停顿「整理结论中」→ 逐字流式输出 → 每个正文区块先显示 「思考中」提示再揭示内容,整体明显慢于「瞬间甩结果」,让推理过程可感知。 用 elapsed-time 调度:即使页面在后台 tab 被节流,回到前台后也会按真实 耗时一次性追赶到对应进度,不会无限卡住。 */ const [streamed, setStreamed] = useState(''); const [preparing, setPreparing] = useState(true); const [phases, setPhases] = useState>(() => Object.fromEntries(BLOCKS.map(k => [k, 'hidden'])) as Record ); const scheduleRef = useRef([]); const startRef = useRef(0); const streamStartRef = useRef(0); const preparingRef = useRef(true); const conclusionDone = answer.conclusion.length === 0 || streamed.length === answer.conclusion.length; useEffect(() => { setStreamed(''); setPreparing(true); preparingRef.current = true; streamStartRef.current = 0; setPhases(Object.fromEntries(BLOCKS.map(k => [k, 'hidden'])) as Record); startRef.current = Date.now(); // 预排各正文块的 thinking / ready 时间点,按相对结论输出完成后的绝对时间推进 let offset = 80; scheduleRef.current = BLOCKS.reduce((acc, key, idx) => { if (!blockHasContent(answer, key)) return acc; const thinkAt = offset; const readyAt = thinkAt + THINK_MS + idx * 80; acc.push({ key, thinkAt, readyAt }); offset = readyAt + GAP_MS; return acc; }, []); const chars = answer.conclusion.split(''); const totalChars = chars.length; const updateBlocks = (blockElapsed: number) => { scheduleRef.current.forEach(({ key, thinkAt, readyAt }) => { if (blockElapsed >= readyAt) { setPhases(p => (p[key] === 'ready' ? p : { ...p, [key]: 'ready' })); } else if (blockElapsed >= thinkAt) { setPhases(p => (p[key] === 'thinking' || p[key] === 'ready' ? p : { ...p, [key]: 'thinking' })); } }); }; const tick = () => { const now = Date.now(); const elapsed = now - startRef.current; if (preparingRef.current) { if (elapsed >= PRE_CONCLUSION_MS) { preparingRef.current = false; streamStartRef.current = now; setPreparing(false); } return; } if (totalChars === 0) { // 无结论时直接揭示区块 updateBlocks(elapsed - PRE_CONCLUSION_MS); return; } const streamElapsed = now - streamStartRef.current; const idx = Math.min(totalChars, Math.floor(streamElapsed / CHAR_MS)); setStreamed(chars.slice(0, idx).join('')); if (idx >= totalChars) { // 结论已流完,按结论完成后时间点推进区块 updateBlocks(streamElapsed - totalChars * CHAR_MS); } }; const id = window.setInterval(tick, TICK_MS); return () => window.clearInterval(id); // eslint-disable-next-line react-hooks/exhaustive-deps }, [answer.conclusion]); return (
{/* 卡头:智能体名 + 识别到的意图 + 派发员工 + 时间 */}
{t.agent.aiName} {t.agent.secIntent}:{answer.intent} {staff && ( {staff} )} {time}
{/* 正文卡 */}
{/* ① 结论:先「整理结论中」停顿,再逐字流出 */}
{preparing ? ( 正在整合关键数字与规则口径,生成结论… ) : ( <> {streamed} {!conclusionDone && } )}
{/* ② 关键数字 */} {phases.metrics === 'thinking' && } {phases.metrics === 'ready' && !!answer.metrics.length && (
} text={t.agent.secNumbers} tk={tk} />
{answer.metrics.map((m, i) => { const c = tone(m.tone); return ( {m.label} {m.value} ); })}
)} {/* ③ 风险明细表 */} {phases.table === 'thinking' && } {phases.table === 'ready' && answer.table && (
} text={t.agent.secTable} tk={tk} hint={`${answer.table.rows.length} 行`} />
{answer.table.cols.map((c, i) => ( ))} {answer.table.rows.map((r, ri) => { const c = tone(r.tone); return ( {r.cells.map((cell, ci) => ( ))} ); })}
{c}
{cell}
)} {/* ④ 依据与口径 */} {phases.points === 'thinking' && } {phases.points === 'ready' && !!answer.points.length && (
} text={t.agent.secBasis} tk={tk} />
{answer.points.map((p, i) => { const c = tone(p.tone); return (
{p.label} {p.text}
); })}
)} {/* ⑤ 建议动作 */} {phases.actions === 'thinking' && } {phases.actions === 'ready' && !!answer.actions.length && (
} text={t.agent.secActions} tk={tk} />
{answer.actions.map((a, i) => ( ))}
)} {/* ⑥ 数据来源 */} {phases.source === 'thinking' && } {phases.source === 'ready' && (
{t.agent.secSource}:{answer.source}
)}
); }