新增模块

This commit is contained in:
2026-10-18 15:27:02 +08:00
parent 5b7d1e8f64
commit f2e6d2b3f1
228 changed files with 46089 additions and 11 deletions
@@ -0,0 +1,420 @@
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 (
<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>
);
}
/** 小节思考提示:斜体 + info 色,让各区块的生成有呼吸感 */
function BlockThink({ text, tk }: { text: string; tk: ThemeTokens }) {
return (
<div className="content-reveal" style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '2px 0' }}>
<Dots tk={tk} />
<span style={{ fontSize: 11.5, fontStyle: 'italic', color: tk.info, lineHeight: 1.55 }}>{text}</span>
</div>
);
}
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 (
<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} 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>
);
}
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 ? <ArrowRight size={11} /> : a.ontology ? <Layers size={11} /> : <ChevronRight size={11} />;
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<Record<BlockKey, Phase>>(() =>
Object.fromEntries(BLOCKS.map(k => [k, 'hidden'])) as Record<BlockKey, Phase>
);
const scheduleRef = useRef<BlockSchedule[]>([]);
const startRef = useRef<number>(0);
const streamStartRef = useRef<number>(0);
const preparingRef = useRef<boolean>(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<BlockKey, Phase>);
startRef.current = Date.now();
// 预排各正文块的 thinking / ready 时间点,按相对结论输出完成后的绝对时间推进
let offset = 80;
scheduleRef.current = BLOCKS.reduce<BlockSchedule[]>((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 (
<div 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={answer.steps.length} elapsed={elapsed} running={false} 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,
}}>
{/* ① 结论:先「整理结论中」停顿,再逐字流出 */}
<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 }}>
{preparing ? (
<span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Dots tk={tk} />
<span style={{ fontStyle: 'italic', color: tk.info }}>正在整合关键数字与规则口径,生成结论…</span>
</span>
) : (
<>
{streamed}
{!conclusionDone && <span className="stream-cursor" style={{ marginLeft: 2, display: 'inline-block', width: 2, height: 14, background: tk.accent, verticalAlign: 'middle' }} />}
</>
)}
</span>
</div>
{/* ② 关键数字 */}
{phases.metrics === 'thinking' && <BlockThink text={t.agent.thinkNumbers} tk={tk} />}
{phases.metrics === 'ready' && !!answer.metrics.length && (
<div className="content-reveal" style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
<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.map((m, i) => {
const c = tone(m.tone);
return (
<span key={i} 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>
)}
{/* ③ 风险明细表 */}
{phases.table === 'thinking' && <BlockThink text={t.agent.thinkTable} tk={tk} />}
{phases.table === 'ready' && answer.table && (
<div className="content-reveal" style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
<SecHead
icon={<Table2 size={11} color={tk.textMuted} />} text={t.agent.secTable} tk={tk}
hint={`${answer.table.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 }}>
{answer.table.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>
{answer.table.rows.map((r, ri) => {
const c = tone(r.tone);
return (
<tr key={ri} style={{ background: r.tone ? `${c}0d` : undefined, borderBottom: ri < answer.table!.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>
)}
{/* ④ 依据与口径 */}
{phases.points === 'thinking' && <BlockThink text={t.agent.thinkBasis} tk={tk} />}
{phases.points === 'ready' && !!answer.points.length && (
<div className="content-reveal" style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
<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.map((p, i) => {
const c = tone(p.tone);
return (
<div key={i} 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>
)}
{/* ⑤ 建议动作 */}
{phases.actions === 'thinking' && <BlockThink text={t.agent.thinkActions} tk={tk} />}
{phases.actions === 'ready' && !!answer.actions.length && (
<div className="content-reveal" style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
<SecHead icon={<Lightbulb size={11} color={tk.textMuted} />} text={t.agent.secActions} tk={tk} />
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', marginTop: 4 }}>
{answer.actions.map((a, i) => (
<button
key={i}
onClick={() => run(a)}
data-role={a.locateSO ? `locate-${a.locateSO}` : a.ontology ? 'open-ontology' : 'ask-more'}
className="ghost-btn"
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',
}}
>
{iconOf(a)}{a.label}
</button>
))}
</div>
</div>
)}
{/* ⑥ 数据来源 */}
{phases.source === 'thinking' && <BlockThink text={t.agent.thinkSource} tk={tk} />}
{phases.source === 'ready' && (
<div className="content-reveal" 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>
)}
</div>
</div>
</div>
);
}