Load native PromptKit settings through application configuration version six
This commit is contained in:
@@ -2,7 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -14,7 +13,6 @@ func resolveDocumentOperations(document *Document, resolution *configkit.Resolut
|
||||
if document == nil {
|
||||
return nil
|
||||
}
|
||||
normalizeDocumentPromptKit(document.PromptKit, resolution)
|
||||
normalizeDocumentState(document, resolution)
|
||||
normalizeDocumentPaths(document, resolution, state)
|
||||
normalizeDocumentPipelines(document, resolution, state)
|
||||
@@ -27,33 +25,6 @@ func resolveDocumentOperations(document *Document, resolution *configkit.Resolut
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeDocumentPromptKit(promptKit *PromptKitDocument, resolution *configkit.Resolution) {
|
||||
if promptKit == nil {
|
||||
return
|
||||
}
|
||||
profileDirPath := configkit.Keys("promptkit", "profile_dir")
|
||||
profileFilePath := configkit.Keys("promptkit", "profile_file")
|
||||
if promptKit.ProfileDir == nil && resolution.IsExplicit(profileDirPath) {
|
||||
resolution.Error(profileDirPath, "must not be null")
|
||||
}
|
||||
if promptKit.ProfileFile == nil && resolution.IsExplicit(profileFilePath) {
|
||||
resolution.Error(profileFilePath, "must not be null")
|
||||
}
|
||||
normalizeDocumentString(resolution, profileDirPath, &promptKit.ProfileDir, "trimmed PromptKit profile directory")
|
||||
normalizeDocumentString(resolution, profileFilePath, &promptKit.ProfileFile, "trimmed PromptKit profile file")
|
||||
|
||||
if promptKit.LocalBackend == nil {
|
||||
return
|
||||
}
|
||||
endpointPath := configkit.Keys("promptkit", "local_backend", "endpoint")
|
||||
limitPath := configkit.Keys("promptkit", "local_backend", "concurrency_limit")
|
||||
normalizeDocumentString(resolution, endpointPath, &promptKit.LocalBackend.Endpoint, "trimmed PromptKit local backend endpoint")
|
||||
if promptKit.LocalBackend.ConcurrencyLimit == nil && !resolution.IsExplicit(limitPath) {
|
||||
configkit.Default(resolution, limitPath, &promptKit.LocalBackend.ConcurrencyLimit,
|
||||
documentValue(0), "defaulted PromptKit local backend concurrency limit")
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeDocumentState(document *Document, resolution *configkit.Resolution) {
|
||||
if document.Output != nil {
|
||||
normalizeDocumentString(resolution, configkit.Keys("output", "directory"), &document.Output.Directory, "trimmed output directory")
|
||||
@@ -104,7 +75,6 @@ func validateDocumentOperations(document *Document, validation *configkit.Valida
|
||||
return
|
||||
}
|
||||
validateDocumentConcurrency(document.Concurrency, validation)
|
||||
validateDocumentPromptKit(document.PromptKit, validation)
|
||||
validateDocumentState(document, validation)
|
||||
validateDocumentPipelines(document, validation)
|
||||
}
|
||||
@@ -144,57 +114,6 @@ func validateDocumentConcurrency(concurrency *ConcurrencyDocument, validation *c
|
||||
}
|
||||
}
|
||||
|
||||
func validateDocumentPromptKit(promptKit *PromptKitDocument, validation *configkit.Validation) {
|
||||
if promptKit == nil {
|
||||
return
|
||||
}
|
||||
profileDirPath := configkit.Keys("promptkit", "profile_dir")
|
||||
profileFilePath := configkit.Keys("promptkit", "profile_file")
|
||||
profileDir := ""
|
||||
if promptKit.ProfileDir != nil {
|
||||
profileDir = *promptKit.ProfileDir
|
||||
if strings.TrimSpace(profileDir) == "" {
|
||||
validation.Error(profileDirPath, "must not be empty when set")
|
||||
}
|
||||
}
|
||||
profileFile := ""
|
||||
if promptKit.ProfileFile != nil {
|
||||
profileFile = *promptKit.ProfileFile
|
||||
if strings.TrimSpace(profileFile) == "" {
|
||||
validation.Error(profileFilePath, "must not be empty when set")
|
||||
}
|
||||
}
|
||||
if err := validatePromptKitProfileSources(profileDir, profileFile); err != nil {
|
||||
validation.Error(configkit.Keys("promptkit"), err.Error())
|
||||
}
|
||||
if promptKit.TransientRetries == nil {
|
||||
validation.Error(configkit.Keys("promptkit", "transient_retries"), "must be an integer")
|
||||
} else if err := validatePromptKitTransientRetries(*promptKit.TransientRetries); err != nil {
|
||||
validation.Error(configkit.Keys("promptkit", "transient_retries"), err.Error())
|
||||
}
|
||||
if promptKit.LocalBackend == nil {
|
||||
return
|
||||
}
|
||||
endpointPath := configkit.Keys("promptkit", "local_backend", "endpoint")
|
||||
limitPath := configkit.Keys("promptkit", "local_backend", "concurrency_limit")
|
||||
if promptKit.LocalBackend.Endpoint == nil {
|
||||
validation.Error(endpointPath, "must be nonempty when local backend is configured")
|
||||
}
|
||||
if promptKit.LocalBackend.ConcurrencyLimit == nil {
|
||||
validation.Error(limitPath, "must be an integer when local backend is configured")
|
||||
}
|
||||
if promptKit.LocalBackend.Endpoint != nil {
|
||||
if err := validatePromptKitLocalBackendEndpoint(*promptKit.LocalBackend.Endpoint); err != nil {
|
||||
validation.Error(endpointPath, err.Error())
|
||||
}
|
||||
}
|
||||
if promptKit.LocalBackend.ConcurrencyLimit != nil {
|
||||
if err := validatePromptKitLocalBackendConcurrencyLimit(*promptKit.LocalBackend.ConcurrencyLimit); err != nil {
|
||||
validation.Error(limitPath, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func validateDocumentState(document *Document, validation *configkit.Validation) {
|
||||
if document.Output == nil || document.Output.Directory == nil {
|
||||
validation.Error(configkit.Keys("output", "directory"), "must not be empty")
|
||||
@@ -249,52 +168,6 @@ func validateExtractWorkerCount(workers, total int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePromptKitProfileSources(profileDir, profileFile string) error {
|
||||
if strings.TrimSpace(profileDir) != "" && strings.TrimSpace(profileFile) != "" {
|
||||
return fmt.Errorf("promptkit profile_dir and profile_file are mutually exclusive")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePromptKitLocalBackend(endpoint string, concurrencyLimit int) error {
|
||||
if err := validatePromptKitLocalBackendEndpoint(endpoint); err != nil {
|
||||
return err
|
||||
}
|
||||
return validatePromptKitLocalBackendConcurrencyLimit(concurrencyLimit)
|
||||
}
|
||||
|
||||
func validatePromptKitLocalBackendEndpoint(endpoint string) error {
|
||||
if strings.TrimSpace(endpoint) == "" {
|
||||
return fmt.Errorf("promptkit.local_backend.endpoint must not be empty when set")
|
||||
}
|
||||
parsed, err := url.Parse(strings.TrimSpace(endpoint))
|
||||
if err != nil ||
|
||||
(!strings.EqualFold(parsed.Scheme, "http") && !strings.EqualFold(parsed.Scheme, "https")) ||
|
||||
!parsed.IsAbs() ||
|
||||
parsed.Hostname() == "" ||
|
||||
parsed.User != nil ||
|
||||
parsed.RawQuery != "" ||
|
||||
parsed.ForceQuery ||
|
||||
strings.Contains(endpoint, "#") {
|
||||
return fmt.Errorf("promptkit.local_backend.endpoint must be an absolute HTTP or HTTPS URL with a host and no user information, query, or fragment")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePromptKitLocalBackendConcurrencyLimit(concurrencyLimit int) error {
|
||||
if concurrencyLimit < 0 {
|
||||
return fmt.Errorf("promptkit.local_backend.concurrency_limit must not be negative")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePromptKitTransientRetries(transientRetries int) error {
|
||||
if transientRetries < 0 {
|
||||
return fmt.Errorf("promptkit.transient_retries must not be negative")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRequiredPath(name, value string) error {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%s must not be empty", name)
|
||||
|
||||
Reference in New Issue
Block a user