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([]), [view, setView] = useState(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(`/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(`/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(`/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

恢复检查点

{records.length} 个

保存完整目标、原工具结果和待处理项;续接前核对源码与任务身份。

{notice &&

{notice}

} {records.map(checkpoint =>
{new Date(checkpoint.createdAt).toLocaleString('zh-CN')}

{checkpoint.counts.completed} 项已完成工具 · {checkpoint.counts.unknown} 项待核对 · {checkpoint.counts.pendingChanges} 项待审查

SHA256 {checkpoint.sha256.slice(0, 16)}
)} {view &&
{view.resumable ? '当前可以续接原任务' : '暂不能从此检查点续接'} {view.reasons.length > 0 &&
    {view.reasons.map(reason =>
  • {reason}
  • )}
} {view.resumable && }
}
; }