Files
Agent-Workbench/docs/reading-s-evidence/source-bytes/src/client/SkillCenter.tsx
T

64 lines
9.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 { useEffect, useRef, useState } from 'react';
import type { FormEvent } from 'react';
import { BookOpenText, Check, CircleAlert, FileText, LoaderCircle, Plus, Power, RefreshCw, ShieldCheck } from 'lucide-react';
import { api } from './api';
import './SkillCenter.css';
type SkillVersion = { revision: number; version: string; source: { path: string; sha256: string; bytes: number }; snapshotSha256: string; createdAt: string };
type Skill = SkillVersion & { id: string; projectId: string; name: string; description: string; enabled: boolean; configurationRevision: number; updatedAt: string; status: string; reason: string; revisions: SkillVersion[] };
type SkillView = { skills: Skill[]; projectId: string; revision: number; modelRequests: number };
type Snapshot = SkillVersion & { id: string; content: string };
const statusNames: Record<string, string> = { current: '来源一致', changed: '来源已变化', unavailable: '来源不可用' };
export function SkillCenter({ projectId, busy }: { projectId: string; busy: boolean }) {
const [view, setView] = useState<SkillView | null>(null), [working, setWorking] = useState(false);
const [error, setError] = useState(''), [notice, setNotice] = useState('');
const [snapshot, setSnapshot] = useState<Snapshot | null>(null), [versions, setVersions] = useState<Record<string, string>>({});
const [draft, setDraft] = useState({ name: '', description: '', path: '', version: '1.0.0' });
const generation = useRef(0);
const url = '/projects/' + projectId + '/skills';
const load = async (current = generation.current) => { const result = await api<SkillView>(url); if (current === generation.current) setView(result); };
useEffect(() => {
const current = ++generation.current; setView(null); setSnapshot(null); setError('');
void load(current).catch(cause => { if (current === generation.current) setError(String(cause.message ?? cause)); });
return () => { generation.current++; };
}, [projectId]);
const perform = async (action: () => Promise<unknown>, success: string) => {
const current = generation.current; setWorking(true); setError(''); setNotice('');
try { await action(); if (current === generation.current) { setNotice(success); await load(current); } }
catch (cause) { if (current === generation.current) setError(cause instanceof Error ? cause.message : String(cause)); }
finally { if (current === generation.current) setWorking(false); }
};
const register = (event: FormEvent) => {
event.preventDefault();
void perform(async () => { await api(url, draft); }, '技能已登记。审查原文后,可以为当前项目启用。');
};
const read = (skill: Skill, revision: number) => {
const current = generation.current;
void perform(async () => {
const value = await api<Snapshot>(url + '/content?id=' + skill.id + '&revision=' + revision);
if (current === generation.current) setSnapshot(value);
}, '已读取保存的原修订。');
};
const skills = view?.skills ?? [], active = skills.filter(value => value.enabled && value.status === 'current').length;
const locked = busy || working;
return <section className="skill-center" aria-label="项目技能管理" aria-busy={working}>
<header className="skill-heading"><div><span className="skill-kicker"><BookOpenText size={15} />项目能力 / SKILLS</span><h3>让工作流程有据可查</h3><p>登记本地技能,在本项目任务中按需读取。启用前先审查内容,来源改变后需要重新确认。</p></div><button type="button" className="button secondary" disabled={working} onClick={() => void perform(() => load(), '来源状态已重新核对。')}><RefreshCw size={14} className={working ? 'spin' : ''} />核对来源</button></header>
<div className="skill-status-bar"><span><i className="skill-light" />{active} 项可用</span><span>{skills.length} 项已登记</span><span><ShieldCheck size={14} />沿用项目权限与审查</span></div>
{error && <p className="skill-alert" role="alert"><CircleAlert size={16} />{error}</p>}
{notice && <p className="skill-notice" role="status"><Check size={16} />{notice}</p>}
{busy && <p className="skill-warning">项目正在执行,停止任务后可以修改技能配置。</p>}
<div className="skill-list">{skills.map(skill => <article className={'skill-row skill-' + skill.status} key={skill.id}>
<div className="skill-row-title"><BookOpenText size={19} /><div><h4>{skill.name}<span>v{skill.version}</span></h4><p>{skill.description}</p></div><span className={'skill-switch-state ' + (skill.enabled ? 'on' : '')}>{skill.enabled ? '已启用' : '已停用'}</span></div>
<div className="skill-source"><span className={'skill-source-state ' + skill.status}>{statusNames[skill.status]}</span><span>修订 {skill.revision}</span><code title={skill.source.sha256}>SHA256 {skill.source.sha256.slice(0, 16)}</code><span>{skill.source.bytes.toLocaleString()} B</span><code className="skill-path">{skill.source.path}</code></div>
{skill.reason && <p className="skill-warning">{skill.reason}</p>}
<div className="skill-controls"><button type="button" className="button secondary" disabled={working} onClick={() => read(skill, skill.revision)}><FileText size={14} />审查原文</button><button type="button" className={'button ' + (skill.enabled ? 'secondary' : 'primary')} disabled={locked || !skill.enabled && skill.status !== 'current'} onClick={() => void perform(() => api(url + '/toggle', { id: skill.id, revision: skill.revision, configurationRevision: skill.configurationRevision, enabled: !skill.enabled }), skill.enabled ? '技能已停用。' : '技能已启用,后续任务可以按需读取。')}><Power size={14} />{skill.enabled ? '停用技能' : '启用技能'}</button></div>
<details className="skill-revisions"><summary>版本与来源记录 · {skill.revisions.length} 个修订</summary><div className="skill-revision-list">{skill.revisions.map(version => <div key={version.revision}><span>r{version.revision} · {version.version}</span><code>{version.source.sha256.slice(0, 16)}</code><button type="button" className="text-button" disabled={working} onClick={() => read(skill, version.revision)}>查看该修订</button></div>)}</div><form onSubmit={event => { event.preventDefault(); void perform(() => api(url + '/refresh', { id: skill.id, revision: skill.revision, configurationRevision: skill.configurationRevision, version: versions[skill.id] || skill.version }), '新修订已登记,并已停用。请审查后重新启用。'); }} className="skill-refresh"><label>新修订版本<input aria-label={'新修订版本 ' + skill.name} value={versions[skill.id] ?? skill.version} onChange={event => setVersions({ ...versions, [skill.id]: event.target.value })} maxLength={80} /></label><button type="submit" className="button secondary" disabled={locked}><RefreshCw size={14} />登记新修订</button></form></details>
</article>)}</div>
{!view && !error && <p className="skill-empty"><LoaderCircle size={20} className="spin" />正在核对已登记技能</p>}
{view && !skills.length && <div className="skill-empty"><BookOpenText size={27} /><strong>为项目接入第一项技能</strong><p>选择你已审查的 SKILL.md,填写名称与用途。登记后默认停用。</p></div>}
{snapshot && <section className="skill-snapshot" aria-label="技能原文"><header><strong>原文审查 · {snapshot.version} / r{snapshot.revision}</strong><button type="button" className="text-button" onClick={() => setSnapshot(null)}>关闭原文</button></header><p>这是保存的技能参考流程,执行仍需遵循项目权限。附带脚本和文件不会自动运行。</p><code>快照 SHA256 {snapshot.snapshotSha256}</code><pre tabIndex={0}>{snapshot.content}</pre></section>}
<form className="skill-register" onSubmit={register}><div className="skill-register-heading"><h4><Plus size={17} />登记本地技能</h4><span>UTF-8 · SKILL.md · 最大 64KB</span></div><div className="skill-form-grid"><label>技能名称<input required value={draft.name} onChange={event => setDraft({ ...draft, name: event.target.value })} maxLength={100} placeholder="例如:工程交付检查" /></label><label>版本<input required value={draft.version} onChange={event => setDraft({ ...draft, version: event.target.value })} maxLength={80} /></label><label className="skill-full">技能文件路径<input required value={draft.path} onChange={event => setDraft({ ...draft, path: event.target.value })} placeholder="D:\skills\project-delivery\SKILL.md" /></label><label className="skill-full">用途说明<textarea required rows={2} maxLength={500} value={draft.description} onChange={event => setDraft({ ...draft, description: event.target.value })} placeholder="说明何时使用这项技能,便于任务选择。" /></label></div><footer><span>登记与核对来源不会请求模型。</span><button type="submit" className="button primary" disabled={locked || !draft.name.trim() || !draft.path.trim() || !draft.description.trim()}>{working ? <LoaderCircle size={14} className="spin" /> : <Plus size={14} />}登记技能</button></footer></form>
</section>;
}