Files
magpie/internal/library/skill_from.go
T
yetone 6d198f7f23 library, gui: a repository's skills are one group on the Library page however each came in — the skills CLI's lock, a git clone, or installed from it again (White Immortal on Discord)
White Immortal asked for the skills of github.com/mattpocock/skills to be one group, the ones installed before too. Skills installed from GitHub in magpie were already grouped by repository, but one installed another way sat under On this computer. Each skill on the Library page now carries the repository it came from (repo). A skill installed from GitHub, or by CC Switch from there, says so as before. For the others it is traced:

- npx skills add (the skills CLI) writes ~/.agents/.skill-lock.json, with each skill's repository by name. A skill in ~/.agents/skills, or moved into the library from there, gets its repository from it.
- A skill linked from a git clone gets the remote "origin" of the clone it is in (up to four folders up). The URL can be https, ssh or scp-style, and a user or token in it is never kept. A checkout that is the home folder, or an app folder in it such as ~/.claude kept in git, is the user's dotfiles, and its skills are not grouped by it.
- When a skill is brought in from an agent's folder, its repository is traced first and kept as From, since a copy leaves its .git behind.
- Typing a repository in Install (or opening it from the market) notes it as From on the skills the library has already whose SKILL.md is that repository's very own. Installing from github.com/mattpocock/skills again then groups the ones installed before. A skill of the same name whose file differs is someone else's, and stays where it was.

From and a traced repository only group. Such a skill is still not updated from GitHub. A repository on another git host (gitlab.com/…) is a group of its own, and its icon opens it at that host. A repository group with skills that only were traced there goes by name, not by a folder in it. TestSkillRepoTraced, TestRemoteRepo and TestInstallAgainNotesFrom are new, along with skill-repo-groups.test.cjs (Chromium and WebKit, English and Chinese). The GUI test fails against the old library.js. go vet, go build (GOOS=linux and windows, nogui) and go test -tags nogui ./... under a temp HOME pass. TestProvidersAnswerWhileListsComeIn and TestUserPath failed once under load and pass on their own. The library's GUI tests pass on WebKit. On Chromium, import-all-skills, library-own-folder and shared-skills fail the same way at HEAD without this change.
2026-10-06 00:36:31 +08:00

233 lines
6.4 KiB
Go

package library
import (
"bytes"
"encoding/json"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
// Where a skill came from, for the page to group it with the others from
// the same repository (White Immortal on Discord: the skills of
// github.com/mattpocock/skills together, however each came in). One
// installed from GitHub says so itself; for the others it is traced:
// CC Switch's record, the skills CLI's lock (npx skills add, in
// ~/.agents/.skill-lock.json), the git checkout a linked folder is in,
// or the repository the user installed from since that has the very same
// skill (From). It only groups: such a skill is not updated from there.
// repoOf is the repository a library skill came from: owner/repo on
// GitHub, host/path elsewhere, "" when it can't be told.
func (tr *tracer) repoOf(s *Skill) string {
if s.Source != nil && s.Source.Kind == "github" {
return s.Source.Repo
}
if o, ok := ccSwitchOrigin(s); ok {
return o.Repo
}
if s.From != "" {
return s.From
}
dir := skillDir(s.Name)
if !linked(dir) {
// the library's own copy: the skills CLI's, moved in, by its name
if e, ok := tr.lock()[s.Name]; ok && (s.Source == nil || within(realDir(s.Source.Dir), realDir(sharedSkillsDir()))) {
return e
}
return ""
}
return tr.folder(realDir(dir))
}
// folder is the repository a skill's folder elsewhere came from: the
// skills CLI's lock for one in ~/.agents/skills, else its git checkout.
func (tr *tracer) folder(dir string) string {
if within(dir, realDir(sharedSkillsDir())) {
if e, ok := tr.lock()[filepath.Base(dir)]; ok {
return e
}
}
return gitRepoOf(dir)
}
// tracer reads what tracing needs once for a whole page.
type tracer struct {
locked map[string]string
read bool
}
// lock is the skills CLI's lock: each skill's repository by its name.
func (tr *tracer) lock() map[string]string {
if !tr.read {
tr.read, tr.locked = true, readSkillLock(filepath.Join(home(), ".agents", ".skill-lock.json"))
}
return tr.locked
}
func readSkillLock(path string) map[string]string {
b, err := os.ReadFile(path)
if err != nil {
return nil
}
var f struct {
Skills map[string]struct {
Source string `json:"source"`
SourceType string `json:"sourceType"`
SourceURL string `json:"sourceUrl"`
} `json:"skills"`
}
if json.Unmarshal(b, &f) != nil {
return nil
}
out := map[string]string{}
for name, e := range f.Skills {
r := ""
if e.SourceType == "github" && repoRe.MatchString(e.Source) {
r = strings.TrimSuffix(e.Source, ".git")
} else if e.SourceURL != "" {
r = remoteRepo(e.SourceURL)
}
if r != "" {
out[name] = r
}
}
return out
}
// gitRepoOf is the repository of the git checkout a skill's folder is, or
// is in a few folders down: its remote origin. A checkout that is the home
// folder, or a folder of an app's own in it (~/.claude kept in git), is
// the user's dotfiles rather than where the skill came from.
func gitRepoOf(dir string) string {
h := realDir(home())
for i := 0; i < 4 && dir != "" && dir != h; i++ {
if p := filepath.Dir(dir); p == h && strings.HasPrefix(filepath.Base(dir), ".") {
return ""
}
if fi, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
cfg := filepath.Join(dir, ".git", "config")
if !fi.IsDir() {
cfg = gitdirConfig(dir)
}
return originURL(cfg)
}
next := filepath.Dir(dir)
if next == dir {
break
}
dir = next
}
return ""
}
// gitdirConfig is the config of a checkout whose .git is a file (a
// worktree or a submodule): the folder it names, or its common one.
func gitdirConfig(dir string) string {
b, err := os.ReadFile(filepath.Join(dir, ".git"))
if err != nil {
return ""
}
g, ok := strings.CutPrefix(strings.TrimSpace(string(b)), "gitdir:")
if !ok {
return ""
}
g = strings.TrimSpace(g)
if !filepath.IsAbs(g) {
g = filepath.Join(dir, g)
}
if c, err := os.ReadFile(filepath.Join(g, "commondir")); err == nil {
cd := strings.TrimSpace(string(c))
if !filepath.IsAbs(cd) {
cd = filepath.Join(g, cd)
}
g = cd
}
return filepath.Join(g, "config")
}
var originSection = regexp.MustCompile(`^\[\s*remote\s+"origin"\s*\]$`)
// originURL is the repository remote "origin" of a git config fetches.
func originURL(cfg string) string {
b, err := os.ReadFile(cfg)
if err != nil {
return ""
}
in := false
for _, line := range bytes.Split(b, []byte("\n")) {
l := strings.TrimSpace(string(line))
if strings.HasPrefix(l, "[") {
in = originSection.MatchString(l)
continue
}
if k, v, ok := strings.Cut(l, "="); in && ok && strings.TrimSpace(k) == "url" {
return remoteRepo(strings.TrimSpace(v))
}
}
return ""
}
var scpRemote = regexp.MustCompile(`^(?:[\w.-]+@)?([\w.-]+\.[\w.-]+):(.+)$`)
// remoteRepo reads a git remote's address as a repository: owner/repo on
// GitHub, host/path elsewhere. A user or a token in it is never kept.
func remoteRepo(s string) string {
s = strings.TrimSpace(s)
host, p := "", ""
if u, err := url.Parse(s); err == nil && u.Scheme != "" && u.Host != "" {
host, p = u.Hostname(), u.Path
} else if m := scpRemote.FindStringSubmatch(s); m != nil {
host, p = m[1], m[2]
} else if repoRe.MatchString(s) {
host, p = "github.com", s
} else {
return ""
}
host = strings.TrimPrefix(strings.ToLower(host), "www.")
p = strings.TrimSuffix(strings.Trim(p, "/"), ".git")
if strings.Count(p, "/") < 1 || strings.Contains(p, "..") {
return ""
}
if host == "github.com" {
parts := strings.Split(p, "/")
return parts[0] + "/" + parts[1]
}
return host + "/" + p
}
// noteFrom writes down, for the skills the library has already that a
// repository being installed from has too, that they came from there,
// when the library's SKILL.md is that repository's very own: installing
// from it again groups them with the rest of it. root is where the
// repository was fetched to.
func noteFrom(src Source, root string, cands []Candidate) {
if src.Kind != "github" {
return
}
mu.Lock()
defer mu.Unlock()
l, err := load()
if err != nil {
return
}
changed := false
for _, c := range cands {
s := l.skill(c.Name)
if s == nil || (s.Source != nil && s.Source.Kind == "github") || s.From == src.Repo {
continue
}
mine, err1 := os.ReadFile(filepath.Join(skillDir(s.Name), "SKILL.md"))
theirs, err2 := os.ReadFile(filepath.Join(root, filepath.FromSlash(c.Path), "SKILL.md"))
if err1 != nil || err2 != nil || !bytes.Equal(bytes.TrimSpace(mine), bytes.TrimSpace(theirs)) {
continue
}
s.From, changed = src.Repo, true
}
if changed {
l.save()
}
}