-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
267 lines (250 loc) · 7.34 KB
/
Copy pathconfig.go
File metadata and controls
267 lines (250 loc) · 7.34 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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"github.com/BurntSushi/toml"
)
type ModelSpec struct {
Model string
Efforts bool
}
type Profile struct {
Model string
Effort string
Flags []string
Env map[string]string
Claude string
// Extends records the [bases] name this profile was flattened
// against — display provenance only; resolution never reads it.
Extends string
}
type Config struct {
Claude string
Models map[string]ModelSpec
Efforts map[string]string
Bases map[string]Profile
Profiles map[string]Profile
}
func defaultConfig() Config {
return Config{
Claude: "claude",
Models: map[string]ModelSpec{
"o": {Model: "opus", Efforts: true},
"s": {Model: "sonnet", Efforts: true},
"f": {Model: "fable", Efforts: true},
"h": {Model: "haiku", Efforts: false},
},
Efforts: map[string]string{
"1": "low", "2": "medium", "3": "high", "4": "xhigh", "5": "max",
},
Bases: map[string]Profile{},
Profiles: map[string]Profile{},
}
}
func configPath() string {
if p := os.Getenv("CLAU_CONFIG"); p != "" {
return p
}
base := os.Getenv("XDG_CONFIG_HOME")
if base == "" {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
base = filepath.Join(home, ".config")
}
return filepath.Join(base, "clau", "config.toml")
}
var (
modelKeyRe = regexp.MustCompile(`^[a-z]+$`)
effortKeyRe = regexp.MustCompile(`^[1-9]$`)
profileNameRe = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)
)
type rawModel struct {
Model string `toml:"model"`
Efforts *bool `toml:"efforts"`
}
type rawProfile struct {
Model string `toml:"model"`
Effort string `toml:"effort"`
Flags []string `toml:"flags"`
Env map[string]string `toml:"env"`
Claude string `toml:"claude"`
Extends *string `toml:"extends"`
}
// rawBase is rawProfile without extends: bases cannot chain, and the
// missing field makes `extends` inside [bases.*] an unknown-key error.
type rawBase struct {
Model string `toml:"model"`
Effort string `toml:"effort"`
Flags []string `toml:"flags"`
Env map[string]string `toml:"env"`
Claude string `toml:"claude"`
}
type rawConfig struct {
Claude string `toml:"claude"`
Models map[string]toml.Primitive `toml:"models"`
Efforts map[string]string `toml:"efforts"`
Bases map[string]rawBase `toml:"bases"`
Profiles map[string]rawProfile `toml:"profiles"`
}
func loadConfig(path string) (Config, error) {
return applyConfigFile(defaultConfig(), path)
}
func cloneMap[V any](m map[string]V) map[string]V {
out := make(map[string]V, len(m))
for k, v := range m {
out[k] = v
}
return out
}
// flattenProfile fills child fields from base where the child leaves
// them unset: scalars fill in, flags concatenate base-first, env merges
// key-wise with the child winning. The result shares no slice or map
// storage with base, so later layers cannot mutate stored bases.
func flattenProfile(base, child Profile) Profile {
out := child
if out.Model == "" {
out.Model = base.Model
}
if out.Effort == "" {
out.Effort = base.Effort
}
if out.Claude == "" {
out.Claude = base.Claude
}
if len(base.Flags) > 0 {
out.Flags = append(append([]string{}, base.Flags...), child.Flags...)
}
if len(base.Env) > 0 {
env := cloneMap(base.Env)
for k, v := range child.Env {
env[k] = v
}
out.Env = env
}
return out
}
// applyConfigFile reads path and delegates to applyConfigData. A missing
// file returns cfg unchanged, nil error.
func applyConfigFile(cfg Config, path string) (Config, error) {
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return cfg, nil
}
if err != nil {
return Config{}, fmt.Errorf("%s: %w", path, err)
}
return applyConfigData(cfg, path, data)
}
// applyConfigData decodes data and merges it over cfg: models per key,
// bases and profiles per name (wholesale) — each profile's extends
// flattened against the bases accumulated so far, then discarded — a
// non-empty efforts table replaces the ladder, claude if set. path is
// used only to name errors, so a caller
// that must hash and apply the exact same bytes can read the file once
// and pass the bytes to both. The maps of the cfg argument are never
// mutated.
func applyConfigData(cfg Config, path string, data []byte) (Config, error) {
var raw rawConfig
md, err := toml.Decode(string(data), &raw)
if err != nil {
var pe toml.ParseError
if errors.As(err, &pe) {
return Config{}, fmt.Errorf("%s: %s", path, pe.ErrorWithPosition())
}
return Config{}, fmt.Errorf("%s: %w", path, err)
}
cfg.Models = cloneMap(cfg.Models)
cfg.Bases = cloneMap(cfg.Bases)
cfg.Profiles = cloneMap(cfg.Profiles)
if raw.Claude != "" {
cfg.Claude = raw.Claude
}
for key, prim := range raw.Models {
if !modelKeyRe.MatchString(key) {
return Config{}, fmt.Errorf("%s: invalid model key %q (lowercase letters only)", path, key)
}
var name string
if err := md.PrimitiveDecode(prim, &name); err == nil {
if name == "" {
return Config{}, fmt.Errorf("%s: model %q: empty model name", path, key)
}
cfg.Models[key] = ModelSpec{Model: name, Efforts: true}
continue
}
var rm rawModel
if err := md.PrimitiveDecode(prim, &rm); err != nil {
return Config{}, fmt.Errorf("%s: model %q: %w", path, key, err)
}
if rm.Model == "" {
return Config{}, fmt.Errorf("%s: model %q: missing model name", path, key)
}
spec := ModelSpec{Model: rm.Model, Efforts: true}
if rm.Efforts != nil {
spec.Efforts = *rm.Efforts
}
cfg.Models[key] = spec
}
if len(raw.Efforts) > 0 {
cfg.Efforts = map[string]string{}
for key, level := range raw.Efforts {
if !effortKeyRe.MatchString(key) {
return Config{}, fmt.Errorf("%s: invalid effort key %q (single digit 1-9)", path, key)
}
if level == "" {
return Config{}, fmt.Errorf("%s: effort %q: empty level", path, key)
}
cfg.Efforts[key] = level
}
}
for name, rb := range raw.Bases {
if !profileNameRe.MatchString(name) {
return Config{}, fmt.Errorf("%s: invalid base name %q (want ^[a-z][a-z0-9-]*$)", path, name)
}
cfg.Bases[name] = Profile{
Model: rb.Model, Effort: rb.Effort, Flags: rb.Flags,
Env: rb.Env, Claude: rb.Claude,
}
}
for name, rp := range raw.Profiles {
if !profileNameRe.MatchString(name) {
return Config{}, fmt.Errorf("%s: invalid profile name %q (want ^[a-z][a-z0-9-]*$)", path, name)
}
p := Profile{
Model: rp.Model, Effort: rp.Effort, Flags: rp.Flags,
Env: rp.Env, Claude: rp.Claude,
}
if rp.Extends != nil {
parent := *rp.Extends
if parent == "" {
return Config{}, fmt.Errorf("%s: profile %q: empty extends", path, name)
}
base, ok := cfg.Bases[parent]
if !ok {
_, inFile := raw.Profiles[parent]
_, inCfg := cfg.Profiles[parent]
if inFile || inCfg {
return Config{}, fmt.Errorf("%s: profile %q: extends %q, which is a profile; extends references [bases]", path, name, parent)
}
return Config{}, fmt.Errorf("%s: profile %q: extends unknown base %q", path, name, parent)
}
p = flattenProfile(base, p)
p.Extends = parent
}
cfg.Profiles[name] = p
}
if undec := md.Undecoded(); len(undec) > 0 {
keys := make([]string, len(undec))
for i, k := range undec {
keys[i] = k.String()
}
sort.Strings(keys)
return Config{}, fmt.Errorf("%s: unknown key(s): %v", path, keys)
}
return cfg, nil
}