library: a check offers the skills a git repository or folder added since
refeccd (Discord): skills added from a repository or a folder only had
the existing ones updated; ones the source added later never reached
the library. GitHub sources already had this (findNew); git repositories
on other hosts and folders on this machine did not, and a folder install
didn't even remember the folder it came from.
- git: when ls-remote gives the ref a head, findNewGit reads the fetch
the check shares and offers skills under newBase that the library
doesn't have and hasn't offered. Once per head while its finds are
kept (gitLooked). The address is kept as the library has it (.git and
all), so an Add lands on the same repo@ref key.
- folder: an install from a folder now records "folder:<path>" in
SeenSkills (offered), which is how the library knows where to look
again. findNewFolder reads it as Add from folder does and skips a
folder the library links, has offered, or has a skill by that name of.
- A check never adds or writes a skill: each is offered with Add and
Ignore, as GitHub's are. A skill the user removed or set aside isn't
offered again, and a folder edit is never written over.
- View.CheckFolders shows Check for updates with only linked skills and
makes checkDue true; its title says it looks in folders too. A find's
row shows its folder (pathLink) or git address. The old head note
("on GitHub beside skills you have…") is gone; the new title is in all
five languages.
- docs/subsystems/library.md says so.
Old data: a folder added before this kept no folder: key, so it is
looked in only after the user adds from that folder once more (the
picker then lists the new skills, and the install records the folder).
Run (mote, at fcc0daa23 plus this change):
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...; go build ./... and the CGO_ENABLED=0
nogui build: ok.
- go test ./internal/library: ok. go test ./internal/gui: ok except
TestPanelHiddenByWailsMarksPageOutOfSight (#5322) and
TestMainWindowCorners (#3949), both red the same on origin/main
19c45d5d4 run alone.
- GUI, both engines: every library-*, skill-*, github-token,
plugin-check-*, plugin-git, i18n-keys, i18n-en-no-cjk, gui-de,
gui-ja, gui-zh-tw file: 1526 of 1529. Reds: i18n-keys "a plugin's
words are its own" (#5332, #5426, #5482, red on origin/main) and
skill-where "the switch scrolls nothing", flaky on origin/main
19c45d5d4 too (1, 0 and 2 reds in three runs; noted on #5154).
skill-new-in-repo's new folder case runs en and zh at 860 and 440px.
At the pushed head (rebased on origin/main 94d4866b5), again on mote:
go vet of internal/library and internal/gui, go test ./internal/library
ok, internal/gui's Budget/Size tests ok, skill-new-in-repo 4 of 4 and
i18n-keys with only the known red above.
Broken by hand: with the git branch of CheckSkills and lookInFolders
off, TestCheckSkillsFindsNewInGitRepo and TestCheckSkillsFindsNewInFolder
fail ("new: []"); with offered() not recording the picker's shown
skills, both fail offering what the picker showed ([review], [xlsx]).
Not tried: a real GitLab or intranet repository (the test uses a local
file:// bare repo with the real git); the CLI and TUI, which don't list
new skills; the Windows and Linux boxes.
Reported-by: refeccd (Discord)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3614,9 +3614,11 @@ magpie.plugin("library", (ctx) => {
|
||||
const kept = lib.skills.filter((s) => s.shared);
|
||||
if (kept.length > 1) rh.append(handOverButton(kept));
|
||||
if (lib.skills.length > 1) rh.append(removeEveryButton("skills"));
|
||||
if (lib.skills.some((s) => s.kind === "github" || s.kind === "git")) {
|
||||
if (lib.checkFolders || lib.skills.some((s) => s.kind === "github" || s.kind === "git")) {
|
||||
const c = button(checking ? t("Checking…") : t("Check for updates"), "lib-updall", () => checkSkills());
|
||||
c.title = lib.skills.some((s) => s.kind === "git")
|
||||
c.title = lib.checkFolders
|
||||
? t("Look in the repositories and folders your skills came from for changes and new skills")
|
||||
: lib.skills.some((s) => s.kind === "git")
|
||||
? t("Ask the repositories your skills came from which skills changed since they were installed")
|
||||
: t("Ask GitHub which skills changed since they were installed");
|
||||
c.disabled = checking;
|
||||
@@ -3718,7 +3720,7 @@ magpie.plugin("library", (ctx) => {
|
||||
function renderNewSkills(body) {
|
||||
const list = lib.newSkills;
|
||||
const rh = el("div", "row-head lib-newhead");
|
||||
rh.append(el("span", "label", t("Also in their repositories")), el("span", "grow"), el("span", "note", t("on GitHub beside skills you have, not in the library yet")));
|
||||
rh.append(el("span", "label", t("Also in their repositories")), el("span", "grow"));
|
||||
if (list.length > 1) {
|
||||
const ids = list.map((n) => n.id);
|
||||
const ign = button(t("Ignore all"), "lib-updall", () => change("skills/ignore-new", { names: ids }, t("{n} skills set aside", { n: ids.length })));
|
||||
@@ -3747,10 +3749,20 @@ magpie.plugin("library", (ctx) => {
|
||||
sub.title = n.description || "";
|
||||
who.append(sub);
|
||||
const src = el("div", "lib-src");
|
||||
const a = el("a", "lib-srclink", n.repo + "/" + n.path);
|
||||
a.href = n.id;
|
||||
a.onclick = (e) => { e.preventDefault(); e.stopPropagation(); browse(n.id); };
|
||||
src.append(a);
|
||||
if (n.kind === "folder") {
|
||||
// a folder the user added skills from: shown where it is
|
||||
src.append(pathLink(n.id));
|
||||
} else if (n.kind === "git" && !/^https?:\/\//.test(n.id)) {
|
||||
// an SSH address (git@host:path) or a file:// one: no page to open
|
||||
const a = el("span", "lib-srclink", n.id);
|
||||
a.title = n.id;
|
||||
src.append(a);
|
||||
} else {
|
||||
const a = el("a", "lib-srclink", n.kind === "git" ? n.id.replace(/^https?:\/\//, "") : n.repo + "/" + n.path);
|
||||
a.href = n.id;
|
||||
a.onclick = (e) => { e.preventDefault(); e.stopPropagation(); browse(n.id); };
|
||||
src.append(a);
|
||||
}
|
||||
who.append(src);
|
||||
const have = el("div", "lib-have");
|
||||
// one find without its list of agents doesn't take the page down (#1217)
|
||||
|
||||
@@ -404,7 +404,7 @@ magpie.strings({
|
||||
"{agents} has another server by this name; bringing this one in leaves that one as it is": "{agents} 有另一个同名服务器;导入这个不会改动那个",
|
||||
"{name} is in the library now": "{name} 已加入资源库",
|
||||
"Also in their repositories": "同一仓库里的其他技能",
|
||||
"on GitHub beside skills you have, not in the library yet": "GitHub 上与你已有技能同处一处,尚未加入资源库",
|
||||
"Look in the repositories and folders your skills came from for changes and new skills": "在你的技能来自的仓库和文件夹里查找更新和新技能",
|
||||
"Ignore all": "全部忽略",
|
||||
"{n} skills set aside": "已忽略 {n} 个技能",
|
||||
"A check for updates won't offer these again": "检查更新时不再提示这些技能",
|
||||
@@ -1194,7 +1194,7 @@ magpie.strings({
|
||||
"{agents} has another server by this name; bringing this one in leaves that one as it is": "{agents} 有另一個同名伺服器;匯入這個不會改動那個",
|
||||
"{name} is in the library now": "{name} 已加入資源庫",
|
||||
"Also in their repositories": "同一倉庫裡的其他技能",
|
||||
"on GitHub beside skills you have, not in the library yet": "GitHub 上與你已有技能同處一處,尚未加入資源庫",
|
||||
"Look in the repositories and folders your skills came from for changes and new skills": "在你的技能來自的倉庫和資料夾裡查找更新和新技能",
|
||||
"Ignore all": "全部忽略",
|
||||
"{n} skills set aside": "已忽略 {n} 個技能",
|
||||
"A check for updates won't offer these again": "檢查更新時不再提示這些技能",
|
||||
@@ -1979,7 +1979,7 @@ magpie.strings({
|
||||
"{agents} has another server by this name; bringing this one in leaves that one as it is": "{agents} に同じ名前の別のサーバーがあります。これを取り込んでもそちらはそのままです",
|
||||
"{name} is in the library now": "{name} をライブラリに追加しました",
|
||||
"Also in their repositories": "同じリポジトリの他のスキル",
|
||||
"on GitHub beside skills you have, not in the library yet": "GitHub で手持ちのスキルと同じ場所にあり、まだライブラリにありません",
|
||||
"Look in the repositories and folders your skills came from for changes and new skills": "スキルの元のリポジトリとフォルダで、更新と新しいスキルを探します",
|
||||
"Ignore all": "すべて無視",
|
||||
"{n} skills set aside": "{n} 個のスキルを無視しました",
|
||||
"A check for updates won't offer these again": "更新の確認でこれらを再び表示しません",
|
||||
@@ -2767,7 +2767,7 @@ magpie.strings({
|
||||
"{agents} has another server by this name; bringing this one in leaves that one as it is": "{agents} hat einen anderen Server mit diesem Namen; das Übernehmen dieses Servers lässt jenen unverändert",
|
||||
"{name} is in the library now": "{name} ist jetzt in der Bibliothek",
|
||||
"Also in their repositories": "Auch in ihren Repositorys",
|
||||
"on GitHub beside skills you have, not in the library yet": "auf GitHub neben deinen Skills, noch nicht in der Bibliothek",
|
||||
"Look in the repositories and folders your skills came from for changes and new skills": "In den Repositorys und Ordnern deiner Skills nach Änderungen und neuen Skills suchen",
|
||||
"Ignore all": "Alle ignorieren",
|
||||
"{n} skills set aside": "{n} Skills ignoriert",
|
||||
"A check for updates won't offer these again": "Die Suche nach Updates bietet diese nicht mehr an",
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
// Add all and Ignore all are there for more than one. The row's buttons fit
|
||||
// at narrow widths. In English and Chinese, in Chromium and WebKit. No
|
||||
// backend: the API is faked here.
|
||||
// refeccd (Discord): skills added from a folder never got the ones put
|
||||
// there later. With only those, Check for updates is there, says it
|
||||
// looks in folders too, and a find shows the folder it is in.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
@@ -24,6 +27,7 @@ const lib = (st) => ({
|
||||
agents: [agent("claude", "Claude Code"), agent("codex", "Codex"), agent("pi", "Pi")],
|
||||
instructions: { agents: [] }, servers: [], skills: st.skills, foundServers: [], projects: [], foundSkills: [],
|
||||
newSkills: st.checked ? st.newSkills : [],
|
||||
checkFolders: !!st.checkFolders,
|
||||
});
|
||||
|
||||
function server(lang, posts, st) {
|
||||
@@ -132,3 +136,55 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
const ROOT = `${HOME}/src/skills`;
|
||||
const linked = (name) => ({ name, description: name + " skill", kind: "folder", source: `${ROOT}/${name}`, agents: ["claude"] });
|
||||
const inFolder = (name) => ({ id: `${ROOT}/${name}`, name, description: name, kind: "folder", repo: ROOT, path: name, from: ROOT, agents: ["claude"] });
|
||||
const folderWords = {
|
||||
en: { check: "Check for updates", tip: "Look in the repositories and folders your skills came from for changes and new skills", said: "Every skill is up to date · 1 more skill in their repositories", add: "Add", added: "slides is in the library now" },
|
||||
zh: { check: "检查更新", tip: "在你的技能来自的仓库和文件夹里查找更新和新技能", said: "所有技能都是最新的 · 同一仓库里还有 1 个技能", add: "添加", added: "slides 已加入资源库" },
|
||||
};
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
test(engine + ": a check offers the skills a folder has added since", async (t) => {
|
||||
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();
|
||||
});
|
||||
for (const width of [860, 440]) for (const lang of ["en", "zh"]) {
|
||||
await t.test(`${lang} at ${width}px`, async () => {
|
||||
const w = folderWords[lang], posts = [];
|
||||
const st = { checked: false, checkFolders: true, skills: [linked("pdf")], newSkills: [inFolder("slides")] };
|
||||
const ctx = await browser.newContext({ viewport: { width, height: 1300 } });
|
||||
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, st));
|
||||
await page.goto("http://magpie.test/");
|
||||
await page.locator('button[data-view="library"]').click();
|
||||
await page.locator("#view-library .lib-body:not(.lib-skel) .lib-row").first().waitFor();
|
||||
const v = page.locator("#view-library");
|
||||
const check = v.locator(".row-head button.lib-updall", { hasText: w.check });
|
||||
assert.equal(await check.getAttribute("title"), w.tip);
|
||||
await check.click();
|
||||
await v.locator(".lib-newskill").first().waitFor();
|
||||
assert.equal((await page.locator("#status").textContent()).trim(), w.said);
|
||||
const row = v.locator(".lib-newskill");
|
||||
assert.equal((await row.locator(".lib-path").textContent()).trim(), "~/src/skills/slides");
|
||||
assert.equal(await row.locator("a").count(), 0, "a folder is no web link");
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), true, "no sideways scroll");
|
||||
await press(page, row.locator("button.action", { hasText: w.add }));
|
||||
await page.waitForTimeout(300);
|
||||
assert.deepEqual(posts, [{ what: "add-new", names: [`${ROOT}/slides`] }]);
|
||||
assert.equal((await page.locator("#status").textContent()).trim(), w.added);
|
||||
assert.equal(await v.locator(".lib-newskill").count(), 0);
|
||||
await ctx.close();
|
||||
});
|
||||
}
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,8 +70,9 @@ type Library struct {
|
||||
// BridgesOn are the Remote magpies (provider ids) the user asked to
|
||||
// give their router a server, in the editor's switch (bridge_mcp.go)
|
||||
BridgesOn []string `json:"bridgesOn,omitempty"`
|
||||
// SeenSkills are, by repository and ref ("owner/repo@ref"), the
|
||||
// folders of skills there the page has offered: picked or not at
|
||||
// SeenSkills are, by repository and ref ("owner/repo@ref", or a git
|
||||
// address and ref), or by a folder the user added ("folder:<path>"),
|
||||
// the folders of skills there the page has offered: picked or not at
|
||||
// install, or set aside after a check. A check offers only the others
|
||||
// as new (skillnew.go).
|
||||
SeenSkills map[string][]string `json:"seenSkills,omitempty"`
|
||||
|
||||
@@ -406,7 +406,7 @@ func noteFrom(src Source, root string, cands []Candidate) int {
|
||||
if dir := skillDir(s.Name); s.Source == nil && !linked(dir) && sameTree(dir, filepath.Join(root, filepath.FromSlash(c.Path))) {
|
||||
s.Source = &Source{Kind: "github", Repo: src.Repo, Ref: src.Ref, Path: c.Path}
|
||||
s.Hash, s.Own, s.Commit, s.From = hashDir(dir), ownHash(dir), "", ""
|
||||
l.seenAt(src.Repo, src.Ref, c.Path)
|
||||
l.seenAt(repoKey(src.Repo, src.Ref), c.Path)
|
||||
forgetCheck(s.Name)
|
||||
bound++
|
||||
}
|
||||
|
||||
@@ -95,10 +95,10 @@ var checkedAt atomic.Int64
|
||||
const checkEvery = 12 * time.Hour
|
||||
|
||||
// checkDue is whether the page should check the library's skills from
|
||||
// GitHub for updates: none checked since magpie started, or not for
|
||||
// checkEvery.
|
||||
// GitHub or git for updates, and the folders the user added skills from
|
||||
// for new ones: none checked since magpie started, or not for checkEvery.
|
||||
func checkDue(l *Library) bool {
|
||||
if !slices.ContainsFunc(l.Skills, func(s *Skill) bool { return s.Source.remote() }) {
|
||||
if !slices.ContainsFunc(l.Skills, func(s *Skill) bool { return s.Source.remote() }) && !l.addedFromFolders() {
|
||||
return false
|
||||
}
|
||||
at := checkedAt.Load()
|
||||
@@ -314,6 +314,22 @@ func firstLine(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// gitLooked is, by repoKey, the commit a git repository was last read at
|
||||
// for new skills: one at the same commit has none since, and isn't
|
||||
// fetched again for them.
|
||||
var gitLooked sync.Map
|
||||
|
||||
func gitLookedAt(repo, ref, head string) bool {
|
||||
h, ok := gitLooked.Load(repoKey(repo, ref))
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
newFound.Lock()
|
||||
_, kept := newFound.m[repoKey(repo, ref)]
|
||||
newFound.Unlock()
|
||||
return kept && strings.EqualFold(h.(string), head)
|
||||
}
|
||||
|
||||
// CheckSkills asks GitHub, for every skill installed from there, for the
|
||||
// last commit to touch its folder, and tells whether it's newer than what
|
||||
// the library has: by the commit a skill is known to be at, or, while
|
||||
@@ -321,7 +337,9 @@ func firstLine(s string) string {
|
||||
// folder's files with the skill's. A skill GitHub couldn't be asked about
|
||||
// is unknown, and the others are checked all the same; once GitHub limits
|
||||
// requests, the rest aren't asked about. A skill from another git
|
||||
// repository is checked with the user's git (checkGitSkill).
|
||||
// repository is checked with the user's git (checkGitSkill). Each
|
||||
// repository, and each folder the user added skills from, is also looked
|
||||
// in for skills added beside them since (skillnew.go).
|
||||
func CheckSkills() ([]SkillCheck, error) {
|
||||
mu.Lock()
|
||||
l, err := load()
|
||||
@@ -373,19 +391,30 @@ func CheckSkills() ([]SkillCheck, error) {
|
||||
}
|
||||
}
|
||||
// and what the repository has added beside them since: GitHub's
|
||||
// list of its files says, which another host has no API for
|
||||
// list of its files says; another host's repository is read
|
||||
// from its fetch, once for each commit its ref is at
|
||||
for _, ref := range refs {
|
||||
if skills[0].Source.Kind != "github" {
|
||||
break
|
||||
repo := skills[0].Source.Repo
|
||||
var found []NewSkill
|
||||
var ok bool
|
||||
switch head := heads[ref]; {
|
||||
case skills[0].Source.Kind == "github":
|
||||
found, ok = findNew(l, repo, ref, f, &limited)
|
||||
case head != "" && !gitLookedAt(repo, ref, head):
|
||||
if found, ok = findNewGit(l, repo, ref, f); ok {
|
||||
gitLooked.Store(repoKey(repo, ref), head)
|
||||
}
|
||||
}
|
||||
if found, ok := findNew(l, skills[0].Source.Repo, ref, f, &limited); ok {
|
||||
if ok {
|
||||
newFound.Lock()
|
||||
newFound.m[repoKey(skills[0].Source.Repo, ref)] = found
|
||||
newFound.m[repoKey(repo, ref)] = found
|
||||
newFound.Unlock()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
// the folders the user added skills from, for ones put there since
|
||||
lookInFolders(l)
|
||||
wg.Wait()
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
if len(known) > 0 {
|
||||
|
||||
+229
-34
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
slash "path" // a repository's paths, always with /
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -16,19 +17,24 @@ import (
|
||||
|
||||
// ---- skills a repository has added since --------------------------------
|
||||
|
||||
// NewSkill is a skill a check for updates found in a GitHub repository the
|
||||
// library has skills from, beside them, that the library doesn't have and
|
||||
// that the page hasn't offered before: one the repository added since its
|
||||
// skills were installed. The page offers to add it, or to set it aside.
|
||||
// NewSkill is a skill a check for updates found where the library has
|
||||
// skills from, beside them, that the library doesn't have and that the
|
||||
// page hasn't offered before: one a GitHub or git repository, or a folder
|
||||
// on this machine, added since its skills were installed (refeccd on
|
||||
// Discord). The page offers to add it, or to set it aside.
|
||||
type NewSkill struct {
|
||||
// ID is the skill's folder on GitHub, what the page adds it from and
|
||||
// sets it aside by
|
||||
// ID is the skill's folder (on GitHub, in the git repository, or on
|
||||
// this machine), what the page adds it from and sets it aside by
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Repo string `json:"repo"`
|
||||
Ref string `json:"ref,omitempty"`
|
||||
Path string `json:"path"`
|
||||
// Kind is where it is: github, git or folder
|
||||
Kind string `json:"kind"`
|
||||
// Repo is the repository (owner/repo, or the address git fetches),
|
||||
// or the folder the user added
|
||||
Repo string `json:"repo"`
|
||||
Ref string `json:"ref,omitempty"`
|
||||
Path string `json:"path"`
|
||||
// From is the repository's folder the check found it in, what Add
|
||||
// installs it from
|
||||
From string `json:"from"`
|
||||
@@ -45,16 +51,32 @@ var newFound = struct {
|
||||
}{m: map[string][]NewSkill{}}
|
||||
|
||||
// repoKey is a repository at a ref, as SeenSkills and newFound keep it.
|
||||
// A git repository's address never reads as GitHub's owner/repo.
|
||||
func repoKey(repo, ref string) string { return repo + "@" + ref }
|
||||
|
||||
// folderKey is a folder the user added skills from, as SeenSkills and
|
||||
// newFound keep it: its key in SeenSkills is how the library knows which
|
||||
// folder its linked skills were added from, to look in it again.
|
||||
func folderKey(root string) string { return foldersKey + root }
|
||||
|
||||
const foldersKey = "folder:"
|
||||
|
||||
// placeKey is where a found skill is, as SeenSkills and newFound keep it.
|
||||
func (n *NewSkill) placeKey() string {
|
||||
if n.Kind == "folder" {
|
||||
return folderKey(n.Repo)
|
||||
}
|
||||
return repoKey(n.Repo, n.Ref)
|
||||
}
|
||||
|
||||
// seenAt records that the page offered the skills at those paths of a
|
||||
// repository: picked or not at install, or set aside after a check. A
|
||||
// check doesn't offer them as new again.
|
||||
func (l *Library) seenAt(repo, ref string, paths ...string) {
|
||||
// repository or folder (by repoKey or folderKey): picked or not at
|
||||
// install, or set aside after a check. A check doesn't offer them as new
|
||||
// again.
|
||||
func (l *Library) seenAt(k string, paths ...string) {
|
||||
if l.SeenSkills == nil {
|
||||
l.SeenSkills = map[string][]string{}
|
||||
}
|
||||
k := repoKey(repo, ref)
|
||||
for _, p := range paths {
|
||||
if !slices.Contains(l.SeenSkills[k], p) {
|
||||
l.SeenSkills[k] = append(l.SeenSkills[k], p)
|
||||
@@ -63,17 +85,71 @@ func (l *Library) seenAt(repo, ref string, paths ...string) {
|
||||
sort.Strings(l.SeenSkills[k])
|
||||
}
|
||||
|
||||
// fromRepo are the library's skills installed from a repository at a ref.
|
||||
func (l *Library) fromRepo(repo, ref string) []*Skill {
|
||||
// fromRepo are the library's skills installed from a GitHub repository
|
||||
// at a ref.
|
||||
func (l *Library) fromRepo(repo, ref string) []*Skill { return l.fromPlace("github", repo, ref) }
|
||||
|
||||
// fromPlace are the library's skills installed from a GitHub or git
|
||||
// repository at a ref, or linked from a folder under the one the user
|
||||
// added (repo, for kind folder).
|
||||
func (l *Library) fromPlace(kind, repo, ref string) []*Skill {
|
||||
var out []*Skill
|
||||
for _, s := range l.Skills {
|
||||
if s.Source != nil && s.Source.Kind == "github" && s.Source.Repo == repo && s.Source.Ref == ref {
|
||||
out = append(out, s)
|
||||
if s.Source == nil || s.Source.Kind != kind {
|
||||
continue
|
||||
}
|
||||
switch kind {
|
||||
case "github":
|
||||
if s.Source.Repo == repo && s.Source.Ref == ref {
|
||||
out = append(out, s)
|
||||
}
|
||||
case "git":
|
||||
if sameGitRepo(s.Source.Repo, repo) && s.Source.Ref == ref {
|
||||
out = append(out, s)
|
||||
}
|
||||
case "folder":
|
||||
if _, in := relUnder(repo, s.Source.Dir); in {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// relUnder is dir's path from root, with /, when dir is root or in it:
|
||||
// as the paths are, or with links followed (/tmp is /private/tmp).
|
||||
func relUnder(root, dir string) (string, bool) {
|
||||
if root == "" || dir == "" {
|
||||
return "", false
|
||||
}
|
||||
for _, p := range [][2]string{{filepath.Clean(root), filepath.Clean(dir)}, {realDir(root), realDir(dir)}} {
|
||||
rel, err := filepath.Rel(p[0], p[1])
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || filepath.IsAbs(rel) {
|
||||
continue
|
||||
}
|
||||
if rel == "." {
|
||||
rel = ""
|
||||
}
|
||||
return filepath.ToSlash(rel), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// agentsOf are the agents that have any of those skills, sorted: none is
|
||||
// [], which the page reads as a list (#1217).
|
||||
func agentsOf(skills []*Skill) []string {
|
||||
agents := []string{}
|
||||
for _, s := range skills {
|
||||
for _, a := range s.Agents {
|
||||
if !slices.Contains(agents, a) {
|
||||
agents = append(agents, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(agents)
|
||||
return agents
|
||||
}
|
||||
|
||||
// newBase is the folder of a repository a check looks for new skills in:
|
||||
// the deepest one that holds every installed skill's folder. Skills
|
||||
// installed from skills/engineering are looked for beside them, not in
|
||||
@@ -296,17 +372,7 @@ func findNew(l *Library, repo, ref string, f *fetcher, limited *atomic.Pointer[e
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
// none when the repository's skills are on no agent: [], which the
|
||||
// page reads as a list (#1217)
|
||||
agents := []string{}
|
||||
for _, s := range skills {
|
||||
for _, a := range s.Agents {
|
||||
if !slices.Contains(agents, a) {
|
||||
agents = append(agents, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(agents)
|
||||
agents := agentsOf(skills)
|
||||
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})) {
|
||||
@@ -317,11 +383,141 @@ func findNew(l *Library, repo, ref string, f *fetcher, limited *atomic.Pointer[e
|
||||
continue
|
||||
}
|
||||
id := (&Source{Kind: "github", Repo: repo, Ref: ref, Path: c.Path}).String()
|
||||
found = append(found, NewSkill{ID: id, Name: c.Name, Description: c.Description, Repo: repo, Ref: ref, Path: c.Path, From: from, Agents: slices.Clone(agents)})
|
||||
found = append(found, NewSkill{ID: id, Name: c.Name, Description: c.Description, Kind: "github", Repo: repo, Ref: ref, Path: c.Path, From: from, Agents: slices.Clone(agents)})
|
||||
}
|
||||
return found, true
|
||||
}
|
||||
|
||||
// findNewGit is findNew for a repository on another git host, which has
|
||||
// no API to list its files: the repository fetched with the user's git
|
||||
// (the fetch a check of a changed skill makes, shared through f) is read
|
||||
// for skills beside the library's from there.
|
||||
func findNewGit(l *Library, repo, ref string, f *fetcher) (found []NewSkill, ok bool) {
|
||||
skills := l.fromPlace("git", repo, ref)
|
||||
if len(skills) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
base := newBase(skills)
|
||||
known := map[string]bool{}
|
||||
for _, p := range l.SeenSkills[repoKey(repo, ref)] {
|
||||
known[p] = true
|
||||
}
|
||||
for _, s := range l.Skills {
|
||||
if s.Source != nil && s.Source.Kind == "git" && sameGitRepo(s.Source.Repo, repo) {
|
||||
known[s.Source.Path] = true
|
||||
}
|
||||
}
|
||||
src := Source{Kind: "git", Repo: repo, Ref: ref}
|
||||
tmp, err := f.fetch(src)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
agents := agentsOf(skills)
|
||||
found = []NewSkill{}
|
||||
// the address as the library has it, .git and all: what an Add
|
||||
// installs from is then the same repository, by the same key
|
||||
ish := ref
|
||||
if ish == "" {
|
||||
ish = "HEAD"
|
||||
}
|
||||
from := repo
|
||||
if ref != "" || base != "" {
|
||||
from = strings.TrimSuffix(repo+"/-/tree/"+ish+"/"+base, "/")
|
||||
}
|
||||
for _, c := range candidates(tmp, base, srcFolder(src)) {
|
||||
if known[c.Path] || checkName("skill", c.Name) != nil ||
|
||||
l.hadSkill(src, c, filepath.Join(tmp, filepath.FromSlash(c.Path))) != nil {
|
||||
continue
|
||||
}
|
||||
id := (&Source{Kind: "git", Repo: repo, Ref: ref, Path: c.Path}).String()
|
||||
found = append(found, NewSkill{ID: id, Name: c.Name, Description: c.Description, Kind: "git", Repo: repo, Ref: ref, Path: c.Path, From: from, Agents: slices.Clone(agents)})
|
||||
}
|
||||
return found, true
|
||||
}
|
||||
|
||||
// findNewFolder looks in a folder the user added skills from for skills
|
||||
// that the library doesn't link and the page hasn't offered: ones put
|
||||
// there since (a git pull in a checkout, a skill written by hand). It is
|
||||
// read as Add from folder reads it. ok is false when the folder can't be
|
||||
// read; what an earlier check found then stays.
|
||||
func findNewFolder(l *Library, root string) (found []NewSkill, ok bool) {
|
||||
skills := l.fromPlace("folder", root, "")
|
||||
if len(skills) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
if fi, err := os.Stat(root); err != nil || !fi.IsDir() {
|
||||
return nil, false
|
||||
}
|
||||
known := map[string]bool{}
|
||||
for _, p := range l.SeenSkills[folderKey(root)] {
|
||||
known[p] = true
|
||||
}
|
||||
for _, s := range l.Skills {
|
||||
if s.Source != nil && s.Source.Kind == "folder" {
|
||||
if rel, in := relUnder(root, s.Source.Dir); in {
|
||||
known[rel] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
agents := agentsOf(skills)
|
||||
found = []NewSkill{}
|
||||
src := Source{Kind: "folder", Dir: root}
|
||||
for _, c := range candidates(root, "", filepath.Base(root)) {
|
||||
from := filepath.Join(root, filepath.FromSlash(c.Path))
|
||||
// a folder by a name the library has is never added beside it
|
||||
// (installFrom), and one in the library's own folder is had
|
||||
if known[c.Path] || checkName("skill", c.Name) != nil || l.skill(c.Name) != nil ||
|
||||
l.hadSkill(src, c, from) != nil {
|
||||
continue
|
||||
}
|
||||
if _, in := relUnder(skillsDir(), from); in {
|
||||
continue
|
||||
}
|
||||
found = append(found, NewSkill{ID: from, Name: c.Name, Description: c.Description, Kind: "folder", Repo: root, Path: c.Path, From: root, Agents: slices.Clone(agents)})
|
||||
}
|
||||
return found, true
|
||||
}
|
||||
|
||||
// folderRoots are the folders the user added the library's linked skills
|
||||
// from, as SeenSkills keeps them.
|
||||
func (l *Library) folderRoots() []string {
|
||||
var out []string
|
||||
for k := range l.SeenSkills {
|
||||
if root, ok := strings.CutPrefix(k, foldersKey); ok && root != "" && len(l.fromPlace("folder", root, "")) > 0 {
|
||||
out = append(out, root)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// addedFromFolders is whether the library has linked skills and knows a
|
||||
// folder it added skills from: a check then looks in it. It is on every
|
||||
// read of the page, so it reads no folder.
|
||||
func (l *Library) addedFromFolders() bool {
|
||||
if !slices.ContainsFunc(l.Skills, func(s *Skill) bool { return s.Source != nil && s.Source.Kind == "folder" }) {
|
||||
return false
|
||||
}
|
||||
for k := range l.SeenSkills {
|
||||
if strings.HasPrefix(k, foldersKey) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// lookInFolders is the part of a check that reads the folders the user
|
||||
// added skills from, for skills put there since.
|
||||
func lookInFolders(l *Library) {
|
||||
for _, root := range l.folderRoots() {
|
||||
if found, ok := findNewFolder(l, root); ok {
|
||||
newFound.Lock()
|
||||
newFound.m[folderKey(root)] = found
|
||||
newFound.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// newSkills is what the last checks found new, less what the library has
|
||||
// taken in or set aside since.
|
||||
func newSkills(l *Library) []NewSkill {
|
||||
@@ -330,13 +526,12 @@ 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 ||
|
||||
if n.placeKey() != k || len(l.fromPlace(n.Kind, n.Repo, n.Ref)) == 0 ||
|
||||
slices.Contains(l.SeenSkills[k], n.Path) {
|
||||
continue
|
||||
}
|
||||
if slices.ContainsFunc(l.Skills, func(s *Skill) bool {
|
||||
return s.Source != nil && s.Source.Kind == "github" && s.Source.Repo == n.Repo && s.Source.Path == n.Path
|
||||
}) {
|
||||
src := Source{Kind: n.Kind, Repo: n.Repo, Ref: n.Ref}
|
||||
if slices.ContainsFunc(l.Skills, func(s *Skill) bool { return fromSameSource(s, src, n.Path, n.ID) }) {
|
||||
continue
|
||||
}
|
||||
out = append(out, n)
|
||||
@@ -422,7 +617,7 @@ func IgnoreNewSkills(ids []string) error {
|
||||
return err
|
||||
}
|
||||
for _, n := range hit {
|
||||
l.seenAt(n.Repo, n.Ref, n.Path)
|
||||
l.seenAt(n.placeKey(), n.Path)
|
||||
}
|
||||
return l.save()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// refeccd (Discord): skills added from a git repository or a folder were
|
||||
// only ever updated in place; ones the repository or folder added since
|
||||
// never reached the library. A check now offers them, as it does GitHub's.
|
||||
|
||||
func TestCheckSkillsFindsNewInGitRepo(t *testing.T) {
|
||||
sandbox(t)
|
||||
addr, commit := gitRepo(t, map[string]string{
|
||||
"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs\n---\n",
|
||||
"skills/review/SKILL.md": "---\nname: review\ndescription: Reviews\n---\n",
|
||||
"README.md": "hi",
|
||||
})
|
||||
// pdf picked, review shown and left
|
||||
ok(t)(InstallSkills(addr, []string{"skills/pdf"}, []string{"claude", "codex"}))
|
||||
check(t)
|
||||
if got := newNames(t); len(got) != 0 {
|
||||
t.Fatalf("offered what the picker showed already: %v", got)
|
||||
}
|
||||
|
||||
commit(map[string]string{
|
||||
"skills/slides/SKILL.md": "---\nname: slides\ndescription: Slides\n---\n",
|
||||
"skills/notes/SKILL.md": "---\nname: notes\ndescription: Notes\n---\n",
|
||||
})
|
||||
check(t)
|
||||
if got := newNames(t); !slices.Equal(got, []string{"notes", "slides"}) {
|
||||
t.Fatalf("new: %v", got)
|
||||
}
|
||||
v, _ := Read(nil)
|
||||
n := v.NewSkills[1]
|
||||
if n.Kind != "git" || n.Repo != addr || n.Path != "skills/slides" || n.Description != "Slides" ||
|
||||
!slices.Equal(n.Agents, []string{"claude", "codex"}) {
|
||||
t.Errorf("new skill: %+v", n)
|
||||
}
|
||||
|
||||
// added from the same repository, by the same address and key
|
||||
ok(t)(AddNewSkills([]string{n.ID}))
|
||||
l, _ := load()
|
||||
s := l.skill("slides")
|
||||
if s == nil || s.Source.Kind != "git" || s.Source.Repo != addr || s.Source.Path != "skills/slides" || len(s.Commit) != 40 {
|
||||
t.Fatalf("added: %+v", s)
|
||||
}
|
||||
if !slices.Equal(s.Agents, []string{"claude", "codex"}) {
|
||||
t.Errorf("added for %v", s.Agents)
|
||||
}
|
||||
if got := newNames(t); !slices.Equal(got, []string{"notes"}) {
|
||||
t.Errorf("after adding one: %v", got)
|
||||
}
|
||||
|
||||
// the other set aside, and one removed: neither offered again
|
||||
if err := IgnoreNewSkills([]string{v.NewSkills[0].ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok(t)(RemoveSkills([]string{"slides"}))
|
||||
commit(map[string]string{"README.md": "changed"})
|
||||
check(t)
|
||||
if got := newNames(t); len(got) != 0 {
|
||||
t.Errorf("a set-aside or removed skill offered again: %v", got)
|
||||
}
|
||||
l, _ = load()
|
||||
if l.skill("slides") != nil || l.skill("notes") != nil {
|
||||
t.Error("a check added a skill by itself")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSkillsFindsNewInFolder(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
src := filepath.Join(h, "src/skills")
|
||||
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
|
||||
skill(t, filepath.Join(src, "xlsx"), "xlsx", "Sheets")
|
||||
checkedAt.Store(0) // no check since magpie started
|
||||
t.Cleanup(func() { checkedAt.Store(0) })
|
||||
if v, _ := Read(nil); v.CheckFolders || checkDue(mustLoad(t)) {
|
||||
t.Fatal("a check is offered with nothing to look at")
|
||||
}
|
||||
// pdf picked, xlsx shown and left
|
||||
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude"}))
|
||||
v, _ := Read(nil)
|
||||
if !v.CheckFolders || !checkDue(mustLoad(t)) {
|
||||
t.Fatalf("no check offered for the folder: %v", v.CheckFolders)
|
||||
}
|
||||
check(t)
|
||||
if got := newNames(t); len(got) != 0 {
|
||||
t.Fatalf("offered what the picker showed already: %v", got)
|
||||
}
|
||||
|
||||
// the user puts two more there (a git pull, say), and edits pdf
|
||||
skill(t, filepath.Join(src, "slides"), "slides", "Slides")
|
||||
skill(t, filepath.Join(src, "team/notes"), "notes", "Notes")
|
||||
write(t, filepath.Join(src, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Mine\n---\n")
|
||||
check(t)
|
||||
if got := newNames(t); !slices.Equal(got, []string{"notes", "slides"}) {
|
||||
t.Fatalf("new: %v", got)
|
||||
}
|
||||
v, _ = Read(nil)
|
||||
n := v.NewSkills[1]
|
||||
if n.Kind != "folder" || n.Repo != src || n.Path != "slides" || n.ID != filepath.Join(src, "slides") ||
|
||||
!slices.Equal(n.Agents, []string{"claude"}) {
|
||||
t.Errorf("new skill: %+v", n)
|
||||
}
|
||||
ok(t)(AddNewSkills([]string{n.ID}))
|
||||
l, _ := load()
|
||||
s := l.skill("slides")
|
||||
if s == nil || s.Source.Kind != "folder" || realDir(s.Source.Dir) != realDir(filepath.Join(src, "slides")) {
|
||||
t.Fatalf("added: %+v", s)
|
||||
}
|
||||
if b, _ := os.ReadFile(filepath.Join(src, "pdf/SKILL.md")); string(b) != "---\nname: pdf\ndescription: Mine\n---\n" {
|
||||
t.Errorf("the user's edit was written over: %q", b)
|
||||
}
|
||||
|
||||
// set aside, removed, or a folder by a name the library has: none
|
||||
// offered again
|
||||
if err := IgnoreNewSkills([]string{v.NewSkills[0].ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok(t)(RemoveSkills([]string{"slides"}))
|
||||
skill(t, filepath.Join(src, "other/pdf"), "pdf", "Another pdf")
|
||||
check(t)
|
||||
if got := newNames(t); len(got) != 0 {
|
||||
t.Errorf("offered again: %v", got)
|
||||
}
|
||||
|
||||
// one put there later is offered; with no skill linked from the
|
||||
// folder any more, nothing is, and no check is offered for it
|
||||
skill(t, filepath.Join(src, "late"), "late", "Late")
|
||||
check(t)
|
||||
if got := newNames(t); !slices.Equal(got, []string{"late"}) {
|
||||
t.Fatalf("new: %v", got)
|
||||
}
|
||||
ok(t)(RemoveSkills([]string{"pdf"}))
|
||||
if got := newNames(t); len(got) != 0 {
|
||||
t.Errorf("offered from a folder the library has no skill from: %v", got)
|
||||
}
|
||||
if v, _ := Read(nil); v.CheckFolders {
|
||||
t.Error("a check offered with no linked skill left")
|
||||
}
|
||||
}
|
||||
@@ -1557,6 +1557,7 @@ func installFrom(l *Library, p *Probe, paths, agents []string, shown bool, in *i
|
||||
// own (skill_beside.go)
|
||||
if s := l.hadSkill(src, c, from); s != nil {
|
||||
in.had = append(in.had, s.Name)
|
||||
l.offered(p, c.Path, shown)
|
||||
continue
|
||||
}
|
||||
name := c.Name
|
||||
@@ -1646,21 +1647,36 @@ func installFrom(l *Library, p *Probe, paths, agents []string, shown bool, in *i
|
||||
}
|
||||
if src.Kind == "github" {
|
||||
sk.Hash, sk.Own = hashDir(dir), ownHash(dir)
|
||||
// every skill the picker showed was offered: a check
|
||||
// offers only those the repository adds later
|
||||
l.seenAt(src.Repo, src.Ref, c.Path)
|
||||
if shown {
|
||||
for _, c := range p.Candidates {
|
||||
l.seenAt(src.Repo, src.Ref, c.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
l.offered(p, c.Path, shown)
|
||||
forgetCheck(name)
|
||||
l.Skills = append(l.Skills, sk)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// offered records that the skill at path of a probe's source was offered,
|
||||
// and with shown every skill the picker listed: a check offers only those
|
||||
// the repository or folder adds later. A folder's key is also how the
|
||||
// library remembers which folder its linked skills were added from.
|
||||
func (l *Library) offered(p *Probe, path string, shown bool) {
|
||||
var k string
|
||||
switch p.src.Kind {
|
||||
case "github", "git":
|
||||
k = repoKey(p.src.Repo, p.src.Ref)
|
||||
case "folder":
|
||||
k = folderKey(p.root)
|
||||
default:
|
||||
return
|
||||
}
|
||||
l.seenAt(k, path)
|
||||
if shown {
|
||||
for _, c := range p.Candidates {
|
||||
l.seenAt(k, c.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateSkill fetches a skill from GitHub again, in place: the agents'
|
||||
// links go on pointing at it. One changed here since it was fetched is
|
||||
// an *EditedError unless replace is set, and then the changed version is
|
||||
|
||||
@@ -495,9 +495,13 @@ type View struct {
|
||||
FoundServers []Found `json:"foundServers"`
|
||||
Skills []SkillView `json:"skills"`
|
||||
FoundSkills []FoundSkill `json:"foundSkills"`
|
||||
// NewSkills are skills a check found its skills' repositories to have
|
||||
// added beside them, to add or set aside
|
||||
NewSkills []NewSkill `json:"newSkills"`
|
||||
// NewSkills are skills a check found its skills' repositories, or
|
||||
// the folders they were added from, to have beside them since, to
|
||||
// add or set aside
|
||||
NewSkills []NewSkill `json:"newSkills"`
|
||||
// CheckFolders is whether a check looks in folders the user added
|
||||
// skills from, which the page then offers with no remote skill
|
||||
CheckFolders bool `json:"checkFolders,omitempty"`
|
||||
Projects []ProjectView `json:"projects"`
|
||||
Instructions *InstructionsView `json:"instructions"`
|
||||
Dir string `json:"dir"`
|
||||
@@ -725,6 +729,7 @@ func readView(problems []Problem, glance bool) (*View, error) {
|
||||
v.FoundServers[i].Icon = serverIcon(l, v.FoundServers[i].Server)
|
||||
}
|
||||
v.NewSkills = newSkills(l)
|
||||
v.CheckFolders = l.addedFromFolders()
|
||||
v.Projects = projectViews(l, problems)
|
||||
v.SkillGroups = l.skillGroups()
|
||||
v.CheckDue = checkDue(l)
|
||||
|
||||
Reference in New Issue
Block a user