import { Fragment, forwardRef, memo, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type FocusEvent as ReactFocusEvent, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, } from 'react' import { createPortal } from 'react-dom' import { ListChecks } from 'lucide-react' import clsx from 'clsx' import { rovingTarget } from '../components/ui' import { EVIDENCE_STATE, alertTier, claimCounts, claimLabel, claimProblems, conflicting, docTally, segmentKind, segmentLabel, segmentState, tallySummary, unitText, isJudged, limitOf, unitVerdict, verdictMissing, verdictState, withVerdicts, type EvidenceStateCode, type EvidenceTally as Tally, } from '../lib/evidence' import { formatNumber } from '../lib/format' import { EVIDENCE_STATE_LABEL, EVIDENCE_TEXT, JUDGE_TEXT, claimTally, evidenceTally, type ClaimTallyCounts } from '../lib/terms' import { useEvidence, useVerdicts } from '../store/evidence' import type { EvidenceBlock, EvidenceDocData, EvidenceSegment, EvidenceUnit, EvidenceVerdict } from '../types' import { EvidencePanel, type PanelMode, type PanelView } from './EvidencePanel' import { BlockView, Markdown, StyledSlice, inlineStyles, numericColumns, type InlineStyles } from './Markdown' /** * 报告撰写节点的文档:逐段可点的证据。 * * 按后端切好的块、句、片段画,不再解析 Markdown:片段的边界、每个数字的出处都是后端 * 算定、进了封存的,前端重新解析一遍只会切出另一套边界。块的外观借 Markdown.tsx 的 * BlockView,和普通答案长得一模一样;有出处的数字画细实线,没有出处的画点状线、 * 句末挂「无证据」。 * * 结论句的判定(四期)只在句末挂一枚小徽标(◆ ◇ ! ○ ?),整句不画线;证据相矛盾(和旧的证据不支持)的整句 * 铺一层浅底。徽标也是按钮:点开是这一句的「模型的解释」。判定是模型给的,颜色一律不用确定性的绿。 * * 键盘:整份报告只占一个 Tab 位(roving tabindex)。←/→ 在有状态的片段、句末徽标间走,↑/↓ 按 * 句子走(表格里按列走到上一行 / 下一行,出了表格接着按句子走),n / N 跳到下一处 / * 上一处有问题的地方(无证据、可疑名称、证据相矛盾、部分有依据;证据不足的排在这些之后),回车打开证据面板, * Esc 关掉并留在原片段;面板开着时方向键走到哪、面板跟到哪,Tab 进面板。 * * 长报告:事件都挂在容器上(片段只带 data-seg),块用 memo,外加 * content-visibility: auto;超过 TEXT_CAP 的部分先折叠,跳转到折叠里的片段时自动展开。 */ export interface EvidenceDocHandle { /** 跳到下一处(dir=-1 上一处)无证据并打开面板。一处都画不出来时打开违规清单;什么都没有返回 false */ next: (dir?: 1 | -1) => boolean /** 打开违规清单。没有违规返回 false */ list: () => boolean /** 打开某个片段的面板(记录页的审计表点一行)。focus 为 true 时焦点也挪到正文里那一段;没有这个片段返回 false */ open: (segId: string, opts?: { focus?: boolean }) => boolean } /** 和 AssistantStream 的长文本折叠同一个量级:先显示这么多字,后面的折起来 */ const TEXT_CAP = 3000 /** 句末徽标在键盘顺序里的键:片段 id 是 s0…,不会撞 */ const badgeKey = (unit: string) => `claim:${unit}` const unitOfKey = (key: string) => (key.startsWith('claim:') ? key.slice(6) : null) interface Badge { unit: EvidenceUnit; block: number; verdict: EvidenceVerdict; state: EvidenceStateCode } interface Model { /** 可点的片段和句末徽标,按正文顺序 */ order: string[] index: Map seg: Map /** 句末徽标:键是 claim:,带着这一句眼下的判定 */ badge: Map /** 每个有可点片段的句子里第一个可点片段,按正文顺序(↑/↓ 用) */ unitFirst: string[] unitOf: Map /** * 有问题的片段和句末徽标(n / N 用):先第 1 轮(无证据、可疑名称、证据相矛盾、部分有依据),再第 2 轮 * (证据不足),每一轮里按正文顺序 */ alerts: string[] /** alerts 里每一处在第几轮 */ tier: Map /** 每块到它为止的累计字数(折叠用) */ chars: number[] /** 表格里的片段在第几行第几列(表头 row = -1 不进来);↑/↓ 在表格里按列走 */ cell: Map /** `块:行:列` → 那一格里第一个可点的片段 */ grid: Map } function buildModel(blocks: EvidenceBlock[], overlay?: Record): Model { const order: string[] = [] const index = new Map() const seg = new Map() const badge = new Map() const unitFirst: string[] = [] const unitOf = new Map() const tiered: [string, 1 | 2][] = [] const chars: number[] = [] const cell = new Map() const grid = new Map() let total = 0 blocks.forEach((block, b) => { for (const unit of block.units ?? []) { let first = true const loc = block.type === 'table' && unit.loc && unit.loc.row >= 0 ? unit.loc : null for (const s of unit.segments ?? []) { if (s.kind !== 'structural') total += s.text.length const state = segmentState(s) if (!state) continue seg.set(s.id, { seg: s, unit, block: b }) index.set(s.id, order.length) order.push(s.id) if (first) { unitFirst.push(s.id); first = false } unitOf.set(s.id, unitFirst.length - 1) const t = alertTier(state) if (t) tiered.push([s.id, t]) if (loc) { cell.set(s.id, { block: b, row: loc.row, col: loc.col }) const key = `${b}:${loc.row}:${loc.col}` if (!grid.has(key)) grid.set(key, s.id) } } // 句末徽标排在这一句的片段后面:←/→ 走到句尾就是它,证据相矛盾、部分有依据、证据不足的也进 n / N const verdict = unitVerdict(unit, overlay) const state = verdictState(verdict) if (verdict && state) { const key = badgeKey(unit.id) badge.set(key, { unit, block: b, verdict, state }) index.set(key, order.length) order.push(key) if (first) { unitFirst.push(key); first = false } unitOf.set(key, unitFirst.length - 1) const t = alertTier(state) if (t) tiered.push([key, t]) } } chars.push(total) }) // 按(轮次、正文顺序)排:tiered 本来就是正文顺序,稳定排序只按轮次挪 const sorted = [...tiered].sort((a, b) => a[1] - b[1]) return { order, index, seg, badge, unitFirst, unitOf, alerts: sorted.map(([k]) => k), tier: new Map(sorted), chars, cell, grid } } /** 片段或徽标在第几块 */ const blockOf = (model: Model, key: string | null | undefined): number => (key ? model.seg.get(key)?.block ?? model.badge.get(key)?.block : undefined) ?? -1 /** * ↑/↓ 该去哪。表格里按列走:下一行同一列(那一格空着就找这一行离它最近的一格); * 走出表格的上下边,接着按句子走到表格前后那一句——不在表格里一格一格地横着挪 */ function verticalTarget(model: Model, id: string, down: boolean): string | null { const n = model.unitFirst.length if (!n) return null const at = model.cell.get(id) if (at) { const rows = [...new Set([...model.cell.values()].filter((c) => c.block === at.block).map((c) => c.row))].sort((a, b) => a - b) const next = rows[rows.indexOf(at.row) + (down ? 1 : -1)] if (next !== undefined) { const same = model.grid.get(`${at.block}:${next}:${at.col}`) if (same) return same const near = [...model.cell.entries()].filter(([, c]) => c.block === at.block && c.row === next) .sort((a, b) => Math.abs(a[1].col - at.col) - Math.abs(b[1].col - at.col))[0] if (near) return near[0] } // 出了表格:往下找表格之后的第一句,往上找表格之前的最后一句 const u = model.unitOf.get(id) ?? 0 const list = down ? model.unitFirst.slice(u + 1) : model.unitFirst.slice(0, u).reverse() const out = list.find((sid) => blockOf(model, sid) !== at.block) if (out) return out // 表格就是整份报告的首尾:和句子一样首尾相接 const wrap = down ? model.unitFirst : [...model.unitFirst].reverse() return wrap.find((sid) => blockOf(model, sid) !== at.block) ?? null } const u = model.unitOf.get(id) ?? 0 return model.unitFirst[(u + (down ? 1 : -1) + n) % n] ?? null } /** 片段的外观:线型走 data 属性(CSS 里只管线型),颜色写成变量交给 index.css(引文前的引号用 --ev-glyph) */ function segStyle(state: EvidenceStateCode): CSSProperties { const meta = EVIDENCE_STATE[state] return { '--ev-line': meta.decoration, '--ev-soft': meta.soft, '--ev-glyph': meta.color } as CSSProperties } /** 句末小标签:哪几种异常态在句末挂字形加文字(去掉颜色也读得出)。可疑名字和无证据分开挂 */ const TAG_TEXT: Partial> = { none: EVIDENCE_STATE_LABEL.none, suspect: EVIDENCE_TEXT.suspectTag, } export const EvidenceDoc = forwardRef void /** 自己显示「N/N 数字有出处」那一条。上面已经有出具横幅(它会写这一行)时不用 */ tally?: boolean /** * 运行类别(formal / exploratory):「请模型判断这句」只在探索运行里有。开了裁判的文档自己记着 * (on_demand / inline),没开的看这里,不给就按运行 id 取一次 */ runClass?: string }>(function EvidenceDoc({ doc, artifact, runId, dense = false, label, panel = 'auto', dock, onView, tally = false, runClass }, ref) { const blocks = useMemo(() => doc.blocks ?? [], [doc]) const report = doc.node_id || undefined // 封存之后按需追加的判定:盖过文档里的「未裁判 · 按需」。只换句末徽标,封存的文档一个字不改 const overlay = useVerdicts(runId, report) const model = useMemo(() => buildModel(blocks, overlay), [blocks, overlay]) const counts = useMemo(() => docTally(doc), [doc]) const claims = useMemo(() => claimCounts(doc, overlay), [doc, overlay]) // 探索运行的报告(开了裁判的按需文档,或调用方说了是探索运行):证据图带着这次运行里之前按需判过的句子 // (reports[].post_seal_verdicts),取一次,重新打开页面时徽标还在。别的报告不多发这个请求 const loadGraph = useEvidence((s) => s.loadGraph) const lateOnes = doc.judge?.mode === 'on_demand' || (runClass === 'exploratory' && doc.judge?.mode !== 'inline') useEffect(() => { if (lateOnes && runId) void loadGraph(runId) }, [lateOnes, runId, loadGraph]) const rootRef = useRef(null) const instance = useId() const panelId = `${instance}-panel` const summaryId = `${instance}-summary` const [current, setCurrent] = useState(() => model.order[0] ?? null) const [view, setView] = useState(null) const [announce, setAnnounce] = useState('') const [expanded, setExpanded] = useState(false) const pendingFocus = useRef(null) const mode = usePanelMode(panel, dense) // 换了一份文档:焦点位回到第一个片段,面板关掉。只认文档本身——判定回来了(徽标变了)不算换文档, // 面板正开着这一句的解释,不能因此收起 useEffect(() => { setCurrent(model.order[0] ?? null) setView(null) // eslint-disable-next-line react-hooks/exhaustive-deps }, [blocks]) // 拿着 Tab 位的徽标没了(判成「不是结论句」):Tab 位交回第一个,整份报告不能一个 Tab 位都没有 useEffect(() => { if (current && !model.index.has(current)) setCurrent(model.order[0] ?? null) }, [model, current]) // 同一时刻只开一个面板:另一份报告打开了自己的面板,这边的就收起 const owner = useEvidence((s) => s.owner) const claim = useEvidence((s) => s.claim) useEffect(() => { if (view && owner && owner !== instance) setView(null) }, [owner, instance, view]) const foldAt = useMemo(() => { const total = model.chars[model.chars.length - 1] ?? 0 if (total <= TEXT_CAP * 1.2) return blocks.length const at = model.chars.findIndex((c) => c > TEXT_CAP) return Math.max(1, at < 0 ? blocks.length : at) }, [model, blocks.length]) const shownBlocks = expanded ? blocks.length : foldAt const totalChars = model.chars[model.chars.length - 1] ?? 0 const segEl = useCallback((id: string) => { const unit = unitOfKey(id) const sel = unit ? `[data-ev-claim="${CSS.escape(unit)}"]` : `[data-seg="${CSS.escape(id)}"]` return rootRef.current?.querySelector(sel) ?? null }, []) /** 把焦点挪到某个片段(或句末徽标)上。在折叠里的先展开,渲染出来再聚焦 */ const focusSeg = useCallback((id: string, opts?: { flash?: boolean }) => { const block = blockOf(model, id) if (block < 0) return setCurrent(id) if (block >= shownBlocks) { pendingFocus.current = id setExpanded(true) return } const el = segEl(id) if (!el) return el.focus({ preventScroll: false }) if (opts?.flash) flashOnce(el) }, [model, shownBlocks, segEl]) useLayoutEffect(() => { const id = pendingFocus.current if (!id || !expanded) return pendingFocus.current = null const el = segEl(id) if (el) { el.focus(); flashOnce(el) } }, [expanded, segEl]) const openSeg = useCallback((id: string) => { const badge = model.badge.get(id) if (badge) { claim(instance) setView({ kind: 'unit', id: badge.unit.id }) setAnnounce(`${JUDGE_TEXT.section}:${claimLabel(badge.unit, badge.verdict)}`) return } const hit = model.seg.get(id) if (!hit) return claim(instance) setView({ kind: 'seg', id }) setAnnounce(`${EVIDENCE_TEXT.panelTitle}:${segmentLabel(hit.seg, doc)}`) }, [model, doc, claim, instance]) const openViolations = useCallback(() => { claim(instance) setView({ kind: 'violations' }) setAnnounce(`${EVIDENCE_TEXT.violations}:${formatNumber(doc.violations?.length ?? 0)} 条`) }, [claim, instance, doc]) const close = useCallback((refocus = true) => { const back = view?.kind === 'seg' ? view.id : view?.kind === 'unit' ? badgeKey(view.id) : current setView(null) setAnnounce('') if (refocus && back) requestAnimationFrame(() => segEl(back)?.focus()) }, [view, current, segEl]) /** * n / N 的下一处:按(轮次、正文位置)排的一圈,走到头接回开头。当前位置不是问题(普通片段、有依据的徽标)时 * 按第 1 轮算——第 1 轮里它后面没有了,就接着走第 2 轮(证据不足) */ const step = useCallback((list: string[], from: string | null, dir: 1 | -1): string | null => { if (!list.length) return null const rank = (id: string | null): [number, number] => [id ? model.tier.get(id) ?? 1 : 1, id ? model.index.get(id) ?? -1 : -1] const cmp = (a: [number, number], b: [number, number]) => a[0] - b[0] || a[1] - b[1] const cur = rank(from) if (dir > 0) return list.find((id) => cmp(rank(id), cur) > 0) ?? list[0] for (let i = list.length - 1; i >= 0; i--) if (cmp(rank(list[i]), cur) < 0) return list[i] return list[list.length - 1] }, [model]) // 面板开关告诉页面(dock 的空位据此显示「点片段看出处」) const open = !!view useEffect(() => { onView?.(open) }, [open, onView]) const viewKey = view?.kind === 'seg' ? view.id : view?.kind === 'unit' ? badgeKey(view.id) : null useImperativeHandle(ref, () => ({ open: (id, opts) => { if (!model.seg.has(id) && !model.badge.has(id)) return false if (opts?.focus) focusSeg(id, { flash: true }) else setCurrent(id) openSeg(id) return true }, next: (dir = 1) => { const target = step(model.alerts, viewKey ?? current, dir) if (target) { focusSeg(target, { flash: true }) openSeg(target) return true } if (doc.violations?.length) { openViolations(); return true } return false }, list: () => { if (!doc.violations?.length) return false openViolations() return true }, }), [step, model, viewKey, current, focusSeg, openSeg, openViolations, doc]) // 事件委托:片段只带 data-seg、句末徽标只带 data-ev-claim,点击和按键都在容器上认 const segOf = (target: EventTarget | null): string | null => { const el = (target as HTMLElement | null)?.closest?.('[data-seg], [data-ev-claim]') if (!el || !rootRef.current?.contains(el)) return null return el.dataset.seg ?? (el.dataset.evClaim ? badgeKey(el.dataset.evClaim) : null) } const onClick = (e: ReactMouseEvent) => { const id = segOf(e.target) if (!id) return e.preventDefault() setCurrent(id) if (viewKey === id) close(false) else openSeg(id) } const onFocus = (e: ReactFocusEvent) => { const id = segOf(e.target) if (id && id !== current) setCurrent(id) } const onKeyDown = (e: ReactKeyboardEvent) => { const id = segOf(e.target) if (!id || e.altKey || e.metaKey || e.ctrlKey) return const i = model.index.get(id) ?? 0 let target: string | null = null if (e.key === 'ArrowLeft' || e.key === 'ArrowRight' || e.key === 'Home' || e.key === 'End') { const to = rovingTarget(e, i, model.order.length, 'horizontal') target = to >= 0 ? model.order[to] : null } else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { target = verticalTarget(model, id, e.key === 'ArrowDown') } else if (e.key === 'n' || e.key === 'N') { target = step(model.alerts, id, e.shiftKey || e.key === 'N' ? -1 : 1) if (!target) return } else if (e.key === 'Escape' && view) { e.preventDefault() e.stopPropagation() close() return } else if (e.key === 'Tab' && !e.shiftKey && view && mode !== 'inline') { // 面板在页面末尾(portal)或页面另一栏(dock):Tab 直接进面板,不让人穿过整页去找 const heading = document.getElementById(`${panelId}-title`) if (heading) { e.preventDefault(); heading.focus() } return } else { return } e.preventDefault() e.stopPropagation() if (!target) return focusSeg(target) // 面板开着:跟着走,读屏从 live region 听到新片段 if (view) openSeg(target) } const activeSeg = viewKey const activeBlock = blockOf(model, activeSeg) const currentBlock = blockOf(model, current) const visual = blocks.length > 40 const panelNode = view && ( close()} onLocate={(id) => { focusSeg(id, { flash: true }); openSeg(id) }} onViolations={openViolations} /> ) // 画不了线的两种分开说:列表序号、代码块标签里的数字,和正文里根本没有对应字的(句末依据里的引用) const summary = tallySummary({ ...counts, claims }, model.order.length > 0) return (

{summary}

{announce}
{tally && (counts.total > 0 || counts.other > 0 || !!counts.suspect || !!claims?.total) && ( { const target = step(model.alerts, current, 1) if (target) { focusSeg(target, { flash: true }); openSeg(target) } else openViolations() }} onList={doc.violations?.length ? openViolations : undefined} /> )}
{blocks.slice(0, shownBlocks).map((block, b) => ( // 栏内面板紧跟在片段所在的块后面:片段和它的证据在视线上挨着 {mode === 'inline' && b === activeBlock && panelNode} ))} {mode === 'inline' && view?.kind === 'violations' && panelNode}
{shownBlocks < blocks.length && (
…以下内容已折叠
)} {expanded && foldAt < blocks.length && (
)} {mode === 'dock' && panelNode && dock && createPortal(panelNode, dock)} {mode !== 'inline' && mode !== 'dock' && panelNode && createPortal(panelNode, document.body)}
) }) /** 定位到的片段描一下边(CSS 的 [data-flash]),同 AssistantStream 的 flash */ function flashOnce(el: HTMLElement) { el.dataset.flash = 'focus' setTimeout(() => { if (el.dataset.flash === 'focus') delete el.dataset.flash }, 1600) } /** 面板放哪。auto:窄栏(dense)里栏内展开,宽屏侧边,窄屏底部抽屉;屏宽变了跟着变 */ function usePanelMode(panel: 'auto' | PanelMode, dense: boolean): PanelMode { // dock 由页面自己定宽窄(记录页窄的时候换成 drawer 再传进来),这里原样用 const query = '(min-width: 900px)' const [wide, setWide] = useState(() => typeof matchMedia !== 'function' || matchMedia(query).matches) useEffect(() => { if (panel !== 'auto' || dense || typeof matchMedia !== 'function') return const mq = matchMedia(query) const on = () => setWide(mq.matches) on() mq.addEventListener('change', on) return () => mq.removeEventListener('change', on) }, [panel, dense]) if (panel !== 'auto') return panel if (dense) return 'inline' return wide ? 'side' : 'drawer' } /** * 证据条:没有出具横幅时由文档自己说「7/12 数字有出处 · 无证据 5 · 另有 1 处引用解析不了」, * 有结论句判定时接着说「结论 4 句(支持 3 · 不支持 1)」 */ export function EvidenceTally({ counts, onNext, onList }: { counts: Pick onNext?: () => void onList?: () => void }) { const suspect = counts.suspect ?? 0 const claims = counts.claims const clean = !counts.none && !counts.other && !suspect && !claimProblems(claims) const numbers = evidenceTally(counts.cited, counts.total, counts.other) return (
{numbers && ( {numbers} )} {/* 可疑名字另起一句:它不是数字,混进「无证据 M」就算不平了 */} {suspect > 0 && ( {numbers ? '· ' : ''}{EVIDENCE_STATE.suspect.glyph} {EVIDENCE_TEXT.suspectTag} {formatNumber(suspect)} )} {claims && claims.total > 0 && 0} />} {!clean && onNext && ( )} {onList && ( )}
) } /** * 「结论 4 句(有依据 3 · 证据相矛盾 1)」:和数字那一段同一种说法。判定是模型给的:全都支持也不用确定性的绿, * 有问题(证据相矛盾、旧的证据不支持、部分有依据、证据不足)、没挂依据的用提醒色,其余用暗字色(它是概率性的, * 不该和「有出处」一样笃定) */ export function ClaimTally({ claims, lead }: { claims: ClaimTallyCounts; lead: boolean }) { const text = claimTally(claims) if (!text) return null const warn = claimProblems(claims) > 0 || claims.uncited > 0 return ( {lead ? '· ' : ''}{text} ) } // ------------------------------------------------------------------------- // 块与片段 // ------------------------------------------------------------------------- /** 列表项的开头:结构片段里带着行首的 - / 1. 。列表项被分成几句时,后面几句接在同一项里 */ const ITEM_START = /(^|\n)[ \t]*([-*+]|\d+[.)])[ \t]+$/ const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, active, panelId, model }: { block: EvidenceBlock dense: boolean doc: EvidenceDocData /** 句末徽标从这里取:这一句眼下的判定(封存后追加的盖过文档里的) */ model: Model /** * 长文档:块上加 content-visibility,屏幕外的块不排版不绘制。短文档不加:它自带 paint * containment,贴着块边的片段的焦点描边会被裁掉一截,而短文档本来就不慢 */ visual: boolean /** 这一块里拿着 Tab 位的片段 */ current: string | null /** 这一块里面板正开着的片段 */ active: string | null panelId: string }) { const units = block.units ?? [] const root = { 'data-block': block.id, ...(visual ? { style: { contentVisibility: 'auto', containIntrinsicSize: 'auto 2.5em' } as CSSProperties } : {}), } const unit = (u: EvidenceUnit, opts?: { code?: boolean; tag?: boolean }) => ( ) switch (block.type) { case 'heading': return unit(u)) }} /> case 'hr': return case 'quote': return unit(u)) }} /> case 'code': return ( unit(u, { code: true, tag: false })), }} /> ) case 'list': { const items: { key: string; depth: number; units: EvidenceUnit[] }[] = [] for (const u of units) { const lead = u.segments?.[0] const starts = !items.length || (lead?.kind === 'structural' && ITEM_START.test(lead.text)) if (starts) items.push({ key: u.id, depth: u.depth ?? 0, units: [u] }) else items[items.length - 1].units.push(u) } return ( ({ key: it.key, depth: it.depth, body: it.units.map((u) => unit(u)) })), }} /> ) } case 'table': { // 表头 row = -1;一格可能被分成几句,按位置归拢 const cells = new Map() let cols = 0 const rowIds: number[] = [] for (const u of units) { const loc = u.loc ?? { row: 0, col: 0 } const k = `${loc.row}:${loc.col}` cells.set(k, [...(cells.get(k) ?? []), u]) cols = Math.max(cols, loc.col + 1) if (loc.row >= 0 && !rowIds.includes(loc.row)) rowIds.push(loc.row) } rowIds.sort((a, b) => a - b) const text = (row: number, col: number) => (cells.get(`${row}:${col}`) ?? []).map(unitText).join('') const node = (row: number, col: number) => (cells.get(`${row}:${col}`) ?? []).map((u) => unit(u, { tag: false })) const head = Array.from({ length: cols }, (_, c) => text(-1, c)) const rows = rowIds.map((r) => Array.from({ length: cols }, (_, c) => text(r, c))) return ( node(-1, c)), rows: rowIds.map((r) => Array.from({ length: cols }, (_, c) => node(r, c))), numeric: numericColumns(head, rows), csv: () => [head, ...rows].map((r) => r.map((v) => (/[",\n]/.test(v) ? `"${v.replace(/"/g, '""')}"` : v)).join(',')).join('\n'), }} /> ) } default: return unit(u)) }} /> } }) /** * 一句话:文字按行内 Markdown 渲染(整句算一次样式,跨片段的 `code`、**粗体** 不会 * 被切坏),有状态的片段包成按钮。含无证据片段的句子末尾挂「? 无证据」;有判定的结论句末尾挂 * 一枚徽标(也是按钮),证据相矛盾(和旧的证据不支持)的整句铺浅底——字本身不画线 */ function UnitView({ unit, doc, current, active, panelId, code, tag, badge }: { unit: EvidenceUnit; doc: EvidenceDocData; current: string | null; active: string | null; panelId: string code?: boolean; tag: boolean; badge?: Badge }) { const segs = (unit.segments ?? []).filter((s) => s.kind !== 'structural') const text = segs.map((s) => s.text).join('') const styles = useMemo(() => (code ? null : inlineStyles(text)), [text, code]) let pos = 0 const alerts = new Set() const nodes = segs.map((s) => { const from = pos pos += s.text.length const state = segmentState(s) const body = styles ? : s.text if (!state) return {s.strong ? {body} : body} const meta = EVIDENCE_STATE[state] if (meta.alert && TAG_TEXT[state]) alerts.add(state) const open = active === s.id return ( ) }) const key = badge ? badgeKey(unit.id) : '' const meta = badge ? EVIDENCE_STATE[badge.state] : null const open = !!badge && active === key return ( <> {conflicting(badge?.state) // 证据相矛盾(和旧的证据不支持):整句铺一层浅底(折行时每行各自带圆角),字色、字重都不动,也不画线 ? {nodes} : nodes} {tag && [...alerts].map((state) => ( ))} {badge && meta && ( )} ) } /** * 成果里带证据的那个字段:按工件 id 取文档,取到之前和取不到时照普通答案显示—— * 证据是锦上添花,拿不到不能让答案本身消失。以下情况也不画,免得把证据挂到别的字上: * - 文档的正文和字段对不上(字段被模板加工过),或者文档根本没记正文; * - 文档记着的运行和这份成果的运行不是同一个(成果上的 doc_artifact 是可以改写的一列) */ export const EvidenceField = forwardRef void runClass?: string }>(function EvidenceField({ artifact, text, runId, dense, label, tally, onDoc, runClass }, ref) { const slot = useEvidence((s) => s.docs[artifact]) const loadDoc = useEvidence((s) => s.loadDoc) useEffect(() => { void loadDoc(artifact) }, [artifact, loadDoc]) const doc = slot?.status === 'ok' ? slot.data ?? null : null const mismatch = !!doc && (typeof doc.markdown !== 'string' || doc.markdown.trim() !== text.trim()) const otherRun = !!doc && !!runId && typeof doc.run_id === 'string' && !!doc.run_id && doc.run_id !== runId const usable = doc && !mismatch && !otherRun ? doc : null const overlay = useVerdicts(runId, usable?.node_id || undefined) const counted = useMemo(() => (usable ? withVerdicts(usable, overlay) : null), [usable, overlay]) useEffect(() => { onDoc?.(counted) }, [counted, onDoc]) if (usable) { return } const note = slot?.status === 'error' ? EVIDENCE_TEXT.docMissing : otherRun ? EVIDENCE_TEXT.docOtherRun : mismatch ? EVIDENCE_TEXT.docMismatch : '' return (
{note &&
{note}
}
) })