-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
380 lines (336 loc) · 10.5 KB
/
config.go
File metadata and controls
380 lines (336 loc) · 10.5 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// readProjectConfig reads .lore/config.json and returns platforms.
// Falls back to defaults if the file is missing or malformed.
func readProjectConfig() (platforms map[string]bool, err error) {
platforms = defaultPlatforms()
data, err := os.ReadFile(filepath.Join(".lore", "config.json"))
if err != nil {
return platforms, fmt.Errorf("read config: %w", err)
}
var cfg map[string]interface{}
if err := json.Unmarshal(stripJSONComments(data), &cfg); err != nil {
return platforms, fmt.Errorf("parse config: %w", err)
}
if raw, ok := cfg["platforms"]; ok {
platforms = parsePlatformsConfig(raw)
}
return platforms, nil
}
// --- Hook scripts (dispatcher model) ---
// HookScripts maps hook event names to accumulated script paths.
// All scripts for an event run in parallel. Blocking events (pre-tool-use,
// prompt-submit, stop) fail if any script returns non-zero.
type HookScripts struct {
scripts map[string][]string // event name → ordered list of script paths
}
// allHookEvents lists every canonical hook event name.
var allHookEvents = []string{
"pre-tool-use", "post-tool-use", "prompt-submit",
"session-start", "stop", "pre-compact", "session-end",
"subagent-start", "subagent-stop",
}
// blockingEvents are hook events where a non-zero exit blocks the action.
var blockingEvents = map[string]bool{
"pre-tool-use": true,
"prompt-submit": true,
"stop": true,
"subagent-stop": true,
}
// readHookScripts resolves hook scripts using three-layer accumulation:
//
// Bundle(s) → Global → Project
//
// All layers contribute scripts. They all run in parallel at dispatch time.
func readHookScripts() HookScripts {
hs := HookScripts{scripts: make(map[string][]string)}
// Layer 1: Bundles (in priority order)
for _, slug := range readBundleSlugs() {
hs.appendFromBundle(slug)
}
// Layer 2: Global — ~/.config/lore/HOOKS/<event>.mjs
hs.appendFromDir(filepath.Join(globalPath(), "HOOKS"))
// Layer 3: Project — .lore/HOOKS/<event>.mjs
hs.appendFromDir(filepath.Join(".lore", "HOOKS"))
return hs
}
// ScriptsFor returns all script paths for a given event, or nil.
func (hs HookScripts) ScriptsFor(event string) []string {
return hs.scripts[event]
}
// appendFromDir scans a HOOKS directory for behavior scripts.
// New layout: HOOKS/<event>/<name>.mjs (directory per event, file per behavior).
// Legacy layout: HOOKS/<event>.mjs (single file per event) — still supported.
func (hs *HookScripts) appendFromDir(dir string) {
for _, event := range allHookEvents {
// New layout: HOOKS/<event>/*.mjs
eventDir := filepath.Join(dir, event)
if entries, err := os.ReadDir(eventDir); err == nil {
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".mjs") {
continue
}
absPath, _ := filepath.Abs(filepath.Join(eventDir, e.Name()))
hs.scripts[event] = append(hs.scripts[event], absPath)
}
continue
}
// Legacy layout: HOOKS/<event>.mjs
p := filepath.Join(dir, event+".mjs")
if _, err := os.Stat(p); err == nil {
absPath, _ := filepath.Abs(p)
hs.scripts[event] = append(hs.scripts[event], absPath)
}
}
}
// appendFromBundle reads hook behavior paths from a bundle's manifest.
// Supports array format: [{"name": "...", "script": "..."}]
// and legacy string format: "SCRIPTS/foo.mjs"
func (hs *HookScripts) appendFromBundle(slug string) {
bundleDir := bundleDirForSlug(slug)
if bundleDir == "" {
return
}
data, err := os.ReadFile(filepath.Join(bundleDir, "manifest.json"))
if err != nil {
return
}
var manifest struct {
Hooks map[string]json.RawMessage `json:"hooks"`
}
if json.Unmarshal(data, &manifest) != nil {
return
}
for event, raw := range manifest.Hooks {
// Try array of behavior objects
var behaviors []struct {
Script string `json:"script"`
}
if json.Unmarshal(raw, &behaviors) == nil && len(behaviors) > 0 {
for _, b := range behaviors {
if b.Script != "" {
absPath := filepath.Join(bundleDir, b.Script)
hs.scripts[event] = append(hs.scripts[event], absPath)
}
}
continue
}
// Legacy: plain string
var scriptPath string
if json.Unmarshal(raw, &scriptPath) == nil && scriptPath != "" {
absPath := filepath.Join(bundleDir, scriptPath)
hs.scripts[event] = append(hs.scripts[event], absPath)
}
}
}
// expandHome replaces a leading ~ with the user's home directory.
func expandHome(path string) string {
if !strings.HasPrefix(path, "~") {
return path
}
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[1:])
}
// writeProjectConfig merges platforms into .lore/config.json,
// preserving any other existing fields.
func writeProjectConfig(projectDir string, platforms map[string]bool) error {
configPath := filepath.Join(projectDir, ".lore", "config.json")
// Read existing config to preserve other fields
cfg := make(map[string]interface{})
if existing, err := os.ReadFile(configPath); err == nil {
_ = json.Unmarshal(stripJSONComments(existing), &cfg)
}
// Update only the fields we manage
cfg["platforms"] = orderedPlatformJSON(platforms)
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(configPath, append(data, '\n'), 0644)
}
// readEnabledPlatforms reads .lore/config.json and returns enabled platform names.
func readEnabledPlatforms() ([]string, error) {
platforms, err := readProjectConfig()
if err != nil {
return nil, err
}
return enabledPlatformNames(platforms), nil
}
// enabledPlatformNames returns sorted platform names where value is true.
func enabledPlatformNames(platforms map[string]bool) []string {
var result []string
for _, p := range validPlatforms {
if platforms[p] {
result = append(result, p)
}
}
return result
}
// defaultPlatforms returns the full platform map with all disabled.
func defaultPlatforms() map[string]bool {
m := make(map[string]bool)
for _, p := range validPlatforms {
m[p] = false
}
return m
}
// platformsFromList converts a string slice to a platform map (selected=true, rest=false).
func platformsFromList(selected []string) map[string]bool {
m := defaultPlatforms()
for _, p := range selected {
if _, ok := m[p]; ok {
m[p] = true
}
}
return m
}
// parsePlatformsConfig parses the platforms object from config JSON.
func parsePlatformsConfig(raw interface{}) map[string]bool {
obj, ok := raw.(map[string]interface{})
if !ok {
return defaultPlatforms()
}
m := defaultPlatforms()
for k, v := range obj {
if b, ok := v.(bool); ok {
if _, valid := m[k]; valid {
m[k] = b
}
}
}
return m
}
// sortedPlatformMap returns a consistently ordered JSON-friendly map.
func orderedPlatformJSON(platforms map[string]bool) json.RawMessage {
type entry struct {
Name string
Enabled bool
}
var entries []entry
for _, p := range validPlatforms {
entries = append(entries, entry{p, platforms[p]})
}
// Also include any unknown keys
known := make(map[string]bool)
for _, p := range validPlatforms {
known[p] = true
}
var extras []string
for k := range platforms {
if !known[k] {
extras = append(extras, k)
}
}
sort.Strings(extras)
for _, k := range extras {
entries = append(entries, entry{k, platforms[k]})
}
// Manual JSON build for consistent ordering
buf := []byte("{")
for i, e := range entries {
if i > 0 {
buf = append(buf, ',')
}
key, _ := json.Marshal(e.Name)
val := []byte("false")
if e.Enabled {
val = []byte("true")
}
buf = append(buf, '\n', ' ', ' ', ' ', ' ')
buf = append(buf, key...)
buf = append(buf, ':', ' ')
buf = append(buf, val...)
}
buf = append(buf, '\n', ' ', ' ')
buf = append(buf, '}')
return json.RawMessage(buf)
}
// --- inherit.json ---
// validInheritValues are the valid values for inheritance state.
var validInheritValues = map[string]bool{
"off": true,
"defer": true,
"overwrite": true,
}
// readInheritConfig reads .lore/inherit.json and returns the inheritance map.
// Returns nil if the file doesn't exist or is malformed.
func readInheritConfig(projectDir string) map[string]map[string]string {
data, err := os.ReadFile(filepath.Join(projectDir, ".lore", "inherit.json"))
if err != nil {
return nil
}
var cfg map[string]map[string]string
if err := json.Unmarshal(data, &cfg); err != nil {
return nil
}
return cfg
}
// writeInheritConfig writes .lore/inherit.json with the given inheritance map.
func writeInheritConfig(projectDir string, config map[string]map[string]string) error {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("marshal inherit config: %w", err)
}
return os.WriteFile(filepath.Join(projectDir, ".lore", "inherit.json"), append(data, '\n'), 0644)
}
// enableBundle appends a bundle slug to the "bundles" array in .lore/config.json.
// projectDir is the project root (containing .lore/). Pass "" to use cwd.
// The new bundle is added at the end (highest priority).
func enableBundle(projectDir, slug string) error {
if bundleDirForSlug(slug) == "" {
return fmt.Errorf("bundle '%s' is not installed", slug)
}
configPath := filepath.Join(projectDir, ".lore", "config.json")
cfg := make(map[string]interface{})
if existing, err := os.ReadFile(configPath); err == nil {
_ = json.Unmarshal(stripJSONComments(existing), &cfg)
}
slugs := parseBundleSlugs(cfg)
for _, s := range slugs {
if s == slug {
return fmt.Errorf("bundle '%s' is already enabled", slug)
}
}
slugs = append(slugs, slug)
cfg["bundles"] = slugs
out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(configPath, append(out, '\n'), 0644)
}
// disableBundle removes a bundle slug from the "bundles" array in .lore/config.json.
// projectDir is the project root (containing .lore/). Pass "" to use cwd.
func disableBundle(projectDir, slug string) error {
configPath := filepath.Join(projectDir, ".lore", "config.json")
cfg := make(map[string]interface{})
if existing, err := os.ReadFile(configPath); err == nil {
_ = json.Unmarshal(stripJSONComments(existing), &cfg)
}
slugs := parseBundleSlugs(cfg)
var filtered []string
for _, s := range slugs {
if s != slug {
filtered = append(filtered, s)
}
}
if len(filtered) == len(slugs) {
return fmt.Errorf("bundle '%s' is not enabled", slug)
}
cfg["bundles"] = filtered
out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(configPath, append(out, '\n'), 0644)
}