package config import ( "path/filepath" "strings" "gitea.maximumdirect.net/eric/scriptorium/internal/defaults" "go.maximumdirect.net/configkit" ) // Document is Scriptorium's ConfigKit-native application configuration shape. type Document struct { PromptKit PromptKitDocument `yaml:"promptkit"` TransientRetries *int `yaml:"transient_retries"` Server ServerDocument `yaml:"server"` } // PromptKitDocument selects framework acquisition, not embedded definitions. type PromptKitDocument struct { Configuration string `yaml:"configuration"` SchemaDir string `yaml:"schema_dir"` } 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] { return configkit.Definition[Document]{ Defaults: documentDefaults, Resolve: resolveDocument, Validate: validateDocument, } } func documentDefaults() Document { return Document{ PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault}, 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("promptkit", "configuration"), &document.PromptKit.Configuration) normalizePath(configkit.Keys("promptkit", "schema_dir"), &document.PromptKit.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") return nil } func validateDocument(document *Document, validation *configkit.Validation) { 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") } }