68 lines
5.2 KiB
TypeScript
68 lines
5.2 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { Archive, FileText, LoaderCircle, Play, RefreshCw, Save } from 'lucide-react';
|
||
import { api } from './api';
|
||
import type { Session, TaskCheckpoint, TaskCheckpointView } from './types';
|
||
|
||
export function TaskCheckpointPanel({ session, busy, onSession, onError, openArtifact }: {
|
||
session: Session; busy: boolean; onSession: (value: Session) => void; onError: (error: unknown) => void; openArtifact: (id: string) => void;
|
||
}) {
|
||
const task = session.execution?.task, run = session.execution?.run;
|
||
const scope = `${session.id}/${task?.id}/${run?.id}`;
|
||
const selectedScope = useRef(scope); selectedScope.current = scope;
|
||
const request = useRef(0);
|
||
const [records, setRecords] = useState<TaskCheckpoint[]>([]), [view, setView] = useState<TaskCheckpointView | null>(null);
|
||
const [working, setWorking] = useState(false), [notice, setNotice] = useState('');
|
||
const disabled = busy || working || ['running', 'awaiting_approval', 'stopping'].includes(session.status);
|
||
useEffect(() => {
|
||
let alive = true; const id = ++request.current; setRecords([]); setView(null); setWorking(false); setNotice('');
|
||
if (task) api<TaskCheckpoint[]>(`/sessions/${session.id}/task-checkpoints`).then(values => {
|
||
if (alive && selectedScope.current === scope && request.current === id) setRecords(values.filter(value => value.taskId === task.id));
|
||
}).catch(error => { if (alive && selectedScope.current === scope) onError(error); });
|
||
return () => { alive = false; request.current++; };
|
||
}, [scope]);
|
||
if (!task || !run) return null;
|
||
async function save() {
|
||
if (!task || !run) return;
|
||
const id = ++request.current; setWorking(true); setNotice('');
|
||
try {
|
||
const value = await api<{ checkpoint: TaskCheckpoint; session: Session }>(`/sessions/${session.id}/task-checkpoints`, { taskId: task.id, runId: run.id });
|
||
if (selectedScope.current === scope && request.current === id) {
|
||
onSession(value.session); setRecords(values => [value.checkpoint, ...values]); setView(null); setNotice('恢复检查点已保存;继续前还会核对当前工程。');
|
||
}
|
||
} catch (error) { if (selectedScope.current === scope && request.current === id) onError(error); }
|
||
finally { if (selectedScope.current === scope && request.current === id) setWorking(false); }
|
||
}
|
||
async function inspect(checkpoint: TaskCheckpoint) {
|
||
const id = ++request.current; setWorking(true); setView(null); setNotice('');
|
||
try { const value = await api<TaskCheckpointView>(`/sessions/${session.id}/task-checkpoints/${checkpoint.id}`);
|
||
if (selectedScope.current === scope && request.current === id) setView(value);
|
||
} catch (error) { if (selectedScope.current === scope && request.current === id) onError(error); }
|
||
finally { if (selectedScope.current === scope && request.current === id) setWorking(false); }
|
||
}
|
||
async function resume() {
|
||
if (!view?.resumable) return;
|
||
const id = ++request.current; setWorking(true);
|
||
try { const value = await api<Session>(`/sessions/${session.id}/task-checkpoints/${view.checkpoint.id}/resume`, { sha256: view.checkpoint.sha256 });
|
||
if (selectedScope.current === scope && request.current === id) onSession(value);
|
||
} catch (error) { if (selectedScope.current === scope && request.current === id) onError(error); }
|
||
finally { if (selectedScope.current === scope && request.current === id) setWorking(false); }
|
||
}
|
||
return <section className="task-record-section task-checkpoints" aria-label="任务恢复检查点">
|
||
<div className="record-heading"><Archive size={15} /><h3>恢复检查点</h3><span>{records.length} 个</span></div>
|
||
<p className="record-footnote">保存完整目标、原工具结果和待处理项;续接前核对源码与任务身份。</p>
|
||
<button type="button" className="button secondary" onClick={() => void save()} disabled={disabled}>{working ? <LoaderCircle size={13} className="spin" /> : <Save size={13} />}保存恢复检查点</button>
|
||
{notice && <p className="record-footnote" role="status">{notice}</p>}
|
||
{records.map(checkpoint => <article className="checkpoint-card" key={checkpoint.id}>
|
||
<strong>{new Date(checkpoint.createdAt).toLocaleString('zh-CN')}</strong>
|
||
<p>{checkpoint.counts.completed} 项已完成工具 · {checkpoint.counts.unknown} 项待核对 · {checkpoint.counts.pendingChanges} 项待审查</p>
|
||
<code title={checkpoint.sha256}>SHA256 {checkpoint.sha256.slice(0, 16)}</code>
|
||
<div className="context-actions"><button type="button" className="text-button" disabled={disabled} onClick={() => void inspect(checkpoint)}><RefreshCw size={13} />核对恢复条件</button>
|
||
<button type="button" className="text-button" onClick={() => openArtifact(checkpoint.artifactId)}><FileText size={13} />查看原恢复包</button></div>
|
||
</article>)}
|
||
{view && <div className="checkpoint-assessment" role="status"><strong>{view.resumable ? '当前可以续接原任务' : '暂不能从此检查点续接'}</strong>
|
||
{view.reasons.length > 0 && <ul>{view.reasons.map(reason => <li key={reason}>{reason}</li>)}</ul>}
|
||
{view.resumable && <button type="button" className="button primary" disabled={disabled} onClick={() => void resume()}><Play size={13} />从已核对检查点继续</button>}
|
||
</div>}
|
||
</section>;
|
||
}
|