library: a skill by a name the library has from elsewhere goes in beside it
mora1n on Discord: anthropics/skills' skill-creator and openai/skills'
are two skills by one name, and only the first could be added; the
second was skipped as "another skill called skill-creator, left as it
is". The library keyed being had by name in every place that asked.
Now being had is decided by source (hadSkill): the same source under any
name here, or a skill with no source (taken in from an agent's folder)
by that name with the very same files. Another skill from GitHub or a
git repository whose name is taken goes in beside it as
<name>-<owner> (skill-creator-openai), then owner-repo, repo, a number,
at most 64 bytes (freeSkillName in skill_beside.go). The library's copy
of its SKILL.md gets that name in the front matter's top-level name:
line (nameSkill), so no agent sees two skills by one name; Skill.Called
keeps the name it came with, and the carried bundle carries it. Its hash
is of the renamed copy, and checks (GitHub and git) and updates compare
what they fetch renamed the same way (fetchedHash), so it doesn't read
as edited and an update keeps the name.
The class: probe (Candidate.Have now means this skill; Candidate.As is
the name it has or would get), installFrom (listed and unlisted folder
by that name), the new-skills check (no longer drops a skill because
its name is taken), the market's had check (by name only for a skill
with no repository: none or a folder), carry, the TUI's probe list and
the GUI's probe list ("adds as {name}" with why in its title, "In the
library as {name}"; zh, zh-TW, ja, de; the tag is cut short in its row).
Old data: nothing is renamed. A skill already in the library keeps its
name, folder, SKILL.md and agent links; library.json without "called"
reads as before. Only a skill added from now on can get another name.
Not changed: a folder of the user's own is linked as it is and its
SKILL.md can't be named apart, so one by a taken name is still skipped,
now saying to rename it in its SKILL.md; and an agent's own skills
folder still holds one skill per name (an agent's own skill by the
library's name is settled by Use library / Keep agent's as before).
Run (mote, at c6fd4b37c on origin/main 6a900e0d0, then rebased on
0fe9f97c1, which touches only claudetui, gui and provider):
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build ./..., go build -tags nogui:
clean.
- go test -tags nogui -count=1 -timeout 45m ./...: 82 packages ok,
internal/gui red only in TestSettingsSaveKeepsWhatItDoesNotSend
(SearchAll, #4514, open on main, not this change).
- After the rebase: internal/library and internal/tui ok.
- GUI, both engines: skill-install-beside (new), every skill-*,
library-*, found-skill-*, remove-found-*, mcp-*, i18n-keys, gui-ja,
gui-de, gui-zh-tw, font-cascade, inner-scroll-contain, win-fonts:
1427 tests, 1423 pass. The 4 reds fail the same at origin/main
3baf3b6df run alone: gui-ja (#4580), i18n-keys "Unmarked" (#4524),
library-row-remove chromium (#3099).
Red on the old code: the anthropics-then-openai install against
origin/main code fails with "the library already has another skill
called skill-creator, left as it is". Broken by hand: fetchedHash made
to hash the fetched files unrenamed turns
TestSameNameFromAnotherSourceGoesInBeside red (hash mismatch); the
.lib-pick > .lib-tag rule removed turns skill-install-beside red ("the
tag stays in its row") in both widths.
Not tried: real anthropics/skills and openai/skills installs in a
sandbox HOME (the tests serve both repositories' layouts as tarballs;
the fixtures put both at skills/skill-creator. The real openai/skills
keeps it under skills/.system, which a probe of the repository's top
doesn't walk into; that is how probing has been and isn't changed
here); Windows and Linux runs of the GUI.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -5110,7 +5110,11 @@ magpie.plugin("library", (ctx) => {
|
||||
const w = el("span", "who");
|
||||
w.append(el("span", "name", x.name), el("span", "sub", x.description || x.path || ""));
|
||||
r.append(cb, w);
|
||||
if (x.have) r.append(tag(t("in the library"), ""));
|
||||
// another skill from elsewhere has its name (mora1n on Discord):
|
||||
// this one is in the library, or goes in beside it, by its own
|
||||
if (x.have && x.as) r.append(tag(t("In the library as {name}", { name: x.as }), "lib-as", t("In the library as {name}", { name: x.as })));
|
||||
else if (x.have) r.append(tag(t("in the library"), ""));
|
||||
else if (x.as) r.append(tag(t("adds as {name}", { name: x.as }), "lib-as", t("The library has another skill called {name}, from elsewhere. This one goes in beside it as {as}.", { name: x.name, as: x.as })));
|
||||
list.append(r);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -716,6 +716,8 @@ magpie.strings({
|
||||
"Nothing to show.": "暂无内容。",
|
||||
"Remote · sign-in": "远程 · 需登录",
|
||||
"In the library as {name}": "已在资源库中,名为 {name}",
|
||||
"adds as {name}": "将添加为 {name}",
|
||||
"The library has another skill called {name}, from elsewhere. This one goes in beside it as {as}.": "资源库里已有一个来自别处、同样叫 {name} 的技能。这一个会以 {as} 为名,和它并存。",
|
||||
"Needs {what} to add": "添加时需要 {what}",
|
||||
"Add…": "添加…",
|
||||
"About {name}": "关于 {name}",
|
||||
@@ -1451,6 +1453,8 @@ magpie.strings({
|
||||
"Nothing to show.": "暫無內容。",
|
||||
"Remote · sign-in": "遠端 · 需登入",
|
||||
"In the library as {name}": "已在資源庫中,名為 {name}",
|
||||
"adds as {name}": "將新增為 {name}",
|
||||
"The library has another skill called {name}, from elsewhere. This one goes in beside it as {as}.": "資源庫裡已有一個來自別處、同樣叫 {name} 的技能。這一個會以 {as} 為名,和它並存。",
|
||||
"Needs {what} to add": "新增時需要 {what}",
|
||||
"Add…": "新增…",
|
||||
"About {name}": "關於 {name}",
|
||||
@@ -2185,6 +2189,8 @@ magpie.strings({
|
||||
"Nothing to show.": "表示するものがありません。",
|
||||
"Remote · sign-in": "リモート · サインイン",
|
||||
"In the library as {name}": "ライブラリに {name} として登録済み",
|
||||
"adds as {name}": "{name} として追加",
|
||||
"The library has another skill called {name}, from elsewhere. This one goes in beside it as {as}.": "ライブラリには別の場所から入れた {name} という名前のスキルがすでにあります。このスキルは {as} として並べて追加されます。",
|
||||
"Needs {what} to add": "追加には {what} が必要です",
|
||||
"Add…": "追加…",
|
||||
"About {name}": "{name} について",
|
||||
@@ -2920,6 +2926,8 @@ magpie.strings({
|
||||
"Nothing to show.": "Nichts anzuzeigen.",
|
||||
"Remote · sign-in": "Remote · Anmeldung",
|
||||
"In the library as {name}": "In der Bibliothek als {name}",
|
||||
"adds as {name}": "wird als {name} hinzugefügt",
|
||||
"The library has another skill called {name}, from elsewhere. This one goes in beside it as {as}.": "Die Bibliothek hat schon einen anderen Skill namens {name} aus einer anderen Quelle. Dieser kommt als {as} daneben hinzu.",
|
||||
"Needs {what} to add": "Zum Hinzufügen wird {what} benötigt",
|
||||
"Add…": "Hinzufügen…",
|
||||
"About {name}": "Über {name}",
|
||||
|
||||
@@ -411,6 +411,9 @@ input.lib-setname:focus { border-color: var(--accent); box-shadow: 0 0 0 3px col
|
||||
.lib-pick .who { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
|
||||
.lib-pick .name { font-size: 13px; font-weight: 500; }
|
||||
.lib-pick .sub { font-size: 11.5px; color: var(--muted); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
|
||||
/* "adds as skill-creator-openai": a name can be 64 letters long, so the
|
||||
tag gives way to the skill's own name and is cut short, its title whole */
|
||||
.lib-pick > .lib-tag { flex: 0 1 auto; min-width: 0; max-width: 45%; display: block; line-height: 20px; overflow: hidden; text-overflow: ellipsis; }
|
||||
.lib-probefoot { display: flex; align-items: center; gap: 8px; padding: 8px 4px 0; border-top: 1px solid var(--line-2); flex-wrap: wrap; }
|
||||
.lib-probefoot .note { font-size: 12px; color: var(--muted); }
|
||||
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// mora1n on Discord: anthropics/skills' skill-creator and openai/skills' are
|
||||
// two skills by one name, and only the first could be added. Now the second
|
||||
// goes in beside it under a name of its own, and the list of what a
|
||||
// repository has says so before the click: "adds as skill-creator-openai",
|
||||
// with why in its title, and one had already under such a name says that
|
||||
// name. The skill stays pickable, the tag is cut short rather than pushing
|
||||
// the row wider, in English and Chinese, wide and narrow. No backend: the
|
||||
// API is faked here.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
const { test } = require("node:test");
|
||||
const { chromium, webkit } = require("playwright");
|
||||
|
||||
const assets = path.resolve(__dirname, "../assets");
|
||||
const HOME = "/Users/mora1n";
|
||||
const long = "a-skill-whose-name-is-as-long-as-the-library-lets-it-be-openai";
|
||||
const probe = {
|
||||
source: "openai/skills", kind: "github", candidates: [
|
||||
{ path: "skills/skill-creator", name: "skill-creator", description: "Guide for creating effective skills.", have: false, as: "skill-creator-openai" },
|
||||
{ path: "skills/pdf", name: "pdf", description: "PDFs", have: true, as: "pdf-openai" },
|
||||
{ path: "skills/docx", name: "docx", description: "Word files", have: false },
|
||||
{ path: "skills/long", name: "a-skill-whose-name-is-as-long-as-the-library-lets-it-be", description: "Long", have: false, as: long },
|
||||
],
|
||||
};
|
||||
const lib = () => ({
|
||||
dir: `${HOME}/.config/magpie/library`, backups: `${HOME}/.config/magpie/backups`, home: HOME,
|
||||
agents: [{ id: "claude", name: "Claude Code", icon: "", skills: `${HOME}/.claude/skills` }],
|
||||
instructions: { agents: [] }, servers: [], skills: [], foundServers: [], projects: [], foundSkills: [],
|
||||
});
|
||||
|
||||
function server(lang, posts) {
|
||||
return async (route) => {
|
||||
const req = route.request(), url = new URL(req.url());
|
||||
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
||||
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||||
if (url.pathname === "/api/state") return route.fulfill({ json: { agents: [], profiles: [], settings: { lang, theme: "light" } } });
|
||||
if (url.pathname === "/api/library") return route.fulfill({ json: lib() });
|
||||
if (url.pathname === "/api/library/skills/probe") return route.fulfill({ json: probe });
|
||||
if (url.pathname === "/api/library/skills/install") {
|
||||
posts.push(req.postDataJSON().paths);
|
||||
return route.fulfill({ json: { ...lib(), result: { changed: ["claude"], problems: [], installed: ["skill-creator-openai", "docx"], had: [], skipped: [] } } });
|
||||
}
|
||||
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
|
||||
if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] });
|
||||
if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } });
|
||||
if (url.pathname === "/api/providers") return route.fulfill({ json: { providers: [], gateway: { running: true } } });
|
||||
if (url.pathname.startsWith("/api/")) return route.fulfill({ json: {} });
|
||||
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
||||
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
||||
await route.fulfill({ body: await fs.readFile(file), contentType });
|
||||
};
|
||||
}
|
||||
|
||||
const words = {
|
||||
en: { adds: "adds as skill-creator-openai", had: "In the library as pdf-openai", why: "The library has another skill called skill-creator, from elsewhere. This one goes in beside it as skill-creator-openai." },
|
||||
zh: { adds: "将添加为 skill-creator-openai", had: "已在资源库中,名为 pdf-openai", why: "资源库里已有一个来自别处、同样叫 skill-creator 的技能。这一个会以 skill-creator-openai 为名,和它并存。" },
|
||||
};
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
test(engine + ": a skill by a name the library has from elsewhere says the name it goes in by", async (t) => {
|
||||
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
const errors = [];
|
||||
t.after(async () => {
|
||||
if (errors.length) console.log(errors);
|
||||
await browser.close();
|
||||
});
|
||||
const run = async (lang, width) => {
|
||||
const posts = [];
|
||||
const ctx = await browser.newContext({ viewport: { width, height: 800 } });
|
||||
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "skills"); } catch {} });
|
||||
const page = await ctx.newPage();
|
||||
page.setDefaultTimeout(5000);
|
||||
page.setDefaultNavigationTimeout(60000);
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", server(lang, posts));
|
||||
await page.goto("http://magpie.test/");
|
||||
await page.locator('button[data-view="library"]').click();
|
||||
const src = page.locator('#view-library input[data-lib="source"]');
|
||||
await src.fill("openai/skills");
|
||||
await src.press("Enter");
|
||||
const rows = page.locator("#view-library .lib-pick");
|
||||
await rows.first().waitFor();
|
||||
const w = words[lang];
|
||||
const creator = rows.filter({ hasText: "Guide for creating" });
|
||||
const adds = creator.locator(".lib-tag");
|
||||
assert.equal((await adds.textContent()).trim(), w.adds);
|
||||
assert.equal(await adds.getAttribute("title"), w.why);
|
||||
assert.equal(await creator.locator("input").isDisabled(), false, "it can be picked");
|
||||
const had = rows.filter({ hasText: "PDFs" });
|
||||
assert.equal((await had.locator(".lib-tag").textContent()).trim(), w.had);
|
||||
assert.equal(await had.locator("input").isDisabled(), true);
|
||||
assert.equal(await rows.filter({ hasText: "Word files" }).locator(".lib-tag").count(), 0, "a name nothing has gets no tag");
|
||||
// the longest name a skill can have is cut short inside its row,
|
||||
// never pushing the list wider
|
||||
const fit = await rows.filter({ hasText: "Long" }).evaluate((r) => {
|
||||
const tg = r.querySelector(".lib-tag"), box = r.getBoundingClientRect(), b = tg.getBoundingClientRect();
|
||||
const list = r.closest(".lib-picks");
|
||||
return { inside: b.right <= box.right + 0.5 && b.left >= box.left - 0.5, cut: tg.scrollWidth > tg.clientWidth, over: list.scrollWidth - list.clientWidth, name: r.querySelector(".name").getBoundingClientRect().width };
|
||||
});
|
||||
assert.equal(fit.inside, true, "the tag stays in its row");
|
||||
assert.equal(fit.over, 0, "the list doesn't scroll sideways");
|
||||
assert(fit.name > 60, `the skill's own name keeps room (${fit.name}px)`);
|
||||
if (width < 600) assert.equal(fit.cut, true, "at this width the long tag is cut short");
|
||||
// three to add are picked for you, the one beside another among them
|
||||
assert.equal(await creator.locator("input").isChecked(), true);
|
||||
await page.locator("#view-library .lib-probefoot button.primary").click();
|
||||
await page.waitForTimeout(300);
|
||||
assert.deepEqual(posts.map((p) => [...p].sort()), [["skills/docx", "skills/long", "skills/skill-creator"]]);
|
||||
await ctx.close();
|
||||
};
|
||||
|
||||
await t.test("in English", () => run("en", 980));
|
||||
await t.test("in Chinese, narrow", () => run("zh", 440));
|
||||
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
@@ -43,6 +43,8 @@ type CarriedSkill struct {
|
||||
Files map[string][]byte `json:"files"` // by path in the folder, with /
|
||||
Exec []string `json:"exec,omitempty"` // the files that run
|
||||
Left []string `json:"left,omitempty"` // files too big to carry
|
||||
// Called is the name its SKILL.md gives, when that isn't Name
|
||||
Called string `json:"called,omitempty"`
|
||||
}
|
||||
|
||||
// A skill's file bigger than maxCarried is left out, and so are the
|
||||
@@ -102,7 +104,7 @@ func Collect() (*Bundle, error) {
|
||||
}
|
||||
for i, c := range readSkills(dirs) {
|
||||
s := skills[i]
|
||||
c.Name, c.Source, c.Agents = s.Name, s.Source, orNone(slices.Sorted(slices.Values(s.Agents)))
|
||||
c.Name, c.Source, c.Agents, c.Called = s.Name, s.Source, orNone(slices.Sorted(slices.Values(s.Agents))), s.Called
|
||||
b.Skills = append(b.Skills, c)
|
||||
}
|
||||
subs := []*Subagent{}
|
||||
@@ -414,7 +416,7 @@ func Put(b *Bundle) (*Result, error) {
|
||||
for _, s := range b.Skills {
|
||||
// turned off here stays off here: that is this computer's own
|
||||
off := l.skill(s.Name) != nil && l.skill(s.Name).OffHere
|
||||
skills = append(skills, &Skill{Name: s.Name, Source: s.Source, Agents: slices.Clone(orNone(s.Agents)), OffHere: off})
|
||||
skills = append(skills, &Skill{Name: s.Name, Source: s.Source, Agents: slices.Clone(orNone(s.Agents)), OffHere: off, Called: s.Called})
|
||||
if keep[s.Name] {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -960,10 +960,12 @@ func (l *Library) haveSkill(source, id, name string) string {
|
||||
}
|
||||
}
|
||||
// one taken in from an agent's folder (npx skills puts them in
|
||||
// ~/.agents/skills) has no source to match, and adding is turned away
|
||||
// by name all the same (InstallMarketSkill): it's had by its name
|
||||
// ~/.agents/skills) has no repository to match: it's had by its name,
|
||||
// unless it is known to come from another repository. One by that name
|
||||
// from another repository is another skill, added beside it
|
||||
// (skill_beside.go)
|
||||
for _, n := range []string{id, name} {
|
||||
if s := l.skill(n); s != nil {
|
||||
if s := l.skill(n); s != nil && (s.Source == nil || s.Source.Kind == "folder") && (s.From == "" || strings.EqualFold(s.From, source)) {
|
||||
return s.Name
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Two skills from different places can go by one name: anthropics/skills'
|
||||
// skill-creator and openai/skills' are not the same skill (mora1n on
|
||||
// Discord). The library keeps each under a name of its own, since that
|
||||
// name is its folder here and in every agent's skills folder: the first
|
||||
// keeps the name its SKILL.md gives, and one installed after it goes in
|
||||
// beside it under that name with its source's owner added
|
||||
// (skill-creator-openai). Called keeps the name its SKILL.md gave.
|
||||
|
||||
// hadSkill is the library's skill that is the one at from already: one
|
||||
// installed from that very place, whatever its name here, or one by its
|
||||
// name with no source to tell it by (taken in from an agent's folder) and
|
||||
// the very same files.
|
||||
func (l *Library) hadSkill(src Source, c Candidate, from string) *Skill {
|
||||
for _, s := range l.Skills {
|
||||
if fromSameSource(s, src, c.Path, from) {
|
||||
return s
|
||||
}
|
||||
}
|
||||
if s := l.skill(c.Name); s != nil && s.Source == nil && sameTree(skillDir(s.Name), from) {
|
||||
return s
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// skillNameTaken is whether the library has a skill called name, or its
|
||||
// folder has something by that name. One that can't be looked at is
|
||||
// taken: what is there is unknown.
|
||||
func (l *Library) skillNameTaken(name string) bool {
|
||||
if l.skill(name) != nil {
|
||||
return true
|
||||
}
|
||||
_, err := os.Lstat(skillDir(name))
|
||||
return !errors.Is(err, fs.ErrNotExist)
|
||||
}
|
||||
|
||||
// freeSkillName is the name a skill called name from src goes by in the
|
||||
// library: name itself while nothing has it, else name with the source's
|
||||
// owner, then its repository's (or folder's) name, then a number added.
|
||||
func (l *Library) freeSkillName(name string, src Source) string {
|
||||
if !l.skillNameTaken(name) {
|
||||
return name
|
||||
}
|
||||
owner, repo := sourceParts(src)
|
||||
var tries []string
|
||||
if owner != "" {
|
||||
tries = append(tries, owner, owner+"-"+repo)
|
||||
}
|
||||
if repo != "" {
|
||||
tries = append(tries, repo)
|
||||
}
|
||||
for _, s := range tries {
|
||||
if n := withSuffix(name, s); n != "" && n != name && !l.skillNameTaken(n) {
|
||||
return n
|
||||
}
|
||||
}
|
||||
base := owner
|
||||
if base == "" {
|
||||
base = repo
|
||||
}
|
||||
for i := 2; ; i++ {
|
||||
suffix := fmt.Sprint(i)
|
||||
if base != "" {
|
||||
suffix = base + "-" + suffix
|
||||
}
|
||||
if n := withSuffix(name, suffix); n != "" && !l.skillNameTaken(n) {
|
||||
return n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sourceParts is a source's owner and repository: openai and skills for
|
||||
// openai/skills or https://gitlab.com/openai/skills.git, and for a folder
|
||||
// the folder it is in and its own name.
|
||||
func sourceParts(src Source) (owner, repo string) {
|
||||
var parts []string
|
||||
if src.Kind == "folder" {
|
||||
d := filepath.Clean(src.Dir)
|
||||
parts = []string{filepath.Base(filepath.Dir(d)), filepath.Base(d)}
|
||||
} else {
|
||||
r := strings.TrimSuffix(strings.TrimRight(src.Repo, "/"), ".git")
|
||||
parts = strings.FieldsFunc(r, func(c rune) bool { return c == '/' || c == ':' })
|
||||
}
|
||||
switch len(parts) {
|
||||
case 0:
|
||||
return "", ""
|
||||
case 1:
|
||||
return "", fitName(parts[0])
|
||||
}
|
||||
return fitName(parts[len(parts)-2]), fitName(parts[len(parts)-1])
|
||||
}
|
||||
|
||||
// withSuffix is name-suffix, name cut short so that the whole is a name
|
||||
// the library takes (nameRe): at most 64 bytes.
|
||||
func withSuffix(name, suffix string) string {
|
||||
suffix = fitName(suffix)
|
||||
if suffix == "" {
|
||||
return ""
|
||||
}
|
||||
if max := 64 - len(suffix) - 1; len(name) > max {
|
||||
if max < 1 {
|
||||
return ""
|
||||
}
|
||||
name = strings.TrimRight(name[:max], "-_")
|
||||
}
|
||||
n := name + "-" + suffix
|
||||
if !nameRe.MatchString(n) {
|
||||
return ""
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// nameSkill sets the name a skill's SKILL.md gives in its front matter to
|
||||
// name, the folder's: an agent knows a skill by that name, and two by one
|
||||
// name in an agent are one shadowing the other. Nothing else in it
|
||||
// changes. A SKILL.md that gives no name already goes by its folder's,
|
||||
// and one that is a link isn't written through.
|
||||
func nameSkill(dir, name string) error {
|
||||
p := filepath.Join(dir, "SKILL.md")
|
||||
fi, err := os.Lstat(p)
|
||||
if err != nil || !fi.Mode().IsRegular() {
|
||||
return err
|
||||
}
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if out, ok := renamedMeta(string(b), name); ok {
|
||||
return os.WriteFile(p, []byte(out), fi.Mode().Perm())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// renamedMeta is a SKILL.md with its front matter's top-level name set to
|
||||
// name, its line endings kept; false when it gives no name to set.
|
||||
func renamedMeta(s, name string) (string, bool) {
|
||||
lines := strings.SplitAfter(s, "\n")
|
||||
if len(lines) == 0 || strings.TrimRight(lines[0], "\r\n") != "---" {
|
||||
return s, false
|
||||
}
|
||||
for i := 1; i < len(lines); i++ {
|
||||
line := strings.TrimRight(lines[i], "\r\n")
|
||||
if line == "---" {
|
||||
break
|
||||
}
|
||||
if strings.HasPrefix(line, "name:") {
|
||||
if strings.TrimSpace(strings.Trim(strings.TrimSpace(line[len("name:"):]), `"'`)) == name {
|
||||
return s, false
|
||||
}
|
||||
lines[i] = "name: " + name + lines[i][len(line):]
|
||||
return strings.Join(lines, ""), true
|
||||
}
|
||||
}
|
||||
return s, false
|
||||
}
|
||||
|
||||
// fetchedHash is the hash a skill's files fetched at from have once in
|
||||
// the library: for one kept under another name than its SKILL.md gives
|
||||
// (Called), as named there. One that can't be told is "".
|
||||
func (s *Skill) fetchedHash(from string) string {
|
||||
if s.Called == "" {
|
||||
return hashDir(from)
|
||||
}
|
||||
tmp, err := os.MkdirTemp("", "magpie-skill-named-")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer os.RemoveAll(tmp)
|
||||
dir := filepath.Join(tmp, s.Name)
|
||||
if err := copyDir(from, dir); err != nil {
|
||||
return ""
|
||||
}
|
||||
if err := nameSkill(dir, s.Name); err != nil {
|
||||
return ""
|
||||
}
|
||||
return hashDir(dir)
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// skillCreators serves anthropics/skills and openai/skills, each with a
|
||||
// skill-creator of its own, laid out as the two repositories have them:
|
||||
// anthropics' and openai's in skills/. version is what openai's says it
|
||||
// is.
|
||||
func skillCreators(t *testing.T, version *string) {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, "/anthropics/skills/"):
|
||||
w.Write(tarball(t, map[string]string{
|
||||
"skills/skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Guide for creating effective skills (Anthropic).\nlicense: Complete terms in LICENSE.txt\n---\n\n# Skill Creator\n",
|
||||
"skills/skill-creator/LICENSE.txt": "Apache",
|
||||
}))
|
||||
case strings.HasPrefix(r.URL.Path, "/openai/skills/"):
|
||||
w.Write(tarball(t, map[string]string{
|
||||
"skills/skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Guide for creating effective skills (OpenAI " + *version + ").\nmetadata:\n short-description: Create or update a skill\n---\n\n# Skill Creator\n",
|
||||
}))
|
||||
case strings.HasPrefix(r.URL.Path, "/acme/skills/"):
|
||||
w.Write(tarball(t, map[string]string{
|
||||
"skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Acme's own\n---\n",
|
||||
}))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
old := tarballURL
|
||||
tarballURL = func(repo, ref string) string { return srv.URL + "/" + repo + "/" + ref }
|
||||
t.Cleanup(func() { tarballURL = old })
|
||||
}
|
||||
|
||||
// mora1n on Discord: anthropics' skill-creator and openai's are two
|
||||
// skills by one name, and only the first could be installed; the second
|
||||
// was "left as it is". Now the second goes in beside the first under a
|
||||
// name of its own, its source's owner added, and every agent gets both.
|
||||
// The first keeps its name, folder and links.
|
||||
func TestSameNameFromAnotherSourceGoesInBeside(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
version := "one"
|
||||
skillCreators(t, &version)
|
||||
|
||||
ok(t)(InstallSkills("anthropics/skills", []string{"skills/skill-creator"}, []string{"claude"}))
|
||||
anthropic := read(t, filepath.Join(skillDir("skill-creator"), "SKILL.md"))
|
||||
|
||||
p, err := ProbeSkills("openai/skills")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(p.Candidates) != 1 || p.Candidates[0].Have || p.Candidates[0].As != "skill-creator-openai" {
|
||||
t.Fatalf("openai's probed as %+v", p.Candidates)
|
||||
}
|
||||
r := ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, []string{"claude"}))
|
||||
if !slices.Equal(r.Installed, []string{"skill-creator-openai"}) || len(r.Skipped) != 0 {
|
||||
t.Fatalf("installed %v skipped %+v", r.Installed, r.Skipped)
|
||||
}
|
||||
|
||||
l, _ := load()
|
||||
first, second := l.skill("skill-creator"), l.skill("skill-creator-openai")
|
||||
if first == nil || first.Source.Repo != "anthropics/skills" || first.Called != "" {
|
||||
t.Errorf("anthropics' is now %+v", first)
|
||||
}
|
||||
if second == nil || second.Source.Repo != "openai/skills" || second.Source.Path != "skills/skill-creator" || second.Called != "skill-creator" {
|
||||
t.Fatalf("openai's is %+v", second)
|
||||
}
|
||||
if got := read(t, filepath.Join(skillDir("skill-creator"), "SKILL.md")); got != anthropic {
|
||||
t.Errorf("anthropics' SKILL.md changed: %q", got)
|
||||
}
|
||||
// the agent knows it by its folder's name: two skill-creators in
|
||||
// one agent would be one hiding the other
|
||||
want := "---\nname: skill-creator-openai\ndescription: Guide for creating effective skills (OpenAI one).\nmetadata:\n short-description: Create or update a skill\n---\n\n# Skill Creator\n"
|
||||
if got := read(t, filepath.Join(skillDir("skill-creator-openai"), "SKILL.md")); got != want {
|
||||
t.Errorf("openai's SKILL.md is %q", got)
|
||||
}
|
||||
for _, n := range []string{"skill-creator", "skill-creator-openai"} {
|
||||
if !ours(filepath.Join(h, ".claude/skills", n), n) {
|
||||
t.Errorf("claude wasn't given %s", n)
|
||||
}
|
||||
}
|
||||
// named so, it is what was fetched: not changed here, and a check
|
||||
// finds the same files at its source
|
||||
if edited, known := l.editState(second, Targets()); edited || !known {
|
||||
t.Errorf("openai's reads as edited=%v known=%v", edited, known)
|
||||
}
|
||||
if got := second.fetchedHash(filepath.Join(p.root, "skills", "skill-creator")); got == "" || got != second.Hash {
|
||||
t.Errorf("fetched hash %q, library's %q", got, second.Hash)
|
||||
}
|
||||
v, _ := Read(nil)
|
||||
for _, s := range v.Skills {
|
||||
if s.Name == "skill-creator-openai" && s.Source != "https://github.com/openai/skills/tree/HEAD/skills/skill-creator" {
|
||||
t.Errorf("openai's source reads %q", s.Source)
|
||||
}
|
||||
}
|
||||
|
||||
// installed again, it is had, by the name it has here
|
||||
r = ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, nil))
|
||||
if len(r.Installed) != 0 || !slices.Equal(r.Had, []string{"skill-creator-openai"}) {
|
||||
t.Errorf("again: installed %v had %v", r.Installed, r.Had)
|
||||
}
|
||||
if p, _ := ProbeSkills("openai/skills"); !p.Candidates[0].Have || p.Candidates[0].As != "skill-creator-openai" {
|
||||
t.Errorf("probed again: %+v", p.Candidates)
|
||||
}
|
||||
// the market sees which repository's it is
|
||||
l, _ = load()
|
||||
if got := l.haveSkill("openai/skills", "skill-creator", "skill-creator"); got != "skill-creator-openai" {
|
||||
t.Errorf("market has openai's as %q", got)
|
||||
}
|
||||
if got := l.haveSkill("acme/skills", "skill-creator", "skill-creator"); got != "" {
|
||||
t.Errorf("market has acme's as %q", got)
|
||||
}
|
||||
// a third from elsewhere gets its own owner's name
|
||||
if r := ok(t)(InstallMarketSkill("acme/skills", "skill-creator", []string{"claude"})); !slices.Equal(r.Installed, []string{"skill-creator-acme"}) {
|
||||
t.Errorf("acme's: installed %v skipped %+v", r.Installed, r.Skipped)
|
||||
}
|
||||
|
||||
// an update keeps the name it has here, in its SKILL.md too
|
||||
version = "two"
|
||||
ok(t)(UpdateSkill("skill-creator-openai", false))
|
||||
got := read(t, filepath.Join(skillDir("skill-creator-openai"), "SKILL.md"))
|
||||
if !strings.Contains(got, "name: skill-creator-openai\n") || !strings.Contains(got, "(OpenAI two)") {
|
||||
t.Errorf("after update: %q", got)
|
||||
}
|
||||
l, _ = load()
|
||||
if edited, _ := l.editState(l.skill("skill-creator-openai"), Targets()); edited {
|
||||
t.Error("the update reads as changed here")
|
||||
}
|
||||
}
|
||||
|
||||
// A skill taken in from an agent's folder has no source to tell it by:
|
||||
// the very same files by its name are had, not installed twice.
|
||||
func TestSameFilesByNameAreHad(t *testing.T) {
|
||||
sandbox(t)
|
||||
version := "one"
|
||||
skillCreators(t, &version)
|
||||
p, err := ProbeSkills("acme/skills")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := copyDir(filepath.Join(p.root, "skill-creator"), skillDir("skill-creator")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := change(func(l *Library) error {
|
||||
l.Skills = append(l.Skills, &Skill{Name: "skill-creator", Agents: []string{}})
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := ok(t)(InstallSkills("acme/skills", []string{"skill-creator"}, nil))
|
||||
if len(r.Installed) != 0 || !slices.Equal(r.Had, []string{"skill-creator"}) {
|
||||
t.Errorf("installed %v had %v", r.Installed, r.Had)
|
||||
}
|
||||
// another one by that name, with no source, is another skill
|
||||
r = ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, nil))
|
||||
if !slices.Equal(r.Installed, []string{"skill-creator-openai"}) {
|
||||
t.Errorf("openai's: installed %v had %v", r.Installed, r.Had)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreeSkillName(t *testing.T) {
|
||||
sandbox(t)
|
||||
l := &Library{Skills: []*Skill{{Name: "pdf"}, {Name: "pdf-openai"}, {Name: strings.Repeat("a", 64)}}}
|
||||
gh := Source{Kind: "github", Repo: "openai/skills"}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
src Source
|
||||
want string
|
||||
}{
|
||||
{"docx", gh, "docx"},
|
||||
{"pdf", gh, "pdf-openai-skills"},
|
||||
{"pdf", Source{Kind: "git", Repo: "git@gitlab.example.com:team/tools.git"}, "pdf-team"},
|
||||
{"pdf", Source{Kind: "folder", Dir: "/Users/me/work/kit"}, "pdf-work"},
|
||||
{strings.Repeat("a", 64), gh, strings.Repeat("a", 64-len("-openai")) + "-openai"},
|
||||
} {
|
||||
if got := l.freeSkillName(c.name, c.src); got != c.want || !nameRe.MatchString(got) {
|
||||
t.Errorf("%s from %+v: %q, want %q", c.name, c.src, got, c.want)
|
||||
}
|
||||
}
|
||||
// every other name taken: a number
|
||||
l.Skills = append(l.Skills, &Skill{Name: "pdf-openai-skills"}, &Skill{Name: "pdf-skills"})
|
||||
if got := l.freeSkillName("pdf", gh); got != "pdf-openai-2" {
|
||||
t.Errorf("all taken: %q", got)
|
||||
}
|
||||
// a folder in the library's that it doesn't list is taken too
|
||||
write(t, filepath.Join(skillDir("docx"), "SKILL.md"), "mine")
|
||||
if got := l.freeSkillName("docx", gh); got != "docx-openai" {
|
||||
t.Errorf("unlisted folder: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenamedMeta(t *testing.T) {
|
||||
for _, c := range []struct{ in, want string }{
|
||||
{"---\nname: a\ndescription: d\n---\nbody name: a\n", "---\nname: b\ndescription: d\n---\nbody name: a\n"},
|
||||
{"---\r\nname: \"a\"\r\n---\r\n", "---\r\nname: b\r\n---\r\n"},
|
||||
// a nested name isn't the skill's
|
||||
{"---\nmetadata:\n name: a\nname: a\n---\n", "---\nmetadata:\n name: a\nname: b\n---\n"},
|
||||
{"---\ndescription: d\n---\nname: a\n", "---\ndescription: d\n---\nname: a\n"},
|
||||
{"# no front matter\nname: a\n", "# no front matter\nname: a\n"},
|
||||
} {
|
||||
got, _ := renamedMeta(c.in, "b")
|
||||
if got != c.want {
|
||||
t.Errorf("%q: %q, want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
// a SKILL.md that is a link isn't written through
|
||||
dir := t.TempDir()
|
||||
other := filepath.Join(dir, "other.md")
|
||||
write(t, other, "---\nname: a\n---\n")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "s"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(other, filepath.Join(dir, "s", "SKILL.md")); err != nil {
|
||||
t.Skip(err)
|
||||
}
|
||||
if err := nameSkill(filepath.Join(dir, "s"), "b"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, other); got != "---\nname: a\n---\n" {
|
||||
t.Errorf("written through the link: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -52,13 +52,15 @@ func TestInstallSetSkipsOneInTheWay(t *testing.T) {
|
||||
write(t, filepath.Join(skillDir("caveman-commit"), "notes.md"), "keep me")
|
||||
|
||||
r := ok(t)(InstallSkills("owner/caveman", []string{"skills/caveman", "skills/caveman-commit", "skills/caveman-review"}, []string{"claude"}))
|
||||
if !slices.Equal(r.Installed, []string{"caveman-review"}) {
|
||||
// the repository's caveman-commit goes in beside the user's, by a
|
||||
// name of its own (mora1n on Discord)
|
||||
if !slices.Equal(r.Installed, []string{"caveman-commit-owner", "caveman-review"}) {
|
||||
t.Errorf("installed %v", r.Installed)
|
||||
}
|
||||
if !slices.Equal(r.Had, []string{"caveman"}) {
|
||||
t.Errorf("had %v", r.Had)
|
||||
}
|
||||
if !slices.Equal(skipped(r), []string{"caveman-commit"}) || !strings.Contains(r.Skipped[0].Error, "left as it is") {
|
||||
if len(r.Skipped) != 0 {
|
||||
t.Errorf("skipped %+v", r.Skipped)
|
||||
}
|
||||
if got := read(t, filepath.Join(skillDir("caveman"), "SKILL.md")); got != edited {
|
||||
@@ -77,6 +79,9 @@ func TestInstallSetSkipsOneInTheWay(t *testing.T) {
|
||||
if l.skill("caveman-commit") != nil {
|
||||
t.Error("the user's own caveman-commit was listed as the repository's")
|
||||
}
|
||||
if s := l.skill("caveman-commit-owner"); s == nil || s.Called != "caveman-commit" || s.Source.Path != "skills/caveman-commit" {
|
||||
t.Errorf("the repository's caveman-commit is %+v", s)
|
||||
}
|
||||
|
||||
// a set whose every skill is had already is no error
|
||||
r = ok(t)(InstallSkills("owner/caveman", []string{"skills/caveman", "skills/caveman-review"}, []string{"claude"}))
|
||||
@@ -84,7 +89,7 @@ func TestInstallSetSkipsOneInTheWay(t *testing.T) {
|
||||
t.Errorf("again: installed %v had %v", r.Installed, r.Had)
|
||||
}
|
||||
// one skill alone that can't be installed still fails, saying why
|
||||
if _, err := InstallSkills("owner/caveman", []string{"skills/caveman-commit"}, nil); err == nil || !strings.Contains(err.Error(), "left as it is") {
|
||||
if _, err := InstallSkills("owner/caveman", []string{"skills/nowhere"}, nil); err == nil || !strings.Contains(err.Error(), "no skill at") {
|
||||
t.Errorf("alone: %v", err)
|
||||
}
|
||||
// the market's install of one the library has is had, not an error
|
||||
@@ -122,9 +127,10 @@ func TestInstallListsItsOwnLeftoverFolder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A skill by that name the library lists from somewhere else is the
|
||||
// user's: left as it is and said, the rest installed.
|
||||
func TestInstallSetSkipsAnotherByTheSameName(t *testing.T) {
|
||||
// A skill by that name the library lists from somewhere else is another
|
||||
// skill: it stays as it is, and the repository's goes in beside it by a
|
||||
// name of its own (mora1n on Discord).
|
||||
func TestInstallSetPutsAnotherByTheSameNameBeside(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
cavemanRepo(t)
|
||||
src := filepath.Join(h, "src")
|
||||
@@ -132,7 +138,7 @@ func TestInstallSetSkipsAnotherByTheSameName(t *testing.T) {
|
||||
ok(t)(InstallSkills(src, []string{"caveman"}, []string{"claude"}))
|
||||
|
||||
r := ok(t)(InstallSkills("owner/caveman", []string{"skills/caveman", "skills/caveman-review"}, []string{"claude"}))
|
||||
if !slices.Equal(r.Installed, []string{"caveman-review"}) || !slices.Equal(skipped(r), []string{"caveman"}) {
|
||||
if !slices.Equal(r.Installed, []string{"caveman-owner", "caveman-review"}) || len(r.Skipped) != 0 {
|
||||
t.Errorf("installed %v skipped %+v", r.Installed, r.Skipped)
|
||||
}
|
||||
l, _ := load()
|
||||
@@ -142,4 +148,12 @@ func TestInstallSetSkipsAnotherByTheSameName(t *testing.T) {
|
||||
if got := read(t, filepath.Join(skillDir("caveman"), "SKILL.md")); !strings.Contains(got, "Another caveman") {
|
||||
t.Errorf("caveman's SKILL.md is now %q", got)
|
||||
}
|
||||
if got := read(t, filepath.Join(skillDir("caveman-owner"), "SKILL.md")); !strings.HasPrefix(got, "---\nname: caveman-owner\n") || !strings.Contains(got, "Talk like caveman") {
|
||||
t.Errorf("caveman-owner's SKILL.md is %q", got)
|
||||
}
|
||||
for _, n := range []string{"caveman", "caveman-owner"} {
|
||||
if !ours(filepath.Join(h, ".claude/skills", n), n) {
|
||||
t.Errorf("claude wasn't given %s", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -491,7 +491,7 @@ func checkSkill(s *Skill, f *fetcher, asked map[string]*commit, limited *atomic.
|
||||
if have == "" {
|
||||
have = hashDir(skillDir(s.Name)) // installed before magpie kept it
|
||||
}
|
||||
if theirs := hashDir(from); theirs == "" || theirs != have {
|
||||
if theirs := s.fetchedHash(from); theirs == "" || theirs != have {
|
||||
c.Status, c.URL = "update", history
|
||||
return c, "", ""
|
||||
}
|
||||
|
||||
@@ -445,7 +445,7 @@ func checkGitSkill(s *Skill, f *fetcher, asked map[string]string) (SkillCheck, s
|
||||
if have == "" {
|
||||
have = hashDir(skillDir(s.Name))
|
||||
}
|
||||
if theirs := hashDir(from); theirs == "" || theirs != have {
|
||||
if theirs := s.fetchedHash(from); theirs == "" || theirs != have {
|
||||
c.Status = "update"
|
||||
return c, "", ""
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
slash "path" // a repository's paths, always with /
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -286,7 +287,10 @@ func findNew(l *Library, repo, ref string, f *fetcher, limited *atomic.Pointer[e
|
||||
found = []NewSkill{}
|
||||
from := (&Source{Kind: "github", Repo: repo, Ref: ref, Path: base}).String()
|
||||
for _, c := range candidates(tmp, base, srcFolder(Source{Kind: "github", Repo: repo})) {
|
||||
if known[c.Path] || l.skill(c.Name) != nil || checkName("skill", c.Name) != nil {
|
||||
// another skill by its name, from elsewhere, doesn't make it the
|
||||
// library's: it would go in beside that one (skill_beside.go)
|
||||
if known[c.Path] || checkName("skill", c.Name) != nil ||
|
||||
l.hadSkill(Source{Kind: "github", Repo: repo, Ref: ref}, c, filepath.Join(tmp, filepath.FromSlash(c.Path))) != nil {
|
||||
continue
|
||||
}
|
||||
id := (&Source{Kind: "github", Repo: repo, Ref: ref, Path: c.Path}).String()
|
||||
@@ -303,7 +307,7 @@ func newSkills(l *Library) []NewSkill {
|
||||
out := []NewSkill{}
|
||||
for k, list := range newFound.m {
|
||||
for _, n := range list {
|
||||
if repoKey(n.Repo, n.Ref) != k || len(l.fromRepo(n.Repo, n.Ref)) == 0 || l.skill(n.Name) != nil ||
|
||||
if repoKey(n.Repo, n.Ref) != k || len(l.fromRepo(n.Repo, n.Ref)) == 0 ||
|
||||
slices.Contains(l.SeenSkills[k], n.Path) {
|
||||
continue
|
||||
}
|
||||
|
||||
+79
-22
@@ -54,6 +54,10 @@ type Skill struct {
|
||||
// what a sync carries and stays as the other computers have it; this
|
||||
// is never carried (Collect leaves it out) and a sync keeps it (Put).
|
||||
OffHere bool `json:"offHere,omitempty"`
|
||||
// Called is the name its SKILL.md gives, when the library keeps it
|
||||
// under another because a skill from elsewhere had that name first
|
||||
// (skill_beside.go). Its SKILL.md is named as the library names it.
|
||||
Called string `json:"called,omitempty"`
|
||||
}
|
||||
|
||||
// gives reports whether the agent id gets the skill on this computer: it
|
||||
@@ -717,7 +721,10 @@ type Candidate struct {
|
||||
Path string `json:"path"` // in the repository or the folder; "" for its root
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Have bool `json:"have"` // the library has a skill by that name
|
||||
Have bool `json:"have"` // the library has this skill (from here, or the very same files)
|
||||
// As is the name the library has it by, or would add it by, when
|
||||
// that isn't Name: another skill from elsewhere has that name
|
||||
As string `json:"as,omitempty"`
|
||||
}
|
||||
|
||||
// Probe is what was found at a source.
|
||||
@@ -1168,7 +1175,21 @@ func probeSkills(input, name string) (*Probe, error) {
|
||||
}
|
||||
if l, err := load(); err == nil {
|
||||
for i, c := range p.Candidates {
|
||||
p.Candidates[i].Have = l.skill(c.Name) != nil
|
||||
from := filepath.Join(p.root, filepath.FromSlash(c.Path))
|
||||
if s := l.hadSkill(p.src, c, from); s != nil {
|
||||
p.Candidates[i].Have = true
|
||||
if s.Name != c.Name {
|
||||
p.Candidates[i].As = s.Name
|
||||
}
|
||||
} else if l.skill(c.Name) != nil {
|
||||
if p.src.remote() {
|
||||
p.Candidates[i].As = l.freeSkillName(c.Name, p.src)
|
||||
} else {
|
||||
// a folder of the user's isn't added beside it
|
||||
// (installFrom): shown as had by that name, as before
|
||||
p.Candidates[i].Have = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
noteFrom(p.src, p.root, p.Candidates)
|
||||
@@ -1285,47 +1306,66 @@ func installFrom(l *Library, p *Probe, paths, agents []string, shown bool, in *i
|
||||
from := filepath.Join(p.root, filepath.FromSlash(c.Path))
|
||||
src := p.src
|
||||
// the same skill from the same place is had already, whatever
|
||||
// the user has changed in it since; another by that name is theirs
|
||||
if s := l.skill(c.Name); s != nil {
|
||||
if fromSameSource(s, src, c.Path, from) {
|
||||
in.had = append(in.had, c.Name)
|
||||
} else {
|
||||
in.skip(c.Name, fmt.Errorf("the library already has another skill called %s, left as it is", c.Name))
|
||||
}
|
||||
// the user has changed in it since and whatever it is called
|
||||
// here; another by that name goes in beside it, by a name of its
|
||||
// own (skill_beside.go)
|
||||
if s := l.hadSkill(src, c, from); s != nil {
|
||||
in.had = append(in.had, s.Name)
|
||||
continue
|
||||
}
|
||||
dir := skillDir(c.Name)
|
||||
name := c.Name
|
||||
if l.skill(name) != nil {
|
||||
// the user's own folder is linked as it is, and its SKILL.md
|
||||
// can't be named apart from the other: an agent would see two
|
||||
// by one name
|
||||
if !src.remote() {
|
||||
in.skip(c.Name, fmt.Errorf("the library already has another skill called %s, left as it is: give this one another name in its SKILL.md to add it beside it", c.Name))
|
||||
continue
|
||||
}
|
||||
name = l.freeSkillName(c.Name, src)
|
||||
}
|
||||
dir := skillDir(name)
|
||||
// something by that name in the library's folder that the
|
||||
// library doesn't list is never written into: that would mix
|
||||
// two skills' files, and a failed copy would take it away. One
|
||||
// holding exactly this skill's files (an install that stopped
|
||||
// before the library was saved left it) is listed where it is.
|
||||
// before the library was saved left it) is listed where it is;
|
||||
// another is the user's, and this one goes in beside it.
|
||||
listed := false
|
||||
if _, err := os.Lstat(dir); err == nil {
|
||||
switch {
|
||||
case src.Kind == "folder" && linked(dir) && realDir(dir) == realDir(from):
|
||||
src.Dir = from
|
||||
src.Dir, listed = from, true
|
||||
case src.remote() && !linked(dir) && sameTree(dir, from):
|
||||
src.Path = c.Path
|
||||
src.Path, listed = c.Path, true
|
||||
case src.Kind == "folder" && realDir(from) == realDir(dir):
|
||||
// a folder already in the library's own (#595): linked to
|
||||
// itself it fails, and copied onto itself every file of it
|
||||
// was emptied
|
||||
in.skip(c.Name, fmt.Errorf("%s is in the library's folder already: bring it in from the skills found there", c.Name))
|
||||
continue
|
||||
default:
|
||||
case !src.remote():
|
||||
in.skip(c.Name, fmt.Errorf("the library's folder already has a %s that isn't this one (%s), left as it is: bring it in from the skills found there to use it", c.Name, dir))
|
||||
continue
|
||||
default:
|
||||
name = l.freeSkillName(c.Name, src)
|
||||
dir = skillDir(name)
|
||||
}
|
||||
in.had = append(in.had, c.Name)
|
||||
} else if !errors.Is(err, fs.ErrNotExist) {
|
||||
// can't tell what is there: left alone
|
||||
in.skip(c.Name, err)
|
||||
continue
|
||||
} else if src.Kind == "folder" {
|
||||
}
|
||||
switch {
|
||||
case listed:
|
||||
in.had = append(in.had, name)
|
||||
case src.Kind == "folder":
|
||||
// the user's folder, linked: its files stay as they are, its
|
||||
// SKILL.md's name too
|
||||
src.Dir = from
|
||||
if err := dirLink(from, dir); err != nil {
|
||||
if errors.Is(err, fs.ErrExist) {
|
||||
in.skip(c.Name, fmt.Errorf("the library's folder already has a %s in it (%s)", c.Name, dir))
|
||||
in.skip(c.Name, fmt.Errorf("the library's folder already has a %s in it (%s)", name, dir))
|
||||
continue
|
||||
}
|
||||
if err := copyDir(from, dir); err != nil {
|
||||
@@ -1334,17 +1374,27 @@ func installFrom(l *Library, p *Probe, paths, agents []string, shown bool, in *i
|
||||
continue
|
||||
}
|
||||
}
|
||||
in.added = append(in.added, c.Name)
|
||||
} else {
|
||||
in.added = append(in.added, name)
|
||||
default:
|
||||
src.Path = c.Path
|
||||
if err := copyDir(from, dir); err != nil {
|
||||
os.RemoveAll(dir)
|
||||
in.skip(c.Name, err)
|
||||
continue
|
||||
}
|
||||
in.added = append(in.added, c.Name)
|
||||
if name != c.Name {
|
||||
if err := nameSkill(dir, name); err != nil {
|
||||
os.RemoveAll(dir)
|
||||
in.skip(c.Name, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
in.added = append(in.added, name)
|
||||
}
|
||||
sk := &Skill{Name: name, Source: &src, Agents: slices.Clone(agents)}
|
||||
if name != c.Name {
|
||||
sk.Called = c.Name
|
||||
}
|
||||
sk := &Skill{Name: c.Name, Source: &src, Agents: slices.Clone(agents)}
|
||||
if src.Kind == "git" {
|
||||
sk.Hash, sk.Own, sk.Commit = hashDir(dir), ownHash(dir), gitHeadOf(p.root)
|
||||
}
|
||||
@@ -1359,7 +1409,7 @@ func installFrom(l *Library, p *Probe, paths, agents []string, shown bool, in *i
|
||||
}
|
||||
}
|
||||
}
|
||||
forgetCheck(c.Name)
|
||||
forgetCheck(name)
|
||||
l.Skills = append(l.Skills, sk)
|
||||
}
|
||||
return nil
|
||||
@@ -1655,6 +1705,13 @@ func (up *skillUpdate) apply(l *Library, targets []*Target) error {
|
||||
os.RemoveAll(next)
|
||||
return err
|
||||
}
|
||||
// one kept under another name than its SKILL.md gives stays named so
|
||||
if s := l.skill(name); s != nil && s.Called != "" {
|
||||
if err := nameSkill(next, name); err != nil {
|
||||
os.RemoveAll(next)
|
||||
return err
|
||||
}
|
||||
}
|
||||
hash, own := hashDir(next), ownHash(next)
|
||||
if fi, err := os.Lstat(skillDir(name)); err == nil && linkEntry(fi) {
|
||||
// a link to CC Switch's folder: only the link goes
|
||||
|
||||
@@ -450,8 +450,14 @@ func (m *model) openProbe(p *library.Probe) {
|
||||
var items []agent.Option
|
||||
for _, c := range p.Candidates {
|
||||
note := c.Description
|
||||
if c.Have {
|
||||
switch {
|
||||
case c.Have && c.As != "":
|
||||
note = "in the library as " + c.As + " · " + note
|
||||
case c.Have:
|
||||
note = "in the library already · " + note
|
||||
case c.As != "":
|
||||
// another skill from elsewhere has its name
|
||||
note = "adds as " + c.As + " · " + note
|
||||
}
|
||||
v := c.Path
|
||||
if v == "" {
|
||||
|
||||
Reference in New Issue
Block a user