-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathskill_doc.go
More file actions
194 lines (172 loc) · 5.28 KB
/
skill_doc.go
File metadata and controls
194 lines (172 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package toolfs
import (
"embed"
"fmt"
"io/fs"
"path/filepath"
"strings"
)
//go:embed skills/*.md skills/*/*.md
var skillDocsFS embed.FS
// SkillDocument represents a SKILL.md document
type SkillDocument struct {
Name string `json:"name"`
Description string `json:"description"`
Content string `json:"content"`
Metadata map[string]interface{} `json:"metadata"`
Path string `json:"path"` // File path for reference
}
// SkillDocumentManager manages skill documents from executors and filesystem
type SkillDocumentManager struct {
documents map[string]*SkillDocument // executor name or path -> document
executors map[string]SkillExecutor // executor name -> executor
}
// NewSkillDocumentManager creates a new skill document manager
func NewSkillDocumentManager() *SkillDocumentManager {
return &SkillDocumentManager{
documents: make(map[string]*SkillDocument),
executors: make(map[string]SkillExecutor),
}
}
// RegisterExecutor registers an executor and extracts its skill document if available
func (sdm *SkillDocumentManager) RegisterExecutor(executor SkillExecutor) error {
name := executor.Name()
sdm.executors[name] = executor
// Check if executor implements SkillDocumentProvider
if provider, ok := executor.(SkillDocumentProvider); ok {
content := provider.GetSkillDocument()
if content != "" {
doc, err := sdm.parseSkillDocument(content)
if err != nil {
return fmt.Errorf("failed to parse skill document for skill %s: %w", name, err)
}
doc.Path = fmt.Sprintf("skill:%s", name)
sdm.documents[name] = doc
}
}
return nil
}
// RegisterDocument registers a skill document from filesystem or other source
func (sdm *SkillDocumentManager) RegisterDocument(path string, content string) error {
doc, err := sdm.parseSkillDocument(content)
if err != nil {
return fmt.Errorf("failed to parse skill document at %s: %w", path, err)
}
doc.Path = path
// Use parent directory name if filename is SKILL.md
key := filepath.Base(path)
if strings.HasSuffix(key, ".md") {
key = strings.TrimSuffix(key, ".md")
}
if strings.ToUpper(key) == "SKILL" {
dir := filepath.Dir(path)
if dir != "." && dir != "skills" {
key = filepath.Base(dir)
}
}
sdm.documents[key] = doc
return nil
}
// GetDocument retrieves a skill document by skill name or path key
func (sdm *SkillDocumentManager) GetDocument(key string) (*SkillDocument, error) {
doc, exists := sdm.documents[key]
if !exists {
return nil, fmt.Errorf("skill document not found: %s", key)
}
return doc, nil
}
// ListDocuments returns all registered skill documents
func (sdm *SkillDocumentManager) ListDocuments() []*SkillDocument {
docs := make([]*SkillDocument, 0, len(sdm.documents))
for _, doc := range sdm.documents {
docs = append(docs, doc)
}
return docs
}
// ListDocumentNames returns all registered skill document names/keys
func (sdm *SkillDocumentManager) ListDocumentNames() []string {
names := make([]string, 0, len(sdm.documents))
for name := range sdm.documents {
names = append(names, name)
}
return names
}
// LoadBuiltinSkillDocs loads skill documents from embedded filesystem
func (sdm *SkillDocumentManager) LoadBuiltinSkillDocs() error {
return fs.WalkDir(skillDocsFS, "skills", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if !strings.HasSuffix(path, "SKILL.md") && !strings.HasSuffix(path, ".md") {
return nil
}
content, err := skillDocsFS.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read skill doc %s: %w", path, err)
}
return sdm.RegisterDocument(path, string(content))
})
}
// parseSkillDocument parses a SKILL.md document and extracts front matter and content
func (sdm *SkillDocumentManager) parseSkillDocument(content string) (*SkillDocument, error) {
doc := &SkillDocument{
Content: content,
Metadata: make(map[string]interface{}),
}
// Parse front matter if present
lines := strings.Split(content, "\n")
if len(lines) > 0 && strings.TrimSpace(lines[0]) == "---" {
// Extract front matter
var frontMatter []string
endIdx := -1
for i := 1; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) == "---" {
endIdx = i
break
}
frontMatter = append(frontMatter, lines[i])
}
if endIdx > 0 {
// Parse front matter (simple YAML-like parser)
for _, line := range frontMatter {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.Trim(strings.TrimSpace(parts[1]), "\"'")
switch key {
case "name":
doc.Name = value
case "description":
doc.Description = value
default:
if doc.Metadata == nil {
doc.Metadata = make(map[string]interface{})
}
doc.Metadata[key] = value
}
}
}
// Extract content after front matter
if endIdx+1 < len(lines) {
doc.Content = strings.Join(lines[endIdx+1:], "\n")
}
}
}
// If name not in front matter, try to extract from first heading
if doc.Name == "" {
for _, line := range lines {
if strings.HasPrefix(line, "# ") {
doc.Name = strings.TrimSpace(strings.TrimPrefix(line, "# "))
break
}
}
}
return doc, nil
}