import { useEffect, useRef, useState } from 'react'; import { ArrowRight, ArrowUpLeft, Download, FileSearch, Fingerprint, LoaderCircle, Play, RefreshCw, ShieldCheck, Square } from 'lucide-react'; import { api, downloadArtifact, downloadResearchDocument } from './api'; import { Modal } from './components'; import type { ArtifactPreview, Project, ReaAvailability, ResearchJob, ResearchPage, ResearchTarget, Session } from './types'; import './ResearchPanel.css'; const active = ['queued', 'connecting', 'analyzing', 'stopping']; const operations = [ { id: 'javascript', extensions: ['.asar'], label: 'JavaScript / Electron' }, { id: 'pe-imports', extensions: ['.exe', '.dll'], label: 'PE 导入表' }, { id: 'pe-resources', extensions: ['.exe', '.dll'], label: 'PE 静态资源' }, ]; const states: Record = { queued: '已排队', connecting: '核对 REA 连接', analyzing: '正在静态分析', stopping: '正在停止', completed: '原证据已核验', stale: '工件版本变化', unverified: '原响应尚未核验', failed: '分析未完成', cancelled: '已停止', unknown: '结果待核对', interrupted: '已中断' }; const date = (value: string) => new Date(value).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); const escapeField = (value: string) => value.replace(/~/g, '~0').replace(/\//g, '~1'); type Preview = { job: ResearchJob; kind: 'evidence' | 'raw' | 'trace'; page: ResearchPage }; export function ResearchPanel({ project, session, busy, onSession, onError }: { project: Project; session: Session; busy: boolean; onSession: (value: Session) => void; onError: (error: unknown) => void }) { const [availability, setAvailability] = useState(null); const [filename, setFilename] = useState(''), [label, setLabel] = useState(''), [goal, setGoal] = useState(''); const [choices, setChoices] = useState>({}); const [pending, setPending] = useState(false), [checking, setChecking] = useState(false), [viewBusy, setViewBusy] = useState(false); const [retry, setRetry] = useState(null), [preview, setPreview] = useState(null); const mounted = useRef(true), request = useRef(0), scope = useRef(0), viewRequest = useRef(0); const taskId = session.execution?.task?.id ?? null; const targets = session.execution?.researchTargets ?? [], jobs = (session.execution?.researchJobs ?? []).slice().reverse(); const running = jobs.some(job => active.includes(job.status)), disabled = busy || running || pending; useEffect(() => { mounted.current = true; return () => { mounted.current = false; request.current++; scope.current++; viewRequest.current++; }; }, []); useEffect(() => { scope.current++; viewRequest.current++; setPreview(null); setRetry(null); setPending(false); setViewBusy(false); }, [taskId]); useEffect(() => { void detect(); }, []); async function detect() { const current = ++request.current; setChecking(true); try { const value = await api('/research/availability'); if (mounted.current && current === request.current) setAvailability(value); } catch (error) { if (mounted.current && current === request.current) onError(error); } finally { if (mounted.current && current === request.current) setChecking(false); } } async function register() { const current = scope.current; setPending(true); try { const value = await api<{ target: ResearchTarget; session: Session }>('/sessions/' + session.id + '/research/targets', { path: filename.trim(), ...(label.trim() ? { label: label.trim() } : {}), ...(goal.trim() ? { goal: goal.trim() } : {}) }); if (!mounted.current || current !== scope.current) return; onSession(value.session); setFilename(''); setLabel(''); setGoal(''); } catch (error) { if (mounted.current && current === scope.current) onError(error); } finally { if (mounted.current && current === scope.current) setPending(false); } } async function analyze(targetId: string, operation: string, refresh = false) { const current = scope.current; setPending(true); try { const value = await api<{ job: ResearchJob; session: Session }>('/sessions/' + session.id + '/research/jobs', { targetId, operation, refresh }); if (mounted.current && current === scope.current) { onSession(value.session); setRetry(null); } } catch (error) { if (mounted.current && current === scope.current) onError(error); } finally { if (mounted.current && current === scope.current) setPending(false); } } async function stop() { const current = scope.current; setPending(true); try { const value = await api('/sessions/' + session.id + '/research/stop', {}); if (mounted.current && current === scope.current) onSession(value); } catch (error) { if (mounted.current && current === scope.current) onError(error); } finally { if (mounted.current && current === scope.current) setPending(false); } } async function openView(job: ResearchJob, kind: Preview['kind'] = 'evidence', pointer = '/normalized_result', offset = 0) { const current = ++viewRequest.current, currentScope = scope.current; setViewBusy(true); try { let page: ResearchPage; if (kind === 'trace') { const value = await api('/artifacts/' + job.traceArtifactId + '?session=' + session.id + '&offset=' + offset + '&limit=4000'); page = { content: value.content, offset: value.offset, next_offset: value.offset + value.content.length, total_characters: value.totalCharacters, truncated: value.truncated, source_document_sha256: value.sha256 }; } else page = await api('/sessions/' + session.id + '/research/jobs/' + job.id + '/' + (kind === 'raw' ? 'raw' : 'evidence') + '?pointer=' + encodeURIComponent(pointer) + '&offset=' + offset + '&limit=4000'); if (mounted.current && currentScope === scope.current && current === viewRequest.current) setPreview({ job, kind, page }); } catch (error) { if (mounted.current && currentScope === scope.current && current === viewRequest.current) onError(error); } finally { if (mounted.current && currentScope === scope.current && current === viewRequest.current) setViewBusy(false); } } async function download(job: ResearchJob, kind: Preview['kind']) { const current = scope.current; try { if (kind === 'trace' && job.traceArtifactId) await downloadArtifact(session.id, job.traceArtifactId, 'agent-workbench-rea-transport-' + job.id + '.json'); else await downloadResearchDocument(session.id, job.id, kind === 'raw' ? 'raw' : 'bundle'); } catch (error) { if (mounted.current && current === scope.current) onError(error); } } return
{project.name} · 当前任务

从工件取得原证据

登记一个本地工件,分析其代码、导入或资源,再把原依据交给同一编程任务。

{checking ? '正在检测' : availability?.status === 'available' ? '已找到 REA ' + availability.version : 'REA 尚未就绪'}
{availability && availability.status !== 'available' &&
{availability.status === 'unsupported' ? '当前分析契约使用 REA 6.8.0,请使用匹配的发行包。' : '请使用包含 REA 的工作台发行包,再重新检测本地安装。'}
}
{ event.preventDefault(); void register(); }}>

登记研究工件