diff --git a/internal/router/chat.go b/internal/router/chat.go index 159e3de..d972761 100644 --- a/internal/router/chat.go +++ b/internal/router/chat.go @@ -130,6 +130,13 @@ func (r *Router) principalFor(req *http.Request, user store.User) (auth.Principa // 503 the reader cannot interpret, and the allowlist is applied so the picker // never shows a model the request would refuse. // +// One entry per model, under its catalog name. An endpoint advertises every +// name a client may address it by — the model's own and all of its aliases, +// because that is what the router matches against — so taking those strings as +// they come listed the same model once for each name it has. A picker offering +// "coder", "coder-7b" and "qwen2.5-coder-7b-instruct" as three choices invites +// somebody to compare them. +// // Real models are ordered ahead of simulated ones so the browser's default // selection — the first option — is always a model that answers. Sorted purely // by name, "big-model" came first and every visitor who pressed Send without @@ -143,12 +150,15 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { var out []web.ChatModel for _, s := range r.reg.Snapshots("") { for _, m := range s.Models { - if _, dup := seen[m]; dup { + // Resolved rather than taken as sent: an alias and the name it + // stands for are one model, and the catalog is what says so. + name, entry := r.resolve(m) + if _, dup := seen[name]; dup { continue } - seen[m] = struct{}{} - if r.permits(principal, m) { - out = append(out, web.ChatModel{Name: m, Simulated: r.modelMap()[m].Simulated()}) + seen[name] = struct{}{} + if r.permits(principal, name) { + out = append(out, web.ChatModel{Name: name, Simulated: entry.Simulated()}) } } } diff --git a/internal/router/chat_models_test.go b/internal/router/chat_models_test.go new file mode 100644 index 0000000..a4870ca --- /dev/null +++ b/internal/router/chat_models_test.go @@ -0,0 +1,80 @@ +package router + +import ( + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/config" + "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// chatRouter mirrors what the control plane publishes: an endpoint advertises +// every name a client may address it by, which is the model's own name and all +// of its aliases. +func chatRouter(t *testing.T, engineURL string) (*Router, store.User) { + t.Helper() + + cfg := baseConfig(config.Endpoint{ + ID: "a", URL: engineURL, + Models: []string{"qwen2.5-coder-7b-instruct", "coder-7b", "coder"}, + }) + cfg.Models = []catalog.Model{{ + Name: "qwen2.5-coder-7b-instruct", Aliases: []string{"coder-7b", "coder"}, + }} + cfg.Auth.Enabled = true + cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db") + rt := newTestRouter(t, cfg) + + org, err := rt.store.CreateOrg(t.Context(), "acme") + if err != nil { + t.Fatal(err) + } + team, err := rt.store.CreateTeam(t.Context(), org, "platform") + if err != nil { + t.Fatal(err) + } + project, err := rt.store.CreateProject(t.Context(), team, "gateway") + if err != nil { + t.Fatal(err) + } + return rt, store.User{ID: "usr-1", ProjectID: project} +} + +func TestTheChatPickerOffersAModelOnceRatherThanOncePerAlias(t *testing.T) { + // An endpoint advertises the model's name and every alias, because that is + // what the router matches a request against. Listing those strings as they + // came gave the picker "coder", "coder-7b" and the full name as three + // separate choices for one model. + e := newFakeEngine(t, "qwen2.5-coder-7b-instruct") + rt, user := chatRouter(t, e.srv.URL) + waitForEndpoint(t, rt) + + got := rt.modelsFor(httptest.NewRequest("GET", "/chat", nil), user) + if len(got) != 1 { + names := make([]string, len(got)) + for i, m := range got { + names[i] = m.Name + } + t.Fatalf("the picker offers %d entries for one model: %v", len(got), names) + } + // The catalog name, not whichever alias happened to be listed first: it is + // the one that appears in the model list, the usage table and the logs. + if got[0].Name != "qwen2.5-coder-7b-instruct" { + t.Errorf("offered as %q, expected the catalog name", got[0].Name) + } +} + +func waitForEndpoint(t *testing.T, rt *Router) { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + if len(rt.reg.Snapshots("")) > 0 { + return + } + time.Sleep(20 * time.Millisecond) + } + t.Fatal("no endpoint became available") +}