Files
Agent-Workbench/docs/task-agreement-1.25-evidence/source-bytes/src/client/FileResultsPanel.tsx
T

84 lines
8.7 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 { useEffect, useRef, useState } from 'react';
import { FileCheck2, FileText, LoaderCircle, RefreshCw } from 'lucide-react';
import { api } from './api';
import type { Session, FileResultView } from './types';
import './FileResultsPanel.css';
type View = { taskId: string; results: FileResultView[]; sources: { id: string; taskId: string; runId: string; reason: string; files: { path: string; sha256: string }[] }[] };
type File = { path: string; content: string; sha256: string; bytes: number };
export function FileResultsPanel({ session, busy, onSession, openArtifact }: {
session: Session; busy: boolean; onSession: (value: Session) => void; openArtifact: (id: string) => void;
}) {
const task = session.execution?.task;
const [view, setView] = useState<View | null>(null), [files, setFiles] = useState<string[]>([]);
const [path, setPath] = useState(''), [file, setFile] = useState<File | null>(null), [source, setSource] = useState('');
const [reason, setReason] = useState(''), [notice, setNotice] = useState('');
const [loading, setLoading] = useState(false), [acting, setActing] = useState(false);
const scope = useRef(0), readRequest = useRef(0), viewRequest = useRef(0), flight = useRef(false);
const known = view?.sources.filter(row => row.files.some(file => file.path === path)) ?? [];
const current = view?.results.some(row => row.verification === 'current');
async function load() {
const id = scope.current, request = ++viewRequest.current; setLoading(true);
try {
const [result, inventory] = await Promise.all([api<View>('/sessions/' + session.id + '/file-results' + (path ? '?path=' + encodeURIComponent(path) : '')), api<{ files: { path: string; kind: string; bytes: number }[] }>('/projects/' + session.projectId + '/files')]);
if (scope.current !== id || viewRequest.current !== request) return;
setView(result); setFiles(inventory.files.filter(item => item.kind === 'file' && item.bytes <= 1024 * 1024).map(item => item.path));
} catch (error) { if (scope.current === id && viewRequest.current === request) setNotice(error instanceof Error ? error.message : String(error)); }
finally { if (scope.current === id && viewRequest.current === request) setLoading(false); }
}
useEffect(() => {
scope.current++; readRequest.current++; flight.current = false; setActing(false); setView(null); setPath(''); setFile(null); setSource(''); setReason(''); setNotice(''); void load();
return () => { scope.current++; readRequest.current++; };
}, [session.id, task?.id]);
useEffect(() => { void load(); }, [session.execution?.fileResults?.at(-1)?.id, session.execution?.run?.id, session.status]);
async function choose(value: string) {
setPath(value); setFile(null); setSource(''); setNotice('');
const id = scope.current, request = ++readRequest.current, viewId = ++viewRequest.current;
setLoading(false);
if (!value) return;
try { const [result, currentView] = await Promise.all([api<File>('/projects/' + session.projectId + '/file?path=' + encodeURIComponent(value)), api<View>('/sessions/' + session.id + '/file-results?path=' + encodeURIComponent(value))]);
if (scope.current === id && readRequest.current === request) { setFile(result); if (viewRequest.current === viewId) setView(currentView); }
} catch (error) { if (scope.current === id && readRequest.current === request) setNotice(error instanceof Error ? error.message : String(error)); }
}
async function save(complete = false) {
if (!task || !session.execution?.run || flight.current) return;
if (!complete && (!file || file.path !== path || !reason.trim())) return;
const id = scope.current; viewRequest.current++; setLoading(false); flight.current = true; setActing(true); setNotice('');
try {
const result = await api<View & { session: Session }>('/sessions/' + session.id + '/file-results' + (complete ? '/complete' : ''), {
taskId: task.id, runId: session.execution.run.id,
...(!complete && file ? { files: [{ path: file.path, sha256: file.sha256, ...(source ? { source_result_id: source } : {}) }], reason } : {}),
});
if (scope.current !== id) return;
setView(result); onSession(result.session); setNotice(complete ? '任务已按当前成果结束,原运行和检查记录保留。' : '当前文件已核对并登记;未新增文件改动、测试或Git提交。');
} catch (error) { if (scope.current === id) setNotice(error instanceof Error ? error.message : String(error)); }
finally { if (scope.current === id) { flight.current = false; setActing(false); } }
}
return <section className="task-record-section file-results" aria-label="当前文件成果" aria-busy={loading || acting}>
<div className="record-heading"><FileCheck2 size={15} /><h3>当前文件成果</h3><span>{view?.results.length ?? 0} 份凭据</span></div>
<p className="record-footnote">已有文件可以核对后沿用。成果、实际修改、检查与提交分别保留。</p>
<button type="button" className="text-button" disabled={loading || acting} onClick={() => void load()}>{loading ? <LoaderCircle size={13} className="spin" /> : <RefreshCw size={13} />}刷新成果状态</button>
{view?.results.map(row => <article className="file-result-row" key={row.id}>
<div><strong>{row.verification === 'current' ? '当前文件已核对' : row.verification === 'stale' ? '需要重新核对' : '原凭据需核查'}</strong><small>Task {row.taskId.slice(0, 8)} · Run {row.runId.slice(0, 8)}</small></div>
{row.files.map(item => <div key={item.path}><code>{item.path}</code><small className="file-result-sha">SHA-256 {item.sha256}</small>{item.origin && <p className="record-footnote">采用自 Task {item.origin.taskId.slice(0, 8)} · Run {item.origin.runId.slice(0, 8)}<br />原文件 SHA-256 {item.origin.sha256}</p>}
{row.verification !== 'current' && <button type="button" className="text-button" disabled={busy || acting} onClick={() => { void choose(item.path); setReason(row.reason); }}>重新打开此文件核对</button>}
</div>)}
<p>{row.reason}</p>{row.error && <p className="file-result-error">{row.error}</p>}
<button type="button" className="text-button" onClick={() => openArtifact(row.contractArtifactId)}><FileText size={13} />查看原成果凭据</button>
</article>)}
<details className="file-result-form">
<summary>核对已有文件</summary>
<label>选择项目内文件<select aria-label="成果文件" value={path} disabled={busy || acting || loading} onChange={event => void choose(event.target.value)}><option value="">选择一个文件</option>{files.map(value => <option key={value} value={value}>{value}</option>)}</select></label>
{path && <button type="button" className="text-button" disabled={busy || acting} onClick={() => void choose(path)}><RefreshCw size={13} />重新读取当前文件</button>}
{file && <><p className="record-footnote">{file.bytes.toLocaleString('zh-CN')} 字节 · 当前读取 SHA-256</p><code className="file-result-sha">{file.sha256}</code><pre aria-label="成果文件内容">{file.content.length > 12000 ? file.content.slice(0, 12000) + '\n…界面仅展开前12000字符;核对保存完整文件快照。' : file.content}</pre></>}
{!!known.length && <label>采用来源<select aria-label="成果来源" value={source} disabled={busy || acting} onChange={event => setSource(event.target.value)}><option value="">选择原任务成果</option>{known.map(row => <option key={row.id} value={row.id}>Task {row.taskId.slice(0, 8)} · {row.reason.slice(0, 60)}</option>)}</select></label>}
<label>采用原因<textarea aria-label="成果采用原因" rows={3} maxLength={600} placeholder="说明这个文件满足了当前任务的哪些要求" value={reason} disabled={busy || acting} onChange={event => setReason(event.target.value)} /></label>
<button type="button" className="button secondary" disabled={busy || acting || !file || !reason.trim() || !!known.length && !source} onClick={() => void save()}>{acting ? <LoaderCircle size={14} className="spin" /> : <FileCheck2 size={14} />}核对并采用此文件</button>
</details>
{current && task?.status !== 'completed' && <div className="file-result-finish"><p className="record-footnote">当前要求已满足时可结束。修改要求、原检查和执行计划仍需通过核对。</p><button type="button" className="button secondary" disabled={busy || acting} onClick={() => void save(true)}>按当前成果结束任务</button></div>}
{task?.delivery?.status === 'existing_verified' && <p className="file-result-outcome">结束时采用已有成果 · 无新增修改。当前有效性见逐项核对,检查与Git另列。</p>}
{notice && <p className="file-result-notice" role="status">{notice}</p>}
</section>;
}