@@ -0,0 +1,102 @@
import { useEffect , useRef , useState } from 'react' ;
import { Check , FileText , Plus , RefreshCw } from 'lucide-react' ;
import { api } from './api' ;
import { Modal } from './components' ;
import type { RequirementItem , RequirementView , Session , TaskPlanStep } from './types' ;
import './RequirementPanel.css' ;
const states : Record < string , string > = { pending : '待复核' , failed : '复核未通过' , stale : '依据已失效' , unverified : '原记录待核对' ,
evidence_verified : '所选证据已核对' , user_confirmed : '用户已确认' , not_requested : '按约定未执行' } ;
const kinds : Record < TaskPlanStep [ 'kind' ] , string > = { analysis : '研究与读取' , changes : '已应用改动' , results : '当前文件' , checks : '工程检查' , browser : '页面断言' } ;
const clean = ( row : RequirementItem ) = > ( { key : row.key , title : row.title , criterion : row.criterion , kind : row.kind , method : row.method ,
source : { kind : row.source.kind , id : row.source.id , start : row.source.start , end : row.source.end } ,
. . . ( 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 scope = ( 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 RequirementPanel ( { 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 selected = useRef ( identity ) ; selected . current = identity ;
const generation = useRef ( 0 ) , mutation = useRef ( false ) ;
const [ view , setView ] = useState < RequirementView | null > ( null ) , [ loading , setLoading ] = useState ( false ) , [ saving , setSaving ] = useState ( false ) ;
const [ draft , setDraft ] = useState < RequirementItem | null > ( null ) , [ picked , setPicked ] = useState < Record < string , string > > ( { } ) , [ notes , setNotes ] = useState < Record < string , string > > ( { } ) ;
const [ notice , setNotice ] = useState ( '' ) ;
const disabled = busy || saving || [ 'running' , 'awaiting_approval' , 'stopping' ] . includes ( session . status ) ;
useEffect ( ( ) = > { generation . current ++ ; setView ( null ) ; setDraft ( null ) ; setPicked ( { } ) ; setNotes ( { } ) ; setNotice ( '' ) ; setSaving ( false ) ; mutation . current = false ;
return ( ) = > { generation . current ++ ; } ; } , [ identity ] ) ;
useEffect ( ( ) = > { const timer = setTimeout ( ( ) = > { void load ( ) ; } , 120 ) ; return ( ) = > clearTimeout ( timer ) ; } , [ identity , session . revision , session . status ] ) ;
async function load() {
if ( ! task || mutation . current ) return ;
const scopeId = identity , request = ++ generation . current ; setLoading ( true ) ;
try { const result = await api < RequirementView > ( ` /sessions/ ${ session . id } /requirements?taskId= ${ task . id } ` ) ;
if ( selected . current === scopeId && generation . current === request ) setView ( result ) ;
} catch ( error ) { if ( selected . current === scopeId && generation . current === request ) onError ( error ) ; }
finally { if ( selected . current === scopeId && generation . current === request ) setLoading ( false ) ; }
}
async function mutate ( action : string , input : object ) {
if ( ! task || ! run || ! view || mutation . current ) return ;
mutation . current = true ; generation . current ++ ; setSaving ( true ) ; setNotice ( '' ) ; const scopeId = identity ;
try {
const reply = await api < { session : Session ; requirements : RequirementView ; verification ? : { status : string ; message? : string } } > ( ` /sessions/ ${ session . id } / ${ action } ` ,
{ taskId : task.id , runId : run.id , expected_revision : view.revision , . . . input } ) ;
if ( selected . current !== scopeId ) return ;
setView ( reply . requirements ) ; onSession ( reply . session ) ;
setNotice ( reply . verification ? . status === 'error' ? reply . verification . message ? ? '复核未通过,原失败已保留。' : action === 'requirements' ? '要求与原文来源已保存。' : '本次复核已记录。' ) ;
if ( action === 'requirements' ) setDraft ( null ) ;
} catch ( error ) { if ( selected . current === scopeId ) onError ( error ) ; }
finally { if ( selected . current === scopeId ) { mutation . current = false ; setSaving ( false ) ; setLoading ( false ) ; } }
}
function edit ( row? : RequirementItem ) {
const source = view ? . sources [ 0 ] ; if ( ! source ) return ;
setDraft ( row ? { . . . clean ( row ) , depends_on : [ ] } : { key : 'req_' + crypto . randomUUID ( ) . slice ( 0 , 8 ) , title : '' , criterion : '' , kind : 'analysis' , method : 'evidence' ,
source : { kind : source.kind , id : source.id , start : 0 , end : source.text.length } , depends_on : [ ] , . . . scope ( 'analysis' ) } ) ;
}
const source = view ? . sources . find ( row = > row . id === draft ? . source . id && row . kind === draft ? . source . kind ) ;
return < section className = "task-record-section requirement-panel" aria-label = "要求与证据" >
< div className = "record-heading" > < h3 > 要 求 与 证 据 < / h3 > < span > { view ? . items . length ? ? 0 } 项 < / span > < / div >
< p className = "record-footnote" > 逐 项 保 留 用 户 原 文 与 实 际 依 据 , 工 程 证 据 和 用 户 体 验 分 别 记 录 。 < / p >
< div className = "plan-toolbar" > < button className = "text-button" disabled = { loading || disabled } onClick = { ( ) = > void load ( ) } > < RefreshCw size = { 13 } / > 刷 新 复 核 < / button >
< button className = "button secondary" disabled = { disabled || ! view || view . status === 'unverified' || ( view . items . length >= 24 ) } onClick = { ( ) = > edit ( ) } > < Plus size = { 13 } / > 记 录 要 求 < / button > < / div >
{ notice && < p className = "plan-notice" role = "status" > { notice } < / p > }
{ view ? . error && < p className = "plan-problem" role = "alert" > { view . error } < / p > }
{ ! ! view ? . unreviewed_sources ? . length && < p className = "plan-problem" > 有 { view . unreviewed_sources . length } 份 用 户 来 源 尚 未 审 阅 。 核 对 原 目 标 和 追 加 要 求 后 保 存 要 求 ; 旧 复 核 记 录 保 留 。 < / p > }
{ view && ! view . items . length && < p className = "record-empty" > 尚 未 逐 项 记 录 要 求 。 模 型 的 完 成 答 复 保 留 在 原 会 话 中 。 < / p > }
< ol className = "requirement-list" > { view ? . items . map ( row = > < li key = { row . key } >
< div className = "requirement-heading" > < strong > { row . title } < / strong > < span className = { 'requirement-state requirement-' + row . status } > { states [ row . status ? ? 'pending' ] ? ? row . status } < / span > < / div >
< p > { row . criterion } < / p >
< details > < summary > 用 户 原 文 · { row . source . kind === 'goal' ? '原目标' : '追加要求' } · 要 求 修 订 { row . revision } < / summary >
< blockquote > { row . source . quote } < / blockquote > < code > { row . source . id } [ { row . source . start } , { row . source . end } ) < / code > < / details >
{ row . error && < p className = "plan-problem" > { row . error } < / p > } { row . last_rejection && < p className = "plan-problem" > 原 复 核 失 败 : { row . last_rejection . message } < / p > }
{ row . user_note && < p > 用 户 确 认 : { row . user_note } < / p > }
< div className = "requirement-actions" > < button className = "text-button" disabled = { disabled } onClick = { ( ) = > edit ( row ) } > 修 订 此 要 求 < / button >
{ row . proof_artifact_id && < button className = "text-button" onClick = { ( ) = > openArtifact ( row . proof_artifact_id ! ) } > < FileText size = { 13 } / > 原 复 核 记 录 < / button > } < / div >
{ row . method === 'evidence' && < div className = "plan-evidence" > < label > 选 择 实 际 依 据 · { kinds [ row . kind ] } < select aria-label = { ` 要求依据 ${ row . title } ` } disabled = { disabled } value = { picked [ row . key ] ? ? '' } onChange = { event = > setPicked ( values = > ( { . . . values , [ row . key ] : event . target . value } ) ) } >
< option value = "" > 选 择 此 任 务 的 原 结 果 < / option > { row . candidates ? . map ( candidate = > < option key = { candidate . id } value = { candidate . id } > { candidate . label } · { candidate . status } · { candidate . id . slice ( 0 , 8 ) } < / option > ) } < / select > < / label >
< button className = "button secondary" disabled = { disabled || ! picked [ row . key ] } onClick = { ( ) = > void mutate ( 'requirement-proofs' , { key : row.key , evidence_id : picked [ row . key ] } ) } > < Check size = { 13 } / > 复 核 所 选 证 据 < / button > < / div > }
{ row . method === 'user' && < div className = "requirement-confirm" > < label > 实 际 体 验 与 结 论 < textarea aria-label = { ` 用户确认 ${ row . title } ` } rows = { 2 } maxLength = { 1000 } disabled = { disabled } value = { notes [ row . key ] ? ? '' } onChange = { event = > setNotes ( values = > ( { . . . values , [ row . key ] : event . target . value } ) ) } / > < / label >
< button className = "button secondary" disabled = { disabled || ! notes [ row . key ] ? . trim ( ) } onClick = { ( ) = > void mutate ( 'requirement-proofs' , { key : row.key , note : notes [ row . key ] } ) } > 记 录 用 户 确 认 < / button > < / div > }
{ row . method === 'not_requested' && < p className = "record-footnote" > 检 查 未 执 行 , 不 计 为 测 试 通 过 。 < / p > }
< / li > ) } < / ol >
{ ! ! view ? . history ? . length && < details className = "requirement-history" > < summary > 原 复 核 历 史 · { view . history . length } 条 < / summary > { view . history . map ( row = > < p key = { row . id } > < button className = "text-button" onClick = { ( ) = > openArtifact ( row . artifact_id ) } > { row . key } · 修 订 { row . revision } · { row . status === 'rejected' ? '未通过' : '已记录' } < / button > { row . error && < span > { row . error } < / span > } < / p > ) } < / details > }
{ draft && view && < Modal title = "记录用户要求" description = "选定用户原文、验收范围和复核方式;修订保留旧要求与原失败。" wide onClose = { ( ) = > { if ( ! saving ) setDraft ( null ) ; } } >
< form className = "plan-editor requirement-editor" onSubmit = { event = > { event . preventDefault ( ) ; const rows = view . items . some ( row = > row . key === draft . key ) ? view . items . map ( row = > row . key === draft . key ? clean ( draft ) : clean ( row ) ) : [ . . . view . items . map ( clean ) , clean ( draft ) ] ; void mutate ( 'requirements' , { requirements : rows } ) ; } } >
< fieldset disabled = { saving } className = "plan-editor-step" >
< label > 要 求 名 称 < input aria-label = "要求名称" maxLength = { 100 } required value = { draft . title } onChange = { event = > setDraft ( { . . . draft , title : event.target.value } ) } / > < / label >
< label > 验 收 条 件 < textarea aria-label = "要求验收条件" rows = { 2 } required maxLength = { 300 } value = { draft . criterion } onChange = { event = > setDraft ( { . . . draft , criterion : event.target.value } ) } / > < / label >
< label > 用 户 原 文 来 源 < select aria-label = "要求原文来源" value = { draft . source . id } onChange = { event = > { const row = view . sources . find ( value = > value . id === event . target . value ) ! ; setDraft ( { . . . draft , source : { kind : row.kind , id : row.id , start : 0 , end : row.text.length } } ) ; } } > { view . sources . map ( ( row , i ) = > < option key = { row . id } value = { row . id } > { row . kind === 'goal' ? '原目标' : ` 追加要求 ${ i } ` } · { row . text . slice ( 0 , 65 ) } < / option > ) } < / select > < / label >
< blockquote className = "requirement-quote" > { source ? . text . slice ( draft . source . start , draft . source . end ) } < / blockquote >
< div className = "requirement-offsets" > < label > 原 文 起 点 < input aria-label = "原文起点" type = "number" min = { 0 } max = { Math . max ( 0 , ( source ? . text . length ? ? 1 ) - 1 ) } value = { draft . source . start } onChange = { event = > setDraft ( { . . . draft , source : { . . . draft . source , start : Number ( event . target . value ) } } ) } / > < / label > < label > 原 文 终 点 < input aria-label = "原文终点" type = "number" min = { draft . source . start + 1 } max = { source ? . text . length } value = { draft . source . end } onChange = { event = > setDraft ( { . . . draft , source : { . . . draft . source , end : Number ( event . target . value ) } } ) } / > < / label > < / div >
< label > 复 核 方 式 < select aria-label = "要求复核方式" value = { draft . method } onChange = { event = > setDraft ( { . . . draft , method : event.target.value as RequirementItem [ 'method' ] , . . . ( event . target . value === 'not_requested' ? { kind : 'checks' , checks : [ 'test' , 'build' ] } : { } ) } ) } > < option value = "evidence" > 核 对 实 际 证 据 < / option > < option value = "user" > 用 户 体 验 确 认 < / option > < option value = "not_requested" > 按 用 户 约 定 不 执 行 检 查 < / option > < / select > < / label >
{ draft . method === 'evidence' && < > < label > 证 据 类 别 < select aria-label = "要求证据类别" value = { draft . kind } onChange = { event = > { const kind = event . target . value as TaskPlanStep [ 'kind' ] ; setDraft ( { . . . draft , kind , . . . scope ( kind ) } ) ; } } > { Object . entries ( kinds ) . map ( ( [ key , label ] ) = > < option key = { key } value = { key } > { label } < / option > ) } < / select > < / label >
{ [ 'results' , 'changes' ] . includes ( draft . kind ) && < label > 文 件 范 围 ( 每 行 一 项 ) < textarea aria-label = "要求文件范围" required value = { draft . paths ? . join ( '\n' ) ? ? '' } onChange = { event = > setDraft ( { . . . draft , paths : event.target.value.split ( '\n' ) . filter ( Boolean ) } ) } / > < / label > }
{ draft . kind === 'analysis' && < label > 读 取 工 具 ( 逗 号 分 隔 ) < input aria-label = "要求读取工具" required value = { draft . tools ? . join ( ',' ) ? ? '' } onChange = { event = > setDraft ( { . . . draft , tools : event.target.value.split ( ',' ) . map ( value = > value . trim ( ) ) } ) } / > < / label > }
{ draft . kind === 'checks' && < label > 检 查 名 称 ( 逗 号 分 隔 ) < input aria-label = "要求检查名称" required value = { draft . checks ? . join ( ',' ) ? ? '' } onChange = { event = > setDraft ( { . . . draft , checks : event.target.value.split ( ',' ) . map ( value = > value . trim ( ) ) } ) } / > < / label > }
{ draft . kind === 'browser' && < label > 验 收 视 口 < select aria-label = "要求验收视口" value = { draft . viewport_width } onChange = { event = > setDraft ( { . . . draft , viewport_width : Number ( event . target . value ) } ) } > < option value = { 1280 } > 桌 面 · 1280 px < / option > < option value = { 390 } > 手 机 · 390 px < / option > < / select > < / label > } < / > }
< / fieldset > < div className = "modal-actions" > < button type = "button" className = "button secondary" disabled = { saving } onClick = { ( ) = > setDraft ( null ) } > 取 消 < / button > < button className = "button primary" disabled = { saving } > 保 存 要 求 < / button > < / div >
< / form > < / Modal > }
< / section > ;
}