From 6056f93e55753e7516b42d8a24c3957d05bc0b68 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 22 Nov 2026 18:11:10 +0000 Subject: [PATCH] Add backend provider and cost configuration --- backends.go | 10 +++++ docs/formats.md | 6 ++- docs/roadmap/implementation.md | 2 + internal/configuration/load.go | 9 ++-- internal/configuration/load_test.go | 53 ++++++++++++++++++++--- internal/configuration/resolve_targets.go | 3 ++ internal/llm/provider.go | 12 +++++ internal/settings/settings.go | 8 +++- settings_contract_test.go | 18 ++++++++ 9 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 internal/llm/provider.go diff --git a/backends.go b/backends.go index ac629be..9d4e30f 100644 --- a/backends.go +++ b/backends.go @@ -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 } diff --git a/docs/formats.md b/docs/formats.md index 5ce469d..b2b9d80 100644 --- a/docs/formats.md +++ b/docs/formats.md @@ -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. diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 6421bd1..df86dee 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -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`. diff --git a/internal/configuration/load.go b/internal/configuration/load.go index f40332b..e2aae3f 100644 --- a/internal/configuration/load.go +++ b/internal/configuration/load.go @@ -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") } } } diff --git a/internal/configuration/load_test.go b/internal/configuration/load_test.go index 76e1559..f25e1dd 100644 --- a/internal/configuration/load_test.go +++ b/internal/configuration/load_test.go @@ -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"}}, diff --git a/internal/configuration/resolve_targets.go b/internal/configuration/resolve_targets.go index d8e741b..c392674 100644 --- a/internal/configuration/resolve_targets.go +++ b/internal/configuration/resolve_targets.go @@ -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) diff --git a/internal/llm/provider.go b/internal/llm/provider.go new file mode 100644 index 0000000..8468015 --- /dev/null +++ b/internal/llm/provider.go @@ -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 + } +} diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 118ef9c..8c42061 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -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 diff --git a/settings_contract_test.go b/settings_contract_test.go index 9863696..ae66f43 100644 --- a/settings_contract_test.go +++ b/settings_contract_test.go @@ -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) + } +}