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 (

{t("skills.title")}

{t("skills.hint")}
{(data?.skills.length ?? 0) > 8 && setFilter(e.target.value)} />}
{!data ? ( ) : list.length === 0 ? (
{t("skills.empty")}
) : ( list.map((s) => (
{s.name} {s.files.length > 0 && {t("skills.files", { n: s.files.length })}}
{s.description}
)) )} {editing && ( { setEditing(undefined); if (saved) reload(); }} /> )} {importing && ( { setImporting(false); if (done) reload(); }} /> )}
); } 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(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(); 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 (

{isNew ? t("skills.new") : t("skills.edit")}

{!loaded ? ( ) : ( <>
{files.map((f) => ( ))} {file && file.path !== "SKILL.md" && ( )}