审批质询(前端):报告上的入口、问答串与提问框、回答渲染,审批卡与运行详情的挂法,check-inquiry 换成真检查

- run/Inquiry.tsx:质询的 store(列表按运行与报告缓存、一次运行同一时刻只有一条在回答、请求不随组件卸载作废)、ReportInquiry(问答按句子归拢、同一句下按时间排,进入时按列表接口恢复;就整份报告提问;正在回答的那一条)、提问框(显示被问的那句或划选的原文,Esc 关掉、焦点回到点开它的「质询」,Ctrl+回车提交,字数上限 1000、原文 500,409/422 照服务端的中文说明显示、问题留着)、回答渲染(数字的出处同报告:质询自己的查询点开按质询工件接口取快照、标出那一格;引用报告已有出处的交给页面在报告里打开证据面板;裸数字点状线)、查询列表(SQL、行数、截断、遮罩)、recorded 为假时醒目说明
- EvidenceDoc.tsx 只加入口:父组件传入 inquiry(是否可问与 approval_id)。每句结论后的「质询」平时宽 0、透明,悬停或焦点在这一句上时出现,只在焦点在这一句上时进 Tab 顺序,整份报告不多出一串 Tab 位;没有可点内容的句子,「质询」本身进方向键的顺序;触屏一直显示。划选原文浮出「质询」,原文按选区克隆、去掉徽标和小标签,证据面板里的字不算
- ApprovalReport.tsx:报告核对概况下面挂问答串,逐段证据带上入口;回答里引用报告已有出处时展开逐段证据、在那一处打开证据面板
- 文件所有权之外的两处最小改动(交付说明里写明):RunPanel.tsx 把待处理审批的 id 传给 ApprovalReports,回答期间批准 / 驳回停用并说明原因;pages/runs/EvidencePane.tsx 对封存了的探索运行给入口并挂问答串(判断用 Inquiry.tsx 导出的 inquiryOpen),正式运行不给
- 预览用例 dev/fixtures/inquiry.tsx 与夹具 inquiry.json(报告文档、回答文档由后端 build_catalog + compose_doc 真实组装,虚构数据);check-inquiry.mjs 用 page.route 假接口逐项断言入口、请求体、回答期间停用、问答串恢复、数字取质询工件、报告出处、recorded 为假、409/422、已处理审批与正式运行封存后无入口、390px 与深浅色
This commit is contained in:
ThearchyHelios
2026-10-05 19:13:07 +08:00
parent 23cdc986fc
commit 3dd5216af2
9 changed files with 6471 additions and 42 deletions
File diff suppressed because it is too large Load Diff
+84
View File
@@ -0,0 +1,84 @@
import type { JSX } from 'react'
import { EvidencePane } from '../../pages/runs/EvidencePane'
import { ApprovalCard } from '../../run/RunPanel'
import type { Approval, ApprovalReport, Run } from '../../types'
import fx from './inquiry.json'
/**
* 审批质询的预览用例,scripts/check-inquiry.mjs 按它们检查。夹具在 inquiry.json(检查脚本读同一份):报告文档和回答文档
* 是后端 build_catalog + compose_doc 真实组装的,超市、片区、金额都是虚构的。
*
* 审批卡(运行停在人工审批上,审的是报告「写月报」):
* - ?syn=inquiry-approval:探索运行。核对概况下面是问答串(进入时按列表接口恢复)和「就整份报告提问」;展开逐段证据后,
* 每句结论悬停 / 焦点在它上面时出现「质询」,划选原文时浮出「质询」
* - ?syn=inquiry-approval-formal:正式运行的审批,同样能问(审批阶段还没封存)
* - ?syn=inquiry-approval-dense:同第一个,摆在 360px 的右栏里
* - ?syn=inquiry-approval-done:审批已经处理过(不是待处理的那张):只列问答,不给提问
*
* 运行详情的「证据」页签(pages/runs/EvidencePane,是否可问由它按运行状态判断):
* - ?syn=inquiry-run-explore:封存了的探索运行,报告上给入口,问答追加在封存之后
* - ?syn=inquiry-run-formal:封存了的正式运行,不给入口
*
* 列表、提问、质询工件、报告文档、证据图都由检查脚本用 page.route 伪造;直接打开时问答串照实说无法读取。
*/
const RUNS = fx.runs
const reports = fx.reports as unknown as Record<keyof typeof RUNS, ApprovalReport>
function approvalOf(id: string, runId: string, runClass: 'formal' | 'exploratory', report: ApprovalReport,
status = 'pending'): Approval {
return {
id, run_id: runId, node_id: 'review', mode: 'approve', title: '9 月月报可以发出吗?', status, response: {},
payload: { kind: 'human_node', message: '请审核 9 月月报。', reports: [report] },
created_at: new Date(Date.now() - 300_000).toISOString().replace('Z', ''),
workflow_name: '月报', node_label: '主管审批', run_class: runClass,
}
}
function Card({ approval, name, dense = false }: { approval: Approval; name: string; dense?: boolean }) {
return (
<div className={dense ? 'w-[360px] max-w-full border-r bg-panel' : 'mx-auto max-w-3xl p-4'} data-fixture={name}>
<div className="overflow-hidden rounded-lg border" style={{ borderColor: 'var(--warn)' }}>
<ApprovalCard approval={approval} onResolved={() => undefined} />
</div>
</div>
)
}
/** 运行详情:一次已经结束、封存了的运行 */
function runOf(id: string, runClass: 'formal' | 'exploratory'): Run {
return {
id, workflow_id: null, workflow_name: '月报', status: 'succeeded', input: { month: '2026-09' }, output: {}, error: null,
usage: { input_tokens: 0, output_tokens: 0, cost_usd: 0 } as Run['usage'], run_class: runClass, version: runClass === 'formal' ? 3 : null,
manifest_hash: 'f'.repeat(64), manifest_seq: 30, created_at: '2026-10-05T06:00:00', started_at: '2026-10-05T06:00:00',
finished_at: '2026-10-05T06:40:00',
}
}
const LABELS: Record<string, string> = { write: '写月报', review: '主管审批' }
function RunEvidence({ run, name }: { run: Run; name: string }) {
return (
<div className="h-screen min-w-0" data-fixture={name}>
<EvidencePane run={run} output={null} labelOf={(id) => (id ? LABELS[id] : undefined)} refreshKey="0" />
</div>
)
}
export const cases: Record<string, () => JSX.Element> = {
'inquiry-approval': () => (
<Card name="inquiry-approval" approval={approvalOf(fx.approval_id, RUNS.approve, 'exploratory', reports.approve)} />
),
'inquiry-approval-formal': () => (
<Card name="inquiry-approval-formal" approval={approvalOf(fx.formal_approval_id, RUNS.formal, 'formal', reports.formal)} />
),
'inquiry-approval-dense': () => (
<Card name="inquiry-approval-dense" dense approval={approvalOf(fx.approval_id, RUNS.approve, 'exploratory', reports.approve)} />
),
'inquiry-approval-done': () => (
<Card name="inquiry-approval-done"
approval={approvalOf(fx.approval_id, RUNS.approve, 'exploratory', reports.approve, 'approved')} />
),
'inquiry-run-explore': () => <RunEvidence name="inquiry-run-explore" run={runOf(RUNS.explore, 'exploratory')} />,
'inquiry-run-formal': () => <RunEvidence name="inquiry-run-formal" run={runOf(RUNS.sealed, 'formal')} />,
}
+68
View File
@@ -3305,6 +3305,74 @@ export const INQUIRY_TEXT = {
error: (e: string) => `原因:${e}`,
model: (m: string) => `核查模型:${m}`,
},
// ---- 报告上的入口(EvidenceDoc):句末的「质询」、划选原文时浮出的「质询」 ----
ask: '质询',
askUnit: (text: string) => `质询这句:${text}`,
askSelection: '就选中的原文质询',
/** 读屏摘要里接在按键说明后面 */
keysHint: '每句结论后有「质询」按钮:方向键走到这一句,再按 Tab 到达;没有可点内容的句子,方向键直接走到它的「质询」',
// ---- 问答串(ReportInquiry) ----
headCount: (n: number) => `${formatNumber(n)} 次`,
askReport: '就整份报告提问',
intro: '展开逐段证据后,悬停一句结论或划选一段原文,点「质询」即可就该处提问',
introRun: '悬停一句结论或划选一段原文,点「质询」即可就该处提问',
wholeReport: '整份报告',
/** 报告文档取不到,写不出问的是哪一句 */
unitMissing: '报告中的一句',
loadFailed: '无法读取之前的质询记录',
retry: '重试',
q: '问',
a: '答',
unsigned: '未署名',
entryLabel: (question: string) => `质询:${question}`,
quoteLine: (quote: string) => `原文「${quote}」`,
notRecorded: '这次问答没有写入运行记录',
notRecordedHint: '它只显示在当前页面,刷新后不再显示,也不会随运行封存',
/** 屏幕阅读器:回答回来了 */
arrived: '已收到回答',
// ---- 提问框 ----
target: { unit: '就这句提问', quote: '就选中的原文提问', report: '就整份报告提问' } as Record<'unit' | 'quote' | 'report', string>,
questionLabel: '问题',
placeholder: '例如:这个数是否扣除了退款?',
submit: '提问',
cancel: '取消',
chars: (n: number, max: number) => `${formatNumber(n)}/${formatNumber(max)}`,
keys: 'Ctrl + 回车提交 · Esc 关闭',
empty: '请填写问题',
tooLong: (max: number) => `问题最多 ${formatNumber(max)} 字,请删减后再提交`,
quoteCut: (max: number) => `选中的原文超过 ${formatNumber(max)} 字,只提交前 ${formatNumber(max)} 字`,
hint: '由核查模型只读查询数据作答,问答写入运行记录,不改动报告',
hintPostSeal: '运行已封存:问答追加在封存之后,不改动报告',
busyOther: '这次运行另有一条质询正在回答,回答完再提问',
submitFailed: '提问失败',
// ---- 回答中:审批卡的批准 / 驳回暂停(服务端这时也会拒绝) ----
pending: '正在查询与回答,通常需要几十秒',
waited: (seconds: number) => `已等待 ${formatNumber(seconds)} 秒`,
decideBlocked: '正在回答质询,回答完再批准或驳回',
// ---- 回答 ----
noAnswer: '没有给出回答',
answerIncomplete: (limit: string) => `${limit}:回答依据已查到的结果,可能不完整`,
answeredBy: (model: string | null) => `回答由核查模型${model ? `「${model}」` : ''}给出,并非系统核对`,
cost: (usd: string) => `花费 ${usd}`,
/** 回答里有出处的数字:读屏听到「数字,出处:查询 Q1 · 第 1 行 · amount」,点一下展开那次查询的结果 */
cited: (text: string, source: string) => `${text},出处:${source},查看这次查询的结果`,
citedReport: (text: string, source: string) => `${text},出处:${source},在报告中查看证据`,
/** 引用的是报告已有的出处,报告里却找不到引用它的地方:照回答文档记下的出处说一句 */
reportSource: (source: string) => `出处:${source}(报告已有的出处,报告中没有引用它的地方)`,
queriesHead: (n: number) => `核查模型执行的查询(${formatNumber(n)} 次)`,
noQueries: '核查模型没有执行查询',
rows: (n: number) => `${formatNumber(n)} 行`,
truncated: '结果已截断,数据库中还有更多数据',
showResult: '查看结果',
hideResult: '收起结果',
snapshotLoading: '正在读取查询结果…',
snapshotFailed: '无法读取这次查询的结果',
masked: (columns: string) => `${columns} 在数据源中设置了遮罩,不显示原值`,
}
/**
+14 -4
View File
@@ -16,6 +16,7 @@ import { EVIDENCE_AUDIT_TEXT as T, UPGRADE_TEXT } from '../../lib/terms'
import { IssuanceBanner } from '../../run/AssistantStream'
import { EvidenceDoc, type EvidenceDocHandle } from '../../run/EvidenceDoc'
import { EvidenceGuessView } from '../../run/EvidenceGuess'
import { NO_APPROVAL, ReportInquiry, inquiryOpen, locateIn } from '../../run/Inquiry'
import { Markdown, inlineStyles } from '../../run/Markdown'
import { useEvidence } from '../../store/evidence'
import { useCatalog } from '../../store/catalog'
@@ -124,6 +125,8 @@ export function EvidencePane({ run, output, labelOf, refreshKey }: {
const mode = graph?.mode ?? audit.data?.mode
const seal = audit.data?.seal ?? graph?.seal
// 审批质询:封存了的探索运行可以接着就报告提问(追加在封存之后);正式运行封存后不再开放
const asking = inquiryOpen(run)
if (!graph && graphSlot?.status !== 'error') {
return (
@@ -163,10 +166,17 @@ export function EvidencePane({ run, output, labelOf, refreshKey }: {
</span>
</header>
{doc ? (
<EvidenceDoc ref={(h) => { handles.current.set(r.node_id!, h) }} doc={doc} artifact={r.doc_artifact}
runId={run.id} runClass={run.run_class} label={r.fields?.join('、') || r.node_id} tally
tier={typeof output?._issuance?.tier === 'string' ? output._issuance.tier : undefined}
panel={wide ? 'dock' : 'auto'} dock={dock} onView={(o) => onView(r.node_id!, o)} />
<>
<EvidenceDoc ref={(h) => { handles.current.set(r.node_id!, h) }} doc={doc} artifact={r.doc_artifact}
runId={run.id} runClass={run.run_class} label={r.fields?.join('、') || r.node_id} tally
tier={typeof output?._issuance?.tier === 'string' ? output._issuance.tier : undefined}
panel={wide ? 'dock' : 'auto'} dock={dock} onView={(o) => onView(r.node_id!, o)}
inquiry={asking ? NO_APPROVAL : null} />
{asking && (
<ReportInquiry runId={run.id} report={r.node_id!} docArtifact={r.doc_artifact} access={NO_APPROVAL} postSeal
onLocate={(seg) => locateIn(handles.current.get(r.node_id!), doc, seg)} />
)}
</>
) : slot?.status === 'error' ? (
<p className="text-2xs" style={{ color: 'var(--st-failed)' }}>{T.docGone(labelOf(r.node_id) ?? r.node_id ?? '')}</p>
) : <Skeleton rows={4} height={14} />}
+43 -9
View File
@@ -1,11 +1,12 @@
import { useEffect, useId, useState } from 'react'
import { useCallback, useEffect, useId, useMemo, useRef, useState, type Ref } from 'react'
import { ChevronRight, ShieldCheck } from 'lucide-react'
import { EVIDENCE_STATE, claimCountsOf, claimProblems, statsTally } from '../lib/evidence'
import { formatNumber } from '../lib/format'
import { APPROVAL_TEXT, EVIDENCE_TEXT, JUDGE_TEXT, evidenceTally, runClassLabel } from '../lib/terms'
import { useEvidence } from '../store/evidence'
import type { ApprovalReport } from '../types'
import { EvidenceDoc } from './EvidenceDoc'
import type { ApprovalReport, EvidenceSegment } from '../types'
import { EvidenceDoc, type EvidenceDocHandle } from './EvidenceDoc'
import { ReportInquiry, reportSegmentFor, type InquiryAccess } from './Inquiry'
type RunClass = 'formal' | 'exploratory' | null | undefined
@@ -53,21 +54,46 @@ function claimsLine(report: ApprovalReport, runClass: RunClass): { text: string;
*
* 真实运行里报告有一句结论和证据相矛盾,审批人批准时完全不知道:卡上只有正文
*/
export function ApprovalReports({ reports, runId, runClass }: {
export function ApprovalReports({ reports, runId, runClass, approvalId }: {
reports: ApprovalReport[]
runId: string
runClass: RunClass
/**
* 运行停在的这张审批(待处理的才给)。有它,审批人可以就报告提出质询:报告上的句子、划选的原文挂「质询」,
* 核对概况下面是问答串和提问框(run/Inquiry.tsx)
*/
approvalId?: string
}) {
const access = useMemo<InquiryAccess | null>(() => (approvalId ? { approvalId } : null), [approvalId])
if (!reports.length) return null
return (
<>
{reports.map((r) => <ReportCheck key={r.node_id} report={r} runId={runId} runClass={runClass} />)}
{reports.map((r) => <ReportCheck key={r.node_id} report={r} runId={runId} runClass={runClass} access={access} />)}
</>
)
}
function ReportCheck({ report, runId, runClass }: { report: ApprovalReport; runId: string; runClass: RunClass }) {
function ReportCheck({ report, runId, runClass, access }: {
report: ApprovalReport; runId: string; runClass: RunClass; access: InquiryAccess | null
}) {
const [open, setOpen] = useState(false)
// 回答里引用了报告已有的出处:在逐段证据里打开那一处的证据面板。逐段证据收着的先展开,挂上之后再打开
const docRef = useRef<EvidenceDocHandle>(null)
const opening = useRef<string | null>(null)
const docSlot = useEvidence((s) => (report.doc_artifact ? s.docs[report.doc_artifact] : undefined))
const locate = useCallback((seg: EvidenceSegment) => {
const id = reportSegmentFor(docSlot?.status === 'ok' ? docSlot.data : null, seg)
if (!id) return false
if (open && docRef.current) return docRef.current.open(id, { focus: true })
opening.current = id
setOpen(true)
return true
}, [docSlot, open])
useEffect(() => {
if (!open || !opening.current || !docRef.current) return
docRef.current.open(opening.current, { focus: true })
opening.current = null
})
const docId = useId()
const counts = statsTally(report.stats)
const suspect = typeof report.stats?.unknown_entities === 'number' ? report.stats.unknown_entities : 0
@@ -105,17 +131,24 @@ function ReportCheck({ report, runId, runClass }: { report: ApprovalReport; runI
</button>
{open && (
<div id={docId} className="mt-1.5">
<ReportDoc artifact={report.doc_artifact} runId={runId} runClass={runClass} />
<ReportDoc artifact={report.doc_artifact} runId={runId} runClass={runClass} inquiry={access} handle={docRef} />
</div>
)}
</>
)}
{/* 审批质询:问答串(进入审批卡时按列表接口恢复)、提问框和「就整份报告提问」 */}
<ReportInquiry runId={runId} report={report.node_id} docArtifact={report.doc_artifact} access={access} onLocate={locate} />
</div>
)
}
/** 报告文档:和成果里同一份可逐段点开的文档,自带证据条(定位下一处、违规清单),点开的证据面板在卡内展开 */
function ReportDoc({ artifact, runId, runClass }: { artifact: string; runId: string; runClass: RunClass }) {
function ReportDoc({ artifact, runId, runClass, inquiry, handle }: {
artifact: string; runId: string; runClass: RunClass
/** 审批质询:句子、划选的原文上挂「质询」 */
inquiry: InquiryAccess | null
handle: Ref<EvidenceDocHandle>
}) {
const slot = useEvidence((s) => s.docs[artifact])
const loadDoc = useEvidence((s) => s.loadDoc)
useEffect(() => { void loadDoc(artifact) }, [artifact, loadDoc])
@@ -124,7 +157,8 @@ function ReportDoc({ artifact, runId, runClass }: { artifact: string; runId: str
if (!doc) return <div className="text-2xs text-faint">{APPROVAL_TEXT.docLoading}</div>
return (
<div className="max-h-96 overflow-y-auto rounded border p-2" data-approval-evidence-doc="">
<EvidenceDoc doc={doc} artifact={artifact} runId={runId} dense tally runClass={runClass ?? undefined} />
<EvidenceDoc ref={handle} doc={doc} artifact={artifact} runId={runId} dense tally runClass={runClass ?? undefined}
inquiry={inquiry} />
</div>
)
}
+177 -18
View File
@@ -1,10 +1,10 @@
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,
type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, type RefObject,
} from 'react'
import { createPortal } from 'react-dom'
import { ListChecks } from 'lucide-react'
import { ListChecks, MessageCircleQuestion } from 'lucide-react'
import clsx from 'clsx'
import { rovingTarget } from '../components/ui'
import {
@@ -13,10 +13,11 @@ import {
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 { EVIDENCE_STATE_LABEL, EVIDENCE_TEXT, INQUIRY_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 { askAbout, type InquiryAccess } from './Inquiry'
import { BlockView, Markdown, StyledSlice, inlineStyles, numericColumns, type InlineStyles } from './Markdown'
/**
@@ -54,6 +55,16 @@ const TEXT_CAP = 3000
/** 句末徽标在键盘顺序里的键:片段 id 是 s0…,不会撞 */
const badgeKey = (unit: string) => `claim:${unit}`
const unitOfKey = (key: string) => (key.startsWith('claim:') ? key.slice(6) : null)
/**
* 审批质询的「质询」在键盘顺序里的键。只有没有可点片段、也没有徽标的句子,「质询」本身进键盘顺序(方向键走得到这一句);
* 别的句子的「质询」跟在这一句后面,焦点在这一句上时按 Tab 到达,不多占键盘顺序
*/
const askKey = (unit: string) => `ask:${unit}`
const unitOfAsk = (key: string) => (key.startsWith('ask:') ? key.slice(4) : null)
/** 哪些句子挂「质询」:结论句(老文档没有 kind 的按有字的句子),标题、代码块里的不挂 */
const askable = (unit: EvidenceUnit, block: EvidenceBlock) =>
(unit.kind ? unit.kind === 'claim' : !!unitText(unit).trim()) && !['heading', 'code', 'hr'].includes(block.type)
interface Badge { unit: EvidenceUnit; block: number; verdict: EvidenceVerdict; state: EvidenceStateCode }
@@ -64,6 +75,8 @@ interface Model {
seg: Map<string, { seg: EvidenceSegment; unit: EvidenceUnit; block: number }>
/** 句末徽标:键是 claim:<unit>,带着这一句眼下的判定 */
badge: Map<string, Badge>
/** 进了键盘顺序的「质询」(审批质询):键是 ask:<unit> */
ask: Map<string, { unit: EvidenceUnit; block: number }>
/** 每个有可点片段的句子里第一个可点片段,按正文顺序(↑/↓ 用) */
unitFirst: string[]
unitOf: Map<string, number>
@@ -82,11 +95,12 @@ interface Model {
grid: Map<string, string>
}
function buildModel(blocks: EvidenceBlock[], overlay?: Record<string, EvidenceVerdict>): Model {
function buildModel(blocks: EvidenceBlock[], overlay?: Record<string, EvidenceVerdict>, ask = false): Model {
const order: string[] = []
const index = new Map<string, number>()
const seg = new Map<string, { seg: EvidenceSegment; unit: EvidenceUnit; block: number }>()
const badge = new Map<string, Badge>()
const asks = new Map<string, { unit: EvidenceUnit; block: number }>()
const unitFirst: string[] = []
const unitOf = new Map<string, number>()
const tiered: [string, 1 | 2][] = []
@@ -128,18 +142,27 @@ function buildModel(blocks: EvidenceBlock[], overlay?: Record<string, EvidenceVe
const t = alertTier(state)
if (t) tiered.push([key, t])
}
// 审批质询:一个可点的都没有的结论句,「质询」本身进键盘顺序,不然键盘用户问不了这一句
if (ask && first && askable(unit, block)) {
const key = askKey(unit.id)
asks.set(key, { unit, block: b })
index.set(key, order.length)
order.push(key)
unitFirst.push(key)
unitOf.set(key, unitFirst.length - 1)
}
}
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 }
return { order, index, seg, badge, ask: asks, 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
(key ? model.seg.get(key)?.block ?? model.badge.get(key)?.block ?? model.ask.get(key)?.block : undefined) ?? -1
/**
* ↑/↓ 该去哪。表格里按列走:下一行同一列(那一格空着就找这一行离它最近的一格);
@@ -217,13 +240,21 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
* 数字的出处都对得上,档位却因为别的原因(比如查询没通过 SQL 检查)降了
*/
tier?: string
/**
* 审批质询(期 4):给了就在每句结论后面挂「质询」(悬停、焦点在这一句上时出现),划选原文时浮出「质询」,点了打开
* 这份报告的提问框。能不能问由页面按运行状态定(审批卡:运行停在审批上;运行详情:封存了的探索运行),approvalId 是
* 运行停在的那张审批。给了它,页面上要同时挂这份报告的 ReportInquiry(问答串和提问框在那里)
*/
inquiry?: InquiryAccess | null
}>(function EvidenceDoc({ doc, artifact, runId, dense = false, label, panel = 'auto', dock, onView, tally = false, runClass,
tier }, ref) {
tier, inquiry }, ref) {
const blocks = useMemo(() => doc.blocks ?? [], [doc])
const report = doc.node_id || undefined
// 质询要知道是哪次运行的哪份报告:两样缺一样就不挂入口
const ask = !!inquiry && !!runId && !!report
// 封存之后按需追加的判定:盖过文档里的「未裁判 · 按需」。只换句末徽标,封存的文档一个字不改
const overlay = useVerdicts(runId, report)
const model = useMemo(() => buildModel(blocks, overlay), [blocks, overlay])
const model = useMemo(() => buildModel(blocks, overlay, ask), [blocks, overlay, ask])
const counts = useMemo(() => docTally(doc), [doc])
const claims = useMemo(() => claimCounts(doc, overlay), [doc, overlay])
// 探索运行的报告(开了裁判的按需文档,或调用方说了是探索运行):证据图带着这次运行里之前按需判过的句子
@@ -272,7 +303,9 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
const segEl = useCallback((id: string) => {
const unit = unitOfKey(id)
const sel = unit ? `[data-ev-claim="${CSS.escape(unit)}"]` : `[data-seg="${CSS.escape(id)}"]`
const asked = unitOfAsk(id)
const sel = unit ? `[data-ev-claim="${CSS.escape(unit)}"]`
: asked ? `[data-inquiry-ask="${CSS.escape(asked)}"]` : `[data-seg="${CSS.escape(id)}"]`
return rootRef.current?.querySelector<HTMLElement>(sel) ?? null
}, [])
@@ -373,13 +406,29 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
},
}), [step, model, viewKey, current, focusSeg, openSeg, openViolations, doc])
// 事件委托:片段只带 data-seg、句末徽标只带 data-ev-claim,点击和按键都在容器上认
// 事件委托:片段只带 data-seg、句末徽标只带 data-ev-claim,点击和按键都在容器上认。「质询」只有进了键盘顺序的才算
// (跟在有可点片段的句子后面的那种不算:方向键在它上面不动,Tab 照常离开)
const segOf = (target: EventTarget | null): string | null => {
const el = (target as HTMLElement | null)?.closest?.<HTMLElement>('[data-seg], [data-ev-claim]')
const el = (target as HTMLElement | null)?.closest?.<HTMLElement>('[data-seg], [data-ev-claim], [data-inquiry-ask]')
if (!el || !rootRef.current?.contains(el)) return null
if (el.dataset.inquiryAsk) return model.index.has(askKey(el.dataset.inquiryAsk)) ? askKey(el.dataset.inquiryAsk) : null
return el.dataset.seg ?? (el.dataset.evClaim ? badgeKey(el.dataset.evClaim) : null)
}
/** 点了句末的「质询」:打开这份报告的提问框,问的是这一句 */
const askUnit = (el: HTMLElement) => {
const id = el.dataset.inquiryAsk
if (!id || !inquiry || !runId || !report) return
const unit = blocks.flatMap((b) => b.units ?? []).find((u) => u.id === id)
if (model.index.has(askKey(id))) setCurrent(askKey(id))
askAbout(runId, report, inquiry, { unit: id, quote: null, text: unit ? unitText(unit).trim() : '' }, el)
}
const onClick = (e: ReactMouseEvent) => {
const asking = (e.target as HTMLElement | null)?.closest?.<HTMLElement>('[data-inquiry-ask]')
if (asking && rootRef.current?.contains(asking)) {
e.preventDefault()
askUnit(asking)
return
}
const id = segOf(e.target)
if (!id) return
e.preventDefault()
@@ -445,8 +494,20 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
/>
)
// 审批质询:划选一段原文,浮出「质询」,问的是选中的这段(带上它起头的那一句)
const [pick, dropPick] = useSelectionPick(rootRef, ask)
const askPick = () => {
if (!pick || !inquiry || !runId || !report) return
// 浮出的按钮点完就没了:Esc 之后焦点回到那一句的「质询」
const back = pick.unit ? rootRef.current?.querySelector<HTMLElement>(`[data-inquiry-ask="${CSS.escape(pick.unit)}"]`) : null
document.getSelection()?.removeAllRanges()
dropPick()
askAbout(runId, report, inquiry, { unit: pick.unit, quote: pick.quote, text: pick.quote }, back)
}
// 画不了线的两种分开说:列表序号、代码块标签里的数字,和正文里根本没有对应字的(句末依据里的引用)
const summary = tallySummary({ ...counts, claims }, model.order.length > 0)
const summary = [tallySummary({ ...counts, claims }, model.order.length > 0), ask ? INQUIRY_TEXT.keysHint : '']
.filter(Boolean).join('。')
return (
<div ref={rootRef} data-evidence-doc="" className="min-w-0">
@@ -473,7 +534,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} />
panelId={panelId} model={model} ask={ask} />
{mode === 'inline' && b === activeBlock && panelNode}
</Fragment>
))}
@@ -494,10 +555,78 @@ export const EvidenceDoc = forwardRef<EvidenceDocHandle, {
)}
{mode === 'dock' && panelNode && dock && createPortal(panelNode, dock)}
{mode !== 'inline' && mode !== 'dock' && panelNode && createPortal(panelNode, document.body)}
{pick && createPortal(
// 按下时不抢焦点、不清掉选区:点下去才读选区
<button type="button" className="btn btn-xs fixed z-[70] shadow-elev-3" data-inquiry-selection=""
style={{ left: pick.left, top: pick.top }} aria-label={INQUIRY_TEXT.askSelection}
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => { e.stopPropagation(); askPick() }}>
<MessageCircleQuestion size={11} aria-hidden /> {INQUIRY_TEXT.ask}
</button>,
document.body,
)}
</div>
)
})
/** 划选的一段原文:要问的字、它起头的那一句、浮出按钮的位置(视口坐标) */
interface SelectionPick { quote: string; unit: string | null; left: number; top: number }
/** 浮出按钮的大致尺寸:放在选区末尾下方,贴边时收进视口,下方放不下时放到选区上方 */
const PICK_W = 64
const PICK_H = 26
/**
* 报告正文里划选了一段原文:返回要问的字和浮出按钮的位置,选区收起、移出正文时为 null。只认报告正文里的字——栏内展开
* 的证据面板也在这个容器里,那里的字不算。要问的字按选区克隆出来算,去掉句末徽标、「无证据」小标签、读屏文字和
* 「质询」本身(它们在 DOM 里,却不是报告的原文)
*/
function useSelectionPick(root: RefObject<HTMLElement | null>, enabled: boolean): [SelectionPick | null, () => void] {
const [pick, setPick] = useState<SelectionPick | null>(null)
useEffect(() => {
if (!enabled) { setPick(null); return }
let frame = 0
const read = () => {
frame = 0
const el = root.current
const sel = document.getSelection()
if (!el || !sel || sel.isCollapsed || !sel.rangeCount) { setPick(null); return }
const range = sel.getRangeAt(0)
const at = (n: Node) => (n instanceof Element ? n : n.parentElement)
const inside = (x: Element | null) => !!x && el.contains(x) && !!x.closest('[data-block]') && !x.closest('[data-evidence-panel]')
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())
const quote = (frag.textContent ?? '').replace(/\s+/g, ' ').trim()
if ([...quote].length < 2) { setPick(null); return }
const rects = range.getClientRects()
const last = rects[rects.length - 1] ?? range.getBoundingClientRect()
const first = rects[0] ?? last
const below = last.bottom + 6 + PICK_H <= window.innerHeight
const next: SelectionPick = {
quote, unit: start?.closest('[data-ev-unit]')?.getAttribute('data-ev-unit') ?? null,
left: Math.max(8, Math.min(last.right - PICK_W / 2, window.innerWidth - PICK_W - 8)),
top: below ? last.bottom + 6 : Math.max(8, first.top - PICK_H - 6),
}
// 滚动时每一帧都会量一次:没变就不换对象,整份报告不跟着重画
setPick((cur) => (cur && cur.quote === next.quote && cur.unit === next.unit && cur.left === next.left && cur.top === next.top
? cur : next))
}
// 选区一变、页面一滚(报告在可滚动的卡片里)都重新量:按钮跟着选区走
const schedule = () => { if (!frame) frame = requestAnimationFrame(read) }
document.addEventListener('selectionchange', schedule)
window.addEventListener('scroll', schedule, true)
window.addEventListener('resize', schedule)
return () => {
cancelAnimationFrame(frame)
document.removeEventListener('selectionchange', schedule)
window.removeEventListener('scroll', schedule, true)
window.removeEventListener('resize', schedule)
}
}, [root, enabled])
return [pick, useCallback(() => setPick(null), [])]
}
/** 定位到的片段描一下边(CSS 的 [data-flash]),同 AssistantStream 的 flash */
function flashOnce(el: HTMLElement) {
el.dataset.flash = 'focus'
@@ -590,12 +719,14 @@ 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 }: {
const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, active, panelId, model, ask }: {
block: EvidenceBlock
dense: boolean
doc: EvidenceDocData
/** 句末徽标从这里取:这一句眼下的判定(封存后追加的盖过文档里的) */
model: Model
/** 审批质询:结论句后面挂「质询」 */
ask: boolean
/**
* 长文档:块上加 content-visibility,屏幕外的块不排版不绘制。短文档不加:它自带 paint
* containment,贴着块边的片段的焦点描边会被裁掉一截,而短文档本来就不慢
@@ -614,7 +745,8 @@ 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))}
ask={ask && askable(u, block) ? (model.ask.has(askKey(u.id)) ? 'roving' : 'follow') : undefined} />
)
switch (block.type) {
@@ -684,9 +816,14 @@ const DocBlock = memo(function DocBlock({ block, dense, doc, visual, current, ac
* 被切坏),有状态的片段包成按钮。含无证据片段的句子末尾挂「? 无证据」;有判定的结论句末尾挂
* 一枚徽标(也是按钮),证据相矛盾(和旧的证据不支持)的整句铺浅底——字本身不画线
*/
function UnitView({ unit, doc, current, active, panelId, code, tag, badge }: {
function UnitView({ unit, doc, current, active, panelId, code, tag, badge, ask }: {
unit: EvidenceUnit; doc: EvidenceDocData; current: string | null; active: string | null; panelId: string
code?: boolean; tag: boolean; badge?: Badge
/**
* 审批质询的「质询」:roving 是这一句一个可点的都没有,「质询」本身进键盘顺序;follow 跟在这一句后面,焦点在这一句上时
* 按 Tab 到达
*/
ask?: 'roving' | 'follow'
}) {
const segs = (unit.segments ?? []).filter((s) => s.kind !== 'structural')
const text = segs.map((s) => s.text).join('')
@@ -727,7 +864,7 @@ function UnitView({ unit, doc, current, active, panelId, code, tag, badge }: {
const key = badge ? badgeKey(unit.id) : ''
const meta = badge ? EVIDENCE_STATE[badge.state] : null
const open = !!badge && active === key
return (
const body = (
<>
{conflicting(badge?.state)
// 证据相矛盾(和旧的证据不支持):整句铺一层浅底(折行时每行各自带圆角),字色、字重都不动,也不画线
@@ -762,6 +899,28 @@ function UnitView({ unit, doc, current, active, panelId, code, tag, badge }: {
)}
</>
)
if (!ask) return body
// 「质询」跟在句末:平时不占地方(宽度为 0、透明),悬停这一句、焦点落在这一句上时出现;触屏没有悬停,一直显示。
// 跟随的那种只在焦点在这一句上时进 Tab 顺序,整份报告不因此多出一串 Tab 位
const here = current != null && (current === key || current === askKey(unit.id)
|| (unit.segments ?? []).some((sg) => sg.id === current))
return (
<span className="group/evu" data-ev-unit={unit.id}>
{body}
<button type="button" data-inquiry-ask={unit.id} tabIndex={here ? 0 : -1}
aria-label={INQUIRY_TEXT.askUnit(unitText(unit).trim())}
className={'group/ask inline-flex w-0 select-none overflow-hidden align-middle opacity-0 outline-none focus:w-auto focus:opacity-100 '
+ 'group-hover/evu:w-auto group-hover/evu:opacity-100 group-focus-within/evu:w-auto group-focus-within/evu:opacity-100 '
+ 'pointer-coarse:w-auto pointer-coarse:opacity-100'}>
<span aria-hidden="true"
className={'my-px ml-1 inline-flex items-center gap-0.5 whitespace-nowrap rounded border px-1 text-[10px] font-medium leading-[1.4] '
+ 'text-[color:var(--accent)] hover:bg-hover group-focus-visible/ask:outline group-focus-visible/ask:outline-2 '
+ 'group-focus-visible/ask:outline-offset-1 group-focus-visible/ask:outline-[color:var(--accent)]'}>
<MessageCircleQuestion size={10} aria-hidden /> {INQUIRY_TEXT.ask}
</span>
</button>
</span>
)
}
/**
File diff suppressed because it is too large Load Diff
+15 -5
View File
@@ -8,12 +8,13 @@ import { NODE_DEFS } from '../canvas/nodeDefs'
import { StatusDot, useToast } from '../components/ui'
import { useLocalActor } from '../lib/actor'
import { formatDateTime, formatSpan, formatTime, parseServerTime } from '../lib/format'
import { runClassLabel, runName, TOOL_TRUST_TEXT } from '../lib/terms'
import { INQUIRY_TEXT, runClassLabel, runName, TOOL_TRUST_TEXT } from '../lib/terms'
import { useRunClock } from './useRunClock'
import { Markdown } from './Markdown'
import { ApprovalReports, ApprovalRunClass } from './ApprovalReport'
import { ChallengeSummary } from './ChallengeSummary'
import { DualCheckApproval } from './DualCheckApproval'
import { useInquiryBusy } from './Inquiry'
import type { Approval, ApprovalReport } from '../types'
// 发起运行的表单搬去了 run/RunControl.tsx 的工具栏控件里。它原先常驻在
@@ -48,6 +49,8 @@ export function ApprovalCard({ approval, onResolved, showWorkflow = true }: {
approval.payload?.draft != null ? String(approval.payload.draft) : '',
)
const [busy, setBusy] = useState(false)
// 审批质询正在回答:服务端这时拒绝审批回复(409),批准 / 驳回先停用,并说明原因
const inquiring = useInquiryBusy(approval.run_id)
const payload = approval.payload ?? {}
// 本机署名随 X-Actor 发给后端、写进审批留痕;在设置或发布弹窗里改了,卡底这句跟着变
const actor = useLocalActor()
@@ -152,7 +155,8 @@ export function ApprovalCard({ approval, onResolved, showWorkflow = true }: {
{/* 双盲复算对不上、交给人裁决的审批,卡上画两路结果的对照 */}
{dualCheck && <DualCheckApproval approval={approval} onResolved={onResolved} />}
<ApprovalReports reports={reports} runId={approval.run_id} runClass={approval.run_class} />
<ApprovalReports reports={reports} runId={approval.run_id} runClass={approval.run_class}
approvalId={approval.status === 'pending' ? approval.id : undefined} />
<ChallengeSummary reports={reports} runId={approval.run_id} />
{payload.tool && (
@@ -193,20 +197,26 @@ export function ApprovalCard({ approval, onResolved, showWorkflow = true }: {
/>
<div className="flex gap-2">
<button className="btn btn-primary flex-1 justify-center" disabled={busy} onClick={() => decide(true)}>
<button className="btn btn-primary flex-1 justify-center" disabled={busy || inquiring} onClick={() => decide(true)}>
<Check size={12} aria-hidden /> {approveMode ? '批准' : '提交'}
</button>
{trustKey && (
<button className="btn flex-1 justify-center" disabled={busy} data-approval-always
<button className="btn flex-1 justify-center" disabled={busy || inquiring} data-approval-always
title={TOOL_TRUST_TEXT.alwaysHint(trustKey)} aria-describedby={`approval-always-${approval.id}`}
onClick={() => decide(true, true)}>
<CheckCheck size={12} aria-hidden /> {TOOL_TRUST_TEXT.alwaysButton}
</button>
)}
<button className="btn btn-danger flex-1 justify-center" disabled={busy} onClick={() => decide(false)}>
<button className="btn btn-danger flex-1 justify-center" disabled={busy || inquiring} onClick={() => decide(false)}>
<Ban size={12} aria-hidden /> {approveMode ? '驳回' : '驳回并终止'}
</button>
</div>
{inquiring && (
<div className="mt-1.5 text-2xs font-medium leading-relaxed" role="status" data-approval-inquiry-busy=""
style={{ color: 'var(--st-waiting)' }}>
{INQUIRY_TEXT.decideBlocked}
</div>
)}
{/* 批了会怎样,常显而不是藏在 hover 里:审批人得在按下去之前知道 */}
<div className="mt-1.5 text-2xs leading-relaxed text-dim">
{approveMode
+735 -6
View File
@@ -1,8 +1,737 @@
// 审批质询的检查。现在只是占位:功能还在开发,这里只打一行通过,让 check-all 的清单先排上它。
// 审批质询的工作包把它换成真检查:沿用别的检查的输出格式——每项缩进两格、以 ✓ / ✗ 开头,最后顶格一行
// 总结,没过的退出码非 0(check-all 按缩进的 ✓ / ✗ 计数)。
// 审批质询的回归检查:报告上的入口(run/EvidenceDoc.tsx 的句末「质询」、划选原文浮出的「质询」)、问答串和提问框
// (run/Inquiry.tsx)、审批卡上的挂法(run/ApprovalReport.tsx、run/RunPanel.tsx)、运行详情「证据」页签的挂法
// (pages/runs/EvidencePane.tsx)。
//
// 守的是这些「看着没坏、其实用不了」的地方:悬停、键盘聚焦一句都不出「质询」(或者出了 Tab 却到不了、整份报告多出一串
// Tab 位)、没有可点内容的句子键盘走不到、划选原文不浮出入口(或者带上的原文混进了徽标和「质询」本身)、提交的请求体
// 和约定对不上(多带、少带 quote、approval_id,unit 写错)、回答期间批准 / 驳回还能点、回答回来不挂到那句话下面、
// 进入审批卡时问答串没有恢复、问答串写不出问的是哪一句、回答里的数字点了不去取质询工件(或者每点一次取一次、标不出
// 那一格)、引用报告已有出处的数字点了没反应、查询的 SQL / 行数 / 截断 / 遮罩不显示、触顶和没答成不说、recorded 为假
// 不醒目、409 / 422 不照服务端的中文说明显示(或者问题文字丢了)、Esc 关不掉提问框(或者连带收起了别的、焦点掉到页面上)、
// 已处理的审批和封存了的正式运行还给入口、封存了的探索运行反而没有;窄屏(390px)被撑出横向滚动、浅色主题下看不清。
//
// 跑在预览页上:/preview.html?syn=inquiry-*(用例见 frontend/src/dev/fixtures/inquiry.tsx)。夹具在
// frontend/src/dev/fixtures/inquiry.json,报告文档和回答文档是后端 build_catalog + compose_doc 真实组装的(虚构的数据)。
// 质询的三个接口、工件、证据图、片段接口都在这里伪造;别的写请求一律拦掉并记下来,别的 GET 答 404,不碰真实服务。
//
// node scripts/check-inquiry.mjs
console.log('=== 审批质询 ===')
console.log(' ✓ 占位:功能开发中')
console.log('\n✓ 审批质询:占位通过')
// 对别的实例跑时带上地址:AGENTLAB_WEB=http://localhost:<前端端口> node scripts/check-inquiry.mjs
// INQUIRY_SHOTS=<目录> 时截几张图,供人眼复核。
import { mkdirSync, readFileSync } from 'node:fs'
import { chromium } from '../frontend/node_modules/playwright-core/index.mjs'
const WEB = process.env.AGENTLAB_WEB ?? 'http://localhost:5273'
const CHROME = process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
const SHOTS = process.env.INQUIRY_SHOTS ?? ''
const root = new URL('..', import.meta.url).pathname
const fx = JSON.parse(readFileSync(`${root}frontend/src/dev/fixtures/inquiry.json`, 'utf8'))
const RUNS = fx.runs
const RUN_CLASS = { [RUNS.approve]: 'exploratory', [RUNS.formal]: 'formal', [RUNS.explore]: 'exploratory', [RUNS.sealed]: 'formal' }
const REPORT = { [RUNS.approve]: fx.reports.approve, [RUNS.formal]: fx.reports.formal, [RUNS.explore]: fx.reports.explore,
[RUNS.sealed]: fx.reports.sealed }
const [REFUND, NEWCOMERS, MEMBERS, ONLINE] = Object.keys(fx.snapshots)
const BUSY_DETAIL = '正在回答质询,回答完再批准或驳回'
const ERRORS = {
inquiry_busy: { status: 409, json: { detail: '这次运行另有一条质询正在回答,请稍后再问', code: 'inquiry_busy' } },
inquiry_not_open: { status: 409, json: { detail: '运行已不在等待这张审批,无法质询', code: 'inquiry_not_open' } },
inquiry_disabled: { status: 409, json: { detail: '审批质询已在设置中关闭,可在「设置 → 偏好设置 → 核查」中开启', code: 'inquiry_disabled' } },
invalid: { status: 422, json: { detail: '问题不能为空,最长 1000 字', field: 'question' } },
}
let failed = 0
const check = (name, cond, detail = '') => {
console.log(` ${cond ? '✓' : '✗'} ${name}${detail ? ` — ${detail}` : ''}`)
if (!cond) failed++
}
async function section(name, fn) {
console.log(`\n=== ${name} ===`)
try {
await fn()
} catch (e) {
check(`${name} 中途出错`, false, String(e?.message ?? e).split('\n')[0])
}
}
const browser = await chromium.launch({ executablePath: CHROME })
/** 一次新问的答复:照夹具里的 answer,问题、句子按这次请求的改 */
const answerFor = (body, extra = {}) => ({
inquiry: { ...fx.answer, question: body.question, unit: body.unit ?? null, quote: body.quote ?? null,
approval_id: body.approval_id ?? null, post_seal: !body.approval_id, at: new Date().toISOString() },
recorded: true, note: null, ...extra,
})
/**
* 开一个预览用例。列表按运行答夹具里的问答(state.lists 可改),提问按 state.reply 答({status, json} 或按请求体算的函数;
* state.gate 是个没兑现的 Promise 时先挂着,测「回答期间」)。记下列表、提问、取快照、取工件的请求和拦下的写请求
*/
async function open(syn, { width = 1100, height = 900, theme = 'dark' } = {}) {
const ctx = await browser.newContext({ viewport: { width, height }, colorScheme: theme, timezoneId: 'Asia/Shanghai' })
ctx.setDefaultTimeout(6000)
const page = await ctx.newPage()
const hits = { lists: [], asks: [], snapshots: [], artifacts: [], segments: [], writes: [], other: [] }
const state = {
lists: { [RUNS.approve]: fx.items, [RUNS.formal]: fx.items, [RUNS.explore]: fx.explore_items },
reply: (body) => ({ status: 200, json: answerFor(body) }),
gate: null,
}
const errors = []
page.on('pageerror', (e) => errors.push(e.message))
page.on('dialog', (d) => { errors.push(`原生对话框:${d.message()}`); void d.dismiss() })
await page.route((u) => new URL(u).pathname.startsWith('/api/'), async (route) => {
const req = route.request()
const url = new URL(req.url())
const path = url.pathname
const method = req.method()
const inq = path.match(/^\/api\/runs\/([^/]+)\/inquiries$/)
if (inq && method === 'POST') {
const body = JSON.parse(req.postData() ?? '{}')
hits.asks.push({ run: decodeURIComponent(inq[1]), body })
if (state.gate) await state.gate
const out = typeof state.reply === 'function' ? state.reply(body) : state.reply
return route.fulfill({ status: out.status, json: out.json })
}
if (method !== 'GET') {
hits.writes.push(`${method} ${path}`)
return route.abort()
}
if (inq) {
const run = decodeURIComponent(inq[1])
hits.lists.push({ run, report: url.searchParams.get('report'), unit: url.searchParams.get('unit') })
return route.fulfill({ json: { items: state.lists[run] ?? [] } })
}
const snap = path.match(/^\/api\/runs\/([^/]+)\/inquiries\/artifacts\/([^/]+)$/)
if (snap) {
const id = decodeURIComponent(snap[2])
hits.snapshots.push(id)
const body = fx.snapshots[id]
if (!body) return route.fulfill({ status: 404, json: { detail: '不是这次运行的质询引用过的工件' } })
// 约定没定死外面包不包一层:一份包在 {id, content} 里,其余是快照本身,两种界面都得认
return route.fulfill({ json: id === REFUND ? { id, content: body } : body })
}
const art = path.match(/^\/api\/artifacts\/([^/]+)$/)
if (art) {
const id = decodeURIComponent(art[1])
hits.artifacts.push(id)
return fx.docs[id] ? route.fulfill({ json: { id, content: fx.docs[id] } })
: route.fulfill({ status: 404, json: { detail: '工件不存在,可能已被删除' } })
}
const seg = path.match(/^\/api\/runs\/([^/]+)\/evidence\/segments\/([^/]+)$/)
if (seg) {
hits.segments.push(decodeURIComponent(seg[2]))
return route.fulfill({ json: {} })
}
const graph = path.match(/^\/api\/runs\/([^/]+)\/evidence$/)
if (graph) {
const run = decodeURIComponent(graph[1])
const r = REPORT[run]
return route.fulfill({ json: {
run_id: run, mode: 'cited', seal: { sealed: true, ok: true },
reports: r ? [{ node_id: 'write', doc_artifact: r.doc_artifact, doc_sealed: true, hash_ok: true, fields: ['report'], stats: r.stats }] : [],
} })
}
const run = path.match(/^\/api\/runs\/([^/]+)$/)
if (run) {
const id = decodeURIComponent(run[1])
return route.fulfill({ json: { id, run_class: RUN_CLASS[id] ?? 'exploratory', status: 'interrupted' } })
}
hits.other.push(path)
return route.fulfill({ status: 404, json: { detail: '检查脚本没有伪造这个接口' } })
})
await page.goto(`${WEB}/preview.html?syn=${syn}&theme=${theme}`, { waitUntil: 'networkidle' })
return { ctx, page, hits, state, errors }
}
const text = async (loc) => (await loc.innerText()).replace(/\s+/g, ' ').trim()
const shot = async (page, name) => {
if (!SHOTS) return
mkdirSync(SHOTS, { recursive: true })
await page.screenshot({ path: `${SHOTS}/${name}.png`, fullPage: true })
}
const group = (page, unit) => page.locator(`[data-inquiry-group="${unit}"]`)
const entry = (page, seq) => page.locator(`[data-inquiry-entry="${seq}"]`)
const askBtn = (page, unit) => page.locator(`[data-inquiry-ask="${unit}"]`)
const composer = (page) => page.locator('[data-inquiry-composer]')
/** 一个元素此刻看不看得见:宽度、不透明度(「质询」平时宽 0、透明) */
const seen = (loc) => loc.evaluate((el) => {
const r = el.getBoundingClientRect()
return r.width > 4 && Number(getComputedStyle(el).opacity) > 0.9
})
const active = (page) => page.evaluate(() => {
const el = document.activeElement
return el ? { ask: el.getAttribute('data-inquiry-ask'), seg: el.getAttribute('data-seg'), tag: el.tagName,
input: el.hasAttribute('data-inquiry-input'), entry: el.closest('[data-inquiry-entry]')?.getAttribute('data-inquiry-entry') ?? null,
pending: el.hasAttribute('data-inquiry-pending'), report: el.hasAttribute('data-inquiry-ask-report') } : null
})
const approve = (page) => page.locator('[data-approval]').getByRole('button', { name: '批准', exact: true })
const reject = (page) => page.locator('[data-approval]').getByRole('button', { name: '驳回', exact: true })
/** 展开审批卡里的逐段证据,等报告文档画出来 */
async function expand(page) {
await page.locator('[data-approval-evidence]').click()
await page.locator('[data-approval-evidence-doc] [data-evidence-doc] [data-block]').first().waitFor()
}
/** 像人一样点某一句的「质询」:先把鼠标移到这一句的字上(「质询」这才出现),再点它 */
async function clickAsk(page, unit) {
// 句子会折行:包住它的框的左上角可能落在上一句上,鼠标要放到这一句第一行的字上
const at = await page.locator(`[data-ev-unit="${unit}"]`).evaluate((el) => {
el.scrollIntoView({ block: 'center' })
const r = el.getClientRects()[0]
return { x: r.left + Math.min(8, r.width / 2), y: r.top + r.height / 2 }
})
await page.mouse.move(at.x, at.y)
await askBtn(page, unit).click()
}
/** 等着某个条件成立(最多 ms 毫秒) */
async function until(fn, ms = 3000) {
const end = Date.now() + ms
while (Date.now() < end) {
if (await fn()) return true
await new Promise((r) => setTimeout(r, 50))
}
return !!(await fn())
}
/** 在某一句里划选一段原文(同用鼠标拖选:浏览器发 selectionchange) */
const select = (page, unit, words) => page.evaluate(([u, w]) => {
// 没有「质询」的报告上句子不包一层:在整份报告里找这段字
const el = document.querySelector(`[data-ev-unit="${u}"]`) ?? document.querySelector('[data-evidence-doc] [role="group"]')
if (!el) return false
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT)
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
const i = n.data.indexOf(w)
if (i < 0) continue
const range = document.createRange()
range.setStart(n, i)
range.setEnd(n, i + w.length)
const sel = getSelection()
sel.removeAllRanges()
sel.addRange(range)
return true
}
return false
}, [unit, words])
await section('审批卡:进入时恢复问答串,按句子挂好', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
const box = page.locator('[data-inquiry="write"]')
await box.locator('[data-inquiry-entry="36"]').waitFor()
check('问答串挂在报告核对概况里(同一块),标题写审批质询和次数', await page.locator('[data-approval-report="write"] [data-inquiry="write"]').count() === 1
&& (await text(box.locator('[data-inquiry-count]'))) === '· 3 次', await text(box.locator('div').first()))
check('进入时按列表接口取一次,带上报告节点', hits.lists.length === 1 && hits.lists[0].run === RUNS.approve && hits.lists[0].report === 'write',
JSON.stringify(hits.lists))
const groups = await box.locator('[data-inquiry-group]').evaluateAll((els) => els.map((el) => ({
unit: el.getAttribute('data-inquiry-group'), about: el.querySelector('[data-inquiry-about-text]')?.textContent?.trim() ?? '',
entries: [...el.querySelectorAll('[data-inquiry-entry]')].map((x) => x.getAttribute('data-inquiry-entry')),
})))
check('按句子归拢、按时间排:退款那句、新客那句、枫岭区那句', JSON.stringify(groups.map((g) => [g.unit, g.entries]))
=== JSON.stringify([['u2', ['31']], ['u3', ['34']], ['u4', ['36']]]), JSON.stringify(groups.map((g) => [g.unit, g.entries])))
check('每组写出问的是哪一句(原文取自报告文档,逐段证据收着也取)', groups[0]?.about === '「退款金额 21,500元,退款率 1.89%。」'
&& groups[1]?.about === '「增长主要来自新客。」' && groups[2]?.about.startsWith('「分片区看,枫岭区销售额最高'),
groups.map((g) => g.about).join(' / '))
check('划选原文问的:问题下面写出原文', (await text(entry(page, 36).locator('[data-inquiry-quote]'))) === '原文「枫岭区销售额最高」')
const e1 = entry(page, 31)
// 时间按本地时区写(检查固定在 Asia/Shanghai);不是今天的前面带日期
const meta = await text(e1.locator('[data-inquiry-meta]'))
check('问题、提问人和时间', (await text(e1.locator('[data-inquiry-question]'))) === '退款率的分母是含税销售额还是不含税销售额?'
&& meta.startsWith('张工 · ') && meta.endsWith('14:12'), meta)
check('没署名的写「未署名」', (await text(entry(page, 34).locator('[data-inquiry-meta]'))).startsWith('未署名'))
await e1.locator('[data-inquiry-answer-doc]').waitFor()
check('回答按文档画(数字带出处),取了回答文档', (await text(e1.locator('[data-inquiry-answer-doc]'))).includes('两者之比为 0.0189')
&& hits.artifacts.includes(fx.items[0].answer.doc_artifact))
const cites = await e1.locator('[data-inquiry-cite]').evaluateAll((els) => els.map((el) => [el.getAttribute('data-inquiry-cite'),
el.querySelector('sup')?.textContent ?? null, el.getAttribute('data-ev-line')]))
check('质询自己查询里的数挂查询编号、报告已有出处的不挂', JSON.stringify(cites) === JSON.stringify([['Q1', 'Q1', 'solid'],
['Q1', 'Q1', 'solid'], ['Q1', 'Q1', 'solid'], ['report', null, 'solid']]), JSON.stringify(cites))
check('读屏听得到出处', ((await e1.locator('[data-inquiry-cite="Q1"]').first().getAttribute('aria-label')) ?? '')
.includes('出处:查询 Q1 · 第 1 行 · refund'))
const e2 = entry(page, 34)
await e2.locator('[data-inquiry-answer-doc]').waitFor()
check('裸数字画点状线、挂「无证据」', JSON.stringify(await e2.locator('.ev-mark[data-ev-state="none"]').allInnerTexts()
.then((t) => t.map((x) => x.replace(/\s*(无证据)$/, '')))) === JSON.stringify(['13%'])
&& (await e2.locator('[data-ev-tag="none"]').count()) === 1)
check('触顶收尾:写明哪个上限、回答可能不完整', (await text(e2.locator('[data-inquiry-limit]'))) === '已达查询次数上限:回答依据已查到的结果,可能不完整')
const e3 = entry(page, 36)
check('没答成:写明原因(服务端的说明),不再另说「没有给出回答」', (await text(e3.locator('[data-inquiry-failed]'))).startsWith('未能回答(已达核查每日金额上限):今日核查费用已达上限')
&& await e3.locator('[data-inquiry-answer-empty]').count() === 0, await text(e3.locator('[data-inquiry-failed]')))
check('没答成、没调用过模型:不写「回答由核查模型给出」,也不写花费 $0', await e3.locator('[data-inquiry-judged]').count() === 0
&& !(await text(e3)).includes('$0'), await text(e3))
check('没查询的写明没有执行查询', (await text(e3.locator('[data-inquiry-queries="0"]'))) === '核查模型没有执行查询')
check('说清回答是核查模型给的、不是系统核对', (await text(e1.locator('[data-inquiry-judged]'))).startsWith('回答由核查模型「verify-mini」给出,并非系统核对'))
check('写进了运行记录的不说「没有写入」', await box.locator('[data-inquiry-unrecorded]').count() === 0)
check('给「就整份报告提问」、没有提问框', await box.locator('[data-inquiry-ask-report]').count() === 1 && await composer(page).count() === 0)
check('逐段证据收着时不取片段、不取快照', hits.snapshots.length === 0 && hits.segments.length === 0)
await shot(page, 'approval-dark')
check('没有发出任何写请求', hits.writes.length === 0, hits.writes.join('、'))
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('回答里的数字:质询自己的查询取质询工件、标出那一格;查询列表', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
const e1 = entry(page, 31)
await e1.locator('[data-inquiry-answer-doc]').waitFor()
check('查询列表默认收着,写明共几次', (await text(e1.locator('[data-inquiry-queries-toggle]'))) === '核查模型执行的查询(1 次)'
&& (await e1.locator('[data-inquiry-queries-toggle]').getAttribute('aria-expanded')) === 'false')
await e1.locator('[data-inquiry-cite="Q1"]').first().click()
const q1 = e1.locator('[data-inquiry-query="Q1"]')
await q1.locator('[data-inquiry-snapshot="ok"]').waitFor()
check('点数字:按质询工件接口取那次查询的快照(包了一层 {id, content} 的也认)', JSON.stringify(hits.snapshots) === JSON.stringify([REFUND]),
hits.snapshots.join('、'))
const cell = await q1.locator('[data-highlight="cell"]').allInnerTexts()
check('标出被引用的那一格(第 1 行 refund)', JSON.stringify(cell) === JSON.stringify(['21500']), JSON.stringify(cell))
check('焦点落到那次查询上', (await page.evaluate(() => document.activeElement?.closest('[data-inquiry-query]')?.getAttribute('data-inquiry-query'))) === 'Q1')
check('查询卡:SQL、行数', (await text(q1.locator('[data-inquiry-sql]'))).startsWith('SELECT SUM(r.amount) AS refund')
&& (await text(q1.locator('[data-inquiry-rows]'))) === '1 行')
await e1.locator('[data-inquiry-cite="Q1"]').nth(2).click()
await page.waitForTimeout(150)
check('点同一次查询的另一个数:换高亮的格,不再取快照', JSON.stringify(await q1.locator('[data-highlight="cell"]').allInnerTexts()) === JSON.stringify(['0.0189'])
&& hits.snapshots.length === 1, `${await q1.locator('[data-highlight="cell"]').allInnerTexts()} · ${hits.snapshots.length}`)
const e2 = entry(page, 34)
await e2.locator('[data-inquiry-queries-toggle]').click()
const q2 = e2.locator('[data-inquiry-query="Q2"]')
check('截断的照实写:行数和「结果已截断」', (await text(q2.locator('[data-inquiry-rows]'))) === '50 行'
&& (await text(q2.locator('[data-inquiry-truncated]'))) === '结果已截断,数据库中还有更多数据')
await q2.locator('[data-inquiry-result-toggle]').click()
await q2.locator('[data-inquiry-snapshot="ok"]').waitFor()
const masked = await q2.locator('td[data-masked]').count()
check('展开结果:遮罩列写「已遮罩」,并说明', masked === 50 && (await text(q2.locator('[data-inquiry-masked]'))) === 'phone 在数据源中设置了遮罩,不显示原值'
&& !(await text(q2)).includes('000-1000'), `${masked} 格`)
check('取的是这次查询的快照', hits.snapshots.at(-1) === MEMBERS)
await q2.locator('[data-inquiry-result-toggle]').click()
check('收起结果', await q2.locator('[data-inquiry-snapshot]').count() === 0)
// 第二个数引用的是第二次查询:展开它,焦点跟过去
await e2.locator('[data-inquiry-cite="Q2"]').click()
await q2.locator('[data-inquiry-snapshot="ok"]').waitFor()
check('点第二次查询的数:标出第 1 行 amount', JSON.stringify(await q2.locator('[data-highlight="cell"]').allInnerTexts()) === JSON.stringify(['9000']))
await shot(page, 'cite-dark')
check('没有发出任何写请求', hits.writes.length === 0, hits.writes.join('、'))
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('回答里引用报告已有出处的数字:在报告里打开证据面板', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
const e1 = entry(page, 31)
await e1.locator('[data-inquiry-answer-doc]').waitFor()
check('逐段证据收着', await page.locator('[data-approval-evidence-doc]').count() === 0)
await e1.locator('[data-inquiry-cite="report"]').click()
await page.locator('[data-approval] [data-evidence-panel]').waitFor()
const opened = await page.evaluate(() => [...document.querySelectorAll('[data-seg][aria-expanded="true"]')].map((el) => el.getAttribute('data-seg')))
check('展开逐段证据,在引用同一出处的那一处(销售额)打开证据面板', JSON.stringify(opened) === JSON.stringify(['s4']) && hits.segments.includes('s4'),
JSON.stringify(opened))
check('焦点落到报告里那一处', (await active(page))?.seg === 's4', JSON.stringify(await active(page)))
check('不去质询工件接口取', hits.snapshots.length === 0)
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('入口:悬停、键盘聚焦一句出现「质询」,Tab 到得了、Esc 关得掉', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
await expand(page)
const units = await page.locator('[data-inquiry-ask]').evaluateAll((els) => els.map((el) => el.getAttribute('data-inquiry-ask')))
check('每句结论后各一个「质询」,标题不挂', JSON.stringify(units) === JSON.stringify(['u1', 'u2', 'u3', 'u4', 'u5', 'u6', 'u7']), JSON.stringify(units))
check('平时都看不见、不占地方', !(await seen(askBtn(page, 'u2'))) && (await askBtn(page, 'u2').boundingBox())?.width < 1)
await page.locator('[data-ev-unit="u2"] [data-seg="s9"]').hover()
await page.waitForTimeout(100)
check('悬停一句:这一句的「质询」出现,别的句子的不出现', await seen(askBtn(page, 'u2')) && !(await seen(askBtn(page, 'u1'))))
check('读屏听得到问的是哪一句', (await askBtn(page, 'u2').getAttribute('aria-label')) === '质询这句:退款金额 21,500元,退款率 1.89%。')
await page.mouse.move(5, 5)
await page.waitForTimeout(100)
// 键盘:焦点落到一句的数字上,「质询」出现,Tab 到它
await page.locator('[data-seg="s4"]').focus()
check('焦点在这一句上:「质询」出现,进 Tab 顺序', await seen(askBtn(page, 'u1'))
&& (await askBtn(page, 'u1').getAttribute('tabindex')) === '0' && (await askBtn(page, 'u2').getAttribute('tabindex')) === '-1')
await page.keyboard.press('Tab')
check('Tab 到这一句的「质询」', (await active(page))?.ask === 'u1', JSON.stringify(await active(page)))
await page.keyboard.press('Shift+Tab')
check('Shift + Tab 回到这一句', (await active(page))?.seg === 's4')
await page.keyboard.press('ArrowRight')
await page.keyboard.press('ArrowRight')
check('方向键走到下一句:Tab 位跟着换到那一句的「质询」', (await active(page))?.seg === 's9'
&& (await askBtn(page, 'u2').getAttribute('tabindex')) === '0' && (await askBtn(page, 'u1').getAttribute('tabindex')) === '-1')
const tabbable = await page.locator('[data-evidence-doc] [tabindex="0"]').count()
check('整份报告仍然只占一个 Tab 位,外加这一句的「质询」', tabbable === 2, `${tabbable} 个`)
await page.keyboard.press('ArrowDown')
check('没有可点内容的句子(增长主要来自新客):方向键直接走到它的「质询」,出现', (await active(page))?.ask === 'u3' && await seen(askBtn(page, 'u3')),
JSON.stringify(await active(page)))
await page.keyboard.press('Enter')
await composer(page).waitFor()
check('回车打开提问框:写明就这句提问、显示这一句', (await composer(page).getAttribute('data-inquiry-composer')) === 'unit'
&& (await text(composer(page).locator('[data-inquiry-target]'))) === '增长主要来自新客。')
check('提问框挂在这一句的问答下面', await group(page, 'u3').locator('[data-inquiry-composer]').count() === 1)
check('焦点在问题输入框里', (await active(page))?.input === true)
await page.keyboard.press('Escape')
await page.waitForTimeout(100)
check('Esc 关掉提问框,焦点回到这一句的「质询」', await composer(page).count() === 0 && (await active(page))?.ask === 'u3', JSON.stringify(await active(page)))
check('Esc 不连带收起别的(逐段证据还开着)', await page.locator('[data-approval-evidence-doc]').count() === 1)
await clickAsk(page, 'u1')
await composer(page).waitFor()
await page.locator('[data-inquiry-input]').fill('销售额有没有扣除优惠券?')
await clickAsk(page, 'u2')
await page.waitForTimeout(100)
check('提问框开着又点另一句:换到那一句,问题留着', (await text(composer(page).locator('[data-inquiry-target]'))) === '退款金额 21,500元,退款率 1.89%。'
&& (await page.locator('[data-inquiry-input]').inputValue()) === '销售额有没有扣除优惠券?' && await group(page, 'u2').locator('[data-inquiry-composer]').count() === 1)
await composer(page).locator('button', { hasText: '取消' }).click()
check('取消关掉提问框', await composer(page).count() === 0)
await shot(page, 'entry-dark')
check('没有发出任何写请求', hits.writes.length === 0 && hits.asks.length === 0, hits.writes.join('、'))
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('提问:请求体按约定;回答期间批准 / 驳回不可用;回答挂到那句话下面', async () => {
const { ctx, page, hits, state, errors } = await open('inquiry-approval')
await expand(page)
await page.locator('[data-seg="s4"]').focus()
await page.keyboard.press('Tab')
await page.keyboard.press('Enter')
await composer(page).waitFor()
check('提交按钮:没写问题时不可用', await page.locator('[data-inquiry-submit]').isDisabled())
await page.locator('[data-inquiry-input]').fill('这里的销售额包含线上订单吗?')
let release
state.gate = new Promise((r) => { release = r })
await page.locator('[data-inquiry-submit]').click()
await page.locator('[data-inquiry-pending]').waitFor()
check('请求体:report、unit、question、approval_id,没划选就不带 quote', JSON.stringify(hits.asks.map((a) => a.body))
=== JSON.stringify([{ report: 'write', unit: 'u1', question: '这里的销售额包含线上订单吗?', approval_id: fx.approval_id }])
&& hits.asks[0].run === RUNS.approve, JSON.stringify(hits.asks))
check('回答期间:说正在查询与回答,通常需要几十秒', (await text(page.locator('[data-inquiry-pending] [role="status"]'))).startsWith('正在查询与回答,通常需要几十秒')
&& await group(page, 'u1').locator('[data-inquiry-pending]').count() === 1)
check('回答期间:提问框收起,焦点接到正在回答的那一条上', await composer(page).count() === 0 && (await active(page))?.pending === true,
JSON.stringify(await active(page)))
check('回答期间:批准、驳回不可用,并说明原因', await approve(page).isDisabled() && await reject(page).isDisabled()
&& (await text(page.locator('[data-approval-inquiry-busy]'))) === BUSY_DETAIL)
await clickAsk(page, 'u2')
await composer(page).waitFor()
await page.locator('[data-inquiry-input]').fill('第二个问题')
check('回答期间再开提问框:提交不可用,写明要等这一条答完', await page.locator('[data-inquiry-submit]').isDisabled()
&& (await text(composer(page))).includes('这次运行另有一条质询正在回答,回答完再提问'))
await composer(page).locator('button', { hasText: '取消' }).click()
await shot(page, 'pending-dark')
release()
await group(page, 'u1').locator('[data-inquiry-entry="40"]').waitFor()
check('回答回来:挂在问的那一句下面,按文档画', (await text(entry(page, 40).locator('[data-inquiry-answer-doc]'))).includes('线上订单销售额 118,000')
&& await entry(page, 40).locator('[data-inquiry-cite="Q1"]').count() === 2
&& await page.locator('[data-inquiry-pending]').count() === 0, await text(group(page, 'u1')))
check('回答回来:批准、驳回恢复可用,原因说明拿掉', !(await approve(page).isDisabled()) && !(await reject(page).isDisabled())
&& await page.locator('[data-approval-inquiry-busy]').count() === 0)
check('回答回来:次数加一', (await text(page.locator('[data-inquiry-count]'))) === '· 4 次')
check('写进了运行记录:不说没有写入', await entry(page, 40).locator('[data-inquiry-unrecorded]').count() === 0)
await page.waitForTimeout(100)
check('回答回来时焦点已经在别处(刚才点过的那句「质询」):不抢', (await active(page))?.ask === 'u2', JSON.stringify(await active(page)))
check('除了提问没有别的写请求', hits.writes.length === 0, hits.writes.join('、'))
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('划选原文:浮出「质询」,带上原文', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
await expand(page)
check('还没划选:没有浮出的「质询」', await page.locator('[data-inquiry-selection]').count() === 0)
check('划选一段原文', await select(page, 'u4', '枫岭区销售额最高'))
await page.locator('[data-inquiry-selection]').waitFor()
const at = await page.evaluate(() => {
const sel = getSelection().getRangeAt(0).getBoundingClientRect()
const b = document.querySelector('[data-inquiry-selection]').getBoundingClientRect()
return { gap: b.top - sel.bottom, near: Math.abs(b.left + b.width / 2 - sel.right) }
})
check('浮出的「质询」贴在选区末尾下方', at.gap >= 0 && at.gap < 20 && at.near < 60, JSON.stringify(at))
await page.locator('[data-inquiry-selection]').click()
await composer(page).waitFor()
check('点它:提问框写明就选中的原文提问,显示原文', (await composer(page).getAttribute('data-inquiry-composer')) === 'quote'
&& (await text(composer(page).locator('[data-inquiry-target]'))) === '枫岭区销售额最高')
check('浮出的按钮随之拿掉、选区清掉', await page.locator('[data-inquiry-selection]').count() === 0
&& await page.evaluate(() => getSelection().isCollapsed || !document.querySelector('[data-approval-evidence-doc]').contains(getSelection().anchorNode)))
check('挂在原文起头那一句(枫岭区那句)的问答下面', await group(page, 'u4').locator('[data-inquiry-composer]').count() === 1)
await page.locator('[data-inquiry-input]').fill('枫岭区的销售额是否包含门店自提?')
await page.keyboard.press('Control+Enter')
await entry(page, 40).waitFor()
check('Ctrl + 回车提交;请求体带上原文和它起头的那一句', JSON.stringify(hits.asks.map((a) => a.body)) === JSON.stringify([{
report: 'write', unit: 'u4', quote: '枫岭区销售额最高', question: '枫岭区的销售额是否包含门店自提?', approval_id: fx.approval_id }]),
JSON.stringify(hits.asks.map((a) => a.body)))
// 跨两句划选:原文里不能混进句末的「质询」、「无证据」小标签
check('跨句划选', await page.evaluate(() => {
const a = document.querySelector('[data-ev-unit="u4"]')
const b = document.querySelector('[data-ev-unit="u5"]')
const range = document.createRange()
range.setStart(a, 0)
range.setEnd(b, b.childNodes.length)
getSelection().removeAllRanges()
getSelection().addRange(range)
return true
}))
await page.locator('[data-inquiry-selection]').waitFor()
await page.locator('[data-inquiry-selection]').click()
await composer(page).waitFor()
const quote = await text(composer(page).locator('[data-inquiry-target]'))
check('跨句的原文:只有报告的字,没有混进「质询」和「无证据」', quote.startsWith('分片区看,枫岭区销售额最高') && quote.endsWith('环比下降 3.2%。')
&& !quote.includes('质询') && !quote.includes('无证据'), quote)
await page.keyboard.press('Escape')
// 证据面板里的字不算报告原文
await page.locator('[data-seg="s17"]').click()
await page.locator('[data-approval] [data-evidence-panel]').waitFor()
await page.evaluate(() => {
const p = document.querySelector('[data-evidence-panel] [data-ev-body]')
const range = document.createRange()
range.selectNodeContents(p)
getSelection().removeAllRanges()
getSelection().addRange(range)
})
await page.waitForTimeout(200)
check('划选证据面板里的字:不浮出「质询」', await page.locator('[data-inquiry-selection]').count() === 0)
await shot(page, 'selection-dark')
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('就整份报告提问;recorded 为假时醒目说明', async () => {
const { ctx, page, hits, state, errors } = await open('inquiry-approval')
state.reply = (body) => ({ status: 200, json: answerFor(body, {
inquiry: { ...answerFor(body).inquiry, seq: null }, recorded: false, note: '运行已不在等待审批,问答未写入运行记录' }) })
await page.locator('[data-inquiry-ask-report]').click()
await composer(page).waitFor()
check('提问框写明就整份报告提问,不显示原文', (await composer(page).getAttribute('data-inquiry-composer')) === 'report'
&& await composer(page).locator('[data-inquiry-target]').count() === 0 && await group(page, 'report').locator('[data-inquiry-composer]').count() === 1)
await page.locator('[data-inquiry-input]').fill('这份报告的数据截止到哪一天?')
await page.locator('[data-inquiry-submit]').click()
await group(page, 'report').locator('[data-inquiry-recorded="false"]').waitFor()
check('请求体:unit 为 null,不带 quote', JSON.stringify(hits.asks[0]?.body) === JSON.stringify({
report: 'write', unit: null, question: '这份报告的数据截止到哪一天?', approval_id: fx.approval_id }), JSON.stringify(hits.asks[0]?.body))
await page.waitForTimeout(100)
check('提交后焦点接到正在回答的那一条,回答回来再接到回答上,不掉到页面上', (await active(page))?.entry === '', JSON.stringify(await active(page)))
const note = group(page, 'report').locator('[data-inquiry-unrecorded]')
check('没写进运行记录:醒目说明,带上服务端的说明', (await text(note)).startsWith('这次问答没有写入运行记录:运行已不在等待审批,问答未写入运行记录')
&& Number(await note.evaluate((el) => getComputedStyle(el).fontWeight)) >= 600, await text(note))
check('……并说清后果:只在当前页面、不随运行封存', (await text(note)).includes('不会随运行封存'))
await shot(page, 'unrecorded-dark')
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('409 / 422:照服务端的中文说明显示,问题留着', async () => {
const { ctx, page, hits, state, errors } = await open('inquiry-approval')
for (const [code, reply] of Object.entries(ERRORS)) {
state.reply = reply
await page.locator('[data-inquiry-ask-report]').click()
await composer(page).waitFor()
await page.locator('[data-inquiry-input]').fill(`问题 ${code}`)
await page.locator('[data-inquiry-submit]').click()
const err = composer(page).locator('[data-inquiry-error]')
await err.waitFor()
const want = code === 'invalid' ? '422' : code
check(`${reply.status} ${code === 'invalid' ? '输入不合法' : code}:提问框带着原来的问题重新打开,照服务端的说明显示`,
(await err.getAttribute('data-inquiry-error')) === want && (await text(err)) === reply.json.detail
&& (await page.locator('[data-inquiry-input]').inputValue()) === `问题 ${code}` && (await err.getAttribute('role')) === 'alert',
`${await err.getAttribute('data-inquiry-error')} · ${await text(err)}`)
check(`${code}:批准、驳回照常可用,没有留下正在回答`, !(await approve(page).isDisabled()) && await page.locator('[data-inquiry-pending]').count() === 0)
if (code === 'invalid') {
check('422 指明是问题这一项:输入框标为无效', (await page.locator('[data-inquiry-input]').getAttribute('aria-invalid')) === 'true')
}
await page.locator('[data-inquiry-input]').fill('改一下')
check(`${code}:改了问题,报错拿掉`, await composer(page).locator('[data-inquiry-error]').count() === 0)
await page.keyboard.press('Escape')
}
check('四次都发出了请求', hits.asks.length === 4, `${hits.asks.length} 次`)
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('字数上限、空问题', async () => {
const { ctx, page, hits, errors } = await open('inquiry-approval')
await page.locator('[data-inquiry-ask-report]').click()
await composer(page).waitFor()
await page.locator('[data-inquiry-input]').fill(' ')
await page.keyboard.press('Control+Enter')
check('只有空白:提交不可用,Ctrl + 回车也不发', await page.locator('[data-inquiry-submit]').isDisabled() && hits.asks.length === 0)
await page.locator('[data-inquiry-input]').fill('问'.repeat(1001))
check('超过 1000 字:字数标红、写明上限、提交不可用', (await text(composer(page).locator('[data-inquiry-chars]'))) === '1,001/1,000'
&& (await text(composer(page))).includes('问题最多 1,000 字') && await page.locator('[data-inquiry-submit]').isDisabled()
&& (await page.locator('[data-inquiry-input]').getAttribute('aria-invalid')) === 'true')
await page.locator('[data-inquiry-input]').fill('问'.repeat(1000))
check('正好 1000 字:可以提交', !(await page.locator('[data-inquiry-submit]').isDisabled()))
check('页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
})
await section('正式运行的审批:审批阶段照样能问;已处理的审批只读', async () => {
{
const { ctx, page, hits, errors } = await open('inquiry-approval-formal')
await page.locator('[data-inquiry="write"] [data-inquiry-entry="31"]').waitFor()
await page.locator('[data-inquiry-ask-report]').click()
await page.locator('[data-inquiry-input]').fill('正式运行里问一句')
await page.locator('[data-inquiry-submit]').click()
await entry(page, 40).waitFor()
check('正式运行停在审批上:可以问,带的是这张审批', hits.asks[0]?.run === RUNS.formal && hits.asks[0]?.body.approval_id === fx.formal_approval_id,
JSON.stringify(hits.asks[0]))
check('正式运行:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
{
const { ctx, page, hits, errors } = await open('inquiry-approval-done')
await page.locator('[data-inquiry="write"] [data-inquiry-entry="31"]').waitFor()
await expand(page)
check('已处理的审批:问答串照常列出', await page.locator('[data-inquiry-entry]').count() === 3)
check('已处理的审批:不给「就整份报告提问」、报告上没有「质询」', await page.locator('[data-inquiry-ask-report], [data-inquiry-ask]').count() === 0)
await select(page, 'u4', '枫岭区销售额最高')
await page.waitForTimeout(200)
check('已处理的审批:划选也不浮出「质询」', await page.locator('[data-inquiry-selection]').count() === 0)
check('已处理的审批:没有发出提问', hits.asks.length === 0)
check('已处理的审批:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
})
await section('运行详情「证据」页签:封存了的探索运行有入口,正式运行没有', async () => {
{
const { ctx, page, hits, errors } = await open('inquiry-run-explore')
await page.locator('[data-evidence-report="write"] [data-ev-unit="u1"]').waitFor()
await page.locator('[data-inquiry="write"] [data-inquiry-entry="58"]').waitFor()
check('报告上有「质询」,每句结论一个', await page.locator('[data-evidence-report="write"] [data-inquiry-ask]').count() === 7)
check('问答串挂在报告下面:封存之后问的写明封存后追加', (await text(entry(page, 58).locator('[data-inquiry-meta]'))).includes('封存后追加'))
check('问答串按运行和报告取', hits.lists.length === 1 && hits.lists[0].run === RUNS.explore && hits.lists[0].report === 'write')
await page.locator('[data-evidence-report="write"] [data-seg="s4"]').focus()
await page.keyboard.press('Tab')
await page.keyboard.press('Enter')
await composer(page).waitFor()
check('提问框写明运行已封存、问答追加在封存之后', (await text(composer(page))).includes('运行已封存:问答追加在封存之后,不改动报告'))
await page.locator('[data-inquiry-input]').fill('封存之后再问一句')
await page.locator('[data-inquiry-submit]').click()
await entry(page, 40).waitFor()
check('请求体不带 approval_id', JSON.stringify(hits.asks[0]?.body) === JSON.stringify({ report: 'write', unit: 'u1', question: '封存之后再问一句' }),
JSON.stringify(hits.asks[0]?.body))
await shot(page, 'run-explore-dark')
check('探索运行:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
{
const { ctx, page, hits, errors } = await open('inquiry-run-formal')
await page.locator('[data-evidence-report="write"] [data-ev-unit], [data-evidence-report="write"] [data-seg="s4"]').first().waitFor()
await page.waitForTimeout(300)
check('正式运行封存后:报告上没有「质询」,也没有问答串和提问入口',
await page.locator('[data-inquiry-ask], [data-inquiry], [data-inquiry-ask-report]').count() === 0)
await page.evaluate(() => {
const el = [...document.querySelectorAll('[data-evidence-report="write"] [data-block]')].find((b) => b.textContent.includes('枫岭区'))
const range = document.createRange()
range.selectNodeContents(el)
getSelection().removeAllRanges()
getSelection().addRange(range)
})
await page.waitForTimeout(200)
check('正式运行封存后:划选也不浮出「质询」', await page.locator('[data-inquiry-selection]').count() === 0)
check('正式运行封存后:不取问答列表', hits.lists.length === 0, JSON.stringify(hits.lists))
check('正式运行:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
})
/** 页面和某个容器都没有横向滚动(表格、SQL 框自己的滚动条不算) */
async function overflow(page, sel) {
return page.evaluate((s) => {
const doc = document.documentElement
const box = document.querySelector(s)
return { page: doc.scrollWidth - doc.clientWidth, box: box ? box.scrollWidth - box.clientWidth : -1 }
}, sel)
}
/** 两个颜色的对比度(WCAG):颜色是 getComputedStyle 给的 rgb / rgba / color(srgb …),半透明的叠在底色上 */
function contrast(fg, bg, base) {
const parse = (c) => {
const m = c.match(/color\(srgb ([\d.]+) ([\d.]+) ([\d.]+)(?: \/ ([\d.]+))?\)/)
if (m) return [m[1] * 255, m[2] * 255, m[3] * 255, m[4] == null ? 1 : Number(m[4])]
const n = c.match(/rgba?\(([\d.]+),\s*([\d.]+),\s*([\d.]+)(?:,\s*([\d.]+))?\)/)
return n ? [Number(n[1]), Number(n[2]), Number(n[3]), n[4] == null ? 1 : Number(n[4])] : null
}
const over = (top, under) => top.slice(0, 3).map((v, i) => v * top[3] + under[i] * (1 - top[3]))
const lum = ([r, g, b]) => {
const f = (v) => { const s = v / 255; return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4 }
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)
}
const b0 = parse(base); const b1 = parse(bg); const f0 = parse(fg)
if (!b0 || !b1 || !f0) return 0
const under = over(b1, b0.slice(0, 3))
const a = lum(over(f0, under)); const b = lum(under)
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05)
}
/** 某个元素的字色对它所在那一块底色的对比度:底色往上找第一个不透明的,最底下是 body */
const ratioOf = (page, sel) => page.evaluate((s) => {
const el = document.querySelector(s)
if (!el) return null
let bgEl = el
let bg = 'rgba(0, 0, 0, 0)'
while (bgEl && /rgba\(0, 0, 0, 0\)|transparent/.test(bg)) {
bg = getComputedStyle(bgEl).backgroundColor
bgEl = bgEl.parentElement
}
return { fg: getComputedStyle(el).color, bg, base: getComputedStyle(document.body).backgroundColor }
}, sel).then((c) => (c ? contrast(c.fg, c.bg, c.base) : 0))
await section('窄屏与深浅色', async () => {
for (const theme of ['dark', 'light']) {
const { ctx, page, state, errors } = await open('inquiry-approval', { width: 390, height: 844, theme })
state.reply = (body) => ({ status: 200, json: answerFor(body, { inquiry: { ...answerFor(body).inquiry, seq: null }, recorded: false,
note: '运行已不在等待审批' }) })
await page.locator('[data-inquiry-entry="34"] [data-inquiry-answer-doc]').waitFor()
await expand(page)
await page.locator('[data-inquiry-entry="34"] [data-inquiry-cite="Q2"]').click()
await page.locator('[data-inquiry-entry="34"] [data-inquiry-query="Q2"] [data-inquiry-snapshot="ok"]').waitFor()
await page.locator('[data-inquiry-ask-report]').click()
await page.locator('[data-inquiry-input]').fill('窄屏里问一句')
await page.locator('[data-inquiry-submit]').click()
await page.locator('[data-inquiry-unrecorded]').waitFor()
await page.locator('[data-seg="s4"]').focus()
await page.keyboard.press('Tab')
await page.keyboard.press('Enter')
await composer(page).waitFor()
const o = await overflow(page, '[data-approval]')
check(`390px(${theme}):展开逐段证据、查询结果、提问框也没有横向溢出`, o.page <= 0 && o.box <= 1, JSON.stringify(o))
const ratios = {
unrecorded: await ratioOf(page, '[data-inquiry-unrecorded]'),
limit: await ratioOf(page, '[data-inquiry-limit]'),
failed: await ratioOf(page, '[data-inquiry-failed]'),
ask: await ratioOf(page, '[data-inquiry-ask="u1"] > span'),
judged: await ratioOf(page, '[data-inquiry-judged]'),
meta: await ratioOf(page, '[data-inquiry-meta]'),
}
check(`${theme}:没写入、触顶、没答成的说明,「质询」,模型说明对比度都 ≥ 4.5`, Object.values(ratios).every((r) => r >= 4.5),
JSON.stringify(Object.fromEntries(Object.entries(ratios).map(([k, v]) => [k, Number(v.toFixed(2))]))))
await shot(page, `narrow-${theme}`)
// 报错那一行
state.reply = ERRORS.inquiry_busy
await page.locator('[data-inquiry-input]').fill('再问一句')
await page.locator('[data-inquiry-submit]').click()
await page.locator('[data-inquiry-error]').waitFor()
const r = await ratioOf(page, '[data-inquiry-error]')
const o2 = await overflow(page, '[data-approval]')
check(`390px(${theme}):报错说明不横向溢出、对比度 ≥ 4.5`, o2.page <= 0 && o2.box <= 1 && r >= 4.5, `${JSON.stringify(o2)} · ${r.toFixed(2)}`)
check(`390px(${theme}):页面没有报错`, errors.length === 0, errors.join(';'))
await ctx.close()
}
{
const { ctx, page, errors } = await open('inquiry-approval-dense', { width: 1100, height: 900 })
await page.locator('[data-inquiry-entry="34"] [data-inquiry-answer-doc]').waitFor()
await expand(page)
await page.locator('[data-inquiry-entry="31"] [data-inquiry-cite="Q1"]').first().click()
await page.locator('[data-inquiry-entry="31"] [data-inquiry-snapshot="ok"]').waitFor()
await clickAsk(page, 'u2')
await composer(page).waitFor()
const o = await overflow(page, '[data-fixture="inquiry-approval-dense"]')
check('360px 右栏:查询结果、提问框都不撑宽右栏', o.page <= 0 && o.box <= 0, JSON.stringify(o))
await shot(page, 'dense-dark')
check('360px 右栏:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
{
const { ctx, page, errors } = await open('inquiry-run-explore', { width: 390, height: 844, theme: 'light' })
await page.locator('[data-inquiry="write"] [data-inquiry-entry="58"]').waitFor()
const o = await overflow(page, '[data-evidence-main]')
check('390px 运行详情(light):报告和问答串不横向溢出', o.page <= 0 && o.box <= 0, JSON.stringify(o))
await shot(page, 'run-explore-narrow-light')
check('390px 运行详情:页面没有报错', errors.length === 0, errors.join(';'))
await ctx.close()
}
})
await browser.close()
console.log(failed ? `\n✗ 审批质询:${failed} 项未通过` : '\n✓ 审批质询:全部通过')
process.exit(failed ? 1 : 0)