Files
scriptorium/internal/config/document.go

108 lines
4.2 KiB
Go

package config
import (
"path/filepath"
"regexp"
"strings"
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
// Document is Scriptorium's ConfigKit-native application configuration shape.
type Document struct {
PromptKit promptkit.Settings `yaml:"promptkit"`
TransientRetries *int `yaml:"transient_retries"`
Server ServerDocument `yaml:"server"`
}
type ServerDocument struct {
Addr string `yaml:"addr"`
ArtifactRoot string `yaml:"artifact_root"`
MaxRequestBytes int64 `yaml:"max_request_bytes"`
MaxArtifactBytes int64 `yaml:"max_artifact_bytes"`
MaxResponseBytes int64 `yaml:"max_response_bytes"`
}
func documentDefinition() configkit.Definition[Document] {
definition := configkit.Definition[Document]{
Defaults: documentDefaults,
Resolve: resolveDocument,
Validate: validateDocument,
Sensitive: func(path configkit.Path) bool { return rawKeyPath.MatchString(path.String()) },
}
return promptkit.Configure(definition, func(document *Document) *promptkit.Settings { return &document.PromptKit })
}
var rawKeyPath = regexp.MustCompile(`^/promptkit/(?:backends|profiles)/[0-9]+/api_key$|^/promptkit/collections/[^/]+/(?:backends|profiles)/[0-9]+/api_key$`)
func documentDefaults() Document {
return Document{
Server: ServerDocument{
Addr: defaults.HTTPAddrDefault,
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault,
MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault,
},
}
}
func resolveDocument(document *Document, resolution *configkit.Resolution) error {
normalizePath := func(path configkit.Path, value *string) {
cleaned := strings.TrimSpace(*value)
if cleaned != "" {
cleaned = filepath.Clean(cleaned)
}
if cleaned != *value {
configkit.Normalize(resolution, path, value, cleaned, "normalized application path")
}
}
normalizeString := func(path configkit.Path, value *string, normalized, reason string) {
if normalized != *value {
configkit.Normalize(resolution, path, value, normalized, reason)
}
}
normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot)
normalizeString(configkit.Keys("server", "addr"), &document.Server.Addr,
strings.TrimSpace(document.Server.Addr), "trimmed server address")
return nil
}
func validateDocument(document *Document, validation *configkit.Validation) {
for _, key := range []string{"configuration", "schema_dir"} {
if _, exists := document.PromptKit.Application[key]; exists {
validation.Error(configkit.Keys("promptkit", key), "retired setting; migrate to native PromptKit configuration")
}
}
validateResources := func(path configkit.Path, backends []promptkit.BackendSettings, profiles []promptkit.ProfileSettings) {
for index, backend := range backends {
if _, exists := backend.Application["api_key"]; exists {
validation.Error(path.Key("backends").Index(index).Key("api_key"), "raw API keys are not supported; use an environment variable")
}
}
for index, profile := range profiles {
if _, exists := profile.Application["api_key"]; exists {
validation.Error(path.Key("profiles").Index(index).Key("api_key"), "raw API keys are not supported; use an environment variable")
}
}
}
validateResources(configkit.Keys("promptkit"), document.PromptKit.Backends, document.PromptKit.Profiles)
for name, collection := range document.PromptKit.Collections {
validateResources(configkit.Keys("promptkit", "collections", name), collection.Backends, collection.Profiles)
}
if document.TransientRetries != nil && *document.TransientRetries < 0 {
validation.Error(configkit.Keys("transient_retries"), "must be greater than or equal to zero")
}
if document.Server.MaxRequestBytes < 0 {
validation.Error(configkit.Keys("server", "max_request_bytes"), "must be greater than or equal to zero")
}
if document.Server.MaxArtifactBytes < 0 {
validation.Error(configkit.Keys("server", "max_artifact_bytes"), "must be greater than or equal to zero")
}
if document.Server.MaxResponseBytes < 0 {
validation.Error(configkit.Keys("server", "max_response_bytes"), "must be greater than or equal to zero")
}
}