Add backend provider and cost configuration

This commit is contained in:
2026-11-22 18:11:10 +00:00
parent f2b0ed8d6c
commit 6056f93e55
9 changed files with 108 additions and 13 deletions

View File

@@ -291,6 +291,9 @@ func validateBackendRecord(backend backendConfig, base configkit.Path, validatio
if backend.Endpoint == nil || *backend.Endpoint == "" {
validation.Error(base.Key("endpoint"), "endpoint is required")
}
if backend.Provider != nil && !llm.SupportedProvider(*backend.Provider) {
validation.Error(base.Key("provider"), "unsupported provider")
}
if backend.APIKeyEnv != nil && *backend.APIKeyEnv != "" && !envName.MatchString(*backend.APIKeyEnv) {
validation.Error(base.Key("api_key_env"), "environment variable name is invalid")
}
@@ -319,10 +322,8 @@ func validateBackendRecord(backend backendConfig, base configkit.Path, validatio
}
if authentication := smokeTests.Authentication; authentication != nil {
kindPath := smokePath.Key("authentication").Key("kind")
if authentication.Kind == nil {
validation.Error(kindPath, "authentication kind is required")
} else if *authentication.Kind != "none" && *authentication.Kind != "openrouter_key" {
validation.Error(kindPath, "authentication kind must be none or openrouter_key")
if authentication.Kind != nil && *authentication.Kind != "none" && *authentication.Kind != "openrouter_key" && *authentication.Kind != "vllm_models" {
validation.Error(kindPath, "unsupported authentication kind")
}
}
}

View File

@@ -532,6 +532,54 @@ func TestLoadInterpretsBackendNormalizationAndNullDefault(t *testing.T) {
}
}
func TestBackendProviderFinalConfiguration(t *testing.T) {
underlays := []configkit.Underlay{{Inputs: []configkit.Input{mapping("defaults", map[string]any{
"backends": []any{map[string]any{"id": "base", "endpoint": "https://example.invalid/v1", "provider": "bad", "zero_cost": true}},
})}}}
main := mapping("override", map[string]any{"backends": []any{
map[string]any{"id": "base", "endpoint": "https://example.invalid/v1", "provider": " OPENROUTER ", "zero_cost": false, "smoke_tests": map[string]any{"authentication": map[string]any{}}},
map[string]any{"id": "child", "extends": map[string]any{"id": "base"}},
map[string]any{"id": "cleared", "extends": map[string]any{"id": "base"}, "provider": nil, "zero_cost": nil},
map[string]any{"id": "generic", "endpoint": "https://example.invalid/v1"},
}})
state, err := load(main, underlays)
if err != nil {
var detail *configkit.LoadError
if errors.As(err, &detail) {
t.Fatalf("%v: %+v", err, detail.Diagnostics())
}
t.Fatal(err)
}
for _, name := range []string{"base", "child"} {
b := state.backendRecords[name]
if b.Provider == nil || *b.Provider != "openrouter" || b.ZeroCost == nil || *b.ZeroCost {
t.Fatalf("%s: %+v", name, b)
}
}
for _, name := range []string{"cleared", "generic"} {
b := state.backendRecords[name]
if b.Provider != nil || b.ZeroCost != nil {
t.Fatalf("%s: %+v", name, b)
}
}
field, found := state.inspection.Metadata().Lookup(configkit.Keys("promptkit", "backends").Index(0).Key("provider"))
if !found || field.Origin.Operation != configkit.ChangeNormalization {
t.Fatalf("provider normalization: %+v", field)
}
for _, provider := range []string{"openai-compatible", "openrouter", "vllm", " ", ""} {
_, err := load(mapping("provider", map[string]any{"backends": []any{map[string]any{"id": "x", "endpoint": "https://example.invalid/v1", "provider": provider, "smoke_tests": map[string]any{"authentication": map[string]any{"kind": " VLLM_MODELS "}}}}}), nil)
if err != nil {
t.Errorf("provider %q: %v", provider, err)
}
}
for _, fields := range []map[string]any{{"provider": "unsupported"}, {"zero_cost": "false"}} {
fields["id"], fields["endpoint"] = "x", "https://example.invalid/v1"
if _, err := load(mapping("invalid", map[string]any{"backends": []any{fields}}), nil); err == nil {
t.Fatalf("accepted invalid fields: %v", fields)
}
}
}
func TestLoadBackendSmokeTestsOmissionAndNull(t *testing.T) {
for _, test := range []struct {
name string
@@ -638,11 +686,6 @@ func TestLoadRejectsInvalidFinalBackendSmokeTests(t *testing.T) {
data: map[string]any{"default_profile": " \t "},
path: "/promptkit/backends/0/smoke_tests/default_profile",
},
{
name: "authentication kind required",
data: map[string]any{"authentication": map[string]any{}},
path: "/promptkit/backends/0/smoke_tests/authentication/kind",
},
{
name: "unknown authentication kind",
data: map[string]any{"authentication": map[string]any{"kind": "bearer"}},

View File

@@ -15,6 +15,9 @@ func resolveBackends(entries []backendConfig, base configkit.Path, resolution *c
for i := range entries {
entry := &entries[i]
path := base.Index(i)
if entry.Provider != nil {
recordStringNormalization(&entry.Provider, path.Key("provider"), resolution, strings.ToLower(strings.TrimSpace(*entry.Provider)))
}
if entry.Endpoint != nil {
if normalized, err := domain.NormalizeOpenAICompatibleBaseEndpoint(*entry.Endpoint); err == nil {
recordStringNormalization(&entry.Endpoint, path.Key("endpoint"), resolution, normalized)

12
internal/llm/provider.go Normal file
View File

@@ -0,0 +1,12 @@
package llm
// SupportedProvider reports whether a normalized provider selection is supported.
// An empty selection uses the generic OpenAI-compatible conventions.
func SupportedProvider(provider string) bool {
switch provider {
case "", "openai-compatible", "openrouter", "vllm":
return true
default:
return false
}
}

View File

@@ -37,6 +37,10 @@ type CollectionSettings struct {
type BackendSettings struct {
// ID identifies the backend in its resource list.
ID string `yaml:"id"`
// Provider selects response and diagnostic conventions when supplied.
Provider *string `yaml:"provider"`
// ZeroCost enables a configured-zero fallback when no cost is reported.
ZeroCost *bool `yaml:"zero_cost"`
// Endpoint is the backend's absolute base URL.
Endpoint *string `yaml:"endpoint"`
// APIKeyEnv names an environment variable containing a credential.
@@ -163,7 +167,6 @@ type BackendSmokeTestConfig struct {
// diagnostics. A nil endpoint uses the effective backend endpoint.
Reachability *ReachabilityProbeConfig `json:"reachability,omitempty" yaml:"reachability,omitempty"`
// Authentication optionally declares a named authentication diagnostic.
// A supplied object requires Kind.
Authentication *AuthenticationProbeConfig `json:"authentication,omitempty" yaml:"authentication,omitempty"`
}
@@ -178,7 +181,8 @@ type ReachabilityProbeConfig struct {
// AuthenticationProbeConfig configures an optional named authentication diagnostic.
type AuthenticationProbeConfig struct {
// Kind selects the authentication adapter. Supported values are "none" and
// "openrouter_key"; surrounding whitespace and case are normalized.
// "openrouter_key" and "vllm_models"; surrounding whitespace and case are
// normalized. Nil leaves adapter selection to the provider default.
Kind *string `json:"kind,omitempty" yaml:"kind,omitempty"`
// Endpoint is an absolute HTTP(S) URL, including any desired path or query.
// Nil selects the adapter's backend-relative destination; an explicit blank