Files
magpie/internal/gateway/models_kind_test.go
T
yetone 15e1d65bcd gateway: Cherry Studio's model list has magpie's image and video models (pingqipai3 on X)
pingqipai3 on X: with magpie as a provider in Cherry Studio, its image
generation models weren't read. /v1/models lists chat models alone unless
the caller asks ?kind=image,video, and Cherry can't ask, so its list had
no image model at all, and its Paintings page none of magpie's.

What Cherry reads (CherryHQ/cherry-studio v2.2.1, c74990c): a custom
OpenAI-type provider (the type magpie's Cherry link gives it) is fetched
by listOpenAICompatibleModels, which keeps only id, name and owned_by.
The renderer then resolves each id against Cherry's model registry
(provider-registry models.json, normalizeModelId: the id's last "/"
segment, lower-cased), which carries image-generation for gpt-image-2,
dall-e-3, Gemini's image models, Imagen, Seedream, grok-imagine-image and
~130 more, and video-generation for grok-imagine-video and Seedance. Those
go to Paintings (supportsImageGenerationEndpoint), not the chat picker
(isNonChatModel), so the chat-picker worry behind ?kind= doesn't hold for
Cherry. Its new-api fetcher (new-api, CherryIN, AIOnly presets) reads
supported_endpoint_types instead.

Fix: a request of Cherry Studio's, by the key magpie's link gives it or
by its User-Agent (Electron's default with "CherryStudio/<ver>" in it,
read from Cherry Studio 2.1.2's Electron Framework), gets the image and
video models in its plain list, as ?kind=all would; ?format=text stays
chat. Each image model says supported_endpoint_types ["image-generation"]
(plus "image-edit" when it takes images), each video model
["openai-video"], new-api's words, for every caller. Docs: Cherry
Studio's page (en, zh, ja) and the integration page (md, en, zh).

Run (mote, at 889758605):
- go test -run TestModelsListMedia ./internal/gateway: ok
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows on internal/gateway;
  gofmt -l clean
Broken by hand: listKinds' media set false for Cherry:
TestModelsListMediaForCherryStudio red for the UA, Cherry 1.x's UA and
the link's key (image and video rows missing).
Not tried: a real Cherry Studio fetching the list (it would write the
user's live Cherry database); the registry lookup was read from Cherry's
source, not run. Full suite runs after the tag.
2026-11-13 12:53:04 +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", "embedding", "", "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)
}
}
}