-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject_copilot.go
More file actions
174 lines (150 loc) · 5.16 KB
/
project_copilot.go
File metadata and controls
174 lines (150 loc) · 5.16 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
package main
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"text/template"
)
// CopilotProjector generates GitHub Copilot platform files.
type CopilotProjector struct{}
func (p *CopilotProjector) Name() string { return "copilot" }
func (p *CopilotProjector) OutputPaths(rules, skills, agents []string, hasMCP bool) []string {
paths := []string{"AGENTS.md", ".github/copilot-instructions.md", ".github/hooks/lore.json"}
for _, n := range rules {
paths = append(paths, ".github/instructions/"+n+".instructions.md")
}
for _, n := range skills {
paths = append(paths, ".github/skills/"+n+"/", ".github/skills/"+n+"/SKILL.md")
}
for _, n := range agents {
paths = append(paths, ".github/agents/"+n+".agent.md")
}
return paths
}
func (p *CopilotProjector) Project(root string, ms *MergedSet) error {
// Rules → .github/instructions/<name>.instructions.md
for _, name := range sortedKeys(ms.Rules) {
rule := ms.Rules[name]
if err := p.writeRule(root, name, rule); err != nil {
return err
}
}
// Skills → .github/skills/<name>/SKILL.md
if err := projectSkills(filepath.Join(root, ".github"), ms); err != nil {
return err
}
// Agents → .github/agents/<name>.agent.md
for _, name := range sortedKeys(ms.Agents) {
agent := ms.Agents[name]
if err := p.writeAgent(root, agent); err != nil {
return err
}
}
// .github/copilot-instructions.md
if err := p.writeCopilotInstructions(root, ms); err != nil {
return err
}
// .github/hooks/lore.json
if err := p.writeHooks(root); err != nil {
return err
}
// AGENTS.md at root
return writeAGENTSMD(root, ms)
}
func (p *CopilotProjector) writeRule(root, name string, rule *AgenticFile) error {
fm := map[string]interface{}{}
if rule.Description != "" {
fm["description"] = rule.Description
}
if len(rule.Globs) > 0 {
fm["applyTo"] = rule.Globs
}
content := renderFrontmatter(fm) + "\n" + rule.Body + "\n"
path := filepath.Join(root, ".github", "instructions", name+".instructions.md")
return writeFile(path, []byte(content))
}
func (p *CopilotProjector) writeAgent(root string, agent *AgenticFile) error {
fm := map[string]interface{}{}
if agent.Description != "" {
fm["description"] = agent.Description
}
if len(agent.Tools) > 0 {
fm["tools"] = agent.Tools
}
if len(agent.Skills) > 0 {
fm["handoffs"] = agent.Skills
}
content := renderFrontmatter(fm) + "\n" + agent.Body + "\n"
path := filepath.Join(root, ".github", "agents", agent.Name+".agent.md")
return writeFile(path, []byte(content))
}
func (p *CopilotProjector) writeCopilotInstructions(root string, ms *MergedSet) error {
tmplData, err := templateFS.ReadFile("templates/copilot-instructions.md.tmpl")
if err != nil {
return fmt.Errorf("read copilot template: %w", err)
}
tmpl, err := template.New("copilot").Parse(string(tmplData))
if err != nil {
return fmt.Errorf("parse copilot template: %w", err)
}
type ruleInfo struct {
Name string
Body string
}
var ruleList []ruleInfo
for _, name := range sortedKeys(ms.Rules) {
r := ms.Rules[name]
ruleList = append(ruleList, ruleInfo{Name: r.Name, Body: r.Body})
}
var sb strings.Builder
if err := tmpl.Execute(&sb, struct {
LoreMD string
Rules []ruleInfo
}{ms.LoreMD, ruleList}); err != nil {
return fmt.Errorf("execute copilot template: %w", err)
}
return writeFile(filepath.Join(root, ".github", "copilot-instructions.md"), []byte(sb.String()))
}
// copilotHooksConfig matches .github/hooks/<name>.json format.
// Uses Coding Agent schema (camelCase, version field) for GA compatibility.
// VS Code agent mode also reads hooks from .claude/settings.json (projected separately).
type copilotHooksConfig struct {
Version int `json:"version"`
Hooks copilotHooksEvents `json:"hooks"`
}
type copilotHooksEvents struct {
PreToolUse []copilotHookEntry `json:"preToolUse,omitempty"`
PostToolUse []copilotHookEntry `json:"postToolUse,omitempty"`
UserPromptSubmitted []copilotHookEntry `json:"userPromptSubmitted,omitempty"`
SessionStart []copilotHookEntry `json:"sessionStart,omitempty"`
AgentStop []copilotHookEntry `json:"agentStop,omitempty"`
SessionEnd []copilotHookEntry `json:"sessionEnd,omitempty"`
SubagentStop []copilotHookEntry `json:"subagentStop,omitempty"`
}
type copilotHookEntry struct {
Type string `json:"type"`
Bash string `json:"bash"`
}
func (p *CopilotProjector) writeHooks(root string) error {
h := func(cmd string) []copilotHookEntry {
return []copilotHookEntry{{Type: "command", Bash: cmd}}
}
cfg := copilotHooksConfig{
Version: 1,
Hooks: copilotHooksEvents{
PreToolUse: h("lore hook pre-tool-use"),
PostToolUse: h("lore hook post-tool-use"),
UserPromptSubmitted: h("lore hook prompt-submit"),
SessionStart: h("lore hook session-start"),
AgentStop: h("lore hook stop"),
SessionEnd: h("lore hook session-end"),
SubagentStop: h("lore hook subagent-stop"),
},
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("marshal copilot hooks: %w", err)
}
return writeFile(filepath.Join(root, ".github", "hooks", "lore.json"), append(data, '\n'))
}