-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
208 lines (187 loc) · 4.65 KB
/
config.go
File metadata and controls
208 lines (187 loc) · 4.65 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
package sight
import (
"os"
"path/filepath"
"strconv"
"strings"
)
// FileConfig represents the contents of a .sight.toml configuration file.
type FileConfig struct {
Model string `json:"model"`
Concerns []string `json:"concerns"`
FailOn string `json:"fail_on"`
MaxTokens int `json:"max_tokens"`
Exclude []string `json:"exclude"`
GitContext *bool `json:"git_context"`
Reflection *bool `json:"reflection"`
Parallel *bool `json:"parallel"`
Prompts map[string]string `json:"prompts"`
}
// LoadConfigFile reads .sight.toml from the given directory (or parents).
// Returns nil if no config file is found. Errors only on malformed files.
func LoadConfigFile(dir string) (*FileConfig, error) {
path := findConfigFile(dir)
if path == "" {
return nil, nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return parseTOMLConfig(string(data))
}
// ApplyFileConfig converts a FileConfig into Options.
func ApplyFileConfig(fc *FileConfig) []Option {
if fc == nil {
return nil
}
var opts []Option
if fc.Model != "" {
opts = append(opts, WithModel(fc.Model))
}
if len(fc.Concerns) > 0 {
opts = append(opts, WithConcerns(fc.Concerns...))
}
if fc.FailOn != "" {
opts = append(opts, WithFailOn(ParseSeverity(fc.FailOn)))
}
if fc.MaxTokens > 0 {
opts = append(opts, WithMaxTokens(fc.MaxTokens))
}
if fc.GitContext != nil {
opts = append(opts, WithGitContext(*fc.GitContext))
}
if fc.Reflection != nil {
opts = append(opts, WithReflection(*fc.Reflection))
}
if fc.Parallel != nil {
opts = append(opts, WithParallel(*fc.Parallel))
}
if len(fc.Exclude) > 0 {
opts = append(opts, WithExclude(fc.Exclude...))
}
return opts
}
func findConfigFile(dir string) string {
names := []string{".sight.toml", ".sight.json", "sight.toml"}
for {
for _, name := range names {
path := filepath.Join(dir, name)
if _, err := os.Stat(path); err == nil {
return path
}
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return ""
}
// parseTOMLConfig parses a simplified TOML format.
// Supports key = "value", key = true/false, key = 123, arrays, and [section] headers.
func parseTOMLConfig(content string) (*FileConfig, error) {
cfg := &FileConfig{
Prompts: make(map[string]string),
}
section := ""
for _, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
section = strings.Trim(line, "[]")
continue
}
key, value, ok := parseTOMLKeyValue(line)
if !ok {
continue
}
if section == "prompts" {
cfg.Prompts[key] = value
continue
}
switch key {
case "model":
cfg.Model = value
case "concerns":
cfg.Concerns = parseTOMLArray(value)
case "fail_on":
cfg.FailOn = value
case "max_tokens":
if n := parseInt(value); n > 0 {
cfg.MaxTokens = n
}
case "exclude":
cfg.Exclude = parseTOMLArray(value)
case "git_context":
b := value == "true"
cfg.GitContext = &b
case "reflection":
b := value == "true"
cfg.Reflection = &b
case "parallel":
b := value == "true"
cfg.Parallel = &b
}
}
return cfg, nil
}
// parseTOMLKeyValue splits a TOML line into key and value, handling quoted values
// that may contain '=' signs.
func parseTOMLKeyValue(line string) (key, value string, ok bool) {
idx := strings.Index(line, "=")
if idx < 0 {
return "", "", false
}
key = strings.TrimSpace(line[:idx])
value = strings.TrimSpace(line[idx+1:])
// Strip matching outer quotes
if len(value) >= 2 {
if (value[0] == '"' && value[len(value)-1] == '"') ||
(value[0] == '\'' && value[len(value)-1] == '\'') {
value = value[1 : len(value)-1]
}
}
return key, value, key != ""
}
func parseTOMLArray(s string) []string {
s = strings.Trim(s, "[]")
parts := strings.Split(s, ",")
var result []string
for _, p := range parts {
p = strings.TrimSpace(p)
p = strings.Trim(p, `"'`)
if p != "" {
result = append(result, p)
}
}
return result
}
// parseInt parses a string as an integer with range validation.
// It reads leading digits (ignoring trailing non-digit chars for TOML compat)
// and caps the result at 1,000,000 to prevent unreasonable values.
func parseInt(s string) int {
s = strings.TrimSpace(s)
if s == "" {
return 0
}
// Extract leading digits
end := 0
for end < len(s) && s[end] >= '0' && s[end] <= '9' {
end++
}
if end == 0 {
return 0
}
n, err := strconv.Atoi(s[:end])
if err != nil {
return 0
}
if n < 0 || n > 1_000_000 {
return 0
}
return n
}