feat: pin agent definitions next to skills
A source can install agents (agents/<name>.md files with a frontmatter name) alongside or instead of skills. - manifest: optional "agents" selection; lock: "agents" map - add --agent / --all-agents, remove --agent, list shows the kind - check/update diff agents like skills (files, not directories) - dashboard lists and removes agents - home-manager module: agentTargets (default .claude/agents) links <name>.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0aedef8c8c
commit
df8d11d8cb
15 files changed
+648
-131
No files matched your search
+139
-67
@@ -3,6 +3,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
@@ -88,16 +89,24 @@ type AddRequest struct {
|
||||
// Skills to add. Ignored when All is set.
|
||||
Skills []manifest.Skill
|
||||
All bool
|
||||
// Agents to add, by name or path; ignored when AllAgents is set.
|
||||
Agents []manifest.Skill
|
||||
AllAgents bool
|
||||
}
|
||||
|
||||
// NoSelectionError is returned by Add when neither skills nor All were given.
|
||||
type NoSelectionError struct {
|
||||
Source string
|
||||
Available []skills.Skill
|
||||
Source string
|
||||
Available []skills.Skill
|
||||
AvailableAgents []skills.Skill
|
||||
}
|
||||
|
||||
func (e *NoSelectionError) Error() string {
|
||||
return fmt.Sprintf("no skills selected from %s; available: %s", e.Source, available(e.Available))
|
||||
msg := fmt.Sprintf("no skills selected from %s; available: %s", e.Source, available(e.Available))
|
||||
if len(e.AvailableAgents) > 0 {
|
||||
msg += "; agents: " + available(e.AvailableAgents)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func available(found []skills.Skill) string {
|
||||
@@ -120,43 +129,60 @@ func (s Store) Add(ctx context.Context, req AddRequest) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res, found, err := s.fetchSkills(ctx, ref)
|
||||
res, found, agents, err := s.fetchSource(ctx, ref)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case req.All:
|
||||
src.Skills = manifest.Selection{All: true}
|
||||
case len(req.Skills) == 0:
|
||||
return "", &NoSelectionError{Source: key, Available: found}
|
||||
case src.Skills.All:
|
||||
return "", fmt.Errorf("source %q already installs every skill", key)
|
||||
default:
|
||||
for _, k := range req.Skills {
|
||||
if k.Name == "" {
|
||||
i := slices.IndexFunc(found, func(f skills.Skill) bool { return f.Path == k.Path })
|
||||
if i < 0 {
|
||||
return "", fmt.Errorf("%s: no skill at %s", key, k.Path)
|
||||
}
|
||||
k.Name = found[i].Name
|
||||
}
|
||||
if slices.ContainsFunc(src.Skills.Items, func(e manifest.Skill) bool { return e.Installed() == k.Installed() }) {
|
||||
return "", fmt.Errorf("%s is already installed from %s", k.Installed(), key)
|
||||
}
|
||||
src.Skills.Items = append(src.Skills.Items, k)
|
||||
}
|
||||
if !req.All && len(req.Skills) == 0 && !req.AllAgents && len(req.Agents) == 0 {
|
||||
return "", &NoSelectionError{Source: key, Available: found, AvailableAgents: agents}
|
||||
}
|
||||
if err := selectInto(&src.Skills, req.Skills, req.All, found, key, "skill"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := selectInto(&src.Agents, req.Agents, req.AllAgents, agents, key, "agent"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resolved, err := resolve(src.Skills, found)
|
||||
resolved, err := resolve(src.Skills, found, "skill")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
resolvedAgents, err := resolve(src.Agents, agents, "agent")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
m.Sources[key] = src
|
||||
l.Sources[key] = &lock.Source{Locked: res.Locked, Skills: resolved}
|
||||
l.Sources[key] = &lock.Source{Locked: res.Locked, Skills: resolved, Agents: resolvedAgents}
|
||||
return key, s.save(m, l)
|
||||
}
|
||||
|
||||
// selectInto adds the requested entries of one kind to sel, or switches it to "*".
|
||||
func selectInto(sel *manifest.Selection, req []manifest.Skill, all bool, found []skills.Skill, key, kind string) error {
|
||||
switch {
|
||||
case all:
|
||||
*sel = manifest.Selection{All: true}
|
||||
case len(req) == 0:
|
||||
case sel.All:
|
||||
return fmt.Errorf("source %q already installs every %s", key, kind)
|
||||
default:
|
||||
for _, k := range req {
|
||||
if k.Name == "" {
|
||||
i := slices.IndexFunc(found, func(f skills.Skill) bool { return f.Path == k.Path })
|
||||
if i < 0 {
|
||||
return fmt.Errorf("%s: no %s at %s", key, kind, k.Path)
|
||||
}
|
||||
k.Name = found[i].Name
|
||||
}
|
||||
if slices.ContainsFunc(sel.Items, func(e manifest.Skill) bool { return e.Installed() == k.Installed() }) {
|
||||
return fmt.Errorf("%s is already installed from %s", k.Installed(), key)
|
||||
}
|
||||
sel.Items = append(sel.Items, k)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// target resolves the source an add goes to and the flake ref to fetch: the pinned
|
||||
// rev for a tracked source, the head of its ref for a new one. src is a copy that
|
||||
// is safe to modify.
|
||||
@@ -180,10 +206,10 @@ func target(m *manifest.Manifest, l *lock.Lock, req AddRequest) (key string, src
|
||||
if locked == nil {
|
||||
return "", nil, "", fmt.Errorf("source %q is in %s but not locked; run skm update %s first", key, ManifestFile, key)
|
||||
}
|
||||
src = &manifest.Source{URL: existing.URL, Ref: existing.Ref, Skills: manifest.Selection{
|
||||
All: existing.Skills.All,
|
||||
Items: slices.Clone(existing.Skills.Items),
|
||||
}}
|
||||
src = &manifest.Source{URL: existing.URL, Ref: existing.Ref,
|
||||
Skills: manifest.Selection{All: existing.Skills.All, Items: slices.Clone(existing.Skills.Items)},
|
||||
Agents: manifest.Selection{All: existing.Agents.All, Items: slices.Clone(existing.Agents.Items)},
|
||||
}
|
||||
return key, src, fetch.AtRev(flakeRef(src), locked.Rev()), nil
|
||||
}
|
||||
|
||||
@@ -216,7 +242,7 @@ func (s Store) Available(ctx context.Context, req AddRequest) ([]skills.Skill, m
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
_, found, err := s.fetchSkills(ctx, ref)
|
||||
_, found, _, err := s.fetchSource(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
@@ -229,27 +255,41 @@ func (s Store) Available(ctx context.Context, req AddRequest) ([]skills.Skill, m
|
||||
return found, installed, nil
|
||||
}
|
||||
|
||||
func (s Store) fetchSkills(ctx context.Context, ref string) (fetch.Result, []skills.Skill, error) {
|
||||
// fetchSource prefetches ref and finds its skills and agent definitions.
|
||||
func (s Store) fetchSource(ctx context.Context, ref string) (fetch.Result, []skills.Skill, []skills.Skill, error) {
|
||||
res, err := s.Fetcher.Prefetch(ctx, ref)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, err
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
found, err := skills.Discover(res.StorePath)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, err
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
if len(found) == 0 {
|
||||
return fetch.Result{}, nil, fmt.Errorf("%s contains no SKILL.md", ref)
|
||||
agents, err := skills.DiscoverAgents(res.StorePath)
|
||||
if err != nil {
|
||||
return fetch.Result{}, nil, nil, err
|
||||
}
|
||||
return res, found, nil
|
||||
if len(found) == 0 && len(agents) == 0 {
|
||||
return fetch.Result{}, nil, nil, fmt.Errorf("%s contains no SKILL.md or agent definition", ref)
|
||||
}
|
||||
return res, found, agents, nil
|
||||
}
|
||||
|
||||
// fetchSkills is fetchSource for callers that only install skills.
|
||||
func (s Store) fetchSkills(ctx context.Context, ref string) (fetch.Result, []skills.Skill, error) {
|
||||
res, found, _, err := s.fetchSource(ctx, ref)
|
||||
if err == nil && len(found) == 0 {
|
||||
err = fmt.Errorf("%s contains no SKILL.md", ref)
|
||||
}
|
||||
return res, found, err
|
||||
}
|
||||
|
||||
// resolve maps installed names to paths within the source.
|
||||
func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, error) {
|
||||
func resolve(sel manifest.Selection, found []skills.Skill, kind string) (map[string]string, error) {
|
||||
out := map[string]string{}
|
||||
add := func(name, path string) error {
|
||||
if prev, dup := out[name]; dup {
|
||||
return fmt.Errorf("two skills installed as %q: %s and %s", name, prev, path)
|
||||
return fmt.Errorf("two %ss installed as %q: %s and %s", kind, name, prev, path)
|
||||
}
|
||||
out[name] = path
|
||||
return nil
|
||||
@@ -258,7 +298,7 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
if sel.All {
|
||||
for _, f := range found {
|
||||
if err := add(f.Name, f.Path); err != nil {
|
||||
return nil, fmt.Errorf(`%w; list skills explicitly, picking one with "path"`, err)
|
||||
return nil, fmt.Errorf(`%w; list %ss explicitly, picking one with "path"`, err, kind)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
@@ -272,15 +312,15 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
}
|
||||
switch {
|
||||
case len(matches) == 0 && k.Path != "":
|
||||
return nil, fmt.Errorf("no skill %q at %s", k.Name, k.Path)
|
||||
return nil, fmt.Errorf("no %s %q at %s", kind, k.Name, k.Path)
|
||||
case len(matches) == 0:
|
||||
return nil, fmt.Errorf("no skill %q; available: %s", k.Name, available(found))
|
||||
return nil, fmt.Errorf("no %s %q; available: %s", kind, k.Name, available(found))
|
||||
case len(matches) > 1:
|
||||
var paths []string
|
||||
for _, f := range matches {
|
||||
paths = append(paths, f.Path)
|
||||
}
|
||||
return nil, fmt.Errorf(`several skills named %q (%s); pick one with "path"`, k.Name, strings.Join(paths, ", "))
|
||||
return nil, fmt.Errorf(`several %ss named %q (%s); pick one with "path"`, kind, k.Name, strings.Join(paths, ", "))
|
||||
}
|
||||
if err := add(k.Installed(), matches[0].Path); err != nil {
|
||||
return nil, err
|
||||
@@ -290,13 +330,19 @@ func resolve(sel manifest.Selection, found []skills.Skill) (map[string]string, e
|
||||
}
|
||||
|
||||
func checkCollisions(l *lock.Lock) error {
|
||||
owner := map[string]string{}
|
||||
for _, key := range slices.Sorted(maps.Keys(l.Sources)) {
|
||||
for _, name := range slices.Sorted(maps.Keys(l.Sources[key].Skills)) {
|
||||
if prev, ok := owner[name]; ok {
|
||||
return fmt.Errorf(`skill %q comes from both %s and %s; rename one with "as"`, name, prev, key)
|
||||
for _, kind := range []string{"skill", "agent"} {
|
||||
owner := map[string]string{}
|
||||
for _, key := range slices.Sorted(maps.Keys(l.Sources)) {
|
||||
installed := l.Sources[key].Skills
|
||||
if kind == "agent" {
|
||||
installed = l.Sources[key].Agents
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(installed)) {
|
||||
if prev, ok := owner[name]; ok {
|
||||
return fmt.Errorf(`%s %q comes from both %s and %s; rename one with "as"`, kind, name, prev, key)
|
||||
}
|
||||
owner[name] = key
|
||||
}
|
||||
owner[name] = key
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -304,42 +350,51 @@ func checkCollisions(l *lock.Lock) error {
|
||||
|
||||
// Remove drops one installed skill, and its source once that is empty.
|
||||
// Returns the source it came from.
|
||||
func (s Store) Remove(name string) (string, error) {
|
||||
func (s Store) Remove(name string) (string, error) { return s.removeOne(name, false) }
|
||||
|
||||
// RemoveAgent drops one installed agent definition, like Remove.
|
||||
func (s Store) RemoveAgent(name string) (string, error) { return s.removeOne(name, true) }
|
||||
|
||||
func (s Store) removeOne(name string, agent bool) (string, error) {
|
||||
kind := "skill"
|
||||
if agent {
|
||||
kind = "agent"
|
||||
}
|
||||
m, l, err := s.load()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, key := range slices.Sorted(maps.Keys(m.Sources)) {
|
||||
src := m.Sources[key]
|
||||
i := slices.IndexFunc(src.Skills.Items, func(k manifest.Skill) bool { return k.Installed() == name })
|
||||
locked := isLocked(l, key, name)
|
||||
sel, locks := &src.Skills, func(ls *lock.Source) map[string]string { return ls.Skills }
|
||||
if agent {
|
||||
sel, locks = &src.Agents, func(ls *lock.Source) map[string]string { return ls.Agents }
|
||||
}
|
||||
i := slices.IndexFunc(sel.Items, func(k manifest.Skill) bool { return k.Installed() == name })
|
||||
locked := l.Sources[key] != nil && has(locks(l.Sources[key]), name)
|
||||
if i < 0 && !locked {
|
||||
continue
|
||||
}
|
||||
if src.Skills.All {
|
||||
return "", fmt.Errorf(`%s comes from %s, which installs every skill ("*"); remove the whole source with --source %s`, name, key, key)
|
||||
if sel.All {
|
||||
return "", fmt.Errorf(`%s comes from %s, which installs every %s ("*"); remove the whole source with --source %s`, name, key, kind, key)
|
||||
}
|
||||
if i >= 0 {
|
||||
src.Skills.Items = slices.Delete(src.Skills.Items, i, i+1)
|
||||
sel.Items = slices.Delete(sel.Items, i, i+1)
|
||||
}
|
||||
if locked {
|
||||
delete(l.Sources[key].Skills, name)
|
||||
delete(locks(l.Sources[key]), name)
|
||||
}
|
||||
if len(src.Skills.Items) == 0 {
|
||||
if src.Empty() {
|
||||
delete(m.Sources, key)
|
||||
delete(l.Sources, key)
|
||||
}
|
||||
return key, s.save(m, l)
|
||||
}
|
||||
return "", fmt.Errorf("no installed skill %q", name)
|
||||
return "", fmt.Errorf("no installed %s %q", kind, name)
|
||||
}
|
||||
|
||||
func isLocked(l *lock.Lock, key, name string) bool {
|
||||
src := l.Sources[key]
|
||||
if src == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := src.Skills[name]
|
||||
func has(m map[string]string, name string) bool {
|
||||
_, ok := m[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
@@ -362,9 +417,11 @@ type Installed struct {
|
||||
Source string
|
||||
Path string
|
||||
Rev string
|
||||
// Agent is set for an agent definition, which Path names as a file.
|
||||
Agent bool
|
||||
}
|
||||
|
||||
// List returns the locked skills, sorted by name.
|
||||
// List returns the locked skills and agents, sorted by name.
|
||||
func (s Store) List() ([]Installed, error) {
|
||||
_, l, err := s.load()
|
||||
if err != nil {
|
||||
@@ -375,7 +432,22 @@ func (s Store) List() ([]Installed, error) {
|
||||
for name, path := range src.Skills {
|
||||
out = append(out, Installed{Name: name, Source: key, Path: path, Rev: src.Rev()})
|
||||
}
|
||||
for name, path := range src.Agents {
|
||||
out = append(out, Installed{Name: name, Source: key, Path: path, Rev: src.Rev(), Agent: true})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(out, func(a, b Installed) int { return strings.Compare(a.Name, b.Name) })
|
||||
slices.SortFunc(out, func(a, b Installed) int {
|
||||
return cmp.Or(strings.Compare(a.Name, b.Name), compareBool(a.Agent, b.Agent))
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func compareBool(a, b bool) int {
|
||||
switch {
|
||||
case a == b:
|
||||
return 0
|
||||
case b:
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
Reference in new issue
Block a user