1package skills
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "sync"
8
9 "github.com/patriceckhart/zot/internal/core"
10 "github.com/patriceckhart/zot/internal/provider"
11)
12
13// Tool implements core.Tool, exposing a `skill` tool the LLM can call
14// to load the body of a discovered skill on demand. The system-prompt
15// addendum lists the available names; this tool returns the full
16// markdown for the requested one.
17//
18// The list of skills is held behind a mutex so tests / future
19// /reload-skills wiring can swap in a fresh set without races.
20type Tool struct {
21 mu sync.RWMutex
22 skills []*Skill
23}
24
25// NewTool returns a skill loader tool seeded with the given skills.
26// Pass the slice from Discover().
27func NewTool(skills []*Skill) *Tool { return &Tool{skills: skills} }
28
29// SetSkills atomically replaces the underlying skill set. Used when
30// the user re-runs discovery (e.g. after editing a SKILL.md).
31func (t *Tool) SetSkills(s []*Skill) {
32 t.mu.Lock()
33 t.skills = s
34 t.mu.Unlock()
35}
36
37// Skills returns a snapshot for callers that need to render the
38// current set (e.g. the /skills picker).
39func (t *Tool) Skills() []*Skill {
40 t.mu.RLock()
41 defer t.mu.RUnlock()
42 out := make([]*Skill, len(t.skills))
43 copy(out, t.skills)
44 return out
45}
46
47// ---- core.Tool implementation ----
48
49// Name is the LLM-facing tool name.
50func (*Tool) Name() string { return "skill" }
51
52// Description tells the model what this tool does. Kept blunt so the
53// model reliably uses it instead of guessing what a "skill" is.
54func (*Tool) Description() string {
55 return "Load the full body of a named skill. Use this when the user's request matches one of the skills listed in the system prompt; the tool returns the skill's instructions, which you should then follow."
56}
57
58// Schema is one required string parameter: the skill name.
59func (*Tool) Schema() json.RawMessage {
60 return json.RawMessage(`{
61 "type": "object",
62 "properties": {
63 "name": {
64 "type": "string",
65 "description": "The skill name (must match one listed in the system prompt)."
66 }
67 },
68 "required": ["name"]
69 }`)
70}
71
72// Execute returns the markdown body of the requested skill.
73func (t *Tool) Execute(ctx context.Context, args json.RawMessage, progress func(string)) (core.ToolResult, error) {
74 var in struct {
75 Name string `json:"name"`
76 }
77 if err := json.Unmarshal(args, &in); err != nil {
78 return core.ToolResult{
79 IsError: true,
80 Content: []provider.Content{provider.TextBlock{Text: "skill: invalid args: " + err.Error()}},
81 }, nil
82 }
83 if in.Name == "" {
84 return core.ToolResult{
85 IsError: true,
86 Content: []provider.Content{provider.TextBlock{Text: "skill: name is required"}},
87 }, nil
88 }
89
90 t.mu.RLock()
91 s := FindByName(t.skills, in.Name)
92 t.mu.RUnlock()
93 if s == nil {
94 return core.ToolResult{
95 IsError: true,
96 Content: []provider.Content{provider.TextBlock{Text: fmt.Sprintf("skill: no skill named %q (run /skills in zot to see what's available)", in.Name)}},
97 }, nil
98 }
99
100 header := fmt.Sprintf("# Skill: %s\n\n%s\n\n---\n\n", s.Name, s.Description)
101 return core.ToolResult{
102 Content: []provider.Content{provider.TextBlock{Text: header + s.Body}},
103 Details: map[string]any{
104 "skill": s.Name,
105 "path": s.Path,
106 },
107 }, nil
108}