Files
magpie/internal/provider/group_copy.go
T
yetone e70e7ed46a routing: a routing group is made from a template in one click, of the user's own models: hard turns to the strongest and easy ones to a cheap one, cheap first, or never stuck
Making a group took the whole editor (owner, 10-10). Three templates are
made from the models the user has (provider.Templates, grouptemplate.go):

- smart (smart-split): an ordered group of the strongest model and a cheap
  one; a classifier sends a hard turn to the strong one and an easy turn or
  a compaction to the cheap one, images to a model that sees them. The
  classifier is the decision API when there is one, else the cheapest
  priced model that doesn't reason.
- thrifty (cheap-first): two cheap models of different providers, then the
  strongest, which also takes high reasoning.
- steady (never-stuck): the strongest model of up to three providers, each
  failing over to the next.

A model weighs by its maker's list price, else its provider's, else by its
id's size words; one over $100 a million output tokens is never picked. A
template that can't be made says what is missing. AddTemplate saves it
under a name and id no group has (freeGroupName, now shared with
CopyGroup), never over one.

GUI: before the user has a group of their own, the Routing page shows the
templates as cards (models, Add, Edit first); after, a Templates menu by
New group. CLI: `magpie group template [kind] [name]`, no args to list.
Pricing goes through priceOf with the served entry's provider, so
/api/groups pays ~75ms for templates on 203 models, not ~600ms.

Run, at this head rebased on origin/main 4e584faa: go test ./... green;
gofmt -l clean; go vet on darwin, GOOS=linux -tags nogui and GOOS=windows.
GUI: group-template.test.cjs and group-duplicate.test.cjs (Chromium and
WebKit, 5 languages, 420px) 30/30, the template test 3 runs in a row; it
skips WebKit's "ResizeObserver loop" notice as two other tests do. The
group-*/routing-* GUI tests ran before the rebase: 628/635, the 7 being
WebKit flights in routing-group-fold and routing-keys-fold, one Chromium
routing-proxy, and the ResizeObserver notice above; run alone at this head,
routing-proxy passes and the two fold files pass twice on WebKit, as on
origin/main. Real data in a sandbox HOME: `magpie web` on 3561, the
Templates menu added smart-split as GPT-6.1 Sol → GPT-6 Luna with 3 rules.
No real request was routed through the new group.
2026-10-10 19:46:58 +08:00

95 lines
2.5 KiB
Go

package provider
import (
"encoding/json"
"fmt"
"slices"
"strings"
)
// CopyGroup makes a group of the user's that is the group id as it is now
// (lc on Discord): its models and patterns, routing, rules, classifier,
// effort, context, levels, fast and off members, under name — "<name>
// copy" when empty, numbered past a name or id in use — listed right after
// the one it copies. A found group's copy is the user's, with the members
// it has now. The copy is on, whether the group was or not, and the
// original is left as it is.
func CopyGroup(id, name string) (Group, error) {
all := Groups()
i := slices.IndexFunc(all, func(g Group) bool { return g.ID == id && !g.Hidden })
if i < 0 {
return Group{}, fmt.Errorf("no group %q", id)
}
src := all[i]
// a copy shares nothing with the group it was read from: the rules'
// time windows are pointers, the lists slices
raw, err := json.Marshal(src)
if err != nil {
return Group{}, err
}
var g Group
if err := json.Unmarshal(raw, &g); err != nil {
return Group{}, err
}
g.Auto, g.Hidden, g.Disabled = false, false, false
name = strings.TrimSpace(name)
if name == "" {
name = src.Name + " copy"
}
g.Name, g.ID = freeGroupName(all, name, "")
if err := SaveGroup(g); err != nil {
return Group{}, err
}
// listed after the one it copies, not at the end of the user's
order := make([]string, 0, len(all)+1)
for _, o := range all {
order = append(order, o.ID)
if o.ID == src.ID {
order = append(order, g.ID)
}
}
if err := SetGroupOrder(order); err != nil {
return Group{}, err
}
for _, o := range Groups() {
if o.ID == g.ID {
return o, nil
}
}
return g, nil
}
// freeGroupName is name, numbered past a name in use ("name 2"), and slug —
// or, when it is "", the id made of the name — numbered past an id in use or a group removed ("slug-2"):
// what a group made in a click (a copy, a template) is saved as, so it
// never replaces one there is.
func freeGroupName(all []Group, name, slug string) (string, string) {
names, ids := map[string]bool{}, map[string]bool{}
for _, o := range all {
ids[o.ID] = true
if !o.Hidden {
names[strings.ToLower(o.Name)] = true
}
}
for _, removed := range RemovedGroups() {
ids[removed] = true
}
base := name
for n := 2; names[strings.ToLower(name)]; n++ {
name = fmt.Sprintf("%s %d", base, n)
}
if slug == "" {
slug = GroupSlug(name)
}
if slug == "" {
slug = "group"
}
id := slug
for n := 2; ids[id]; n++ {
id = fmt.Sprintf("%s-%d", slug, n)
}
return name, id
}