Files
magpie/internal/gateway/models_kind_test.go
T
yetone af9ac3d25d gateway: /v1/models lists image and video models when ?kind= asks for them, chat models alone by default
axelburks on Discord asked that /v1/models return the image and video
models too, so a client can find them. It listed chat models alone, with
no way to ask for the others.

Decided: they are listed only when asked for, with ?kind=image,video
(or ?kind=chat,image,video, or ?kind=all), each row marked "kind":
"image" or "kind": "video". Without ?kind= the list is the same chat
list as before. Why not add them by default:
- Agents and clients offer every id this list gives as a model to chat
  with: OpenCode's and Cherry Studio's model lists, ZCode's and others'
  pasted ?format=text. An image or video id there would sit in their chat
  pickers and fail on the first message (an image model on
  /v1/chat/completions).
- OpenAI's own list mixes gpt-image and sora in with nothing to tell them
  apart, so a client has no field it would already read to skip them.
- magpie's own callers that want them already ask (another magpie's
  drawers header, kind=image/video/decision on the MCP list); ?kind=
  gives any other client the same.
An unknown kind is a 400 that names the kinds, not read as none. A
gateway key held to models or accounts sees only its own image and video
models, as on the chat list (drawerObjects/videomakerObjects already
check modelAllowed and accountServes). Unticked image and video models
(HiddenDrawers, HiddenVideomakers) are left out.

TestModelsListMediaOnlyWhenAsked: the default list and ?kind=chat have no
image or video row (so no agent's chat picker that reads it can offer
one), ?format=text likewise; ?kind=image / video / image,video / all give
them, marked; a bad kind is 400.

Docs: docs/integrating.md, providers-accounts.md, site docs (en, zh).

Run on mote, this change rebased on f4c664a09:
- go test -count=1 -timeout 45m ./internal/catalog ./internal/provider
  ./internal/gateway ./internal/gui: catalog, provider and gateway ok;
  gui red only in TestMainWindowCorners/macOS_26_SDK (#3949, red on main).
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./...: clean. gofmt -l: nothing.
- GUI: the 51 provider-*, drawers, image-models, i18n and
  video-models-hide files, both engines, every language, wide and narrow:
  1161 of 1188 ok. The 27 reds are 5 files that are red the same 27 at
  origin/main d1cdce130 run alone: provider-fetch-again (#3292/#5057),
  provider-models-rest (#4396), provider-picks-stay (#4397),
  provider-levels-scroll (#4395), provider-typed (#4673).
  video-models-hide: 22 of 22 ok.
- The whole GUI suite (all files) ran earlier at this change on 8af751987,
  347 of 19153 red at load 100 to 200; the 66 files with a red, run again
  alone, are red in the same tests at origin/main 17f2387e1 (all filed:
  #4388, #4391, #4398, #4320, #4541/#5013, #2564, #4244, #4304 and the
  above).
Rebased again onto d1cdce130 (sessions, Cursor mover, library and routing
plugins, group-unlists; nothing this change touches but gateway.go,
which merged clean) and run there: TestCoreScriptBudget and internal/gui's
new tests, the gateway's ?kind= and video tests, internal/provider and
internal/catalog, video-models-hide 22 of 22: ok.

Broken by hand, each red, then put back:
- listKinds' default returning all three kinds:
  TestModelsListMediaOnlyWhenAsked red
- an unknown kind let through: TestModelsListMediaOnlyWhenAsked red

Not tried: no real client was pointed at ?kind=; the agents' pickers were
checked by what they read (the default list, unchanged), not by opening
each one.

Credit: axelburks on Discord.
2026-11-03 18:34:19 +08:00

103 lines
3.7 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.
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"},
"cherry": {"User-Agent": "CherryStudio/1.5"},
"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"},
}
for name, header := range agents {
rows := list("/v1/models", header)
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", "embedding", "", "chat,"} {
if code, body := get("/v1/models?kind="+bad, nil); code != 400 {
t.Errorf("kind=%q: %d %s", bad, code, body)
}
}
}