import { useMemo, useState } from 'react';
import type { ReactNode } from 'react';
import {
BookOpen, Boxes, BrainCircuit, ChevronsRight, ChevronRight, CircleAlert, Database,
FlaskConical, Gauge, LayoutGrid, Lightbulb, ListChecks, MessageCircle, Sparkles, Table2,
} from 'lucide-react';
import type { ThemeTokens } from '../../context/ThemeContext';
import type { I18n } from '../../i18n/locales';
import type { AskAnswer, AskJump, AskStep, AskTone } from '../../lib/qualityAsk';
/**
* 质量问答回答卡:思考过程(逐条揭示,揭示完折叠成一行复查)+ 六段式正文
* 结论 / 关键数字 / 质量明细 / 依据与口径 / 建议动作(可跳转)/ 数据来源。
* 只负责渲染,所有数字与文案由 lib/qualityAsk.ts 从 17 张源表与本体规则现算。
*
* 流式:pending 时父级给 `reveal={ sections, typed }`,卡片按段亮出正文、
* 结论按打字机逐字出现;落卡后不传 reveal,整张卡全量渲染。
*/
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 => (
))}
);
}
/**
* 思考过程轨: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}
)}
)}
);
}
/** 跳转目的地的图标与 data-role(供核验脚本点击) */
const JUMP_META: Record = {
metric: { icon: , role: 'jump-metric' },
tab: { icon: , role: 'jump-tab' },
ontology: { icon: , role: 'jump-ontology' },
dataset: { icon: , role: 'jump-dataset' },
knowledge: { icon: , role: 'jump-knowledge' },
feature: { icon: , role: 'jump-feature' },
ask: { icon: , role: 'jump-ask' },
};
export function JumpButton({ jump, label, tk, onJump, block }: {
jump: AskJump; label: string; tk: ThemeTokens; onJump: (j: AskJump) => void; block?: boolean;
}) {
const meta = JUMP_META[jump.kind];
return (
);
}
/** title 提示:告诉用户点下去会走到哪一眼数据 */
function t_jumpTitle(j: AskJump): string {
switch (j.kind) {
case 'metric': return `在「指标明细」打开口径 ${j.code}`;
case 'tab': return '切换中央工作区页签';
case 'ontology': return '打开业务本体';
case 'dataset': return `回数据集总览对账 ${j.id}`;
case 'knowledge': return `打开知识库 ${j.role} · ${j.chunkId}`;
case 'feature': return `打开工艺建模 ${j.objKey}${j.instanceId ? ` · ${j.instanceId}` : ''}`;
case 'ask': return '把这个问题再抛回对话区';
}
}
interface CardProps {
answer: AskAnswer;
elapsed: number;
tk: ThemeTokens;
t: I18n;
time: string;
/** 应答归属(卡头小字:由哪位质量专家作答) */
staff?: string;
onJump: (j: AskJump) => void;
/**
* 流式推演中:思考轨按 revealedSteps 揭示;phase 转 body 后正文按 sections 分段亮出、
* 结论按 typed 逐字打出。不传就是已落卡的完整回答。
*/
streaming?: { revealedSteps: number; phase: 'think' | 'body'; sections: number; typed: number; items: number };
/** 「跳过推演」:直接落成完整卡 */
onSkip?: () => void;
}
export default function AskAnswerCard({ answer, elapsed, tk, t, time, staff, onJump, streaming, onSkip }: CardProps) {
const sev = toneColor(answer.severity, tk);
const tone = (x?: AskTone) => toneColor(x, tk);
const inBody = !!streaming && streaming.phase === 'body';
/* 正文按段构建:与 hooks/useAgentChat 的 sectionPlan 同一套"有没有内容"判断,
顺序也必须一致(结论 → 关键数字 → 明细 → 依据 → 动作 → 来源),否则流式会跳段。
每段是一个 render(limit):流式时只铺到 limit 条,数字/表格行/依据/动作逐条出现。 */
const parts = useMemo(() => {
const out: { key: string; count: number; node: (limit: number) => ReactNode }[] = [];
out.push({
key: 'conclusion', count: 1,
node: () => (
{answer.conclusion}
),
});
if (answer.metrics.length) {
out.push({
key: 'numbers', count: answer.metrics.length,
node: (limit) => (
} text={t.agent.secNumbers} tk={tk} />
{answer.metrics.slice(0, limit).map((m, i) => {
const c = tone(m.tone);
return (
{m.label}
{m.value}
);
})}
),
});
}
if (answer.table) {
const tbl = answer.table;
out.push({
key: 'table', count: tbl.rows.length,
node: (limit) => (
} text={t.agent.secTable} tk={tk} hint={`${Math.min(limit, tbl.rows.length)} 行`} />
{tbl.cols.map((c, i) => (
| {c} |
))}
{tbl.rows.slice(0, limit).map((r, ri) => {
const c = tone(r.tone);
return (
{r.cells.map((cell, ci) => (
| {cell} |
))}
);
})}
),
});
}
if (answer.points.length) {
out.push({
key: 'basis', count: answer.points.length,
node: (limit) => (
} text={t.agent.secBasis} tk={tk} />
{answer.points.slice(0, limit).map((p, i) => {
const c = tone(p.tone);
return (
{p.label}
{p.text}
);
})}
),
});
}
if (answer.actions.length) {
out.push({
key: 'actions', count: answer.actions.length,
node: (limit) => (
} text={t.agent.secActions} tk={tk}
hint={t.agent.jumpHint}
/>
{answer.actions.slice(0, limit).map((a, i) => (
))}
),
});
}
out.push({
key: 'source', count: 1,
node: () => (
{t.agent.secSource}:{answer.source}
),
});
return out;
}, [answer, onJump, sev, t, tk]);
const shownParts = inBody ? parts.slice(0, Math.max(1, streaming!.sections)) : parts;
const typedText = inBody ? answer.conclusion.slice(0, streaming!.typed) : answer.conclusion;
const typing = inBody && streaming!.typed < answer.conclusion.length;
return (
{/* 卡头:智能体名 + 识别到的意图 + 应答归属 + 时间 */}
{t.agent.aiName}
{t.agent.secIntent}:{answer.intent}
{staff && (
{staff}
)}
{time}
{/* 思考轨:推演中逐条展开,落卡后折叠成一行供复查 */}
{/* 思考阶段:正文还没开始写,只给一行"正在取数"的占位,不摆假数字 */}
{streaming && !inBody && (
{answer.steps[Math.min(streaming.revealedSteps, answer.steps.length - 1)]?.act ?? t.agent.thinkHint}
· {t.agent.thinkHint}
)}
{inBody && (
<>
{typedText}
{typing && ▍}
{shownParts.slice(1).map((p, idx) => {
/* 只有最后一段还在「写」:按 items 逐条铺;之前的段已经铺满 */
const top = idx + 1 === shownParts.length - 1;
const limit = top ? Math.max(0, Math.min(p.count, streaming!.items)) : p.count;
return
{p.node(limit)}
;
})}
>
)}
{!streaming && parts.map(p =>
{p.node(p.count)}
)}
{/* 推演进行中:给一个「跳过推演」,不想等的用户一键落到完整卡 */}
{streaming && onSkip && (
)}
);
}