Garydesu on X: "能不能让路由组里的模型设置多个可选推理强度,现在只能选一个或者整个 路由组的". A member was one fixed `:effort` or followed the group's levels. A member's suffix may now join several levels with "+", `provider/model:low+xhigh`, kept lowest first, each once. provider.MemberLevels splits it into Member.Levels and leaves Effort empty, so the member isn't fixed; one level is a fixed effort as before. The whole stays the member's id, so rules, fast, off, weights, share and the CLI key it as any member. The gateway fits the agent's ask to the member's own levels (groupLevels returns the candidate's levels first; ofMember, the quiet check and the trace see them), and the group lists them to agents (buildGroupEntries), intersected with the other members' as usual. In the Routing page's group editor the member's reasoning menu has "Several levels…", a menu that stays open with each level ticked or not; a fixed level picked from the first menu replaces the old one as before. i18n in zh, zh-TW, ja, de; CLI help and docs/subsystems/gateway-routing.md say it. Run on mote (mote-run). Rebased onefb55b5c9(paste1721d640, which changed the group editor) and run again there: provider, gateway, cli ok; gofmt clean; GUI group-member-levels, group-member-fast, group-member-weight, group-from-model-add and i18n-keys 53 of 54, the red being i18n-keys' "masking…" frome296211b8, red on main (#4053). Linux and Windows vet were clean at7fe98c2af. Earlier, at the first base: - go test -tags nogui -timeout 45m ./internal/provider ./internal/gateway ./internal/cli ./internal/tui ./internal/gui: all ok (gateway 525s). internal/cli again after the help wording below: ok. - gofmt -l: nothing. - GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go vet ./...: clean (darwin prints cgo warnings only). - Whole GUI suite (node --test "internal/gui/tests/*.test.cjs", both engines, load 140 at its peak): 13,931 tests, 119 red in 31 files. One was this change: i18n-keys' "a plugin's words are its own", because the CLI help said "the agent picks among them" too; reworded the help, and i18n-keys passes. The other 30 files, run alone at --test-concurrency=4 at this tree and at its parente8c7dddcb: the same files red on both (97 and 100 cases, the routing-rule ones a random subset each run, all waiting on clicks), each with an open issue (#3527 lists the pattern; segs-press-redraw #3922, routing-story-shrinks #3954). group-from-model-add, account-*, login-import and signin-* pass alone. - group-member-levels.test.cjs: 30 of 30 (5 languages, both engines, 1100px and 420px); group-member-fast.test.cjs 4 of 4. Broken by hand: - groupLevels without the member's levels: TestMemberSeveralLevelsFollowAgent red (asked high, sent high, not xhigh). - buildGroupEntries without the member's levels: TestMemberSeveralLevels red (offered all four, not low and xhigh). - main's routing client.js: group-member-levels red 3 of 3 (chromium en). Not tried: a real agent picking among a member's levels against a real vendor; the TUI (its reasoning toggle cycles a "low+high" member back to following the group, unchanged code).
187 lines
6.5 KiB
Go
187 lines
6.5 KiB
Go
package provider
|
|
|
|
// A group's member may carry a reasoning effort of its own (#189):
|
|
// "provider/model:low". A request routed to that member asks its model for
|
|
// that much reasoning — whatever the agent asked for, and whatever the
|
|
// group's classifier picked for the turn — at the level the model has
|
|
// nearest to it. A member without one reasons as the group says (the
|
|
// agent's, or the classifier's pick).
|
|
//
|
|
// The effort is part of the member's id: the same model at two efforts is
|
|
// two members, which rules, stickiness and the trace tell apart, while the
|
|
// vendor is asked for the model's own id. It is stored as it is typed, so
|
|
// a group saved before reads the same, and a backup or a sync carries it
|
|
// as any other member.
|
|
//
|
|
// A member may instead carry several levels, joined by "+"
|
|
// ("provider/model:low+high", Garydesu on X): it follows the agent's
|
|
// effort, as a member without one does, but only among those levels — the
|
|
// group offers agents them for it (groupEntries), and a request asking
|
|
// for another is sent the nearest of them (Member.Levels). One level is a
|
|
// fixed effort as before, so a member saved before reads the same.
|
|
//
|
|
// A model's own id may have a colon in it — OpenRouter's
|
|
// "deepseek/deepseek-r1:free", Ollama's "qwen:7b", Bedrock's
|
|
// "anthropic.claude-…-v1:0" — so only a last part that is a level's name
|
|
// is taken as an effort, and only when the whole id isn't a model the
|
|
// provider has.
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// MemberEfforts are the levels a group's member may be fixed at, lowest
|
|
// first: the words agents and vendors use for reasoning effort.
|
|
var MemberEfforts = []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"}
|
|
|
|
// MemberEffort splits a group's member into the model it names and the
|
|
// effort fixed on it ("" for none): "a/m:high" is a/m at high, unless a
|
|
// has a model "m:high" of its own. A group in the group ("group/<id>")
|
|
// takes none; one typed with one comes back split, for SaveGroup to refuse.
|
|
func MemberEffort(id string) (model, effort string) {
|
|
return memberEffortIn(nil, id)
|
|
}
|
|
|
|
// memberEffortIn is MemberEffort with the catalog's entries read already
|
|
// (nil to read the providers' models as needed). Several levels come
|
|
// back as MemberLevels reads them: each once, lowest first, joined by "+".
|
|
func memberEffortIn(entries []Entry, id string) (string, string) {
|
|
i := strings.LastIndex(id, ":")
|
|
if i <= 0 {
|
|
return id, ""
|
|
}
|
|
level := memberLevelsSpelled(id[i+1:])
|
|
if level == "" {
|
|
return id, "" // ":free", ":7b", ":0" — the model's own
|
|
}
|
|
if !strings.HasPrefix(id, GroupPrefix) && isModel(entries, id) {
|
|
return id, ""
|
|
}
|
|
return id[:i], level
|
|
}
|
|
|
|
// memberLevelsSpelled is a member's effort suffix as it is kept: one
|
|
// level, or several joined by "+" each once, lowest first, lowercase; ""
|
|
// when a part isn't a level (the suffix is the model's own).
|
|
func memberLevelsSpelled(suffix string) string {
|
|
parts := strings.Split(strings.ToLower(suffix), "+")
|
|
for _, p := range parts {
|
|
if !slices.Contains(MemberEfforts, p) {
|
|
return ""
|
|
}
|
|
}
|
|
return strings.Join(fixedLevels(parts), "+")
|
|
}
|
|
|
|
// MemberLevels splits the effort MemberEffort gives into the one a member
|
|
// is fixed at and the several the agent picks among (nil for one or none):
|
|
// "high" is fixed at high, "low+high" follows the agent among low and high.
|
|
func MemberLevels(effort string) (fixed string, levels []string) {
|
|
if !strings.Contains(effort, "+") {
|
|
return effort, nil
|
|
}
|
|
return "", strings.Split(effort, "+")
|
|
}
|
|
|
|
// Spelled is the member's model as the group names it: with the effort it
|
|
// is fixed at, or the levels it follows the agent among.
|
|
func (m Member) Spelled() string {
|
|
return WithMemberEffort(m.Provider.ID+"/"+m.Model, cmp.Or(m.Effort, strings.Join(m.Levels, "+")))
|
|
}
|
|
|
|
// isModel reports whether id is a model a provider has as it is spelled,
|
|
// colon and all: a catalog id, a model the user picked for its provider,
|
|
// or one its provider lists.
|
|
func isModel(entries []Entry, id string) bool {
|
|
if slices.ContainsFunc(entries, func(e Entry) bool { return e.ID == id || e.Model == id }) {
|
|
return true
|
|
}
|
|
// what the providers have, looked at once while a request holds them
|
|
// (XcodeFish, #2502): a group's members are expanded several times a
|
|
// request, and each member at an effort found every provider again
|
|
// (Find) and read its whole model list (Available), a third of
|
|
// /v1/models' CPU
|
|
return heldOf("has-model\x00"+id, func() bool { return providersHave(id) })
|
|
}
|
|
|
|
// providersLooked counts providersHave's looks, for tests.
|
|
var providersLooked atomic.Int64
|
|
|
|
// providersHave is isModel's look at the providers, past the catalog.
|
|
func providersHave(id string) bool {
|
|
providersLooked.Add(1)
|
|
pid, model, ok := strings.Cut(id, "/")
|
|
var ps []Provider
|
|
if ok {
|
|
if p, err := Find(pid); err == nil {
|
|
ps = []Provider{*p}
|
|
}
|
|
} else {
|
|
model, ps = id, All()
|
|
}
|
|
for _, p := range ps {
|
|
if slices.Contains(p.Models, model) || slices.ContainsFunc(p.Available(), func(m catalog.Model) bool { return m.ID == model }) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// WithMemberEffort is the member id of model at effort: the model itself
|
|
// for "".
|
|
func WithMemberEffort(model, effort string) string {
|
|
if effort == "" {
|
|
return model
|
|
}
|
|
return model + ":" + effort
|
|
}
|
|
|
|
// cleanMember spells a member as it is kept: its effort lowercase, a found
|
|
// group by group/auto-<slug> (CanonicalRef).
|
|
func cleanMember(entries []Entry, id string) string {
|
|
id = CanonicalRef(id) // a found group by the id magpie keeps it by
|
|
m, e := memberEffortIn(entries, id)
|
|
return WithMemberEffort(m, e)
|
|
}
|
|
|
|
// fixedLevels are the efforts a group's members are fixed at, lowest first.
|
|
func fixedLevels(fixed []string) []string {
|
|
var out []string
|
|
for _, l := range MemberEfforts {
|
|
if slices.Contains(fixed, l) {
|
|
out = append(out, l)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// CleanLevels are the reasoning levels a group names for itself
|
|
// (Group.Levels) as they are kept: lowercase, each once, lowest first.
|
|
// Only the levels magpie knows are taken.
|
|
func CleanLevels(in []string) ([]string, error) {
|
|
var want []string
|
|
for _, l := range in {
|
|
l = strings.ToLower(strings.TrimSpace(l))
|
|
if l == "" {
|
|
continue
|
|
}
|
|
if !slices.Contains(Levels, l) {
|
|
return nil, fmt.Errorf("%q is not a reasoning level magpie knows (they are %s)", l, strings.Join(Levels, ", "))
|
|
}
|
|
want = append(want, l)
|
|
}
|
|
var out []string
|
|
for _, l := range Levels {
|
|
if slices.Contains(want, l) {
|
|
out = append(out, l)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|