monkeyin_92 on X: "can a rule take several models? Otherwise one rule
with several models is several rules." A rule now takes more than one
model (Rule.More after Rule.Use; `more` in the group's JSON, use=a,b
typed). They go first in that order: when the first fails or rests the
rule's next takes the request, ahead of the models the later matching
rules name and the group's others.
Every reader of Rule.Use goes through Uses/SetUses/MapUses/Without/Names
in grouprule.go:
- cleanRules: each must be the group's, each named once; a pause names one.
- gateway: ruleFor's turn hold, tool rounds and outgrown turns, compaction
(the rule's first that takes the summary, the others in Small),
ruleMembers/ruleOver/applyRule, nestedRules and ruleLed (the one a
nested group's turn stays on), modelRuled, ruleEffort (useEffort asked
of each), ThenUses (leaves out all of the rule's own). RuleHit.More is
in the trace; Instead from More has Unready false, one from a later
rule Unready true, as before.
- ruledEntry: what the group takes is what every one of them takes.
- provider rename, auto/ refs, groupfast, a member's removal (a rule
goes with its last), ShareGroup and its import (one this magpie hasn't
is left out of the rule).
- CLI `group rule add use=a,b` and its listing; `group models-=` keeps a
rule's other models. The TUI shows them joined and keeps the rule when
one is deleted.
- GUI (plugins/routing): a chip per model, a + for the next one, an ×
on each while there is more than one, a chip picks another in its place
(app menu, no native select); the card's tooltip and the trace's why
("…so c/mini, which the rule names after it, goes first"), nested too;
en, zh, zh-TW, ja, de.
Old data: a rule of one model writes no `more`, so groups saved before
read and save unchanged (TestRuleMoreOnDisk). An older magpie reads `use`
alone: its requests go to the first model, the others are plain group
members in group order; if it saves the group, `more` is dropped.
Tests: TestRuleSeveralModelsInOrder, TestRuleEffortReachesTheRulesNextModel,
TestRuleCompactPassesOverTheRulesSmallModel (gateway); TestCleanRulesSeveralModels,
TestRuleMoreOnDisk, TestRuleWithout, TestParseRuleSeveralModels,
TestThenUsesLeavesOutTheRulesOwn, TestRuledEntrySeveralModels,
TestSharedGroupRuleSeveralModels, TestRuleMoreRenamedAndKept (provider);
TestGroupRuleSeveralModelsCmd (cli); TestGroupTakeOutKeepsARulesOtherModels
(tui); routing-rule-models.test.cjs. Broken by hand: ruleMembers on Use
alone turns the first two gateway tests red.
Run at this head:
- gofmt -l clean; go vet -tags nogui clean with GOOS=linux and GOOS=windows
for provider, gateway, cli, tui.
- go test -tags nogui -timeout 45m: internal/provider, internal/cli,
internal/tui ok; internal/gateway ok (384s); the new gateway tests
again after; internal/gui budgets ok.
- GUI: every routing-*.test.cjs plus i18n-keys, click-scroll and
group-member-drag, chromium and webkit (en, zh, zh-TW, ja, de where the
file has them): 1319 pass, 57 red. routing-rule-models passes in both.
The reds are in files that are red the same on origin/main 42976f9a0,
run there just now: routing-pause (#3410), routing-intent-entry
(#3408), routing-rule-effort (#3411), routing-rule-remove (#3096,
#3094), routing-manual (#3409), routing-steady (#3412, #1804),
routing-live-patch (#2174), routing-time (#3465). The other 10 reds are
routing-rule-drag.test.cjs, which is the next commit's and not in this one.
- Real run, sandbox HOME, built binary: three fake OpenAI vendors, group
Mix = three/m, one/m, two/m, rule `use=one/m,two/m tokens=1`. one/m
answers 429: the request went one -> two (not three, the group's
first); the next went straight to two, the trace saying
instead=two/m with more=[two/m].
Not tried: a real vendor account; the TUI by hand; Windows and Linux
runs (vet only).
Rebased onto 5b2b3d3f5 (10 commits, gateway, gui and routing's
client.js among them) and run again at the rebased pair's head
68701dfe4: gofmt -l clean; GOOS=linux and windows vet -tags nogui of
provider, gateway, cli, tui clean; go test -tags nogui -timeout 45m
internal/provider (171s), cli, tui, gateway (422s), gui (budgets): all
ok; GUI routing-rule-drag, routing-rule-models, routing-compact,
i18n-keys, click-scroll, group-member-drag in both engines all pass;
routing-rule-remove red as on main (#3096, #3094).
230 lines
10 KiB
Go
230 lines
10 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A group's rules from the terminal: magpie group rule add|rm|mv …, as the
|
|
// Routing view's Rules section makes them.
|
|
|
|
const ruleUsage = `usage:
|
|
magpie group rule <group> the group's rules
|
|
magpie group rule add <group> use=<model> [use-effort=low|medium|high|xhigh|max] [tokens=<n>] [images] [effort=on|low|medium|high|xhigh|max] [agents=a,b…]
|
|
[intent="<what the message asks for>"] [compact] [time=HH:MM-HH:MM] [days=mon-fri]
|
|
[folders=<folder>,…] [except-folders=<folder>,…] [classifier=<model>] [at=<n>]
|
|
a rule: a turn that matches it goes to <model>, one of the group's
|
|
(or group/<id>, a group in it), first; a model in the group at an
|
|
effort of its own is named with it (use=glm/glm-5.3-flash:high).
|
|
use=a,b names several, in order: b goes when a can't take the
|
|
request, before the models of the rules after it and the others.
|
|
use-effort= is the reasoning <model> is asked for on the requests
|
|
the rule sends it, whatever the agent asked: at the model's nearest
|
|
level, or as the agent asked when its levels aren't known
|
|
magpie group rule add <group> pause=<model> [time=HH:MM-HH:MM] [days=mon-fri] [agents=a,b…]
|
|
[folders=<folder>,…] [except-folders=<folder>,…] [at=<n>]
|
|
a pause: while it holds, <model> is left out of the group — sent
|
|
nothing, not even when the others fail or a conversation was on it
|
|
magpie group rule rm <group> <n> remove rule n
|
|
magpie group rule mv <group> <n> <to> move rule n to place <to>
|
|
magpie group rule classifier <group> <model>
|
|
the model that tells which intent a message is, or
|
|
group/<id>: another group, its models tried in turn
|
|
|
|
Rules are looked at top first when you send a message (a new turn); the first that
|
|
matches puts its model first, and the group's others stay behind it if it fails.
|
|
A pause is looked at on every request and leaves its model out while it holds; its
|
|
conditions are time, days, agents, folders and except-folders only.
|
|
The agent's tool results within the turn stay with the model the turn began on.
|
|
Every condition given must hold:
|
|
tokens the request is at least this long (200000, 200k, 1m): estimated from its size, or
|
|
what the vendor counted the conversation's last request as, whichever is more
|
|
images it carries an image, now or earlier in the conversation
|
|
effort the agent asked for reasoning: on (any), or at least this level
|
|
agents it comes from one of these agents (claude, codex, opencode, … as magpie usage names them)
|
|
intent the user's message is of this kind, in your words ("writing or fixing tests", "a quick
|
|
question"): as the turn begins, the group's classifier — any model magpie has, best a small
|
|
fast one without reasoning, or another group of them to fail over — is asked which of the intents that may match the message is,
|
|
once; if it fails or can't say, no intent matches. Its call shows in the usage as magpie's own
|
|
compact the agent is compacting the conversation (Claude Code's /compact or auto-compact, Codex's,
|
|
OpenCode's, Pi's, Gemini CLI's, Qwen Code's, Kimi's): a cheaper, faster model can write the
|
|
summary. The request is as long as the conversation, so a model known to take less is passed
|
|
over; it is not the turn's model, and the requests after it go on as before
|
|
time the turn begins within these hours, on this computer's clock (09:00-18:00; 22:00-08:00
|
|
runs past midnight) — a provider's peak-price hours sent to another, say
|
|
days only on these days (mon-fri, sat,sun); with time, a window past midnight is of the day it
|
|
begins on. Days alone hold all day
|
|
folders the agent works in one of these folders, or a folder inside one (~/work/acme, ~/oss;
|
|
C:\work, which is /mnt/c/work in WSL): a project, or every project under a folder. The
|
|
folder is the one the agent tells its model in the request (Claude Code, Codex, OpenCode,
|
|
Pi); a request that doesn't tell it matches no folder. Quote a folder with a space:
|
|
folders="~/My Project,~/oss"
|
|
except-folders
|
|
the agent works in none of these folders, nor a folder inside one. A request that doesn't
|
|
tell its folder isn't known to be in one, so it holds for it: pause=<model>
|
|
except-folders=~/big-task keeps <model> for the task in ~/big-task — every other request,
|
|
a subagent's or a small model's too when it doesn't tell its folder, goes elsewhere
|
|
|
|
e.g. magpie group rule add opus-anywhere use=openrouter/google/gemini-3-pro tokens=200k
|
|
magpie group rule add opus-anywhere use=a/vision-model images
|
|
magpie group rule add fast use=codex/gpt-5.6-sol:xhigh effort=high
|
|
magpie group rule add opus-anywhere use=deepseek/deepseek-v4-flash compact
|
|
magpie group rule add cheap use=glm/glm-5.3 time=14:00-18:00 days=mon-fri
|
|
magpie group rule add cheap pause=glm/glm-5.3 time=14:00-18:00 days=mon-fri
|
|
magpie group rule add opus-anywhere use=kimi/kimi-k2.6 folders=~/oss
|
|
magpie group rule add auto-claude-opus-5-5 pause=claude/claude-opus-5-5 except-folders=~/big-task
|
|
magpie group rule add opus-anywhere use=deepseek/deepseek-v4-flash intent="a quick question" classifier=groq/llama-3.1-8b-instant
|
|
magpie group rule add fast use=codex/gpt-5.6-sol use-effort=low intent="a quick question"`
|
|
|
|
// parseTokens reads 200000, 200k, 1.5m.
|
|
func parseTokens(v string) (int, error) { return provider.ParseTokens(v) }
|
|
|
|
// parseRule makes a rule of k=v words (provider.ParseRule), an unknown
|
|
// word answered with the usage.
|
|
func parseRule(g provider.Group, words []string) (provider.Rule, int, string, error) {
|
|
r, at, classifier, err := provider.ParseRule(g, words)
|
|
if err != nil && strings.HasPrefix(err.Error(), provider.UnknownRuleWord) {
|
|
err = fmt.Errorf("%w\n\n%s", err, ruleUsage)
|
|
}
|
|
return r, at, classifier, err
|
|
}
|
|
|
|
// groupMember is the group's member a typed model names.
|
|
func groupMember(g provider.Group, in string) (string, error) { return provider.GroupMember(g, in) }
|
|
|
|
// ruleCmd: magpie group rule …
|
|
func ruleCmd(args []string) error {
|
|
if len(args) == 0 || slices.Contains([]string{"help", "-h", "--help"}, args[0]) {
|
|
fmt.Println(ruleUsage)
|
|
return nil
|
|
}
|
|
verb, rest := args[0], args[1:]
|
|
if len(rest) == 0 {
|
|
// magpie group rule <group>: the group, its rules with it
|
|
if g, err := findGroup(verb); err == nil {
|
|
if len(g.Rules) == 0 {
|
|
fmt.Println(muted.Render(" " + g.ID + " has no rules · magpie group rule add " + g.ID + " use=<model> …"))
|
|
return nil
|
|
}
|
|
return showGroup(g)
|
|
}
|
|
return fmt.Errorf("%s", ruleUsage)
|
|
}
|
|
g, err := findGroup(rest[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if g.Hidden {
|
|
return fmt.Errorf("%s was removed: magpie group restore %s brings it back first", g.ID, g.ID)
|
|
}
|
|
place := func(s string) (int, error) {
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil || n < 1 || n > len(g.Rules) {
|
|
if len(g.Rules) == 0 {
|
|
return 0, fmt.Errorf("%s has no rules", g.ID)
|
|
}
|
|
return 0, fmt.Errorf("rule %s: %s has rules 1 to %d", s, g.ID, len(g.Rules))
|
|
}
|
|
return n - 1, nil
|
|
}
|
|
switch verb {
|
|
case "add", "new":
|
|
r, at, classifier, err := parseRule(g, rest[1:])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if classifier != "" {
|
|
g.Classifier = classifier
|
|
}
|
|
if at == 0 || at > len(g.Rules) {
|
|
g.Rules = append(g.Rules, r)
|
|
} else {
|
|
g.Rules = slices.Insert(g.Rules, at-1, r)
|
|
}
|
|
case "rm", "remove", "delete":
|
|
if len(rest) != 2 {
|
|
return fmt.Errorf("magpie group rule rm <group> <n>")
|
|
}
|
|
i, err := place(rest[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
g.Rules = slices.Delete(g.Rules, i, i+1)
|
|
case "mv", "move":
|
|
if len(rest) != 3 {
|
|
return fmt.Errorf("magpie group rule mv <group> <n> <to>")
|
|
}
|
|
i, err := place(rest[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
j, err := place(rest[2])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r := g.Rules[i]
|
|
g.Rules = slices.Insert(slices.Delete(g.Rules, i, i+1), j, r)
|
|
case "classifier", "classify":
|
|
if len(rest) != 2 {
|
|
return fmt.Errorf("magpie group rule classifier <group> <model>")
|
|
}
|
|
if !slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Intent != "" }) {
|
|
return fmt.Errorf("%s has no rule with an intent to classify for", g.ID)
|
|
}
|
|
g.Classifier = rest[1]
|
|
default:
|
|
return fmt.Errorf("magpie group rule has no %q\n\n%s", verb, ruleUsage)
|
|
}
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
return err
|
|
}
|
|
g, err = findGroup(g.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "saved", bold.Render(g.Name))
|
|
return showGroup(g)
|
|
}
|
|
|
|
// ruleLine is a rule as magpie group shows it.
|
|
func ruleLine(r provider.Rule) string {
|
|
if r.Pause {
|
|
return strings.Join(r.Conditions(), " · ") + muted.Render(" → pause ") + r.Use
|
|
}
|
|
// the models it sends to, in order: the next when one can't take it
|
|
uses := strings.Join(r.Uses(), muted.Render(", "))
|
|
if r.UseEffort != "" {
|
|
return strings.Join(r.Conditions(), " · ") + muted.Render(" → ") + uses + muted.Render(" at ") + r.UseEffort
|
|
}
|
|
return strings.Join(r.Conditions(), " · ") + muted.Render(" → ") + uses
|
|
}
|
|
|
|
// pruneRules drops the rules for models no longer in the group, saying so.
|
|
func pruneRules(g *provider.Group) {
|
|
var keep []provider.Rule
|
|
for i, r := range g.Rules {
|
|
// a model gone from the group leaves the rule; the rule goes with
|
|
// the last it sends to
|
|
for _, u := range r.Uses() {
|
|
if slices.Contains(g.Members, u) {
|
|
continue
|
|
}
|
|
kept, ok := r.Without(u)
|
|
if !ok {
|
|
fmt.Println(amber.Render("!"), fmt.Sprintf("rule %d is removed with %s", i+1, u))
|
|
break
|
|
}
|
|
fmt.Println(amber.Render("!"), fmt.Sprintf("rule %d no longer sends to %s", i+1, u))
|
|
r = kept
|
|
}
|
|
if slices.Contains(g.Members, r.Use) {
|
|
keep = append(keep, r)
|
|
}
|
|
}
|
|
g.Rules = keep
|
|
}
|