Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
291 lines
11 KiB
TypeScript
291 lines
11 KiB
TypeScript
import { useState } from "react";
|
|
import { del, get, post, put } from "../../api";
|
|
import { confirmDialog, promptDialog, toast } from "../../dialogs";
|
|
import { useFetch } from "../../hooks";
|
|
import { useT } from "../../i18n";
|
|
import { Icon } from "../Icon";
|
|
import { Spinner } from "../Activity";
|
|
|
|
interface SkillInfo {
|
|
name: string;
|
|
description: string;
|
|
files: string[];
|
|
}
|
|
interface SkillFile {
|
|
path: string;
|
|
content: string;
|
|
}
|
|
|
|
const TEMPLATE = `---
|
|
name: my-skill
|
|
description: When to use this skill, in one or two sentences
|
|
---
|
|
|
|
# Instructions
|
|
|
|
Describe what the agent should do.
|
|
`;
|
|
|
|
/** The user's own skills: SKILL.md plus further files; created here or imported from GitHub. */
|
|
export function Skills() {
|
|
const t = useT();
|
|
const { data, reload } = useFetch<{ skills: SkillInfo[] }>("/skills");
|
|
const [editing, setEditing] = useState<{ name: string; isNew: boolean }>();
|
|
const [importing, setImporting] = useState(false);
|
|
const [filter, setFilter] = useState("");
|
|
const list = (data?.skills ?? []).filter((s) => !filter.trim() || `${s.name} ${s.description}`.toLowerCase().includes(filter.trim().toLowerCase()));
|
|
|
|
const remove = async (s: SkillInfo) => {
|
|
if (!(await confirmDialog({ message: t("skills.confirmDelete", { name: s.name }), danger: true }))) return;
|
|
try {
|
|
await del(`/skills/${s.name}`);
|
|
reload();
|
|
} catch (e: any) {
|
|
toast(e.message);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="panel">
|
|
<h3>
|
|
<Icon name="sparkle" /> {t("skills.title")}
|
|
</h3>
|
|
<div className="hint" style={{ marginBottom: 8 }}>
|
|
{t("skills.hint")}
|
|
</div>
|
|
<div className="row" style={{ gap: 6, marginBottom: 8 }}>
|
|
<button className="primary" onClick={() => setEditing({ name: "", isNew: true })}>
|
|
<Icon name="plus" /> {t("skills.new")}
|
|
</button>
|
|
<button onClick={() => setImporting(true)}>
|
|
<Icon name="git" /> {t("skills.import")}
|
|
</button>
|
|
<span className="spacer" />
|
|
{(data?.skills.length ?? 0) > 8 && <input placeholder={t("skills.filter")} value={filter} onChange={(e) => setFilter(e.target.value)} />}
|
|
</div>
|
|
{!data ? (
|
|
<Spinner />
|
|
) : list.length === 0 ? (
|
|
<div className="empty">{t("skills.empty")}</div>
|
|
) : (
|
|
list.map((s) => (
|
|
<div key={s.name} className="row" style={{ gap: 8, padding: "6px 0", borderBottom: "1px solid var(--border)" }}>
|
|
<div style={{ flex: 1, minWidth: 0 }}>
|
|
<code>{s.name}</code>
|
|
{s.files.length > 0 && <span className="chip" style={{ marginLeft: 6 }}>{t("skills.files", { n: s.files.length })}</span>}
|
|
<div className="hint" style={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
|
|
{s.description}
|
|
</div>
|
|
</div>
|
|
<button className="small" onClick={() => setEditing({ name: s.name, isNew: false })}>
|
|
<Icon name="edit" /> {t("common.edit")}
|
|
</button>
|
|
<button className="small danger" onClick={() => void remove(s)} aria-label={t("common.delete")}>
|
|
<Icon name="trash" />
|
|
</button>
|
|
</div>
|
|
))
|
|
)}
|
|
{editing && (
|
|
<SkillEditor
|
|
name={editing.name}
|
|
isNew={editing.isNew}
|
|
onClose={(saved) => {
|
|
setEditing(undefined);
|
|
if (saved) reload();
|
|
}}
|
|
/>
|
|
)}
|
|
{importing && (
|
|
<ImportDialog
|
|
onClose={(done) => {
|
|
setImporting(false);
|
|
if (done) reload();
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SkillEditor({ name: initial, isNew, onClose }: { name: string; isNew: boolean; onClose: (saved: boolean) => void }) {
|
|
const t = useT();
|
|
const [name, setName] = useState(initial);
|
|
const [files, setFiles] = useState<SkillFile[]>(isNew ? [{ path: "SKILL.md", content: TEMPLATE }] : []);
|
|
const [loaded, setLoaded] = useState(isNew);
|
|
const [current, setCurrent] = useState("SKILL.md");
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string>();
|
|
|
|
useState(() => {
|
|
if (!isNew)
|
|
get<{ files: SkillFile[] }>(`/skills/${initial}`)
|
|
.then((d) => (setFiles(d.files), setLoaded(true)))
|
|
.catch((e) => (setError(e.message), setLoaded(true)));
|
|
});
|
|
|
|
const file = files.find((f) => f.path === current) ?? files[0];
|
|
const setContent = (content: string) => setFiles(files.map((f) => (f.path === file?.path ? { ...f, content } : f)));
|
|
const addFile = async () => {
|
|
const p = await promptDialog({ message: t("skills.addFile"), placeholder: "references/guide.md", confirmLabel: t("common.add") });
|
|
const path = p?.trim().replace(/^\/+/, "");
|
|
if (!path) return;
|
|
if (files.some((f) => f.path === path)) return setCurrent(path);
|
|
setFiles([...files, { path, content: "" }]);
|
|
setCurrent(path);
|
|
};
|
|
const removeFile = () => {
|
|
if (!file || file.path === "SKILL.md") return;
|
|
setFiles(files.filter((f) => f.path !== file.path));
|
|
setCurrent("SKILL.md");
|
|
};
|
|
const save = async () => {
|
|
setBusy(true);
|
|
setError(undefined);
|
|
try {
|
|
await put(`/skills/${name.trim()}`, { files });
|
|
onClose(true);
|
|
} catch (e: any) {
|
|
setError(e.message);
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="modal-backdrop">
|
|
<div className="modal draft-modal" role="dialog" aria-label={t("skills.edit")}>
|
|
<h3>{isNew ? t("skills.new") : t("skills.edit")}</h3>
|
|
{!loaded ? (
|
|
<Spinner />
|
|
) : (
|
|
<>
|
|
<label style={{ display: "block", marginBottom: 8 }}>
|
|
<span className="hint">{t("skills.name")}</span>
|
|
<input className="mono" style={{ width: "100%" }} value={name} disabled={!isNew} placeholder="frontend" onChange={(e) => setName(e.target.value.toLowerCase().replace(/[^a-z0-9_-]/g, "-"))} />
|
|
</label>
|
|
<div className="row" style={{ gap: 4, flexWrap: "wrap", marginBottom: 6 }}>
|
|
{files.map((f) => (
|
|
<button key={f.path} className={`small ${f.path === file?.path ? "primary" : ""}`} onClick={() => setCurrent(f.path)}>
|
|
{f.path}
|
|
</button>
|
|
))}
|
|
<button className="small ghost" onClick={() => void addFile()}>
|
|
<Icon name="plus" /> {t("skills.file")}
|
|
</button>
|
|
{file && file.path !== "SKILL.md" && (
|
|
<button className="small danger" onClick={removeFile} aria-label={t("common.delete")}>
|
|
<Icon name="trash" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
<textarea className="mono" rows={18} style={{ width: "100%" }} value={file?.content ?? ""} onChange={(e) => setContent(e.target.value)} spellCheck={false} />
|
|
<div className="hint">{t("skills.fileHint")}</div>
|
|
</>
|
|
)}
|
|
{error && <div className="notice err">{error}</div>}
|
|
<div className="row" style={{ marginTop: 10, justifyContent: "flex-end", gap: 6 }}>
|
|
<button onClick={() => onClose(false)} disabled={busy}>
|
|
{t("common.cancel")}
|
|
</button>
|
|
<button className="primary" onClick={() => void save()} disabled={busy || !loaded || !name.trim() || !files.some((f) => f.path === "SKILL.md")}>
|
|
{busy ? <Spinner small /> : <Icon name="check" />} {t("common.save")}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
interface Found {
|
|
path: string;
|
|
name: string;
|
|
files: number;
|
|
}
|
|
|
|
function ImportDialog({ onClose }: { onClose: (done: boolean) => void }) {
|
|
const t = useT();
|
|
const [url, setUrl] = useState("");
|
|
const [found, setFound] = useState<Found[]>();
|
|
const [picked, setPicked] = useState<Set<string>>(new Set());
|
|
const [filter, setFilter] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string>();
|
|
|
|
const scan = async () => {
|
|
setBusy(true);
|
|
setError(undefined);
|
|
try {
|
|
const r = await post<{ skills: Found[] }>("/skills/github/scan", { url: url.trim() });
|
|
setFound(r.skills);
|
|
setPicked(new Set());
|
|
if (r.skills.length === 0) setError(t("skills.noneFound"));
|
|
} catch (e: any) {
|
|
setError(e.message);
|
|
}
|
|
setBusy(false);
|
|
};
|
|
const shown = (found ?? []).filter((s) => !filter.trim() || s.path.toLowerCase().includes(filter.trim().toLowerCase()));
|
|
const toggle = (p: string) => setPicked((s) => (s.has(p) ? new Set([...s].filter((x) => x !== p)) : new Set(s).add(p)));
|
|
const run = async () => {
|
|
setBusy(true);
|
|
setError(undefined);
|
|
try {
|
|
const r = await post<{ saved: unknown[]; skipped: { path: string; why: string }[] }>("/skills/github/import", { url: url.trim(), paths: [...picked] });
|
|
toast(t("skills.imported", { n: r.saved.length }) + (r.skipped.length ? ` (${r.skipped.length} ${t("skills.skipped")})` : ""), "success");
|
|
onClose(true);
|
|
} catch (e: any) {
|
|
setError(e.message);
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="modal-backdrop">
|
|
<div className="modal draft-modal" role="dialog" aria-label={t("skills.import")}>
|
|
<h3>{t("skills.import")}</h3>
|
|
<div className="hint" style={{ marginBottom: 8 }}>
|
|
{t("skills.importHint")}
|
|
</div>
|
|
<div className="row" style={{ gap: 6 }}>
|
|
<input className="mono" style={{ flex: 1 }} value={url} placeholder="https://github.com/owner/repo/tree/main/skills" onChange={(e) => setUrl(e.target.value)} onKeyDown={(e) => e.key === "Enter" && url.trim() && !busy && void scan()} />
|
|
<button onClick={() => void scan()} disabled={busy || !url.trim()}>
|
|
{busy && !found ? <Spinner small /> : <Icon name="search" />} {t("skills.scan")}
|
|
</button>
|
|
</div>
|
|
{found && found.length > 0 && (
|
|
<>
|
|
<div className="row" style={{ gap: 6, margin: "8px 0" }}>
|
|
<input style={{ flex: 1 }} placeholder={t("skills.filter")} value={filter} onChange={(e) => setFilter(e.target.value)} />
|
|
<button className="small" onClick={() => setPicked(new Set([...picked, ...shown.map((s) => s.path)]))}>
|
|
{t("skills.selectShown", { n: shown.length })}
|
|
</button>
|
|
<button className="small ghost" onClick={() => setPicked(new Set())}>
|
|
{t("skills.selectNone")}
|
|
</button>
|
|
</div>
|
|
<div className="issue-list">
|
|
{shown.map((s) => (
|
|
<label key={s.path} className="row" style={{ gap: 8, padding: "2px 0" }}>
|
|
<input type="checkbox" checked={picked.has(s.path)} onChange={() => toggle(s.path)} />
|
|
<span>
|
|
<code>{s.name}</code> <span className="hint">{s.path} · {t("skills.files", { n: s.files })}</span>
|
|
</span>
|
|
</label>
|
|
))}
|
|
</div>
|
|
</>
|
|
)}
|
|
{error && <div className="notice err">{error}</div>}
|
|
<div className="row" style={{ marginTop: 10, justifyContent: "flex-end", gap: 6 }}>
|
|
<button onClick={() => onClose(false)} disabled={busy}>
|
|
{t("common.cancel")}
|
|
</button>
|
|
<button className="primary" onClick={() => void run()} disabled={busy || picked.size === 0}>
|
|
{busy && found ? <Spinner small /> : <Icon name="check" />} {t("skills.importN", { n: picked.size })}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|