Files
promptkit/internal/configuration/resolve_targets.go

103 lines
4.0 KiB
Go

package configuration
import (
"net/http"
"strings"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit/internal/domain"
)
func resolveBackends(entries []backendConfig, base configkit.Path, resolution *configkit.Resolution) {
if !eligibleResourceList(entries, func(v backendConfig) string { return v.ID }) {
return
}
for i := range entries {
entry := &entries[i]
path := base.Index(i)
if entry.Endpoint != nil {
if normalized, err := domain.NormalizeOpenAICompatibleBaseEndpoint(*entry.Endpoint); err == nil {
recordStringNormalization(&entry.Endpoint, path.Key("endpoint"), resolution, normalized)
}
}
if entry.APIKeyEnv != nil {
recordStringNormalization(&entry.APIKeyEnv, path.Key("api_key_env"), resolution, strings.TrimSpace(*entry.APIKeyEnv))
}
if entry.ConcurrencyLimit != nil && *entry.ConcurrencyLimit > 0 && entry.QueueCapacity == nil {
capacity := 1024
entry.QueueCapacity = &capacity
resolution.RecordDerivedDefault(path.Key("queue_capacity"), "Derive queue capacity", path.Key("concurrency_limit"))
}
if entry.SmokeTests == nil {
continue
}
smoke := entry.SmokeTests
smokePath := path.Key("smoke_tests")
if smoke.Reachability != nil {
resolveDiagnosticEndpoint(&smoke.Reachability.Endpoint, smokePath.Key("reachability").Key("endpoint"), resolution)
}
if smoke.Authentication != nil {
auth := smoke.Authentication
authPath := smokePath.Key("authentication")
if auth.Kind != nil {
recordStringNormalization(&auth.Kind, authPath.Key("kind"), resolution, strings.ToLower(strings.TrimSpace(*auth.Kind)))
}
resolveDiagnosticEndpoint(&auth.Endpoint, authPath.Key("endpoint"), resolution)
}
}
}
func diagnosticEndpointValid(value string) bool {
normalized := strings.TrimSpace(value)
request, err := http.NewRequest(http.MethodGet, normalized, nil)
return normalized != "" && err == nil && request.URL != nil && (strings.EqualFold(request.URL.Scheme, "http") || strings.EqualFold(request.URL.Scheme, "https")) && request.URL.Hostname() != ""
}
func validateBackendEndpoints(entry backendConfig, path configkit.Path, validation *configkit.Validation) {
if entry.Endpoint != nil {
if _, err := domain.NormalizeOpenAICompatibleBaseEndpoint(*entry.Endpoint); err != nil {
validation.Error(path.Key("endpoint"), err.Error())
}
}
if entry.SmokeTests == nil {
return
}
smoke := entry.SmokeTests
path = path.Key("smoke_tests")
if smoke.Reachability != nil && smoke.Reachability.Endpoint != nil && !diagnosticEndpointValid(*smoke.Reachability.Endpoint) {
validation.Error(path.Key("reachability").Key("endpoint"), "diagnostic endpoint must be an absolute HTTP(S) URL with a host")
}
if smoke.Authentication != nil && smoke.Authentication.Endpoint != nil && !diagnosticEndpointValid(*smoke.Authentication.Endpoint) {
validation.Error(path.Key("authentication").Key("endpoint"), "diagnostic endpoint must be an absolute HTTP(S) URL with a host")
}
}
func resolveDiagnosticEndpoint(value **string, path configkit.Path, resolution *configkit.Resolution) {
if *value != nil && diagnosticEndpointValid(**value) {
recordStringNormalization(value, path, resolution, strings.TrimSpace(**value))
}
}
func resolveProfiles(entries []profileConfig, base configkit.Path, resolution *configkit.Resolution) {
if !eligibleResourceList(entries, func(v profileConfig) string { return v.ID }) {
return
}
for i := range entries {
entry := &entries[i]
path := base.Index(i)
if entry.Endpoint != nil && *entry.Endpoint != "" {
if normalized, err := domain.NormalizeOpenAICompatibleBaseEndpoint(*entry.Endpoint); err == nil {
recordStringNormalization(&entry.Endpoint, path.Key("endpoint"), resolution, normalized)
}
}
for _, field := range []struct {
value **string
name string
}{{&entry.ServiceTier, "service_tier"}, {&entry.ReasoningEffort, "reasoning_effort"}, {&entry.APIKeyEnv, "api_key_env"}} {
if *field.value != nil {
recordStringNormalization(field.value, path.Key(field.name), resolution, strings.TrimSpace(**field.value))
}
}
}
}