diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 7683dd54e..8a5bc4db3 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -88,7 +88,7 @@ A plugin can tell magpie of a request it sent its vendor on its own, outside any A plugin can say how busy each model is (axelburks on Discord: an in-house vendor queues requests to its busy models, and a load put in the model's name changed only when the list did). It calls `input.magpie.load(provider, {"": 0 and up | null})` whenever it likes, from its own timer, as usage data refreshes; magpie never asks. The host ([`host.js`](../../internal/plugin/host.js) `reportLoad`) throws a TypeError for a provider that isn't a string of 1–200 characters or a `loads` that isn't a plain object, clamps each finite number to 0–10000 (`LOAD_MOST`, `plugin.LoadMost`: past 100 is a vendor that queues, 200 twice what it serves at once, axelburks again; 10000 is a sanity cap), sends `null` as -1 (cleared), drops every other value and an id over 200 characters, takes at most 1000, and sends what is left as an unasked `{event:"load", provider, loads}` (`dispatch` in [`host.go`](../../internal/plugin/host.go)). It returns how many it took. -[`load.go`](../../internal/plugin/load.go) keeps each by the plugin's provider id and the model id lowercased, with when it came (`noteLoads`, at most 1000 a provider). `ModelLoad(provider, model, now)` answers it, a model@size falling back to its model, and unknown once it is older than `LoadFresh` (5 minutes): a plugin that stops reporting leaves its models unknown, never idle. Nothing is saved; a restart starts with none known. +[`load.go`](../../internal/plugin/load.go) keeps each by the plugin's provider id and the model id lowercased, with when it came (`noteLoads`, at most 1000 a provider). `ModelLoad(provider, model, now)` answers it by the plugin's own id of the model (its callers, the provider editor and `busyOf`, map a model the user lists by another id back through `OwnModelID` first), a model@size falling back to its model, and unknown once it is older than `LoadFresh` (5 minutes): a plugin that stops reporting leaves its models unknown, never idle. Nothing is saved; a restart starts with none known. The provider editor shows it by the model as a grey badge (`/api/providers`' `models[].load`, `loadTag` in app.js). Routing reads it only when the user sets *Busy models last* (`settings.BusyLoad`, `POST /api/settings/busy-load {at}`, off by default): see `restLast` and `busyBehind` in [gateway routing](gateway-routing.md). diff --git a/internal/gateway/busy_load_test.go b/internal/gateway/busy_load_test.go index 5c3cdcf66..50bf18f61 100644 --- a/internal/gateway/busy_load_test.go +++ b/internal/gateway/busy_load_test.go @@ -146,3 +146,33 @@ func TestBusyModelNotKeptAtNewTurn(t *testing.T) { t.Fatal("a new conversation may go to the busy one") } } + +// A plugin reports a model's load by its own id. One the user lists by +// another (Listed as) is judged busy by that load all the same (axelburks +// on Discord: a renamed model was never busy, the others were). +func TestBusyModelListedAsAnotherID(t *testing.T) { + plugin.ForgetLoads() + t.Cleanup(plugin.ForgetLoads) + s := settings.Load() + was := s.ModelIDs + s.ModelIDs = map[string]string{"inhouse-plugin/fast": "big", "inhouse-plugin/grok": "grok-4.7@256k"} + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { s := settings.Load(); s.ModelIDs = was; settings.Save(s) }) + now := time.Now() + plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 92, "grok-4.7": 95}, now) + p := provider.PluginForTest("inhouse-plugin", "inhouse") + if own := p.OwnModelID("fast"); own != "big" { + t.Fatalf("OwnModelID(fast) = %q", own) + } + for _, model := range []string{"fast", "grok", "big"} { + load, busy := busyOf(candidate{p: p, model: model, rest: "inhouse-plugin/" + model}, 80, now) + if !busy || load < 92 { + t.Errorf("%s: load %v busy %v, want busy over 80", model, load, busy) + } + } + if load, busy := busyOf(candidate{p: p, model: "other", rest: "inhouse-plugin/other"}, 80, now); busy || load != 0 { + t.Errorf("other: load %v busy %v, want none", load, busy) + } +} diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index 7ca91a107..9ec8f8c27 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -1144,12 +1144,14 @@ func restLast(out []candidate, pl planned) ([]candidate, planned) { // busyOf is the load c's plugin last reported for its model, and whether // that is over the percent over (settings.BusyLoad; 0 is off). A model // with no load reported, or none within plugin.LoadFresh, isn't busy: -// unknown is never taken for idle nor for busy. +// unknown is never taken for idle nor for busy. The plugin reports it by +// its own id of the model, so a model the user lists by another is looked +// up by that (axelburks on Discord). func busyOf(c candidate, over int, now time.Time) (float64, bool) { if over <= 0 { return 0, false } - load, _, ok := plugin.ModelLoad(c.p.PluginProvider(), c.model, now) + load, _, ok := plugin.ModelLoad(c.p.PluginProvider(), c.p.OwnModelID(c.model), now) return load, ok && load > float64(over) } diff --git a/internal/gui/model_load_listed_test.go b/internal/gui/model_load_listed_test.go new file mode 100644 index 000000000..34d0267c2 --- /dev/null +++ b/internal/gui/model_load_listed_test.go @@ -0,0 +1,55 @@ +package gui + +import ( + "os" + "testing" + "time" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/plugin" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// The Providers page shows a plugin model's load for a model the user +// lists by another id (Listed as) too: the plugin reports it by its own id +// (axelburks on Discord: a renamed model showed no load). +func TestModelLoadListedAsAnotherID(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", os.Getenv("HOME")) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + plugin.ForgetLoads() + t.Cleanup(plugin.ForgetLoads) + s := settings.Load() + s.ModelIDs = map[string]string{"inhouse-plugin/fast": "big", "inhouse-plugin/grok": "grok-4.7@256k"} + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + plugin.NoteLoadsForTest("inhouse", map[string]float64{"big": 87.4, "grok-4.7": 60, "small": 12}, time.Now()) + p := provider.PluginForTest("inhouse-plugin", "inhouse") + ms := []catalog.Model{{ID: "fast"}, {ID: "grok"}, {ID: "small"}, {ID: "quiet"}} + info := providerInfoWith(p, provider.Listing{Available: ms, Exposed: ms}, nil, nil) + want := map[string]float64{"fast": 87.4, "grok": 60, "small": 12} + seen := 0 + for _, m := range info.Models { + w, has := want[m.ID] + switch { + case !has && m.Load != nil: + t.Errorf("%s: load %v, want none", m.ID, *m.Load) + case has && m.Load == nil: + t.Errorf("%s: no load, want %v", m.ID, w) + case has && *m.Load != w: + t.Errorf("%s: load %v, want %v", m.ID, *m.Load, w) + } + if has { + seen++ + } + if m.ID == "fast" && m.Vendor != "big" { + t.Errorf("fast: Vendor %q, want big", m.Vendor) + } + } + if seen != len(want) { + t.Fatalf("models %+v", info.Models) + } +} diff --git a/internal/gui/providers.go b/internal/gui/providers.go index 15952429d..3ffe82b14 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -849,7 +849,10 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs } window := p.WindowOf(m) j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on, Context: window, Output: provider.OutputWithin(window, p.ReplyLimitIn(m, set)), Listed: provider.ListedWindow(m), Max: m.MaxContext, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Images: images, ImageSet: imageSet, Own: own, Best: newest[m.ID]} - if l, _, ok := plugin.ModelLoad(p.PluginProvider(), m.ID, time.Now()); ok { + // a plugin reports a model's load by its own id, not one the user + // lists it by (axelburks on Discord) + ownID := p.OwnModelID(m.ID) + if l, _, ok := plugin.ModelLoad(p.PluginProvider(), ownID, time.Now()); ok { j.Load = &l } if i := slices.IndexFunc(most, func(c catalog.Model) bool { return c.ID == m.ID }); j.Max == 0 && i >= 0 { @@ -871,8 +874,8 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs } j.NoAPI = noAPI(m.ID) j.Same = sames[ref] - if own := p.OwnModelID(m.ID); own != m.ID { - j.Vendor = own + if ownID != m.ID { + j.Vendor = ownID } j.IDs = p.TakesModelIDs() j.Kind = p.ModelKind(m.ID)