Files
Agent-Workbench/src/client/ExperienceDraftControls.tsx
T

72 lines
8.1 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 { useRef, useEffect, useState } from 'react';
import { Archive, Check, Download, FilePenLine, Undo2 } from 'lucide-react';
import type { ExperienceSourceView, ProjectMemory, Session } from './types';
import { api, downloadArtifact } from './api';
const stages: Record<string, string> = { observed: '已观察', model_suggested: '模型建议', source_checked: '来源已核对',
engineering_verified: '原检查已核对', user_confirmed: '用户已采用', rejected: '已拒绝', expired: '已失效' };
const actionLabel: Record<string, string> = { created: '生成草稿', edit: '修订', adopt: '采用', withdraw: '撤销',
reject: '拒绝', counterexample: '记录反例', expired: '失效', archive: '归档' };
export function ExperienceDraftControls({ memory, session, sourceView, refresh, onSelectSession }: {
memory: ProjectMemory; session: Session | null; sourceView: ExperienceSourceView | null; refresh: () => Promise<void>;
onSelectSession?: (id: string) => void;
}) {
const experience = memory.experience!;
const [editing, setEditing] = useState(false), [counterexample, setCounterexample] = useState(false);
const [form, setForm] = useState({ title: memory.title, content: memory.content, applicability: experience.applicability, invalidWhen: experience.invalidWhen });
const [note, setNote] = useState(''), [busy, setBusy] = useState(false), [problem, setProblem] = useState('');
const alive = useRef(true);
useEffect(() => { alive.current = true; return () => { alive.current = false; }; }, []);
const owner = memory.sessionId === session?.id;
const usable = memory.status === 'current' && !experience.stages.includes('rejected') && !experience.unavailableReason;
const change = async (action: string, fields = {}) => {
if (!session || !sourceView || busy) return;
setBusy(true); setProblem('');
try {
await api('/sessions/' + session.id + '/memories/experiences/' + memory.id + '/' + action,
{ cursor: sourceView.cursor, revision: experience.revision, ...fields });
if (alive.current) { setEditing(false); setCounterexample(false); setNote(''); await refresh(); }
} catch (cause) { if (alive.current) setProblem(cause instanceof Error ? cause.message : '经验操作未保存,请重新核对。'); }
finally { if (alive.current) setBusy(false); }
};
const download = async (id: string) => {
setProblem('');
try { await downloadArtifact(memory.sessionId, id, 'orven-experience-' + id); }
catch (cause) { if (alive.current) setProblem(cause instanceof Error ? cause.message : '来源原文读取失败。'); }
};
return <div className="experience-detail">
<div className="experience-stages">{experience.stages.map((stage, index) => <span key={stage + index}>{stages[stage] || stage}</span>)}</div>
<dl className="experience-scope"><div><dt>适用范围</dt><dd>{experience.applicability}</dd></div><div><dt>失效条件</dt><dd>{experience.invalidWhen}</dd></div></dl>
<p className="experience-boundary">{experience.engineering.verified ? '所选原工程检查通过核对;经验结论仍未验证。' : '记录了原始事实;尚无工程验证证明这条经验结论。'}失败原因与复用收益尚未证明。</p>
<details className="experience-provenance"><summary>来源与修订({experience.revision})</summary>
<dl><div><dt>任务 / 运行</dt><dd><code>{memory.taskId}<br />{memory.runId}</code></dd></div>
<div><dt>来源模型 / 协议</dt><dd>{experience.provenance.model.model || '未记录'} · {experience.provenance.model.protocol} · 配置修订 {experience.provenance.model.configRevision ?? '未记录'}</dd></div>
<div><dt>来源源码</dt><dd><code>{experience.provenance.sourceFingerprint || '原来源未记录;仅作历史草稿'}</code></dd></div></dl>
<details><summary>所选原始事实</summary><pre>{JSON.stringify(experience.provenance.facts, null, 2)}</pre></details>
<ul>{experience.provenance.refs.map(ref => <li key={ref.id}><span>{ref.kind} · <code>{ref.sha256.slice(0, 16)}</code></span><button type="button" className="text-button" onClick={() => void download(ref.id)}><Download size={13} />来源原文</button></li>)}</ul>
<ol>{experience.revisions.map(ref => <li key={ref.artifactId}><span>修订 {ref.revision} · {actionLabel[ref.action] || ref.action} · <code>{ref.sha256.slice(0, 12)}</code></span><button type="button" className="text-button" onClick={() => void download(ref.artifactId)}>查看原修订</button></li>)}</ol>
</details>
{experience.counterexamples.length > 0 && <div className="experience-counterexamples"><strong>反例记录</strong>{experience.counterexamples.map((row, index) => <p key={index}>{row.note}<small>用户记录 · 工程验证未确认</small></p>)}</div>}
{problem && <p className="memory-error" role="alert">{problem}</p>}
{memory.status !== 'archived' && <div className="experience-actions">
{!owner ? <button type="button" className="text-button" onClick={() => onSelectSession?.(memory.sessionId)} disabled={!onSelectSession}>前往来源会话管理</button> : <>
{experience.adoption === 'active' ? <button type="button" className="button secondary" disabled={busy || !sourceView} onClick={() => void change('withdraw')}><Undo2 size={14} />撤销采用</button>
: usable && <button type="button" className="button secondary" disabled={busy || !sourceView} onClick={() => void change('adopt')}><Check size={14} />采用到本项目</button>}
{usable && <button type="button" className="text-button" disabled={busy || !sourceView} onClick={() => { setForm({ title: memory.title, content: memory.content, applicability: experience.applicability, invalidWhen: experience.invalidWhen }); setEditing(!editing); }}><FilePenLine size={14} />修订草稿</button>}
<button type="button" className="text-button" disabled={busy || !sourceView} onClick={() => setCounterexample(!counterexample)}>记录反例</button>
{!experience.stages.includes('rejected') && <button type="button" className="text-button" disabled={busy || !sourceView} onClick={() => void change('reject')}>拒绝这条经验</button>}
<button type="button" className="text-button" disabled={busy || !sourceView} onClick={() => void change('archive')}><Archive size={13} />归档</button>
</>}
</div>}
{editing && <form className="experience-edit memory-form-grid" onSubmit={event => { event.preventDefault(); void change('edit', form); }}>
<label className="memory-full">经验标题<input aria-label="经验标题" required value={form.title} onChange={event => setForm({ ...form, title: event.target.value })} /></label>
<label className="memory-full">经验内容<textarea aria-label="经验内容" required rows={5} value={form.content} onChange={event => setForm({ ...form, content: event.target.value })} /></label>
<label>适用范围<textarea aria-label="适用范围" required rows={3} value={form.applicability} onChange={event => setForm({ ...form, applicability: event.target.value })} /></label>
<label>失效条件<textarea aria-label="失效条件" required rows={3} value={form.invalidWhen} onChange={event => setForm({ ...form, invalidWhen: event.target.value })} /></label>
<div className="memory-full experience-actions"><button type="submit" className="button secondary" disabled={busy}>保存草稿修订</button><button type="button" className="text-button" onClick={() => setEditing(false)}>取消修订</button><span>修订后停止采用,旧文本与失败证据保留。</span></div>
</form>}
{counterexample && <form className="experience-edit" onSubmit={event => { event.preventDefault(); void change('counterexample', { note }); }}><label>新反例<textarea aria-label="新反例" required rows={3} value={note} onChange={event => setNote(event.target.value)} placeholder="记录出现的反例和依据;不会把文字当作已通过的工程检查。" /></label><div className="experience-actions"><button type="submit" className="button secondary" disabled={busy || !note.trim()}>记录反例并停用</button><button type="button" className="text-button" onClick={() => setCounterexample(false)}>取消</button></div></form>}
</div>;
}