mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: move model settings page to ai settings
This commit is contained in:
@@ -8,21 +8,24 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
// SchemaField describes a single form field in the generated schema.
|
||||
type SchemaField struct {
|
||||
JSONName string `json:"json_name"`
|
||||
GoName string `json:"go_name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Label string `json:"label,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
Enum []string `json:"enum,omitempty"`
|
||||
InputType string `json:"input_type"`
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
JSONName string `json:"json_name"`
|
||||
GoName string `json:"go_name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Label string `json:"label,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
Enum []string `json:"enum,omitempty"`
|
||||
InputType string `json:"input_type"`
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
VisibleWhen string `json:"visible_when,omitempty"`
|
||||
ConflictsWith []string `json:"conflicts_with,omitempty"`
|
||||
}
|
||||
|
||||
// FieldGroup holds the fields for a struct or provider.
|
||||
@@ -53,6 +56,10 @@ func main() {
|
||||
"",
|
||||
map[string]bool{"ProviderOptions": true},
|
||||
)
|
||||
if err := validateFieldReferences("general", schema.General); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Provider-specific options. Each entry maps a provider key
|
||||
// to the concrete options struct used for that provider.
|
||||
@@ -70,6 +77,10 @@ func main() {
|
||||
|
||||
for _, p := range providerTypes {
|
||||
schema.Providers[p.key] = extractFields(p.typ, "", nil)
|
||||
if err := validateFieldReferences(p.key, schema.Providers[p.key]); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(schema, "", "\t")
|
||||
@@ -83,6 +94,25 @@ func main() {
|
||||
_, _ = fmt.Println(string(out))
|
||||
}
|
||||
|
||||
func validateFieldReferences(group string, fg FieldGroup) error {
|
||||
names := make(map[string]bool, len(fg.Fields))
|
||||
for _, f := range fg.Fields {
|
||||
names[f.JSONName] = true
|
||||
}
|
||||
|
||||
for _, f := range fg.Fields {
|
||||
if f.VisibleWhen != "" && !names[f.VisibleWhen] {
|
||||
return xerrors.Errorf("field %q in group %q has visible_when=%q referencing an unknown sibling field", f.JSONName, group, f.VisibleWhen)
|
||||
}
|
||||
for _, sibling := range f.ConflictsWith {
|
||||
if !names[sibling] {
|
||||
return xerrors.Errorf("field %q in group %q has conflicts_with entry %q referencing an unknown sibling field", f.JSONName, group, sibling)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// extractFields walks the struct fields of t and returns a FieldGroup.
|
||||
// prefix is used to build dot-separated json_name values for nested
|
||||
// structs. skip lists Go field names to exclude from output.
|
||||
@@ -138,6 +168,12 @@ func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGro
|
||||
description := f.Tag.Get("description")
|
||||
label := f.Tag.Get("label")
|
||||
enumTag := f.Tag.Get("enum")
|
||||
visibleWhen := f.Tag.Get("visible_when")
|
||||
|
||||
var conflictsWith []string
|
||||
if conflictsTag := f.Tag.Get("conflicts_with"); conflictsTag != "" {
|
||||
conflictsWith = strings.Split(conflictsTag, ",")
|
||||
}
|
||||
|
||||
var enumValues []string
|
||||
if enumTag != "" {
|
||||
@@ -148,15 +184,17 @@ func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGro
|
||||
inputType := inferInputType(typeName, enumValues)
|
||||
|
||||
fields = append(fields, SchemaField{
|
||||
JSONName: fullJSONName,
|
||||
GoName: goFieldPath(prefix, f.Name, t, fullJSONName),
|
||||
Type: typeName,
|
||||
Description: description,
|
||||
Label: label,
|
||||
Required: required,
|
||||
Enum: enumValues,
|
||||
InputType: inputType,
|
||||
Hidden: hidden,
|
||||
JSONName: fullJSONName,
|
||||
GoName: goFieldPath(prefix, f.Name, t, fullJSONName),
|
||||
Type: typeName,
|
||||
Description: description,
|
||||
Label: label,
|
||||
Required: required,
|
||||
Enum: enumValues,
|
||||
InputType: inputType,
|
||||
Hidden: hidden,
|
||||
VisibleWhen: visibleWhen,
|
||||
ConflictsWith: conflictsWith,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user