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

92 lines
9.8 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 { ArrowDown, ArrowLeft, Check, FolderGit2, GitBranch, LoaderCircle, RefreshCw, ShieldQuestion } from 'lucide-react';
import { api } from './api';
import { Modal } from './components';
import type { Project, Session, WorkspaceRecord } from './types';
import './WorkspacePanel.css';
const active = ['creating', 'verifying', 'registering'];
const labels: Record<string, string> = { prepared: '等待确认创建', creating: '正在创建独立分支', verifying: '正在核对文件与分支', registering: '正在登记项目与会话', ready: '可以开始开发', unknown: '创建结果需要核对', resolved_external: '已保留外部状态', discarded: '计划已撤销' };
export function WorkspacePanel({ project, session, busy, onRefresh, onNavigate, onError }: {
project: Project; session: Session; busy: boolean;
onRefresh: (projectId: string, sessionId: string, isCurrent?: () => boolean) => Promise<void>;
onNavigate: (projectId: string, sessionId: string, isCurrent?: () => boolean) => Promise<void>;
onError: (error: unknown) => void;
}) {
const records = session.execution?.workspaces ?? [];
const [label, setLabel] = useState('');
const [working, setWorking] = useState('');
const [confirm, setConfirm] = useState<WorkspaceRecord | null>(null);
const [external, setExternal] = useState<WorkspaceRecord | null>(null);
const identity = useRef(0), inFlight = useRef(false);
const unresolved = records.some(value => value.requiresVerification || value.status === 'prepared' || active.includes(value.status));
useEffect(() => {
identity.current++; inFlight.current = false; setWorking(''); setLabel(''); setConfirm(null); setExternal(null);
return () => { identity.current++; };
}, [project.id, session.id]);
async function perform(action: string, record?: WorkspaceRecord) {
if (inFlight.current) return;
inFlight.current = true; const request = ++identity.current; setWorking(record?.id ?? 'prepare');
try {
const value = await api<WorkspaceRecord>(`/sessions/${session.id}/workspaces${record ? '/' + record.id + '/' + action : ''}`,
record ? { fingerprint: record.fingerprint, ...(action === 'acknowledge' ? { observationFingerprint: record.observation?.fingerprint } : {}) } : { label: label.trim() });
if (request !== identity.current) return;
await onRefresh(project.id, session.id, () => request === identity.current);
if (request !== identity.current) return;
if (action === 'prepare') { setConfirm(value); setLabel(''); }
else { setConfirm(null); setExternal(null); }
} catch (error) { if (request === identity.current) onError(error); }
finally { if (request === identity.current) { inFlight.current = false; setWorking(''); } }
}
async function navigate(projectId: string, sessionId: string, record?: WorkspaceRecord) {
if (inFlight.current) return;
inFlight.current = true; const request = ++identity.current; setWorking(record?.id ?? 'origin');
try {
if (record) await api(`/sessions/${record.sessionId}/workspaces/${record.id}/open`, {});
if (request !== identity.current) return;
await onNavigate(projectId, sessionId, () => request === identity.current);
} catch (error) { if (request === identity.current) onError(error); }
finally { if (request === identity.current) { inFlight.current = false; setWorking(''); } }
}
return <section className="task-record-section workspace-panel" aria-label="独立开发工作区">
<div className="record-heading"><FolderGit2 size={15} /><h3>独立开发工作区</h3></div>
{project.workspace && <div className="workspace-current"><span>当前独立分支</span><code>{project.workspace.branch}</code><p>修改、检查和提交都在此目录执行。交付后可以审查,再合并回来源项目。</p><button type="button" className="text-button" disabled={!!working} onClick={() => navigate(project.workspace!.sourceProjectId, project.workspace!.sourceSessionId)}><ArrowLeft size={13} />返回来源会话</button></div>}
<p className="record-footnote">从当前已提交版本创建独立目录和分支,让新任务有明确的开发范围。</p>
<form className="workspace-prepare" onSubmit={event => { event.preventDefault(); perform('prepare'); }}>
<label htmlFor={'workspace-label-' + session.id}>工作区名称</label>
<input id={'workspace-label-' + session.id} value={label} onChange={event => setLabel(event.target.value)} maxLength={64} placeholder="例如:订单导出功能" disabled={busy || !!working || unresolved} />
<button type="submit" className="button secondary" disabled={!label.trim() || busy || !!working || unresolved}>{working === 'prepare' ? <LoaderCircle size={14} className="spin" /> : <GitBranch size={14} />}准备独立工作区</button>
</form>
{records.slice().reverse().map(record => <article className={'workspace-record workspace-' + record.status} key={record.id}>
<div className="workspace-record-heading"><strong>{record.label}</strong><span>{labels[record.status] || record.status}</span></div>
<code className="workspace-branch">{record.branch}</code><p>{record.summary}</p>
{record.observation?.problems.length ? <div className="workspace-problems" role="status"><ShieldQuestion size={14} /><ul>{record.observation.problems.map((value, index) => <li key={index}>{value}</li>)}</ul></div> : null}
<details><summary>目录与来源</summary><dl className="workspace-fields"><dt>来源目录</dt><dd><code>{record.source.root}</code></dd><dt>起始提交</dt><dd><code>{record.source.head}</code></dd><dt>工作区目录</dt><dd><code>{record.root}</code></dd></dl></details>
<div className="workspace-actions">
{record.sessionId !== session.id ? <button type="button" className="text-button" disabled={!!working} onClick={() => navigate(record.projectId, record.sessionId)}>转到所属会话</button> : <>
{record.status === 'prepared' && <><button type="button" className="text-button" disabled={busy || !!working} onClick={() => setConfirm(record)}>查看创建预览</button><button type="button" className="text-button" disabled={busy || !!working} onClick={() => perform('discard', record)}>撤销计划</button></>}
{active.includes(record.status) && <button type="button" className="text-button" disabled={!!working} onClick={() => perform('stop', record)}>停止创建</button>}
{record.status === 'unknown' && <><button type="button" className="text-button" disabled={busy || !!working} onClick={() => perform('reconcile', record)}>{working === record.id ? <LoaderCircle size={13} className="spin" /> : <RefreshCw size={13} />}核对创建结果</button>{record.observation && <button type="button" className="text-button" disabled={busy || !!working} onClick={() => setExternal(record)}>保留当前外部状态</button>}</>}
{record.status === 'ready' && <button type="button" className="button secondary" disabled={!!working} onClick={() => navigate(record.childProjectId, record.childSessionId, record)}><FolderGit2 size={14} />打开此工作区</button>}
</>}
</div>
</article>)}
{confirm && <Modal title="创建独立工作区" description="核对来源版本和新目录后,再开始这个工作区的开发。" onClose={() => { if (!working) setConfirm(null); }}>
<div className="workspace-review">
<div className="workspace-track"><span>来源已提交版本</span><strong>{project.name}</strong><code>{confirm.source.branch} · {confirm.source.head.slice(0, 12)}</code><p>{confirm.source.treeFiles.length} 份文件 · {(confirm.source.committedBytes / 1024).toFixed(1)} KB</p></div>
<ArrowDown size={18} className="workspace-track-arrow" aria-hidden="true" />
<div className="workspace-track workspace-track-child"><span>新独立工作区</span><strong>{confirm.label}</strong><code>{confirm.branch}</code><code>{confirm.root}</code></div>
<p className="workspace-source-note">{confirm.source.dirty ? `来源有 ${confirm.source.changedPathCount} 项未提交改动,将保留在来源目录。新工作区使用上方已提交版本。` : '来源目录当前没有未提交改动。'}</p>
{!!confirm.source.changedPaths.length && <details><summary>查看来源已有改动</summary><ul className="workspace-source-paths">{confirm.source.changedPaths.map(value => <li key={value.path}><code>{value.status}</code><span>{value.path}</span></li>)}</ul></details>}
<p className="record-footnote">创建后打开为新的项目和会话。需要的依赖可在工作区中审查安装。</p>
</div>
<div className="modal-actions"><button type="button" className="button secondary" disabled={!!working} onClick={() => setConfirm(null)}>稍后创建</button><button type="button" className="button primary" disabled={busy || !!working} onClick={() => perform('create', confirm)}>{working ? <LoaderCircle size={14} className="spin" /> : <Check size={14} />}确认创建工作区</button></div>
</Modal>}
{external && <Modal title="保留当前外部状态" description="结束本次创建结果核对,目录与分支继续保留。" onClose={() => { if (!working) setExternal(null); }}><div className="workspace-review"><p>这次创建将记为“已保留外部状态”。需要继续开发时,请核对目录后从项目栏打开;这项操作不会把创建记为成功。</p><code className="workspace-branch">{external.root}</code>{external.observation?.problems.map((value, i) => <p key={i}>{value}</p>)}</div><div className="modal-actions"><button type="button" className="button secondary" disabled={!!working} onClick={() => setExternal(null)}>返回核对</button><button type="button" className="button primary" disabled={!!working} onClick={() => perform('acknowledge', external)}>确认保留当前状态</button></div></Modal>}
</section>;
}