Files
magpie/internal/provider/decide_group.go
T
yetone b1396d2360 gateway, provider, gui: a routing group of decision models answers /v1/systemone, failing over between them (ARNO on Discord)
ARNO asked for decision models to get the group feature too: /v1/systemone
took only "<provider>/<model>" (xxx/jev-latest), so one refusing key or
vendor failed the call with nothing to fall back on.

A routing group may now be of decision models alone (provider.Group.Decides,
provider/decide_group.go). SaveGroup refuses one mixed with models that hold
a conversation, a decision group inside a chat group, and patterns, rules,
classifier, effort, levels, fast mode or a member effort on it. It is kept
out of the agents' catalog (groupEntries), and a chat request to it gets a
400 pointing at /v1/systemone.

POST /v1/systemone with "model":"group/<id>" (or the group's id or name, as
GroupFor reads them) goes to serveDecisionGroup (gateway/decide_group.go):
- the members' keys and accounts are planned with planGroup in the group's
  routing order, resting ones last; perKeyBarred no longer drops a
  decision-only provider's keys (it speaks no chat protocol), so each key
  is a try of its own;
- a gateway key's model and account limits and pause rules hold as for chat;
- a retryable failure that isn't the request's own (shape/prompt/protection)
  rests the candidate via restAfter and the next is asked; a transport error
  rests it as a 502; the last one's answer and Retry-After are passed on;
- every try is a Try of one route in the routing log; one usage row for the
  answer.
A group classifier naming a decision group fails over the same way
(askDecisionGroup; systemOne now returns decideRefused with the vendor's
status, headers and body, and postDecide returns the headers).

The GUI group editor offers a group of decision models only decision models
(groups.deciders), a chat group no decision model or decision group, a new
one both, and a decision model's row has no reasoning picker (groupJSON
.decides). The TUI member picker and `magpie group add` follow the same rule.
#1514's plugin_decide_test and TestMixedDecisionProvider now assert a mixed group is refused and a group
of decision models saves. Docs: reference.md (routing groups),
gateway-routing.md, provider-plugins.md.

Run:
- go test -tags nogui -timeout 40m . ./internal/provider ./internal/gateway
  ./internal/gui ./internal/tui: ok
- new: TestSystemOneDecisionGroupFailsOver (httptest System One fakes: a
  provider with two keys answering 429 and 503, then a second provider
  answering 200: tries ka1,ka2,kb, both failures rested and ask by name
  goes to kb first; all failing passes the last answer on; chat to the
  group is 400), TestDecisionGroupClassifierFailsOver, TestDecisionGroup,
  TestGroupAddDecisionModels, TestMemberOptionsDecisionGroup,
  group-decide-members.test.cjs (Chromium and WebKit, en and zh; no new
  strings)
- each broken by hand and seen red: no dispatch (404), no perKeyBarred
  change (ka2 skipped), no classifier failover, no rest, decision group in
  the catalog, CLI resolver, TUI and GUI picker filters
- GUI: group-*, routing-effort*, routing-levels, routing-save*,
  routing-intent-entry, routing-manual, i18n-keys in both engines: 169 pass
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on the
  touched packages
- after rebasing on bc2496dca, the five packages again: ok. The first run
  had TestMaxRPMWaitsForRoomInTheMinute red once under load; it passed
  -count=3 alone and in the rerun, and nothing here touches rpm.go.
Not tried against a real TypeSafe or Bailian account.
2026-10-11 01:37:18 +08:00

90 lines
3.4 KiB
Go

package provider
import (
"fmt"
"slices"
"strings"
)
// A decision group is a routing group of decision models (Jev and the
// like) and nothing else (ARNO on Discord: 给decision模型也加上group功能):
// a System One request naming it (/v1/systemone, "group/<id>" or the
// group's id or name) goes to its models as a conversation goes to a
// chat group's, the next asked when one fails. It holds no conversation,
// so it is kept out of the agents' catalog, a chat group can't hold it,
// and it takes nothing that shapes a conversation: no patterns, rules,
// classifier, effort, levels or fast mode.
// decidingMember reports whether a group's member names a decision model:
// "provider/model" whose provider answers model at its decision API, on
// or not. A group, or a member with an effort of its own, is not one.
func decidingMember(id string) bool {
if strings.HasPrefix(id, GroupPrefix) {
return false
}
if IsDecider(id) {
return true
}
pid, model, ok := strings.Cut(id, "/")
if !ok || model == "" {
return false
}
p, err := Find(pid)
return err == nil && p.DecidesModel(model)
}
// Decides reports whether g is a decision group: every model it names is
// a decision model.
func (g Group) Decides() bool {
return len(g.Members) > 0 && !slices.ContainsFunc(g.Members, func(m string) bool { return !decidingMember(m) })
}
// DecisionGroup reports whether ms, a group's models as they resolve now,
// are decision models, every one: the group answers System One, not a
// conversation.
func DecisionGroup(ms []Member) bool {
return len(ms) > 0 && !slices.ContainsFunc(ms, func(m Member) bool { return !m.Provider.DecidesModel(m.Model) })
}
// cleanDecisionGroup checks a decision group as SaveGroup is to keep it,
// and a chat group for decision models or decision groups in it; all is
// every group magpie has.
func cleanDecisionGroup(g *Group, all []Group) error {
var deciders, others []string
for _, m := range g.Members {
if model, _ := memberEffortIn(nil, m); decidingMember(model) {
deciders = append(deciders, m)
} else {
others = append(others, m)
}
}
if len(deciders) == 0 {
// a chat group: none of its groups may be a decision group
for _, m := range g.Members {
if gid, ok := strings.CutPrefix(m, GroupPrefix); ok {
if sub, ok := groupOf(all, gid); ok && sub.Decides() {
return fmt.Errorf("%s holds decision models, which answer System One, not a conversation: it can be %s's classifier, not one of its models", sub.Name, g.Name)
}
}
}
return nil
}
if len(others) > 0 {
return fmt.Errorf("%s is a decision model and %s holds conversations: a group is of decision models only, asked at /v1/systemone, or of models that hold conversations", deciders[0], others[0])
}
switch {
case len(g.Match) > 0:
return fmt.Errorf("%s is of decision models: it takes them by name, not by a pattern", g.Name)
case len(g.Rules) > 0, g.Classifier != "", g.Effort != "":
return fmt.Errorf("%s is of decision models, asked at /v1/systemone: it has no conversation for rules, a classifier or an effort to pick for", g.Name)
case len(g.Levels) > 0, len(g.Fast) > 0:
return fmt.Errorf("%s is of decision models: they take no reasoning level or fast mode", g.Name)
}
for _, m := range g.Members {
if model, effort := memberEffortIn(nil, m); effort != "" {
return fmt.Errorf("%s is a decision model: it takes no effort of its own (:%s)", model, effort)
}
}
return nil
}