mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-22 04:02:03 +00:00
账本前端:证据「口径」一步、数字上的小标记、问数据轮次摘要与出具 / 对账、可沉淀的问题、设置里的口径账本一组
第 2、3 波只有接口,这一波接上界面(docs/design/next-round/02-口径账本.md 做法 3–6、8 的界面、做法 11;总方案 X3、X10)。
界面层不放逻辑:标签、整题判断、能不能出具、能不能自动、不能的原因、要不要先核对参数都照 GET /api/runs/{id}/ledger 给的画。
- 比对结果不靠实时流(第 2 波遗留):ledger.matched 在封存之后追加、事件总线已经关了,打开着的页面收不到。store/ledger
按退避轮询(1、2、3、5、8、13 秒,之后每 15 秒),服务端答 pending 时写「正在比对口径」,等满 2 分钟还没结论照实说、
给「再取一次」;要自动出具的再等出具的那一次出现,出具、对账的运行没结束的等它们结束。只在有组件看着时轮询,同一次
运行的正文、面板、轮次摘要共用一份
- 证据正文(EvidenceDoc):来自口径的数字后面挂一个小标记,写明受管 / 已发布,悬停写是哪一份口径、怎么认的;相关口径的
数不加标记
- 证据面板出处链最上面加「口径」一步(run/LedgerStep):来自口径——「X」v3(受管)· 参数 · 等价已证明 / 助手选用 ·
按口径正式出具;相关口径——差在哪 + 看差多少;这次的推理——没有相关的已发布口径;没比对的写原因;脚注写「封存后比对,
不在封存范围内」,事后补比的写按哪种口径回看
- 口径对账(run/LedgerReconcile):核对参数(照动作里的入口字段画,推断的参数要先核对,遮罩值不回填)→ 发起(认不出列
怎么配时照 409 带回的选项选对照的指标)→ 结果卡片:每组按你的问法、按口径、差值(附相对差)各自点得开出处链
(apps/EvidenceDrawer,同应用页点数)
- 问数据轮次底部(run/LedgerTurn):一行摘要(几个数来自口径「X」vN、几个是这次的推理、几个没有比对)、整题等价时按口径
正式出具(能自动的写正在自动出具;不能的照写原因,使用者没有应用授权写「请搭建者出具」)、有相关口径时对账;出具过的
正式运行挂在下面,档位写完整出具 / 降档出具 / 不予出具,搭建者点开去运行;回收站里的对话只看不发起
- 可沉淀的问题(pages/ledger/CertifyQueue):工作流列表弹窗里的抽屉,确定性重复、问题聚类、被反复用的答案分开写;
「固化成口径草稿」走 /copilot/from-run 的 freeze 并打开画布,聚类没有共同的 SQL、按人挑的那一轮固化;「不是口径」
要写理由
- 设置(pages/settings/LedgerSettings,照 VerifySettings):自动按口径正式出具、每日上限、比对时限、升版后每次最多重算几组,
和其余偏好共用保存条、整组写 ledger
- 类型 typesLedger.ts、接口 api/caliberLedger.ts(挂 api.caliberLedger)、文案 lib/termsLedger.ts;types.ts 的证据片段
答复加 ledger、rendered 两个键
This commit is contained in:
@@ -16,7 +16,7 @@ import {
|
||||
} from '../lib/evidence'
|
||||
import { formatNumber } from '../lib/format'
|
||||
import {
|
||||
CHALLENGE_TEXT, EVIDENCE_STATE_LABEL, EVIDENCE_TEXT, INQUIRY_TEXT, JUDGE_TEXT, claimTally, evidenceTally,
|
||||
CHALLENGE_TEXT, EVIDENCE_STATE_LABEL, EVIDENCE_TEXT, INQUIRY_TEXT, JUDGE_TEXT, LEDGER_TEXT, claimTally, evidenceTally,
|
||||
type ClaimTallyCounts,
|
||||
} from '../lib/terms'
|
||||
import { useEvidence, useVerdicts, type Slot } from '../store/evidence'
|
||||
@@ -26,6 +26,10 @@ import type {
|
||||
} from '../types'
|
||||
import { EvidencePanel, type PanelMode, type PanelView } from './EvidencePanel'
|
||||
import { askAbout, type InquiryAccess } from './Inquiry'
|
||||
import { caliberOf, ledgerEnabled } from './LedgerStep'
|
||||
import { LEDGER_UI } from '../lib/termsLedger'
|
||||
import { useRunLedger } from '../store/ledger'
|
||||
import type { LedgerLabel } from '../typesLedger'
|
||||
import { BlockView, Markdown, StyledSlice, inlineStyles, numericColumns, type InlineStyles } from './Markdown'
|
||||
|
||||
/**
|
||||
@@ -47,6 +51,10 @@ import { BlockView, Markdown, StyledSlice, inlineStyles, numericColumns, type In
|
||||
* 上一处有问题的地方(无证据、可疑名称、证据相矛盾、部分有依据;证据不足的排在这些之后),回车打开证据面板,
|
||||
* Esc 关掉并留在原片段;面板开着时方向键走到哪、面板跟到哪,Tab 进面板。
|
||||
*
|
||||
* 口径账本(next-round 02 做法 3):来自口径的数字后面挂一个小标记,写明受管还是已发布(悬停写是哪一份口径、怎么认的);
|
||||
* 相关口径的数不加标记,只在证据面板的「口径」一步里说。标签按运行另取(GET /runs/{id}/ledger,store/ledger 按退避
|
||||
* 等比对出结果),正式运行、没有运行的文档不取。
|
||||
*
|
||||
* 长报告:事件都挂在容器上(片段只带 data-seg),块用 memo,外加
|
||||
* content-visibility: auto;超过 TEXT_CAP 的部分先折叠,跳转到折叠里的片段时自动展开。
|
||||
*/
|
||||
@@ -370,6 +378,10 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
|
||||
// 被反方审过的句子:句末挂标记(和裁判的判定并在一个徽标里),点开是整句面板
|
||||
const marks = useMemo(() => challengeMarks(graph, report, artifact), [graph, report, artifact])
|
||||
const model = useMemo(() => buildModel(blocks, overlay, ask, marks), [blocks, overlay, ask, marks])
|
||||
// 来自口径的数字上的小标记:按运行取的口径账本里这份报告的标签(只画 caliber 一种)
|
||||
const ledger = useRunLedger(runId, ledgerEnabled(runId, runClass))
|
||||
const labels = report ? ledger?.data?.reports?.[report]?.labels : undefined
|
||||
const caliberMarks = useMemo(() => caliberMarksOf(labels), [labels])
|
||||
const counts = useMemo(() => docTally(doc), [doc])
|
||||
const claims = useMemo(() => claimCounts(doc, overlay), [doc, overlay])
|
||||
const rootRef = useRef<HTMLDivElement>(null)
|
||||
@@ -664,7 +676,7 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
|
||||
<DocBlock block={block} dense={dense} doc={doc} visual={visual}
|
||||
current={b === currentBlock ? current : null}
|
||||
active={b === activeBlock ? activeSeg : null}
|
||||
panelId={panelId} model={model} ask={ask} />
|
||||
panelId={panelId} model={model} ask={ask} calibers={caliberMarks} />
|
||||
{mode === 'inline' && b === activeBlock && panelNode}
|
||||
</Fragment>
|
||||
))}
|
||||
@@ -741,7 +753,7 @@ function useSelectionPick(root: RefObject<HTMLElement | null>, anchor: RefObject
|
||||
const start = at(range.startContainer)
|
||||
if (!inside(start) || !inside(at(range.endContainer))) { setPick(null); return }
|
||||
const frag = range.cloneContents()
|
||||
frag.querySelectorAll('[data-inquiry-ask], .ev-tag, .ev-badge, .sr-only').forEach((n) => n.remove())
|
||||
frag.querySelectorAll('[data-inquiry-ask], .ev-tag, .ev-badge, .ev-caliber, .sr-only').forEach((n) => n.remove())
|
||||
const quote = (frag.textContent ?? '').replace(/\s+/g, ' ').trim()
|
||||
if ([...quote].length < 2) { setPick(null); return }
|
||||
const rects = range.getClientRects()
|
||||
@@ -878,8 +890,10 @@ export function ClaimTally({ claims, lead }: { claims: ClaimTallyCounts; lead: b
|
||||
/** 列表项的开头:结构片段里带着行首的 - / 1. 。列表项被分成几句时,后面几句接在同一项里 */
|
||||
const ITEM_START = /(^|\n)[ \t]*([-*+]|\d+[.)])[ \t]+$/
|
||||
|
||||
const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, active, panelId, model, ask }: {
|
||||
const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, active, panelId, model, ask, calibers }: {
|
||||
block: EvidenceBlock
|
||||
/** 来自口径的数字:片段 id → 小标记(口径账本) */
|
||||
calibers?: CaliberMarks
|
||||
dense: boolean
|
||||
doc: EvidenceDocData
|
||||
/** 句末徽标从这里取:这一句眼下的判定(封存后追加的盖过文档里的) */
|
||||
@@ -904,7 +918,7 @@ const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, ac
|
||||
}
|
||||
const unit = (u: EvidenceUnit, opts?: { code?: boolean; tag?: boolean }) => (
|
||||
<UnitView key={u.id} unit={u} doc={doc} current={current} active={active} panelId={panelId}
|
||||
code={opts?.code} tag={opts?.tag ?? true} badge={model.badge.get(badgeKey(u.id))}
|
||||
code={opts?.code} tag={opts?.tag ?? true} badge={model.badge.get(badgeKey(u.id))} calibers={calibers}
|
||||
ask={ask && askable(u, block) ? (model.ask.has(askKey(u.id)) ? 'roving' : 'follow') : undefined} />
|
||||
)
|
||||
|
||||
@@ -970,6 +984,23 @@ const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, ac
|
||||
}
|
||||
})
|
||||
|
||||
/** 来自口径的数字上的小标记:片段 id → 标记上的字(级别)、悬停、读屏 */
|
||||
type CaliberMarks = Map<string, { text: string; level: string; title: string; aria: string }>
|
||||
|
||||
function caliberMarksOf(labels: Record<string, LedgerLabel> | undefined): CaliberMarks | undefined {
|
||||
if (!labels) return undefined
|
||||
const out: CaliberMarks = new Map()
|
||||
for (const [id, label] of Object.entries(labels)) {
|
||||
if (label.kind !== 'caliber') continue
|
||||
const how = LEDGER_TEXT.how[label.how === 'composed' ? 'composed' : 'independent']
|
||||
const caliber = caliberOf(label)
|
||||
const level = label.level_text || (label.level ? (LEDGER_TEXT.level as Record<string, string>)[label.level] ?? '' : '')
|
||||
out.set(id, { text: level || LEDGER_TEXT.kind.caliber, level: label.level ?? '', title: LEDGER_UI.mark.title(caliber, how),
|
||||
aria: LEDGER_UI.mark.aria(caliber, how) })
|
||||
}
|
||||
return out.size ? out : undefined
|
||||
}
|
||||
|
||||
/** 反方审稿的判断在句末徽标里的样子。soft 是悬停、展开时铺的浅底,醒目的那两种不铺(见 LOUD_RING) */
|
||||
interface MarkLook { icon: LucideIcon; color: string; soft: string; loud: boolean }
|
||||
|
||||
@@ -1016,9 +1047,10 @@ function ChallengeMark({ verdict, look, after }: { verdict: string; look: MarkLo
|
||||
* 一枚徽标(也是按钮),证据相矛盾(和旧的证据不支持)的整句铺浅底——字本身不画线。被反方审过的
|
||||
* 句子同样挂徽标(和判定并在一起),点开都是整句面板
|
||||
*/
|
||||
function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask }: {
|
||||
function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask, calibers }: {
|
||||
unit: EvidenceUnit; doc: EvidenceDocData; current: string | null; active: string | null; panelId: string
|
||||
code?: boolean; tag: boolean; badge?: Badge
|
||||
calibers?: CaliberMarks
|
||||
/**
|
||||
* 审批质询的「质询」:roving 是这一句一个可点的都没有,「质询」本身进键盘顺序;follow 跟在这一句后面,焦点在这一句上时
|
||||
* 按 Tab 到达
|
||||
@@ -1041,7 +1073,8 @@ function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask }
|
||||
const meta = EVIDENCE_STATE[state]
|
||||
if (meta.alert && TAG_TEXT[state]) alerts.add(state)
|
||||
const open = active === s.id
|
||||
return (
|
||||
const mark = calibers?.get(s.id)
|
||||
const button = (
|
||||
<button
|
||||
key={s.id}
|
||||
type="button"
|
||||
@@ -1052,7 +1085,7 @@ function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask }
|
||||
// 实体、引文:线型和颜色照状态,另有自己的样子(引文前挂引号,见 index.css)
|
||||
data-ev-kind={segmentKind(s) ?? undefined}
|
||||
tabIndex={current === s.id ? 0 : -1}
|
||||
aria-label={segmentLabel(s, doc)}
|
||||
aria-label={mark ? `${segmentLabel(s, doc)}${mark.aria}` : segmentLabel(s, doc)}
|
||||
aria-expanded={open}
|
||||
aria-controls={open ? panelId : undefined}
|
||||
style={segStyle(state)}
|
||||
@@ -1060,6 +1093,18 @@ function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask }
|
||||
{s.strong ? <strong className="font-semibold">{body}</strong> : body}
|
||||
</button>
|
||||
)
|
||||
if (!mark) return button
|
||||
// 来自口径的数:后面挂一个小标记(受管 / 已发布),悬停写是哪一份口径、怎么认的。不进键盘顺序(读屏念在片段上)
|
||||
return (
|
||||
<Fragment key={s.id}>
|
||||
{button}
|
||||
<sup className="ev-caliber ml-px select-none whitespace-nowrap rounded-sm px-0.5 text-[9px] font-medium"
|
||||
style={{ color: 'var(--accent)', background: 'var(--accent-soft, transparent)' }}
|
||||
aria-hidden="true" title={mark.title} data-ev-caliber={mark.level} data-ev-caliber-of={s.id}>
|
||||
{mark.text}
|
||||
</sup>
|
||||
</Fragment>
|
||||
)
|
||||
})
|
||||
const key = badge ? badgeKey(unit.id) : ''
|
||||
const meta = badge?.state ? EVIDENCE_STATE[badge.state] : null
|
||||
|
||||
@@ -34,6 +34,7 @@ import { ArtifactViewer, ResultTable } from './AssistantStream'
|
||||
import { ChallengeSummary, ClaimVerdictPending, type ClaimWait } from './ChallengeSummary'
|
||||
import { useEvidenceHost } from './evidenceHost'
|
||||
import { EvidenceProvenance } from './EvidenceProvenance'
|
||||
import { LedgerPart } from './LedgerStep'
|
||||
import { CopyChip } from './Markdown'
|
||||
import { SqlCheckList } from './SqlChecks'
|
||||
import {
|
||||
@@ -86,6 +87,10 @@ import {
|
||||
* 输入来源里只说核对结果、不写值;遮没遮核对不了的(快照读不出来)同样不显示值,写明原因。类型都在 types.ts
|
||||
* (EvidenceDualCheck、EvidenceUnitChallenge、EvidenceInput 的 masked / mask_unverified)。
|
||||
*
|
||||
* 口径账本(next-round 02 做法 3、6):数字、取值片段的出处链最上面多「口径」一步(run/LedgerStep 的 LedgerPart)——
|
||||
* 来自口径「X」v3(受管)· 参数 · 等价已证明 / 助手选用 · 按口径正式出具;相关口径的差在哪和看差多少(口径对账);这次的
|
||||
* 推理。比对在封存之后做,不在封存范围内,脚注写明;比对还没出结果时写「正在比对口径」并按退避再取。
|
||||
*
|
||||
* 四种摆法:side 从右侧弹出(宽屏),drawer 从底部抽出(窄屏),inline 在 360px 的
|
||||
* 画布右栏里直接栏内展开,dock 放进页面给的一块常驻位置(记录页的「证据」页签)。
|
||||
* 都不是模态的:开着面板照样能在正文里走。
|
||||
@@ -424,6 +429,13 @@ function SegmentBody({ panelId, doc, artifact, runId, runClass, seg, unit, block
|
||||
<Sentence unit={unit} active={seg.id} />
|
||||
</Part>
|
||||
|
||||
{state === 'deterministic' && (isMetric || isCell) && !docIssue && (
|
||||
// 口径账本:出处链最上面的「口径」一步(来自口径 / 相关口径 / 这次的推理)。比对在封存之后做,这一步按运行另取
|
||||
// (GET /runs/{id}/ledger,还在比对时按退避再取);片段接口的 ledger 键先顶上
|
||||
<LedgerPart runId={runId} runClass={runClass} report={report} segId={seg.id} segText={seg.text}
|
||||
segLedger={detail?.ledger} />
|
||||
)}
|
||||
|
||||
{bad && (
|
||||
<Part title={state === 'suspect' ? '标记为可疑的原因' : state === 'unverified' ? '无法核实的原因' : '无证据的原因'}
|
||||
data-ev-reason="">
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Scale } from 'lucide-react'
|
||||
import { ApiError, api } from '../api/client'
|
||||
import { EvidenceDrawer, type EvidenceTarget } from '../apps/EvidenceDrawer'
|
||||
import { Field, Modal, Spinner, StatusBadge, toast } from '../components/ui'
|
||||
import { errorMessage } from '../lib/errors'
|
||||
import { LEDGER_UI } from '../lib/termsLedger'
|
||||
import { useAtLeast } from '../store/auth'
|
||||
import { useLedger } from '../store/ledger'
|
||||
import type {
|
||||
LedgerField, LedgerPairOptions, LedgerReconcileAction, LedgerReconcileIn, LedgerReconciled, LedgerReconcileGroup,
|
||||
LedgerRelated, RunLedger,
|
||||
} from '../typesLedger'
|
||||
import { BindingList, DiffList, caliberOf } from './LedgerStep'
|
||||
|
||||
/**
|
||||
* 口径对账(docs/design/next-round/02-口径账本.md 做法 6 的界面):「差在哪」比对时就有,「差多少」点开才跑一次确定性
|
||||
* 派生的只读图(来源 reconcile,服务端专用),差值在对账卡里算(叙述层无算术,界面也不算)。
|
||||
*
|
||||
* 流程:核对参数 → 发起 → 三个数各自点得开。
|
||||
* - 核对参数:服务端在动作里写了 needs_check(参数是推断的、不全的、带遮罩值的)就先让人核对,表单照动作里的入口字段画
|
||||
* (必填、有没有缺省值),遮罩值不回填、要人重新填写;不用核对的照样能「修改参数」,改了就按核对过的参数发起。
|
||||
* - 发起:服务端认不出这次的哪一列对口径的哪个指标时回 409(ledger_reconcile_needs_pairs)、带上可选的列和指标,这里照它
|
||||
* 画「选择要对照的指标」,人选好再发起;参数不对的回 422 / 409,把服务端的话写在表单下面。
|
||||
* - 结果(ReconcileResults):每一组对照写按你的问法、按口径、差值(另附相对差),每个数按
|
||||
* /runs/{对账运行}/evidence/metrics/{对账卡}/{指标} 取值,点一下打开它的出处链(apps/EvidenceDrawer,和应用页点数同一套)。
|
||||
* 哪几个指标是一组由服务端给(reconciled.groups),这里不按命名规则拼。
|
||||
*/
|
||||
|
||||
const NEEDS_PAIRS = 'ledger_reconcile_needs_pairs'
|
||||
const CHECK_INPUTS = 'ledger_reconcile_check_inputs'
|
||||
|
||||
/** 核对参数的表单:动作里的入口字段(老答复没有时按绑定的字段),值先填绑定出来的,遮罩值空着 */
|
||||
function fieldsOf(action: LedgerReconcileAction): LedgerField[] {
|
||||
if (action.fields?.length) return action.fields
|
||||
return action.binding.map((b) => ({ name: b.field, required: false, has_default: false }))
|
||||
}
|
||||
|
||||
function draftOf(action: LedgerReconcileAction): Record<string, string> {
|
||||
const out: Record<string, string> = {}
|
||||
for (const f of fieldsOf(action)) {
|
||||
const b = action.binding.find((x) => x.field === f.name)
|
||||
out[f.name] = b && !b.masked && b.value != null ? String(b.value) : ''
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
export function ReconcileDialog({ runId, action, related, onClose }: {
|
||||
runId: string; action: LedgerReconcileAction; related?: LedgerRelated; onClose: () => void
|
||||
}) {
|
||||
const refresh = useLedger((s) => s.refresh)
|
||||
const fields = fieldsOf(action)
|
||||
const [values, setValues] = useState<Record<string, string>>(() => draftOf(action))
|
||||
const [editing, setEditing] = useState(action.needs_check)
|
||||
const [options, setOptions] = useState<LedgerPairOptions | null>(null)
|
||||
const [pairs, setPairs] = useState<Record<string, string>>({})
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [problem, setProblem] = useState('')
|
||||
const caliber = caliberOf(action)
|
||||
const masked = new Set(action.binding.filter((b) => b.masked).map((b) => b.field))
|
||||
const chosen = Object.entries(pairs).filter(([, m]) => !!m).map(([ours, metric]) => ({ ours, metric }))
|
||||
const dirty = editing && JSON.stringify(values) !== JSON.stringify(draftOf(action))
|
||||
|
||||
const launch = async () => {
|
||||
if (options && !chosen.length) { setProblem(LEDGER_UI.reconcile.pairEmpty); return }
|
||||
setBusy(true)
|
||||
setProblem('')
|
||||
const body: LedgerReconcileIn = { query: action.query, workflow_id: action.workflow_id, version: action.version }
|
||||
if (editing) {
|
||||
// 空着的不送:留空的项按口径的缺省值(服务端按入口字段校验,缺必填的照实拒收)
|
||||
body.inputs = Object.fromEntries(Object.entries(values).filter(([, v]) => v.trim() !== '').map(([k, v]) => [k, v.trim()]))
|
||||
body.confirm = true
|
||||
}
|
||||
if (options) body.pairs = chosen
|
||||
try {
|
||||
await api.caliberLedger.reconcile(runId, body)
|
||||
toast.ok(LEDGER_UI.reconcile.launched)
|
||||
refresh(runId)
|
||||
onClose()
|
||||
} catch (e) {
|
||||
if (e instanceof ApiError && e.code === NEEDS_PAIRS) {
|
||||
const raw = (e.body as { options?: LedgerPairOptions } | undefined)?.options
|
||||
if (raw && Array.isArray(raw.columns) && Array.isArray(raw.metrics)) setOptions(raw)
|
||||
setProblem(e.message)
|
||||
} else if (e instanceof ApiError && e.code === CHECK_INPUTS) {
|
||||
setEditing(true)
|
||||
setProblem(e.message)
|
||||
} else {
|
||||
setProblem(errorMessage(e))
|
||||
}
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal open onClose={onClose} width={560} dirty={dirty} title={LEDGER_UI.reconcile.title(caliber)}
|
||||
footer={<>
|
||||
<button type="button" className="btn" onClick={onClose}>{LEDGER_UI.reconcile.cancel}</button>
|
||||
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => void launch()}
|
||||
data-ledger-reconcile-launch="">
|
||||
{busy ? <Spinner size={12} /> : <Scale size={12} aria-hidden />} {busy ? LEDGER_UI.reconcile.busy : LEDGER_UI.reconcile.launch}
|
||||
</button>
|
||||
</>}>
|
||||
<div className="space-y-3 text-xs" data-ledger-reconcile-dialog="">
|
||||
<p className="text-dim">{LEDGER_UI.reconcile.intro}</p>
|
||||
{related && (
|
||||
<section>
|
||||
<div className="mb-1 text-2xs font-medium text-faint">{LEDGER_UI.reconcile.where}</div>
|
||||
<DiffList related={related} />
|
||||
</section>
|
||||
)}
|
||||
<section data-ledger-params={editing ? 'edit' : 'fixed'}>
|
||||
<div className="mb-1 flex items-center gap-2">
|
||||
<span className="text-2xs font-medium text-faint">{LEDGER_UI.reconcile.params}</span>
|
||||
{!editing && fields.length > 0 && (
|
||||
<button type="button" className="btn btn-xs btn-ghost" onClick={() => setEditing(true)} data-ledger-edit-params="">
|
||||
{LEDGER_UI.reconcile.editParams}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{editing ? (
|
||||
<>
|
||||
<p className="mb-1.5 text-2xs" style={{ color: action.needs_check ? 'var(--st-waiting)' : undefined }}
|
||||
data-ledger-check-reason="">
|
||||
{action.check_reason || LEDGER_UI.reconcile.paramsHint}
|
||||
</p>
|
||||
<div className="grid grid-cols-2 gap-2 max-sm:grid-cols-1">
|
||||
{fields.map((f) => (
|
||||
<Field key={f.name} label={f.name} htmlFor={`ledger-param-${f.name}`} required={f.required}
|
||||
hint={f.has_default ? LEDGER_UI.reconcile.defaulted : f.required ? LEDGER_UI.reconcile.required : undefined}>
|
||||
{(p) => (
|
||||
<input {...p} className="field mono text-xs" value={values[f.name] ?? ''} data-ledger-param-input={f.name}
|
||||
placeholder={masked.has(f.name) ? LEDGER_UI.reconcile.maskedPlaceholder : undefined}
|
||||
onChange={(e) => setValues((v) => ({ ...v, [f.name]: e.target.value }))} />
|
||||
)}
|
||||
</Field>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="text-2xs">
|
||||
<p className="mb-1 text-faint">{LEDGER_UI.reconcile.paramsFixed}</p>
|
||||
<BindingList binding={action.binding} />
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
{options && (
|
||||
<section data-ledger-pairs="">
|
||||
<div className="mb-1 text-2xs font-medium text-faint">{LEDGER_UI.reconcile.pairs}</div>
|
||||
<p className="mb-1.5 text-2xs text-dim">{LEDGER_UI.reconcile.pairsHint}</p>
|
||||
<div className="space-y-1.5">
|
||||
{options.columns.map((col) => (
|
||||
<label key={col} className="flex items-center gap-2 text-2xs">
|
||||
<span className="mono w-28 shrink-0 truncate" title={col}>{col}</span>
|
||||
<span className="text-faint" aria-hidden>→</span>
|
||||
<select className="field text-xs" value={pairs[col] ?? ''} data-ledger-pair={col}
|
||||
onChange={(e) => setPairs((p) => ({ ...p, [col]: e.target.value }))}>
|
||||
<option value="">{LEDGER_UI.reconcile.pairNone}</option>
|
||||
{options.metrics.map((m) => (
|
||||
<option key={m.id} value={m.id}>{m.name}{m.unit ? `(${m.unit})` : ''}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
{problem && <p className="text-2xs text-[var(--err)]" role="alert" data-ledger-reconcile-problem="">{problem}</p>}
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
/** 正在取的指标值,按(运行、卡、指标)记一份:同一组数在面板、轮次两处挂着时只取一次 */
|
||||
const values = new Map<string, Promise<string>>()
|
||||
function valueOf(runId: string, card: string, metric: string): Promise<string> {
|
||||
const key = `${runId}\u0000${card}\u0000${metric}`
|
||||
let p = values.get(key)
|
||||
if (!p) {
|
||||
p = api.apps.metricEvidence(runId, card, metric).then((d) => String(d?.rendered ?? ''))
|
||||
values.set(key, p)
|
||||
p.catch(() => values.delete(key))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
/** 对账卡上的一个数:取值,点一下打开它的出处链 */
|
||||
function MetricValue({ runId, card, metric, what, label, onOpen }: {
|
||||
runId: string; card: string; metric: string; what: string; label: string; onOpen: (t: EvidenceTarget) => void
|
||||
}) {
|
||||
const [text, setText] = useState<string | null>(null)
|
||||
const [failed, setFailed] = useState(false)
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
setText(null)
|
||||
setFailed(false)
|
||||
valueOf(runId, card, metric).then((v) => { if (alive) setText(v) }, () => { if (alive) setFailed(true) })
|
||||
return () => { alive = false }
|
||||
}, [runId, card, metric])
|
||||
return (
|
||||
<span className="inline-flex items-baseline gap-1">
|
||||
<span className="text-faint">{what}</span>
|
||||
<button type="button" className="mono tnum rounded px-0.5 underline decoration-dotted underline-offset-2 hover:bg-hover"
|
||||
disabled={text == null} title={LEDGER_UI.result.open(what, label)} data-ledger-value={metric}
|
||||
onClick={() => onOpen({ evidence: { run_id: runId, kind: 'metric', node: card, metric }, label: `${label} · ${what}`,
|
||||
text: text ?? '' })}>
|
||||
{failed ? LEDGER_UI.result.noValue : text == null ? '…' : text || '—'}
|
||||
</button>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const SHOWN = 3
|
||||
|
||||
function GroupRow({ runId, card, group, onOpen }: {
|
||||
runId: string; card: string; group: LedgerReconcileGroup; onOpen: (t: EvidenceTarget) => void
|
||||
}) {
|
||||
const R = LEDGER_UI.result
|
||||
return (
|
||||
<li className="flex flex-wrap items-baseline gap-x-3 gap-y-0.5 text-2xs" data-ledger-group={group.ours}>
|
||||
<span className="font-medium">{group.label}</span>
|
||||
<MetricValue runId={runId} card={card} metric={group.ours} what={R.ours} label={group.label} onOpen={onOpen} />
|
||||
<MetricValue runId={runId} card={card} metric={group.caliber} what={R.caliber} label={group.label} onOpen={onOpen} />
|
||||
<MetricValue runId={runId} card={card} metric={group.diff} what={R.diff} label={group.label} onOpen={onOpen} />
|
||||
<MetricValue runId={runId} card={card} metric={group.rel} what={R.rel} label={group.label} onOpen={onOpen} />
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
function ReconcileCard({ item, data, original }: { item: LedgerReconciled; data?: RunLedger; original?: string }) {
|
||||
const builder = useAtLeast('builder')
|
||||
const [all, setAll] = useState(false)
|
||||
const [target, setTarget] = useState<EvidenceTarget | null>(null)
|
||||
const c = (data?.calibers ?? []).find((x) => x.caliber === item.caliber)
|
||||
const caliber = c ? caliberOf(c) : caliberOf({ version: item.version })
|
||||
const groups = item.groups ?? []
|
||||
const shown = all ? groups : groups.slice(0, SHOWN)
|
||||
const R = LEDGER_UI.result
|
||||
const state = item.status === 'succeeded' ? 'succeeded' : item.status === 'failed' ? 'failed'
|
||||
: item.status === 'cancelled' ? 'cancelled' : 'running'
|
||||
return (
|
||||
<div className="mt-1.5 rounded border px-2 py-1.5" data-ledger-reconciled={item.run_id} data-ledger-reconciled-state={state}>
|
||||
<div className="flex flex-wrap items-center gap-1.5 text-2xs">
|
||||
<StatusBadge status={state} size={11} decorative />
|
||||
<span className="font-medium">{R.title(caliber)}</span>
|
||||
{item.inputs_from && R.inputs[item.inputs_from] && <span className="text-faint">{R.inputs[item.inputs_from]}</span>}
|
||||
{builder && <Link to={`/runs/${item.run_id}`} className="text-[var(--accent)] hover:underline">{R.openRun}</Link>}
|
||||
</div>
|
||||
{state === 'running' && <p className="mt-1 flex items-center gap-1.5 text-2xs text-dim"><Spinner size={11} /> {R.running}</p>}
|
||||
{state === 'failed' && <p className="mt-1 text-2xs text-dim">{R.failed}</p>}
|
||||
{state === 'cancelled' && <p className="mt-1 text-2xs text-dim">{R.cancelled}</p>}
|
||||
{state === 'succeeded' && item.card && groups.length > 0 && (
|
||||
<>
|
||||
<ul className="mt-1 space-y-1">
|
||||
{shown.map((g) => <GroupRow key={g.ours} runId={item.run_id} card={item.card!} group={g} onOpen={setTarget} />)}
|
||||
</ul>
|
||||
{groups.length > SHOWN && (
|
||||
<button type="button" className="btn btn-xs btn-ghost mt-0.5 -ml-1" onClick={() => setAll((v) => !v)}>
|
||||
{all ? R.less : R.more(groups.length - SHOWN)}
|
||||
</button>
|
||||
)}
|
||||
{original && <p className="mt-1 text-2xs text-faint" data-ledger-original="">{R.original(original)}</p>}
|
||||
</>
|
||||
)}
|
||||
<EvidenceDrawer target={target} onClose={() => setTarget(null)} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 对过的账(新的在前)。original:从证据面板打开时,正文里这个数写的是什么 */
|
||||
export function ReconcileResults({ items, data, original }: {
|
||||
runId: string; items: LedgerReconciled[]; data?: RunLedger; original?: string
|
||||
}) {
|
||||
const sorted = useMemo(() => items, [items])
|
||||
if (!sorted.length) return null
|
||||
return (
|
||||
<div data-ledger-reconciled-list="">
|
||||
{sorted.map((item) => <ReconcileCard key={item.run_id} item={item} data={data} original={original} />)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
import { useState, type ReactNode } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { BookMarked, RotateCw, Scale } from 'lucide-react'
|
||||
import { ApiError, api } from '../api/client'
|
||||
import { Spinner, StatusBadge, toast } from '../components/ui'
|
||||
import { errorMessage } from '../lib/errors'
|
||||
import { LEDGER_TEXT } from '../lib/terms'
|
||||
import { LEDGER_UI, caliberName } from '../lib/termsLedger'
|
||||
import { useAtLeast } from '../store/auth'
|
||||
import { useLedger, useRunLedger, type LedgerSlot } from '../store/ledger'
|
||||
import type {
|
||||
LedgerBinding, LedgerIssueAction, LedgerIssued, LedgerLabel, LedgerReconcileAction, LedgerRelated, RunLedger, SegmentLedger,
|
||||
} from '../typesLedger'
|
||||
import { ReconcileDialog, ReconcileResults } from './LedgerReconcile'
|
||||
|
||||
/**
|
||||
* 口径账本在证据里的那一部分(docs/design/next-round/02-口径账本.md 做法 3、5、6 的界面):
|
||||
* - 证据面板出处链最上面的「口径」一步(LedgerPart):来自口径——「X」v3(受管)· 参数 · 怎么认的(等价已证明 / 助手选用)
|
||||
* · 按口径正式出具;相关口径——差在哪 + 看差多少(口径对账:核对参数 → 发起 → 三个数各自点得开);这次的推理——
|
||||
* 写「没有相关的已发布口径」;没比对的写原因。比对还没出结果时写「正在比对口径」,store/ledger 按退避轮询,
|
||||
* 等满了还没结论照实说、给「再取一次」;
|
||||
* - 按口径正式出具那一项(IssueControl)和出具过的正式运行(IssuedList):问数据的轮次摘要用同一份。
|
||||
*
|
||||
* 界面层不放逻辑:标签、能不能出具、能不能自动、不能的原因、要不要先核对参数都是服务端给的(GET /runs/{id}/ledger),
|
||||
* 这里只照着画。比对在封存之后做,脚注写明「封存后比对,不在封存范围内」,事后补比的另写按哪种口径回看。
|
||||
*/
|
||||
|
||||
/** 级别的叫法:服务端给 level_text,老答复没有时按 LEDGER_TEXT 的说法 */
|
||||
export const levelOf = (x: { level?: string | null; level_text?: string }) =>
|
||||
x.level_text || (x.level ? (LEDGER_TEXT.level as Record<string, string>)[x.level] ?? '' : '')
|
||||
|
||||
export const caliberOf = (x: { workflow_name?: string | null; version?: number | null; level?: string | null; level_text?: string }) =>
|
||||
caliberName(x.workflow_name, x.version, levelOf(x))
|
||||
|
||||
/** 一次运行的口径账本里,某份报告的某个片段的标签:先看按运行取的那一份,没有时退回片段接口的 ledger 键 */
|
||||
export function labelOf(data: RunLedger | undefined, report: string | undefined, segId: string,
|
||||
fallback?: SegmentLedger | null): LedgerLabel | undefined {
|
||||
const fromRun = report ? data?.reports?.[report]?.labels?.[segId] : undefined
|
||||
return fromRun ?? fallback?.label ?? undefined
|
||||
}
|
||||
|
||||
/** 能不能取账本:正式运行本身就是某一版口径的出具,不比对;没有运行(预览、导出的文档)也没有 */
|
||||
export const ledgerEnabled = (runId: string | undefined, runClass: string | undefined) => !!runId && runClass !== 'formal'
|
||||
|
||||
/**
|
||||
* 取账本出了错要不要说:服务端说这次运行不存在(404)时不说——证据链那边已经照实说了运行取不到,这里再挂一句「口径比对
|
||||
* 读不出」是同一件事说两遍;别的错(服务端出错、连不上)照实说、给「再取一次」
|
||||
*/
|
||||
export const ledgerErrorShown = (slot: LedgerSlot | undefined) =>
|
||||
slot?.status === 'error' && !(slot.error instanceof ApiError && slot.error.status === 404)
|
||||
|
||||
/** 绑定的参数:字段 = 值,遮罩值只写「已遮罩」 */
|
||||
export function BindingList({ binding }: { binding?: LedgerBinding[] }) {
|
||||
if (!binding?.length) return <span className="text-faint">{LEDGER_UI.step.noParams}</span>
|
||||
return (
|
||||
<span className="flex flex-wrap gap-1" data-ledger-binding="">
|
||||
{binding.map((b) => (
|
||||
<span key={b.field} className="chip mono" data-ledger-param={b.field} data-masked={b.masked ? '' : undefined}>
|
||||
{b.field} = {b.masked ? LEDGER_UI.step.masked : String(b.value ?? '')}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
/** 比对还没出结论 / 等满了 / 读不出:三种都说清,后两种给「再取一次」 */
|
||||
export function LedgerWaiting({ slot, runId, compact = false }: { slot: LedgerSlot; runId: string; compact?: boolean }) {
|
||||
const refresh = useLedger((s) => s.refresh)
|
||||
const retry = (
|
||||
<button type="button" className="btn btn-xs btn-ghost" onClick={() => refresh(runId)} data-ledger-retry="">
|
||||
<RotateCw size={11} aria-hidden /> {LEDGER_UI.step.retry}
|
||||
</button>
|
||||
)
|
||||
if (slot.status === 'error') {
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-1.5 text-dim" data-ledger-state="error">
|
||||
<span>{LEDGER_UI.step.failed}:{errorMessage(slot.error)}</span>{retry}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (slot.timedOut) {
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-1.5 text-dim" data-ledger-state="timeout">
|
||||
<span>{compact ? LEDGER_UI.turn.timeout : LEDGER_UI.step.timeout}</span>{retry}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div data-ledger-state="pending" role="status">
|
||||
<span className="flex items-center gap-1.5 text-dim"><Spinner size={11} /> {LEDGER_UI.step.pending}</span>
|
||||
{!compact && <span className="mt-0.5 block text-2xs text-faint">{LEDGER_UI.step.pendingSub}</span>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 比对的来历:封存后比对,不在封存范围内;事后补比的写按哪种口径回看 */
|
||||
export function LedgerFootnote({ data }: { data: RunLedger }) {
|
||||
return (
|
||||
<p className="mt-1 text-2xs text-faint" data-ledger-footnote="">
|
||||
{[data.note || LEDGER_TEXT.sub, data.retro ? data.retro_note || LEDGER_TEXT.retro : ''].filter(Boolean).join(' · ')}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 按口径正式出具(整题等价时)。服务端给了能不能、能不能自动、不能的原因:能自动的写「已在自动出具」、等出具的那一次出现
|
||||
* (等满了还没有,给按钮);只给一键的给按钮,按钮下写为什么不自动;不能出具的照写原因(使用者没有应用授权时是「请搭建者
|
||||
* 出具」)。同一运行、同一口径、同一组参数只发起一次:再点交回那一次
|
||||
*/
|
||||
export function IssueControl({ runId, action, slot, readOnly = false }: {
|
||||
runId: string; action: LedgerIssueAction; slot?: LedgerSlot; readOnly?: boolean
|
||||
}) {
|
||||
const refresh = useLedger((s) => s.refresh)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const caliber = caliberOf(action)
|
||||
const issued = action.issued_run || (slot?.data?.issued ?? []).some((i) => i.workflow_id === action.workflow_id
|
||||
&& i.version === action.version)
|
||||
if (issued) return null
|
||||
if (!action.available) {
|
||||
return <p className="text-2xs text-dim" data-ledger-issue="refused">{action.reason}</p>
|
||||
}
|
||||
if (action.auto && !slot?.timedOut) {
|
||||
return (
|
||||
<p className="flex items-center gap-1.5 text-2xs text-dim" data-ledger-issue="auto" role="status">
|
||||
<Spinner size={11} /> {LEDGER_UI.issue.auto}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
const issue = async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const out = await api.caliberLedger.issue(runId, { workflow_id: action.workflow_id, version: action.version })
|
||||
toast.ok(out.created ? LEDGER_UI.issue.launched(caliber) : LEDGER_UI.issue.again(caliber))
|
||||
refresh(runId)
|
||||
} catch (e) {
|
||||
toast.error(e)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
return (
|
||||
<div data-ledger-issue="manual">
|
||||
<button type="button" className="btn btn-xs" disabled={busy || readOnly} onClick={() => void issue()}
|
||||
title={readOnly ? LEDGER_UI.issue.readOnly : undefined} data-ledger-issue-button="">
|
||||
{busy ? <Spinner size={11} /> : <BookMarked size={11} aria-hidden />} {busy ? LEDGER_UI.issue.busy : LEDGER_UI.issue.button}
|
||||
</button>
|
||||
{action.auto && slot?.timedOut && <p className="mt-0.5 text-2xs text-faint">{LEDGER_UI.issue.autoTimeout}</p>}
|
||||
{!action.auto && action.auto_reason && (
|
||||
<p className="mt-0.5 text-2xs text-faint" data-ledger-auto-reason="">{action.auto_reason}</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 出具过的一次正式运行的状态:结束了写档位(完整出具 / 降档出具 / 不予出具),没结束写进行中 */
|
||||
function issuedState(i: LedgerIssued): { code: string; text: string } {
|
||||
if (i.status === 'succeeded') return { code: 'succeeded', text: i.tier_text || LEDGER_TEXT.kind.caliber }
|
||||
if (i.status === 'cancelled' && i.end_reason === 'rejected') return { code: 'rejected', text: LEDGER_UI.issued.rejected }
|
||||
if (i.status === 'cancelled') return { code: 'cancelled', text: LEDGER_UI.issued.cancelled }
|
||||
if (i.status === 'failed') return { code: 'failed', text: LEDGER_UI.issued.failed }
|
||||
return { code: 'running', text: LEDGER_UI.issued.running }
|
||||
}
|
||||
|
||||
/** 出具过的正式运行,挂在轮次下面、面板里:点开去运行(使用者读不了正式运行的原始记录,写明由搭建者查看) */
|
||||
export function IssuedList({ data }: { data: RunLedger }) {
|
||||
const builder = useAtLeast('builder')
|
||||
const issued = data.issued ?? []
|
||||
if (!issued.length) return null
|
||||
const nameOf = (i: LedgerIssued) => {
|
||||
const c = (data.calibers ?? []).find((x) => x.workflow_id === i.workflow_id && x.version === i.version)
|
||||
const a = (data.actions ?? []).find((x): x is LedgerIssueAction => x.kind === 'issue' && x.workflow_id === i.workflow_id
|
||||
&& x.version === i.version)
|
||||
return caliberOf(c ?? a ?? { version: i.version })
|
||||
}
|
||||
return (
|
||||
<ul className="space-y-1" data-ledger-issued-list="">
|
||||
{issued.map((i) => {
|
||||
const st = issuedState(i)
|
||||
return (
|
||||
<li key={i.run_id} className="flex flex-wrap items-center gap-1.5 text-2xs" data-ledger-issued={i.run_id}
|
||||
data-ledger-issued-state={st.code}>
|
||||
<StatusBadge status={st.code} size={11} decorative />
|
||||
<span className="font-medium">{LEDGER_UI.issued.title(i.auto)}</span>
|
||||
<span className="text-dim">{nameOf(i)}</span>
|
||||
<span data-ledger-tier="">{st.text}</span>
|
||||
{builder
|
||||
? <Link to={`/runs/${i.run_id}`} className="text-[var(--accent)] hover:underline" data-ledger-issued-open="">{LEDGER_UI.issued.open}</Link>
|
||||
: <span className="text-faint">{LEDGER_UI.issued.userNote}</span>}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
/** 一份相关口径对应的那一项「看差多少」:同一条查询(指标的几条查询之一)、同一份口径 */
|
||||
export function reconcileActionFor(data: RunLedger | undefined, queries: string[], caliber: string | undefined) {
|
||||
return (data?.actions ?? []).find((a): a is LedgerReconcileAction => a.kind === 'reconcile' && a.caliber === caliber
|
||||
&& queries.includes(a.query))
|
||||
}
|
||||
|
||||
/** 差在哪:explain_sql_diff 的条目(SQL 片段服务端已按遮罩换过);兜底说明另写一句 */
|
||||
export function DiffList({ related }: { related?: LedgerRelated }) {
|
||||
const items = (related?.diff?.items ?? []).filter((i) => i.text)
|
||||
if (!items.length && !related?.diff?.fallback) {
|
||||
return <p className="text-2xs text-faint">{related?.reason || LEDGER_UI.step.noDiff}</p>
|
||||
}
|
||||
return (
|
||||
<ul className="list-disc space-y-0.5 pl-4" data-ledger-diff="">
|
||||
{items.map((i, n) => <li key={n}>{i.text}</li>)}
|
||||
{related?.diff?.fallback && !items.length && <li className="text-faint">{LEDGER_UI.step.fallback}</li>}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
/** 面板里的一小节:同 EvidencePanel 的 Part */
|
||||
function Section({ title, children, ...rest }: { title: string; children: ReactNode } & Record<`data-${string}`, string>) {
|
||||
return (
|
||||
<div {...rest}>
|
||||
<div className="mb-1 text-2xs font-medium text-faint">{title}</div>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 证据面板出处链最上面的「口径」一步。只给数字、取值片段(单元格、指标):别的片段服务端本来就不标。正式运行、没有运行、
|
||||
* 不比对的运行不画这一步
|
||||
*/
|
||||
export function LedgerPart({ runId, runClass, report, segId, segText, segLedger, readOnly = false }: {
|
||||
runId?: string; runClass?: string; report?: string; segId: string
|
||||
/** 正文里这个数写的是什么:对账结果里对照着说原答案当时的值 */
|
||||
segText?: string
|
||||
/** 片段接口的 ledger 键(按运行取的那一份还没回来时先用它) */
|
||||
segLedger?: SegmentLedger | null
|
||||
readOnly?: boolean
|
||||
}) {
|
||||
const on = ledgerEnabled(runId, runClass)
|
||||
const slot = useRunLedger(runId, on)
|
||||
const [dialog, setDialog] = useState<{ action: LedgerReconcileAction; related?: LedgerRelated } | null>(null)
|
||||
if (!on || !runId) return null
|
||||
const data = slot?.data
|
||||
const label = labelOf(data, report, segId, segLedger)
|
||||
if (!label) {
|
||||
// 还在比对、等满了、读不出:照实说;比对过了这个片段没有标签(不是数)、不比对的、第一次还没取回来的:这一步不画
|
||||
if (!slot || (slot.status === 'loading' && !data)) return null
|
||||
if (ledgerErrorShown(slot) || (slot.status !== 'error' && data?.state === 'pending')) {
|
||||
return <Section title={LEDGER_UI.step.title} data-ev-ledger="waiting"><LedgerWaiting slot={slot} runId={runId} /></Section>
|
||||
}
|
||||
if (data?.state === 'unreadable') {
|
||||
return (
|
||||
<Section title={LEDGER_UI.step.title} data-ev-ledger="unreadable">
|
||||
<p className="text-dim">{LEDGER_UI.step.unreadable(data.reason ?? '')}</p>
|
||||
</Section>
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
const queries = label.queries ?? (label.query ? [label.query] : [])
|
||||
const issue = label.kind === 'caliber' && data?.whole?.equivalent
|
||||
? (data.actions ?? []).find((a): a is LedgerIssueAction => a.kind === 'issue' && a.workflow_id === label.workflow_id
|
||||
&& a.version === label.version)
|
||||
: undefined
|
||||
const how = label.how === 'composed' ? 'composed' : 'independent'
|
||||
return (
|
||||
<Section title={LEDGER_UI.step.title} data-ev-ledger={label.kind}>
|
||||
{label.kind === 'caliber' && (
|
||||
<div className="space-y-1">
|
||||
<p className="font-medium" data-ledger-caliber="">
|
||||
{LEDGER_TEXT.kind.caliber}{caliberOf(label)}
|
||||
<span className="chip ml-1.5" data-ledger-how={how}>{LEDGER_TEXT.how[how]}</span>
|
||||
</p>
|
||||
<p className="text-2xs text-dim">{LEDGER_UI.step.how[how]}</p>
|
||||
<div className="flex flex-wrap items-center gap-1 text-2xs">
|
||||
<span className="text-faint">{LEDGER_UI.step.params}</span>
|
||||
<BindingList binding={label.binding} />
|
||||
</div>
|
||||
{label.column && <p className="text-2xs text-faint">{LEDGER_UI.step.column(label.column)}</p>}
|
||||
{issue && <IssueControl runId={runId} action={issue} slot={slot} readOnly={readOnly} />}
|
||||
{data && issue && <IssuedList data={{ ...data, issued: (data.issued ?? []).filter((i) =>
|
||||
i.workflow_id === issue.workflow_id && i.version === issue.version) }} />}
|
||||
</div>
|
||||
)}
|
||||
{label.kind === 'related' && (
|
||||
<div className="space-y-2">
|
||||
<p className="text-2xs text-dim">{LEDGER_UI.step.relatedSub}{label.reason ? `:${label.reason}` : ''}</p>
|
||||
{(label.related ?? []).map((r, i) => {
|
||||
const action = reconcileActionFor(data, queries, r.caliber)
|
||||
// 指标的相关口径只写是哪一版,差在哪在它的各条查询上
|
||||
const where = r.diff ? r : (data?.queries ?? []).filter((q) => queries.includes(q.artifact))
|
||||
.flatMap((q) => q.candidates).find((c) => c.caliber === r.caliber && c.status === 'related')
|
||||
const done = (data?.reconciled ?? []).filter((x) => x.caliber === r.caliber && !!x.query && queries.includes(x.query))
|
||||
return (
|
||||
<div key={r.caliber ?? i} className="rounded border px-2 py-1.5" data-ledger-related={r.caliber ?? ''}>
|
||||
<p className="font-medium">{LEDGER_TEXT.kind.related}{caliberOf(r)}</p>
|
||||
<div className="mt-1 text-2xs" data-ledger-where="">
|
||||
<div className="text-faint">{LEDGER_UI.step.where}</div>
|
||||
<DiffList related={where} />
|
||||
</div>
|
||||
{action ? (
|
||||
<button type="button" className="btn btn-xs mt-1.5" disabled={readOnly}
|
||||
title={readOnly ? LEDGER_UI.reconcile.readOnly : undefined}
|
||||
onClick={() => setDialog({ action, related: where })} data-ledger-reconcile-open={r.caliber ?? ''}>
|
||||
<Scale size={11} aria-hidden /> {LEDGER_UI.reconcile.button}
|
||||
</button>
|
||||
) : r.comparable === false ? <p className="mt-1 text-2xs text-faint">{r.reason || LEDGER_UI.step.notComparable}</p> : null}
|
||||
{!!done.length && <ReconcileResults runId={runId} items={done} data={data} original={segText} />}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
{label.kind === 'reasoning' && (
|
||||
<p data-ledger-reasoning="">{LEDGER_TEXT.kind.reasoning}<span className="text-dim"> · {LEDGER_UI.step.reasoningSub}</span></p>
|
||||
)}
|
||||
{label.kind === 'unknown' && (
|
||||
<p className="text-dim" data-ledger-unknown="">{LEDGER_TEXT.kind.unknown}{label.reason ? `:${label.reason}` : ''}</p>
|
||||
)}
|
||||
{data && <LedgerFootnote data={data} />}
|
||||
{dialog && data && (
|
||||
<ReconcileDialog runId={runId} action={dialog.action} related={dialog.related} onClose={() => setDialog(null)} />
|
||||
)}
|
||||
</Section>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { BookMarked, Scale } from 'lucide-react'
|
||||
import { LEDGER_TEXT } from '../lib/terms'
|
||||
import { LEDGER_UI } from '../lib/termsLedger'
|
||||
import { useRunLedger } from '../store/ledger'
|
||||
import type { LedgerIssueAction, LedgerReconcileAction, LedgerRelated, RunLedger } from '../typesLedger'
|
||||
import { IssueControl, IssuedList, LedgerFootnote, LedgerWaiting, caliberOf, ledgerEnabled, ledgerErrorShown } from './LedgerStep'
|
||||
import { ReconcileDialog, ReconcileResults } from './LedgerReconcile'
|
||||
|
||||
/**
|
||||
* 问数据轮次底部的口径账本(docs/design/next-round/02-口径账本.md 做法 11):一行摘要(「3 个数来自口径「周销售」v3,
|
||||
* 1 个数是这次的推理」)和对应的动作——整题等价时按口径正式出具(能自动的写正在自动出具),有相关口径时对账;
|
||||
* 正式出具的结果(自动的、一键的)挂在轮次下面、点开去运行;对过的账一张结果卡片,三个数各自点得开。
|
||||
*
|
||||
* 比对在封存之后做:轮次刚交付时写「正在比对口径」,store/ledger 按退避再取,等满了还没结论照实说、给「再取一次」。
|
||||
* 不靠实时流(ledger.matched 追加时这次运行的事件总线已经关了)。界面层不放逻辑:每个数的标签、整题判断、能不能出具、
|
||||
* 能不能自动、不能的原因都是服务端给的;摘要只是把主报告里服务端标好的数按标签数一数、写出是哪份口径。
|
||||
*/
|
||||
|
||||
/** 摘要的几个数:主报告里服务端标好的标签按种类数,来自口径的写出是哪几份口径(按出现的先后) */
|
||||
function summaryOf(data: RunLedger) {
|
||||
const labels = Object.values((data.primary ? data.reports?.[data.primary]?.labels : undefined) ?? {})
|
||||
const counts = { caliber: 0, related: 0, reasoning: 0, unknown: 0 }
|
||||
const calibers: string[] = []
|
||||
for (const l of labels) {
|
||||
counts[l.kind] += 1
|
||||
if (l.kind === 'caliber') {
|
||||
const name = caliberOf(l)
|
||||
if (!calibers.includes(name)) calibers.push(name)
|
||||
}
|
||||
}
|
||||
return { ...counts, calibers, total: labels.length }
|
||||
}
|
||||
|
||||
/** 对账的入口:每条查询、每份相关口径一项;差在哪取那条查询上的比对 */
|
||||
function reconcileEntries(data: RunLedger): { action: LedgerReconcileAction; related?: LedgerRelated }[] {
|
||||
return (data.actions ?? []).filter((a): a is LedgerReconcileAction => a.kind === 'reconcile').map((action) => ({
|
||||
action,
|
||||
related: (data.queries ?? []).find((q) => q.artifact === action.query)?.candidates
|
||||
.find((c) => c.caliber === action.caliber && c.status === 'related'),
|
||||
}))
|
||||
}
|
||||
|
||||
export function LedgerTurn({ runId, runClass, done, readOnly = false }: {
|
||||
runId: string | undefined
|
||||
runClass?: string
|
||||
/** 这一轮交付了(运行成功结束):之前不取,运行没封存、没比对 */
|
||||
done: boolean
|
||||
/** 对话在回收站里:只看,不出具、不对账 */
|
||||
readOnly?: boolean
|
||||
}) {
|
||||
const on = done && ledgerEnabled(runId, runClass)
|
||||
const slot = useRunLedger(runId, on)
|
||||
const [dialog, setDialog] = useState<{ action: LedgerReconcileAction; related?: LedgerRelated } | null>(null)
|
||||
const data = slot?.data
|
||||
const summary = useMemo(() => (data?.state === 'matched' ? summaryOf(data) : null), [data])
|
||||
if (!on || !runId || !slot) return null
|
||||
if (ledgerErrorShown(slot) || (slot.status !== 'error' && data?.state === 'pending')) {
|
||||
return (
|
||||
<div className="w-full basis-full text-2xs" data-ledger-turn="waiting">
|
||||
<LedgerWaiting slot={slot} runId={runId} compact />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (!data || data.state !== 'matched' || !summary) return null
|
||||
const issue = (data.actions ?? []).find((a): a is LedgerIssueAction => a.kind === 'issue')
|
||||
const reconciles = reconcileEntries(data)
|
||||
const issued = data.issued ?? []
|
||||
const reconciled = data.reconciled ?? []
|
||||
if (!summary.total && !issued.length && !reconciled.length) return null
|
||||
const whole = data.whole
|
||||
return (
|
||||
<div className="w-full basis-full space-y-1.5 rounded-md border px-2.5 py-2 text-xs" data-ledger-turn="matched">
|
||||
{summary.total > 0 && (
|
||||
<p className="flex flex-wrap items-center gap-1.5" data-ledger-summary="">
|
||||
<BookMarked size={12} className="shrink-0 text-faint" aria-hidden />
|
||||
<span className="text-2xs font-medium text-faint">{LEDGER_UI.turn.label}</span>
|
||||
<span>{LEDGER_UI.turn.summary(summary)}</span>
|
||||
</p>
|
||||
)}
|
||||
{whole?.equivalent && (
|
||||
<p className="text-2xs text-dim" data-ledger-whole="">
|
||||
{LEDGER_UI.turn.whole(caliberOf(whole), LEDGER_TEXT.how[whole.how === 'composed' ? 'composed' : 'independent'])}
|
||||
</p>
|
||||
)}
|
||||
{(issue || reconciles.length > 0) && (
|
||||
<div className="flex flex-wrap items-start gap-1.5" data-ledger-actions="">
|
||||
{issue && <IssueControl runId={runId} action={issue} slot={slot} readOnly={readOnly} />}
|
||||
{reconciles.map(({ action, related }) => (
|
||||
<button key={`${action.query}|${action.caliber}`} type="button" className="btn btn-xs" disabled={readOnly}
|
||||
title={readOnly ? LEDGER_UI.reconcile.readOnly : undefined}
|
||||
onClick={() => setDialog({ action, related })} data-ledger-turn-reconcile={action.caliber}>
|
||||
<Scale size={11} aria-hidden /> {LEDGER_UI.reconcile.turnButton(caliberOf(action))}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<IssuedList data={data} />
|
||||
{reconciled.length > 0 && <ReconcileResults runId={runId} items={reconciled} data={data} />}
|
||||
<LedgerFootnote data={data} />
|
||||
{dialog && <ReconcileDialog runId={runId} action={dialog.action} related={dialog.related} onClose={() => setDialog(null)} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user