This commit is contained in:
2026-11-19 10:09:33 +08:00
parent d31f5a701c
commit d2013c2a07
181 changed files with 47968 additions and 4 deletions
@@ -0,0 +1,412 @@
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 (
<div style={{ display: 'flex', alignItems: 'center', gap: 5, marginBottom: 1 }}>
{icon}
<span style={{ fontSize: 10.5, fontWeight: 700, letterSpacing: '0.06em', color: tk.textMuted }}>{text}</span>
{hint && <span style={{ fontSize: 10.5, color: tk.textMuted, opacity: 0.8 }}>· {hint}</span>}
<span style={{ flex: 1, height: 1, background: tk.borderSub }} />
</div>
);
}
/** 打字点:思考进行中用,揭示一条停一条 */
function Dots({ tk }: { tk: ThemeTokens }) {
return (
<span style={{ display: 'flex', gap: 3, alignItems: 'center' }}>
{[0, 1, 2].map(i => (
<i key={i} className="typing-dot" style={{ animationDelay: `${i * 0.16}s`, background: tk.accent }} />
))}
</span>
);
}
/**
* 思考过程轨: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 (
<div style={{
border: `1px ${running ? 'solid' : 'dashed'} ${tk.borderAccent}`, borderRadius: 10,
background: running ? tk.accentBg : tk.cardBg, overflow: 'hidden',
}}>
<button
onClick={() => setOpen(v => !v)}
aria-expanded={open}
data-role="think-toggle"
style={{
display: 'flex', alignItems: 'center', gap: 6, width: '100%', cursor: 'pointer',
padding: '5px 9px', border: 'none', background: 'transparent', textAlign: 'left',
}}
>
<BrainCircuit size={12} color={tk.accent} style={{ flexShrink: 0 }} />
<span style={{ fontSize: 11.5, fontWeight: 700, color: tk.accent, flexShrink: 0 }}>{t.agent.thinkTitle}</span>
<span style={{ fontSize: 11, color: tk.textMuted, fontVariantNumeric: 'tabular-nums' }}>{label} · {time}</span>
<span style={{ flex: 1 }} />
{running && <Dots tk={tk} />}
<ChevronRight size={12} color={tk.textMuted} style={{ flexShrink: 0, transform: open ? 'rotate(90deg)' : undefined, transition: 'transform 0.18s ease' }} />
</button>
{open && (
<div style={{ padding: '2px 9px 8px', display: 'flex', flexDirection: 'column', gap: 5 }}>
{shown.map((s, i) => (
<div key={i} data-role="think-step" className="content-reveal" style={{ display: 'flex', gap: 7, alignItems: 'flex-start' }}>
<span style={{
flex: '0 0 15px', width: 15, height: 15, borderRadius: 5, display: 'grid', placeItems: 'center', marginTop: 1,
fontSize: 10, fontWeight: 700, fontVariantNumeric: 'tabular-nums',
color: tk.onAccent, background: tk.accent,
}}>{i + 1}</span>
<span style={{ minWidth: 0, fontSize: 11.5, lineHeight: 1.55 }}>
<span style={{ color: tk.text, fontWeight: 700 }}>{s.act}</span>
<span style={{ color: tk.textMuted }}> · {s.detail}</span>
</span>
</div>
))}
{running && revealed < steps.length && (
<div style={{ display: 'flex', alignItems: 'center', gap: 7, fontSize: 11.5, color: tk.textMuted }}>
<Dots tk={tk} />
{steps[revealed]?.act ?? t.agent.thinkHint}
</div>
)}
</div>
)}
</div>
);
}
/** 跳转目的地的图标与 data-role(供核验脚本点击) */
const JUMP_META: Record<AskJump['kind'], { icon: ReactNode; role: string }> = {
metric: { icon: <Gauge size={11} />, role: 'jump-metric' },
tab: { icon: <LayoutGrid size={11} />, role: 'jump-tab' },
ontology: { icon: <Boxes size={11} />, role: 'jump-ontology' },
dataset: { icon: <Database size={11} />, role: 'jump-dataset' },
knowledge: { icon: <BookOpen size={11} />, role: 'jump-knowledge' },
feature: { icon: <FlaskConical size={11} />, role: 'jump-feature' },
ask: { icon: <MessageCircle size={11} />, 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 (
<button
onClick={() => onJump(jump)}
data-role={meta.role}
data-jump={block ? JSON.stringify(jump) : undefined}
className="ghost-btn"
title={t_jumpTitle(jump)}
style={{
display: 'flex', alignItems: 'center', gap: 4, fontSize: 11.5, fontWeight: 600, cursor: 'pointer',
color: tk.accent, background: tk.accentBg, border: `1px solid ${tk.borderAccent}`,
borderRadius: 8, padding: '3px 10px', whiteSpace: 'nowrap',
}}
>
{meta.icon}{label}
</button>
);
}
/** 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: () => (
<div style={{ display: 'flex', gap: 7, alignItems: 'flex-start' }}>
<CircleAlert size={13} color={sev} style={{ flexShrink: 0, marginTop: 2 }} />
<span style={{ fontSize: 13, fontWeight: 700, lineHeight: 1.65, color: tk.text, minWidth: 0 }}>
{answer.conclusion}
</span>
</div>
),
});
if (answer.metrics.length) {
out.push({
key: 'numbers', count: answer.metrics.length,
node: (limit) => (
<div>
<SecHead icon={<ListChecks size={11} color={tk.textMuted} />} text={t.agent.secNumbers} tk={tk} />
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', marginTop: 4 }}>
{answer.metrics.slice(0, limit).map((m, i) => {
const c = tone(m.tone);
return (
<span key={i} className="content-reveal" style={{
display: 'flex', alignItems: 'baseline', gap: 5, padding: '3px 9px', borderRadius: 8,
background: tk.panelBgAlt, border: `1px solid ${tk.borderSub}`,
}}>
<span style={{ fontSize: 11, color: tk.textMuted, whiteSpace: 'nowrap' }}>{m.label}</span>
<span style={{ fontSize: 13, fontWeight: 800, color: c, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace' }}>{m.value}</span>
</span>
);
})}
</div>
</div>
),
});
}
if (answer.table) {
const tbl = answer.table;
out.push({
key: 'table', count: tbl.rows.length,
node: (limit) => (
<div>
<SecHead icon={<Table2 size={11} color={tk.textMuted} />} text={t.agent.secTable} tk={tk} hint={`${Math.min(limit, tbl.rows.length)} 行`} />
<div style={{ marginTop: 4, overflowX: 'auto', border: `1px solid ${tk.borderSub}`, borderRadius: 9 }}>
<table style={{ borderCollapse: 'collapse', width: '100%', fontSize: 11.5, whiteSpace: 'nowrap' }}>
<thead>
<tr style={{ background: tk.panelBgAlt }}>
{tbl.cols.map((c, i) => (
<th key={i} style={{
textAlign: 'left', fontWeight: 700, color: tk.textMuted, padding: '5px 8px',
borderBottom: `1px solid ${tk.border}`, fontSize: 10.5, letterSpacing: '0.04em',
}}>{c}</th>
))}
</tr>
</thead>
<tbody>
{tbl.rows.slice(0, limit).map((r, ri) => {
const c = tone(r.tone);
return (
<tr key={ri} className="content-reveal" style={{ background: r.tone ? `${c}0d` : undefined, borderBottom: ri < tbl.rows.length - 1 ? `1px solid ${tk.borderSub}` : undefined }}>
{r.cells.map((cell, ci) => (
<td key={ci} style={{
padding: '4px 8px',
color: ci === 0 ? (r.tone ? c : tk.text) : tk.textSub,
fontWeight: ci === 0 ? 700 : 400,
fontVariantNumeric: 'tabular-nums',
fontFamily: ci === 0 ? undefined : 'ui-monospace, monospace',
}}>{cell}</td>
))}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
),
});
}
if (answer.points.length) {
out.push({
key: 'basis', count: answer.points.length,
node: (limit) => (
<div>
<SecHead icon={<BrainCircuit size={11} color={tk.textMuted} />} text={t.agent.secBasis} tk={tk} />
<div style={{ display: 'flex', flexDirection: 'column', gap: 4, marginTop: 4 }}>
{answer.points.slice(0, limit).map((p, i) => {
const c = tone(p.tone);
return (
<div key={i} className="content-reveal" style={{ display: 'flex', gap: 7, alignItems: 'flex-start' }}>
<span style={{
flex: '0 0 auto', fontSize: 10.5, fontWeight: 700, marginTop: 1, padding: '1px 6px', borderRadius: 6,
color: p.tone ? c : tk.textMuted,
background: p.tone ? `${c}14` : tk.panelBgAlt,
border: `1px solid ${p.tone ? `${c}33` : tk.border}`,
whiteSpace: 'nowrap', maxWidth: 130, overflow: 'hidden', textOverflow: 'ellipsis',
}}>{p.label}</span>
<span style={{ fontSize: 12, lineHeight: 1.6, color: tk.textSub, minWidth: 0 }}>{p.text}</span>
</div>
);
})}
</div>
</div>
),
});
}
if (answer.actions.length) {
out.push({
key: 'actions', count: answer.actions.length,
node: (limit) => (
<div>
<SecHead
icon={<Lightbulb size={11} color={tk.textMuted} />} text={t.agent.secActions} tk={tk}
hint={t.agent.jumpHint}
/>
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', marginTop: 4 }}>
{answer.actions.slice(0, limit).map((a, i) => (
<span key={i} className="content-reveal" style={{ display: 'inline-flex' }}>
<JumpButton jump={a.jump} label={a.label} tk={tk} onJump={onJump} block />
</span>
))}
</div>
</div>
),
});
}
out.push({
key: 'source', count: 1,
node: () => (
<div style={{ display: 'flex', alignItems: 'flex-start', gap: 5, paddingTop: 3, borderTop: `1px dashed ${tk.borderSub}` }}>
<Database size={10} color={tk.textMuted} style={{ flexShrink: 0, marginTop: 2 }} />
<span style={{ fontSize: 10.5, color: tk.textMuted, minWidth: 0, lineHeight: 1.55 }}>
<span style={{ fontWeight: 700 }}>{t.agent.secSource}:</span>{answer.source}
</span>
</div>
),
});
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 (
<div data-role="answer-card" className="content-reveal" style={{ display: 'flex', justifyContent: 'flex-start', marginBottom: 8 }}>
<div style={{ maxWidth: '96%', minWidth: 0, display: 'flex', flexDirection: 'column', gap: 6 }}>
{/* 卡头:智能体名 + 识别到的意图 + 应答归属 + 时间 */}
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<span style={{
width: 18, height: 18, borderRadius: 6, display: 'grid', placeItems: 'center', flexShrink: 0,
background: `linear-gradient(135deg, ${tk.accent}, ${tk.info})`,
}}>
<Sparkles size={11} color={tk.onAccent} />
</span>
<span style={{ fontSize: 12.5, fontWeight: 700, color: tk.accent, flexShrink: 0 }}>{t.agent.aiName}</span>
<span style={{
fontSize: 10.5, color: tk.textMuted, padding: '0 6px', borderRadius: 7,
border: `1px solid ${tk.border}`, background: tk.cardBg, whiteSpace: 'nowrap',
}}>{t.agent.secIntent}:{answer.intent}</span>
{staff && (
<span style={{ fontSize: 10.5, color: tk.textMuted, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', minWidth: 0 }}>{staff}</span>
)}
<span style={{ marginLeft: 'auto', color: tk.textMuted, fontSize: 10.5, fontVariantNumeric: 'tabular-nums', flexShrink: 0 }}>{time}</span>
</div>
{/* 思考轨:推演中逐条展开,落卡后折叠成一行供复查 */}
<StepTrace
steps={answer.steps}
revealed={streaming ? streaming.revealedSteps : answer.steps.length}
running={!!streaming}
elapsed={streaming ? undefined : elapsed}
tk={tk} t={t}
/>
<div style={{
background: tk.cardBg, border: `1px solid ${tk.border}`, borderLeft: `3px solid ${sev}`,
borderRadius: '4px 12px 12px 12px', padding: '9px 12px',
display: 'flex', flexDirection: 'column', gap: 9, minWidth: 0,
}}>
{/* 思考阶段:正文还没开始写,只给一行"正在取数"的占位,不摆假数字 */}
{streaming && !inBody && (
<div style={{ display: 'flex', alignItems: 'center', gap: 7, fontSize: 12, color: tk.textMuted }}>
<Dots tk={tk} />
{answer.steps[Math.min(streaming.revealedSteps, answer.steps.length - 1)]?.act ?? t.agent.thinkHint}
· {t.agent.thinkHint}
</div>
)}
{inBody && (
<>
<div style={{ display: 'flex', gap: 7, alignItems: 'flex-start' }}>
<CircleAlert size={13} color={sev} style={{ flexShrink: 0, marginTop: 2 }} />
<span style={{ fontSize: 13, fontWeight: 700, lineHeight: 1.65, color: tk.text, minWidth: 0 }}>
{typedText}
{typing && <span style={{ color: tk.accent }}>▍</span>}
</span>
</div>
{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 <div key={p.key} className="content-reveal">{p.node(limit)}</div>;
})}
</>
)}
{!streaming && parts.map(p => <div key={p.key}>{p.node(p.count)}</div>)}
{/* 推演进行中:给一个「跳过推演」,不想等的用户一键落到完整卡 */}
{streaming && onSkip && (
<button
onClick={onSkip}
data-role="skip-reasoning"
className="ghost-btn"
style={{
alignSelf: 'flex-start', display: 'flex', alignItems: 'center', gap: 4, cursor: 'pointer',
fontSize: 11.5, fontWeight: 700, color: tk.textMuted,
background: tk.panelBgAlt, border: `1px solid ${tk.border}`, borderRadius: 8, padding: '3px 10px',
}}
>
<ChevronsRight size={11} />{t.agent.skip}
</button>
)}
</div>
</div>
</div>
);
}