File size: 8,524 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {useEffect,useState} from 'react';
import type {Actor,ReviewEvent,ReviewQuestion,ScriptReview,SegmentView} from './contract.ts';
import {getJSON} from './api.ts';
import {actorStyle,shapePath} from './visual.ts';
import {adjacentEvent,arrangementList,entityScript,splitLine} from './script-aside.ts';
import {FactsPanel} from './FactsPanel.tsx';
import type {ScriptEvent} from './script-aside.ts';
import './script-aside.css';

const OP_LABELS:Record<string,string>={fact:'读取事实',distance:'计算距离',divide:'相除',multiply:'相乘',identity:'取结果',add:'相加',subtract:'求差',sum:'求和'};
export type ProofDetail={candidate_id:string;decision:{result:string;remaining_margin?:number};proof:{nodes:{id:string;op:string;inputs?:string[];fact_id?:string}[];output:string}|null;facts:{fact_id:string;status:string;source_kind?:string;value?:unknown;error?:number;support_ref?:unknown}[]|null;replay:unknown;binding:unknown;world_span_s:number[]|null;scope:string};
export function QuestionPicker({review,selected,onSelect}:{review:ScriptReview;selected:string|null;onSelect:(q:ReviewQuestion|null)=>void}){
 const [open,setOpen]=useState(false),[status,setStatus]=useState('');
 const questions=review.questions.filter(q=>!status||q.decision===status);
 return <div className="script-questions"><button className="primary" aria-expanded={open} onClick={()=>setOpen(!open)}>看这段出的题 · {review.questions.length}</button><span className="question-counts">PASS {review.question_counts.PASS} · FAIL {review.question_counts.FAIL} · UNKNOWN {review.question_counts.UNKNOWN}</span>{open&&<div className="question-picker"><label>判定 <select aria-label="筛选题目判定" value={status} onChange={e=>{setStatus(e.target.value);onSelect(null);}}><option value="">全部</option>{['PASS','FAIL','UNKNOWN'].map(s=><option key={s}>{s}</option>)}</select></label><select size={Math.min(6,Math.max(2,questions.length))} aria-label="选择题目" value={selected||''} onChange={e=>onSelect(review.questions.find(q=>q.id===e.target.value)||null)}><option value="">选择一道题,高亮它用到的时段</option>{questions.map(q=><option key={q.id} value={q.id}>{q.decision} · {q.text}</option>)}</select></div>}</div>;
}
function Proof({question}:{question:ReviewQuestion}){
 const [value,setValue]=useState<ProofDetail|null>(null),[error,setError]=useState('');
 useEffect(()=>{setValue(null);setError('');const c=new AbortController();getJSON<ProofDetail>(question.proof_url,c.signal).then(setValue).catch(e=>{if(!c.signal.aborted)setError(String(e));});return()=>c.abort();},[question]);
 return <section className="question-proof" data-proof-candidate={question.id}><h3>证明步骤 <span className={'decision-'+question.decision}>{question.decision}</span></h3><p>{question.text}</p><small>{question.version_id}</small>{error?<p role="alert">{error}</p>:!value?<p>读取原证明…</p>:<><p className="review-source">R13 原始计划模型结果;不是正式接收</p>{value.proof?.nodes?.length?<ol>{value.proof.nodes.map(node=><li key={node.id}><code>{node.id}</code> · {OP_LABELS[node.op]||node.op}<small>{node.fact_id?'读取 '+node.fact_id:(node.inputs||[]).join(' → ')}</small></li>)}</ol>:<p>没有证明步骤。</p>}<details><summary>原始事实与重放结果</summary><pre>{JSON.stringify({facts:value.facts,replay:value.replay,decision:value.decision},null,2)}</pre></details></>}</section>;
}
function EntitySymbol({actor}: {actor: Actor}) {
 const {color,shape}=actorStyle(actor);
 return <svg width="18" height="18" viewBox="-1.4 -1.4 2.8 2.8" aria-hidden="true"><path d={shapePath(shape)} fill={color}/></svg>;
}
const EVENT_STATUS:Record<ReviewEvent['status'],string>={effective:'生效',deviated:'偏了',ineffective:'没生效',NOT_CHECKED:'未检查'};
const timeSpan=(event:ReviewEvent)=>event.span_s ? event.span_s.map(t=>Number(t.toFixed(2))).join('–')+' s' : '时段无数据';

export function ScriptReviewPanel({view,review,selected,question,onSelect,onSeek}:{
 view:Pick<SegmentView,'actors'|'intents'|'observers'|'segment'>;review?:ScriptReview;selected:ReviewEvent|null;
 question:ReviewQuestion|null;onSelect:(event:ReviewEvent|null)=>void;onSeek:(time:number)=>void;
}){
 const [tab,setTab]=useState<'situations'|'script'|'facts'>('situations'),[entityId,setEntityId]=useState(view.actors[0]?.id||'');
 const actorId=view.actors.some(a=>a.id===entityId)?entityId:(view.actors[0]?.id||'');
 const [all,setAll]=useState(false),{groups,total,problems}=arrangementList(review,all);
 const events=groups.flatMap(group=>group.events),index=events.findIndex(event=>event.id===selected?.id);
 const script=entityScript(review,view.intents,actorId);
 const select=(event:ReviewEvent|null)=>{onSelect(event);if(event?.span_s)onSeek(event.span_s[0]);};
 const eventRow=(event:ScriptEvent,isScript=false)=><button
   className={'aside-event '+(selected?.id===event.id?'selected':'')} key={event.id}
   aria-pressed={selected?.id===event.id} onClick={()=>select(event)}>
   <span className="aside-event-title">{timeSpan(event)} · {isScript&&event.kind+' · '}{event.label}</span>
   <span className="aside-event-meta">
     {!isScript&&event.entity_ids.map(id=>{const actor=view.actors.find(a=>a.id===id);return <span className="aside-entity" key={id} title={id}>{actor&&<EntitySymbol actor={actor}/>}<span>{id}</span></span>;})}
     <span className="aside-status">{event.statusLabel||EVENT_STATUS[event.status]}</span><span>{event.source}</span>
   </span>
 </button>;
 return <div className="script-review-panel">
   <div className="aside-tabs" aria-label="右侧栏内容">
     <button aria-pressed={tab==='situations'} onClick={()=>setTab('situations')}>安排情形</button>
     <button aria-pressed={tab==='script'} onClick={()=>setTab('script')}>主体剧本</button>
     <button aria-pressed={tab==='facts'} onClick={()=>setTab('facts')}>证明器事实</button>
   </div>
   {question&&<Proof key={question.id} question={question}/>}
   {tab==='situations'?<section aria-label="安排情形">
     {splitLine(view.observers)&&<p className="aside-summary" data-testid="split-layer">{splitLine(view.observers)}</p>}
     <p className="aside-summary">安排 {total} 条 · 没生效或偏了 {problems} 条{groups.length>0&&':'+groups.map(g=>g.label+' '+g.count).join(' · ')}</p>
     <button className="aside-filter" aria-pressed={all} onClick={()=>setAll(x=>!x)}>{all?'只看没生效、偏了':`显示全部 ${total} 条`}</button>
     <div className="aside-navigation">
       <button disabled={index<=0} onClick={()=>select(adjacentEvent(events,selected?.id||null,-1))}>上一条</button>
       <button disabled={!events.length||index===events.length-1} onClick={()=>select(adjacentEvent(events,selected?.id||null,1))}>下一条</button>
     </div>
     {events.length?groups.map(group=><div className="aside-group" key={group.kind}><h3>{group.label} · {group.count}</h3>{group.events.map(event=>eventRow(event))}</div>):
       <p className="review-empty">{total&&!all?`${total} 条安排都生效了`:review?`本段没有安排情形:生成器没计划(planned ${review.planned.length} 条),证据层算出 ${review.situations.length} 段`:'本段没有安排情形数据:生成器计划与证据层均未提供。'}</p>}
     <small className="aside-situation-note">证据层从轨迹算出,表示采样正支持;0 段不解释为真实不可见。{review?.situation_missing.join(';')}</small>
     {review?.situation_source&&<details className="aside-source"><summary>情形计算来源</summary><code>{review.situation_source}</code></details>}
   </section>:tab==='facts'?<FactsPanel segmentId={view.segment.id} onSeek={onSeek}/>:<section aria-label="主体剧本">
     <div className="aside-actors" aria-label="选择主体">{view.actors.map(actor=><button key={actor.id} aria-pressed={actorId===actor.id} onClick={()=>setEntityId(actor.id)}><EntitySymbol actor={actor}/>{actor.id}</button>)}</div>
     <p className="aside-summary">{actorId||'无主体'} · 共 {script.length} 段 · {review?'生成器安排':'原始意图'}</p>
     {script.length?script.map(event=>eventRow(event,true)):<p className="review-empty">{review?.actual_arrangements===null?'生成器未输出主体安排。':'该主体没有剧本记录。'}</p>}
   </section>}
   {selected&&<div className="event-evidence"><button onClick={()=>select(null)}>清除路径高亮</button>{selected.basis&&<p>{selected.basis}</p>}</div>}
 </div>;
}