Files
Agent-Workbench/src/client/EvaluationPanel.tsx
T

99 lines
20 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useRef, useState } from 'react';
import { ArrowUpRight, ClipboardCheck, Download, GitBranch, LoaderCircle, Plus, RefreshCw, Square } from 'lucide-react';
import { api, downloadBlob } from './api';
import { Modal } from './components';
import type { Config, Session, EvaluationBatch, EvaluationSample, EvaluationReport, EvaluationRole } from './types';
import './EvaluationPanel.css';
const live = ['preparing','baseline_running','candidate_running','collecting','adopting'];
const labels: Record<string,string> = {pending:'未执行',planned:'计划已封存',ready:'同源工作区就绪',preparing:'准备同源工程',baseline_running:'原版检查中',baseline_complete:'原版结果已保存',candidate_running:'候选执行中',awaiting_review:'等待改动审查',awaiting_approval:'等待检查审批',candidate_complete:'候选完成',paused:'候选已暂停',failed:'失败',stopped:'已停止',unknown:'结果待核对',sealed:'报告已封存',between_samples:'等待下一样本',completed:'本批已封存',retained:'已保留现场结束',collecting:'核对原证据',adopting:'准备采纳',awaiting_delivery:'等待父工程交付',passed:'通过'};
const label = (status:string) => labels[status] ?? status;
const split = (value:string) => value.split(/[\n,,]/).map(value=>value.trim()).filter(Boolean);
const draft = () => ({label:'',goal:'',criteria:'',paths:'',checks:'test, build'});
const number = (value:number) => value.toLocaleString('zh-CN');
const elapsed = (role:EvaluationRole) => role.startedAt && role.finishedAt ? Math.max(0,Math.round((Date.parse(role.finishedAt)-Date.parse(role.startedAt))/1000))+' 秒' : '未记录完整起止';
export function EvaluationPanel({session,config,busy,onSession,onNavigate,onReview,onRecovery}: {
session:Session;config:Config;busy:boolean;onSession:(session:Session)=>void;
onNavigate:(projectId:string,sessionId:string,view?:'task'|'changes'|'approval')=>Promise<void>;onReview:()=>void;onRecovery:()=>void;
}) {
const [batches,setBatches]=useState<EvaluationBatch[]>([]),[selected,setSelected]=useState(''),[sampleId,setSampleId]=useState('');
const [loading,setLoading]=useState(true),[action,setAction]=useState(''),[error,setError]=useState(''),[creating,setCreating]=useState(false);
const [title,setTitle]=useState(''),[samples,setSamples]=useState([draft()]);
const [evidenceMode,setEvidenceMode]=useState('configured-model');
const [tokens,setTokens]=useState(config.maxRunTokens),[steps,setSteps]=useState(Math.min(200,config.maxSteps*2)),[tools,setTools]=useState(96),[minutes,setMinutes]=useState(Math.floor(config.maxRunDurationMs/60000));
const [confirmation,setConfirmation]=useState<{batchId:string;sampleId:string;operation:string}|null>(null),[report,setReport]=useState<EvaluationReport|null>(null);
const scope=useRef(0),fetching=useRef(false),currentBatch=useRef(''),currentSample=useRef('');
const load=useCallback(async()=>{
if(fetching.current)return;fetching.current=true;const generation=scope.current;
try{const view=await api<{batches:EvaluationBatch[]}>('/sessions/'+session.id+'/evaluations');if(generation===scope.current)setBatches(view.batches);}
catch(error){if(generation===scope.current)setError(error instanceof Error?error.message:String(error));}
finally{if(generation===scope.current){fetching.current=false;setLoading(false);}}
},[session.id]);
useEffect(()=>{scope.current++;fetching.current=false;setBatches([]);setSelected('');setSampleId('');setReport(null);void load();return()=>{scope.current++;};},[load]);
useEffect(()=>{void load();},[session.revision,load]);
const batch=batches.find(batch=>batch.id===selected)??batches.at(-1),sample=batch?.samples.find(sample=>sample.id===sampleId)??batch?.samples.find(sample=>!sample.report)??batch?.samples.at(-1);
currentBatch.current=batch?.id??'';
currentSample.current=sample?.id??'';
const running=!!batch&&live.includes(batch.status);
useEffect(()=>{if(!running)return;const timer=setInterval(()=>void load(),1200);return()=>clearInterval(timer);},[running,load]);
useEffect(()=>{setConfirmation(null);setReport(null);setError('');},[session.id,batch?.id,sample?.id]);
async function execute(operation:string,target=batch,item=sample){
if(!target||currentBatch.current!==target.id)return;const generation=scope.current;setAction(operation);setError('');setConfirmation(null);
try{const reply=await api<{session:Session}>('/sessions/'+session.id+'/evaluations/'+target.id+'/'+operation,{fingerprint:target.fingerprint,...(item?{sampleId:item.id}:{})});if(generation===scope.current){onSession(reply.session);await load();}}
catch(error){if(generation===scope.current)setError(error instanceof Error?error.message:String(error));}
finally{if(generation===scope.current)setAction('');}
}
async function create(){const generation=scope.current;setAction('create');setError('');
try{const reply=await api<{batch:EvaluationBatch;session:Session}>('/sessions/'+session.id+'/evaluations',{title,samples:samples.map(sample=>({...sample,paths:split(sample.paths),checks:split(sample.checks)})),budget:{maxTokens:tokens,maxSteps:steps,maxToolCalls:tools,maxDurationMs:minutes*60000},evidenceMode});if(generation===scope.current){onSession(reply.session);setSelected(reply.batch.id);setCreating(false);await load();}}
catch(error){if(generation===scope.current)setError(error instanceof Error?error.message:String(error));}
finally{if(generation===scope.current)setAction('');}
}
async function showReport(){if(!batch||!sample)return;const generation=scope.current,target=batch.id,id=sample.id;setAction('report');setError('');
try{const result=await api<EvaluationReport>('/sessions/'+session.id+'/evaluations/'+batch.id+'/report?sampleId='+sample.id);if(generation===scope.current&&currentBatch.current===target&&currentSample.current===id)setReport(result);}
catch(error){if(generation===scope.current)setError(error instanceof Error?error.message:String(error));}finally{if(generation===scope.current)setAction('');}
}
const confirm=(operation:string)=>{if(batch&&sample)setConfirmation({batchId:batch.id,sampleId:sample.id,operation});};
const canCreate=!busy&&!action&&(!batch||['completed','retained'].includes(batch.status));
return <div className="evaluation-console" aria-label="工程评测控制台">
<header className="evaluation-heading"><div><p className="evaluation-kicker">工程质量 · 固定样本</p><h2>同一份需求,核对改进前后</h2><p>封存来源与原测试,在隔离工程里生成候选,再把真实结果带回审查。</p></div><button className="button secondary" disabled={loading||!!action} onClick={()=>void load()}><RefreshCw size={14}/>刷新</button></header>
{error&&<div className="evaluation-error" role="alert">{error}</div>}
{loading?<p role="status"><LoaderCircle size={15} className="spin"/>正在读取评测…</p>:<>
{!!batches.length&&<label className="evaluation-history">评测记录<select aria-label="选择评测记录" value={batch?.id??''} onChange={event=>{setSelected(event.target.value);setSampleId('');setCreating(false);}}>{batches.map(batch=><option key={batch.id} value={batch.id}>{batch.title} · {label(batch.status)}</option>)}</select></label>}
{(!batch||creating)?<section className="evaluation-form" aria-label="新建工程评测">
<label>评测名称<input value={title} maxLength={120} onChange={event=>setTitle(event.target.value)} placeholder="例如 修复导入边界并保持原测试"/></label>
<label>评测来源<select aria-label="评测来源" value={evidenceMode} onChange={event=>setEvidenceMode(event.target.value)}><option value="configured-model">实际配置模型 · 保留真实结果</option><option value="local-fixture">本地固定响应 · 只验证执行链</option></select></label>
<p className="evaluation-note">每批 1–3 个样本,按队列显式推进。每个样本都从当前已提交 HEAD 检出;原暂存和未提交内容留在来源工程。</p>
{samples.map((sample,index)=><fieldset key={index}><legend>固定样本 {index+1}</legend>{(['label','goal','criteria','paths','checks'] as const).map(key=><label key={key}>{({label:'样本名称',goal:'具体需求',criteria:'验收条件',paths:'允许修改的源码文件',checks:'完整检查名称'})[key]}{key==='label'?<input value={sample[key]} maxLength={64} onChange={event=>setSamples(values=>values.map((value,i)=>i===index?{...value,[key]:event.target.value}:value))}/>:<textarea aria-label={({label:'样本名称',goal:'具体需求',criteria:'验收条件',paths:'允许修改的源码文件',checks:'完整检查名称'})[key]} rows={key==='goal'||key==='criteria'?3:2} maxLength={key==='goal'?6000:4000} value={sample[key]} placeholder={key==='paths'?'例如 src/parser.mjs,每行一个已有文件':key==='checks'?'test, typecheck, build;Python 使用 python:test, python:compile':undefined} onChange={event=>setSamples(values=>values.map((value,i)=>i===index?{...value,[key]:event.target.value}:value))}/>}</label>)}{samples.length>1&&<button className="text-button" onClick={()=>setSamples(values=>values.filter((_,i)=>i!==index))}>移除此未保存样本</button>}</fieldset>)}
{samples.length<3&&<button className="button secondary" onClick={()=>setSamples(values=>[...values,draft()])}><Plus size={14}/>添加固定样本</button>}
<div className="evaluation-budget"><label>总 token 预算<input type="number" min={samples.length*2000} max={Math.min(config.maxTaskTokens,config.maxRunTokens*samples.length)} value={tokens} onChange={event=>setTokens(Number(event.target.value))}/></label><label>总请求轮次<input type="number" min={samples.length*2} value={steps} onChange={event=>setSteps(Number(event.target.value))}/></label><label>总工具次数<input type="number" min={samples.length*2} value={tools} onChange={event=>setTools(Number(event.target.value))}/></label><label>总时限(分钟)<input type="number" min={1} max={Math.floor(config.maxRunDurationMs/60000)} value={minutes} onChange={event=>setMinutes(Number(event.target.value))}/></label></div>
<p className="evaluation-note">预算按样本均分,续接保留累计用量。总时限从首次原版检查开始,包含审查等待。候选不修改测试、脚本或运行配置。</p>
<div className="evaluation-actions"><button className="button" disabled={!canCreate||!title.trim()||samples.some(sample=>!sample.label.trim()||!sample.goal.trim()||!sample.criteria.trim()||!split(sample.paths).length||!split(sample.checks).length)} onClick={()=>void create()}><ClipboardCheck size={15}/>封存评测计划</button>{creating&&<button className="button secondary" onClick={()=>setCreating(false)}>返回原评测</button>}</div>
</section>:sample&&<>
<section className="evaluation-source"><div><strong>{batch.title}</strong><span className="evaluation-status">{label(batch.status)}</span></div><p>{batch.message}</p><dl><div><dt>固定来源</dt><dd><GitBranch size={13}/>{batch.source.branch} · <code>{batch.source.head.slice(0,12)}</code></dd></div><div><dt>候选模型</dt><dd>{batch.model.mode==='demo'?'离线演示,不能验证模型能力':batch.model.model}</dd></div><div><dt>完整样本分母</dt><dd>{batch.summary.finished} / {batch.summary.samples} 已封存 · 通过 {batch.summary.candidate.passed} · 失败 {batch.summary.candidate.failed} · 未知 {batch.summary.candidate.unknown} · 未执行 {batch.summary.candidate.pending}</dd></div></dl><p className="evaluation-note">{batch.evidenceMode==='local-fixture'?'本批使用本地固定响应,仅验证执行链和真实检查。':batch.summary.scope}</p></section>
<div className="evaluation-workspace"><nav aria-label="固定样本队列">{batch.samples.map((item,index)=><button key={item.id} aria-pressed={sample.id===item.id} onClick={()=>{setSampleId(item.id);setReport(null);}}><small>样本 {index+1}</small><strong>{item.label}</strong><span>{label(item.status)}</span>{item.report&&<span>原版 {label(item.report.summary.original)} → 候选 {label(item.report.summary.candidate)}</span>}</button>)}</nav>
<article className="evaluation-sample"><h3>{sample.label}</h3><p className="evaluation-goal">{sample.goal}</p><div className="evaluation-criteria"><strong>验收条件</strong><p>{sample.criteria}</p></div><div className="evaluation-paths">{sample.paths.map(path=><code key={path}>{path}</code>)}</div>
{sample.error&&<p className="evaluation-error">{sample.error}</p>}
<details className="evaluation-commands" open={sample.status==='ready'}><summary>固定检查 · {sample.checks.join(' / ')}</summary>{sample.commands?.map(command=><div key={command.name}><strong>{command.command}</strong><pre>{command.script}</pre>{command.lifecycle?.map(step=><p key={step.name}>{step.name}:<code>{step.script}</code></p>)}</div>)??<p>工作区准备完成后显示实际命令,执行前需要核对。</p>}</details>
{!!sample.workers?.length&&<div className="evaluation-lanes">{sample.workers.map(worker=><section key={worker.id} aria-label={worker.evaluation?.role==='original'?'原版工程':'候选工程'}><div className="evaluation-lane-title"><h4>{worker.evaluation?.role==='original'?'原版':'候选'}</h4><span>{worker.evaluation?.role==='original'?'只跑固定检查':label(worker.sessionStatus==='awaiting_approval'?'awaiting_approval':worker.status)}</span></div><p>{worker.summary}</p>{worker.status!=='unprepared'&&<button className="button secondary" onClick={()=>void onNavigate(worker.projectId,worker.sessionId,worker.sessionStatus==='awaiting_approval'?'approval':worker.sessionStatus==='needs_review'?'changes':'task')}>{worker.sessionStatus==='awaiting_approval'?'核对并审批检查':worker.sessionStatus==='needs_review'?'审查候选改动':'打开原任务'}<ArrowUpRight size={13}/></button>}</section>)}</div>}
{!!sample.workers?.length&&<p className="evaluation-note">新检出的工程如缺依赖,可先打开两侧原任务,在「工程环境与依赖」分别核对并准备;两侧使用同一锁文件及运行时,再执行原版检查。</p>}{sample.contractSha256&&<p className="evaluation-note">同源输入已封存 · SHA-256 <code>{sample.contractSha256.slice(0,16)}</code></p>}
<div className="evaluation-actions">
{sample.status==='pending'&&<button className="button" disabled={!!action||running||busy||batch.samples.slice(0,batch.samples.indexOf(sample)).some(sample=>!sample.report)} onClick={()=>confirm('prepare')}>准备同源工作区</button>}
{sample.status==='ready'&&<button className="button" disabled={!!action||running} onClick={()=>confirm('baseline')}>核对并运行原版</button>}
{['baseline_complete','paused','awaiting_review','failed','stopped'].includes(sample.status)&&!!sample.baselineJobId&&!sample.report&&<button className="button" disabled={!!action||running||config.mode==='demo'||sample.workers?.some(worker=>worker.evaluation?.role==='candidate'&&worker.sessionStatus==='needs_review')} onClick={()=>confirm('start')}>{sample.status==='baseline_complete'?'开始候选改进':'继续候选任务'}</button>}
{running&&<button className="button secondary" disabled={action==='stop'} onClick={()=>void execute('stop')}><Square size={14}/>停止本批执行</button>}
{!running&&!sample.report&&sample.status!=='pending'&&<button className="button secondary" disabled={!!action} onClick={()=>confirm('seal')}>封存当前结果</button>}
{sample.report&&<button className="button secondary" disabled={!!action} onClick={()=>void showReport()}>查看原版 / 候选对照</button>}
{sample.report?.summary.candidate==='passed'&&!batch.adoption&&batch.samples.every(sample=>sample.report)&&<button className="button" disabled={!!action||running||batch.status==='retained'} onClick={()=>confirm('adopt')}>采纳此候选,准备父工程审查</button>}
{batch.status==='unknown'&&<button className="button secondary" onClick={onRecovery}>打开原协作核对现场</button>}
</div>
</article></div>
{batch.adoption&&<section className="evaluation-delivery"><h3>父工程交付</h3><p>{batch.adoption.delivery?'已完成本地提交 '+batch.adoption.delivery.commit:batch.message}</p>{batch.status==='awaiting_delivery'&&<div className="evaluation-actions"><button className="button secondary" onClick={onReview}>审查父工程变更集</button><button className="button secondary" disabled={!!action||busy} onClick={()=>void execute('finalize')}>核对父工程检查与 Git</button></div>}</section>}
<footer className="evaluation-actions">{!running&&!['completed','retained'].includes(batch.status)&&<button className="text-button" disabled={!!action} onClick={()=>confirm('retain')}>保留现场,结束本批</button>}{canCreate&&<button className="button secondary" onClick={()=>setCreating(true)}><Plus size={14}/>新建下一批评测</button>}</footer>
</>}
</>}
{confirmation&&batch&&sample&&<Modal title="确认评测操作" onClose={()=>setConfirmation(null)}><div className="evaluation-confirm"><h3>{sample.label}</h3><p>{({prepare:'从封存 HEAD 创建两个独立 Git 工作区,保留来源原修改。',baseline:'请核对上方完整检查命令。将以本机账户执行原工程程序,不请求模型。',start:'将请求封存模型并使用剩余预算。候选提案仍需审查,工程检查仍需审批。',seal:'保存当前全部尝试、失败、停止与未知。封存后该样本不能再执行或修改。',adopt:'核对原通过报告及当前来源后,生成父工程待审查变更集。应用、父检查和 Git 交付仍需确认。',retain:'保留原任务、工作区和全部结果,结束本批;未执行样本仍计入原分母。'} as Record<string,string>)[confirmation.operation]}</p><button className="button" disabled={!!action} onClick={()=>{if(confirmation.batchId===batch.id&&confirmation.sampleId===sample.id)void execute(confirmation.operation);}}>确认{({prepare:'准备',baseline:'运行原版',start:'开始候选',seal:'封存结果',adopt:'准备采纳',retain:'保留现场结束'} as Record<string,string>)[confirmation.operation]}</button></div></Modal>}
{report&&sample&&<Modal title="原版与候选对照" wide onClose={()=>setReport(null)}><div className="evaluation-report"><div className="evaluation-report-heading"><div><h3>{report.label}</h3>{report.evidenceMode==='local-fixture'&&<p className="evaluation-note">本地固定响应 · 执行链验收</p>}<p>{report.inputVerified?'原测试与非候选输入已核对':report.inputError??'原输入无法核对'}</p></div><button className="button secondary" onClick={()=>downloadBlob(new Blob([JSON.stringify(report,null,2)],{type:'application/json'}),'agent-workbench-evaluation-'+report.sampleId+'.json')}><Download size={14}/>导出原报告</button></div><div className="evaluation-lanes">{(['original','candidate'] as const).map(role=>{const result=report[role];return <section key={role}><div className="evaluation-lane-title"><h4>{role==='original'?'原版':'候选'}</h4><strong className={'evaluation-outcome '+result.outcome}>{label(result.outcome)}</strong></div><p>{result.reason??result.evidenceError}</p><dl><div><dt>用时</dt><dd>{elapsed(result)}</dd></div><div><dt>模型请求</dt><dd>{result.usage?.requests??'未知'}</dd></div><div><dt>上游用量</dt><dd>{result.usage?.reported?number(result.usage.totalTokens)+' tokens':'未知;保留预算估算'}</dd></div><div><dt>费用</dt><dd>未报告</dd></div><div><dt>实际人工操作</dt><dd>审批 {result.approvals?.filter(approval=>approval.status==='approved').length??0} 次 · 应用提案 {result.changes?.filter(change=>change.status==='applied').length??0} 项</dd></div></dl>{result.attempts.length?result.attempts.map((attempt,index)=><div className="evaluation-attempt" key={attempt.id}><strong>检查尝试 {index+1} · {label(attempt.status)}</strong>{attempt.results.map(check=><div key={check.logArtifactId}><span>{check.name} · {check.status?label(check.status):'证据缺失'}</span>{check.tests&&<span>测试 {check.tests.passed}/{check.tests.total} · 失败 {check.tests.failed}</span>}<code>{check.logSha256?.slice(0,16)??'原日志无法核对'}</code></div>)}</div>):<p>没有完整检查结果;保持未知。</p>}</section>;})}</div><p className="evaluation-note">{report.environmentComparison&&<span>依赖环境:{report.environmentComparison.matched?'原版与候选实际输入一致':'缺少相同环境的有效证据'}。 </span>}{report.note}</p><details><summary>报告原字段与工件引用</summary><pre>{JSON.stringify(report,null,2)}</pre></details></div></Modal>}
</div>;
}