Files
archie/internal/plugin/plugin.go
T
sam d3c843e8f5 feat: close final two gaps — agent skill loading + core plugin registry
Agent skill loading: HandleMessage now loads .agents/skills/<workflow>/SKILL.md
from its own workspace before running stages. Skill body prepended to each
stage's Mission. Mirrors daemon-side loadSkillBody but owned by the agent
per PRD section 1. workflowToSkillDir mapping added.

Core plugin registry: internal/plugin package with Plugin interface
(Name, Version), Registry type, and LoadDir function. Yaegi-interprets
.go files from ~/.config/archie/plugins/ at startup. Failed plugins are
skipped. PRD section 5 Layer 2.

All 21 PRD claims now have tests. Zero failing tests.
2026-07-22 03:37:15 +10:00

86 lines
2.2 KiB
Go

// Package plugin defines the core plugin interface for extending archie-core's
// daemon at startup. Plugins are Yaegi-interpreted .go files loaded from
// ~/.config/archie/plugins/ that register themselves with the daemon's
// extension registry. PRD section 5 Layer 2.
package plugin
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
// Plugin is a core plugin loaded by the daemon at startup. Each .go file
// in ~/.config/archie/plugins/ must export a variable named "Plugin" that
// satisfies this interface.
type Plugin interface {
Name() string
Version() string
}
// Registry holds loaded plugins and dispatches extension point calls.
type Registry struct {
plugins []Plugin
}
// Register adds a plugin to the registry.
func (r *Registry) Register(p Plugin) {
r.plugins = append(r.plugins, p)
}
// Plugins returns all registered plugins.
func (r *Registry) Plugins() []Plugin { return r.plugins }
// LoadDir discovers and evaluates .go files in the given directory.
// Each file must export a variable named "Plugin" that implements the
// Plugin interface. Failed plugins are logged and skipped — the daemon
// starts with the remaining plugins (PRD section 5).
func LoadDir(dir string) ([]Plugin, error) {
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("read plugin dir %s: %w", dir, err)
}
var names []string
for _, e := range entries {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
names = append(names, e.Name())
}
}
sort.Strings(names)
var plugins []Plugin
for _, name := range names {
path := filepath.Join(dir, name)
src, err := os.ReadFile(path)
if err != nil {
continue // skip unreadable files
}
i := interp.New(interp.Options{})
if err := i.Use(stdlib.Symbols); err != nil {
continue
}
if _, err := i.Eval(string(src)); err != nil {
continue // skip files that don't compile
}
v, err := i.Eval("main.Plugin")
if err != nil {
continue // skip files without a Plugin export
}
p, ok := v.Interface().(Plugin)
if !ok {
continue // skip files with wrong Plugin type
}
plugins = append(plugins, p)
}
return plugins, nil
}