Files
magpie/internal/gateway/models_kind_test.go
T
yetone 6bb90df8e9 gateway, gui: an added provider's embedding and rerank models are listed (@rfiacne on X)
@rfiacne asked why an added provider's embedding models aren't
recognised. Routing them already worked: /v1/embeddings and /v1/rerank
took a bare id, provider/id or a slashed id from the provider's fetched
list. But they were nowhere a user looks. GET /v1/models answered
?kind=embedding with 400 and never listed them, and the provider editor
showed only a count, and only when the provider had no chat model.

- /v1/models?kind= takes embedding and rerank (all includes them). Each
  row is provider/model with "kind", owned_by, magpie_label and
  supported_endpoint_types embeddings or jina-rerank, the names Cherry
  Studio's new-api provider reads (listModels.ts ENDPOINT_TYPE_ALIASES).
  Disabled models and those a held key can't reach are left out. The
  default list stays chat models (TestKindGroupsServeEachAPIByRoute).
  Cherry Studio, listed every kind already, gets them too for its
  knowledge bases; ?format=text stays chat. A remote magpie asks by
  headers, not ?kind=, so its list is unchanged.
- gateway.RetrievalModels: the fetched list's Serves models and the
  preset's, each id once, shared by the list and the GUI.
- The provider editor lists Embedding models and Rerank models as read
  only chips (embedIds/rerankIds, retrievalRows), in every language.
  The script budgets rise by 354 for it: the editor draws it, so there
  is no page to move it to.
- docs: integrating.md, the site's integrate page (en, zh), and
  providers-accounts.md; site/node docs-index.mjs rebuilt llms-full.txt
  and search/*.json for them.

Run on mote (load ~20-28 of 18 cores; mica was at 40 of 12), at
6ce8f232c, rebased on origin/main 6635dec7c:
- go test -tags nogui -run 'TestAddedProvidersRetrievalModelsListed|
  TestRetrievalModelsOnce|TestKindGroupsServeEachAPIByRoute|Kind|Cherry|
  Models' ./internal/gateway: ok. TestAddedProvidersRetrievalModelsListed
  is red on the old gateway.go (?kind=embedding,rerank: 400).
- go test -tags nogui -run 'Budget|Provider' ./internal/gui: ok (before
  the rebase; the budget was re-measured after it, at exactly the new
  numbers).
- node --test provider-retrieval-rows and provider-retrieval-only, both
  engines, 5 languages, 900 and 420 px: 46 pass. provider-retrieval-rows
  is red on the old app.js (no rows).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./...:
  ok.
- After the rebase on origin/main 3c9d79d28 and again on 70a881071: the tests above plus
  TestCoreScriptBudget, TestTogetherLiveList and
  TestModelsListMediaOnlyWhenAsked in gateway, gui and provider: ok
  (testenv's docs-index check passes after the rebuild).
- The full go test -tags nogui -timeout 45m ./... was stopped before it
  ended, not run to the end (owner: never hold a verified fix behind a
  full suite).
Not tried: a real provider's live list in the app; the whole GUI suite
(1636 files); OpenRouter, whose embedding models are on a separate
/api/v1/embeddings/models list that magpie doesn't fetch.
2026-11-20 16:12:11 +08:00

160 lines
5.8 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"slices"
"strings"
"testing"
)
// axelburks on Discord asked why /v1/models lists no image or video models.
// An agent or client that offers every id this list gives in its chat
// picker would offer them to chat with, so they are listed only when the
// caller asks: ?kind=image,video (or all), each marked by its "kind". No
// agent's list, the plain one, Claude Desktop's, Claude Science's,
// Cursor's, OpenCode's or the ids of ?format=text, gains an image or a
// video model. Cherry Studio's does (TestModelsListMediaForCherryStudio).
func TestModelsListMediaOnlyWhenAsked(t *testing.T) {
grokSignedIn(t)
newGrokMedia(t)
h := New().Handler()
const image, video = "grok/grok-imagine-image", "grok/grok-imagine-video"
get := func(path string, header map[string]string) (int, string) {
r := httptest.NewRequest("GET", path, nil)
for k, v := range header {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w.Code, w.Body.String()
}
type row struct{ ID, Kind string }
list := func(path string, header map[string]string) []row {
t.Helper()
code, body := get(path, header)
if code != 200 {
t.Fatalf("%s %v: %d %s", path, header, code, body)
}
var l struct{ Data []row }
if err := json.Unmarshal([]byte(body), &l); err != nil {
t.Fatalf("%s %v: %s", path, header, body)
}
return l.Data
}
agents := map[string]map[string]string{
"plain": nil,
"opencode": {"User-Agent": "opencode/1.2.3"},
"claude-desktop": {"User-Agent": "Mozilla/5.0 (Macintosh) Claude/1.2.3 Chrome/130"},
"science": {"X-Anthropic-Surface": "operon", "User-Agent": "Anthropic/JS 0.60.0"},
"cursor": {"User-Agent": "Cursor-CLI/1.0"},
"magpie-key": {"Authorization": "Bearer magpie-acme"},
// Cherry Studio's list has them, its text list (a paste) not
"cherry-text": {"User-Agent": cherryUA},
}
for name, header := range agents {
rows := list("/v1/models", header)
if name == "cherry-text" {
rows = nil
}
if len(rows) == 0 && name == "plain" {
t.Fatalf("plain list is empty")
}
for _, m := range rows {
if m.Kind == "image" || m.Kind == "video" || strings.Contains(m.ID, "imagine") {
t.Errorf("%s's chat list offers %s (%s)", name, m.ID, m.Kind)
}
}
if _, body := get("/v1/models?format=text", header); strings.Contains(body, "imagine") {
t.Errorf("%s's text list offers a media model:\n%s", name, body)
}
}
kinds := func(rows []row) (chat, img, vid []string) {
for _, m := range rows {
switch m.Kind {
case "image":
img = append(img, m.ID)
case "video":
vid = append(vid, m.ID)
default:
chat = append(chat, m.ID)
}
}
return
}
if chat, img, vid := kinds(list("/v1/models?kind=image,video", nil)); len(chat) != 0 || !slices.Contains(img, image) || !slices.Contains(vid, video) {
t.Fatalf("kind=image,video: chat %v image %v video %v", chat, img, vid)
}
if chat, img, vid := kinds(list("/v1/models?kind=video", nil)); len(chat) != 0 || len(img) != 0 || !slices.Contains(vid, video) {
t.Fatalf("kind=video: chat %v image %v video %v", chat, img, vid)
}
if chat, img, vid := kinds(list("/v1/models?kind=chat&kind=Image", nil)); len(chat) == 0 || !slices.Contains(img, image) || len(vid) != 0 {
t.Fatalf("kind=chat&kind=Image: chat %v image %v video %v", chat, img, vid)
}
if chat, img, vid := kinds(list("/v1/models?kind=all", nil)); len(chat) == 0 || len(img) == 0 || len(vid) == 0 {
t.Fatalf("kind=all: chat %v image %v video %v", chat, img, vid)
}
if _, body := get("/v1/models?kind=image&format=text", nil); !strings.Contains(body, image) || strings.Contains(body, video) {
t.Fatalf("kind=image&format=text:\n%s", body)
}
for _, bad := range []string{"images", "embeddings", "moderation", "", "chat,"} {
if code, body := get("/v1/models?kind="+bad, nil); code != 400 {
t.Errorf("kind=%q: %d %s", bad, code, body)
}
}
}
// Cherry Studio 2.x's User-Agent, Electron's default with the app's name in
// it, as Cherry Studio 2.1.2 sends it.
const cherryUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/2.1.2 Chrome/146.0.7680.65 Electron/44.2.0 Safari/537.36"
// pingqipai3 on X: with magpie as a provider in Cherry Studio, its image
// models weren't there to pick. Cherry can't ask for ?kind=, and reads an
// image model's kind itself (its model registry, by the id's last
// segment), so its plain list, by its User-Agent or by the key magpie's
// Cherry link gives it, has the image and video models, each with the
// endpoints a new-api provider of Cherry's reads; its chat models stay.
func TestModelsListMediaForCherryStudio(t *testing.T) {
grokSignedIn(t)
newGrokMedia(t)
h := New().Handler()
type row struct {
ID, Kind string
Endpoints []string `json:"supported_endpoint_types"`
}
for name, header := range map[string]map[string]string{
"user-agent": {"User-Agent": cherryUA},
"cherry 1.x": {"User-Agent": "CherryStudio/1.5"},
"link's key": {"Authorization": "Bearer magpie-cherrystudio"},
} {
r := httptest.NewRequest("GET", "/v1/models", nil)
for k, v := range header {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
var l struct{ Data []row }
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &l) != nil {
t.Fatalf("%s: %d %s", name, w.Code, w.Body)
}
got := map[string]row{}
chat := 0
for _, m := range l.Data {
got[m.ID] = m
if m.Kind == "" {
chat++
}
}
if chat == 0 {
t.Errorf("%s: no chat models", name)
}
if m := got["grok/grok-imagine-image"]; m.Kind != "image" || len(m.Endpoints) == 0 || m.Endpoints[0] != "image-generation" {
t.Errorf("%s: image model %+v", name, m)
}
if m := got["grok/grok-imagine-video"]; m.Kind != "video" || !slices.Equal(m.Endpoints, []string{"openai-video"}) {
t.Errorf("%s: video model %+v", name, m)
}
}
}