-
-
Notifications
You must be signed in to change notification settings - Fork 70
feat(providers): per-model metadata overrides for custom endpoints #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SantiagoDePolonia
merged 5 commits into
ENTERPILOT:main
from
epicfilemcnulty:feat/provider-model-metadata
Apr 23, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f825f18
feat(providers): per-model metadata overrides for custom endpoints
epicfilemcnulty 2d341f7
fix(providers): tighten metadata override invariants and locking
epicfilemcnulty 7e59f87
fix(providers): harden metadata override helpers
epicfilemcnulty f1d884a
fix(providers): use reflect.IsZero for override-empty helpers
epicfilemcnulty b68541d
fix(providers): count config overrides and treat empty collections as…
epicfilemcnulty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "gomodel/internal/core" | ||
| ) | ||
|
|
||
| // RawProviderModel is a single entry under providers.<name>.models. It supports | ||
| // two YAML shapes so operators can opt into rich metadata without churning | ||
| // simple configs: | ||
| // | ||
| // models: | ||
| // - some-model-id # bare string | ||
| // - id: local-model # mapping with optional metadata | ||
| // metadata: | ||
| // context_window: 131072 | ||
| // capabilities: | ||
| // tools: true | ||
| // | ||
| // Metadata is merged onto whatever the remote model registry supplies, with | ||
| // config-declared fields taking precedence. This lets local providers (Ollama, | ||
| // custom OpenAI-compatible endpoints) advertise their context windows, pricing, | ||
| // and capabilities via /v1/models even when the remote registry has no entry. | ||
| type RawProviderModel struct { | ||
| ID string `yaml:"id"` | ||
| Metadata *core.ModelMetadata `yaml:"metadata,omitempty"` | ||
| } | ||
|
|
||
| // UnmarshalYAML accepts either a bare string (model ID) or a mapping with id and metadata. | ||
| func (m *RawProviderModel) UnmarshalYAML(node *yaml.Node) error { | ||
| switch node.Kind { | ||
| case yaml.ScalarNode: | ||
| var id string | ||
| if err := node.Decode(&id); err != nil { | ||
| return fmt.Errorf("provider model: %w", err) | ||
| } | ||
| id = strings.TrimSpace(id) | ||
| if id == "" { | ||
| return fmt.Errorf("provider model: id is required") | ||
| } | ||
| m.ID = id | ||
| return nil | ||
| case yaml.MappingNode: | ||
| type rawAlias RawProviderModel | ||
| var alias rawAlias | ||
| if err := node.Decode(&alias); err != nil { | ||
| return fmt.Errorf("provider model: %w", err) | ||
| } | ||
| *m = RawProviderModel(alias) | ||
| m.ID = strings.TrimSpace(m.ID) | ||
| if m.ID == "" { | ||
| return fmt.Errorf("provider model: id is required") | ||
| } | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("provider model: expected scalar or mapping, got kind %d", node.Kind) | ||
| } | ||
| } | ||
|
SantiagoDePolonia marked this conversation as resolved.
|
||
|
|
||
| // ProviderModelIDs returns the ID of each model entry, preserving order and | ||
| // dropping entries with empty IDs. | ||
| func ProviderModelIDs(models []RawProviderModel) []string { | ||
| if len(models) == 0 { | ||
| return nil | ||
| } | ||
| ids := make([]string, 0, len(models)) | ||
| for _, m := range models { | ||
| if m.ID != "" { | ||
| ids = append(ids, m.ID) | ||
| } | ||
| } | ||
| return ids | ||
| } | ||
|
|
||
| // ProviderModelMetadataOverrides returns id -> metadata for entries with | ||
| // non-nil Metadata. Returns nil if no entries declare metadata. | ||
| func ProviderModelMetadataOverrides(models []RawProviderModel) map[string]*core.ModelMetadata { | ||
| var out map[string]*core.ModelMetadata | ||
| for _, m := range models { | ||
| if m.ID == "" || m.Metadata == nil { | ||
| continue | ||
| } | ||
| if out == nil { | ||
| out = make(map[string]*core.ModelMetadata) | ||
| } | ||
| out[m.ID] = m.Metadata | ||
| } | ||
| return out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "gomodel/internal/core" | ||
| ) | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_String(t *testing.T) { | ||
| const data = `- some-model` | ||
| var models []RawProviderModel | ||
| if err := yaml.Unmarshal([]byte(data), &models); err != nil { | ||
| t.Fatalf("unmarshal: %v", err) | ||
| } | ||
| if len(models) != 1 { | ||
| t.Fatalf("len = %d, want 1", len(models)) | ||
| } | ||
| if models[0].ID != "some-model" { | ||
| t.Errorf("ID = %q, want some-model", models[0].ID) | ||
| } | ||
| if models[0].Metadata != nil { | ||
| t.Errorf("Metadata = %+v, want nil", models[0].Metadata) | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_MappingWithMetadata(t *testing.T) { | ||
| const data = ` | ||
| - id: local-model | ||
| metadata: | ||
| display_name: Local Model | ||
| context_window: 131072 | ||
| max_output_tokens: 8192 | ||
| modes: [chat] | ||
| capabilities: | ||
| tools: true | ||
| vision: false | ||
| pricing: | ||
| currency: USD | ||
| input_per_mtok: 0 | ||
| output_per_mtok: 0 | ||
| ` | ||
| var models []RawProviderModel | ||
| if err := yaml.Unmarshal([]byte(data), &models); err != nil { | ||
| t.Fatalf("unmarshal: %v", err) | ||
| } | ||
| if len(models) != 1 { | ||
| t.Fatalf("len = %d, want 1", len(models)) | ||
| } | ||
| m := models[0] | ||
| if m.ID != "local-model" { | ||
| t.Errorf("ID = %q, want local-model", m.ID) | ||
| } | ||
| if m.Metadata == nil { | ||
| t.Fatal("Metadata = nil, want non-nil") | ||
| } | ||
| if m.Metadata.DisplayName != "Local Model" { | ||
| t.Errorf("DisplayName = %q", m.Metadata.DisplayName) | ||
| } | ||
| if m.Metadata.ContextWindow == nil || *m.Metadata.ContextWindow != 131072 { | ||
| t.Errorf("ContextWindow = %v, want 131072", m.Metadata.ContextWindow) | ||
| } | ||
| if m.Metadata.MaxOutputTokens == nil || *m.Metadata.MaxOutputTokens != 8192 { | ||
| t.Errorf("MaxOutputTokens = %v, want 8192", m.Metadata.MaxOutputTokens) | ||
| } | ||
| if got := m.Metadata.Capabilities["tools"]; !got { | ||
| t.Errorf("Capabilities[tools] = %v, want true", got) | ||
| } | ||
| if m.Metadata.Pricing == nil || m.Metadata.Pricing.Currency != "USD" { | ||
| t.Errorf("Pricing = %+v", m.Metadata.Pricing) | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_MixedList(t *testing.T) { | ||
| const data = ` | ||
| - plain-id | ||
| - id: rich-model | ||
| metadata: | ||
| context_window: 4096 | ||
| ` | ||
| var models []RawProviderModel | ||
| if err := yaml.Unmarshal([]byte(data), &models); err != nil { | ||
| t.Fatalf("unmarshal: %v", err) | ||
| } | ||
| if len(models) != 2 { | ||
| t.Fatalf("len = %d, want 2", len(models)) | ||
| } | ||
| if models[0].ID != "plain-id" || models[0].Metadata != nil { | ||
| t.Errorf("models[0] = %+v", models[0]) | ||
| } | ||
| if models[1].ID != "rich-model" || models[1].Metadata == nil { | ||
| t.Errorf("models[1] = %+v", models[1]) | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_RejectsMappingWithoutID(t *testing.T) { | ||
| const data = ` | ||
| - metadata: | ||
| context_window: 1024 | ||
| ` | ||
| var models []RawProviderModel | ||
| err := yaml.Unmarshal([]byte(data), &models) | ||
| if err == nil { | ||
| t.Fatal("expected error for mapping without id, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_RejectsEmptyScalar(t *testing.T) { | ||
| const data = `- ""` | ||
| var models []RawProviderModel | ||
| err := yaml.Unmarshal([]byte(data), &models) | ||
| if err == nil { | ||
| t.Fatal("expected error for empty scalar id, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_RejectsWhitespaceOnlyScalar(t *testing.T) { | ||
| const data = `- " "` | ||
| var models []RawProviderModel | ||
| err := yaml.Unmarshal([]byte(data), &models) | ||
| if err == nil { | ||
| t.Fatal("expected error for whitespace-only scalar id, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_RejectsWhitespaceOnlyMappingID(t *testing.T) { | ||
| const data = ` | ||
| - id: " " | ||
| metadata: | ||
| context_window: 1024 | ||
| ` | ||
| var models []RawProviderModel | ||
| err := yaml.Unmarshal([]byte(data), &models) | ||
| if err == nil { | ||
| t.Fatal("expected error for whitespace-only mapping id, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestRawProviderModel_UnmarshalYAML_TrimsScalar(t *testing.T) { | ||
| const data = `- " some-model "` | ||
| var models []RawProviderModel | ||
| if err := yaml.Unmarshal([]byte(data), &models); err != nil { | ||
| t.Fatalf("unmarshal: %v", err) | ||
| } | ||
| if models[0].ID != "some-model" { | ||
| t.Errorf("ID = %q, want some-model (trimmed)", models[0].ID) | ||
| } | ||
| } | ||
|
|
||
| func TestProviderModelIDs(t *testing.T) { | ||
| models := []RawProviderModel{ | ||
| {ID: "a"}, | ||
| {ID: "b", Metadata: &core.ModelMetadata{}}, | ||
| {ID: ""}, // filtered | ||
| } | ||
| ids := ProviderModelIDs(models) | ||
| if len(ids) != 2 || ids[0] != "a" || ids[1] != "b" { | ||
| t.Errorf("ids = %v, want [a b]", ids) | ||
| } | ||
| if got := ProviderModelIDs(nil); got != nil { | ||
| t.Errorf("nil input -> %v, want nil", got) | ||
| } | ||
| } | ||
|
|
||
| func TestProviderModelMetadataOverrides(t *testing.T) { | ||
| ctxWindow := 2048 | ||
| models := []RawProviderModel{ | ||
| {ID: "plain"}, | ||
| {ID: "rich", Metadata: &core.ModelMetadata{ContextWindow: &ctxWindow}}, | ||
| {ID: "", Metadata: &core.ModelMetadata{ContextWindow: &ctxWindow}}, // filtered | ||
| } | ||
| overrides := ProviderModelMetadataOverrides(models) | ||
| if len(overrides) != 1 { | ||
| t.Fatalf("len = %d, want 1", len(overrides)) | ||
| } | ||
| if overrides["rich"].ContextWindow == nil || *overrides["rich"].ContextWindow != ctxWindow { | ||
| t.Errorf("overrides[rich] = %+v", overrides["rich"]) | ||
| } | ||
| if got := ProviderModelMetadataOverrides(nil); got != nil { | ||
| t.Errorf("nil input -> %v, want nil", got) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.