mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 08:02:02 +00:00
合入期 3 反方审稿(前端):审批卡与证据里的反方审稿概况、逐句判断与理由、检查器
# Conflicts: # frontend/src/canvas/nodeDefs.ts # frontend/src/types.ts
This commit is contained in:
@@ -96,7 +96,7 @@ function summarize(type: NodeType, config: Record<string, any>): string {
|
||||
return config.workflow_id ? '嵌套工作流' : '未选择工作流'
|
||||
// 还是占位的两种:卡片上直接说尚未生效,免得以为放上去就会执行
|
||||
case 'challenge':
|
||||
return CHALLENGE_TEXT.pending
|
||||
return CHALLENGE_TEXT.summary(config)
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '../lib/terms'
|
||||
import type { NodeType } from '../types'
|
||||
import { dualCheckFields, dualCheckSummary } from './DualCheckFields'
|
||||
import { nodePickField, triStateField, upstreamModel, verifyModelFields } from './verifyFields'
|
||||
|
||||
export type FieldType =
|
||||
| 'text' | 'textarea' | 'prompt' | 'code' | 'number' | 'select' | 'switch'
|
||||
@@ -735,9 +736,44 @@ export const NODE_DEFS: Record<NodeType, NodeDef> = {
|
||||
},
|
||||
challenge: {
|
||||
type: 'challenge', label: NODE_TYPE_LABEL.challenge, category: '把关', icon: ShieldAlert,
|
||||
description: `${CHALLENGE_TEXT.description}。${CHALLENGE_TEXT.pending}`,
|
||||
description: CHALLENGE_TEXT.description,
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [],
|
||||
// 上限、模型、开关都可以不写:服务端按「设置 → 偏好设置 → 核查」里反方审稿那一组取(verify_settings)。
|
||||
// 没写的项不往配置里落默认值,设置改了,没单独配过的节点跟着变
|
||||
fields: [
|
||||
// 留空时先认直接连着的报告(「初稿 → 终稿 → 反方审稿」审终稿),同服务端 schema.challenge_report
|
||||
nodePickField({
|
||||
key: 'report', label: CHALLENGE_TEXT.field.report, nodeType: 'report', rule: 'direct', text: CHALLENGE_TEXT.reportPick,
|
||||
}),
|
||||
{
|
||||
key: 'max_claims', label: CHALLENGE_TEXT.field.max_claims, type: 'number', min: 1,
|
||||
placeholder: CHALLENGE_TEXT.followSettings, help: CHALLENGE_TEXT.help.max_claims,
|
||||
},
|
||||
// 和撰写报告的模型相同时提醒:同一个模型审自己写的结论,较难发现错误
|
||||
...verifyModelFields({
|
||||
labels: { provider: CHALLENGE_TEXT.field.provider, model: CHALLENGE_TEXT.field.model },
|
||||
text: CHALLENGE_TEXT.model, subject: upstreamModel('report', 'report', 'direct'),
|
||||
}),
|
||||
{
|
||||
// 没写等于 degrade:降档放第一项,没写时下拉框正好落在它上面
|
||||
key: 'on_refuted', label: CHALLENGE_TEXT.field.on_refuted, type: 'select',
|
||||
options: [
|
||||
{ value: 'degrade', label: CHALLENGE_TEXT.onRefuted.degrade },
|
||||
{ value: 'flag', label: CHALLENGE_TEXT.onRefuted.flag },
|
||||
{ value: 'fail', label: CHALLENGE_TEXT.onRefuted.fail },
|
||||
],
|
||||
help: CHALLENGE_TEXT.help.on_refuted, unknownLabel: CHALLENGE_TEXT.unknownValue,
|
||||
},
|
||||
triStateField({ key: 'enabled', label: CHALLENGE_TEXT.field.enabled, text: CHALLENGE_TEXT.enabled }),
|
||||
{
|
||||
key: 'max_queries', label: CHALLENGE_TEXT.field.max_queries, type: 'number', min: 1, advanced: true,
|
||||
placeholder: CHALLENGE_TEXT.followSettings, help: CHALLENGE_TEXT.help.max_queries,
|
||||
},
|
||||
{
|
||||
key: 'timeout_s', label: CHALLENGE_TEXT.field.timeout_s, type: 'number', min: 1, advanced: true,
|
||||
placeholder: CHALLENGE_TEXT.followSettings, help: CHALLENGE_TEXT.help.timeout_s,
|
||||
},
|
||||
],
|
||||
defaults: {},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,348 @@
|
||||
import { useMemo } from 'react'
|
||||
import type { Edge } from '@xyflow/react'
|
||||
import clsx from 'clsx'
|
||||
import { useRadioGroup } from '../components/ui'
|
||||
import { nodeTypeLabel } from '../lib/terms'
|
||||
import { modelOptions, providerOfModel, useCatalog } from '../store/catalog'
|
||||
import { useStudio, type FlowNode } from '../store/studio'
|
||||
import type { NodeType } from '../types'
|
||||
import { fieldOfIssue } from './issues'
|
||||
import type { FieldDef, FieldRenderProps } from './nodeDefs'
|
||||
|
||||
/**
|
||||
* 核查类节点(反方审稿、双盲复算)检查器里的几样通用控件,nodeDefs 用下面的工厂函数拼进字段表:
|
||||
* - nodePickField:从画布上挑本节点上游的某一类节点(要审的报告、要复算的口径卡),留空按缺省取上游的那一个;
|
||||
* - triStateField:跟随设置 / 开启 / 关闭三态(配置里没有这一项、true、false);
|
||||
* - verifyModelFields:核查模型的接入加模型,写法同结论句裁判的模型选择。
|
||||
*
|
||||
* 文案一律由调用方传进来(各节点自己那一块的 *_TEXT),控件里不写死任何一句话:两个节点说法不同(反方模型 / 盲算模型),
|
||||
* 控件是同一个。读画布节点用 studio store,不往 FieldRenderProps 里加字段。
|
||||
*
|
||||
* 本地提示(上游有好几个、模型相同……)只在画布校验还没在这一项上报问题时画:校验的话 Inspector 会挂在字段下面,
|
||||
* 再画一句就是同一件事说两遍(同结论句裁判「校验也会报,报了就不重复说」)。校验没回来、校验失败时照画。
|
||||
*/
|
||||
|
||||
/**
|
||||
* 画布校验在这个节点的这一项上报了问题没有。认法和 Inspector 把问题挂到字段下面时一样(fieldOfIssue),
|
||||
* 两边说的「这一项有问题」不会对不上
|
||||
*/
|
||||
function useReported(nodeId: string, key: string): boolean {
|
||||
return useStudio((s) => {
|
||||
const node = s.nodes.find((n) => n.id === nodeId)
|
||||
return s.issues.some((i) => i.node_id === nodeId && fieldOfIssue(i, node)?.key === key)
|
||||
})
|
||||
}
|
||||
|
||||
/** 节点在下拉里的叫法:名字和 id 不同时把 id 带上(同 NodeRefsInput) */
|
||||
function nodeLabel(n: FlowNode): string {
|
||||
const label = String(n.data?.label || n.id)
|
||||
return label === n.id ? label : `${label}(${n.id})`
|
||||
}
|
||||
|
||||
/** 本节点的全部上游(沿连线往回找)。只认上游:下游的节点运行到这里时还没有产出 */
|
||||
function upstreamOf(nodeId: string, edges: Edge[]): Set<string> {
|
||||
const parents = new Map<string, string[]>()
|
||||
for (const e of edges) parents.set(e.target, [...(parents.get(e.target) ?? []), e.source])
|
||||
const seen = new Set<string>()
|
||||
const stack = [...(parents.get(nodeId) ?? [])]
|
||||
while (stack.length) {
|
||||
const id = stack.pop()!
|
||||
if (seen.has(id) || id === nodeId) continue
|
||||
seen.add(id)
|
||||
stack.push(...(parents.get(id) ?? []))
|
||||
}
|
||||
return seen
|
||||
}
|
||||
|
||||
/**
|
||||
* 留空时怎么认,照各节点服务端的认法:
|
||||
* - 'upstream':整个上游里恰好一个才取它(双盲复算的口径卡,schema.dual_check_card);
|
||||
* - 'direct':先看直接连着的,恰好一个就是它,否则再看整个上游(反方审稿的报告,schema.challenge_report:
|
||||
* 「初稿 → 终稿 → 反方审稿」审终稿,不让人在初稿、终稿之间选)
|
||||
*/
|
||||
export type PickRule = 'upstream' | 'direct'
|
||||
|
||||
/**
|
||||
* 配置里那一项按服务端的认法落到哪个节点上:target 是定下来的那一个;其余几种是定不下来的原因——写了的节点不存在、
|
||||
* 不是这一类、不在上游(先后同服务端),留空时上游一个都没有、不止一个
|
||||
*/
|
||||
export type PickState =
|
||||
| { kind: 'target'; node: FlowNode }
|
||||
| { kind: 'missing' }
|
||||
| { kind: 'wrongType'; node: FlowNode }
|
||||
| { kind: 'notUpstream'; node: FlowNode }
|
||||
| { kind: 'none' }
|
||||
| { kind: 'many'; count: number }
|
||||
|
||||
export function resolvePick(
|
||||
nodeId: string, chosen: unknown, type: NodeType, rule: PickRule, nodes: FlowNode[], edges: Edge[],
|
||||
): PickState {
|
||||
const up = upstreamOf(nodeId, edges)
|
||||
if (typeof chosen === 'string' && chosen) {
|
||||
const node = nodes.find((n) => n.id === chosen)
|
||||
if (!node) return { kind: 'missing' }
|
||||
if (node.data?.nodeType !== type) return { kind: 'wrongType', node }
|
||||
return up.has(node.id) ? { kind: 'target', node } : { kind: 'notUpstream', node }
|
||||
}
|
||||
if (rule === 'direct') {
|
||||
const direct = new Set(edges.filter((e) => e.target === nodeId).map((e) => e.source))
|
||||
const near = nodes.filter((n) => direct.has(n.id) && n.data?.nodeType === type)
|
||||
if (near.length === 1) return { kind: 'target', node: near[0] }
|
||||
}
|
||||
const candidates = nodes.filter((n) => up.has(n.id) && n.data?.nodeType === type)
|
||||
return candidates.length === 1 ? { kind: 'target', node: candidates[0] }
|
||||
: candidates.length ? { kind: 'many', count: candidates.length } : { kind: 'none' }
|
||||
}
|
||||
|
||||
/** 这个节点实际要核查的是哪一个;定不下来(服务端这时报错、不核查)时返回 undefined */
|
||||
export function targetNode(
|
||||
nodeId: string, chosen: unknown, type: NodeType, rule: PickRule, nodes: FlowNode[], edges: Edge[],
|
||||
): FlowNode | undefined {
|
||||
const pick = resolvePick(nodeId, chosen, type, rule, nodes, edges)
|
||||
return pick.kind === 'target' ? pick.node : undefined
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 选画布上的节点
|
||||
|
||||
export interface NodePickText {
|
||||
/** 留空那一项:按缺省能认出是哪一个时给出它的名字 */
|
||||
auto: (only: string | null) => string
|
||||
/** 上游一个都没有 */
|
||||
none: string
|
||||
/** 上游不止一个、又没有选 */
|
||||
many: (n: number) => string
|
||||
/** 选中的节点已不在画布上(下拉里那一项的叫法) */
|
||||
missing: (id: string) => string
|
||||
/** 选中的节点不在上游(下拉里那一项的叫法) */
|
||||
notUpstream: (label: string) => string
|
||||
/** 选中的节点不是这一类(下拉里那一项的叫法) */
|
||||
wrongType: (label: string) => string
|
||||
missingHint: string
|
||||
notUpstreamHint: string
|
||||
/** type 是选中节点的类型名(「输入」) */
|
||||
wrongTypeHint: (type: string) => string
|
||||
}
|
||||
|
||||
function NodePick({ p, fieldKey, nodeType, rule, text }: {
|
||||
p: FieldRenderProps; fieldKey: string; nodeType: NodeType; rule: PickRule; text: NodePickText
|
||||
}) {
|
||||
const nodes = useStudio((s) => s.nodes)
|
||||
const edges = useStudio((s) => s.edges)
|
||||
const reported = useReported(p.nodeId, fieldKey)
|
||||
const value = typeof p.value === 'string' ? p.value : ''
|
||||
const options = useMemo(() => {
|
||||
const up = upstreamOf(p.nodeId, edges)
|
||||
return nodes.filter((n) => up.has(n.id) && n.data?.nodeType === nodeType)
|
||||
}, [nodes, edges, p.nodeId, nodeType])
|
||||
const pick = useMemo(
|
||||
() => resolvePick(p.nodeId, value, nodeType, rule, nodes, edges), [p.nodeId, value, nodeType, rule, nodes, edges],
|
||||
)
|
||||
// 选中的节点被删了、不是这一类、挪到了下游:下拉里照实多列一项(选不回去),不让它看着像「未选择」。
|
||||
// 校验已经报了的,这一项照样列(看得出选的是什么),只是下面那句提示不再说
|
||||
const stale = pick.kind === 'missing' || pick.kind === 'wrongType' || pick.kind === 'notUpstream' ? pick : null
|
||||
const hint = reported ? ''
|
||||
: pick.kind === 'missing' ? text.missingHint
|
||||
: pick.kind === 'wrongType' ? text.wrongTypeHint(nodeTypeLabel(pick.node.data?.nodeType))
|
||||
: pick.kind === 'notUpstream' ? text.notUpstreamHint
|
||||
: pick.kind === 'none' ? text.none
|
||||
: pick.kind === 'many' ? text.many(pick.count) : ''
|
||||
const hintId = `${p.id}-hint`
|
||||
const only = !value && pick.kind === 'target' ? String(pick.node.data?.label || pick.node.id) : null
|
||||
return (
|
||||
<>
|
||||
<select id={p.id} className="field" value={value} disabled={p.disabled} aria-invalid={p.invalid || undefined}
|
||||
aria-describedby={[p.describedBy, hint ? hintId : ''].filter(Boolean).join(' ') || undefined}
|
||||
style={p.invalid || stale ? { borderColor: p.invalid ? 'var(--err)' : 'var(--warn)' } : undefined}
|
||||
onChange={(e) => p.onChange(e.target.value || undefined)}>
|
||||
<option value="">{text.auto(only)}</option>
|
||||
{options.map((n) => <option key={n.id} value={n.id}>{nodeLabel(n)}</option>)}
|
||||
{stale && (
|
||||
<option value={value} disabled>
|
||||
{stale.kind === 'missing' ? text.missing(value)
|
||||
: stale.kind === 'wrongType' ? text.wrongType(nodeLabel(stale.node)) : text.notUpstream(nodeLabel(stale.node))}
|
||||
</option>
|
||||
)}
|
||||
</select>
|
||||
{hint && (
|
||||
<div id={hintId} className="mt-1 text-2xs leading-snug" data-node-pick-hint={pick.kind} style={{ color: 'var(--st-waiting)' }}>
|
||||
{hint}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从画布上挑本节点上游的某一类节点。配置里存节点 id;留空(写回 undefined,不留空字符串)时服务端按 rule 取缺省的那一个
|
||||
*/
|
||||
export function nodePickField(def: {
|
||||
key: string; label: string; nodeType: NodeType; rule?: PickRule; text: NodePickText; help?: string; advanced?: boolean
|
||||
}): FieldDef {
|
||||
return {
|
||||
key: def.key, label: def.label, type: 'select', help: def.help, advanced: def.advanced,
|
||||
render: (p) => <NodePick p={p} fieldKey={def.key} nodeType={def.nodeType} rule={def.rule ?? 'upstream'} text={def.text} />,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 三态开关
|
||||
|
||||
export interface TriStateText {
|
||||
follow: string
|
||||
on: string
|
||||
off: string
|
||||
/** 选中各态时下面那句说明:跟随的是哪一项设置、关掉之后会怎样 */
|
||||
hint: { follow: string; on: string; off: string }
|
||||
}
|
||||
|
||||
type Tri = 'follow' | 'on' | 'off'
|
||||
const TRI: readonly Tri[] = ['follow', 'on', 'off']
|
||||
|
||||
function TriState({ p, label, text }: { p: FieldRenderProps; label: string; text: TriStateText }) {
|
||||
// 只认真正的布尔:手写的 "false"、0 不当成关闭,按跟随设置显示(服务端校验会报出来)
|
||||
const state: Tri = p.value === true ? 'on' : p.value === false ? 'off' : 'follow'
|
||||
// 跟随设置写回 undefined:配置里不留这一项,和没配过的节点一样
|
||||
const set = (t: Tri) => p.onChange(t === 'on' ? true : t === 'off' ? false : undefined)
|
||||
const radio = useRadioGroup(TRI, state, set)
|
||||
const hintId = `${p.id}-hint`
|
||||
return (
|
||||
<>
|
||||
{/* 字段标签的 for 指不到一组按钮,组名另写在 aria-label 上 */}
|
||||
<div id={p.id} role="radiogroup" aria-label={label} data-tri-state={state}
|
||||
aria-describedby={[hintId, p.describedBy].filter(Boolean).join(' ')}
|
||||
className="inline-flex max-w-full flex-wrap rounded-md border p-px">
|
||||
{TRI.map((t) => (
|
||||
<button key={t} type="button" {...radio(t)} disabled={p.disabled} data-tri={t} onClick={() => set(t)}
|
||||
className={clsx('rounded px-2 py-0.5 text-2xs', state === t ? 'bg-accent-soft text-fg' : 'text-faint hover:text-dim')}>
|
||||
{text[t]}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div id={hintId} className="mt-1 text-2xs leading-snug text-faint" data-tri-hint={state}>{text.hint[state]}</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** 跟随设置 / 开启 / 关闭:配置里没有这一项(或 null)/ true / false */
|
||||
export function triStateField(def: { key: string; label: string; text: TriStateText; advanced?: boolean }): FieldDef {
|
||||
return {
|
||||
key: def.key, label: def.label, type: 'select', advanced: def.advanced,
|
||||
render: (p) => <TriState p={p} label={def.label} text={def.text} />,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 核查模型
|
||||
|
||||
export interface VerifyModelText {
|
||||
/** 接入、模型都留空 */
|
||||
providerFollow: string
|
||||
/** 接入留空、模型填了:服务端按模型名找接入,不再跟随设置 */
|
||||
providerFromModel: string
|
||||
providerMissing: (name: string) => string
|
||||
/** 接在已停用的接入名后面 */
|
||||
providerDisabled: string
|
||||
/** 模型框的占位:接入也没选 / 选了接入 */
|
||||
modelFollow: string
|
||||
modelDefault: string
|
||||
/** 取值链的说明,常显在模型框下面 */
|
||||
help: string
|
||||
byModel: (model: string, found: { name: string; enabled: boolean } | null | undefined, fallback: string | null | undefined) => string
|
||||
/** 和被核查的模型是同一个 */
|
||||
same: (model: string) => string
|
||||
}
|
||||
|
||||
/** 被核查的那个模型:节点上明确写着的模型名;说不准(没写、跟随默认)时为 null,不提醒 */
|
||||
export type SubjectModel = (nodeId: string, config: Record<string, any>, graph: { nodes: FlowNode[]; edges: Edge[] }) => string | null
|
||||
|
||||
function ProviderPick({ p, text }: { p: FieldRenderProps; text: VerifyModelText }) {
|
||||
const providers = useCatalog((s) => s.providers)
|
||||
const provider = typeof p.value === 'string' ? p.value : ''
|
||||
const model = typeof p.config.model === 'string' ? p.config.model.trim() : ''
|
||||
const known = provider ? providers.find((x) => x.name === provider) : undefined
|
||||
return (
|
||||
<select id={p.id} className="field" value={provider} disabled={p.disabled} aria-invalid={p.invalid || undefined}
|
||||
aria-describedby={p.describedBy} style={p.invalid ? { borderColor: 'var(--err)' } : undefined}
|
||||
onChange={(e) => p.onChange(e.target.value || undefined)}>
|
||||
<option value="">{model ? text.providerFromModel : text.providerFollow}</option>
|
||||
{/* 接入列表还没取回来时照写名字,免得落到第一项、看着像跟随设置 */}
|
||||
{provider && !known && <option value={provider}>{providers.length ? text.providerMissing(provider) : provider}</option>}
|
||||
{providers.map((x) => (
|
||||
<option key={x.id} value={x.name}>{x.name}{x.enabled ? '' : text.providerDisabled}</option>
|
||||
))}
|
||||
</select>
|
||||
)
|
||||
}
|
||||
|
||||
function ModelInput({ p, fieldKey, text, subject }: {
|
||||
p: FieldRenderProps; fieldKey: string; text: VerifyModelText; subject?: SubjectModel
|
||||
}) {
|
||||
const providers = useCatalog((s) => s.providers)
|
||||
// 选择器只返回一个字符串:画布上别的节点改动不让这一栏重渲染
|
||||
const against = useStudio((s) => (subject ? subject(p.nodeId, p.config, s) : null))
|
||||
// 校验也会报模型相同(challenge.same_model 之类),报了就不重复说
|
||||
const reported = useReported(p.nodeId, fieldKey)
|
||||
const provider = typeof p.config.provider === 'string' ? p.config.provider : ''
|
||||
const model = typeof p.value === 'string' ? p.value : ''
|
||||
const name = model.trim()
|
||||
const pv = provider ? providers.find((x) => x.name === provider) : undefined
|
||||
const models = pv ? (pv.models ?? []).map((m) => m.id) : [...new Set(modelOptions(providers).map((o) => o.value))]
|
||||
// 只填了模型:这一级写了就整组用节点上的(verify_model_spec 不跨级拼),服务端按模型名找接入
|
||||
const byModel = !provider && !!name
|
||||
const owner = byModel ? providerOfModel(providers, name) : undefined
|
||||
const enabled = providers.filter((x) => x.enabled)
|
||||
const fallback = enabled.find((x) => x.kind !== 'mock') ?? enabled[0]
|
||||
const byModelHint = byModel && providers.length > 0
|
||||
? text.byModel(name, owner && { name: owner.provider.name, enabled: owner.enabled }, fallback?.name) : ''
|
||||
const same = !!name && !!against && name === against && !reported
|
||||
return (
|
||||
<>
|
||||
<input id={p.id} className="field mono text-xs" list={`${p.id}-models`} value={model} spellCheck={false}
|
||||
disabled={p.disabled} aria-invalid={p.invalid || undefined} aria-describedby={p.describedBy}
|
||||
placeholder={provider ? text.modelDefault : text.modelFollow}
|
||||
style={p.invalid ? { borderColor: 'var(--err)' } : undefined}
|
||||
onChange={(e) => p.onChange(e.target.value.trim() ? e.target.value : undefined)} />
|
||||
<datalist id={`${p.id}-models`}>{models.map((m) => <option key={m} value={m} />)}</datalist>
|
||||
{byModelHint && (
|
||||
<div className="mt-1 text-2xs leading-snug" data-verify-by-model={owner ? (owner.enabled ? 'found' : 'disabled') : 'default'}
|
||||
style={owner?.enabled ? undefined : { color: 'var(--st-waiting)' }}>
|
||||
<span className={owner?.enabled ? 'text-faint' : undefined}>{byModelHint}</span>
|
||||
</div>
|
||||
)}
|
||||
{same && (
|
||||
<div className="mt-1 text-2xs leading-snug" style={{ color: 'var(--st-waiting)' }} data-verify-same-model="">
|
||||
{text.same(name)}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-1 text-2xs leading-snug text-faint">{text.help}</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 核查模型:接入、模型两个字段(配置里的 provider、model)。都留空时用设置里的核查模型;写了任一项就整组以节点为准。
|
||||
* subject 给出被核查的那个模型,相同时提醒(等于自己审自己)
|
||||
*/
|
||||
export function verifyModelFields(def: {
|
||||
labels: { provider: string; model: string }; text: VerifyModelText; subject?: SubjectModel; advanced?: boolean
|
||||
}): FieldDef[] {
|
||||
return [
|
||||
{
|
||||
key: 'provider', label: def.labels.provider, type: 'select', advanced: def.advanced,
|
||||
render: (p) => <ProviderPick p={p} text={def.text} />,
|
||||
},
|
||||
{
|
||||
key: 'model', label: def.labels.model, type: 'model', advanced: def.advanced,
|
||||
render: (p) => <ModelInput p={p} fieldKey="model" text={def.text} subject={def.subject} />,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 被核查节点上写着的模型:refKey 是本节点配置里指向它的那一项(反方审稿的 report),没写时按 rule 认缺省的那一个
|
||||
* (和 nodePickField 用同一个 rule,两处认的是同一个节点)。定不下来时服务端只报选报告的错、不比模型,这里也不提醒
|
||||
*/
|
||||
export function upstreamModel(refKey: string, nodeType: NodeType, rule: PickRule = 'upstream'): SubjectModel {
|
||||
return (nodeId, config, { nodes, edges }) => {
|
||||
const model = targetNode(nodeId, config[refKey], nodeType, rule, nodes, edges)?.data?.config?.model
|
||||
return typeof model === 'string' && model.trim() ? model.trim() : null
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,212 @@
|
||||
import { useState, type JSX } from 'react'
|
||||
import { ReactFlow, ReactFlowProvider } from '@xyflow/react'
|
||||
import { Inspector } from '../../canvas/Inspector'
|
||||
import { NodeCard } from '../../canvas/NodeCard'
|
||||
import { ApprovalCard } from '../../run/RunPanel'
|
||||
import { useCatalog } from '../../store/catalog'
|
||||
import { useStudio } from '../../store/studio'
|
||||
import type { Approval, ApprovalChallenge, ApprovalReport, Provider, Workflow } from '../../types'
|
||||
import fx from './challenge.json'
|
||||
|
||||
/**
|
||||
* 反方审稿的预览用例,scripts/check-challenge.mjs 按它们检查。夹具在 challenge.json(检查脚本读同一份):理由文档是后端
|
||||
* run_verify_agent + compose_doc 真跑出来的,库、表名、地名、数字都是虚构的。
|
||||
*
|
||||
* 审批卡(payload.reports[0].challenge):
|
||||
* - ?syn=challenge-approval:正式运行,没写 on_refuted(按降档),四种判断各一句,有被推翻的 →「出具会降档」
|
||||
* - ?syn=challenge-approval-flag:正式运行,只标注,不写降档
|
||||
* - ?syn=challenge-approval-explore:探索运行,不说任何和出具有关的话
|
||||
* - ?syn=challenge-approval-none:审的报告没有经过反方审稿,什么都不画
|
||||
* - ?syn=challenge-approval-many:十二句,条目只列前十句,「显示全部」从工件里补齐
|
||||
* - ?syn=challenge-approval-broken:工件取不到(检查脚本先答 404,再改成能取到,测重试)
|
||||
* - ?syn=challenge-approval-dense:同第一个,摆在 360px 的右栏里
|
||||
* - ?syn=challenge-approval-clear / -clear-flag:正式运行,没有被推翻的结论(降档 / 只标注),两种都不写降档、不写只标注
|
||||
* - ?syn=challenge-approval-explore-flag:探索运行配只标注,同样不说任何和出具有关的话
|
||||
* - ?syn=challenge-approval-runfail:运行类别取不到(检查脚本先答 500),说明并可重试
|
||||
* - ?syn=challenge-approval-dup:同 many,工件里补出来的一句和条目里的一句原文相同、unit 不同(工件由检查脚本合成)
|
||||
* - ?syn=challenge-approval-norecord:同 many,审稿记录没存上(artifact 为 null)
|
||||
*
|
||||
* 运行类别(GET /api/runs/{id})、工件(GET /api/artifacts/{id})都由检查脚本用 page.route 伪造;直接打开时取不到,
|
||||
* 卡上照实不说降档、点开一句说无法获取。
|
||||
*
|
||||
* 检查器:选中反方审稿节点,旁边是同一张图的节点卡片和节点配置的实时内容。
|
||||
* - ?syn=challenge-inspector:两个上游报告、一个下游报告
|
||||
* - ?syn=challenge-inspector-chain:「初稿 → 终稿 → 反方审稿」,留空时审直接连着的终稿,反方模型和终稿的模型相同
|
||||
*/
|
||||
|
||||
const report = fx.report as unknown as ApprovalReport
|
||||
const challenge = fx.challenge as unknown as ApprovalChallenge
|
||||
|
||||
function approvalOf(id: string, runId: string, runClass: 'formal' | 'exploratory', reviewed: ApprovalReport): Approval {
|
||||
return {
|
||||
id, run_id: runId, node_id: 'review', mode: 'approve', title: '本月月报可以发出吗?', status: 'pending', response: {},
|
||||
payload: { kind: 'human_node', message: '请审核本月月报。', reports: [reviewed] },
|
||||
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>
|
||||
)
|
||||
}
|
||||
|
||||
const RUNS = fx.runs
|
||||
const withChallenge = (c: Partial<ApprovalChallenge>): ApprovalReport => ({ ...report, challenge: { ...challenge, ...c } })
|
||||
const many = fx.many as unknown as ApprovalChallenge
|
||||
/** 没有被推翻的结论:同第一个用例去掉被推翻的那句,计数跟着改 */
|
||||
const clear: Partial<ApprovalChallenge> = {
|
||||
counts: { stands: 1, weakened: 1, refuted: 0, inconclusive: 1 },
|
||||
items: challenge.items.filter((it) => it.verdict !== 'refuted'),
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 检查器
|
||||
|
||||
const PROVIDERS = [
|
||||
{
|
||||
id: 'pv-main', name: '主力接入', kind: 'anthropic', base_url: null, default_model: 'writer-large',
|
||||
models: [{ id: 'writer-large' }, { id: 'critic-large' }], enabled: true, extra: {}, api_key_masked: '', has_key: true,
|
||||
},
|
||||
{
|
||||
id: 'pv-spare', name: '备用接入', kind: 'openai', base_url: null, default_model: 'spare-mini',
|
||||
models: [{ id: 'spare-mini' }], enabled: false, extra: {}, api_key_masked: '', has_key: true,
|
||||
},
|
||||
] as Provider[]
|
||||
|
||||
const at = (x: number, y: number) => ({ x, y })
|
||||
const WORKFLOW = {
|
||||
id: 'wf-challenge-preview', name: '月报(预览)', description: '', tags: [], version: 1, is_template: false, status: 'draft',
|
||||
graph: {
|
||||
nodes: [
|
||||
{ id: 'input', type: 'input', position: at(0, 120), data: { label: '输入', config: { fields: [{ name: 'month', required: true }] } } },
|
||||
{ id: 'write', type: 'report', position: at(260, 0), data: { label: '写月报', config: { instructions: '写本月月报', model: 'writer-large' } } },
|
||||
{ id: 'write2', type: 'report', position: at(260, 240), data: { label: '写周报', config: { instructions: '写本周周报' } } },
|
||||
{ id: 'critic', type: 'challenge', position: at(540, 120), data: { label: '反方审稿', config: {} } },
|
||||
{ id: 'later', type: 'report', position: at(820, 120), data: { label: '写摘要', config: { instructions: '写一段摘要' } } },
|
||||
{ id: 'out', type: 'output', position: at(1100, 120), data: { label: '输出', config: {} } },
|
||||
],
|
||||
edges: [
|
||||
{ source: 'input', target: 'write' }, { source: 'input', target: 'write2' },
|
||||
{ source: 'write', target: 'critic' }, { source: 'write2', target: 'critic' },
|
||||
{ source: 'critic', target: 'later' }, { source: 'later', target: 'out' },
|
||||
],
|
||||
},
|
||||
} as unknown as Workflow
|
||||
|
||||
/** 链式接法:初稿、终稿都在上游,直接连着的只有终稿。终稿和反方审稿写的是同一个模型 */
|
||||
const CHAIN = {
|
||||
id: 'wf-challenge-chain', name: '月报(链式,预览)', description: '', tags: [], version: 1, is_template: false, status: 'draft',
|
||||
graph: {
|
||||
nodes: [
|
||||
{ id: 'input', type: 'input', position: at(0, 120), data: { label: '输入', config: { fields: [{ name: 'month', required: true }] } } },
|
||||
{ id: 'draft', type: 'report', position: at(260, 120), data: { label: '初稿', config: { instructions: '写初稿', model: 'spare-mini' } } },
|
||||
{ id: 'final', type: 'report', position: at(520, 120), data: { label: '终稿', config: { instructions: '改成终稿', model: 'writer-large' } } },
|
||||
{ id: 'critic', type: 'challenge', position: at(780, 120), data: { label: '反方审稿', config: { model: 'writer-large' } } },
|
||||
{ id: 'out', type: 'output', position: at(1040, 120), data: { label: '输出', config: {} } },
|
||||
],
|
||||
edges: [
|
||||
{ source: 'input', target: 'draft' }, { source: 'draft', target: 'final' },
|
||||
{ source: 'final', target: 'critic' }, { source: 'critic', target: 'out' },
|
||||
],
|
||||
},
|
||||
} as unknown as Workflow
|
||||
|
||||
const nodeTypes = { card: NodeCard }
|
||||
|
||||
/** 同一张图的节点卡片:看反方审稿节点的摘要跟着配置变 */
|
||||
function MiniCanvas() {
|
||||
const nodes = useStudio((s) => s.nodes)
|
||||
const edges = useStudio((s) => s.edges)
|
||||
return (
|
||||
<ReactFlowProvider>
|
||||
<ReactFlow nodes={nodes} edges={edges} nodeTypes={nodeTypes} fitView nodesDraggable={false} proOptions={{ hideAttribution: true }} />
|
||||
</ReactFlowProvider>
|
||||
)
|
||||
}
|
||||
|
||||
function ConfigView() {
|
||||
const config = useStudio((s) => s.nodes.find((n) => n.id === 'critic')?.data.config)
|
||||
return (
|
||||
<pre className="mono max-h-40 overflow-auto whitespace-pre-wrap border-t p-2 text-2xs text-dim [overflow-wrap:anywhere]"
|
||||
data-challenge-config="">
|
||||
{JSON.stringify(config ?? null, null, 1)}
|
||||
</pre>
|
||||
)
|
||||
}
|
||||
|
||||
function InspectorCase({ name, workflow }: { name: string; workflow: Workflow }) {
|
||||
// 首次渲染前装好图和接入:检查器、卡片一挂上就是选中反方审稿节点的样子
|
||||
useState(() => {
|
||||
useCatalog.setState({ providers: PROVIDERS })
|
||||
useStudio.getState().load(workflow)
|
||||
useStudio.getState().select('critic')
|
||||
return true
|
||||
})
|
||||
return (
|
||||
<div className="flex min-h-screen flex-col md:h-screen md:flex-row" data-fixture={name}>
|
||||
<div className="w-full shrink-0 border-b bg-panel md:h-full md:w-[360px] md:border-b-0 md:border-r" data-challenge-inspector="">
|
||||
<Inspector />
|
||||
</div>
|
||||
<div className="flex min-h-0 min-w-0 flex-1 flex-col">
|
||||
<div className="h-72 min-h-0 md:h-auto md:flex-1"><MiniCanvas /></div>
|
||||
<ConfigView />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const cases: Record<string, () => JSX.Element> = {
|
||||
'challenge-approval': () => (
|
||||
<Card name="challenge-approval" approval={approvalOf('ap-chal-1', RUNS.formal, 'formal', withChallenge({}))} />
|
||||
),
|
||||
'challenge-approval-flag': () => (
|
||||
<Card name="challenge-approval-flag"
|
||||
approval={approvalOf('ap-chal-2', RUNS.flag, 'formal', withChallenge({ on_refuted: 'flag' }))} />
|
||||
),
|
||||
'challenge-approval-explore': () => (
|
||||
<Card name="challenge-approval-explore"
|
||||
approval={approvalOf('ap-chal-3', RUNS.explore, 'exploratory', withChallenge({ on_refuted: 'degrade' }))} />
|
||||
),
|
||||
'challenge-approval-none': () => (
|
||||
<Card name="challenge-approval-none" approval={approvalOf('ap-chal-4', RUNS.none, 'formal', report)} />
|
||||
),
|
||||
'challenge-approval-many': () => (
|
||||
<Card name="challenge-approval-many" approval={approvalOf('ap-chal-5', RUNS.many, 'formal', { ...report, challenge: many })} />
|
||||
),
|
||||
'challenge-approval-broken': () => (
|
||||
<Card name="challenge-approval-broken"
|
||||
approval={approvalOf('ap-chal-6', RUNS.broken, 'formal', withChallenge({ artifact: 'e'.repeat(64) }))} />
|
||||
),
|
||||
'challenge-approval-dense': () => (
|
||||
<Card name="challenge-approval-dense" dense approval={approvalOf('ap-chal-7', RUNS.formal, 'formal', withChallenge({}))} />
|
||||
),
|
||||
'challenge-approval-clear': () => (
|
||||
<Card name="challenge-approval-clear" approval={approvalOf('ap-chal-8', RUNS.clear, 'formal', withChallenge(clear))} />
|
||||
),
|
||||
'challenge-approval-clear-flag': () => (
|
||||
<Card name="challenge-approval-clear-flag"
|
||||
approval={approvalOf('ap-chal-9', RUNS.clearFlag, 'formal', withChallenge({ ...clear, on_refuted: 'flag' }))} />
|
||||
),
|
||||
'challenge-approval-explore-flag': () => (
|
||||
<Card name="challenge-approval-explore-flag"
|
||||
approval={approvalOf('ap-chal-10', RUNS.exploreFlag, 'exploratory', withChallenge({ on_refuted: 'flag' }))} />
|
||||
),
|
||||
'challenge-approval-runfail': () => (
|
||||
<Card name="challenge-approval-runfail" approval={approvalOf('ap-chal-11', RUNS.runfail, 'formal', withChallenge({}))} />
|
||||
),
|
||||
'challenge-approval-dup': () => (
|
||||
<Card name="challenge-approval-dup"
|
||||
approval={approvalOf('ap-chal-12', RUNS.dup, 'formal', { ...report, challenge: { ...many, artifact: 'd'.repeat(64) } })} />
|
||||
),
|
||||
'challenge-approval-norecord': () => (
|
||||
<Card name="challenge-approval-norecord"
|
||||
approval={approvalOf('ap-chal-13', RUNS.norecord, 'formal', { ...report, challenge: { ...many, artifact: null } })} />
|
||||
),
|
||||
'challenge-inspector': () => <InspectorCase name="challenge-inspector" workflow={WORKFLOW} />,
|
||||
'challenge-inspector-chain': () => <InspectorCase name="challenge-inspector-chain" workflow={CHAIN} />,
|
||||
}
|
||||
+104
-3
@@ -3598,10 +3598,111 @@ export const DUAL_CHECK_TEXT = {
|
||||
},
|
||||
}
|
||||
|
||||
/** 反方审稿节点:节点库说明、检查器和运行结果 */
|
||||
/**
|
||||
* 反方审稿节点:节点库说明、检查器、节点卡片摘要,以及审批卡上的反方审稿(run/ChallengeSummary.tsx)。
|
||||
* 四种判断全站只有这一种叫法:经受住、被削弱、被推翻、无法判断。判断由反方模型给出,不是系统核对。
|
||||
* 「出具会降档」只在正式运行、有被推翻的结论、按降档处理时才说;探索运行不出具,不说任何和出具有关的话;
|
||||
* 运行类别取不到时照实说取不到
|
||||
*/
|
||||
export const CHALLENGE_TEXT = {
|
||||
description: '从反方立场审阅上游报告,逐条提出质疑',
|
||||
pending: `${PENDING_TEXT}:本节点暂未生效,运行时将跳过`,
|
||||
description: '从反方立场复核上游报告的关键结论:另一个模型只读查库,逐句判断经受住、被削弱、被推翻还是无法判断',
|
||||
verdict: { stands: '经受住', weakened: '被削弱', refuted: '被推翻', inconclusive: '无法判断' },
|
||||
/** 服务端给了一个界面不认识的判断:照写原值,不归到哪一类 */
|
||||
verdictUnknown: (v: string) => `未知判断「${v}」`,
|
||||
|
||||
// ---- 节点卡片的摘要。report 是节点 id:卡片摘要只看配置 ----
|
||||
summary: (c: { report?: unknown; max_claims?: unknown; enabled?: unknown }) => [
|
||||
c.enabled === false ? '已关闭' : '',
|
||||
typeof c.report === 'string' && c.report.trim() ? `审报告节点 ${c.report.trim()}` : '审上游报告',
|
||||
typeof c.max_claims === 'number' && c.max_claims > 0 ? `最多审 ${formatNumber(c.max_claims)} 句结论` : '句数跟随设置',
|
||||
].filter(Boolean).join(' · '),
|
||||
|
||||
// ---- 检查器 ----
|
||||
field: {
|
||||
report: '要审的报告',
|
||||
max_claims: '最多审几句',
|
||||
max_queries: '每句最多查询次数',
|
||||
timeout_s: '每句时长上限(秒)',
|
||||
provider: '反方模型 · 接入',
|
||||
model: '反方模型',
|
||||
on_refuted: '结论被推翻时',
|
||||
enabled: '启用',
|
||||
},
|
||||
followSettings: '跟随设置',
|
||||
help: {
|
||||
max_claims: '每份报告最多挑几句关键结论来审。留空则使用「设置 → 偏好设置 → 核查」中的值',
|
||||
max_queries: '复核每句结论时最多执行几次查询。留空则使用「设置 → 偏好设置 → 核查」中的值',
|
||||
timeout_s: '复核每句结论的时长上限,到时按已查到的结果给出判断。留空则使用「设置 → 偏好设置 → 核查」中的值',
|
||||
on_refuted: '降档:被推翻的结论计入出具缺口,正式运行降档出具;只标注:在审批卡和运行记录中标出,不影响出具;'
|
||||
+ '报错:本节点失败,运行停止',
|
||||
},
|
||||
onRefuted: { degrade: '出具降档(默认)', flag: '只标注', fail: '报错并停止运行' },
|
||||
unknownValue: (v: string) => `${v}:无法识别的值,请从上方选项中选择`,
|
||||
reportPick: {
|
||||
auto: (only: string | null) => (only ? `未选择:审上游的「${only}」` : '未选择:审上游的报告撰写节点'),
|
||||
none: '上游没有报告撰写节点,请先在本节点之前连接一个报告撰写节点',
|
||||
many: (n: number) => `上游有 ${formatNumber(n)} 个报告撰写节点,请选择要审的那一个`,
|
||||
missing: (id: string) => `${id}(已不在画布上)`,
|
||||
notUpstream: (label: string) => `${label}(不在上游)`,
|
||||
wrongType: (label: string) => `${label}(不是报告撰写节点)`,
|
||||
missingHint: '所选节点已不在画布上,请重新选择',
|
||||
notUpstreamHint: '所选节点不在本节点上游,运行到这里时它还没有写出报告,请重新选择或调整连线',
|
||||
wrongTypeHint: (type: string) => `所选节点是「${type}」节点,不是报告撰写节点,请重新选择`,
|
||||
},
|
||||
enabled: {
|
||||
follow: '跟随设置', on: '开启', off: '关闭',
|
||||
hint: {
|
||||
follow: '使用「设置 → 偏好设置 → 核查」中「反方审稿 · 启用」的值',
|
||||
on: '无论设置如何都执行反方审稿',
|
||||
off: '不执行反方审稿,上游的结果原样交给下游,运行记录中记一条说明',
|
||||
},
|
||||
},
|
||||
model: {
|
||||
providerFollow: '跟随设置',
|
||||
providerFromModel: '按模型名找接入',
|
||||
providerMissing: (name: string) => `${name}(不存在)`,
|
||||
providerDisabled: '(已停用)',
|
||||
modelFollow: '跟随设置',
|
||||
modelDefault: '留空则使用该接入的默认模型',
|
||||
help: '接入和模型均未填写时,使用「设置 → 偏好设置 → 核查」中的核查模型;填写任一项后,整组以节点配置为准,'
|
||||
+ '不与设置合并。建议反方模型与撰写报告的模型不同,同一模型较难发现自身的错误',
|
||||
byModel: (model: string, found: { name: string; enabled: boolean } | null | undefined, fallback: string | null | undefined) => (
|
||||
found && !found.enabled
|
||||
? `只填了模型:「${model}」属于已停用的接入「${found.name}」,调用将失败。请启用该接入或选择其他接入`
|
||||
: found
|
||||
? `只填了模型:将按模型名使用接入「${found.name}」。接入和模型都未填写时,才跟随设置中的核查模型`
|
||||
: `只填了模型:所有接入的模型列表中都没有「${model}」,将使用默认接入${fallback ? `「${fallback}」` : ''}调用;`
|
||||
+ '如该模型不属于默认接入,请选择对应的接入'),
|
||||
same: (model: string) => `反方模型与撰写报告的模型相同(「${model}」),审稿效果有限`,
|
||||
},
|
||||
|
||||
// ---- 审批卡 ----
|
||||
head: (label: string) => `报告「${label}」的反方审稿`,
|
||||
count: (verdict: string, n: number) => `${verdict} ${formatNumber(n)} 句`,
|
||||
none: '报告中没有需要反方复核的关键结论',
|
||||
degrade: '出具会降档',
|
||||
degradeHint: '被推翻的结论计入出具缺口,本次正式运行将降档出具',
|
||||
flagNote: '按节点设置,被推翻的结论只做标注,不影响出具档位',
|
||||
/** 运行类别没取到:不知道是不是正式运行,照实说,并写出正式运行时的后果(旁边给重试) */
|
||||
runClassFailed: '暂时无法确认运行类别;如为正式运行,被推翻的结论会使出具降档',
|
||||
/** 审批里没带工件(服务端保存审稿记录失败):每句都看不了理由和查询,整块说一次 */
|
||||
noRecord: '反方审稿记录未保存,无法查看理由和查询',
|
||||
more: (n: number) => `另有 ${formatNumber(n)} 句未列出`,
|
||||
showAll: '显示全部',
|
||||
loading: '正在加载反方审稿…',
|
||||
failed: '无法获取反方审稿的详细内容',
|
||||
retry: '重试',
|
||||
claimMissing: '反方审稿记录中没有这一句的详细内容',
|
||||
judgedBy: (model: string | null) => `判断由反方模型${model ? `「${model}」` : ''}给出,并非系统核对`,
|
||||
rationale: '理由',
|
||||
rationaleEmpty: '反方没有给出理由',
|
||||
docLoading: '正在加载理由…',
|
||||
queries: (n: number) => `反方执行的查询(${formatNumber(n)} 次)`,
|
||||
noQueries: '反方没有执行查询',
|
||||
rows: (n: number) => `${formatNumber(n)} 行`,
|
||||
truncated: '结果已截断,数据库中还有更多数据',
|
||||
/** 理由里有出处的数字:读屏听到的是「数字,出处:查询 Q2 · 第 1 行 · amount」,点一下跳到那次查询 */
|
||||
cited: (text: string, source: string) => `${text},出处:${source},跳到这次查询`,
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import type { ApprovalReport } from '../types'
|
||||
import { Fragment, useCallback, useEffect, useId, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react'
|
||||
import { AlertTriangle, ChevronRight, ShieldAlert } from 'lucide-react'
|
||||
import { api } from '../api/client'
|
||||
import { EVIDENCE_STATE, segmentLabel, segmentState, sourceOf, unitText, type EvidenceStateCode } from '../lib/evidence'
|
||||
import { CHALLENGE_TEXT, EVIDENCE_STATE_LABEL, EVIDENCE_TEXT } from '../lib/terms'
|
||||
import { asDoc, useEvidence } from '../store/evidence'
|
||||
import type {
|
||||
ApprovalChallenge, ApprovalReport, ChallengeArtifact, ChallengeClaim, ChallengeCounts, ChallengeItem, ChallengeQuery,
|
||||
ChallengeVerdict, EvidenceBlock, EvidenceDocData, EvidenceSegment, EvidenceUnit,
|
||||
} from '../types'
|
||||
import { BlockView, CopyChip, Markdown, StyledSlice, inlineStyles, numericColumns } from './Markdown'
|
||||
|
||||
export interface ChallengeSummaryProps {
|
||||
/** 这张审批卡审的报告(payload.reports,和 ApprovalReports 拿到的是同一份) */
|
||||
@@ -7,11 +17,642 @@ export interface ChallengeSummaryProps {
|
||||
}
|
||||
|
||||
/**
|
||||
* 审批卡里反方审稿的摘要,紧跟在报告核对概况(ApprovalReports)后面。
|
||||
* 审批卡里反方审稿的摘要,紧跟在报告核对概况(ApprovalReports)后面:一行概况(四种判断各几句),逐句列出结论原文和判断,
|
||||
* 点开一句再按 challenge_artifact 取完整内容,看反方的理由和它执行过的查询。审稿记录没存上(没有工件)时只列判断、
|
||||
* 不能点开,整块说一次。
|
||||
*
|
||||
* 现在是占位,什么都不画;反方审稿的工作包在这里补上反驳意见的摘要。审的不是报告、或者报告没经过
|
||||
* 反方审稿时也应该什么都不画,卡片和以前一样。
|
||||
* 判断是反方模型给的,不是系统核对:颜色照结论句裁判的那一套,「经受住」也不用确定性的绿。
|
||||
* 「出具会降档」只在正式运行、有被推翻的结论、按降档处理时才写——只标注的不写降档,探索运行不出具,和出具有关的话一句不说。
|
||||
* 运行类别不由审批卡传进来,按运行 id 从证据 store 取(loadRunClass),取到之前不说;取不到时照实说取不到,可重试。
|
||||
*
|
||||
* 审的报告都没有经过反方审稿时什么都不画,也不发任何请求,卡片和以前一样。
|
||||
*/
|
||||
export function ChallengeSummary(_props: ChallengeSummaryProps) {
|
||||
return null
|
||||
export function ChallengeSummary({ reports, runId }: ChallengeSummaryProps) {
|
||||
const reviewed = useMemo(() => reports.flatMap((r) => {
|
||||
const challenge = challengeOf(r)
|
||||
return challenge ? [{ report: r, challenge }] : []
|
||||
}), [reports])
|
||||
if (!reviewed.length) return null
|
||||
return <ChallengeList reviewed={reviewed} runId={runId} />
|
||||
}
|
||||
|
||||
function ChallengeList({ reviewed, runId }: {
|
||||
reviewed: { report: ApprovalReport; challenge: ApprovalChallenge }[]; runId: string
|
||||
}) {
|
||||
const run = useRunClass(runId)
|
||||
return (
|
||||
<>
|
||||
{reviewed.map(({ report, challenge }) => (
|
||||
<ChallengeBlock key={report.node_id} report={report} challenge={challenge} run={run} />
|
||||
))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 认 payload
|
||||
|
||||
const VERDICTS: readonly ChallengeVerdict[] = ['stands', 'weakened', 'refuted', 'inconclusive']
|
||||
const isVerdict = (v: unknown): v is ChallengeVerdict => typeof v === 'string' && (VERDICTS as readonly string[]).includes(v)
|
||||
|
||||
/** 颜色同结论句裁判(lib/evidence 的 EVIDENCE_STATE):经受住同「有依据」,被削弱同「部分有依据」,被推翻同「证据相矛盾」 */
|
||||
const VERDICT_COLOR: Record<ChallengeVerdict, string> = {
|
||||
stands: 'var(--st-running)',
|
||||
weakened: 'var(--st-waiting)',
|
||||
refuted: 'var(--st-failed)',
|
||||
inconclusive: 'var(--st-cancelled)',
|
||||
}
|
||||
|
||||
const verdictLabel = (v: string) => (isVerdict(v) ? CHALLENGE_TEXT.verdict[v] : CHALLENGE_TEXT.verdictUnknown(v))
|
||||
|
||||
function isItem(x: unknown): x is ChallengeItem {
|
||||
return !!x && typeof x === 'object' && typeof (x as ChallengeItem).text === 'string' && typeof (x as ChallengeItem).verdict === 'string'
|
||||
}
|
||||
|
||||
const count = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? Math.floor(v) : 0)
|
||||
|
||||
/**
|
||||
* payload.reports[i].challenge 认不认得:要么有计数、要么有条目。计数缺了按条目数(条目最多 10 条,计数才是全部);
|
||||
* 条目缺了 unit 的留空(行的键带序号,区分得开),不补假的:补出来的键会被当成真的 unit 去和工件里的句子比
|
||||
*/
|
||||
function challengeOf(report: ApprovalReport): ApprovalChallenge | null {
|
||||
const raw = report.challenge as unknown
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const c = raw as Record<string, unknown>
|
||||
const items = (Array.isArray(c.items) ? c.items.filter(isItem) : [])
|
||||
.map((it) => ({ unit: typeof it.unit === 'string' ? it.unit : '', text: it.text, verdict: it.verdict }))
|
||||
// 计数里一个数都没有(格式不对)也按条目数,免得概况写四个 0、下面却列着几句
|
||||
const given = c.counts && typeof c.counts === 'object' && VERDICTS.some((v) => typeof (c.counts as any)[v] === 'number')
|
||||
? c.counts as Record<string, unknown> : null
|
||||
const counts = Object.fromEntries(VERDICTS.map((v) => [v, given
|
||||
? count(given[v]) : items.filter((it) => it.verdict === v).length])) as ChallengeCounts
|
||||
if (!given && !items.length) return null
|
||||
return {
|
||||
counts, items, artifact: typeof c.artifact === 'string' && c.artifact ? c.artifact : null,
|
||||
on_refuted: typeof c.on_refuted === 'string' ? c.on_refuted : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
interface RunClass {
|
||||
runClass: 'formal' | 'exploratory' | null
|
||||
/** 取失败了、还没取到(重取期间也算:那一行和焦点所在的重试按钮不跟着闪掉) */
|
||||
failed: boolean
|
||||
retry: () => void
|
||||
}
|
||||
|
||||
function useRunClass(runId: string): RunClass {
|
||||
const slot = useEvidence((s) => (runId ? s.runClasses[runId] : undefined))
|
||||
const load = useEvidence((s) => s.loadRunClass)
|
||||
useEffect(() => { if (runId) void load(runId) }, [runId, load])
|
||||
const status = slot?.status
|
||||
const [hadError, setHadError] = useState(false)
|
||||
useEffect(() => {
|
||||
if (status === 'error') setHadError(true)
|
||||
else if (status === 'ok') setHadError(false)
|
||||
}, [status])
|
||||
const failed = status === 'error' || (hadError && status === 'loading')
|
||||
// 取失败了不就此作罢:窗口重新拿到焦点时再取一次(store 对出错的槽会重发)。只取一次就放弃的话,一次网络抖动
|
||||
// 会让「出具会降档」一直不出现,而卡顶按审批自带的运行类别照样写着正式运行
|
||||
useEffect(() => {
|
||||
if (status !== 'error' || !runId) return
|
||||
const again = () => { void load(runId) }
|
||||
window.addEventListener('focus', again)
|
||||
return () => window.removeEventListener('focus', again)
|
||||
}, [status, runId, load])
|
||||
const retry = useCallback(() => { if (runId) void load(runId) }, [runId, load])
|
||||
const runClass = status === 'ok' && (slot?.data === 'formal' || slot?.data === 'exploratory') ? slot.data : null
|
||||
return { runClass, failed, retry }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 取工件
|
||||
|
||||
/** 工件 id → 取回的内容。工件内容寻址、不可变,取到的永久缓存;出错的不缓存成定论,下次点开再取 */
|
||||
const fetched = new Map<string, Promise<ChallengeArtifact>>()
|
||||
|
||||
function loadChallenge(id: string): Promise<ChallengeArtifact> {
|
||||
let pending = fetched.get(id)
|
||||
if (!pending) {
|
||||
pending = api.artifact(id).then((res) => {
|
||||
const content = asChallenge(res?.content)
|
||||
if (!content) throw new Error(CHALLENGE_TEXT.failed)
|
||||
return content
|
||||
})
|
||||
pending.catch(() => { fetched.delete(id) })
|
||||
fetched.set(id, pending)
|
||||
}
|
||||
return pending
|
||||
}
|
||||
|
||||
const text = (v: unknown) => (typeof v === 'string' ? v : '')
|
||||
|
||||
function asQuery(x: unknown): ChallengeQuery | null {
|
||||
if (!x || typeof x !== 'object') return null
|
||||
const q = x as Record<string, unknown>
|
||||
return {
|
||||
alias: text(q.alias), artifact: text(q.artifact), sql: text(q.sql),
|
||||
row_count: typeof q.row_count === 'number' && Number.isFinite(q.row_count) ? q.row_count : null,
|
||||
truncated: q.truncated === true, source: typeof q.source === 'string' && q.source ? q.source : null,
|
||||
}
|
||||
}
|
||||
|
||||
/** 工件内容认不认得:至少得有 claims 列表;每一句缺的字段按空的补,界面照实少显示 */
|
||||
function asChallenge(content: unknown): ChallengeArtifact | null {
|
||||
if (!content || typeof content !== 'object' || !Array.isArray((content as ChallengeArtifact).claims)) return null
|
||||
const c = content as Record<string, any>
|
||||
const claims: ChallengeClaim[] = (c.claims as unknown[]).filter(isItem).map((it) => {
|
||||
const raw = it as unknown as Record<string, unknown>
|
||||
const doc = raw.rationale_doc
|
||||
return {
|
||||
unit: text(raw.unit), text: it.text, verdict: it.verdict, rationale: text(raw.rationale),
|
||||
rationale_doc: typeof doc === 'string' || (doc && typeof doc === 'object') ? doc as ChallengeClaim['rationale_doc'] : null,
|
||||
queries: (Array.isArray(raw.queries) ? raw.queries : []).map(asQuery).filter((q): q is ChallengeQuery => !!q),
|
||||
}
|
||||
})
|
||||
return { report: text(c.report), doc_artifact: text(c.doc_artifact), claims, model: typeof c.model === 'string' && c.model ? c.model : null }
|
||||
}
|
||||
|
||||
type Fetch = { status: 'idle' | 'loading' | 'ok' | 'error'; data?: ChallengeArtifact }
|
||||
/** retry 没有时不给重试:审批里根本没带工件 id,再取也是一样 */
|
||||
type Detail = Fetch & { retry?: () => void }
|
||||
|
||||
/** 整份反方审稿:want 为真(点开过一句、要看全部)之后才取,一张卡里各句共用这一次 */
|
||||
function useChallengeArtifact(id: string, want: boolean): Detail {
|
||||
const [state, setState] = useState<Fetch>({ status: 'idle' })
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
useEffect(() => {
|
||||
if (!want || !id) return
|
||||
let alive = true
|
||||
setState((s) => (s.status === 'ok' ? s : { status: 'loading' }))
|
||||
loadChallenge(id).then(
|
||||
(data) => { if (alive) setState({ status: 'ok', data }) },
|
||||
() => { if (alive) setState({ status: 'error' }) },
|
||||
)
|
||||
return () => { alive = false }
|
||||
}, [id, want, attempt])
|
||||
const retry = useCallback(() => setAttempt((n) => n + 1), [])
|
||||
return id ? { ...state, retry } : { status: 'error' }
|
||||
}
|
||||
|
||||
/**
|
||||
* 工件里的一句和审批条目里的一句是不是同一句:两边都有 unit 时只按 unit 认——同一句话在摘要和正文各出现一次、各自被挑来审,
|
||||
* 原文一样、unit 不同,是两句(按原文认会把其中一句吞掉,被吞的可能正是被推翻的那句);有一边没给 unit(老服务端)才按原文认
|
||||
*/
|
||||
const sameClaim = (a: ChallengeItem, b: ChallengeItem) => (a.unit && b.unit ? a.unit === b.unit : a.text === b.text)
|
||||
|
||||
/** 这一句在工件里的完整内容 */
|
||||
function claimOf(artifact: ChallengeArtifact | undefined, item: ChallengeItem): ChallengeClaim | undefined {
|
||||
return artifact?.claims.find((c) => sameClaim(c, item))
|
||||
}
|
||||
|
||||
/** 补齐、重试之后接住焦点的几处:本身不在 Tab 顺序里(tabIndex -1),键盘用户得看得见焦点落在哪 */
|
||||
const FOCUS_RING = 'rounded-sm focus:outline-none focus-visible:outline focus-visible:outline-solid focus-visible:outline-2 '
|
||||
+ 'focus-visible:outline-offset-1 focus-visible:outline-[color:var(--accent)]'
|
||||
|
||||
/** 焦点丢了(按过的按钮被拿掉,落回 body)才替它接住;取的这一会儿用户已经去了别处的,不抢 */
|
||||
const focusLost = () => !document.activeElement || document.activeElement === document.body
|
||||
|
||||
// ---------------------------------------------------------------- 卡片
|
||||
|
||||
function ChallengeBlock({ report, challenge, run }: {
|
||||
report: ApprovalReport; challenge: ApprovalChallenge; run: RunClass
|
||||
}) {
|
||||
const [open, setOpen] = useState<Set<string>>(() => new Set())
|
||||
const [all, setAll] = useState(false)
|
||||
const detail = useChallengeArtifact(challenge.artifact ?? '', all || open.size > 0)
|
||||
const { counts, items } = challenge
|
||||
const total = VERDICTS.reduce((n, v) => n + counts[v], 0)
|
||||
// 审批里没带工件:服务端保存审稿记录失败了。每句都看不了理由和查询,不画成能点开的样子,整块说一次
|
||||
const noRecord = !challenge.artifact
|
||||
const refuted = counts.refuted > 0 && run.runClass === 'formal'
|
||||
// 没给 on_refuted(老服务端)按降档;报错的节点到不了审批,不会是 fail
|
||||
const flagged = challenge.on_refuted === 'flag'
|
||||
// 条目之外的几句(条目最多 10 条):要看全部时从工件里补上,排在后面,顺序照工件
|
||||
const extra = all && detail.data ? detail.data.claims.filter((c) => !items.some((it) => sameClaim(c, it))) : []
|
||||
const rows: ChallengeItem[] = [...items, ...extra]
|
||||
const hidden = Math.max(0, total - items.length)
|
||||
const toggle = (key: string) => setOpen((cur) => {
|
||||
const next = new Set(cur)
|
||||
if (next.has(key)) next.delete(key)
|
||||
else next.add(key)
|
||||
return next
|
||||
})
|
||||
|
||||
// 「显示全部」那一行补齐后整行拿掉、取的时候按钮也停用,焦点会掉到页面上,下一个 Tab 就跳过了新补出来的几句:
|
||||
// 按过「显示全部」或那一行的「重试」之后,取到了落到补出来的第一句上(计数和工件对不上、什么也没补到时落到最后一句),
|
||||
// 又没取到落到那一行新出现的「重试」上
|
||||
const blockRef = useRef<HTMLDivElement>(null)
|
||||
const moreRef = useRef<HTMLDivElement>(null)
|
||||
const focusMore = useRef(false)
|
||||
useEffect(() => {
|
||||
if (!focusMore.current || !all || (detail.status !== 'ok' && detail.status !== 'error')) return
|
||||
focusMore.current = false
|
||||
if (!focusLost()) return
|
||||
if (detail.status === 'error') {
|
||||
moreRef.current?.querySelector<HTMLElement>('button')?.focus()
|
||||
return
|
||||
}
|
||||
const toggles = blockRef.current?.querySelectorAll<HTMLElement>('[data-challenge-toggle]')
|
||||
;(toggles?.[items.length] ?? toggles?.[toggles.length - 1])?.focus()
|
||||
}, [all, detail.status, items.length])
|
||||
const showAll = () => { focusMore.current = true; setAll(true) }
|
||||
const retryAll = () => { focusMore.current = true; detail.retry?.() }
|
||||
|
||||
// 运行类别重取成功后,「暂时无法确认」那一行(连同焦点所在的重试按钮)换成降档说明:焦点接到概况那一行上
|
||||
const countsRef = useRef<HTMLDivElement>(null)
|
||||
const runRetried = useRef(false)
|
||||
useEffect(() => {
|
||||
if (run.failed || !runRetried.current) return
|
||||
runRetried.current = false
|
||||
if (focusLost()) countsRef.current?.focus()
|
||||
}, [run.failed])
|
||||
const retryRun = () => { runRetried.current = true; run.retry() }
|
||||
|
||||
return (
|
||||
<div ref={blockRef} className="mb-2 rounded border bg-bg p-2" data-challenge={report.node_id}>
|
||||
<div className="mb-1 flex items-center gap-1 text-2xs font-medium text-faint">
|
||||
<ShieldAlert size={11} aria-hidden /> {CHALLENGE_TEXT.head(report.label || report.node_id)}
|
||||
</div>
|
||||
{total > 0 || items.length ? (
|
||||
<div ref={countsRef} tabIndex={-1} className={`tnum flex flex-wrap items-center gap-x-1.5 gap-y-1 text-2xs ${FOCUS_RING}`}
|
||||
data-challenge-counts="">
|
||||
{VERDICTS.map((v, i) => (
|
||||
<Fragment key={v}>
|
||||
{i > 0 && <span className="text-faint" aria-hidden>·</span>}
|
||||
<span data-challenge-count={v} style={{ color: counts[v] ? VERDICT_COLOR[v] : 'var(--text-faint)' }}>
|
||||
{CHALLENGE_TEXT.count(CHALLENGE_TEXT.verdict[v], counts[v])}
|
||||
</span>
|
||||
</Fragment>
|
||||
))}
|
||||
{refuted && !flagged && (
|
||||
<span className="chip shrink-0 font-semibold" data-challenge-degrade=""
|
||||
style={{ color: 'var(--st-failed)', borderColor: 'var(--st-failed)' }}>
|
||||
<AlertTriangle size={11} aria-hidden /> {CHALLENGE_TEXT.degrade}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-2xs text-dim" data-challenge-empty="">{CHALLENGE_TEXT.none}</div>
|
||||
)}
|
||||
{/* 降档的原因常显,不放在悬停提示里:触屏、键盘用户也得看得到为什么、会怎样(只标注那一档同样常显一行) */}
|
||||
{refuted && !flagged && (
|
||||
<div className="mt-0.5 text-2xs leading-relaxed text-dim" data-challenge-degrade-note="">{CHALLENGE_TEXT.degradeHint}</div>
|
||||
)}
|
||||
{refuted && flagged && (
|
||||
<div className="mt-0.5 text-2xs leading-relaxed text-dim" data-challenge-flag="">{CHALLENGE_TEXT.flagNote}</div>
|
||||
)}
|
||||
{/* 运行类别取不到:不知道是不是正式运行,不能装作没事(卡顶按审批自带的运行类别照样写着正式运行),照实说并可重试 */}
|
||||
{counts.refuted > 0 && !flagged && run.failed && (
|
||||
<div className="mt-0.5 flex flex-wrap items-center gap-x-2 gap-y-1 text-2xs leading-relaxed" data-challenge-runclass="failed">
|
||||
<span style={{ color: 'var(--st-waiting)' }}>{CHALLENGE_TEXT.runClassFailed}</span>
|
||||
<button type="button" className="btn btn-xs" onClick={retryRun}>{CHALLENGE_TEXT.retry}</button>
|
||||
</div>
|
||||
)}
|
||||
{rows.length > 0 && (
|
||||
<ul className="mt-1.5 space-y-0.5" data-challenge-items="">
|
||||
{rows.map((item, i) => {
|
||||
const key = `${item.unit}\u0000${i}`
|
||||
if (noRecord) {
|
||||
return (
|
||||
<li key={key} data-challenge-item={item.unit} data-challenge-verdict={item.verdict}
|
||||
className="flex min-w-0 items-start gap-1.5 px-1 py-0.5 text-2xs leading-relaxed">
|
||||
<ClaimLine item={item} />
|
||||
</li>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<ClaimRow key={key} item={item} open={open.has(key)} onToggle={() => toggle(key)}>
|
||||
<ClaimDetail item={item} detail={detail} />
|
||||
</ClaimRow>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
{/* 工件取回来了、却没有条目之外的句子(计数和工件对不上):没什么可补的,这一行也不留。没有工件时照样交代
|
||||
还有几句没列出,只是补不了 */}
|
||||
{hidden > 0 && !extra.length && !(all && detail.status === 'ok') && (
|
||||
<div ref={moreRef} className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-2xs text-dim" data-challenge-more={hidden}>
|
||||
<span>{CHALLENGE_TEXT.more(hidden)}</span>
|
||||
{noRecord ? null : detail.status === 'error' && all ? (
|
||||
<>
|
||||
<span style={{ color: 'var(--st-waiting)' }} role="alert">{CHALLENGE_TEXT.failed}</span>
|
||||
{detail.retry && <button type="button" className="btn btn-xs" onClick={retryAll}>{CHALLENGE_TEXT.retry}</button>}
|
||||
</>
|
||||
) : (
|
||||
<button type="button" className="btn btn-xs" disabled={all && detail.status === 'loading'}
|
||||
data-challenge-show-all="" onClick={showAll}>
|
||||
{all && detail.status === 'loading' ? CHALLENGE_TEXT.loading : CHALLENGE_TEXT.showAll}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{noRecord && (total > 0 || items.length > 0) && (
|
||||
<div className="mt-1 text-2xs leading-relaxed" style={{ color: 'var(--st-waiting)' }} data-challenge-norecord="">
|
||||
{CHALLENGE_TEXT.noRecord}
|
||||
</div>
|
||||
)}
|
||||
{/* 判断标签看着像结论,得说清是谁判的:整块说一次,工件取回来之后带上模型名 */}
|
||||
{(total > 0 || items.length > 0) && (
|
||||
<p className="mt-1 text-2xs leading-relaxed text-faint" data-challenge-judged="">
|
||||
{CHALLENGE_TEXT.judgedBy(detail.data?.model ?? null)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 判断标签:文字加颜色(去掉颜色也读得出),不铺底色——浅色主题下底色会把小字的对比度压到 4.5 以下 */
|
||||
function VerdictTag({ verdict }: { verdict: string }) {
|
||||
const color = isVerdict(verdict) ? VERDICT_COLOR[verdict] : 'var(--text-dim)'
|
||||
return (
|
||||
<span className="chip shrink-0 font-medium" data-challenge-tag={verdict}
|
||||
style={{ color, borderColor: `color-mix(in srgb, ${color} 45%, transparent)` }}>
|
||||
{verdictLabel(verdict)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
/** 一句的判断标签和结论原文 */
|
||||
function ClaimLine({ item }: { item: ChallengeItem }) {
|
||||
return (
|
||||
<>
|
||||
<VerdictTag verdict={item.verdict} />
|
||||
{/* 原文取自报告文档,是 Markdown:粗体、行内代码照样画出来;链接不画成链接,按钮里不能再套一个可点的东西 */}
|
||||
<span className="min-w-0 flex-1 [overflow-wrap:anywhere]" data-challenge-text="">
|
||||
<StyledSlice text={item.text} styles={inlineStyles(item.text)} from={0} to={item.text.length} links={false} />
|
||||
</span>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function ClaimRow({ item, open, onToggle, children }: {
|
||||
item: ChallengeItem; open: boolean; onToggle: () => void; children: ReactNode
|
||||
}) {
|
||||
const detailId = useId()
|
||||
return (
|
||||
<li data-challenge-item={item.unit} data-challenge-verdict={item.verdict}>
|
||||
<button type="button" aria-expanded={open} aria-controls={open ? detailId : undefined} onClick={onToggle} data-challenge-toggle=""
|
||||
className="flex w-full min-w-0 items-start gap-1.5 rounded px-1 py-0.5 text-left text-2xs leading-relaxed hover:bg-hover focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-[color:var(--accent)]">
|
||||
<ChevronRight size={11} aria-hidden className="mt-[3px] shrink-0 text-faint"
|
||||
style={{ transform: open ? 'rotate(90deg)' : 'none', transition: 'transform .15s' }} />
|
||||
<ClaimLine item={item} />
|
||||
</button>
|
||||
{open && (
|
||||
<div id={detailId} className="mb-1 ml-4 mt-1 min-w-0 space-y-2 rounded border p-2" data-challenge-detail={item.unit}>
|
||||
{children}
|
||||
</div>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
/** 点开的一句:工件取回来之前说正在加载;取不到给重试;取到了画理由和查询 */
|
||||
function ClaimDetail({ item, detail }: { item: ChallengeItem; detail: Detail }) {
|
||||
const baseId = useId()
|
||||
const claim = claimOf(detail.data, item)
|
||||
// 理由里的数字点一下:焦点落到那次查询上,描一下边(减少动效时也看得见)
|
||||
const jump = useCallback((alias: string) => {
|
||||
const part = document.getElementById(`${baseId}-q-${alias}`)
|
||||
const head = part?.querySelector<HTMLElement>('[data-challenge-query-head]')
|
||||
if (!part || !head) return
|
||||
head.focus({ preventScroll: true })
|
||||
head.scrollIntoView({ block: 'nearest' })
|
||||
part.dataset.flash = 'focus'
|
||||
setTimeout(() => { if (part.dataset.flash === 'focus') delete part.dataset.flash }, 1600)
|
||||
}, [baseId])
|
||||
// 点了「重试」,按钮在加载时就拿掉了:取到了焦点落到理由上,又没取到落到新的「重试」上,不掉到页面上
|
||||
const retried = useRef(false)
|
||||
const landing = useRef<HTMLElement | null>(null)
|
||||
const land = useCallback((el: HTMLElement | null) => { landing.current = el }, [])
|
||||
useEffect(() => {
|
||||
if (!retried.current || (detail.status !== 'ok' && detail.status !== 'error')) return
|
||||
retried.current = false
|
||||
if (focusLost()) landing.current?.focus()
|
||||
}, [detail.status])
|
||||
if (detail.status === 'error') {
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-2 text-2xs" data-challenge-detail-state="error">
|
||||
<span role="alert" style={{ color: 'var(--st-waiting)' }}>{CHALLENGE_TEXT.failed}</span>
|
||||
{detail.retry && (
|
||||
<button ref={land} type="button" className="btn btn-xs"
|
||||
onClick={() => { retried.current = true; detail.retry?.() }}>
|
||||
{CHALLENGE_TEXT.retry}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (detail.status !== 'ok') {
|
||||
return <div className="text-2xs text-faint" data-challenge-detail-state="loading">{CHALLENGE_TEXT.loading}</div>
|
||||
}
|
||||
if (!claim) {
|
||||
return (
|
||||
<div ref={land} tabIndex={-1} className={`text-2xs text-dim ${FOCUS_RING}`}
|
||||
data-challenge-detail-state="missing">
|
||||
{CHALLENGE_TEXT.claimMissing}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const aliases = new Set(claim.queries.map((q) => q.alias))
|
||||
return (
|
||||
<>
|
||||
<section ref={land} tabIndex={-1} className={FOCUS_RING} data-challenge-detail-state="ok" data-challenge-rationale="">
|
||||
<div className="mb-0.5 text-2xs font-medium text-faint">{CHALLENGE_TEXT.rationale}</div>
|
||||
<Rationale claim={claim} onJump={(alias) => (aliases.has(alias) ? jump(alias) : undefined)} />
|
||||
</section>
|
||||
<section data-challenge-queries={claim.queries.length}>
|
||||
<div className="mb-1 text-2xs font-medium text-faint">
|
||||
{claim.queries.length ? CHALLENGE_TEXT.queries(claim.queries.length) : CHALLENGE_TEXT.noQueries}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
{claim.queries.map((q, i) => <QueryCard key={`${q.alias}-${i}`} id={`${baseId}-q-${q.alias}`} query={q} />)}
|
||||
</div>
|
||||
</section>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function QueryCard({ id, query }: { id: string; query: ChallengeQuery }) {
|
||||
return (
|
||||
<div id={id} data-challenge-query={query.alias}
|
||||
className="min-w-0 rounded data-[flash]:outline data-[flash]:outline-solid data-[flash]:outline-2 data-[flash]:outline-offset-2 data-[flash=focus]:outline-[color:var(--accent)]">
|
||||
<div className="mb-1 flex min-w-0 items-center gap-2">
|
||||
{/* 从理由里的数字跳过来时焦点落在这里:得看得见落在哪 */}
|
||||
<span tabIndex={-1} data-challenge-query-head=""
|
||||
className="min-w-0 flex-1 truncate rounded-sm text-2xs font-medium text-faint focus:outline-none focus-visible:outline focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-[color:var(--accent)]">
|
||||
{EVIDENCE_TEXT.query(query.alias)}
|
||||
{query.source && <span className="mono ml-1.5 font-normal">{query.source}</span>}
|
||||
</span>
|
||||
{query.sql && <CopyChip label={EVIDENCE_TEXT.copySql} text={() => query.sql} />}
|
||||
</div>
|
||||
{query.sql && (
|
||||
<pre className="mono mb-1 max-h-28 overflow-auto whitespace-pre-wrap rounded bg-panel px-1.5 py-1 text-2xs leading-relaxed text-dim [overflow-wrap:anywhere]"
|
||||
data-challenge-sql="">
|
||||
{query.sql}
|
||||
</pre>
|
||||
)}
|
||||
<div className="tnum flex flex-wrap gap-x-2 text-2xs text-faint">
|
||||
{query.row_count != null && <span data-challenge-rows={query.row_count}>{CHALLENGE_TEXT.rows(query.row_count)}</span>}
|
||||
{query.truncated && (
|
||||
<span data-challenge-truncated="" style={{ color: 'var(--st-waiting)' }}>{CHALLENGE_TEXT.truncated}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 理由
|
||||
|
||||
/**
|
||||
* 理由正文。有理由文档时按文档画:数字的出处照报告里的样子(有出处的实线、裸数字点状线加「无证据」),有出处的数字后面
|
||||
* 挂查询编号,点一下跳到那次查询。文档可能直接放在工件里,也可能是另一个工件的 id(约定没定死,两种都认);
|
||||
* 都没有、或者取不到时按渲染后的正文显示,数字照写,只是不标出处
|
||||
*/
|
||||
function Rationale({ claim, onJump }: { claim: ChallengeClaim; onJump: (alias: string) => void }) {
|
||||
const ref = claim.rationale_doc
|
||||
const inline = ref && typeof ref === 'object' ? asDoc(ref) : null
|
||||
const artifact = typeof ref === 'string' ? ref : ''
|
||||
const slot = useEvidence((s) => (artifact ? s.docs[artifact] : undefined))
|
||||
const loadDoc = useEvidence((s) => s.loadDoc)
|
||||
useEffect(() => { if (artifact) void loadDoc(artifact) }, [artifact, loadDoc])
|
||||
const doc = inline ?? (slot?.status === 'ok' ? slot.data ?? null : null)
|
||||
if (doc && (doc.blocks ?? []).length) return <RationaleDoc doc={doc} onJump={onJump} />
|
||||
if (artifact && slot?.status !== 'error' && !doc) {
|
||||
return <div className="text-2xs text-faint" data-challenge-doc-loading="">{CHALLENGE_TEXT.docLoading}</div>
|
||||
}
|
||||
if (!claim.rationale.trim()) return <div className="text-2xs text-faint">{CHALLENGE_TEXT.rationaleEmpty}</div>
|
||||
return <div data-challenge-rationale-text=""><Markdown text={claim.rationale} dense /></div>
|
||||
}
|
||||
|
||||
/** 列表项从哪一个片段开始:同 EvidenceDoc(行首的「- 」「1. 」是结构片段) */
|
||||
const ITEM_START = /(^|\n)[ \t]*([-*+]|\d+[.)])[ \t]+$/
|
||||
|
||||
function RationaleDoc({ doc, onJump }: { doc: EvidenceDocData; onJump: (alias: string) => void }) {
|
||||
return (
|
||||
<div className="min-w-0 space-y-1.5 text-[11.5px] leading-relaxed" data-challenge-doc="">
|
||||
{doc.blocks.map((block, b) => <RationaleBlock key={block.id ?? b} block={block} doc={doc} onJump={onJump} />)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 一块:外观借 Markdown 的 BlockView,和报告文档、普通答案长得一样(块的分法同 EvidenceDoc 的 DocBlock) */
|
||||
function RationaleBlock({ block, doc, onJump }: { block: EvidenceBlock; doc: EvidenceDocData; onJump: (alias: string) => void }) {
|
||||
const units = block.units ?? []
|
||||
const unit = (u: EvidenceUnit, code = false) => <RationaleUnit key={u.id} unit={u} doc={doc} code={code} onJump={onJump} />
|
||||
switch (block.type) {
|
||||
case 'heading':
|
||||
return <BlockView dense shape={{ kind: 'h', level: block.level ?? 3, body: units.map((u) => unit(u)) }} />
|
||||
case 'hr':
|
||||
return <BlockView dense shape={{ kind: 'hr' }} />
|
||||
case 'quote':
|
||||
return <BlockView dense shape={{ kind: 'quote', body: units.map((u) => unit(u)) }} />
|
||||
case 'code':
|
||||
return <BlockView dense shape={{ kind: 'code', lang: block.lang ?? '', code: units.map(unitText).join(''), body: units.map((u) => unit(u, true)) }} />
|
||||
case 'list': {
|
||||
const items: { key: string; depth: number; units: EvidenceUnit[] }[] = []
|
||||
for (const u of units) {
|
||||
const lead = u.segments?.[0]
|
||||
if (!items.length || (lead?.kind === 'structural' && ITEM_START.test(lead.text))) {
|
||||
items.push({ key: u.id, depth: u.depth ?? 0, units: [u] })
|
||||
} else items[items.length - 1].units.push(u)
|
||||
}
|
||||
return (
|
||||
<BlockView dense shape={{
|
||||
kind: 'list', ordered: !!block.ordered, start: block.start, fitMarkers: true,
|
||||
items: items.map((it) => ({ key: it.key, depth: it.depth, body: it.units.map((u) => unit(u)) })),
|
||||
}} />
|
||||
)
|
||||
}
|
||||
case 'table': {
|
||||
const cells = new Map<string, EvidenceUnit[]>()
|
||||
let cols = 0
|
||||
const rowIds: number[] = []
|
||||
for (const u of units) {
|
||||
const loc = u.loc ?? { row: 0, col: 0 }
|
||||
cells.set(`${loc.row}:${loc.col}`, [...(cells.get(`${loc.row}:${loc.col}`) ?? []), u])
|
||||
cols = Math.max(cols, loc.col + 1)
|
||||
if (loc.row >= 0 && !rowIds.includes(loc.row)) rowIds.push(loc.row)
|
||||
}
|
||||
rowIds.sort((a, b) => a - b)
|
||||
const plain = (r: number, c: number) => (cells.get(`${r}:${c}`) ?? []).map(unitText).join('')
|
||||
const node = (r: number, c: number) => (cells.get(`${r}:${c}`) ?? []).map((u) => unit(u))
|
||||
const head = Array.from({ length: cols }, (_, c) => plain(-1, c))
|
||||
const body = rowIds.map((r) => Array.from({ length: cols }, (_, c) => plain(r, c)))
|
||||
return (
|
||||
<BlockView dense shape={{
|
||||
kind: 'table',
|
||||
head: Array.from({ length: cols }, (_, c) => node(-1, c)),
|
||||
rows: rowIds.map((r) => Array.from({ length: cols }, (_, c) => node(r, c))),
|
||||
numeric: numericColumns(head, body),
|
||||
csv: () => [head, ...body].map((r) => r.join(',')).join('\n'),
|
||||
}} />
|
||||
)
|
||||
}
|
||||
default:
|
||||
return <BlockView dense shape={{ kind: 'p', body: units.map((u) => unit(u)) }} />
|
||||
}
|
||||
}
|
||||
|
||||
/** 片段的颜色写成变量交给 index.css 的 .ev-seg / .ev-mark(同 EvidenceDoc) */
|
||||
const evStyle = (state: EvidenceStateCode) => ({
|
||||
'--ev-line': EVIDENCE_STATE[state].decoration, '--ev-soft': EVIDENCE_STATE[state].soft,
|
||||
}) as CSSProperties
|
||||
|
||||
/** 引用的是查询结果里的一格:返回查询编号 */
|
||||
function cellAlias(seg: EvidenceSegment, doc: EvidenceDocData): string | null {
|
||||
const cite = seg.cite
|
||||
if (!cite?.alias || segmentState(seg) !== 'deterministic') return null
|
||||
const entry = doc.catalog?.[cite.alias]
|
||||
return cite.kind === 'cell' || (cite.kind !== 'metric' && cite.kind !== 'input' && entry?.kind === 'query') ? cite.alias : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 一句话:文字按行内 Markdown 渲染(整句算一次样式,同 EvidenceDoc 的 UnitView)。有出处的数字是按钮,后面挂查询编号;
|
||||
* 裸数字、疑似不存在的名称画点状线,句末挂「无证据」「疑似不存在的名称」。别的片段(表名、字段名的链接)照普通文字
|
||||
*/
|
||||
function RationaleUnit({ unit, doc, code, onJump }: {
|
||||
unit: EvidenceUnit; doc: EvidenceDocData; code: boolean; onJump: (alias: string) => void
|
||||
}) {
|
||||
const segs = (unit.segments ?? []).filter((s) => s.kind !== 'structural')
|
||||
const whole = segs.map((s) => s.text).join('')
|
||||
const styles = useMemo(() => (code ? null : inlineStyles(whole)), [whole, code])
|
||||
const alerts = new Set<EvidenceStateCode>()
|
||||
let pos = 0
|
||||
const nodes = segs.map((s) => {
|
||||
const from = pos
|
||||
pos += s.text.length
|
||||
const state = segmentState(s)
|
||||
const alias = cellAlias(s, doc)
|
||||
const body = styles ? <StyledSlice text={whole} styles={styles} from={from} to={pos} links={!state} /> : s.text
|
||||
if (alias) {
|
||||
const source = sourceOf(s, doc)
|
||||
return (
|
||||
<button key={s.id} type="button" className="ev-seg" data-ev-state="deterministic" data-ev-line="solid"
|
||||
data-challenge-cite={alias} style={evStyle('deterministic')} title={source}
|
||||
aria-label={CHALLENGE_TEXT.cited(s.text, source)} onClick={() => onJump(alias)}>
|
||||
{body}
|
||||
{/* inline-block:不吃上面那条下划线 */}
|
||||
<sup className="mono ml-px inline-block select-none text-[9px] font-semibold text-faint" aria-hidden>{alias}</sup>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
if (state === 'none' || state === 'suspect') {
|
||||
alerts.add(state)
|
||||
return (
|
||||
<span key={s.id} className="ev-mark" data-ev-state={state} data-ev-line="dotted" style={evStyle(state)}
|
||||
title={segmentLabel(s, doc)}>
|
||||
{body}<span className="sr-only">({EVIDENCE_STATE_LABEL[state]})</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return <span key={s.id}>{s.strong ? <strong className="font-semibold">{body}</strong> : body}</span>
|
||||
})
|
||||
return (
|
||||
<>
|
||||
{nodes}
|
||||
{!code && [...alerts].map((state) => (
|
||||
<span key={state} className="ev-tag" aria-hidden="true" title={EVIDENCE_STATE[state].hint} data-ev-tag={state}>
|
||||
{EVIDENCE_STATE[state].glyph}{EVIDENCE_STATE_LABEL[state]}
|
||||
</span>
|
||||
))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3617,3 +3617,76 @@ export interface VerifySpend {
|
||||
unpriced_calls?: number
|
||||
daily_max_usd?: number | null
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 反方审稿(challenge 节点):审批卡上的概况、challenge_artifact 工件的内容。形状照前后端共用的接口约定,字段名一个不改;
|
||||
// 判断由反方模型给出,界面只排版,理由正文、行数这些都是服务端给的
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** 一句结论经受住反方的复核没有:经受住 / 被削弱 / 被推翻 / 无法判断 */
|
||||
export type ChallengeVerdict = 'stands' | 'weakened' | 'refuted' | 'inconclusive'
|
||||
|
||||
/** 各种判断的句数 */
|
||||
export type ChallengeCounts = Record<ChallengeVerdict, number>
|
||||
|
||||
/** 审批卡、节点产出里列的一句:unit 是报告文档里这一句的 id,text 是结论原文 */
|
||||
export interface ChallengeItem {
|
||||
unit: string
|
||||
text: string
|
||||
verdict: ChallengeVerdict | (string & {})
|
||||
}
|
||||
|
||||
/**
|
||||
* 审批卡上的反方审稿概况(human 节点 payload.reports[i].challenge)。items 最多 10 条,完整内容在 artifact 指的工件里。
|
||||
* on_refuted 是实际生效的处理方式:degrade 被推翻的结论计入出具缺口;flag 只标注。报错(fail)时节点已经失败,到不了审批。
|
||||
* 老服务端不给时按 degrade
|
||||
*/
|
||||
export interface ApprovalChallenge {
|
||||
counts: ChallengeCounts
|
||||
items: ChallengeItem[]
|
||||
/** challenge_artifact 工件 id。服务端保存审稿记录失败时为 null:判断照样有,理由和查询看不了 */
|
||||
artifact: string | null
|
||||
on_refuted?: 'degrade' | 'flag' | (string & {})
|
||||
}
|
||||
|
||||
/**
|
||||
* 审批审的报告经过反方审稿时多一个 challenge。和上面的 ApprovalReport 是同一个接口(声明合并):反方审稿的类型集中在
|
||||
* 这一段,这一项也就近写在这里
|
||||
*/
|
||||
export interface ApprovalReport {
|
||||
challenge?: ApprovalChallenge
|
||||
}
|
||||
|
||||
/** 反方为复核一句结论执行的一次只读查询:编号是它自己的(Q1、Q2…),和报告里的查询编号无关 */
|
||||
export interface ChallengeQuery {
|
||||
alias: string
|
||||
/** 查询快照工件 */
|
||||
artifact: string
|
||||
sql: string
|
||||
row_count: number | null
|
||||
truncated: boolean
|
||||
/** 数据源名 */
|
||||
source: string | null
|
||||
}
|
||||
|
||||
/** 工件里的一句:判断、理由、查询 */
|
||||
export interface ChallengeClaim extends ChallengeItem {
|
||||
/** 理由渲染后的正文(数字已换成查询结果里的值) */
|
||||
rationale: string
|
||||
/**
|
||||
* 理由组装成的文档(和报告文档同一种形状,数字带出处、裸数字被标出)。约定没说是工件 id 还是文档本身,两种都认;
|
||||
* 没有时只显示 rationale
|
||||
*/
|
||||
rationale_doc?: string | EvidenceDocData | null
|
||||
queries: ChallengeQuery[]
|
||||
usage?: { input_tokens?: number; output_tokens?: number; total_tokens?: number; cost_usd?: number | null; calls?: number }
|
||||
}
|
||||
|
||||
/** challenge_artifact 工件的内容:审的哪份报告、每句的复核,以及反方用的模型 */
|
||||
export interface ChallengeArtifact {
|
||||
/** 被审的报告撰写节点 */
|
||||
report: string
|
||||
doc_artifact: string
|
||||
claims: ChallengeClaim[]
|
||||
model: string | null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user