Files
scriptorium/internal/config/document.go

116 lines
4.2 KiB
Go
Raw Normal View History

package config
import (
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
"go.maximumdirect.net/configkit"
)
// Document is Scriptorium's ConfigKit-native application configuration shape.
type Document struct {
PromptKitSources []SourceSelection `yaml:"promptkit_sources"`
SchemaDir string `yaml:"schema_dir"`
TransientRetries *int `yaml:"transient_retries"`
Server ServerDocument `yaml:"server"`
Defaults DefaultsDocument `yaml:"defaults"`
}
// SourceSelection identifies one PromptKit configuration file or directory.
type SourceSelection struct {
File string `yaml:"file"`
Directory string `yaml:"directory"`
}
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"`
}
type DefaultsDocument struct {
RenderFormat string `yaml:"render_format"`
}
func documentDefinition() configkit.Definition[Document] {
return configkit.Definition[Document]{
Defaults: documentDefaults,
Resolve: resolveDocument,
Validate: validateDocument,
}
}
func documentDefaults() Document {
return Document{
SchemaDir: defaults.SchemaDirDefault,
Server: ServerDocument{
Addr: defaults.HTTPAddrDefault,
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault,
MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault,
},
Defaults: DefaultsDocument{RenderFormat: string(renderformat.DefaultPreparedRunOutputFormat)},
}
}
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)
}
}
for i := range document.PromptKitSources {
path := configkit.Keys("promptkit_sources").Index(i)
normalizePath(path.Key("file"), &document.PromptKitSources[i].File)
normalizePath(path.Key("directory"), &document.PromptKitSources[i].Directory)
}
normalizePath(configkit.Keys("schema_dir"), &document.SchemaDir)
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")
normalizeString(configkit.Keys("defaults", "render_format"), &document.Defaults.RenderFormat,
strings.ToLower(strings.TrimSpace(document.Defaults.RenderFormat)), "normalized render format")
return nil
}
func validateDocument(document *Document, validation *configkit.Validation) {
for i, source := range document.PromptKitSources {
hasFile := source.File != ""
hasDirectory := source.Directory != ""
if hasFile == hasDirectory {
validation.Error(configkit.Keys("promptkit_sources").Index(i), "exactly one of file or directory must be non-empty")
}
}
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")
}
switch document.Defaults.RenderFormat {
case string(renderformat.OutputFormatText), string(renderformat.OutputFormatJSON):
default:
validation.Error(configkit.Keys("defaults", "render_format"), "must be text or json")
}
}