-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject_opencode.go
More file actions
171 lines (147 loc) · 4.96 KB
/
project_opencode.go
File metadata and controls
171 lines (147 loc) · 4.96 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
// OpenCodeProjector generates OpenCode platform files.
// OpenCode reads .claude/ dirs natively, so projectClaudeDir handles shared output.
// It also has its own .opencode/ directory for native skills/agents.
type OpenCodeProjector struct{}
func (p *OpenCodeProjector) Name() string { return "opencode" }
func (p *OpenCodeProjector) OutputPaths(rules, skills, agents []string, hasMCP bool) []string {
// OpenCode shares .claude/ directory + has its own .opencode/
paths := []string{"AGENTS.md", ".opencode/plugins/lore-hooks.mjs"}
if hasMCP {
paths = append(paths, "opencode.json")
}
for _, n := range rules {
paths = append(paths, ".claude/rules/"+n+".md")
}
for _, n := range skills {
paths = append(paths, ".claude/skills/"+n+"/", ".claude/skills/"+n+"/SKILL.md")
paths = append(paths, ".opencode/skills/"+n+"/", ".opencode/skills/"+n+"/SKILL.md")
paths = append(paths, ".opencode/commands/"+n+".md")
}
for _, n := range agents {
paths = append(paths, ".claude/agents/"+n+".md")
paths = append(paths, ".opencode/agents/"+n+".md")
}
return paths
}
func (p *OpenCodeProjector) Project(root string, ms *MergedSet) error {
// Shared .claude/ directory — OpenCode reads these natively
if err := projectClaudeDir(root, ms); err != nil {
return err
}
// Also project to .opencode/ native directories
opencodeDir := filepath.Join(root, ".opencode")
if err := projectSkills(opencodeDir, ms); err != nil {
return err
}
if err := projectAgentsRecord(opencodeDir, ms); err != nil {
return err
}
// AGENTS.md at root (OpenCode mandate file)
if err := writeAGENTSMD(root, ms); err != nil {
return err
}
// MCP: OpenCode expects MCP servers in opencode.json under "mcp".
if len(ms.MCP) > 0 {
if err := p.writeMCPConfig(root, ms.MCP); err != nil {
return err
}
}
// User-invocable skills → .opencode/commands/<name>.md (OpenCode slash commands)
// $ARGUMENTS is already the native OpenCode convention.
for _, skill := range userInvocableSkills(ms) {
fm := map[string]interface{}{}
if skill.Description != "" {
fm["description"] = skill.Description
}
content := renderFrontmatter(fm) + "\n" + skill.Body + "\n"
path := filepath.Join(root, ".opencode", "commands", skill.Name+".md")
if err := writeFile(path, []byte(content)); err != nil {
return fmt.Errorf("write opencode command %s: %w", skill.Name, err)
}
}
// Hooks: OpenCode uses JS plugins, not JSON config.
// Generate a stub plugin that shells out to `lore hook`.
return p.writeHookPlugin(root)
}
func (p *OpenCodeProjector) writeMCPConfig(root string, servers []MCPServer) error {
configPath := filepath.Join(root, "opencode.json")
config := map[string]interface{}{}
if data, err := os.ReadFile(configPath); err == nil {
cleaned := stripJSONComments(data)
if err := json.Unmarshal(cleaned, &config); err != nil {
return fmt.Errorf("parse opencode.json: %w", err)
}
}
if _, ok := config["$schema"]; !ok {
config["$schema"] = "https://opencode.ai/config.json"
}
merged := map[string]interface{}{}
if raw, ok := config["mcp"]; ok {
if existing, ok := raw.(map[string]interface{}); ok {
for name, entry := range existing {
merged[name] = entry
}
}
}
for _, s := range servers {
command := make([]string, 0, 1+len(s.Args))
command = append(command, s.Command)
command = append(command, s.Args...)
entry := map[string]interface{}{
"type": "local",
"command": command,
"enabled": true,
}
if len(s.Env) > 0 {
entry["environment"] = s.Env
}
merged[s.Name] = entry
}
config["mcp"] = merged
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("marshal opencode.json: %w", err)
}
if err := writeFile(configPath, append(data, '\n')); err != nil {
return fmt.Errorf("write opencode.json: %w", err)
}
return nil
}
func (p *OpenCodeProjector) writeHookPlugin(root string) error {
plugin := `// Lore hook plugin for OpenCode
// Generated by lore generate — do not edit manually.
import { execSync } from "child_process";
function loreHook(name, ctx) {
try {
const input = JSON.stringify(ctx);
execSync("lore hook " + name, { input, stdio: ["pipe", "pipe", "inherit"] });
} catch (_) {}
}
export default {
name: "lore-hooks",
hooks: {
"tool.execute.before": (ctx) => loreHook("pre-tool-use", ctx),
"tool.execute.after": (ctx) => loreHook("post-tool-use", ctx),
"chat.message": (ctx) => loreHook("prompt-submit", ctx),
"stop": (ctx) => loreHook("stop", ctx),
"experimental.session.compacting": (ctx) => loreHook("pre-compact", ctx),
},
event: (event) => {
if (event.type === "session.created") loreHook("session-start", event);
if (event.type === "session.deleted") loreHook("session-end", event);
},
};
`
path := filepath.Join(root, ".opencode", "plugins", "lore-hooks.mjs")
if err := writeFile(path, []byte(plugin)); err != nil {
return fmt.Errorf("write opencode hook plugin: %w", err)
}
return nil
}