feat: bundled plugin discovery for skills

Adds plugin discovery to internal/skill — Yaegi .go files in a skill's
plugins/ directory are loaded alongside the SKILL.md body.

- Plugin type: Name, Path, Src, Run(input) (string, error) via Yaegi
- DiscoverPlugins(dir, skillName): scans .agents/skills/<name>/plugins/*.go
- Run() uses Yaegi interpreter with full stdlib access; plugins must use
  package main and export func Run(string) string
- Discover() now populates Skill.Plugins alongside frontmatter + body
- Missing plugins/ dir returns empty slice (not an error)
- Missing skill returns nil (not an error)

5 new tests: DiscoverPlugins, DiscoverPluginsNone,
DiscoverPluginsMissingSkill, PluginRun (Yaegi round-trip),
DiscoverIncludesPlugins (integration with Discover).
This commit is contained in:
sam committed 2026-07-22 02:55:31 +10:00
1 parent 121dd7d815
commit fc33e519a5
3 files changed
+301 -3

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
package skill
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
// Plugin is a discovered Yaegi-interpreted .go file in a skill's plugins/
// directory. The source must be a valid Go package exporting a Run function:
//
// func Run(input string) string
type Plugin struct {
Name string // filename without .go extension
Path string // full path to the .go file
Src string // source code content
}
// Run interprets the plugin source via Yaegi and calls its Run function
// with the given input. The plugin runs with full standard library access.
func (p *Plugin) Run(input string) (string, error) {
i := interp.New(interp.Options{})
if err := i.Use(stdlib.Symbols); err != nil {
return "", fmt.Errorf("plugin %s: stdlib: %w", p.Name, err)
}
if _, err := i.Eval(p.Src); err != nil {
return "", fmt.Errorf("plugin %s: eval: %w", p.Name, err)
}
// Plugins must use package main — the convention for runnable Yaegi scripts.
v, err := i.Eval("main.Run")
if err != nil {
return "", fmt.Errorf("plugin %s: resolve Run: %w", p.Name, err)
}
fn, ok := v.Interface().(func(string) string)
if !ok {
return "", fmt.Errorf("plugin %s: Run is %T, want func(string) string", p.Name, v.Interface())
}
return fn(input), nil
}
// DiscoverPlugins scans dir/.agents/skills/<skillName>/plugins/*.go and
// returns the discovered plugins sorted by filename. Returns nil if the
// skill directory does not exist. Returns an empty slice if the skill
// exists but has no plugins/ directory.
func DiscoverPlugins(dir, skillName string) ([]Plugin, error) {
pluginsDir := filepath.Join(dir, skillsDir, skillName, "plugins")
entries, err := os.ReadDir(pluginsDir)
if os.IsNotExist(err) {
// Skill exists but has no plugins directory — not an error.
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("read plugins dir %s: %w", pluginsDir, err)
}
var names []string
for _, e := range entries {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
names = append(names, e.Name())
}
}
sort.Strings(names)
plugins := make([]Plugin, 0, len(names))
for _, name := range names {
path := filepath.Join(pluginsDir, name)
src, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read plugin %s: %w", path, err)
}
plugins = append(plugins, Plugin{
Name: strings.TrimSuffix(name, ".go"),
Path: path,
Src: string(src),
})
}
return plugins, nil
}
+209
View File
@@ -0,0 +1,209 @@
package skill
import (
"os"
"path/filepath"
"testing"
)
func TestDiscoverPlugins(t *testing.T) {
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "my-skill")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
// Write a SKILL.md so the skill directory is valid.
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: my-skill
description: A test skill
version: 1.0.0
---
Body.
`), 0o644); err != nil {
t.Fatal(err)
}
// Write two plugin files.
checkGo := `package main
import "fmt"
func Run(input string) string {
return "checked: " + input
}
`
if err := os.WriteFile(filepath.Join(pluginsDir, "check.go"), []byte(checkGo), 0o644); err != nil {
t.Fatal(err)
}
lintGo := `package main
import "strings"
func Run(input string) string {
if strings.Contains(input, "TODO") {
return "found TODO"
}
return "clean"
}
`
if err := os.WriteFile(filepath.Join(pluginsDir, "lint.go"), []byte(lintGo), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := DiscoverPlugins(dir, "my-skill")
if err != nil {
t.Fatal(err)
}
if len(plugins) != 2 {
t.Fatalf("got %d plugins, want 2", len(plugins))
}
// Plugins are returned sorted by name.
if plugins[0].Name != "check" {
t.Errorf("plugin[0].Name = %q, want check", plugins[0].Name)
}
if plugins[1].Name != "lint" {
t.Errorf("plugin[1].Name = %q, want lint", plugins[1].Name)
}
// Verify source content is loaded.
if plugins[0].Src != checkGo {
t.Errorf("check plugin source mismatch")
}
if plugins[1].Src != lintGo {
t.Errorf("lint plugin source mismatch")
}
}
func TestDiscoverPluginsNone(t *testing.T) {
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "no-plugins")
if err := os.MkdirAll(skillsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: no-plugins
description: desc
version: 1.0.0
---
Body.
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := DiscoverPlugins(dir, "no-plugins")
if err != nil {
t.Fatal(err)
}
if len(plugins) != 0 {
t.Fatalf("got %d plugins, want 0", len(plugins))
}
}
func TestDiscoverPluginsMissingSkill(t *testing.T) {
dir := t.TempDir()
plugins, err := DiscoverPlugins(dir, "nonexistent")
if err != nil {
t.Fatal(err)
}
if plugins != nil {
t.Fatalf("got %v, want nil for missing skill", plugins)
}
}
func TestPluginRun(t *testing.T) {
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "runner")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: runner
description: desc
version: 1.0.0
---
Body.
`), 0o644); err != nil {
t.Fatal(err)
}
src := `package main
func Run(input string) string {
return "result: " + input
}
`
if err := os.WriteFile(filepath.Join(pluginsDir, "echo.go"), []byte(src), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := DiscoverPlugins(dir, "runner")
if err != nil {
t.Fatal(err)
}
if len(plugins) != 1 {
t.Fatalf("got %d plugins, want 1", len(plugins))
}
// Run the plugin via Yaegi and verify the output.
out, err := plugins[0].Run("hello")
if err != nil {
t.Fatal(err)
}
if out != "result: hello" {
t.Errorf("Run() = %q, want %q", out, "result: hello")
}
}
func TestDiscoverIncludesPlugins(t *testing.T) {
// Verify that Discover() populates the Plugins field on Skill.
dir := t.TempDir()
skillsDir := filepath.Join(dir, ".agents", "skills", "full-skill")
pluginsDir := filepath.Join(skillsDir, "plugins")
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
name: full-skill
description: Has plugins
version: 1.0.0
metadata:
archie:
tools: [go]
engine: any
---
Skill body here.
`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(pluginsDir, "gate.go"), []byte(`package main
func Run(input string) string {
return "gated"
}
`), 0o644); err != nil {
t.Fatal(err)
}
skills, err := Discover(dir)
if err != nil {
t.Fatal(err)
}
if len(skills) != 1 {
t.Fatalf("got %d skills, want 1", len(skills))
}
s := skills["full-skill"]
if s.Body != "Skill body here." {
t.Errorf("Body = %q", s.Body)
}
if len(s.Plugins) != 1 {
t.Fatalf("got %d plugins, want 1", len(s.Plugins))
}
if s.Plugins[0].Name != "gate" {
t.Errorf("plugin name = %q, want gate", s.Plugins[0].Name)
}
}
+9 -3
View File
@@ -26,11 +26,13 @@ type Frontmatter struct {
} `yaml:"metadata"`
}
// Skill is a parsed SKILL.md file with its frontmatter and body.
// Skill is a parsed SKILL.md file with its frontmatter, body, and any
// bundled Yaegi plugins found in the skill's plugins/ directory.
type Skill struct {
Frontmatter Frontmatter
Body string
Dir string // skill directory name (e.g. "tdd-bugfix")
Dir string // skill directory name (e.g. "tdd-bugfix")
Plugins []Plugin // bundled Yaegi plugins from plugins/*.go
}
const skillsDir = ".agents/skills"
@@ -95,7 +97,11 @@ func Discover(dir string) (map[string]Skill, error) {
if err != nil {
return nil, fmt.Errorf("parse %s: %w", skillPath, err)
}
out[name] = Skill{Frontmatter: *fm, Body: body, Dir: name}
plugins, err := DiscoverPlugins(dir, name)
if err != nil {
return nil, fmt.Errorf("plugins %s: %w", name, err)
}
out[name] = Skill{Frontmatter: *fm, Body: body, Dir: name, Plugins: plugins}
}
return out, nil
}