axelburks (Discord #feedback): an in-house vendor queues requests to
its busy models, and the plugin could only put the load in the model's
name, which changed only when magpie re-read the list.
- A provider plugin calls input.magpie.load(provider, {"<model id>":
0-100 | null}) whenever it likes, from its own timer. host.js
(reportLoad) clamps each number to 0-100, null clears a model, every
other value is dropped, at most 1000 a call, and it returns the count
taken. A provider that isn't a string or loads that isn't an object
throws a TypeError. It goes to Go as an unasked {event:"load"}.
- internal/plugin/load.go keeps each by provider and lowercased model
id. ModelLoad answers unknown once a load is older than LoadFresh
(5 minutes), never idle. model@size falls back to its model.
- The provider editor shows it by the model as a grey ctx badge
("87%", titled "87% busy now, as its plugin says").
- Settings > Network and sharing > Busy models last (settings.BusyLoad,
POST /api/settings/busy-load, Off by default, over 50/70/80/90% or
1-99 by hand): restLast puts a model reported over it behind the
ready candidates, ahead of the resting. The trace row has busy:<load>.
At a new turn affine moves a conversation off it (Why "busy",
busyBehind); within a turn it stays. after, readyForNew and leastHeld
skip it. Unknown load, a lone candidate, or all busy: nothing moves.
- Docs: site plugins page (en, zh, ja) "Model load" section and input
table; docs/subsystems provider-plugins.md and gateway-routing.md.
Run, all on mote via mote-run at a42736807's tree rebased on
origin/main b683f1c0e:
- go test ./internal/plugin ./internal/gateway -run
'ModelLoad|TestBusy' -v: TestPluginReportsModelLoad,
TestModelLoadGoesStale, TestBusyModelGoesBehind,
TestBusyModelNotKeptAtNewTurn pass.
- Broken by hand, each red: host.js not sending the event
(TestPluginReportsModelLoad "the plugin's loads never arrived"), the
busy case off in restLast (TestBusyModelGoesBehind order), the affine
"busy" case off (TestBusyModelNotKeptAtNewTurn "kept true, why
session").
- go test ./internal/gui -run Budget: ok. No budget raised. app.js
+86 bytes, two comments trimmed to fit. The settings row lives in
plugins/gateway/settings.js.
- node --test model-load, i18n-keys, bare-ids, model-rate: 69/69 pass.
model-load is Chromium and WebKit, 420 and 900 wide, en zh zh-TW ja
de.
- gofmt -l: empty. go vet ./... (darwin), GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./..., go build: ok.
Not run before the push (owner: push and tag first): go test ./...
-timeout 45m and the whole GUI suite. mote and the WSL box were each
running another session's full suite. Both run next, and any red gets
its own commit.
Not tried: a real plugin and vendor reporting load. The plugin side is
tested through the real Bun host with a plugin written in the test.
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package plugin
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// LoadFresh is how long a model's load counts from the plugin's call that
|
|
// reported it (magpie.load in host.js). Past it the load is unknown: a
|
|
// plugin that stopped reporting says nothing of how busy the vendor is
|
|
// now, and routing never takes its silence for idle.
|
|
const LoadFresh = 5 * time.Minute
|
|
|
|
// loadModels caps the models one provider keeps a load for, as host.js
|
|
// caps one call's; a plugin reporting new ids every call can't grow it.
|
|
const loadModels = 1000
|
|
|
|
type modelLoad struct {
|
|
pct float64
|
|
at time.Time
|
|
}
|
|
|
|
// loads are the models' loads the plugins reported, by plugin provider
|
|
// id and then by the model id lowercased.
|
|
var loads = struct {
|
|
sync.Mutex
|
|
m map[string]map[string]modelLoad
|
|
}{m: map[string]map[string]modelLoad{}}
|
|
|
|
// noteLoads keeps a "load" event's models: each a percent from 0 to 100,
|
|
// -1 clearing it. Models the event doesn't name keep theirs, each till
|
|
// its own LoadFresh runs out.
|
|
func noteLoads(provider string, got map[string]float64, now time.Time) {
|
|
if provider == "" || len(got) == 0 {
|
|
return
|
|
}
|
|
loads.Lock()
|
|
defer loads.Unlock()
|
|
m := loads.m[provider]
|
|
if m == nil {
|
|
m = map[string]modelLoad{}
|
|
loads.m[provider] = m
|
|
}
|
|
for id, v := range got {
|
|
k := strings.ToLower(id)
|
|
if v < 0 || v != v {
|
|
delete(m, k)
|
|
continue
|
|
}
|
|
m[k] = modelLoad{pct: min(v, 100), at: now}
|
|
}
|
|
if len(m) > loadModels {
|
|
for k, l := range m {
|
|
if now.Sub(l.at) > LoadFresh || len(m) > loadModels {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
}
|
|
if len(m) == 0 {
|
|
delete(loads.m, provider)
|
|
}
|
|
}
|
|
|
|
// ModelLoad is how busy the plugin provider's model is, a percent from
|
|
// 0 to 100 as its plugin last reported it, and when (a model@size has its
|
|
// model's load unless reported itself); ok is false when the
|
|
// plugin never reported it, cleared it, or reported it more than
|
|
// LoadFresh before now.
|
|
func ModelLoad(provider, model string, now time.Time) (pct float64, at time.Time, ok bool) {
|
|
if provider == "" || model == "" {
|
|
return 0, time.Time{}, false
|
|
}
|
|
k := strings.ToLower(model)
|
|
loads.Lock()
|
|
l, ok := loads.m[provider][k]
|
|
if i := strings.LastIndexByte(k, '@'); !ok && i > 0 {
|
|
// grok-4.7@256k is the model grok-4.7 at a context size: as busy
|
|
l, ok = loads.m[provider][k[:i]]
|
|
}
|
|
loads.Unlock()
|
|
if !ok || now.Sub(l.at) > LoadFresh {
|
|
return 0, time.Time{}, false
|
|
}
|
|
return l.pct, l.at, true
|
|
}
|
|
|
|
// NoteLoadsForTest is a "load" event's loads reported at at, for a test.
|
|
func NoteLoadsForTest(provider string, got map[string]float64, at time.Time) {
|
|
noteLoads(provider, got, at)
|
|
}
|
|
|
|
// ForgetLoads drops every load reported, for a test.
|
|
func ForgetLoads() {
|
|
loads.Lock()
|
|
loads.m = map[string]map[string]modelLoad{}
|
|
loads.Unlock()
|
|
}
|