1package skills
2
3import (
4 "fmt"
5 "sort"
6 "strings"
7)
8
9// Pins records independent global and project selections by discovered name.
10type Pins struct {
11 Global []string `json:"global,omitempty"`
12 Project []string `json:"project,omitempty"`
13}
14
15// Resolve returns each available pinned skill once, ordered by canonical
16// skill name (then path). A pin may be a skill's name or one of its aliases;
17// two pins that reach the same skill (for example a canonical name and its
18// slash alias, or the same name in both scopes) select it once. Names that
19// match nothing produce one warning each, in name order.
20func (p Pins) Resolve(available []*Skill) ([]*Skill, []string) {
21 names := make(map[string]bool)
22 for _, name := range append(append([]string(nil), p.Global...), p.Project...) {
23 if name != "" {
24 names[name] = true
25 }
26 }
27 sorted := make([]string, 0, len(names))
28 for name := range names {
29 sorted = append(sorted, name)
30 }
31 sort.Strings(sorted)
32 var selected []*Skill
33 chosen := make(map[*Skill]bool)
34 var warnings []string
35 for _, name := range sorted {
36 s := FindByName(available, name)
37 if s == nil {
38 warnings = append(warnings, fmt.Sprintf("pinned skill %q is unavailable", name))
39 continue
40 }
41 if !chosen[s] {
42 chosen[s] = true
43 selected = append(selected, s)
44 }
45 }
46 sort.SliceStable(selected, func(i, j int) bool {
47 if selected[i].Name != selected[j].Name {
48 return selected[i].Name < selected[j].Name
49 }
50 return selected[i].Path < selected[j].Path
51 })
52 return selected, warnings
53}
54
55// Scopes reports which scopes pin s, resolving aliases the same way Resolve
56// does: a pin counts when its name resolves to s among available.
57func (p Pins) Scopes(available []*Skill, s *Skill) (project, global bool) {
58 hit := func(names []string) bool {
59 for _, name := range names {
60 if name != "" && FindByName(available, name) == s {
61 return true
62 }
63 }
64 return false
65 }
66 return hit(p.Project), hit(p.Global)
67}
68
69// PreloadPrompt includes pinned instructions with the first real request.
70// It creates no synthetic tool calls and does not start a model turn.
71func PreloadPrompt(selected []*Skill, request string) string {
72 if len(selected) == 0 {
73 return request
74 }
75 var b strings.Builder
76 b.WriteString("Pinned skills for this conversation. Apply these instructions where relevant to the user's requests.\n\n")
77 for _, s := range selected {
78 b.WriteString(InvocationPrompt(s, ""))
79 b.WriteString("\n\n")
80 }
81 b.WriteString("---\n\nUser request:\n")
82 b.WriteString(request)
83 return b.String()
84}