The groups plugin now owns the user's own routing groups at the router strategy point. The groups magpie finds (auto-<model>, one model several providers serve) stay core, as Smart does: a new install has groups off under Essentials, and taking those would silently drop cross-provider failover. planGroup and the walk over a group's members stay core too. - provider.GateGroups / ServeGroups (group_serve.go): the gateway holds the gate while it serves; the plugin holds ServeGroups while on. Gated with none served, groupsIn drops every group not Auto, so FindGroup, GroupFor, the catalog and /v1/models go without them. The CLI and TUI are never gated. Nothing derived from groups is cached past a call, so a flip is seen by the next lookup. - Gated and off, a lookup that finds a user group asks the gate's refresh (pluginrt.Refresh in the gateway) at most once a second, never re-entrantly, then looks again. Without this, `magpie group add` was 404 for up to 2s until KeepFresh turned the plugin on (seen in the sandbox). - plugins/groups and plugins/routing: InUse is provider.NamedGroups, the file's non-Auto, non-hidden groups read past the gate; an unreadable providers.json is an error, so the plugin stays on. Run before rebasing onto707fbb249: - go test -count=1 -timeout 40m ./... : all ok (59 packages) - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean - sandbox, real nogui binary + `magpie serve` + two fake upstreams: before add: group/auto-m listed, group/mine 404; right after `magpie group add Mine models=pa/x,pb/m`: group/mine listed and 200; after `group rm`: 404, auto-m still served. After the rebase onto707fbb249: vets (3 OSes) and go test of internal/provider, internal/gateway, internal/pluginrt and plugins/... all ok. Broken by hand, each went red: - groupsIn filter removed: TestOwnGroupsWaitOnThePlugin, TestGroupsOnAndOff/off - NamedGroups swallowing the read error: TestNamedGroupsUnreadable, plugins/groups TestInUse - Apply not serving: TestOnServesOffDoesNot - refresh not called / not throttled / asked for Auto groups too: TestOwnGroupRefreshesTheGate
47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The router strategy point's groups with plugins/groups on and off: on, a
|
|
// group of the user's own is listed and answers; off, in the process
|
|
// serving the gateway, it is neither, while the group magpie found for a
|
|
// model two providers serve is listed and answers either way.
|
|
func TestGroupsOnAndOff(t *testing.T) {
|
|
fresh(t)
|
|
v := &keyed{}
|
|
serveOn(t, "pa", "ka", []string{"m", "x"}, v)
|
|
serveOn(t, "pb", "kb", []string{"m"}, v)
|
|
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"pa/x", "pb/m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
check := func(when string, mine bool) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
|
|
list := rec.Body.String()
|
|
if strings.Contains(list, `"group/mine"`) != mine || !strings.Contains(list, `"group/auto-m"`) {
|
|
t.Fatalf("%s: models %s", when, list)
|
|
}
|
|
code, body := postAs(t, s, "", `{"model":"group/mine","messages":[{"role":"user","content":"hi"}]}`)
|
|
if (code == 200) != mine {
|
|
t.Fatalf("%s: group/mine %d %s", when, code, body)
|
|
}
|
|
if code, body := postAs(t, s, "", `{"model":"group/auto-m","messages":[{"role":"user","content":"hi"}]}`); code != 200 {
|
|
t.Fatalf("%s: group/auto-m %d %s", when, code, body)
|
|
}
|
|
}
|
|
check("on", true)
|
|
t.Run("off", func(t *testing.T) {
|
|
withoutGroups(t)
|
|
check("off", false)
|
|
})
|
|
check("on again", true)
|
|
}
|