-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathmodel_identifier.go
More file actions
369 lines (331 loc) · 13.1 KB
/
Copy pathmodel_identifier.go
File metadata and controls
369 lines (331 loc) · 13.1 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
// This file implements the Model Alias Format (MAF) identifier parser as defined in
// the Model Alias Format Specification (docs/src/content/docs/reference/model-alias-specification.md).
//
// # Model Identifier Syntax (Section 4)
//
// A model identifier string takes one of the following forms:
//
// bare-name e.g. "sonnet", "auto"
// provider/model-token e.g. "copilot/gpt-5"
// provider/model-glob-token e.g. "copilot/*sonnet*"
// any of the above + "?" params e.g. "opus?effort=high"
//
// # ABNF Grammar (Section 4.1)
//
// model-identifier = base-identifier [ "?" query-string ]
//
// base-identifier = bare-name
// / provider-scoped
// / glob-pattern
//
// bare-name = 1*( ALPHA / DIGIT / "-" / "_" / "." )
// ; MUST NOT start with "-" or "."
//
// provider-scoped = provider-token "/" model-token
//
// provider-token = ALPHA 0*( ALPHA / DIGIT / "-" )
// ; starts with a letter; hyphens allowed but not at end
//
// model-token = model-char 0*( model-char / "." model-char )
//
// model-char = ALPHA / DIGIT / "-" / "_"
//
// glob-pattern = provider-token "/" model-glob-token
//
// model-glob-token = 1*( model-char / "." / "*" )
//
// query-string = param *( "&" param )
//
// param = param-key "=" param-value
//
// param-key = ALPHA 0*( ALPHA / DIGIT / "-" )
//
// param-value = 1*( ALPHA / DIGIT / "-" / "_" / "." )
package workflow
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/github/gh-aw/pkg/logger"
)
var modelIdentifierLog = logger.New("workflow:model_identifier")
// ParsedModelIdentifier holds the components of a parsed model identifier.
type ParsedModelIdentifier struct {
// Raw is the original unparsed string.
Raw string
// Base is the base identifier (before "?").
Base string
// Provider is the provider token (empty for bare identifiers).
Provider string
// ModelToken is the model-token portion after the "/" (empty for bare identifiers).
ModelToken string
// IsGlob reports whether the model token contains a "*" wildcard.
IsGlob bool
// Params holds the URL-style query parameters (key → value).
Params map[string]string
}
// Defined parameter keys recognised by the spec (Section 6).
const (
modelParamEffort = "effort"
modelParamTemperature = "temperature"
)
// knownModelParams is the set of parameter keys defined in Section 6.
var knownModelParams = map[string]struct{}{
modelParamEffort: {},
modelParamTemperature: {},
}
// ─── Character-class helpers ─────────────────────────────────────────────────
// regex patterns derived directly from the ABNF grammar.
var (
// provider-token: starts with ALPHA, followed by ALPHA/DIGIT/"-"; must not end with "-"
reProviderToken = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-]*$`)
// model-char: ALPHA / DIGIT / "-" / "_"
// model-token: model-char segments separated by "."; each segment starts with ALPHA or DIGIT
reModelToken = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]*(\.[A-Za-z0-9][A-Za-z0-9_-]*)*$`)
// bare-name: starts with ALPHA/DIGIT; contains ALPHA/DIGIT/"-"/"_"/"."
reBareName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`)
// param-key: starts with ALPHA; followed by ALPHA/DIGIT/"-"
reParamKey = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-]*$`)
// param-value: 1*(ALPHA / DIGIT / "-" / "_" / ".")
reParamValue = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
)
// firstForbiddenCharInProviderOrParam returns the first character in s that is not in
// [A-Za-z0-9-], or 0 if all characters are allowed.
// Used for provider tokens and parameter keys (V-MAF-006).
func firstForbiddenCharInProviderOrParam(s string) rune {
for _, r := range s {
if !isAlpha(r) && !isDigit(r) && r != '-' {
return r
}
}
return 0
}
// firstForbiddenCharInModelToken returns the first character in s that is not in
// [A-Za-z0-9_.-], or 0 if all characters are allowed.
// Used for model tokens (V-MAF-006).
func firstForbiddenCharInModelToken(s string) rune {
for _, r := range s {
if !isAlpha(r) && !isDigit(r) && r != '_' && r != '.' && r != '-' {
return r
}
}
return 0
}
// firstForbiddenCharInBareName returns the first character in s that is not in
// [A-Za-z0-9._-], or 0 if all characters are allowed.
// Used for bare names (V-MAF-006).
func firstForbiddenCharInBareName(s string) rune {
for _, r := range s {
if !isAlpha(r) && !isDigit(r) && r != '_' && r != '.' && r != '-' {
return r
}
}
return 0
}
// firstForbiddenCharInParamValue returns the first character in s that is not in
// [A-Za-z0-9._-], or 0 if all characters are allowed.
// Used for parameter values (V-MAF-006).
func firstForbiddenCharInParamValue(s string) rune {
for _, r := range s {
if !isAlpha(r) && !isDigit(r) && r != '_' && r != '.' && r != '-' {
return r
}
}
return 0
}
func isAlpha(r rune) bool { return isLetter(r) }
// ParseModelIdentifier parses a model identifier string into its components.
//
// The identifier format follows the ABNF grammar in Section 4.1 of the spec.
// Returns a non-nil *ParsedModelIdentifier on success.
// Returns an error (satisfying V-MAF-001 and V-MAF-006) on syntax violations.
func ParseModelIdentifier(s string) (*ParsedModelIdentifier, error) {
modelIdentifierLog.Printf("Parsing model identifier: %q", s)
if s == "" {
return nil, errors.New("model identifier must not be empty")
}
// Split on the first "?" to separate base from query string.
base, rawQuery, _ := strings.Cut(s, "?")
p := &ParsedModelIdentifier{
Raw: s,
Base: base,
Params: map[string]string{},
}
// ── Validate base identifier ─────────────────────────────────────────────
if strings.Contains(base, "/") {
// Provider-scoped or glob pattern.
provider, modelPart, _ := strings.Cut(base, "/")
if err := validateProviderToken(provider); err != nil {
modelIdentifierLog.Printf("Invalid provider token %q: %v", provider, err)
return nil, err
}
p.Provider = provider
if strings.Contains(modelPart, "*") {
// Glob pattern.
modelIdentifierLog.Printf("Parsing as glob pattern: provider=%q model-glob=%q", provider, modelPart)
if err := validateModelGlobToken(modelPart); err != nil {
return nil, err
}
p.ModelToken = modelPart
p.IsGlob = true
} else {
// Exact provider-scoped name.
modelIdentifierLog.Printf("Parsing as provider-scoped: provider=%q model=%q", provider, modelPart)
if err := validateModelToken(modelPart); err != nil {
return nil, err
}
p.ModelToken = modelPart
}
} else {
// Bare name.
modelIdentifierLog.Printf("Parsing as bare name: %q", base)
if err := validateBareName(base); err != nil {
return nil, err
}
}
// ── Parse and validate query string ─────────────────────────────────────
if rawQuery != "" {
modelIdentifierLog.Printf("Parsing query string: %q", rawQuery)
params, err := parseQueryString(rawQuery)
if err != nil {
return nil, err
}
p.Params = params
modelIdentifierLog.Printf("Parsed %d query param(s)", len(params))
}
modelIdentifierLog.Printf("Successfully parsed model identifier: provider=%q model=%q isGlob=%v params=%d", p.Provider, p.ModelToken, p.IsGlob, len(p.Params))
return p, nil
}
// ─── Segment validators ───────────────────────────────────────────────────────
// validateProviderToken validates a provider token per the ABNF grammar.
func validateProviderToken(token string) error {
if token == "" {
return errors.New("model identifier: provider token must not be empty (segment type: provider)")
}
if !reProviderToken.MatchString(token) {
if r := firstForbiddenCharInProviderOrParam(token); r != 0 {
return fmt.Errorf("model identifier: character %q is not allowed in provider token %q (segment type: provider)", r, token)
}
if !isAlpha(rune(token[0])) {
return fmt.Errorf("model identifier: provider token %q must start with a letter (segment type: provider)", token)
}
}
// Provider token must not end with "-".
if strings.HasSuffix(token, "-") {
return fmt.Errorf("model identifier: provider token %q must not end with '-' (segment type: provider)", token)
}
return nil
}
// validateModelToken validates a model token (no wildcards) per the ABNF grammar.
func validateModelToken(token string) error {
if token == "" {
return errors.New("model identifier: model token must not be empty (segment type: model)")
}
if !reModelToken.MatchString(token) {
if r := firstForbiddenCharInModelToken(token); r != 0 {
return fmt.Errorf("model identifier: character %q is not allowed in model token %q (segment type: model)", r, token)
}
return fmt.Errorf("model identifier: model token %q is syntactically invalid (segment type: model)", token)
}
return nil
}
// validateModelGlobToken validates a model-glob-token (may contain "*").
func validateModelGlobToken(token string) error {
if token == "" {
return errors.New("model identifier: model glob token must not be empty (segment type: model)")
}
for _, r := range token {
switch {
case r >= 'A' && r <= 'Z', r >= 'a' && r <= 'z', r >= '0' && r <= '9':
// ALPHA / DIGIT — always allowed
case r == '-', r == '_', r == '.', r == '*':
// allowed in glob tokens
default:
return fmt.Errorf("model identifier: character %q is not allowed in glob model token %q (segment type: model)", r, token)
}
}
return nil
}
// validateBareName validates a bare identifier name per the ABNF grammar.
func validateBareName(name string) error {
if name == "" {
return errors.New("model identifier: bare name must not be empty (segment type: alias)")
}
// Must not start with "-" or ".".
if name[0] == '-' || name[0] == '.' {
return fmt.Errorf("model identifier: bare name %q must not start with '-' or '.' (segment type: alias)", name)
}
if !reBareName.MatchString(name) {
if r := firstForbiddenCharInBareName(name); r != 0 {
return fmt.Errorf("model identifier: character %q is not allowed in bare name %q (segment type: alias)", r, name)
}
return fmt.Errorf("model identifier: bare name %q is syntactically invalid (segment type: alias)", name)
}
return nil
}
// ─── Parameter parsing ────────────────────────────────────────────────────────
// parseQueryString parses a query string of the form "key=value&key=value…".
// Returns an error if any key or value violates the grammar (syntax only).
// Known-parameter value validation is performed separately by ValidateKnownParams.
func parseQueryString(raw string) (map[string]string, error) {
params := map[string]string{}
for pair := range strings.SplitSeq(raw, "&") {
if pair == "" {
return nil, fmt.Errorf("model identifier: empty parameter pair in query string %q", raw)
}
k, v, found := strings.Cut(pair, "=")
if !found {
return nil, fmt.Errorf("model identifier: parameter %q is missing '=' separator", pair)
}
if err := validateParamKey(k); err != nil {
return nil, err
}
if err := validateParamValue(v); err != nil {
return nil, err
}
params[k] = v
}
return params, nil
}
// validateParamKey validates a parameter key per the ABNF grammar.
func validateParamKey(key string) error {
if key == "" {
return errors.New("model identifier: parameter key must not be empty (segment type: parameter key)")
}
if !reParamKey.MatchString(key) {
if r := firstForbiddenCharInProviderOrParam(key); r != 0 {
return fmt.Errorf("model identifier: character %q is not allowed in parameter key %q (segment type: parameter key)", r, key)
}
if !isAlpha(rune(key[0])) {
return fmt.Errorf("model identifier: parameter key %q must start with a letter (segment type: parameter key)", key)
}
}
return nil
}
// validateParamValue validates a parameter value per the ABNF grammar.
func validateParamValue(value string) error {
if value == "" {
return errors.New("model identifier: parameter value must not be empty (segment type: parameter value)")
}
if !reParamValue.MatchString(value) {
if r := firstForbiddenCharInParamValue(value); r != 0 {
return fmt.Errorf("model identifier: character %q is not allowed in parameter value %q (segment type: parameter value)", r, value)
}
}
return nil
}
// UnrecognizedParams returns the list of parameter keys in params that are not
// defined in Section 6 (i.e., not effort or temperature).
// Reserved future parameters (Section 6.3) are returned here as well since they
// are not yet defined.
func UnrecognizedParams(params map[string]string) []string {
var unknown []string
for k := range params {
if _, known := knownModelParams[k]; !known {
unknown = append(unknown, k)
}
}
return unknown
}
// ─── Utility ──────────────────────────────────────────────────────────────────