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

@@ -26,6 +26,10 @@ type BackendConfig struct {
ID string
// Extends names a sibling backend to inherit through ConfigKit.
Extends *string
// Provider selects the backend's supported API conventions.
Provider *string
// ZeroCost enables a zero-cost fallback when no usable charge is reported.
ZeroCost *bool
// Endpoint is the required absolute HTTP(S) base URL after all layers merge.
Endpoint *string
// APIKeyEnv optionally names an environment variable, not a credential.
@@ -55,6 +59,12 @@ func BackendSource(label string, backends []BackendConfig) configkit.Source {
if config.Extends != nil {
fields["extends"] = map[string]any{"id": *config.Extends}
}
if config.Provider != nil {
fields["provider"] = *config.Provider
}
if config.ZeroCost != nil {
fields["zero_cost"] = *config.ZeroCost
}
if config.Endpoint != nil {
fields["endpoint"] = *config.Endpoint
}

View File

@@ -263,11 +263,13 @@ host; unlike the backend base endpoint, its path and query are allowed. Null
means fallback, while an explicit blank value is invalid.
Omitted/null `authentication` means no authentication adapter is declared. A
non-null object requires `kind`, which is trimmed and case-normalized; the
recognized values are `none` and `openrouter_key`. Its omitted/null endpoint
non-null object's optional `kind` is trimmed and case-normalized; accepted
values are `none`, `openrouter_key`, and `vllm_models`. Its omitted/null endpoint
uses the adapter's backend-relative destination (`openrouter_key` appends
`/key` to the effective backend base); an explicit endpoint follows the same
full-URL rules as reachability. `none` declares authentication inapplicable.
Without a selected adapter, or for `vllm_models`, authentication diagnostics
currently report an unsupported selection.
These values are configuration only: engine construction does not contact a
backend.

View File

@@ -124,6 +124,8 @@ Validation: prepared execution and coordinate observation contract tests.
## Stage 3 — Backend Configuration And Provider Selection
**Completed.**
Touch `internal/settings/settings.go`, `backends.go`, configuration ownership,
resolution/validation code, and the provider-selection functions in `internal/llm`.

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

View File

@@ -65,3 +65,21 @@ func TestSharedSmokeSettingsWorkWithBackendSource(t *testing.T) {
t.Fatalf("backend source = %+v, %v", result, err)
}
}
func TestBackendSourcePreservesProviderFieldPresence(t *testing.T) {
blank, disabled := "", false
result, err := configkit.Load[promptkit.Settings](promptkit.BackendSource("backends", []promptkit.BackendConfig{{ID: "omitted"}, {ID: "supplied", Provider: &blank, ZeroCost: &disabled}}))
if err != nil {
t.Fatal(err)
}
if result.Config.Backends[0].Provider != nil || result.Config.Backends[0].ZeroCost != nil {
t.Fatalf("omitted values: %+v", result.Config.Backends[0])
}
got := result.Config.Backends[1]
if got.Provider == nil || *got.Provider != "" || got.ZeroCost == nil || *got.ZeroCost {
t.Fatalf("supplied values: %+v", got)
}
if local := promptkit.LocalBackend("https://example.invalid/v1", 0); local.Provider != nil || local.ZeroCost != nil {
t.Fatalf("local defaults: %+v", local)
}
}