2026-09-26 17:58:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
2026-11-23 03:25:05 +00:00
|
|
|
"regexp"
|
2026-09-26 17:58:16 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
2026-11-17 22:00:43 +00:00
|
|
|
"go.maximumdirect.net/configkit"
|
2026-11-23 03:25:05 +00:00
|
|
|
"go.maximumdirect.net/promptkit"
|
2026-09-26 17:58:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Document is Scriptorium's ConfigKit-native application configuration shape.
|
|
|
|
|
type Document struct {
|
2026-11-23 03:25:05 +00:00
|
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
|
|
|
|
TransientRetries *int `yaml:"transient_retries"`
|
|
|
|
|
Server ServerDocument `yaml:"server"`
|
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] {
|
2026-11-23 03:25:05 +00:00
|
|
|
definition := configkit.Definition[Document]{
|
|
|
|
|
Defaults: documentDefaults,
|
|
|
|
|
Resolve: resolveDocument,
|
|
|
|
|
Validate: validateDocument,
|
|
|
|
|
Sensitive: func(path configkit.Path) bool { return rawKeyPath.MatchString(path.String()) },
|
2026-09-26 17:58:16 +00:00
|
|
|
}
|
2026-11-23 03:25:05 +00:00
|
|
|
return promptkit.Configure(definition, func(document *Document) *promptkit.Settings { return &document.PromptKit })
|
2026-09-26 17:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 03:25:05 +00:00
|
|
|
var rawKeyPath = regexp.MustCompile(`^/promptkit/(?:backends|profiles)/[0-9]+/api_key$|^/promptkit/collections/[^/]+/(?:backends|profiles)/[0-9]+/api_key$`)
|
|
|
|
|
|
2026-09-26 17:58:16 +00:00
|
|
|
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 {
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-11-23 03:25:05 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-09-26 17:58:16 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|