2026-09-26 17:58:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
2026-11-17 22:00:43 +00:00
|
|
|
"go.maximumdirect.net/configkit"
|
2026-09-26 17:58:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Document is Scriptorium's ConfigKit-native application configuration shape.
|
|
|
|
|
type Document struct {
|
2026-11-20 04:26:17 +00:00
|
|
|
PromptKit PromptKitDocument `yaml:"promptkit"`
|
2026-10-04 11:26:40 +00:00
|
|
|
TransientRetries *int `yaml:"transient_retries"`
|
|
|
|
|
Server ServerDocument `yaml:"server"`
|
2026-09-26 17:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
// PromptKitDocument selects framework acquisition, not embedded definitions.
|
|
|
|
|
type PromptKitDocument struct {
|
|
|
|
|
Configuration string `yaml:"configuration"`
|
|
|
|
|
SchemaDir string `yaml:"schema_dir"`
|
2026-09-26 17:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{
|
2026-11-20 04:26:17 +00:00
|
|
|
PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault},
|
2026-09-26 17:58:16 +00:00
|
|
|
Server: ServerDocument{
|
|
|
|
|
Addr: defaults.HTTPAddrDefault,
|
|
|
|
|
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
|
|
|
|
|
MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault,
|
|
|
|
|
MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveDocument(document *Document, resolution *configkit.Resolution) error {
|
2026-11-17 22:00:43 +00:00
|
|
|
normalizePath := func(path configkit.Path, value *string) {
|
2026-09-26 17:58:16 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
normalizePath(configkit.Keys("promptkit", "configuration"), &document.PromptKit.Configuration)
|
|
|
|
|
normalizePath(configkit.Keys("promptkit", "schema_dir"), &document.PromptKit.SchemaDir)
|
2026-09-26 17:58:16 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|