refactor(registry): rename SkillEntry to ResourceEntry and add unified registry load/save

Rename SkillEntry to ResourceEntry with type alias for backward compat.
All 56 call sites continue to compile without changes.

Add LoadUnifiedRegistry() to merge separate skills and agents registry
files for global mode (skills/registry.yaml + agents/registry.yaml).
Agent entries automatically get Kind="agent" during merge.

Add SaveSplitByKind() to split a unified registry back by kind for
persisting to the correct source directory. Skips writing agents
registry file when no agent entries exist.
This commit is contained in:
Willie
2026-04-07 18:05:40 +08:00
parent c049ccdae0
commit ea055de8e5
3 changed files with 187 additions and 3 deletions
+7 -3
View File
@@ -182,8 +182,9 @@ func (t *ProjectTargetEntry) EnsureSkills() *ResourceTargetConfig {
return t.Skills
}
// SkillEntry represents a remote skill entry in config (shared by global and project).
type SkillEntry struct {
// ResourceEntry represents a remote resource entry in config (shared by global and project).
// Used for both skills and agents.
type ResourceEntry struct {
Name string `yaml:"name"`
Kind string `yaml:"kind,omitempty"`
Source string `yaml:"source"`
@@ -192,9 +193,12 @@ type SkillEntry struct {
Branch string `yaml:"branch,omitempty"`
}
// SkillEntry is an alias for backward compatibility.
type SkillEntry = ResourceEntry
// EffectiveKind returns the resource kind for this entry.
// Returns "skill" if Kind is empty (backward compatibility).
func (s SkillEntry) EffectiveKind() string {
func (s ResourceEntry) EffectiveKind() string {
if s.Kind == "" {
return "skill"
}
+59
View File
@@ -127,3 +127,62 @@ func (r *Registry) Save(dir string) error {
return nil
}
// LoadUnifiedRegistry merges registries from both skills and agents source
// directories into a single Registry. Used in global mode where skills and
// agents have separate registry files.
// Skills entries get Kind="" (backward compat), agent entries get Kind="agent".
func LoadUnifiedRegistry(skillsDir, agentsDir string) (*Registry, error) {
skillsReg, err := LoadRegistry(skillsDir)
if err != nil {
return nil, fmt.Errorf("failed to load skills registry: %w", err)
}
agentsReg, err := LoadRegistry(agentsDir)
if err != nil {
return nil, fmt.Errorf("failed to load agents registry: %w", err)
}
// Ensure agent entries have Kind set
for i := range agentsReg.Skills {
if agentsReg.Skills[i].Kind == "" {
agentsReg.Skills[i].Kind = "agent"
}
}
unified := &Registry{
Skills: make([]SkillEntry, 0, len(skillsReg.Skills)+len(agentsReg.Skills)),
}
unified.Skills = append(unified.Skills, skillsReg.Skills...)
unified.Skills = append(unified.Skills, agentsReg.Skills...)
return unified, nil
}
// SaveSplitByKind splits the unified registry by kind and saves each to
// the appropriate directory. Skills (Kind="" or "skill") go to skillsDir,
// agents (Kind="agent") go to agentsDir.
func (r *Registry) SaveSplitByKind(skillsDir, agentsDir string) error {
skillsReg := &Registry{}
agentsReg := &Registry{}
for _, entry := range r.Skills {
if entry.EffectiveKind() == "agent" {
agentsReg.Skills = append(agentsReg.Skills, entry)
} else {
skillsReg.Skills = append(skillsReg.Skills, entry)
}
}
if err := skillsReg.Save(skillsDir); err != nil {
return fmt.Errorf("failed to save skills registry: %w", err)
}
if len(agentsReg.Skills) > 0 {
if err := agentsReg.Save(agentsDir); err != nil {
return fmt.Errorf("failed to save agents registry: %w", err)
}
}
return nil
}
+121
View File
@@ -130,6 +130,127 @@ func TestMigrateGlobalSkills_NoMigrationWhenRegistryExists(t *testing.T) {
}
}
func TestLoadUnifiedRegistry_MergesBoth(t *testing.T) {
skillsDir := t.TempDir()
agentsDir := t.TempDir()
// Write skills registry
skillsReg := &Registry{
Skills: []SkillEntry{
{Name: "my-skill", Source: "github.com/user/skills-repo"},
},
}
if err := skillsReg.Save(skillsDir); err != nil {
t.Fatalf("Save skills: %v", err)
}
// Write agents registry
agentsReg := &Registry{
Skills: []SkillEntry{
{Name: "my-agent", Source: "github.com/user/agents-repo"},
},
}
if err := agentsReg.Save(agentsDir); err != nil {
t.Fatalf("Save agents: %v", err)
}
unified, err := LoadUnifiedRegistry(skillsDir, agentsDir)
if err != nil {
t.Fatalf("LoadUnifiedRegistry: %v", err)
}
if len(unified.Skills) != 2 {
t.Fatalf("expected 2 entries, got %d", len(unified.Skills))
}
// Agent entry should have Kind="agent"
for _, e := range unified.Skills {
if e.Name == "my-agent" && e.EffectiveKind() != "agent" {
t.Errorf("agent entry should have kind=agent, got %q", e.Kind)
}
if e.Name == "my-skill" && e.EffectiveKind() != "skill" {
t.Errorf("skill entry should have kind=skill, got %q", e.Kind)
}
}
}
func TestLoadUnifiedRegistry_EmptyAgents(t *testing.T) {
skillsDir := t.TempDir()
agentsDir := t.TempDir() // no registry file
skillsReg := &Registry{
Skills: []SkillEntry{
{Name: "s1", Source: "test"},
},
}
skillsReg.Save(skillsDir)
unified, err := LoadUnifiedRegistry(skillsDir, agentsDir)
if err != nil {
t.Fatalf("LoadUnifiedRegistry: %v", err)
}
if len(unified.Skills) != 1 {
t.Fatalf("expected 1 entry, got %d", len(unified.Skills))
}
}
func TestSaveSplitByKind_RoundTrip(t *testing.T) {
skillsDir := t.TempDir()
agentsDir := t.TempDir()
unified := &Registry{
Skills: []SkillEntry{
{Name: "skill-a", Source: "s1"},
{Name: "agent-b", Source: "s2", Kind: "agent"},
{Name: "skill-c", Source: "s3", Kind: "skill"},
},
}
if err := unified.SaveSplitByKind(skillsDir, agentsDir); err != nil {
t.Fatalf("SaveSplitByKind: %v", err)
}
// Load back separately
skillsReg, err := LoadRegistry(skillsDir)
if err != nil {
t.Fatalf("LoadRegistry skills: %v", err)
}
if len(skillsReg.Skills) != 2 {
t.Fatalf("expected 2 skill entries, got %d", len(skillsReg.Skills))
}
agentsReg, err := LoadRegistry(agentsDir)
if err != nil {
t.Fatalf("LoadRegistry agents: %v", err)
}
if len(agentsReg.Skills) != 1 {
t.Fatalf("expected 1 agent entry, got %d", len(agentsReg.Skills))
}
if agentsReg.Skills[0].Name != "agent-b" {
t.Errorf("agent name = %q, want %q", agentsReg.Skills[0].Name, "agent-b")
}
}
func TestSaveSplitByKind_NoAgents_SkipsAgentFile(t *testing.T) {
skillsDir := t.TempDir()
agentsDir := t.TempDir()
unified := &Registry{
Skills: []SkillEntry{
{Name: "only-skill", Source: "s1"},
},
}
if err := unified.SaveSplitByKind(skillsDir, agentsDir); err != nil {
t.Fatalf("SaveSplitByKind: %v", err)
}
// Agents dir should not have registry file
if _, err := os.Stat(filepath.Join(agentsDir, "registry.yaml")); err == nil {
t.Error("expected no agents registry.yaml when no agent entries")
}
}
func TestSourceRoot_NoGit(t *testing.T) {
dir := t.TempDir()
got := SourceRoot(dir)