package stage import ( "fmt" "strconv" "strings" "git.a-wan.net/alan/pipelines/internal/skills" ) // SkillRef is one entry of a model stage's `skills` block: a server skill // library name, pinned to Version when it is positive and otherwise resolved to // the library's latest version when the run starts. type SkillRef struct { Name string Version int } // String renders the entry as written in a definition: name or name@N. func (r SkillRef) String() string { if r.Version > 0 { return r.Name + "@" + strconv.Itoa(r.Version) } return r.Name } // ParseSkillRef parses one `name` or `name@N` entry. N must be a positive // integer without sign or leading zeros. func ParseSkillRef(text string) (SkillRef, error) { text = strings.TrimSpace(text) name, version, pinned := strings.Cut(text, "@") if err := skills.ValidateName(name); err != nil { return SkillRef{}, fmt.Errorf("invalid skill %q: %w", text, err) } ref := SkillRef{Name: name} if pinned { n, err := strconv.Atoi(version) if err != nil || n <= 0 || strconv.Itoa(n) != version { return SkillRef{}, fmt.Errorf("invalid skill %q: the version after @ must be a positive integer", text) } ref.Version = n } return ref, nil } // SkillRefs parses a model stage's optional `skills` block, one entry per // line. An absent or empty block configures no skills. A skill may appear // only once, pinned or not. func SkillRefs(def Definition) ([]SkillRef, error) { text, present := def.Blocks["skills"] if !present { return nil, nil } var refs []SkillRef seen := map[string]bool{} for _, line := range strings.Split(text, "\n") { if strings.TrimSpace(line) == "" { continue } ref, err := ParseSkillRef(line) if err != nil { return nil, err } if seen[ref.Name] { return nil, fmt.Errorf("duplicate skill %s", ref.Name) } seen[ref.Name] = true refs = append(refs, ref) } return refs, nil } // errSkillsModelOnly is the clear diagnostic for a skills block on a stage that // has no model session to load skills into. func errSkillsModelOnly(kind string) error { return fmt.Errorf("skills blocks are supported only on model stages, not %s stages", kind) } // ClaudeSkillPlugin is the name of the per-visit plugin a Claude stage's // configured skills are loaded from; Claude Code names its skills // ClaudeSkillPlugin:. const ClaudeSkillPlugin = "pipelines-stage" // SkillBundle is one resolved skill copied for a single stage visit. Dir holds // the bundle (SKILL.md and its references/, scripts/ and assets/). type SkillBundle struct { Name string Version int Dir string } // SkillSet is the runtime-provided skill material for one model visit. Root is // the visit's private skills directory: it is also a Claude Code plugin root // (.claude-plugin/plugin.json) whose skills// entries are the bundles. // A nil or empty set means the stage configured no skills. type SkillSet struct { Root string Bundles []SkillBundle } // Empty reports whether the set configures no skills. func (s *SkillSet) Empty() bool { return s == nil || len(s.Bundles) == 0 } // Names lists name@version for every bundle, in definition order. func (s *SkillSet) Names() []string { if s.Empty() { return nil } names := make([]string, 0, len(s.Bundles)) for _, b := range s.Bundles { names = append(names, b.Name+"@"+strconv.Itoa(b.Version)) } return names }