Files
Agent-Workbench/src/client/RequirementDraftEditor.tsx
T
2026-11-04 13:38:08 +08:00

70 lines
8.3 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 { useState } from 'react';
import { Modal } from './components';
import type { RequirementDraft, RequirementItem, RequirementView, TaskPlanStep } from './types';
export const requirementRoles = { requirement: '业务要求', reference: '参考资料', prohibition: '禁止事项', unknown: '待澄清' };
const kinds: Record<TaskPlanStep['kind'], string> = { analysis: '研究与读取', changes: '已应用改动', results: '当前文件', checks: '工程检查', browser: '页面断言' };
export const requirementInput = (row: RequirementItem) => ({ key: row.key, title: row.title, criterion: row.criterion,
...(row.role ? { role: row.role } : {}), kind: row.kind, method: row.method,
source: { kind: row.source.kind, id: row.source.id, start: row.source.start, end: row.source.end },
...(row.method === 'scope' ? {} : row.kind === 'analysis' ? { tools: row.tools } : ['changes', 'results'].includes(row.kind) ? { paths: row.paths }
: row.kind === 'checks' ? { checks: row.checks } : { viewport_width: row.viewport_width, actions: row.actions }) });
const evidenceScope = (kind: TaskPlanStep['kind']) => kind === 'analysis' ? { tools: ['read_file'] } : ['changes', 'results'].includes(kind)
? { paths: [] } : kind === 'checks' ? { checks: ['test', 'build'] } : { viewport_width: 1280, actions: ['assert_visible'] };
export function changeRequirementRole(row: RequirementItem, role: NonNullable<RequirementItem['role']>): RequirementItem {
const { tools, paths, checks, actions, viewport_width, ...base } = row;
return role === 'requirement' ? { ...base, role, method: 'evidence', kind: 'analysis', tools: ['read_file'] }
: { ...base, role, kind: 'analysis', method: 'scope' };
}
export function RequirementScopeFields({ row, change, prefix = '' }: { row: RequirementItem; change: (row: RequirementItem) => void; prefix?: string }) {
const updateKind = (kind: TaskPlanStep['kind']) => {
const { tools, paths, checks, actions, viewport_width, ...base } = row;
change({ ...base, kind, ...evidenceScope(kind) });
};
return <>
<label>内容用途<select aria-label={prefix + '内容用途'} value={row.role ?? 'requirement'} onChange={event => change(changeRequirementRole(row, event.target.value as NonNullable<RequirementItem['role']>))}>
{Object.entries(requirementRoles).map(([key, value]) => <option key={key} value={key}>{value}</option>)}</select></label>
{row.method === 'scope' ? <p className="record-footnote">{row.role === 'unknown' ? '尚未确定用途,保存后仍阻止要求复核完成。' : row.role === 'reference' ? '只记录参考用途,不要求修改此文件,也不计为交付成果。' : '保留用户边界供执行和复核使用;记录边界不等于已证明遵守。既有操作审批仍保留。'}</p> : <>
<label>复核方式<select aria-label={prefix + '要求复核方式'} value={row.method} onChange={event => {
const method = event.target.value as RequirementItem['method'];
const { tools, paths, checks, actions, viewport_width, ...base } = row;
change(method === 'not_requested' ? { ...base, method, kind: 'checks', checks: ['test', 'build'] }
: method === 'user' ? { ...base, method, kind: 'analysis', tools: ['read_file'] } : { ...row, method });
}}><option value="evidence">核对实际证据</option><option value="user">用户体验确认</option><option value="not_requested">按用户约定不执行检查</option></select></label>
{row.method === 'evidence' && <><label>证据类别<select aria-label={prefix + '要求证据类别'} value={row.kind} onChange={event => updateKind(event.target.value as TaskPlanStep['kind'])}>{Object.entries(kinds).map(([key, value]) => <option key={key} value={key}>{value}</option>)}</select></label>
{['results', 'changes'].includes(row.kind) && <label>文件范围(每行一项)<textarea aria-label={prefix + '要求文件范围'} required value={row.paths?.join('\n') ?? ''} onChange={event => change({ ...row, paths: event.target.value.split('\n').filter(Boolean) })} /></label>}
{row.kind === 'analysis' && <label>读取工具(逗号分隔)<input aria-label={prefix + '要求读取工具'} required value={row.tools?.join(',') ?? ''} onChange={event => change({ ...row, tools: event.target.value.split(',').map(value => value.trim()) })} /></label>}
{row.kind === 'checks' && <label>检查名称(逗号分隔)<input aria-label={prefix + '要求检查名称'} required value={row.checks?.join(',') ?? ''} onChange={event => change({ ...row, checks: event.target.value.split(',').map(value => value.trim()) })} /></label>}
{row.kind === 'browser' && <label>验收视口<select aria-label={prefix + '要求验收视口'} value={row.viewport_width} onChange={event => change({ ...row, viewport_width: Number(event.target.value) })}><option value={1280}>桌面 · 1280px</option><option value={390}>手机 · 390px</option></select></label>}</>}
</>}
</>;
}
export function RequirementDraftEditor({ value, view, saving, submit, close }: { value: RequirementDraft; view: RequirementView; saving: boolean;
submit: (input: object) => void; close: () => void }) {
const [rows, setRows] = useState<RequirementItem[]>(value.proposal.items.map(row => ({ ...row, role: 'unknown', kind: 'analysis', method: 'scope', depends_on: [] })));
const [reviewed, setReviewed] = useState(false);
const count = view.items.length + rows.filter(row => !view.items.some(old => old.key === row.key)).length;
return <Modal title="审阅原文草案" description="草案只整理原文。逐项确定用途、验收方式和范围后保存;待澄清内容继续保留。" wide onClose={() => { if (!saving) close(); }}>
<form className="plan-editor requirement-editor requirement-draft-editor" onSubmit={event => {
event.preventDefault(); const keys = new Set(rows.map(row => row.key));
submit({ draft_id: value.id, source_set_sha256: value.sourceSetSha256,
reviewed_source_ids: [...new Set([...view.sources.filter(row => !view.unreviewed_sources?.includes(row.id)).map(row => row.id), ...value.proposal.covered_source_ids])],
requirements: [...view.items.filter(row => !keys.has(row.key)).map(requirementInput), ...rows.map(requirementInput)] });
}}>
<p className="record-footnote">{value.proposal.note}</p>
{value.proposal.truncated && <p role="alert" className="plan-problem">草案达到保留上限,部分原文未收录。未完整收录的来源仍保持未审阅,请分小任务或手动核对。</p>}
{count > 24 && <p role="alert" className="plan-problem">合计 {count} 项,超过24项上限。请取消并逐项手动审阅,原草案与旧要求均保留。</p>}
<fieldset disabled={saving} className="requirement-draft-items">{rows.map((row, i) => <section className="plan-editor-step" key={row.key} aria-label={`草案条目 ${i + 1}`}>
<p className="requirement-draft-origin">{row.source.kind === 'goal' ? '原目标' : '追加要求'} · [{row.source.start}, {row.source.end})</p>
<blockquote className="requirement-quote">{row.source.quote}</blockquote>
<label>名称<input aria-label={`草案名称 ${i + 1}`} required maxLength={100} value={row.title} onChange={event => setRows(values => values.map((item, index) => index === i ? { ...item, title: event.target.value } : item))} /></label>
<label>验收条件或范围说明<textarea aria-label={`草案条件 ${i + 1}`} required maxLength={300} rows={2} value={row.criterion} onChange={event => setRows(values => values.map((item, index) => index === i ? { ...item, criterion: event.target.value } : item))} /></label>
<RequirementScopeFields row={row} prefix={`草案${i + 1} `} change={next => setRows(values => values.map((item, index) => index === i ? next : item))} />
</section>)}</fieldset>
<label className="requirement-review-check"><input type="checkbox" required checked={reviewed} disabled={saving} onChange={event => setReviewed(event.target.checked)} />已核对所收录的原文和分类;未确定内容保留为待澄清。</label>
<div className="modal-actions"><button type="button" className="button secondary" disabled={saving} onClick={close}>取消</button><button className="button primary" disabled={saving || !reviewed || count > 24}>审阅并保存</button></div>
</form>
</Modal>;
}