-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.go
More file actions
162 lines (149 loc) · 4.61 KB
/
Copy pathprofile.go
File metadata and controls
162 lines (149 loc) · 4.61 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
package tok
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/BurntSushi/toml"
)
// Profile is a named, versionable bundle of compression settings that teams can
// share. It captures the mode, tier, budget, an optional path to custom filter
// rules, and free-form notes. A Profile resolves to a slice of Option via
// Options, so it can be applied directly:
//
// p, _ := tok.LoadProfile(".tok/profiles/code-safe.toml")
// out, stats := tok.Compress(text, p.Options()...)
type Profile struct {
Name string `toml:"name"` // Human-readable profile name.
Version string `toml:"version"` // Profile version (e.g. "1", "2024.1").
Mode Mode `toml:"mode"` // Compression mode (minimal/aggressive).
Tier Tier `toml:"tier"` // Pipeline tier (surface/trim/...).
Budget int `toml:"budget"` // Hard token budget (0 = unlimited).
FilterRulesPath string `toml:"filter_rules_path"` // Optional path to custom filter rules.
Notes string `toml:"notes"` // Free-form description / changelog.
}
// builtinProfiles holds the in-memory registry of ready-to-use profiles.
var builtinProfiles = map[string]*Profile{
"default": {
Name: "default",
Version: "1",
Mode: ModeMinimal,
Tier: TierCore,
Notes: "Balanced, quality-first compression suitable for most content.",
},
"aggressive": {
Name: "aggressive",
Version: "1",
Mode: ModeAggressive,
Tier: TierExtract,
Notes: "Maximum compression. Favors token savings over fidelity.",
},
"code-safe": {
Name: "code-safe",
Version: "1",
Mode: ModeMinimal,
Tier: TierCode,
Notes: "Code-aware, structure-preserving compression for source and diffs.",
},
}
// BuiltinProfile returns a copy of the named built-in profile and whether it
// exists. Returning a copy keeps the registry immutable to callers.
func BuiltinProfile(name string) (*Profile, bool) {
p, ok := builtinProfiles[name]
if !ok {
return nil, false
}
clone := *p
return &clone, true
}
// BuiltinProfiles returns copies of all registered built-in profiles keyed by
// name.
func BuiltinProfiles() map[string]*Profile {
out := make(map[string]*Profile, len(builtinProfiles))
for name, p := range builtinProfiles {
clone := *p
out[name] = &clone
}
return out
}
// Options resolves the profile into the equivalent slice of Option values, so
// it can be passed to Compress or NewCompressor.
func (p *Profile) Options() []Option {
if p == nil {
return nil
}
var opts []Option
if p.Mode != "" {
opts = append(opts, WithMode(p.Mode))
}
if p.Tier != "" {
opts = append(opts, WithTier(p.Tier))
}
if p.Budget > 0 {
opts = append(opts, WithBudget(p.Budget))
}
return opts
}
// Validate checks the profile for obviously invalid field values.
func (p *Profile) Validate() error {
if p == nil {
return fmt.Errorf("nil profile")
}
if p.Mode != "" && p.Mode != ModeMinimal && p.Mode != ModeAggressive {
return fmt.Errorf("invalid mode %q (want %q or %q)", p.Mode, ModeMinimal, ModeAggressive)
}
if p.Budget < 0 {
return fmt.Errorf("budget must be non-negative, got %d", p.Budget)
}
return nil
}
// LoadProfile reads a single profile from a TOML file. If the profile omits a
// name, the file's base name (without extension) is used.
func LoadProfile(path string) (*Profile, error) {
var p Profile
if _, err := toml.DecodeFile(path, &p); err != nil {
return nil, fmt.Errorf("decode profile %s: %w", path, err)
}
if p.Name == "" {
base := filepath.Base(path)
p.Name = strings.TrimSuffix(base, filepath.Ext(base))
}
if err := p.Validate(); err != nil {
return nil, fmt.Errorf("invalid profile %s: %w", path, err)
}
return &p, nil
}
// LoadProfiles reads every *.toml profile in dir, keyed by profile name. A
// missing directory yields an empty map and no error so callers can treat
// "no shared profiles" as a normal state.
func LoadProfiles(dir string) (map[string]*Profile, error) {
out := make(map[string]*Profile)
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return out, nil
}
return nil, fmt.Errorf("read profiles dir %s: %w", dir, err)
}
// Sort for deterministic loading and stable error reporting.
names := make([]string, 0, len(entries))
for _, e := range entries {
if e.IsDir() {
continue
}
if !strings.EqualFold(filepath.Ext(e.Name()), ".toml") {
continue
}
names = append(names, e.Name())
}
sort.Strings(names)
for _, name := range names {
p, err := LoadProfile(filepath.Join(dir, name))
if err != nil {
return nil, err
}
out[p.Name] = p
}
return out, nil
}