Load shared PromptKit settings with native secrets bootstrap
This commit is contained in:
@@ -2,23 +2,19 @@ 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 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"`
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
TransientRetries *int `yaml:"transient_retries"`
|
||||
Server ServerDocument `yaml:"server"`
|
||||
}
|
||||
|
||||
type ServerDocument struct {
|
||||
@@ -30,16 +26,19 @@ type ServerDocument struct {
|
||||
}
|
||||
|
||||
func documentDefinition() configkit.Definition[Document] {
|
||||
return configkit.Definition[Document]{
|
||||
Defaults: documentDefaults,
|
||||
Resolve: resolveDocument,
|
||||
Validate: validateDocument,
|
||||
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{
|
||||
PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault},
|
||||
Server: ServerDocument{
|
||||
Addr: defaults.HTTPAddrDefault,
|
||||
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
|
||||
@@ -65,8 +64,6 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
@@ -74,6 +71,27 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user