Files
magpie/internal/provider/modelkind.go
T
yetone a65f2131dd gateway: a provider's model can be marked a video or image model in its editor (#1399, shen-joe)
shen-joe's relay serves a video model (agnes-video-2.5-flash), but its
/models list doesn't say "kind": "video". magpie only took a base-URL
provider's video models from that field, and its image models from that
field or from the model's name. So the model could never be asked at
/videos. They asked to tag video models, either in Settings or when
adding the provider.

What changed:
- settings.ModelKinds holds "image" or "video" by "<provider>/<model>".
  RenamePerModel carries it along with the other Model* maps.
- provider/modelkind.go:
  - MarksKinds: only a provider with its own base URL can be marked. A
    subscription, another magpie and a decision API can't.
  - ModelKind, KindModels and SetModelKind. A model can be marked if the
    provider serves it, or if its fetched list has it (a video model is
    left out of the chat list).
  - SetModelPrefs takes a "kind" with the model's other prefs.
- gateway:
  - Drawers adds the models marked image and drops the ones marked video.
  - customVideomakers adds the models marked video and drops the ones
    marked image.
  - The 400 for a model that can't make videos now says where to mark it.
- GUI: the model chip's right-click menu gets "Makes: Auto/Image
  model/Video model…" (an app menu, not a native select). The pick is
  staged until Save, and a marked model shows a small image/video tag.
  Strings are in zh, zh-TW, ja and de.
- docs/subsystems/gateway-routing.md describes it.
- settings_keep_test sets ModelKinds, so a Settings-page save is checked
  to keep it (that guard test went red until it did).

Why per model, in the provider's editor: the kind belongs to one model
of one provider, like the per-model API pref already set there.
Settings → Image/Video only pick the default model. This also covers
their "tag it on the provider" alternative.

Not done: Agnes's own video API (polling at /agnesapi?video_id=, the
finished video in "url") isn't OpenAI's GET /videos/{id}. Marking the
model gets the request to its /videos, but the poll won't match until
there's a mode for it. I've asked the reporter for real responses.

Run:
- go test ./internal/provider ./internal/settings ./internal/gui
  ./internal/gateway: ok (gateway 1658s).
- TestCustomProviderVideoTheUserMarks, which goes red with the
  KindModels loop in video.go removed.
- GUI model-kind.test.cjs, 16/16 (en/zh/ja/de, 900/420, Chromium and
  WebKit). It goes red with the menu item removed.
- Sibling GUI tests: model-api-gemini, model-copy-id, model-api-preset,
  model-test-one/as/why, provider-detect-models, provider-typed,
  i18n-keys, gui-ja and gui-de.
- gofmt, plus linux and windows go vet -tags nogui.
- Not tried against Agnes's real API (no key).
2026-10-13 16:33:44 +08:00

128 lines
3.9 KiB
Go

package provider
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// What a model makes, as the user says it (shen-joe, #1399): a provider set
// up with a base URL tells magpie its image and video models by its own
// model list ("kind": "image" or "video") or, for images, by a model's name.
// A relay whose list says neither — most don't — leaves a video model
// making nothing, and an image model with an odd name drawing nothing. The
// user can say so of one model instead, kept in settings' ModelKinds by
// "<provider id>/<model id>" as a model's name is: an image model is drawn
// with at the images API under the provider's base, a video model asked at
// its videos API, as one its list marks is (gateway's Drawers and
// customVideomakers).
const (
KindImage = "image"
KindVideo = "video"
)
// MarksKinds is whether p's models can be said to make images or videos: a
// provider set up with a base URL, which magpie asks at the OpenAI-shaped
// images and videos APIs under it. A subscription's account, another magpie
// (whose own list says) and a decision API's can't.
func (p Provider) MarksKinds() bool {
return p.Account == nil && !p.IsRemoteMagpie() && !p.DecideOnly() &&
(p.Base(Chat) != "" || p.Base(Responses) != "")
}
// ModelKind is what the user said p's model makes: KindImage, KindVideo,
// or "" when they said nothing, or p can't be told (MarksKinds).
func (p Provider) ModelKind(model string) string {
if !p.MarksKinds() {
return ""
}
return modelKindIn(heldSettings(), p.ID, model)
}
func modelKindIn(s settings.Settings, pid, model string) string {
switch k := s.ModelKinds[pid+"/"+model]; k {
case KindImage, KindVideo:
return k
}
return ""
}
// KindModels are the ids of p's models the user said make kind, those it
// still serves, sorted (a provider's id has no "/", so a model's may:
// OpenRouter's).
func (p Provider) KindModels(kind string) []string {
if !p.MarksKinds() {
return nil
}
var out []string
prefix := p.ID + "/"
for key, k := range heldSettings().ModelKinds {
if model, ok := strings.CutPrefix(key, prefix); ok && k == kind && model != "" {
out = append(out, model)
}
}
if len(out) == 0 {
return nil
}
slices.Sort(out)
return slices.DeleteFunc(out, func(m string) bool { return !p.hasKindable(m) })
}
// hasKindable is whether p has model: one it serves to chat with, or one
// its fetched list has that it leaves out of those, as making images or
// videos.
func (p Provider) hasKindable(model string) bool {
if p.serves(model) {
return true
}
all, _ := catalog.LiveAll(p.ID)
return slices.ContainsFunc(all, func(m catalog.Model) bool { return m.ID == model })
}
// SetModelKind says what a provider's model, spelt "provider/model", makes:
// KindImage, KindVideo, or "" to leave it to its vendor's list and name.
func SetModelKind(ref, kind string) error {
_, err := setModelKind(ref, kind)
return err
}
func setModelKind(ref, kind string) (bool, error) {
p, model, err := splitRef(ref)
if err != nil {
return false, err
}
kind = strings.TrimSpace(kind)
if kind != "" && kind != KindImage && kind != KindVideo {
return false, fmt.Errorf("a model makes %q or %q, not %q", KindImage, KindVideo, kind)
}
if kind != "" {
if !p.MarksKinds() {
return false, errors.New(p.ID + " makes images and videos only as its own API does: none of its models can be said to make them")
}
if !p.hasKindable(model) {
return false, fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
}
}
s := settings.Load()
key := p.ID + "/" + model
if s.ModelKinds[key] == kind {
return false, nil
}
if kind == "" {
delete(s.ModelKinds, key)
} else {
if s.ModelKinds == nil {
s.ModelKinds = map[string]string{}
}
s.ModelKinds[key] = kind
}
if err := settings.Save(s); err != nil {
return false, err
}
return true, nil
}