Files
Agent-Workbench/docs/model-selection-k-evidence/source-bytes/src/client/McpJobHistory.tsx
T
2026-11-03 15:37:04 +08:00

71 lines
7.3 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 { AlertTriangle, Check, CircleAlert, Clock3, Download, FileText, LoaderCircle, ShieldCheck } from 'lucide-react';
import type { McpJob, Session } from './types';
import { api } from './api';
import { Modal } from './components';
import './McpPanel.css';
const active = ['queued', 'connecting', 'discovering', 'calling', 'closing', 'stopping'];
const labels: Record<string, string> = { queued: '排队', connecting: '正在连接', discovering: '发现工具', calling: '正在调用', closing: '正在关闭', stopping: '正在停止', completed: '完成', failed: '失败', cancelled: '已停止', unknown: '结果待核对' };
const timestamp = (value: string | null) => value ? new Date(value).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }) : '进行中';
type Evidence = { job: McpJob; report: Record<string, unknown> | null; input: { artifactId: string; sha256: string; input: Record<string, unknown> }; evidenceSha256: string; note: string };
export function McpJobHistory({ session, jobs, busy, onSession, onError }: {
session: Session; jobs: McpJob[]; busy: boolean; onSession: (value: Session) => void; onError: (error: unknown) => void;
}) {
const [selected, setSelected] = useState<Evidence | null>(null);
const [review, setReview] = useState<Evidence | null>(null);
const [working, setWorking] = useState(false);
const request = useRef(0);
const currentTask = session.execution?.task?.id;
const visible = jobs.filter(job => job.taskId === currentTask || job.requiresVerification).slice().reverse();
useEffect(() => { request.current++; setSelected(null); setReview(null); setWorking(false); }, [session.id, currentTask]);
async function open(job: McpJob, forReview = false) {
const id = ++request.current; setWorking(true);
try {
const value = await api<Evidence>('/sessions/' + session.id + '/mcp/jobs/' + job.id + '/evidence');
if (request.current === id) { setSelected(value); if (forReview) setReview(value); }
}
catch (error) { if (request.current === id) onError(error); }
finally { if (request.current === id) setWorking(false); }
}
async function acknowledge() {
if (!review) return; const id = ++request.current; setWorking(true);
try {
await api('/sessions/' + session.id + '/mcp/jobs/' + review.job.id + '/acknowledge', { confirm: true, evidence_sha256: review.evidenceSha256 });
const updated = await api<Session>('/sessions/' + session.id);
if (request.current === id) { onSession(updated); setReview(null); }
} catch (error) { if (request.current === id) onError(error); }
finally { if (request.current === id) setWorking(false); }
}
return <>
<section className="task-record-section mcp-history" aria-label="MCP运行记录">
<div className="record-heading"><ShieldCheck size={15} /><h3>MCP 运行记录</h3><span>{visible.length} 项</span></div>
{visible.length ? <div className="mcp-history-list">{visible.map(job => <article className={'mcp-history-row state-' + job.status} key={job.id}>
<div className="mcp-history-title"><div><strong>{job.input?.call?.tool || (job.operation === 'probe' ? '协议与工具探测' : '工具调用')}</strong><small>{timestamp(job.startedAt)} · {job.operation === 'probe' ? '探测' : '调用'}</small></div>
<span className={'mcp-job-state state-' + job.status}>{active.includes(job.status) && <LoaderCircle size={12} className="spin" />}{job.requiresVerification && <CircleAlert size={12} />}{job.status === 'unknown' && job.reviewedAt && !job.requiresVerification ? '已人工核对 · 原结果未知' : labels[job.status] ?? job.status}</span></div>
<div className="mcp-history-meta"><span>Task {job.taskId.slice(0, 8)}</span><span>Run {job.runId.slice(0, 8)}</span><span>PID {job.pid ?? '未启动'}</span>
<span>{job.cleanup?.rootExited ? '所属进程已退出' : job.finishedAt ? '进程退出待核对' : '进程运行中'}</span></div>
{job.error?.message && <p className="mcp-history-error">{job.error.message}</p>}
<div className="mcp-history-actions"><button type="button" className="button secondary" disabled={!job.inputArtifactId || working} onClick={() => void open(job)}><FileText size={14} />{job.reportArtifactId ? '查看原报告' : '查看原始输入'}</button>
{job.requiresVerification && <button type="button" className="button secondary" disabled={busy || working || !job.inputArtifactId} onClick={() => void open(job, true)}><AlertTriangle size={14} />人工核对结果</button>}</div>
</article>)}</div> : <p className="mcp-history-empty"><Clock3 size={15} />当前任务还没有 MCP 操作记录。</p>}
{selected && <section className="mcp-report" aria-label="MCP原报告"><header><strong>{selected.report ? '原始 MCP 报告' : '已封存的原始输入'}</strong><button type="button" className="text-button" onClick={() => setSelected(null)}>关闭</button></header>
<div className="mcp-report-meta"><span>SHA256</span><code>{selected.job.reportSha256 ?? selected.input.sha256}</code><span>Task / Run</span><code>{selected.job.taskId} / {selected.job.runId}</code></div>
<p className="mcp-boundary-note">报告绑定原 Task/Run 和工具输入。独立所属进程不等于 OS 沙箱,报告也不能证明外部程序没有网络或其他副作用。</p>
<details className="mcp-original-input"><summary>查看原始操作输入 · SHA256 {selected.input.sha256.slice(0, 16)}</summary><pre>{JSON.stringify(selected.input.input, null, 2)}</pre></details>
{selected.report ? <pre tabIndex={0}>{JSON.stringify(selected.report, null, 2)}</pre> : <p className="mcp-history-error">{selected.note}</p>}
<button type="button" className="text-button" onClick={() => { const blob = new Blob([JSON.stringify(selected.report ?? selected, null, 2)], { type: 'application/json' }); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = (selected.report ? 'mcp-report-' : 'mcp-recovery-') + selected.job.id + '.json'; link.click(); setTimeout(() => URL.revokeObjectURL(link.href), 1000); }}><Download size={13} />{selected.report ? '下载显示的原报告' : '下载已保存的记录'}</button>
</section>}
</section>
{review && <Modal title="核对未知的外部结果" description="这只记录人工核对,不会把未知结果改成成功。" onClose={() => setReview(null)}>
<div className="mcp-review-warning"><AlertTriangle size={17} /><p>外部副作用可能已经发生。请核对原输入、当前文件和程序状态;历史PID仅供核对,不会被用来停止进程。</p></div>
<p className="mcp-boundary-note">{review.note}</p>
<details className="mcp-original-input"><summary>查看已校验原始输入</summary><pre>{JSON.stringify(review.input.input, null, 2)}</pre></details>
<div className="modal-actions"><button type="button" className="button secondary" disabled={working} onClick={() => setReview(null)}>返回</button><button type="button" className="button primary" disabled={working || !review.evidenceSha256} onClick={() => void acknowledge()}>{working ? <LoaderCircle size={14} className="spin" /> : <Check size={14} />}{review.report ? '我已查看原报告' : '我已核对原输入和进程'}</button></div>
</Modal>}
</>;
}