package core import ( "context" "errors" "fmt" "maps" "os" "path" "path/filepath" "slices" "sync" "skill-manager/internal/fetch" "skill-manager/internal/jsonfile" "skill-manager/internal/lock" "skill-manager/internal/manifest" "skill-manager/internal/skills" ) // vercelLock is the part of vercel-labs/skills' .skill-lock.json that Import reads. type vercelLock struct { Skills map[string]struct { Source string `json:"source"` SourceType string `json:"sourceType"` SkillPath string `json:"skillPath"` } `json:"skills"` } type ImportStatus int const ( Identical ImportStatus = iota Differs NoLocalCopy ) // Imported compares an imported skill with the copy committed in the local directory. type Imported struct { Name, Source string Upstream string // skill directory in the store Local string // committed copy; empty if there is none Status ImportStatus } type ImportReport struct { Sources int Imported []Imported Skipped []string // "name: reason" Untracked []string // skills in the local dir that skm doesn't install } type vercelEntry struct{ name, dir string } type fetched struct { key string src *manifest.Source res fetch.Result found []skills.Skill err error } // Import adds every skill in vercel's lock to the manifest. New sources are pinned // at the head of their default branch; sources already in the manifest keep their // pin. Each skill is compared with its committed copy in localDir. func (s Store) Import(ctx context.Context, vercelPath, localDir string) (ImportReport, error) { var rep ImportReport var vl vercelLock ok, err := jsonfile.Read(vercelPath, &vl) if err != nil { return rep, err } if !ok { return rep, fmt.Errorf("%s not found", vercelPath) } m, l, err := s.load() if err != nil { return rep, err } bySource := map[string][]vercelEntry{} for _, name := range slices.Sorted(maps.Keys(vl.Skills)) { e := vl.Skills[name] if e.SourceType != "github" { rep.Skipped = append(rep.Skipped, fmt.Sprintf("%s: unsupported source type %q", name, e.SourceType)) continue } bySource[e.Source] = append(bySource[e.Source], vercelEntry{name: name, dir: path.Dir(e.SkillPath)}) } // Fetch every source in parallel; merge in order afterwards. ghSources := slices.Sorted(maps.Keys(bySource)) results := make([]fetched, len(ghSources)) var wg sync.WaitGroup for i, gh := range ghSources { url := "github:" + gh f := fetched{key: SourceKey(url), src: &manifest.Source{URL: url}} ref := url if existing := m.Sources[f.key]; existing != nil { if existing.URL != url { f.err = fmt.Errorf("source %q already exists with url %s", f.key, existing.URL) results[i] = f continue } f.src = existing ref = flakeRef(existing) if locked := l.Sources[f.key]; locked != nil { ref = fetch.AtRev(ref, locked.Rev()) } } wg.Go(func() { f.res, f.found, f.err = s.fetchSkills(ctx, ref) results[i] = f }) } wg.Wait() for i, f := range results { entries := bySource[ghSources[i]] if f.err != nil { for _, e := range entries { rep.Skipped = append(rep.Skipped, fmt.Sprintf("%s: %v", e.name, f.err)) } continue } // A "*" source already selects everything; its skills are only compared. if !f.src.Skills.All { for _, e := range entries { if slices.ContainsFunc(f.src.Skills.Items, func(k manifest.Skill) bool { return k.Installed() == e.name }) { continue } item, err := importItem(e, f.found) if err != nil { rep.Skipped = append(rep.Skipped, fmt.Sprintf("%s: %v", e.name, err)) continue } f.src.Skills.Items = append(f.src.Skills.Items, item) } } if !f.src.Skills.All && len(f.src.Skills.Items) == 0 { continue } resolved, err := resolve(f.src.Skills, f.found, "skill") if err != nil { return rep, fmt.Errorf("%s: %w", f.key, err) } m.Sources[f.key] = f.src l.Sources[f.key] = &lock.Source{Locked: f.res.Locked, Skills: resolved} rep.Sources++ for _, name := range slices.Sorted(maps.Keys(resolved)) { imp := Imported{Name: name, Source: f.key, Upstream: filepath.Join(f.res.StorePath, resolved[name]), Status: NoLocalCopy} if local := filepath.Join(localDir, name); isDir(local) { imp.Local = local same, err := sameDir(ctx, local, imp.Upstream) if err != nil { return rep, err } imp.Status = Differs if same { imp.Status = Identical } } rep.Imported = append(rep.Imported, imp) } } if err := s.save(m, l); err != nil { return rep, err } rep.Untracked, err = untracked(localDir, l) return rep, err } // importItem picks the upstream skill at the vercel entry's path, aliasing it when // vercel installed it under another name. func importItem(e vercelEntry, found []skills.Skill) (manifest.Skill, error) { i := slices.IndexFunc(found, func(f skills.Skill) bool { return f.Path == e.dir }) if i < 0 { return manifest.Skill{}, fmt.Errorf("not at %s upstream anymore (removed or moved)", e.dir) } item := manifest.Skill{Name: found[i].Name} if e.name != item.Name { item.As = e.name } sameName := 0 for _, f := range found { if f.Name == item.Name { sameName++ } } if sameName > 1 { item.Path = e.dir } return item, nil } func untracked(localDir string, l *lock.Lock) ([]string, error) { entries, err := os.ReadDir(localDir) if errors.Is(err, os.ErrNotExist) { return nil, nil } if err != nil { return nil, err } installed := map[string]bool{} for _, src := range l.Sources { for name := range src.Skills { installed[name] = true } } var out []string for _, e := range entries { if !e.IsDir() || installed[e.Name()] { continue } if _, err := os.Stat(filepath.Join(localDir, e.Name(), "SKILL.md")); err == nil { out = append(out, e.Name()) } } return out, nil } func isDir(p string) bool { fi, err := os.Stat(p) return err == nil && fi.IsDir() }