Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions internal/config/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,8 @@ func TestResolveAppliesProviderCatalogDefaults(t *testing.T) {
}
}

// A profile that names only the OpenGateway catalog ID gets the catalog's
// base URL, the "auto" default model and the env-var credential at load time.
func TestResolveAppliesOpenGatewayCatalogDefaults(t *testing.T) {
path := writeConfig(t, `{
"activeProvider": "gitlawb-opengateway",
Expand All @@ -1378,14 +1380,43 @@ func TestResolveAppliesOpenGatewayCatalogDefaults(t *testing.T) {
if resolved.Provider.BaseURL != "https://api.twigpine.com/v1" {
t.Fatalf("BaseURL = %q, want https://api.twigpine.com/v1", resolved.Provider.BaseURL)
}
if resolved.Provider.Model != "xiaomi/mimo-v2.6-pro" {
t.Fatalf("Model = %q, want xiaomi/mimo-v2.6-pro", resolved.Provider.Model)
if resolved.Provider.Model != "auto" {
t.Fatalf("Model = %q, want auto", resolved.Provider.Model)
}
if resolved.Provider.APIKeyEnv != "GITLAWB_OPENGATEWAY_API_KEY" || resolved.Provider.APIKey != "ogw_live_test" {
t.Fatalf("credential = (%q, %q), want GITLAWB_OPENGATEWAY_API_KEY env-resolved key", resolved.Provider.APIKeyEnv, resolved.Provider.APIKey)
}
}

// The catalog default only fills a profile that names no model. A profile saved
// while MiMo V2.6 Pro was the default persisted that id, and must keep it now
// that the default is the smart router.
func TestResolveKeepsSavedOpenGatewayModel(t *testing.T) {
path := writeConfig(t, `{
"activeProvider": "gitlawb-opengateway",
"providers": [{
"name": "gitlawb-opengateway",
"catalogID": "gitlawb-opengateway",
"providerKind": "openai-compatible",
"baseURL": "https://api.twigpine.com/v1",
"model": "xiaomi/mimo-v2.6-pro"
}]
}`)

resolved, err := Resolve(ResolveOptions{
UserConfigPath: path,
Env: map[string]string{
"GITLAWB_OPENGATEWAY_API_KEY": "ogw_live_test",
},
})
if err != nil {
t.Fatalf("Resolve() error = %v", err)
}
if resolved.Provider.Model != "xiaomi/mimo-v2.6-pro" {
t.Fatalf("Model = %q, want the saved model kept", resolved.Provider.Model)
}
}

// Profiles saved before the Twigpine rename persist the catalog's old base URL
// (https://opengateway.gitlawb.com/v1) explicitly. That host still serves the
// gateway, so those configs must keep resolving exactly as before: same host,
Expand Down
7 changes: 5 additions & 2 deletions internal/providercatalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ var descriptors = []Descriptor{
// Bearer ogw_live_… key; listed first and badged in every picker. The public
// GET /v1/models lists only active models and is what pickers show when
// reachable. DefaultModel must be an exact id from that list (not an alias)
// so onboarding keeps it selected after live discovery. The id and env var
// so onboarding keeps it selected after live discovery. It is the gateway's
// smart router, "auto": an account with no credits is routed to a free
// model instead of failing with 402 on a paid one. Only new setups and
// profiles that name no model get it; a saved model is kept. The id and env var
// keep their gitlawb names so saved configs keep resolving; the old host
// (opengateway.gitlawb.com) still serves clients and stays a legacy catalog
// endpoint so profiles saved against it resolve unchanged.
recommended(withLegacyBaseURLs(openAICompat("gitlawb-opengateway", "Twigpine OpenGateway", "https://api.twigpine.com/v1", "xiaomi/mimo-v2.6-pro", []string{"GITLAWB_OPENGATEWAY_API_KEY"}, "gitlawb opengateway", "opengateway", "twigpine opengateway", "twigpine"), "https://opengateway.gitlawb.com/v1")),
recommended(withLegacyBaseURLs(openAICompat("gitlawb-opengateway", "Twigpine OpenGateway", "https://api.twigpine.com/v1", "auto", []string{"GITLAWB_OPENGATEWAY_API_KEY"}, "gitlawb opengateway", "opengateway", "twigpine opengateway", "twigpine"), "https://opengateway.gitlawb.com/v1")),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// aimlapi.com — OpenAI-compatible aggregating gateway, also badged as
// recommended (second, right after the OpenGateway default). The built-in TUI
// onboarding can register/top up an account and save the issued key; the partner
Expand Down
12 changes: 8 additions & 4 deletions internal/providercatalog/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func TestRecommendedProvidersAreTopOfCatalog(t *testing.T) {
}
}

// TestRecommendedProviderEndpoint pins the OpenGateway descriptor: name, base
// URL, the "auto" default model and transport.
func TestRecommendedProviderEndpoint(t *testing.T) {
descriptor, ok := Get("gitlawb-opengateway")
if !ok {
Expand All @@ -140,10 +142,12 @@ func TestRecommendedProviderEndpoint(t *testing.T) {
t.Fatalf("OpenGateway base URL = %q, want %q", descriptor.DefaultBaseURL, "https://api.twigpine.com/v1")
}
// The default must be an exact id from the live /v1/models list (not a
// gateway alias such as the bare "mimo-v2.6-pro"): onboarding keeps the
// default selected after live discovery only on an exact id match.
if descriptor.DefaultModel != "xiaomi/mimo-v2.6-pro" {
t.Fatalf("OpenGateway default model = %q, want %q", descriptor.DefaultModel, "xiaomi/mimo-v2.6-pro")
// gateway alias such as "twigpine/auto"): onboarding and the provider
// wizard keep the default selected after live discovery only on an exact id
// match. It is the smart
// router so a new account with no credits is routed to a free model.
if descriptor.DefaultModel != "auto" {
t.Fatalf("OpenGateway default model = %q, want %q", descriptor.DefaultModel, "auto")
}
if descriptor.Transport != TransportOpenAICompatible {
t.Fatalf("OpenGateway transport = %q, want %q", descriptor.Transport, TransportOpenAICompatible)
Expand Down
13 changes: 8 additions & 5 deletions internal/providermodelcatalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,15 @@ var curatedModels = map[string][]Model{
// GET /v1/models; pickers and onboarding show that list whenever it is
// reachable (see providermodeldiscovery.DiscoverCatalog). This is only the
// offline fallback, so keep it to ids the gateway is expected to keep
// serving: the smart router, the catalog default, and its fast sibling. Do
// not add promo or time-limited models here. Users can still type any id
// the gateway accepts.
// serving: the smart router (the catalog default), MiMo V2.6 Pro, and its
// fast sibling. Do not add promo or time-limited models here. Users can
// still type any id the gateway accepts. "auto" carries no context window on
// purpose: the gateway reports none (the serving model varies per request),
// so compaction sizes itself from modelregistry.FallbackContextWindow and
// the context gauges show no guessed denominator.
"gitlawb-opengateway": {
{ID: "xiaomi/mimo-v2.6-pro", Description: "catalog default", ContextWindow: 262_144},
{ID: "auto", Description: "smart routing (picks a capable model per request)"},
{ID: "auto", Description: "catalog default"},
{ID: "xiaomi/mimo-v2.6-pro", Description: "general model (Xiaomi MiMo V2.6 Pro)", ContextWindow: 262_144},
{ID: "xiaomi/mimo-v2.6-flash", Description: "fast model (Xiaomi MiMo V2.6 Flash)", ContextWindow: 262_144},
},
"atomic-chat": {
Expand Down
22 changes: 21 additions & 1 deletion internal/providermodelcatalog/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/Twigpine/zero/internal/providercatalog"
)

// TestModelsAreProviderScoped checks that each provider's offline model list
// holds its own models and none borrowed from another provider.
func TestModelsAreProviderScoped(t *testing.T) {
tests := []struct {
provider string
Expand Down Expand Up @@ -76,7 +78,7 @@ func TestModelsAreProviderScoped(t *testing.T) {
{
// Offline fallback only; the live /v1/models list is shown when reachable.
provider: "gitlawb-opengateway",
want: []string{"xiaomi/mimo-v2.6-pro", "auto", "xiaomi/mimo-v2.6-flash"},
want: []string{"auto", "xiaomi/mimo-v2.6-pro", "xiaomi/mimo-v2.6-flash"},
notWant: []string{
"openai/gpt-4.1", "claude-sonnet-4.5",
"mimo-v2.5-pro", "xiaomi/mimo-v2.5-pro", "qwen-plus", "glm-4.6", "gemini-2.5-pro",
Expand Down Expand Up @@ -116,6 +118,24 @@ func TestModelsAreProviderScoped(t *testing.T) {
}
}

// The offline OpenGateway list leads with the smart router, the catalog
// default. The gateway reports no context window for it (the serving model
// varies per request), so the curated entry must not invent one: an unknown
// window sizes compaction from modelregistry.FallbackContextWindow.
func TestOpenGatewayModelsLeadWithAutoDefault(t *testing.T) {
descriptor, ok := providercatalog.Get("gitlawb-opengateway")
if !ok {
t.Fatal("provider gitlawb-opengateway missing from catalog")
}
models := Models(descriptor)
if len(models) == 0 || models[0].ID != "auto" || models[0].ID != descriptor.DefaultModel {
t.Fatalf("OpenGateway models = %#v, want the auto default first", modelIDs(models))
}
if models[0].ContextWindow != 0 {
t.Fatalf("auto context window = %d, want unknown (0)", models[0].ContextWindow)
}
}

func TestModelsDoNotAliasMutableCatalogState(t *testing.T) {
descriptor, ok := providercatalog.Get("minimax")
if !ok {
Expand Down
27 changes: 24 additions & 3 deletions internal/providermodelcatalog/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,27 @@ func TestParseModelsDevProviderScopesAndMapsMetadata(t *testing.T) {
}
}

// TestParseOpenGatewayCatalogSkipsRetiredModels checks that retired gateway
// models are dropped and that null metadata on "auto" parses as unknown.
func TestParseOpenGatewayCatalogSkipsRetiredModels(t *testing.T) {
// Mirrors GET https://api.twigpine.com/v1/models?status=all once the gateway
// retires models: an active entry plus a retired one carrying a successor.
// retires models: the smart router (listed with null metadata, since the
// serving model varies per request), an active entry, plus a retired one
// carrying a successor.
body := []byte(`{
"object": "list",
"data": [
{
"id": "auto",
"object": "model",
"name": "Auto (smart routing)",
"description": "picks the cheapest capable model",
"aliases": ["twigpine/auto", "gitlawb/auto"],
"context_window": null,
"pricing": null,
"supports_tools": null,
"status": "active"
},
{
"id": "xiaomi/mimo-v2.6-pro",
"object": "model",
Expand Down Expand Up @@ -100,8 +115,14 @@ func TestParseOpenGatewayCatalogSkipsRetiredModels(t *testing.T) {
if err != nil {
t.Fatalf("ParseOpenGatewayCatalog returned error: %v", err)
}
if got := strings.Join(modelIDs(models), ","); got != "xiaomi/mimo-v2.6-pro" {
t.Fatalf("models = %q, want only the active entry", got)
// Sorted by description label (flagship…, picks…).
if got := strings.Join(modelIDs(models), ","); got != "xiaomi/mimo-v2.6-pro,auto" {
t.Fatalf("models = %q, want only the active entries", got)
}
// The router's null context window and pricing parse as unknown (0), not
// as an error or a guessed value.
if auto := models[1]; auto.ContextWindow != 0 || auto.InputCost != 0 || auto.OutputCost != 0 {
t.Fatalf("auto metadata = %#v, want unknown context window and pricing", auto)
}
descriptor, ok := providercatalog.Get("gitlawb-opengateway")
if !ok {
Expand Down
21 changes: 14 additions & 7 deletions internal/providermodeldiscovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -346,12 +347,18 @@ func TestDiscoverCatalogOpenGatewayUsesLiveListWithoutKey(t *testing.T) {
}
}

// TestDiscoverCatalogOpenGatewayDropsRetiredAndKeepsDefault checks that live
// OpenGateway discovery hides retired models and still returns the default.
func TestDiscoverCatalogOpenGatewayDropsRetiredAndKeepsDefault(t *testing.T) {
// Both the catalog fetch and the live /v1/models probe return one active and
// one retired entry. Users must only ever see the active one, and the
// catalog's default model must be among the ids discovery returns (onboarding
// keeps the default selected only on an exact id match).
// Both the catalog fetch and the live /v1/models probe return the smart
// router, active and retired entries, and a paid model whose id sorts
// before "auto", as the live list does. Users must only ever see the active
// ones, and the catalog's default model must be among the ids discovery
// returns (onboarding and the provider wizard keep the default selected only
// on an exact id match). It need not be first: results are sorted by id.
const body = `{"object":"list","data":[
{"id":"auto","name":"Auto (smart routing)","context_window":null,"pricing":null,"supports_tools":null,"status":"active"},
{"id":"anthropic/claude-haiku-5.5","name":"Claude Haiku 5.5","context_window":204800,"supports_tools":true,"status":"active"},
{"id":"xiaomi/mimo-v2.6-pro","name":"MiMo V2.6-Pro","context_window":262144,"supports_tools":true,"status":"active"},
{"id":"qwen/qwen-plus","name":"Qwen Plus","context_window":131072,"status":"retired","successor":"qwen/qwen3.7-max"}
]}`
Expand Down Expand Up @@ -385,10 +392,10 @@ func TestDiscoverCatalogOpenGatewayDropsRetiredAndKeepsDefault(t *testing.T) {
if err != nil {
t.Fatalf("DiscoverCatalog: %v", err)
}
if got := strings.Join(modelIDs(models), ","); got != "xiaomi/mimo-v2.6-pro" {
t.Fatalf("models = %q, want only the active entry", got)
if got := strings.Join(modelIDs(models), ","); got != "anthropic/claude-haiku-5.5,auto,xiaomi/mimo-v2.6-pro" {
t.Fatalf("models = %q, want only the active entries", got)
}
if models[0].ID != descriptor.DefaultModel {
if !slices.Contains(modelIDs(models), descriptor.DefaultModel) {
t.Fatalf("default model %q missing from discovered %v", descriptor.DefaultModel, modelIDs(models))
}
if strings.Join(paths, ",") != "/catalog/v1/models,/v1/models" {
Expand Down
30 changes: 29 additions & 1 deletion internal/tui/provider_wizard_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ func (m model) providerModelDiscoveryCmd() tea.Cmd {
}
}

// applyProviderModelsDiscovered replaces the wizard's model list with a live
// discovery result. It ignores stale results, and on an error or an empty list
// it keeps the built-in list and records why.
func (m model) applyProviderModelsDiscovered(msg providerModelsDiscoveredMsg) model {
wizard := m.providerWizard
if wizard == nil || wizard.step != providerWizardStepModel || wizard.currentProvider().ID != msg.providerID || msg.token != wizard.discoveryToken {
Expand All @@ -280,16 +283,41 @@ func (m model) applyProviderModelsDiscovered(msg providerModelsDiscoveredMsg) mo
wizard.refreshModels()
return m
}
// Discovery returns models sorted by ID, so row 0 is whatever sorts first,
// not the model the cursor was on. Keep that model, else the provider
// default, so discovery does not move the cursor from a free default such
// as OpenGateway's "auto" onto a paid model that happens to sort first.
currentID := wizard.currentModel().ID
wizard.models = models
wizard.selectedModel = 0
wizard.modelSource = providerWizardModelsSource(msg.models)
if wizard.modelSource == "" {
wizard.modelSource = "live"
}
wizard.modelLoadError = ""
wizard.selectModelID(currentID, wizard.currentProvider().DefaultModel)
return m
}

// selectModelID puts the cursor on the first of ids that the filtered model
// list contains, trying them in order. It falls back to the first row when none
// of them is listed.
func (wizard *providerWizardState) selectModelID(ids ...string) {
models := wizard.filteredModels()
for _, id := range ids {
id = strings.TrimSpace(id)
if id == "" {
continue
}
for index, model := range models {
if model.ID == id {
wizard.selectedModel = index
return
}
}
}
wizard.selectedModel = 0
}

func providerWizardModelsFromDiscovery(models []providermodeldiscovery.Model) []providerWizardModel {
result := make([]providerWizardModel, 0, len(models))
for _, model := range models {
Expand Down
Loading
Loading