Files
magpie/internal/gateway/image_mcp_models.go
T
yetone 92d2ed74f9 providers: an Ark key's Seedance models are under Video models in its editor, each to tick
axelburks on Discord: "供应商里方舟的配置页,有图像模型选项能勾选模型,但是没
有视频模型选项不能勾选视频模型". A Volcengine Ark key's editor listed its
Seedream models under Image models, to tick, and had its Seedance models
among the chat models or nowhere.

Cause, two parts:
- The editor had no video section at all: video models had no list and no
  hidden set (gateway-routing.md said "Video models aren't listed in the
  editor, so there is nothing to hide there").
- catalog's live parse read only "kind" and modalities.input/output. Ark's
  /api/v3/models marks no kind: it says domain "VideoGeneration" /
  "ImageGeneration" and modalities.input_modalities/output_modalities. So
  Seedance came back as a chat model, and 3D models (output 3d, no text)
  as chat models too.

Change:
- catalog: a live row with no kind is a video model when its domain is
  VideoGeneration or it outputs video without text, an image model for
  ImageGeneration or image without text; Ark's input_modalities /
  output_modalities are read when input/output aren't there, so a model
  that gives no text (Ark's 3D) is left out of the chat list.
  TestFetchArkMarksVideoModels runs on a captured Ark list
  (testdata/volcengine-ark-models-2026-11-03.json, fetched read-only
  with GET /api/v3/models, no key in it).
- provider: HiddenVideomakers beside HiddenDrawers, VideomakerHidden.
- gateway: VideomakersOf (Videomakers on a given list, for the editor),
  OfferedVideomakers = Videomakers less the unticked ones, read by
  AutoVideomaker, videomakerObjects (another magpie's list) and
  mediaModels(true) (generate_video). A hidden one still makes its video
  when named: bareVideomaker, videosCreate and arkFilms read Videomakers.
- gui: the editor's Video models section (renderVideomakers), filmIds and
  hiddenVideomakers on /api/providers, kept on save as hiddenDrawers is;
  i18n for every language. app.js comments cut to stay under
  TestCoreScriptBudget, which this change reached; no raise.
- docs: gateway-routing.md's Hidden image models section.

Old data: an Ark list fetched before keeps Seedance among its chat models
until its list is fetched again;
customVideomakers already counts any Seedance id an Ark key lists or picks
as a video model, so it makes videos and shows under Video models now.

Run on mote, this change and the next (/v1/models ?kind=, gateway and
docs only) rebased on f4c664a09; this commit's own tree, on 17f2387e1,
gave the same Go results with Linux and Windows vet of ./internal/...
clean:
- 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).
- GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. gofmt -l: nothing.
- GUI (at the next commit's tree, which adds no GUI file or script): 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:
- the domain/output check in catalog's live parse:
  TestFetchArkMarksVideoModels red
- OfferedVideomakers returning Videomakers whole:
  TestHiddenVideomakerLeavesListsButStillFilms red
- the editor's filmIds left empty: TestVideoModelsHidden red
- a save without hiddenVideomakers dropping the old ones:
  TestVideoModelsHidden red
- renderVideomakers' call in the editor left out: video-models-hide
  22 of 22 red
- a chip's click not changing the draft: video-models-hide 22 of 22 red

Not tried: no real Ark video was made (no money spent); the list parse was
checked on a real Ark list fetched read-only. The editor was not opened in
the real app, only in the GUI tests' fake API.

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

220 lines
7.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"cmp"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// generate_image and generate_video take the model to use (XYenon on
// Discord): one of those set up for it, as provider/model, else the one
// Settings › Images picks. The tool's schema lists those the caller may use
// (mediaModelsFor), and a model none of them is, is a tool error naming
// them, never a drawing with another. The model chosen is named to the
// images and videos APIs, so a gateway key's models hold it there as they
// hold one named at /v1/images (keyHolds, #882): that check is the API's,
// on every route to it.
// MediaModelMethod is the image MCP server's JSON-RPC method, not a tool,
// that the stdio Magpie Image server asks which model a call's model
// argument stands for: {"tool": …, "model": …} → {"model": provider/model},
// or an error naming those it may be.
const MediaModelMethod = "magpie/media_model"
// mediaEnumMost is the most models a tool's schema lists as its model's
// enum; more are described instead, and named in full by the error an
// unknown one gets.
const mediaEnumMost = 40
// mediaModels are the models generate_image (video false) or
// generate_video (true) may be asked for, as provider/model, the default
// first, and the default itself: "" when image generation is off or
// nothing makes them. Image models are each provider's OfferedDrawers (one
// the user unticked is left out, as Automatic leaves it), videos its
// OfferedVideomakers (one unticked left out the same way).
func mediaModels(video bool) (all []string, def string) {
if video {
def, _ = videomaker()
} else {
def = Drawer()
}
if def == "" {
return nil, ""
}
all = []string{def}
for _, p := range provider.All() {
if !p.On() || p.DecideOnly() {
continue
}
ms := OfferedVideomakers(p)
if !video {
ms = OfferedDrawers(p)
}
for _, m := range ms {
if ref := p.ID + "/" + m.ID; !slices.Contains(all, ref) {
all = append(all, ref)
}
}
}
return all, def
}
// ImageModel is a model magpie draws with, as an agent's own list of
// image models names it (Pi's, internal/agent/pi_extension.go).
type ImageModel struct {
ID string // provider/model, what /v1/images takes as model
Name string // the model's name · its provider's, as /v1/models labels it
Images bool // it takes images as input too
}
// ImageModels are the models generate_image may be asked for
// (mediaModels), the Settings › Images default first, each with its label;
// none when image generation is off.
func ImageModels() []ImageModel {
all, _ := mediaModels(false)
if len(all) == 0 {
return nil
}
known := map[string]ImageModel{}
for _, p := range provider.All() {
if !p.On() || p.DecideOnly() {
continue
}
// every drawer, so a default the user unticked is labelled too
for _, m := range Drawers(p) {
ref := p.ID + "/" + m.ID
known[ref] = ImageModel{ID: ref, Name: cmp.Or(m.Name, m.ID) + " · " + p.Name, Images: m.Images}
}
}
out := make([]ImageModel, 0, len(all))
for _, ref := range all {
m, ok := known[ref]
if !ok {
m = ImageModel{ID: ref, Name: ref}
}
out = append(out, m)
}
return out
}
// mediaModelsFor is mediaModels less those r's gateway key may not use.
func mediaModelsFor(r *http.Request, video bool) (all, shown []string, def string) {
all, def = mediaModels(video)
who, held := keyHolds(r)
if !held {
return all, all, def
}
for _, ref := range all {
if refAllowed(who, ref) {
shown = append(shown, ref)
}
}
return all, shown, def
}
// refAllowed says the key may use ref, a model or a routing group.
func refAllowed(who access.Identity, ref string) bool {
if provider.IsGroupRef(ref) {
g, ms, ok := provider.FindGroup(ref)
return ok && groupAllowed(who, g, ms)
}
return allows(who, ref)
}
// mediaModelProp is the model argument's schema: the models shown, as an
// enum while they are few.
func mediaModelProp(video bool, shown []string, def string) map[string]any {
what := "image"
if video {
what = "video"
}
d := "The " + what + " model to use, as provider/model."
switch {
case def == "":
d += " Leave it out: none is set up in Magpie."
case video:
d += " Leave it out to use the video model set up in Magpie (now " + def + ")."
default:
d += " Leave it out to use the one set in Magpie (Settings → Images → Image generation; now " + def + ")."
}
prop := map[string]any{"type": "string"}
switch n := len(shown); {
case n > mediaEnumMost:
d += fmt.Sprintf(" One of the %d %s models set up in Magpie, such as %s; an unknown one is refused with the full list.", n, what, strings.Join(shown[:3], ", "))
case n > 0:
prop["enum"] = slices.Clone(shown)
case def != "":
d += " No other can be picked here."
}
prop["description"] = d
return prop
}
// pickMediaModel is the model asked stands for: one of all, by its whole
// id or by a bare id only one of them has; a ChatGPT account's other GPT
// Image names as its one (ListedDrawer); or a newer model of a family a
// subscription serves before magpie lists it (drawsFamily,
// grokVideoFamily), as the APIs take it. Else an error that names those
// shown.
func pickMediaModel(tool, asked string, video bool, all, shown []string, def string) (string, error) {
asked = strings.TrimSpace(asked)
if !video && strings.Contains(asked, "/") {
if l := ListedDrawer(asked); slices.Contains(all, l) {
asked = l
}
}
if slices.Contains(all, asked) {
return asked, nil
}
if !strings.Contains(asked, "/") && asked != "" {
var same []string
for _, ref := range all {
if _, id, _ := strings.Cut(ref, "/"); id == asked && !provider.IsGroupRef(ref) {
same = append(same, ref)
}
}
switch len(same) {
case 1:
return same[0], nil
case 0:
default:
return "", fmt.Errorf("%s: more than one provider has %q; name it with its provider: %s", tool, asked, strings.Join(same, ", "))
}
}
if p, id, ok := provider.Resolve(asked); ok && strings.Contains(asked, "/") && p.On() && !p.DecideOnly() {
if video && drawsGrok(p) && strings.HasPrefix(id, grokVideoFamily) || !video && drawsFamily(p, id) {
return asked, nil
}
}
what, a := "image", "an image"
if video {
what, a = "video", "a video"
}
if def == "" {
return "", fmt.Errorf("%s: no %s model is set up in Magpie", tool, what)
}
if len(shown) == 0 {
return "", fmt.Errorf("%s: %q isn't %s model that can be used here; leave model out to use the default", tool, asked, a)
}
list := shown
more := ""
if len(list) > 100 {
list, more = list[:100], ", and "+strconv.Itoa(len(shown)-100)+" more"
}
return "", fmt.Errorf("%s: %q isn't %s model that can be used here. Pick one of: %s%s; or leave model out to use the default (%s)", tool, asked, a, strings.Join(list, ", "), more, def)
}
// mediaModelFor is the model r's call of tool asked for (pickMediaModel),
// among those r's key may use.
func mediaModelFor(r *http.Request, tool, asked string) (string, error) {
video := tool == GenerateVideoTool
all, shown, def := mediaModelsFor(r, video)
return pickMediaModel(tool, asked, video, all, shown, def)
}