import { useEffect, useRef, useState } from 'react'; import { Check, Circle, FileText, GitPullRequestArrow, LoaderCircle, Plus, RefreshCw } from 'lucide-react'; import { api } from './api'; import { Modal, Status } from './components'; import type { Session, TaskPlanReply, TaskPlanStep, TaskPlanView } from './types'; import './TaskPlanPanel.css'; const kinds: Record = { analysis: '读取工程', changes: '源码改动', results: '当前文件成果', checks: '工程检查', browser: '页面验收' }; const readTools = { inspect_project: '识别工程', read_file: '读取文件', list_files: '文件清单', search_files: '搜索文件', index_project: '工程索引', query_code: '定位符号', read_research_evidence: 'REA原结果', inspect_engine_checks: '引擎核验', read_browser_result: '原页面记录' }; const browserActions = { fill: '填写', select: '选择', click: '点击', assert_text: '文本断言', assert_count: '数量断言', assert_visible: '可见断言' }; const cleanStep = (step: TaskPlanStep): TaskPlanStep => ({ key: step.key, title: step.title, criterion: step.criterion, kind: step.kind, depends_on: [...step.depends_on], ...(step.kind === 'analysis' ? { tools: [...(step.tools ?? [])] } : ['changes', 'results'].includes(step.kind) ? { paths: (step.paths ?? []).map(value => value.trim()).filter(Boolean) } : step.kind === 'checks' ? { checks: (step.checks ?? []).map(value => value.trim()).filter(Boolean) } : { viewport_width: step.viewport_width, actions: [...(step.actions ?? [])] }) }); const freshStep = (kind: TaskPlanStep['kind'] = 'analysis'): TaskPlanStep => ({ key: 'step_' + crypto.randomUUID().slice(0, 8), title: kinds[kind], criterion: '', kind, depends_on: [], ...(kind === 'analysis' ? { tools: ['inspect_project'] } : ['changes', 'results'].includes(kind) ? { paths: [] } : kind === 'checks' ? { checks: ['test', 'build'] } : { viewport_width: 1280, actions: ['assert_visible'] }) }); const scopeText = (step: TaskPlanStep) => step.kind === 'analysis' ? (step.tools ?? []).map(tool => readTools[tool as keyof typeof readTools] ?? tool).join(' / ') : ['changes', 'results'].includes(step.kind) ? step.paths?.join('、') : step.kind === 'checks' ? step.checks?.join(' / ') : `${step.viewport_width}px · ${(step.actions ?? []).map(action => browserActions[action as keyof typeof browserActions]).join(' / ')}`; export function TaskPlanPanel({ 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 identity = `${session.projectId}/${session.id}/${task?.id}/${run?.id}`; const selection = useRef(identity); selection.current = identity; const requests = useRef(0); const [view, setView] = useState(null); const [loading, setLoading] = useState(false), [mutating, setMutating] = useState(false); const [editor, setEditor] = useState(false), [draft, setDraft] = useState([]), [fixed, setFixed] = useState(0); const [picked, setPicked] = useState>({}), [notice, setNotice] = useState(''); const currentPlan = session.execution?.taskPlans?.at(-1)?.id, currentProof = session.execution?.taskProofs?.at(-1)?.id; const disabled = busy || mutating || ['running', 'awaiting_approval', 'stopping'].includes(session.status); useEffect(() => { selection.current = identity; setView(null); setLoading(false); setEditor(false); setMutating(false); setPicked({}); setNotice(''); requests.current++; return () => { requests.current++; if (selection.current === identity) selection.current = ''; }; }, [identity]); useEffect(() => { const timer = setTimeout(() => { void load(); }, 120); return () => clearTimeout(timer); }, [identity, currentPlan, currentProof, session.status]); async function load() { if (!task) return; const request = ++requests.current, scope = identity; setLoading(true); try { const value = await api(`/sessions/${session.id}/task-plan?taskId=${task.id}`); if (selection.current === scope && requests.current === request) setView(value); } catch (error) { if (selection.current === scope && requests.current === request) onError(error); } finally { if (selection.current === scope && requests.current === request) setLoading(false); } } function edit() { const steps = view?.steps.map(cleanStep) ?? []; setFixed(steps.length); setDraft(steps.length ? steps : [freshStep()]); setNotice(''); setEditor(true); } function update(index: number, patch: Partial) { setDraft(steps => steps.map((step, at) => at === index ? { ...step, ...patch } : step)); } async function save() { if (!task || !run || !view) return; const scope = identity; setMutating(true); setNotice(''); requests.current++; try { const value = await api(`/sessions/${session.id}/task-plans`, { taskId: task.id, runId: run.id, expected_revision: view.revision, steps: draft.map(cleanStep) }); if (selection.current === scope) { onSession(value.session); setView(value.plan); setEditor(false); setNotice('执行计划已保存,按实际结果核验步骤。'); } } catch (error) { if (selection.current === scope) onError(error); } finally { if (selection.current === scope) { setMutating(false); setLoading(false); } } } async function verify(stepKey: string, evidenceId: string) { if (!task || !run || !view?.plan_id || !evidenceId) return; const scope = identity; setMutating(true); setNotice(''); requests.current++; try { const value = await api(`/sessions/${session.id}/task-proofs`, { taskId: task.id, runId: run.id, plan_id: view.plan_id, expected_revision: view.revision, step_key: stepKey, evidence_id: evidenceId }); if (selection.current === scope) { onSession(value.session); setView(value.plan); setNotice(value.verification?.status === 'completed' ? '原结果核验完成,没有再次执行工具。' : value.verification?.message ?? '核验未通过,请查看原结果。'); } } catch (error) { if (selection.current === scope) onError(error); } finally { if (selection.current === scope) { setMutating(false); setLoading(false); } } } return

执行计划

{view?.configured ? `${view.verified_steps ?? 0} / ${view.total_steps ?? view.steps.length}` : '按结果核验'}
{notice &&

{notice}

} {!view ?

正在核对任务计划。

: !view.configured ?

{task?.status === 'completed' ? '本任务未设置执行计划。新任务可先固定步骤与通过条件。' : '先固定要完成的步骤,再用读取、改动、检查或页面结果核验。'}

: <>
第 {view.revision} 版
{view.error &&

{view.error}

} {view.requirements_current === false &&

任务要求已更新。保留原步骤,补充新要求后重新核验仍有效的结果。

}
    {view.steps.map((step, index) => { const evidenceId = picked[step.key] ?? step.evidence?.id ?? step.candidates?.find(candidate => ['completed', 'passed', 'applied'].includes(candidate.status))?.id ?? step.candidates?.[0]?.id ?? ''; const blockers = step.blockers.map(key => view.steps.find(value => value.key === key)?.title ?? key); return
  1. {index + 1}. {step.title}

    {step.criterion}

    {kinds[step.kind]} · {scopeText(step)} {!!step.depends_on.length &&

    前置:{step.depends_on.map(key => view.steps.find(value => value.key === key)?.title ?? key).join('、')}

    } {!!blockers.length &&

    先核验:{blockers.join('、')}

    } {step.error &&

    {step.error}

    } {step.last_rejection &&
    最近一次核验未通过

    {step.last_rejection.message}

    }
    {step.status === 'verified' ? '查看核验依据' : '选择实际结果'} {step.proof_artifact_id && } {step.evidence?.artifacts?.map(artifact => )}
  2. ; })}
{view.contract_artifact_id && }

核验依据为实际操作和当前文件;业务含义仍需独立验收。

} {editor && { if (!mutating) setEditor(false); }}>
{ event.preventDefault(); void save(); }} className="plan-editor">
{draft.map((step, index) =>
步骤 {index + 1}{index < fixed ? ' · 已封存' : ''}