Files
magpie/internal/gui/groups.go
T
yetone 10b940233c routing: a group's member can be sent in its vendor's fast mode, beside its own reasoning (vs on Discord: a member "gpt-6.1 sol · high · fast") — a member's effort was already its own (#189, "provider/model:effort") but there was no way to ask for speed, so a group could not hold "gpt-6.1-sol at high, priority" next to slower members. A group now lists in fast the members it sends fast, as Members spells them, kept beside Members rather than in their ids so an older version still routes to them, only not fast (checked: HEAD's build on the same providers.json sends the same members at their effort, no tier, and leaves the file as it was). When the gateway sends to such a member it asks in the upstream's own words, only where the model has a fast mode magpie can ask for (provider.CanFast): service_tier "priority" for a ChatGPT account's GPT models and an OpenAI key's GPT/o-series models on its Responses API, Cursor's -fast variant, and speed "fast" with the fast-mode-2026-02-01 beta for Claude Opus 4.8/5/5.5 on an Anthropic key (api.anthropic.com); a relay, Bedrock, a Claude subscription, a model without one, or a provider since moved off those hosts is sent untouched. It is applied in the agent's own API and carried through translation (parseAnthropic now reads speed), so Chat, Responses and Anthropic agents all get it. A translated request for OpenAI's Chat API is still told no tier (TestCursorPluginFast), hence the Responses requirement. The trace marks the try, the order and the group (fast), shown as "fast" beside the reasoning in Requests and the order. Typed, a member is provider/model[:effort]:fast: magpie group add/set models=… takes it, fast=<m1>,… sets them, models-= takes either spelling, and the group lists and shows members as typed with "· fast"; a model with no fast mode, or a group in the group, is refused. The GUI group editor shows a "Standard speed"/"Fast" toggle (标准速度/快速) beside a member's reasoning only for a model that has one, the card reads "· fast" after the reasoning, and fast follows a member's reasoning change and leaves with a removed member; the TUI's group view takes E to step a member's effort and F to toggle fast. Provider renames carry it. TestFastMemberOpenAI, TestFastMemberAnthropic, TestWithFastWire (gateway, per wire format, a fake vendor recording body and anthropic-beta), TestCanFast, TestSaveGroupFast, TestGroupMembersFast, TestGroupMemberEffortAndFast, TestGroupSaveFast and group-member-fast.test.cjs fail without the change. A sandbox magpie (own HOME, gateway on 3966) with providers at http://api.openai.com and http://api.anthropic.com through a local recording proxy sent priority+high to OpenAI and speed fast + the beta to Anthropic from Chat, Responses and Anthropic clients, and nothing extra for Sonnet or a non-fast member. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; group-member-fast, click-scroll, old-webkit, routing-levels, routing-manual, routing-effort, settings-groups pass (Chromium, WebKit, en/zh).
2026-10-01 17:13:34 +08:00

274 lines
8.7 KiB
Go

package gui
import (
"encoding/json"
"fmt"
"net/http"
"slices"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// groupsJSON is what the Routing view manages: the routing groups, the
// models they can be made of, and each provider with several keys or
// accounts on — which any of its models routes over already.
type groupsJSON struct {
Groups []groupJSON `json:"groups"`
Models []modelRef `json:"models"`
Pools []poolJSON `json:"pools"`
// Deciders: the decision providers' models (Jev), which may only be a
// group's classifier
Deciders []modelRef `json:"deciders"`
}
type groupJSON struct {
provider.Group
Ready bool `json:"ready"` // a member is: agents can pick it
Info []memberJSON `json:"memberInfo"`
// Holds: the groups in it, at any depth — none of which can have it in
// turn
Holds []string `json:"holds"`
// Offers: the reasoning levels agents are offered for it; Shared: those
// its members have in common, which it offers unless it names its own
Offers []string `json:"offers"`
Shared []string `json:"shared"`
// Picked: the member a manual group sends every request to
Picked string `json:"picked,omitempty"`
}
type memberJSON struct {
ID string `json:"id"`
Ready bool `json:"ready"`
Provider string `json:"provider,omitempty"`
Name string `json:"name,omitempty"` // the provider's
Icon string `json:"icon,omitempty"`
Model string `json:"model,omitempty"` // what the vendor is asked for
On int `json:"on"` // its keys or accounts on
Group bool `json:"group,omitempty"` // a routing group in the group; Name is its
// Of is the model's id without the effort the member is fixed at, and
// Effort that effort ("provider/model:low"); "" for one without
Of string `json:"of,omitempty"`
Effort string `json:"effort,omitempty"`
// Fast: the group sends it in its vendor's fast mode; CanFast: its
// model has one (provider.CanFast)
Fast bool `json:"fast,omitempty"`
CanFast bool `json:"canFast,omitempty"`
// what a rule may send it: the tokens it takes, when known, and images
Context int `json:"context,omitempty"`
Images bool `json:"images,omitempty"`
}
type modelRef struct {
ID string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
PName string `json:"providerName"`
Icon string `json:"icon,omitempty"`
Context int `json:"context,omitempty"` // the tokens it takes, when known
// Efforts: the model's reasoning levels, when known — those a group's
// member of it may be fixed at
Efforts []string `json:"efforts,omitempty"`
// CanFast: a group's member of it may be sent in its vendor's fast
// mode (provider.CanFast)
CanFast bool `json:"canFast,omitempty"`
}
type poolJSON struct {
Provider string `json:"provider"`
Name string `json:"name"`
Icon string `json:"icon,omitempty"`
Kind string `json:"kind"` // "account" or "key"
Who []string `json:"who"`
Routing string `json:"routing"`
Affinity string `json:"affinity"`
// Protocol: the one its keys are made for, when the provider's keys are
// made for more than one — each protocol's keys are a pool of their own
Protocol provider.Protocol `json:"protocol,omitempty"`
}
// onOf is who a provider's requests spread over: its accounts, or keys.
func onOf(p provider.Provider) (kind string, who []string) {
if p.Account != nil {
who = append(who, p.Account.User)
for _, q := range p.AlsoOn() {
who = append(who, q.Account.User)
}
return "account", who
}
for _, k := range p.KeysOn() {
n := k.Name
if n == "" {
n = provider.Mask(k.Key)
}
who = append(who, n)
}
return "key", who
}
// keyPools is a provider's keys by the protocol they're made for, as the
// gateway routes over them: keys made for different protocols are not one
// pool (gateway.perKeyOf).
func keyPools(p provider.Provider) []poolJSON {
var out []poolJSON
at := map[provider.Protocol]int{}
for _, k := range p.KeysOn() {
if len(p.WithKey(k).Speaks()) == 0 {
continue // made for a protocol this provider has no endpoint for
}
i, ok := at[k.Protocol]
if !ok {
i = len(out)
at[k.Protocol] = i
out = append(out, poolJSON{Protocol: k.Protocol})
}
n := k.Name
if n == "" {
n = provider.Mask(k.Key)
}
out[i].Who = append(out[i].Who, n)
}
return out
}
func groupsState() groupsJSON {
out := groupsJSON{Groups: []groupJSON{}, Models: []modelRef{}, Pools: []poolJSON{}, Deciders: []modelRef{}}
for _, e := range provider.Deciders() {
out.Deciders = append(out.Deciders, modelRef{ID: e.ID, Name: e.Name, Provider: e.Provider.ID, PName: e.Provider.Name, Icon: e.Provider.Icon})
}
served := provider.Served()
for _, e := range served {
if e.Group == "" {
out.Models = append(out.Models, modelRef{ID: e.ID, Name: e.Name, Provider: e.Provider.ID, PName: e.Provider.Name, Icon: e.Provider.Icon, Context: e.Context, Efforts: e.Efforts, CanFast: provider.CanFast(e.Provider, e.Model)})
}
}
for _, g := range provider.Groups() {
gj := groupJSON{Group: g, Info: []memberJSON{}, Holds: []string{}, Offers: []string{}, Shared: []string{}}
for _, e := range served {
if e.ID == provider.GroupPrefix+g.ID {
gj.Offers, gj.Shared = append(gj.Offers, e.Efforts...), append(gj.Shared, e.Shared...)
break
}
}
if _, ms, ok := provider.FindGroup(provider.GroupPrefix + g.ID); ok {
for _, m := range ms {
for _, v := range m.Groups() {
if !slices.Contains(gj.Holds, v) {
gj.Holds = append(gj.Holds, v)
}
}
}
}
for _, id := range g.Members {
m := memberJSON{ID: id}
if gid, ok := strings.CutPrefix(id, provider.GroupPrefix); ok {
// a group in the group: what agents see of it
m.Group = true
for _, e := range served {
if e.ID == id {
_, ms, _ := provider.FindGroup(id)
m.Ready, m.Name, m.Icon, m.On = true, e.Name, e.Provider.Icon, len(ms)
m.Context, m.Images = e.Context, e.Images && (e.ImageInput == nil || *e.ImageInput)
gj.Ready = true
break
}
}
if m.Name == "" {
m.Name = gid
}
gj.Info = append(gj.Info, m)
continue
}
of, effort := provider.MemberEffort(id)
m.Of, m.Effort = of, effort
if p, model, ok := provider.Resolve(of); ok {
_, who := onOf(p)
m.Ready, m.Provider, m.Name, m.Icon, m.Model, m.On = true, p.ID, p.Name, p.Icon, model, max(len(who), 1)
m.CanFast = provider.CanFast(p, model)
m.Fast = m.CanFast && g.IsFast(id)
for _, e := range served {
if e.Group == "" && e.Provider.ID == p.ID && e.Model == model {
m.Context, m.Images = e.Context, e.Images && (e.ImageInput == nil || *e.ImageInput)
break
}
}
gj.Ready = true
}
gj.Info = append(gj.Info, m)
}
if g.Routing == provider.Manual {
// agents can pick it while the member picked can answer
gj.Picked = g.Picked()
i := slices.IndexFunc(gj.Info, func(m memberJSON) bool { return m.ID == gj.Picked })
gj.Ready = i >= 0 && gj.Info[i].Ready
}
out.Groups = append(out.Groups, gj)
}
for _, p := range provider.All() {
if !p.On() {
continue
}
if kind, who := onOf(p); kind == "account" && len(who) > 1 {
out.Pools = append(out.Pools, poolJSON{Provider: p.ID, Name: p.Name, Icon: p.Icon, Kind: kind, Who: who, Routing: p.Routing, Affinity: p.Affinity})
continue
}
pools := keyPools(p)
for _, kp := range pools {
if len(kp.Who) > 1 {
kp.Provider, kp.Name, kp.Icon, kp.Kind, kp.Routing, kp.Affinity = p.ID, p.Name, p.Icon, "key", p.Routing, p.Affinity
if len(pools) == 1 {
kp.Protocol = ""
}
out.Pools = append(out.Pools, kp)
}
}
}
return out
}
func groupRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/groups", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, groupsState())
})
mux.HandleFunc("POST /api/groups/{action}", func(rw http.ResponseWriter, r *http.Request) {
var body struct {
provider.Group
From string `json:"from"` // the id the group had: another is a rename
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
fail(rw, err)
return
}
in := body.Group
var err error
switch r.PathValue("action") {
case "save":
to := strings.ToLower(strings.TrimSpace(in.ID))
if body.From == "" || body.From == to {
err = provider.SaveGroup(in)
break
}
if to == "" || to != provider.Slug(to) {
err = fmt.Errorf("a group's id must be lowercase letters, digits and dashes, not %q", in.ID)
break
}
in.ID = body.From
if err = provider.SaveGroup(in); err == nil {
err = provider.RenameGroup(body.From, to)
}
case "delete":
err = provider.DeleteGroup(in.ID)
case "show":
err = provider.ShowGroup(in.ID)
default:
http.NotFound(rw, r)
return
}
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, groupsState())
})
}