-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfrontmatter.go
More file actions
227 lines (180 loc) · 7.77 KB
/
frontmatter.go
File metadata and controls
227 lines (180 loc) · 7.77 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package markdown
import (
"encoding/json"
"fmt"
"github.com/kitproj/coding-context-cli/pkg/codingcontext/mcp"
"gopkg.in/yaml.v3"
)
// BaseFrontMatter represents parsed YAML frontmatter from markdown files
type BaseFrontMatter struct {
// Name is the skill identifier
// Must be 1-64 characters, lowercase alphanumeric and hyphens only
Name string `yaml:"name,omitempty" json:"name,omitempty"`
// Description explains what the prompt does and when to use it
// Must be 1-1024 characters
Description string `yaml:"description,omitempty" json:"description,omitempty"`
Content map[string]any `json:"-" yaml:",inline"`
}
type baseFrontMatterRaw struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Content map[string]any `yaml:",inline"`
}
// UnmarshalYAML ensures inline fields are properly captured in Content.
func (b *BaseFrontMatter) UnmarshalYAML(value *yaml.Node) error {
var raw baseFrontMatterRaw
if err := value.Decode(&raw); err != nil {
return err
}
b.Name = raw.Name
b.Description = raw.Description
b.Content = raw.Content
if raw.Content == nil {
b.Content = make(map[string]any)
}
return nil
}
// TaskFrontMatter represents the standard frontmatter fields for task files
type TaskFrontMatter struct {
BaseFrontMatter `yaml:",inline"`
// Agent specifies the default agent if not specified via -a flag
// This is not used for selecting tasks or rules, only as a default
Agent string `yaml:"agent,omitempty" json:"agent,omitempty"`
// Languages specifies the programming language(s) for filtering rules
// Array of languages for OR logic (e.g., ["go", "python"])
Languages []string `yaml:"languages,omitempty" json:"languages,omitempty"`
// Model specifies the AI model identifier
// Does not filter rules, metadata only
Model string `yaml:"model,omitempty" json:"model,omitempty"`
// SingleShot indicates whether the task runs once or multiple times
// Does not filter rules, metadata only
SingleShot bool `yaml:"single_shot,omitempty" json:"single_shot,omitempty"`
// Timeout specifies the task timeout in time.Duration format (e.g., "10m", "1h")
// Does not filter rules, metadata only
Timeout string `yaml:"timeout,omitempty" json:"timeout,omitempty"`
// Resume indicates if this task should be resumed
Resume bool `yaml:"resume,omitempty" json:"resume,omitempty"`
// Selectors contains additional custom selectors for filtering rules
Selectors map[string]any `yaml:"selectors,omitempty" json:"selectors,omitempty"`
// ExpandParams controls whether parameter expansion should occur
// Defaults to true if not specified
ExpandParams *bool `yaml:"expand,omitempty" json:"expand,omitempty"`
}
// UnmarshalJSON custom unmarshaler that populates both typed fields and Content map
func (t *TaskFrontMatter) UnmarshalJSON(data []byte) error {
// First unmarshal into a temporary type to avoid infinite recursion
type Alias TaskFrontMatter
aux := &struct {
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, aux); err != nil {
return fmt.Errorf("failed to unmarshal task frontmatter: %w", err)
}
// Also unmarshal into Content map
if err := json.Unmarshal(data, &t.BaseFrontMatter.Content); err != nil {
return fmt.Errorf("failed to unmarshal task frontmatter content: %w", err)
}
return nil
}
// CommandFrontMatter represents the frontmatter fields for command files.
// Previously this was an empty placeholder struct, but now supports the expand field
// to control parameter expansion behavior in command content.
type CommandFrontMatter struct {
BaseFrontMatter `yaml:",inline"`
// ExpandParams controls whether parameter expansion should occur
// Defaults to true if not specified
ExpandParams *bool `yaml:"expand,omitempty" json:"expand,omitempty"`
// Selectors contains additional custom selectors for filtering rules
// When a command is used in a task, its selectors are combined with task selectors
Selectors map[string]any `yaml:"selectors,omitempty" json:"selectors,omitempty"`
}
// UnmarshalJSON custom unmarshaler that populates both typed fields and Content map
func (c *CommandFrontMatter) UnmarshalJSON(data []byte) error {
// First unmarshal into a temporary type to avoid infinite recursion
type Alias CommandFrontMatter
aux := &struct {
*Alias
}{
Alias: (*Alias)(c),
}
if err := json.Unmarshal(data, aux); err != nil {
return fmt.Errorf("failed to unmarshal command frontmatter: %w", err)
}
// Also unmarshal into Content map
if err := json.Unmarshal(data, &c.BaseFrontMatter.Content); err != nil {
return fmt.Errorf("failed to unmarshal command frontmatter content: %w", err)
}
return nil
}
// RuleFrontMatter represents the standard frontmatter fields for rule files
type RuleFrontMatter struct {
BaseFrontMatter `yaml:",inline"`
// TaskNames specifies which task(s) this rule applies to
// Array of task names for OR logic
TaskNames []string `yaml:"task_names,omitempty" json:"task_names,omitempty"`
// Languages specifies which programming language(s) this rule applies to
// Array of languages for OR logic (e.g., ["go", "python"])
Languages []string `yaml:"languages,omitempty" json:"languages,omitempty"`
// Agent specifies which AI agent this rule is intended for
Agent string `yaml:"agent,omitempty" json:"agent,omitempty"`
// MCPServer specifies a single MCP server configuration
// Metadata only, does not filter
MCPServer mcp.MCPServerConfig `yaml:"mcp_server,omitempty" json:"mcp_server,omitempty"`
// ExpandParams controls whether parameter expansion should occur
// Defaults to true if not specified
ExpandParams *bool `yaml:"expand,omitempty" json:"expand,omitempty"`
// Bootstrap contains a shell script to execute before including the rule
// This is preferred over file-based bootstrap scripts
Bootstrap string `yaml:"bootstrap,omitempty" json:"bootstrap,omitempty"`
}
// UnmarshalJSON custom unmarshaler that populates both typed fields and Content map
func (r *RuleFrontMatter) UnmarshalJSON(data []byte) error {
// First unmarshal into a temporary type to avoid infinite recursion
type Alias RuleFrontMatter
aux := &struct {
*Alias
}{
Alias: (*Alias)(r),
}
if err := json.Unmarshal(data, aux); err != nil {
return fmt.Errorf("failed to unmarshal rule frontmatter: %w", err)
}
// Also unmarshal into Content map
if err := json.Unmarshal(data, &r.BaseFrontMatter.Content); err != nil {
return fmt.Errorf("failed to unmarshal rule frontmatter content: %w", err)
}
return nil
}
// SkillFrontMatter represents the standard frontmatter fields for skill files
type SkillFrontMatter struct {
BaseFrontMatter `yaml:",inline"`
// License specifies the license applied to the skill (optional)
License string `yaml:"license,omitempty" json:"license,omitempty"`
// Compatibility indicates environment requirements (optional)
// Max 500 characters
Compatibility string `yaml:"compatibility,omitempty" json:"compatibility,omitempty"`
// Metadata contains arbitrary key-value pairs (optional)
Metadata map[string]string `yaml:"metadata,omitempty" json:"metadata,omitempty"`
// AllowedTools is a space-delimited list of pre-approved tools (optional, experimental)
AllowedTools string `yaml:"allowed-tools,omitempty" json:"allowed-tools,omitempty"`
}
// UnmarshalJSON custom unmarshaler that populates both typed fields and Content map
func (s *SkillFrontMatter) UnmarshalJSON(data []byte) error {
// First unmarshal into a temporary type to avoid infinite recursion
type Alias SkillFrontMatter
aux := &struct {
*Alias
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, aux); err != nil {
return fmt.Errorf("failed to unmarshal skill frontmatter: %w", err)
}
// Also unmarshal into Content map
if err := json.Unmarshal(data, &s.BaseFrontMatter.Content); err != nil {
return fmt.Errorf("failed to unmarshal skill frontmatter content: %w", err)
}
return nil
}