tool.go RawHistoryFolder
1package skills
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "sync"
8
9 "github.com/patriceckhart/zot/packages/core"
10 "github.com/patriceckhart/zot/packages/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 a named skill's instructions. Use when the user's request matches a skill listed above."
56}
57
58// Schema is one required string parameter: the skill name.
59func (*Tool) Schema() json.RawMessage {
60 return json.RawMessage(`{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}`)
61}
62
63// Execute returns the markdown body of the requested skill.
64func (t *Tool) Execute(ctx context.Context, args json.RawMessage, progress func(string)) (core.ToolResult, error) {
65 var in struct {
66 Name string `json:"name"`
67 }
68 if err := json.Unmarshal(args, &in); err != nil {
69 return core.ToolResult{
70 IsError: true,
71 Content: []provider.Content{provider.TextBlock{Text: "skill: invalid args: " + err.Error()}},
72 }, nil
73 }
74 if in.Name == "" {
75 return core.ToolResult{
76 IsError: true,
77 Content: []provider.Content{provider.TextBlock{Text: "skill: name is required"}},
78 }, nil
79 }
80
81 t.mu.RLock()
82 s := FindByName(t.skills, in.Name)
83 t.mu.RUnlock()
84 if s == nil {
85 return core.ToolResult{
86 IsError: true,
87 Content: []provider.Content{provider.TextBlock{Text: fmt.Sprintf("skill: no skill named %q (run /skills in zot to see what's available)", in.Name)}},
88 }, nil
89 }
90
91 header := fmt.Sprintf("# Skill: %s\n\n%s\n\n---\n\n", s.Name, s.Description)
92 return core.ToolResult{
93 Content: []provider.Content{provider.TextBlock{Text: header + s.Body}},
94 Details: map[string]any{
95 "skill": s.Name,
96 "path": s.Path,
97 },
98 }, nil
99}