Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

routing: a rule names several models, tried in order

yetonef99d27abc2b0
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index bfab7958d..82c2d38da 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -105,10 +105,11 @@ recorded as a prediction failure (`noPrediction`).
- A Chat or Responses client's effort spelled `off` (`reasoning_effort`, or `reasoning.effort`, any case) is read at the top of `serve` as magpie's own off level, `none` (`offAsNone`, plugins#334), and a model asked `<model>:off` as `<model>:none` (`askedAt`): every check and route below reads `none`, a model with an off level is sent its own spelling of it, and one without goes at its least level as `none` asked does (`fitEffort`). `off` itself is never sent on. `saysOff` looks for it without parsing the body, which only a body naming it pays for.
- Rules and image inputs are handled here too.
- A rule is decided as a user's turn begins and held for the rest of it (`ruleFor`, [`rules.go`](../../internal/gateway/rules.go)). What counts as the user's own words is one helper, `userWords`/`userSaid` in [`prompt.go`](../../internal/gateway/prompt.go), which the turn count (`turnIn`), the classifier's question (`userText`), the conversation's key (`firstWords`, in `ruleKey` and the affinity scope), Copilot's Auto (`lastUserText`) and the prompt breakdown's turns (`userSays`) all go by. It drops `<system-reminder …>` blocks (a `source=` attribute too) and any text that begins as an agent writes for the model alone: Codex's `<environment_context>`, `<user_instructions>`, `<turn_aborted>`, `<user_shell_command>`, `<subagent_notification>` and `# AGENTS.md instructions for …`; Claude Code's and ZCode's `<task-notification>`, `<subagent-notification>`, agent lists ("Available agent types …", "New agent types are now available …"), TodoWrite and task-tool nudges and rewind notices; ZCode's `<plugin_reference>`; the Codex app's `<in-app-browser-context …>`. The Codex app's context before `## My request (for Codex):` is dropped and what follows it kept (the `injected` list beside `agentsFor`). A user message with nothing of the user's in it (and no image or file) begins no turn: after an answer or a tool round it carries the turn on (`within`), so the turn's rule and stick hold and the classifier isn't asked; right after the user's words it leaves their turn as it began. The classifier is asked about the last user message that has words of the user's, only those words (`userText`: past 4000 runes, its first 3000 and last 1000). It is told what came before it too (`turnBefore`, @HirotaNagai on X: a short follow-up said alone, 继续 or fix that, came out the hardest kind): the user's message before it, the same way and cut to 1000 runes (`priorCap`: the first 750 and the last 250), and the assistant's last reply between the two, its text parts alone and their last 1000 runes. A tool call's arguments, a tool's result, thinking, a search's hits and the system prompt are never in either, and a reply of only tool calls is none. Jev gets them in its `state` as `previous_user_message` and `previous_assistant_reply` beside `message`, `previous_message_kind` and `previous_message_reasoning`, each only when there is one, and its intent, level and effort instructions name the ones there are (`jevState`, `priorAsk` in [`decide.go`](../../internal/gateway/decide.go)); a classifier asked in words gets them in `<previous_user_message>` and `<previous_assistant_reply>` before `<message>` (`writePrior`). Both come from the request the rules parsed, so Privacy masks them as it masks the message, and the answer kept for a message is kept for it with what came before (`classify`'s key). `TestTurnBeforeEachProtocol`, `TestTurnBeforeCuts`, `TestJevIsToldWhatCameBefore`, `TestChatClassifierIsToldWhatCameBefore`, `TestClassifyKeepsAnAnswerPerTurnBefore`, `TestJevSeesTheTurnBefore`. `firstWords` hashes the words of the first user message that has any, so an instruction file or notice the agent changes beside them doesn't move the conversation to a new key and lose its stick mid-turn (#1826: ZCode's TodoWrite nudge classified as a new, harder turn, and three keys for one ZCode session). `TestTurnInSkipsWhatTheAgentPutIn`, `TestUserTextSkipsWhatTheAgentPutIn`, `TestFirstWordsAreWhatTheUserSaid`, `TestIntentHoldsPastWhatTheAgentPutIn`.
- - A rule's members go first (`ruleOver`, `applyRule`): the member it names, then those the matching rules after it name (`ruleOrder`), each not resting, then the group's others to fail over to. A member that is a group in the group is one member here: its models keep the order the group's own routing planned them, where ranking each by the group's listed order pinned a rotate group's first member on every request the rule put it first (#2407, `TestRuleNamingARotateGroupKeepsItsTurns`). That holds within the turn too, at the group's rule and at each nested group's (`nestedRules`): the one the turn stays on (affinity) stays first while it is of the rule's member that goes first, and the member's others follow it ahead of the group's others, so a failure fails over within the member the rule chose before going outside it. One the turn was kept on outside that member, which took over when the member failed, isn't held there: once one of the member is out of its rest, the turn's next request goes back to it (`Affinity.Why` `rule`). Before #1946 (XcodeFish) a held rule's order wasn't applied to a kept turn at all: a coding-quality member failing mid-turn failed over to coding-fast, the parent's first, and the turn stayed there. `TestHeldRuleFailsOverWithinItsMember`. Two groups in the group may share a model (the same provider, model and effort): it is asked once a request, where the walk met it first, and `provider.Member.Also` keeps its later places, each with its own path, `At` (its place in the walk) and Fast. A rule naming the later group has the model as that group does (`Member.In`, `membersNamed`), in that group's own order, and the nested rules below it, Fast and the first token's wait go by that group's way down (`homed`, `homeCands`). Before #2361 (davisjiahao) such a rule found none of the shared models, read unready, and the group ahead of it answered. A pause rule naming the later group still leaves a shared model to the group ahead of it. `rule_shared_test.go`.
+ - A rule's members go first (`ruleOver`, `applyRule`): the members it names, in order, then those the matching rules after it name (`ruleOrder`), each not resting, then the group's others to fail over to. A member that is a group in the group is one member here: its models keep the order the group's own routing planned them, where ranking each by the group's listed order pinned a rotate group's first member on every request the rule put it first (#2407, `TestRuleNamingARotateGroupKeepsItsTurns`). That holds within the turn too, at the group's rule and at each nested group's (`nestedRules`): the one the turn stays on (affinity) stays first while it is of the rule's member that goes first, and the member's others follow it ahead of the group's others, so a failure fails over within the member the rule chose before going outside it. One the turn was kept on outside that member, which took over when the member failed, isn't held there: once one of the member is out of its rest, the turn's next request goes back to it (`Affinity.Why` `rule`). Before #1946 (XcodeFish) a held rule's order wasn't applied to a kept turn at all: a coding-quality member failing mid-turn failed over to coding-fast, the parent's first, and the turn stayed there. `TestHeldRuleFailsOverWithinItsMember`. Two groups in the group may share a model (the same provider, model and effort): it is asked once a request, where the walk met it first, and `provider.Member.Also` keeps its later places, each with its own path, `At` (its place in the walk) and Fast. A rule naming the later group has the model as that group does (`Member.In`, `membersNamed`), in that group's own order, and the nested rules below it, Fast and the first token's wait go by that group's way down (`homed`, `homeCands`). Before #2361 (davisjiahao) such a rule found none of the shared models, read unready, and the group ahead of it answered. A pause rule naming the later group still leaves a shared model to the group ahead of it. `rule_shared_test.go`.
- A classifier's timeout is global (`Settings.ClassifyTimeout`, JSON `classifyTimeout`, seconds, in [`settings.go`](../../internal/settings/settings.go)). Missing or 0 keeps the 8-second default; saving accepts 1–60 seconds or 0 and rejects negative values or values above 60. `Settings.ClassifyWait` bounds a hand-edited value above 60 to 60 seconds and uses the default for a value below 1. `classifyTimeout()` reads it for each call: both `askChat` in [`classify.go`](../../internal/gateway/classify.go) and `askJev` in [`decide.go`](../../internal/gateway/decide.go) give the classifier that time, including its provider's failover. Settings → Models → Replies → *Intent classifier timeout* (`renderReplies` in [`app.js`](../../internal/gui/assets/app.js)) offers 8, 15, 30 and 60 seconds and shows a custom valid value as its own choice. A change applies to the next classifier call, with no restart; the existing fallback, 30-second cooldown (`classifyRest`) and 10-minute answer cache (`classifyKeep`) still apply.
- A classifier that can't say which kind a turn is (an error, no answer within `classifyTimeout()`, an answer that isn't a number, or resting `classifyRest` after a failure) leaves a turn whose conversation had an intent, still one of this turn's, with that intent (`Classified.Fallback` is `previous`, its `Error` kept), so the turn goes where the turn before went rather than to the group's first member; with no intent before, no rule with an intent matches, as before. The Routing page says the turn kept it (`classWhy`). The turn's decision itself (`turnRules`) is kept in memory only: after a restart a turn under way waits for the next one and the stick holds it meanwhile. `TestIntentClassifierFailsKeepsThePreviousIntent`, `routing-intent-fallback.test.cjs`.
- The classifier's requests are magpie's own (#1839). Asked in words, they go out with `RouterAgent`'s User-Agent (`magpie-router/1`), so their records' agent is magpie and `callKind` marks them `routing`; a decision model's (Jev, System One) record is written with `Kind: "routing"` too. They go to the same key or account as any other request and count in its totals, while `usage.Group.Own` (`Record.ByMagpie`: agent `magpie` or one of its `magpie-` User-Agents, with search's and image description's) says how many of an account's, key's or model's calls, tokens and cost were magpie's own. A classifier set to a subscription's model (`modelRef.Sub`, or a group whose first member is one) asks that subscription as every turn begins, twice when it picks both intent and effort in words; the Routing page says so beside it (`classifierSub` in `plugins/routing/client.js`). `TestIntentRoutesATurn`, `TestJevPicksTheIntent`, `TestSummaryCountsMagpiesOwnCallsApart`, `TestByMagpie`, `TestServedRefSaysASubscription`, `own-requests.test.cjs`.
+ - A rule may name several members (`Rule.More` after `Rule.Use`, `more` in the group's JSON, `use=a,b` typed; monkeyin_92 on X: one rule, several models, not a rule each). They go first in that order, ahead of the later matching rules' members (`ThenUses` leaves out every one the rule names) and the group's others: when the first fails or rests the rule's next takes the request, and the hit says so (`RuleHit.More`; `Instead` is that next one with `Unready` false, where an `Instead` from a later rule comes with `Unready` true). `Uses`, `SetUses`, `MapUses`, `Without` and `Names` in [`grouprule.go`](../../internal/provider/grouprule.go) are how every reader goes through them: `cleanRules` (each must be the group's, each named once, a pause names one), the turn's hold, the tool rounds and compaction (`ruleFor`, the rule's first that takes the summary), `nestedRules` and `ruleLed` (the one of them a nested group's turn stays on), `ruleEffort` (the rule's `useEffort` asked of each), `ruledEntry` (what a group takes is what every one of them takes), a model's rename and removal (a rule goes with its last), `ShareGroup` and its import (one this magpie hasn't is left out of the rule), the CLI's `groups rules` and the TUI. The editor draws a chip per member with a + for the next and an × on each while there is more than one (`rUses`/`setUses` in [`plugins/routing/client.js`](../../internal/gui/assets/plugins/routing/client.js)). A rule of one member writes no `more`, so a group saved before it reads and saves as it was. A magpie from before it reads `use` alone: its requests go to the first, and the others are plain members in the group's order; if it saves the group, `more` is dropped. `TestRuleSeveralModelsInOrder`, `TestCleanRulesSeveralModels`, `routing-rule-models.test.cjs`.
- A rule can set the reasoning its member is asked for (`Rule.UseEffort`, `useEffort` in the group's JSON, `use-effort=low` typed; #2633, dengtongcai: an intent picks the model and the effort as a pair). It is an output like `Use`, not a condition like `Effort`. `ruleFor` copies it onto the hit (`RuleHit.UseEffort`) from the rule that put a member first, outer or nested; `ruleOver` keeps that rule's members, and `ruleEffort` gives each candidate of them the level fitted to its model's own (`fitLevel`), which `withFixedEffort` writes into the request's field for its protocol (Chat's `reasoning_effort`, Responses' `reasoning.effort`, Anthropic's thinking budget or `output_config.effort`) even when the agent asked none. A member fixed at an effort keeps its own; otherwise the rule's beats the model's suffix, the turn's pick and the agent's pick, and isn't bounded by the group's levels. A model whose levels aren't known (`Efforts` empty) is asked as the agent asked, as with the auto verdict. The members the group fails over to are asked as the agent asked. A Gemini-protocol client's request is left as it came, as for every effort magpie sets. `cleanRules` refuses a value outside `provider.Efforts` and a pause with one. The try records it (`Try.RuleEffort`), and the Routing page says the rule set it. `rule_effort_test.go`, `grouprule_useeffort_test.go`, `routing-rule-effort.test.cjs`.
- A rule by folder (`Rule.Folders`, `folders=~/work/acme,~/oss`; Cario0x on X: routing by the project being worked on) matches when the folder the agent works in is one of them or inside one (`provider.FolderUnder`, [`rulefolder.go`](../../internal/provider/rulefolder.go)): at a path's boundary, so `/a/b` is not under `/a/bc`; `~` is the home folder; a Windows drive is one with its WSL mount (`C:\work` is `/mnt/c/work`) and a `\\wsl$\<distro>` or `\\wsl.localhost\<distro>` share is the distro's own path; case doesn't count on macOS and Windows, nor ever for a drive. A rule outside folders (`Rule.ExceptFolders`, `except-folders=~/big-task`; #1606, Ti800) matches when the folder is in none of them, and an agent that says none isn't known to be in one, so it matches; a rule may have both (`Rule.ByFolder`, `inFolders`). The folder is read from the request only when a rule of the group has folders or except-folders (`workDir` in [`workdir.go`](../../internal/gateway/workdir.go)), in each agent's own words, taken from captured requests: Claude Code's ` - Primary working directory: <path>` (its `<`/`>` as entities; ` (was <old>)` after a change dropped), Codex's `<environment_context>` `<cwd>`, Pi's `<cwd>` in the system prompt, and OpenCode's `Working directory:` inside its `<env>` block. Only the system prompt and the user's and system messages' text count, never the model's words or a tool's result, and the last said wins. An agent that says none has no folder, and no `folders=` rule matches it (unknown, never empty). The same reading tells a plugin's fetch where its agent works ([provider-plugins](provider-plugins.md#where-the-agent-works-for-a-plugins-fetch)). A pause rule may hold by folder too (below). The trace's `RuleHit.Folder` is the folder read, and the Routing page tells it beside the rule's `folder …` condition. `TestFolderUnder`, `TestRuleMatchesByFolder`, `TestParseRuleFolders`, `TestWorkDirOfEachAgent`, `TestRuleByFolder`, `TestRuleMatchesExceptFolders`, `routing-folder.test.cjs`. Rules by folder are never in a shared group (`ShareGroup` drops every `ByFolder` rule).
- A rule by agent (`Rule.Agents`, `agents=codex,sillytavern`) matches the agent the gateway names the request's (`agentOf` in [`gateway.go`](../../internal/gateway/gateway.go): its `magpie-<name>` key, else its User-Agent), in lower case (`cleanRules`, `MatchesBesidesIntent`, `Pauses`). A client magpie doesn't know is named by its User-Agent's product name, before the first `/` or space (`usage.AgentOf`), so a rule can hold SillyTavern or one's own app (raikyou on Discord). The editor's agent picker lists the agents magpie knows, then those `/api/usage?period=30d` heard from that it doesn't (not `other`, nor `magpie`), and takes a name typed in its filter as the gateway reads a User-Agent (`uaName` in [`plugins/routing/client.js`](../../internal/gui/assets/plugins/routing/client.js), through the picker's `field.typed`): "MyApp/2.0 (Linux)" is `myapp`. `routing-rule-agent-typed.test.cjs`.
diff --git a/internal/cli/groups_rules_cli.go b/internal/cli/groups_rules_cli.go
index 87b6c5199..bd70b7ac2 100644
--- a/internal/cli/groups_rules_cli.go
+++ b/internal/cli/groups_rules_cli.go
@@ -20,6 +20,8 @@ const ruleUsage = `usage:
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
@@ -193,20 +195,34 @@ 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(" → ") + r.Use + muted.Render(" at ") + r.UseEffort
+ return strings.Join(r.Conditions(), " · ") + muted.Render(" → ") + uses + muted.Render(" at ") + r.UseEffort
}
- return strings.Join(r.Conditions(), " · ") + muted.Render(" → ") + r.Use
+ 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)
- } else {
- fmt.Println(amber.Render("!"), fmt.Sprintf("rule %d is removed with %s", i+1, r.Use))
}
}
g.Rules = keep
diff --git a/internal/cli/groups_rules_cli_test.go b/internal/cli/groups_rules_cli_test.go
index 6ec48dc33..6ff998551 100644
--- a/internal/cli/groups_rules_cli_test.go
+++ b/internal/cli/groups_rules_cli_test.go
@@ -186,3 +186,44 @@ func TestGroupMemberNames(t *testing.T) {
t.Errorf("gone: %v", err)
}
}
+
+// A rule naming several models (monkeyin_92 on X): use=a,b, in order; a
+// model dropped from the group leaves the rule, which goes with its last.
+func TestGroupRuleSeveralModelsCmd(t *testing.T) {
+ groupsHome(t)
+ if _, err := addGroup("Opus", []string{"models=a/claude-opus-5-5,b/gpt-5.5,a/only-a"}); err != nil {
+ t.Fatal(err)
+ }
+ rule := func() []provider.Rule {
+ t.Helper()
+ g, err := findGroup("opus")
+ if err != nil {
+ t.Fatal(err)
+ }
+ return g.Rules
+ }
+ if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5,only-a,claude-opus-5-5", "tokens=200k"}); err != nil {
+ t.Fatal(err)
+ }
+ if rs := rule(); len(rs) != 1 || strings.Join(rs[0].Uses(), " ") != "b/gpt-5.5 a/only-a a/claude-opus-5-5" {
+ t.Fatalf("%+v", rs)
+ }
+ if err := ruleCmd([]string{"opus"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5,a/m", "tokens=5"}); err == nil {
+ t.Fatal("a model not in the group taken")
+ }
+ if _, err := setGroup("opus", []string{"models-=b/gpt-5.5"}); err != nil {
+ t.Fatal(err)
+ }
+ if rs := rule(); len(rs) != 1 || strings.Join(rs[0].Uses(), " ") != "a/only-a a/claude-opus-5-5" || rs[0].Tokens != 200000 {
+ t.Fatalf("after models-=b: %+v", rs)
+ }
+ if _, err := setGroup("opus", []string{"models-=a/only-a,a/claude-opus-5-5", "models+=b/gpt-5.5"}); err != nil {
+ t.Fatal(err)
+ }
+ if rs := rule(); len(rs) != 0 {
+ t.Fatalf("a rule left with no model kept: %+v", rs)
+ }
+}
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 624aeb076..efbcccd88 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -2220,8 +2220,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// the groups in the group as the rule's has them: a model it shares
// with a group ahead of it is its, for its rules (#2361)
ruled := ms
- if hit != nil && strings.HasPrefix(hit.Use, provider.GroupPrefix) {
- ruled = homed(ms, []string{hit.Use})
+ if led := ruleLed(hit, ms, cands); strings.HasPrefix(led, provider.GroupPrefix) {
+ ruled = homed(ms, []string{led})
}
// the rules of the groups in the group, down the one that goes first
var nested []NestedRule
diff --git a/internal/gateway/rules.go b/internal/gateway/rules.go
index e29d548ed..c6356bba4 100644
--- a/internal/gateway/rules.go
+++ b/internal/gateway/rules.go
@@ -25,8 +25,11 @@ var ruleClock = time.Now
// RuleHit is what the trace tells of a group's rules for a request.
type RuleHit struct {
- N int `json:"n"` // the rule, from 1; 0 when none matched
- Use string `json:"use,omitempty"` // the member it puts first
+ N int `json:"n"` // the rule, from 1; 0 when none matched
+ Use string `json:"use,omitempty"` // the member it puts first
+ // More: the members the rule names after Use (provider.Rule.More), in
+ // order: they go next, before Then
+ More []string `json:"more,omitempty"`
When []string `json:"when,omitempty"`
Held bool `json:"held,omitempty"` // decided when the turn began
// Waits: the turn began before magpie saw it (or its rule was changed
@@ -200,7 +203,7 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a
// whatever put a member first, the rules after it that match too
// say who comes next
if hit != nil && hit.Use != "" && hit.N >= 1 {
- hit.Then = provider.ThenUses(g.Rules, q, hit.N, hit.Use)
+ hit.Then = provider.ThenUses(g.Rules, q, hit.N, hit.uses())
if hit.N <= len(g.Rules) {
hit.UseEffort = g.Rules[hit.N-1].UseEffort // and how hard it asks it to think
}
@@ -220,11 +223,22 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a
if !r.Matches(q) {
continue
}
- if c := ctx[r.Use]; c > 0 && c < q.Tokens {
- hit.Small = append(hit.Small, r.Use)
+ // of the models the rule names, those that take it, in order
+ var fit []string
+ for _, u := range r.Uses() {
+ if c := ctx[u]; c > 0 && c < q.Tokens {
+ hit.Small = append(hit.Small, u)
+ continue
+ }
+ fit = append(fit, u)
+ }
+ if len(fit) == 0 {
continue
}
- hit.N, hit.Use, hit.When = i+1, r.Use, r.Conditions()
+ hit.N, hit.Use, hit.When = i+1, fit[0], r.Conditions()
+ if len(fit) > 1 {
+ hit.More = fit[1:]
+ }
break
}
return hit
@@ -244,7 +258,7 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a
case tr.use == "":
hit.Held = true
case tr.n >= 1 && tr.n <= len(g.Rules) && g.Rules[tr.n-1].Use == tr.use:
- hit.Held, hit.N, hit.Use, hit.When = true, tr.n, tr.use, g.Rules[tr.n-1].Conditions()
+ hit.Held, hit.N, hit.Use, hit.More, hit.When = true, tr.n, tr.use, g.Rules[tr.n-1].More, g.Rules[tr.n-1].Conditions()
default:
hit.Waits = true
}
@@ -320,7 +334,7 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a
}
}
if i := provider.MatchRule(g.Rules, q); i >= 0 {
- hit.N, hit.Use, hit.When = i+1, g.Rules[i].Use, g.Rules[i].Conditions()
+ hit.N, hit.Use, hit.More, hit.When = i+1, g.Rules[i].Use, g.Rules[i].More, g.Rules[i].Conditions()
}
turnRules.Lock()
defer turnRules.Unlock()
@@ -363,7 +377,7 @@ func outgrown(g provider.Group, ctx map[string]int, held *RuleHit, q provider.Ru
}
out := *held
out.Held, out.Waits, out.Grown = false, false, true
- out.N, out.Use, out.When = i+1, g.Rules[i].Use, g.Rules[i].Conditions()
+ out.N, out.Use, out.More, out.When = i+1, g.Rules[i].Use, g.Rules[i].More, g.Rules[i].Conditions()
return &out, true
}
@@ -485,9 +499,9 @@ func (s *Server) nestedRules(at string, req *Request, agent string, ask classifi
continue
}
cands, pl = ruleOver(h, subMs, cands, pl, aff)
- if strings.HasPrefix(h.Use, provider.GroupPrefix) {
+ if led := ruleLed(h, subMs, cands); strings.HasPrefix(led, provider.GroupPrefix) {
// further down, the way the group it chose has its models
- ms = homed(ms, append(slices.Clone(lead.Path[:depth+1]), h.Use))
+ ms = homed(ms, append(slices.Clone(lead.Path[:depth+1]), led))
}
}
return out, keys, cands, pl
@@ -519,25 +533,51 @@ func membersNamed(id string, ms []provider.Member) []provider.Member {
return out
}
-// ruleMembers are the models of the member a rule names, among those
-// ready now: the model, or a group in the group's.
-func ruleMembers(hit *RuleHit, ms []provider.Member) []provider.Member {
- if hit == nil || hit.Use == "" {
+// uses are the members the rule names, in order: Use, then More.
+func (h *RuleHit) uses() []string {
+ if h == nil || h.Use == "" {
return nil
}
- return membersNamed(hit.Use, ms)
+ return append([]string{h.Use}, h.More...)
+}
+
+// ruleMembers are the models of the members a rule names, among those
+// ready now, in the rule's order: each a model, or a group in the group's.
+// A model two of them have is the first's.
+func ruleMembers(hit *RuleHit, ms []provider.Member) []provider.Member {
+ var out []provider.Member
+ for _, id := range hit.uses() {
+ for _, m := range membersNamed(id, ms) {
+ if !slices.ContainsFunc(out, func(o provider.Member) bool { return o.At == m.At }) {
+ out = append(out, m)
+ }
+ }
+ }
+ return out
+}
+
+// ruleLed is the member of the rule's (Use or one of More) the one going
+// first is of: Use when it is none of them.
+func ruleLed(hit *RuleHit, ms []provider.Member, cs []candidate) string {
+ if hit == nil || len(cs) == 0 {
+ return ""
+ }
+ for _, id := range hit.uses() {
+ if slices.ContainsFunc(membersNamed(id, ms), func(m provider.Member) bool { return ofMember(cs[0], m) }) {
+ return id
+ }
+ }
+ return hit.Use
}
// homeAt are the places (At) of the rule's models met first in the
-// member it names, not shared with a group ahead of it: only those were
-// planned in that member's own routing (#2407, #2361).
+// members it names, not shared with a group ahead of them: only those
+// were planned in that member's own routing (#2407, #2361).
func homeAt(hit *RuleHit, ms []provider.Member) map[int]bool {
out := map[int]bool{}
- if hit == nil || hit.Use == "" {
- return out
- }
+ uses := hit.uses()
for _, m := range ms {
- if m.ID == hit.Use {
+ if slices.Contains(uses, m.ID) {
out[m.At] = true
}
}
@@ -564,14 +604,19 @@ func homed(ms []provider.Member, path []string) []provider.Member {
// rule names itself: one a group in the group has too goes there, and that
// group's rules don't pick another over it (#625).
func modelRuled(hit *RuleHit, ms []provider.Member, cs []candidate) bool {
- if hit == nil || strings.HasPrefix(hit.Use, provider.GroupPrefix) || len(cs) == 0 {
+ if hit == nil || len(cs) == 0 {
return false
}
- return slices.ContainsFunc(ruleMembers(hit, ms), func(m provider.Member) bool { return ofMember(cs[0], m) })
+ led := ruleLed(hit, ms, cs)
+ if led == "" || strings.HasPrefix(led, provider.GroupPrefix) {
+ return false
+ }
+ return slices.ContainsFunc(membersNamed(led, ms), func(m provider.Member) bool { return ofMember(cs[0], m) })
}
// ruleOrder is who a rule's request goes to before the group's others:
-// the member it names, then those the rules after it that match too name.
+// the members it names, in order, then those the rules after it that
+// match too name.
func ruleOrder(hit *RuleHit, ms []provider.Member) []provider.Member {
out := ruleMembers(hit, ms)
if hit == nil || hit.Use == "" {
@@ -657,17 +702,15 @@ func ruleOver(hit *RuleHit, ms []provider.Member, cs []candidate, pl planned, af
kept := aff != nil && aff.Kept
if hit.Held && kept && len(cs) > 1 {
ordered := ruleOrder(hit, ms)
- place := func(i int) int { // which of the rule's members cs[i] is of: 0 its own, then the Then's
+ ids := append(hit.uses(), hit.Then...)
+ place := func(i int) int { // which of the rule's members cs[i] is of: Use 0, then More's, then the Then's
for _, m := range ordered {
if !ofMember(cs[i], m) {
continue
}
- if named(m, hit.Use) {
- return 0
- }
- for j, id := range hit.Then {
+ for j, id := range ids {
if named(m, id) {
- return j + 1
+ return j
}
}
}
@@ -687,9 +730,10 @@ func ruleOver(hit *RuleHit, ms []provider.Member, cs []candidate, pl planned, af
cs = append([]candidate{cs[0]}, rest...)
pl.order = append([]Weighed{pl.order[0]}, sub.order...)
if p >= 0 {
- hit.Unready = p > 0
+ // one of More is the rule's own: ready, though not Use
+ hit.Unready = p > len(hit.More)
if p > 0 {
- hit.Instead = hit.Then[p-1]
+ hit.Instead = ids[p]
}
}
return cs, pl
@@ -739,19 +783,23 @@ func homeCands(cs []candidate, ms []provider.Member) {
}
// applyRule puts a rule's members first (ruleOrder) and says in the hit
-// whether the one it names leads; it reports whether any of them was ready.
+// whether the one it names first leads, and who does when it doesn't; it
+// reports whether any of them was ready.
func applyRule(hit *RuleHit, ms []provider.Member, cs []candidate, pl planned) ([]candidate, planned, bool) {
ordered := ruleOrder(hit, ms)
+ own := len(ruleMembers(hit, ms))
lead := -1
if len(ordered) > 0 && len(cs) > 0 {
- cs, pl, lead = ruleFirst(ordered, cs, pl, len(ruleMembers(hit, ms)), homeAt(hit, ms))
+ cs, pl, lead = ruleFirst(ordered, cs, pl, own, homeAt(hit, ms))
}
- // ordered has the rule's own first, then those of the rules after it
- hit.Unready = lead < 0 || lead >= len(ruleMembers(hit, ms))
- if lead >= 0 && hit.Unready {
+ // ordered has the rule's own first (Use's, then More's), then those
+ // of the rules after it
+ hit.Unready = lead < 0 || lead >= own
+ if lead >= 0 && !named(ordered[lead], hit.Use) {
+ next := append(slices.Clone(hit.More), hit.Then...)
hit.Instead = ordered[lead].ID
- if i := slices.IndexFunc(hit.Then, func(id string) bool { return named(ordered[lead], id) }); i >= 0 {
- hit.Instead = hit.Then[i]
+ if i := slices.IndexFunc(next, func(id string) bool { return named(ordered[lead], id) }); i >= 0 {
+ hit.Instead = next[i]
}
}
return cs, pl, lead >= 0
diff --git a/internal/gateway/rules_more_test.go b/internal/gateway/rules_more_test.go
new file mode 100644
index 000000000..8eacd6d25
--- /dev/null
+++ b/internal/gateway/rules_more_test.go
@@ -0,0 +1,103 @@
+package gateway
+
+import (
+ "net/http/httptest"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/catalog"
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// withFlash adds c/flash (128k, images) to ruled's providers and gives
+// the group the members and rules given.
+func withFlash(t *testing.T, members []string, rules ...provider.Rule) *ruleUp {
+ t.Helper()
+ c := &ruleUp{key: "kc"}
+ srv := httptest.NewServer(c)
+ t.Cleanup(srv.Close)
+ yes := true
+ if err := provider.Save(provider.Provider{ID: "c", Name: "C", Key: "kc", Models: []string{"flash"}, Chat: srv.URL + "/v1"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := catalog.SaveLive("c", srv.URL+"/v1", []catalog.Model{{ID: "flash", Context: 128000, Images: true, ImageInput: &yes}}); err != nil {
+ t.Fatal(err)
+ }
+ setMembers(t, members, rules...)
+ return c
+}
+
+// A rule naming several models (monkeyin_92 on X): the first goes first,
+// and when it fails or rests the rule's next one takes the request, ahead
+// of the group's own first member; the trace says the rule's next went
+// first, the rule's member being ready. The turn's tool rounds stay on it.
+func TestRuleSeveralModelsInOrder(t *testing.T) {
+ s, a, b := ruled(t)
+ c := withFlash(t, []string{"a/small", "b/big", "c/flash"}, provider.Rule{Use: "b/big", More: []string{"c/flash"}, Tokens: 20000})
+ b.mu.Lock()
+ b.fail = 429
+ b.mu.Unlock()
+ code, out := postAs(t, s, "one", chat(long(25000), nil, 0, ""))
+ r := lastRoute(s)
+ if code != 200 || !strings.Contains(out, "from kc") || r.Rule.N != 1 || !slices.Equal(r.Rule.More, []string{"c/flash"}) || len(r.Tries) != 2 {
+ t.Fatalf("b failing: %d %s %+v %d", code, out, r.Rule, len(r.Tries))
+ }
+ b.mu.Lock()
+ b.fail = 0
+ b.mu.Unlock()
+ code, out = postAs(t, s, "two", chat(long(25000), nil, 0, ""))
+ r = lastRoute(s)
+ if code != 200 || !strings.Contains(out, "from kc") || r.Rule.Unready || r.Rule.Instead != "c/flash" || len(r.Tries) != 1 {
+ t.Fatalf("b resting: %d %s %+v %d", code, out, r.Rule, len(r.Tries))
+ }
+ code, out = postAs(t, s, "two", chat(long(25000), nil, 1, ""))
+ r = lastRoute(s)
+ if code != 200 || !strings.Contains(out, "from kc") || !r.Rule.Held || r.Rule.Unready || r.Rule.Instead != "c/flash" {
+ t.Fatalf("tool round: %d %s %+v", code, out, r.Rule)
+ }
+ if a.n() != 0 || b.n() != 1 || c.n() != 3 {
+ t.Fatalf("a %d b %d c %d", a.n(), b.n(), c.n())
+ }
+
+ // the rule's models before the later rules' and the group's others
+ setMembers(t, []string{"a/small", "c/flash", "b/big"}, provider.Rule{Use: "b/big", More: []string{"a/small"}, Tokens: 20000},
+ provider.Rule{Use: "c/flash", Tokens: 10000})
+ _, r = postOK(t, s, "three", chat(long(25000), nil, 0, ""))
+ if !slices.Equal(r.Rule.Then, []string{"c/flash"}) || len(r.Tries) != 1 || !strings.HasPrefix(r.Tries[0].ID, "a") || r.Rule.Instead != "a/small" {
+ t.Fatalf("order: %+v %+v", r.Rule, r.Tries)
+ }
+}
+
+// The rule's useEffort is asked of each model it names, the next one too.
+func TestRuleEffortReachesTheRulesNextModel(t *testing.T) {
+ s, a, b := ruled(t)
+ if err := provider.SetModelEfforts("a/small", []string{"low", "medium", "high"}); err != nil {
+ t.Fatal(err)
+ }
+ setMembers(t, []string{"b/big", "a/small"}, provider.Rule{Use: "b/big", More: []string{"a/small"}, Tokens: 1000, UseEffort: "low"})
+ b.mu.Lock()
+ b.fail = 500
+ b.mu.Unlock()
+ out, r := postOK(t, s, "", chat(long(2000), nil, 0, `,"reasoning_effort":"high"`))
+ if sent := sentBody(t, a); !strings.Contains(out, "from ka") || sent["reasoning_effort"] != "low" {
+ t.Fatalf("sent %v (%s)", sent, out)
+ }
+ if n := len(r.Tries); n != 2 || r.Tries[1].RuleEffort != "low" {
+ t.Fatalf("traced %+v", r.Tries)
+ }
+}
+
+// A compaction longer than a rule's first model takes goes to the rule's
+// next one that takes it, by the same rule.
+func TestRuleCompactPassesOverTheRulesSmallModel(t *testing.T) {
+ s, _, _ := ruled(t, provider.Rule{Use: "a/small", More: []string{"b/big"}, Compact: true})
+ ccSystem := `{"role":"system","content":"You are a helpful AI assistant tasked with summarizing conversations."},`
+ body := `{"model":"group/r","messages":[` + ccSystem + `{"role":"user","content":` + quote(long(70000)) + `},{"role":"assistant","content":"done"},` +
+ `{"role":"user","content":"Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests."}]}`
+ code, out := postAs(t, s, "big", body)
+ r := lastRoute(s)
+ if code != 200 || !strings.Contains(out, "from kb") || !r.Rule.Compact || r.Rule.N != 1 || r.Rule.Use != "b/big" || !slices.Equal(r.Rule.Small, []string{"a/small"}) {
+ t.Fatalf("%d %s %+v", code, out, r.Rule)
+ }
+}
diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js
index b608c5596..3edd7a303 100644
--- a/internal/gui/assets/plugins/routing/client.js
+++ b/internal/gui/assets/plugins/routing/client.js
@@ -810,7 +810,7 @@ function routingPage(part) {
case "rule": {
// the group's rule, or that of a group in it, that moved it
const x = r.nested?.findLast((n) => n.rule?.use && !n.rule.unready && !n.rule.held)?.rule || r.rule;
- return t("{who} answered this conversation last, but a new turn begins and the group's rule {n} puts {use} first.", { who: last, n: x?.n, use: useName(r, x?.use) });
+ return t("{who} answered this conversation last, but a new turn begins and the group's rule {n} puts {use} first.", { who: last, n: x?.n, use: useName(r, x?.instead && !x.unready ? x.instead : x?.use) });
}
}
return null;
@@ -825,11 +825,16 @@ function routingPage(part) {
// the folder the agent said it works in, when a rule asks for one
const dir = x.folder ? " " + t("The agent works in {folder}.", { folder: x.folder }) : "";
if (x.use && !x.unready) {
+ // the rule's first had none to take it, and its next went first
+ const first = x.use;
+ if (x.instead) x.use = useName(r, x.instead);
const held = t("Rule {n} ({when}) sent turn {turn} to {use} as it began; the turn stays with whoever took it then.", { n: x.n, when, turn: x.turn, use: x.use });
if (x.compact) return lead ? t("The agent is compacting the conversation, so rule {n} sends the summary to {use}; the conversation stays on the model it was on.", { n: x.n, use: x.use }) + (x.small?.length ? " " + t("Passed over, as the conversation is longer than they take: {models}.", { models: x.small.map((id) => useName(r, id)).join(", ") }) : "") : null;
if (x.held) return lead || affWhy(r, true) ? held : null;
if (x.grown) return lead ? t("Within turn {turn} the conversation grew to about {tokens} tokens, more than the model it was on takes, so rule {n} ({when}) moves it to {use}.", { turn: x.turn, tokens: tokens(x.tokens), n: x.n, when, use: x.use }) : null;
if (!lead) return null;
+ if (x.instead) return t("Rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first.", { n: x.n, use: first, instead: x.use }) + dir;
+ if (x.more?.length) return t("Turn {turn} begins and rule {n} matches — {when} — so {use} goes first, then {more}, which the rule names after it; the later rules' and the group's others stay behind them.", { turn: x.turn, n: x.n, when, use: x.use, more: x.more.map((id) => useName(r, id)).join(", ") }) + dir;
if (x.then?.length) return t("Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; if it fails, {then}, which the rules after it that match too name, then the group's others.", { turn: x.turn, n: x.n, when, use: x.use, then: x.then.map((id) => useName(r, id)).join(", ") }) + dir;
return t("Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; the group's others stay behind it if it fails.", { turn: x.turn, n: x.n, when, use: x.use }) + dir;
}
@@ -857,6 +862,11 @@ function routingPage(part) {
});
return out + "]".repeat(open.length);
}
+ // rUses is the models a rule sends to, in order: its use, then its
+ // more; setUses writes them back, each once, the first as its use (a
+ // rule of one model has no more, as one saved before it)
+ const rUses = (r) => [r.use, ...(r.more || [])].filter(Boolean);
+ const setUses = (r, ids) => { const u = [...new Set(ids.filter(Boolean))]; r.use = u[0] || ""; if (u.length > 1) r.more = u.slice(1); else delete r.more; };
// useName is what a rule sends to: a model, or a group in the group
// a group magpie found goes by the id agents pick it by (auto/<model>), not its internal group/auto-…
const useName = (r, id) => {
@@ -872,9 +882,10 @@ function routingPage(part) {
const x = n.rule, g = n.name || n.group;
if (!x) continue;
const when = (x.when || []).map(condText).join(", ");
- if (x.use && x.instead) out.push(t("In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.", { group: g, n: x.n, use: x.use, instead: x.instead }));
+ if (x.use && x.instead && !x.unready && !x.held) out.push(t("In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first there.", { group: g, n: x.n, use: x.use, instead: x.instead }));
+ else if (x.use && x.instead && x.unready) out.push(t("In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.", { group: g, n: x.n, use: x.use, instead: x.instead }));
else if (x.use && x.unready) out.push(t("In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {group}'s order stands.", { group: g, n: x.n, use: x.use }));
- else if (x.use && x.held) out.push(t("In {group}, rule {n} ({when}) sent turn {turn} to {use} as it began; the turn stays with whoever took it then.", { group: g, n: x.n, when, turn: x.turn, use: x.use }));
+ else if (x.use && x.held) out.push(t("In {group}, rule {n} ({when}) sent turn {turn} to {use} as it began; the turn stays with whoever took it then.", { group: g, n: x.n, when, turn: x.turn, use: x.instead || x.use }));
else if (x.use) out.push(t("In {group}, rule {n} matches — {when} — so {use} goes first there.", { group: g, n: x.n, when, use: x.use }));
else if (!x.waits) out.push(t("In {group}, no rule matches.", { group: g }));
}
@@ -4190,7 +4201,7 @@ function routingPage(part) {
// there, rather than leaving the slot empty.
const openGroupEditor = async (g) => {
if (groupDirty() && !(await confirmDiscard())) return false;
- gEdit = { id: g.id, draft: { name: g.name, members: [...g.members], match: [...(g.match || [])], matched: [...(g.matched || [])], fast: [...(g.fast || [])], off: [...(g.off || [])], weights: { ...(g.weights || {}) }, routing: g.routing || "", pick: g.pick || "", affinity: g.affinity || "", sink: !!g.sink, unlists: !!g.unlists, firstToken: g.firstToken || 0, patience: patienceOf(g.patience), context: g.context || 0, classifier: g.classifier || "", effort: g.effort || "", levels: [...(g.levels || [])], rules: (g.rules || []).map((r) => ({ ...r, intent: r.intent || "", agents: [...(r.agents || [])], folders: [...(r.folders || [])], exceptFolders: [...(r.exceptFolders || [])], time: r.time ? { ...r.time, days: [...(r.time.days || [])] } : null })) } };
+ gEdit = { id: g.id, draft: { name: g.name, members: [...g.members], match: [...(g.match || [])], matched: [...(g.matched || [])], fast: [...(g.fast || [])], off: [...(g.off || [])], weights: { ...(g.weights || {}) }, routing: g.routing || "", pick: g.pick || "", affinity: g.affinity || "", sink: !!g.sink, unlists: !!g.unlists, firstToken: g.firstToken || 0, patience: patienceOf(g.patience), context: g.context || 0, classifier: g.classifier || "", effort: g.effort || "", levels: [...(g.levels || [])], rules: (g.rules || []).map((r) => ({ ...r, ...(r.more?.length ? { more: [...r.more] } : {}), intent: r.intent || "", agents: [...(r.agents || [])], folders: [...(r.folders || [])], exceptFolders: [...(r.exceptFolders || [])], time: r.time ? { ...r.time, days: [...(r.time.days || [])] } : null })) } };
renderGroups();
return true;
};
@@ -4547,7 +4558,7 @@ function routingPage(part) {
if (groupDirty() && !(await confirmDiscard())) return;
const g = tp.group;
gSel = null;
- gEdit = { id: "", draft: { name: tplName(tp), slug: g.id, members: [...g.members], match: [], matched: [], fast: [], off: [], routing: g.routing || "", affinity: "", rules: (g.rules || []).map((r) => ({ ...r, intent: r.intent || "", agents: [...(r.agents || [])], folders: [], exceptFolders: [], time: null })), classifier: g.classifier || "", effort: "", levels: [] } };
+ gEdit = { id: "", draft: { name: tplName(tp), slug: g.id, members: [...g.members], match: [], matched: [], fast: [], off: [], routing: g.routing || "", affinity: "", rules: (g.rules || []).map((r) => ({ ...r, ...(r.more?.length ? { more: [...r.more] } : {}), intent: r.intent || "", agents: [...(r.agents || [])], folders: [], exceptFolders: [], time: null })), classifier: g.classifier || "", effort: "", levels: [] } };
renderGroups();
const ed = gList.querySelector(".rt-gedit");
if (ed && window.scrollOnPurpose?.(ev)) ed.scrollIntoView({ block: "start", behavior: "smooth" });
@@ -4762,7 +4773,7 @@ function routingPage(part) {
if (g.firstToken > 0 && !manual) tags.append(el("span", "tag", t("Next after {n} without a first token", { n: took(g.firstToken * 1000) })));
if (g.rules?.length) {
const r = el("span", "tag" + (manual ? " idle" : ""), t(g.rules.length === 1 ? "1 rule" : "{n} rules", { n: g.rules.length }));
- r.title = (manual ? t("The rules wait while you pick the model by hand.") + "\n" : "") + g.rules.map((x, i) => `${i + 1}. ${ruleText(x)} → ${x.pause ? t("pause") + " " : ""}${memberLabel(g, x.use)}${x.useEffort ? " · " + t("{level} reasoning", { level: x.useEffort }) : ""}`).join("\n");
+ r.title = (manual ? t("The rules wait while you pick the model by hand.") + "\n" : "") + g.rules.map((x, i) => `${i + 1}. ${ruleText(x)} → ${x.pause ? t("pause") + " " : ""}${rUses(x).map((u) => memberLabel(g, u)).join(", ")}${x.useEffort ? " · " + t("{level} reasoning", { level: x.useEffort }) : ""}`).join("\n");
tags.append(r);
}
if (g.disabled) tags.append(el("span", "tag idle", t("switched off")));
@@ -5175,7 +5186,7 @@ function routingPage(part) {
if (d.members.includes(to)) { status(t("{name} at that reasoning is in the group already", { name: memberName(id) }), "err"); return; }
d.members[i] = to;
d.matched = d.matched.filter((x) => x !== id); // named now, at its own reasoning
- for (const r of d.rules) if (r.use === id) r.use = to;
+ for (const r of d.rules) setUses(r, rUses(r).map((x) => x === id ? to : x));
d.fast = d.fast.map((x) => x === id ? to : x);
d.off = d.off.map((x) => x === id ? to : x);
if (d.weights[id]) { d.weights[to] = d.weights[id]; delete d.weights[id]; }
@@ -5282,7 +5293,8 @@ function routingPage(part) {
const named = d.members.filter((x) => !d.matched.includes(x));
d.matched = matchedBy(d.match, named);
d.members = [...named, ...d.matched];
- d.rules = d.rules.filter((r) => d.members.includes(r.use));
+ // a rule keeps the models still here; it goes with its last
+ d.rules = d.rules.filter((r) => { setUses(r, rUses(r).filter((x) => d.members.includes(x))); return !!r.use; });
d.fast = d.fast.filter((x) => d.members.includes(x));
d.off = d.off.filter((x) => d.members.includes(x));
}
@@ -5756,22 +5768,61 @@ function routingPage(part) {
then.append(el("span", "rt-arrow", "→"));
then.lastChild.setAttribute("aria-hidden", "true");
// the member it sends to
- const ub = el("button", "rt-cond on rt-model");
- const drawUse = () => { const note = memberNote(r.use); ub.replaceChildren(memberIcon(r.use), el("span", "", note ? `${memberName(r.use)} · ${note}` : r.use)); };
+ // the members it sends to, in order (monkeyin_92 on X: one rule,
+ // several models, not a rule each): the first goes first, the
+ // next when it fails or rests, then the later rules' and the
+ // group's others. A pause names one.
+ const ub = el("span", "rt-line rt-uses");
+ const drawUse = () => {
+ const uses = rUses(r);
+ ub.replaceChildren();
+ uses.forEach((u, k) => {
+ const b = el("button", "rt-cond on rt-model");
+ b.type = "button";
+ const note = memberNote(u);
+ b.append(memberIcon(u), el("span", "", note ? `${memberName(u)} · ${note}` : u));
+ // another of the group's in its place, none the rule names twice
+ b.onclick = (ev) => pickFrom("member", b, ev, d.members.filter((id) => id === u || !uses.includes(id)).map((id) => { const s = subOf(id); return { value: id, label: memberName(id), note: memberNote(id), icon: s ? undefined : modelOf(id)?.icon, icons: s ? groupIcons(s) : undefined }; }),
+ (v) => { const next = rUses(r); next[k] = v; setUses(r, next); drawUse(); warn(); }, u);
+ if (uses.length < 2) { ub.append(b); return; }
+ const w = el("span", "rt-c");
+ const x = el("button", "rt-cx");
+ x.type = "button";
+ x.title = t("Remove {name} from this rule", { name: memberName(u) });
+ x.setAttribute("aria-label", x.title);
+ x.append(svg(CROSS, 8, 2));
+ x.onclick = (e) => { e.stopPropagation(); setUses(r, uses.filter((_, j) => j !== k)); drawUse(); warn(); ub.querySelector(".rt-model")?.focus({ preventScroll: true }); };
+ w.append(b, x);
+ ub.append(w);
+ });
+ const left = d.members.filter((id) => !uses.includes(id));
+ if (r.pause || !left.length) return;
+ const add = el("button", "rt-adduse");
+ add.type = "button";
+ add.setAttribute("aria-haspopup", "menu");
+ add.title = t("Add a model it goes to next");
+ add.setAttribute("aria-label", add.title);
+ add.append(svg(PLUS, 11, 1.8));
+ add.onclick = (ev) => pickFrom("member", add, ev, left.map((id) => { const s = subOf(id); return { value: id, label: memberName(id), note: memberNote(id), icon: s ? undefined : modelOf(id)?.icon, icons: s ? groupIcons(s) : undefined }; }),
+ (v) => { setUses(r, [...rUses(r), v]); drawUse(); warn(); ub.querySelector(".rt-adduse")?.focus({ preventScroll: true }); });
+ ub.append(add);
+ };
drawUse();
- ub.onclick = (ev) => pickFrom("member", ub, ev, d.members.map((id) => { const s = subOf(id); return { value: id, label: memberName(id), note: memberNote(id), icon: s ? undefined : modelOf(id)?.icon, icons: s ? groupIcons(s) : undefined }; }),
- (v) => { r.use = v; drawUse(); warn(); });
const hint = el("span", "rt-rwarn");
const need = el("div", "rt-rneed");
const warn = () => {
need.hidden = !(refused.has(r) && bareRule(r));
need.textContent = need.hidden ? "" : t(r.pause ? "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu" : "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu");
- const m = infoOf(r.use), bits = [];
+ const bits = [], uses = rUses(r);
if (r.pause) bits.push(t("left out of the group then: not tried, not a fallback"));
- if (!r.pause && r.images && m && m.ready && !m.images) bits.push(t("it doesn't take images"));
- if (!r.pause && r.tokens && m?.context && m.context < r.tokens) bits.push(t("it takes {n} tokens", { n: m.context.toLocaleString(intlLang()) }));
- // a summary longer than it takes goes by the next rule, or the group
- if (!r.pause && r.compact && m?.context && d.members.some((id) => (infoOf(id)?.context || 0) > m.context)) bits.push(t("longer conversations skip it: it takes {n} tokens", { n: m.context.toLocaleString(intlLang()) }));
+ // each model the rule names, by name when it names more than one
+ for (const u of r.pause ? [] : uses) {
+ const m = infoOf(u), of = (s) => bits.push(uses.length > 1 ? `${memberName(u)}: ${s}` : s);
+ if (r.images && m && m.ready && !m.images) of(t("it doesn't take images"));
+ if (r.tokens && m?.context && m.context < r.tokens) of(t("it takes {n} tokens", { n: m.context.toLocaleString(intlLang()) }));
+ // a summary longer than it takes goes by the rule's next, the next rule, or the group
+ if (r.compact && m?.context && d.members.some((id) => (infoOf(id)?.context || 0) > m.context)) of(t("longer conversations skip it: it takes {n} tokens", { n: m.context.toLocaleString(intlLang()) }));
+ }
const from = clockOf(r.time?.from), to = clockOf(r.time?.to);
if (from && to && to < from) bits.push(t("runs past midnight, into the next day"));
hint.textContent = bits.join(" · ");
@@ -5784,8 +5835,10 @@ function routingPage(part) {
// from there or from the rule's menu.
const setAct = (v) => {
r.pause = v === "pause";
- // what a pause doesn't take is cleared, not kept out of sight
+ // what a pause doesn't take is cleared, not kept out of sight;
+ // it pauses the rule's first model alone
if (r.pause) {
+ delete r.more;
Object.assign(r, { tokens: 0, images: false, effort: "", intent: "", compact: false, useEffort: "" });
for (const [k, , p] of COND_KINDS) if (!p) openedOf(r).delete(k);
}
diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js
index 6d9dfd738..0ab3d0cc2 100644
--- a/internal/gui/assets/plugins/routing/i18n.js
+++ b/internal/gui/assets/plugins/routing/i18n.js
@@ -299,6 +299,11 @@ magpie.strings({
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; if it fails, {then}, which the rules after it that match too name, then the group's others.": "第 {turn} 轮开始,命中规则 {n}({when}),{use} 排第一;失败时先换后续命中规则指定的 {then},再换组内其他模型。",
"Rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "规则 {n} 命中,但 {use} 没有能接这次请求的账号或 Key;后续命中的规则指定 {instead} 优先。",
"In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "在 {group} 中,规则 {n} 命中,但 {use} 没有能接这次请求的账号或 Key;后续命中的规则指定 {instead} 优先。",
+ "Remove {name} from this rule": "从这条规则移除 {name}",
+ "Add a model it goes to next": "添加下一个模型",
+ "Rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first.": "规则 {n} 命中,但 {use} 没有能接这次请求的账号或 Key,于是由这条规则的下一个模型 {instead} 排第一。",
+ "Turn {turn} begins and rule {n} matches — {when} — so {use} goes first, then {more}, which the rule names after it; the later rules' and the group's others stay behind them.": "第 {turn} 轮开始,命中规则 {n}({when}),{use} 排第一,其次是这条规则的 {more};之后才是后续规则和组内的其他成员。",
+ "In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first there.": "在 {group} 中,规则 {n} 命中,但 {use} 没有能接这次请求的账号或 Key,于是由这条规则的下一个模型 {instead} 在那里排第一。",
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; the group's others stay behind it if it fails.": "第 {turn} 轮开始,命中规则 {n}({when}),{use} 排第一;失败时由组内其他成员接手。",
"Rule {n} matches, but {use} has no account or key that can take this request, so the group's order stands.": "规则 {n} 命中,但 {use} 没有能接这次请求的账号或 Key,按组原本的顺序路由。",
"This turn began before magpie saw it, so the rules wait for the next one.": "本轮在 magpie 接收前已开始,规则从下一轮起判断。",
@@ -852,6 +857,11 @@ magpie.strings({
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; if it fails, {then}, which the rules after it that match too name, then the group's others.": "第 {turn} 輪開始,命中規則 {n}({when}),{use} 排第一;失敗時先換後續命中規則指定的 {then},再換組內其他模型。",
"Rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "規則 {n} 命中,但 {use} 沒有能接這次請求的帳號或 Key;後續命中的規則指定 {instead} 優先。",
"In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "在 {group} 中,規則 {n} 命中,但 {use} 沒有能接這次請求的帳號或 Key;後續命中的規則指定 {instead} 優先。",
+ "Remove {name} from this rule": "從這條規則移除 {name}",
+ "Add a model it goes to next": "新增下一個模型",
+ "Rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first.": "規則 {n} 命中,但 {use} 沒有能接這次請求的帳號或 Key,於是由這條規則的下一個模型 {instead} 排第一。",
+ "Turn {turn} begins and rule {n} matches — {when} — so {use} goes first, then {more}, which the rule names after it; the later rules' and the group's others stay behind them.": "第 {turn} 輪開始,命中規則 {n}({when}),{use} 排第一,其次是這條規則的 {more};之後才是後續規則和組內的其他成員。",
+ "In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first there.": "在 {group} 中,規則 {n} 命中,但 {use} 沒有能接這次請求的帳號或 Key,於是由這條規則的下一個模型 {instead} 在那裡排第一。",
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; the group's others stay behind it if it fails.": "第 {turn} 輪開始,命中規則 {n}({when}),{use} 排第一;失敗時由組內其他成員接手。",
"Rule {n} matches, but {use} has no account or key that can take this request, so the group's order stands.": "規則 {n} 命中,但 {use} 沒有能接這次請求的帳號或 Key,按組原本的順序路由。",
"This turn began before magpie saw it, so the rules wait for the next one.": "本輪在 magpie 接收前已開始,規則從下一輪起判斷。",
@@ -1403,6 +1413,11 @@ magpie.strings({
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; if it fails, {then}, which the rules after it that match too name, then the group's others.": "ターン {turn} が始まり、ルール {n} に一致しました({when})。そのため {use} が先頭です。失敗した場合は、同じく一致した後続のルールが指定する {then}、次にグループの他のメンバーに移ります。",
"Rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "ルール {n} に一致しましたが、{use} にはこのリクエストを受けられるアカウントやキーがないため、後に一致したルールが {instead} を先頭にします。",
"In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "{group} で、ルール {n} に一致しましたが、{use} にはこのリクエストを受けられるアカウントやキーがないため、後に一致したルールが {instead} を先頭にします。",
+ "Remove {name} from this rule": "このルールから {name} を外す",
+ "Add a model it goes to next": "次に使うモデルを追加",
+ "Rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first.": "ルール {n} に一致しましたが、{use} にはこのリクエストを受けられるアカウントやキーがないため、このルールの次のモデル {instead} が先頭です。",
+ "Turn {turn} begins and rule {n} matches — {when} — so {use} goes first, then {more}, which the rule names after it; the later rules' and the group's others stay behind them.": "ターン {turn} が始まり、ルール {n} に一致しました({when})。そのため {use} が先頭で、次にこのルールの {more} が続きます。後のルールとグループの他のメンバーはその後に控えます。",
+ "In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first there.": "{group} で、ルール {n} に一致しましたが、{use} にはこのリクエストを受けられるアカウントやキーがないため、このルールの次のモデル {instead} がそこで先頭です。",
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; the group's others stay behind it if it fails.": "ターン {turn} が始まり、ルール {n} に一致しました({when})。そのため {use} が先頭です。失敗した場合はグループの他のメンバーが後に控えます。",
"Rule {n} matches, but {use} has no account or key that can take this request, so the group's order stands.": "ルール {n} に一致しましたが、{use} にはこのリクエストを受けられるアカウントやキーがないため、グループの順番のままです。",
"This turn began before magpie saw it, so the rules wait for the next one.": "このターンは magpie が認識する前に始まったため、ルールは次のターンまで待ちます。",
@@ -1956,6 +1971,11 @@ magpie.strings({
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; if it fails, {then}, which the rules after it that match too name, then the group's others.": "Runde {turn} beginnt und Regel {n} trifft zu – {when} –, daher kommt {use} zuerst; schlägt es fehl, {then}, die von nachfolgenden ebenfalls zutreffenden Regeln genannt werden, dann die übrigen der Gruppe.",
"Rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "Regel {n} trifft zu, aber {use} hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher setzt eine spätere zutreffende Regel {instead} an die Spitze.",
"In {group}, rule {n} matches, but {use} has no account or key that can take this request, so a later matching rule puts {instead} first.": "In {group} trifft Regel {n} zu, aber {use} hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher setzt eine spätere zutreffende Regel {instead} an die Spitze.",
+ "Remove {name} from this rule": "{name} aus dieser Regel entfernen",
+ "Add a model it goes to next": "Ein Modell für danach hinzufügen",
+ "Rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first.": "Regel {n} trifft zu, aber {use} hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher kommt {instead}, das die Regel danach nennt, zuerst.",
+ "Turn {turn} begins and rule {n} matches — {when} — so {use} goes first, then {more}, which the rule names after it; the later rules' and the group's others stay behind them.": "Runde {turn} beginnt und Regel {n} trifft zu – {when} –, daher kommt {use} zuerst, dann {more}, das die Regel danach nennt; die späteren Regeln und die übrigen der Gruppe folgen dahinter.",
+ "In {group}, rule {n} matches, but {use} has no account or key that can take this request, so {instead}, which the rule names after it, goes first there.": "In {group} trifft Regel {n} zu, aber {use} hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher kommt dort {instead}, das die Regel danach nennt, zuerst.",
"Turn {turn} begins and rule {n} matches — {when} — so {use} goes first; the group's others stay behind it if it fails.": "Runde {turn} beginnt und Regel {n} trifft zu – {when} –, daher kommt {use} zuerst; die übrigen der Gruppe folgen dahinter, falls es fehlschlägt.",
"Rule {n} matches, but {use} has no account or key that can take this request, so the group's order stands.": "Regel {n} trifft zu, aber {use} hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher bleibt die Reihenfolge der Gruppe bestehen.",
"This turn began before magpie saw it, so the rules wait for the next one.": "Diese Runde begann, bevor magpie sie sah, daher warten die Regeln auf die nächste.",
diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css
index 932eb3be0..eee170e16 100644
--- a/internal/gui/assets/plugins/routing/routing.css
+++ b/internal/gui/assets/plugins/routing/routing.css
@@ -653,15 +653,16 @@ button.rt-session { cursor: pointer; }
.rt-cx:hover { color: var(--fg); border-color: var(--muted); }
.rt-c:hover > .rt-cx, .rt-c:focus-within > .rt-cx { opacity: 1; pointer-events: auto; }
@media (hover: none) { .rt-cx { opacity: 1; pointer-events: auto; } }
-/* the quiet + that adds a condition; in words while the rule has none */
-.rt-addc {
+/* the quiet + that adds a condition (in words while the rule has none),
+ and the one that adds the rule's next model */
+.rt-addc, .rt-adduse {
display: inline-grid; grid-auto-flow: column; place-items: center; gap: 5px;
width: 24px; height: 24px; padding: 0; border-radius: 999px; border: 1px solid transparent;
background: transparent; color: var(--muted); font: inherit; font-size: 12px; cursor: pointer;
transition: color .15s, border-color .15s, background .15s;
}
.rt-addc.wide { width: auto; height: 26px; padding: 0 10px 0 8px; border: 1px dashed var(--line); }
-.rt-addc:hover, .rt-addc.open, .rt-addc:focus-visible { color: var(--fg); background: var(--card-2); }
+.rt-addc:hover, .rt-addc.open, .rt-addc:focus-visible, .rt-adduse:hover, .rt-adduse.open, .rt-adduse:focus-visible { color: var(--fg); background: var(--card-2); }
.rt-addc.wide:hover, .rt-addc.wide.open { border-color: var(--muted); }
/* the rule's ⋯, always drawn, in the icons' quiet grey: it is the only
way to move or remove a rule, and drawn on hover alone nobody found it
diff --git a/internal/gui/tests/routing-rule-models.test.cjs b/internal/gui/tests/routing-rule-models.test.cjs
new file mode 100644
index 000000000..193b1d8af
--- /dev/null
+++ b/internal/gui/tests/routing-rule-models.test.cjs
@@ -0,0 +1,169 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// A routing group's rule names several models (monkeyin_92 on X: "can a
+// rule take more than one model? Otherwise one rule with several models
+// is several rules"). The editor draws one chip per model in order, a +
+// adds the next from the group's others (from the app's menu, never a
+// native select), a chip's × takes it out while the rule names another,
+// and a chip picks another in its place, none the rule names twice. Saved
+// as use (the first) and more (the rest); a rule of one model has no
+// more. A pause names one model and has no +. The trace says the rule's
+// next went first when its first had none to take it. Chromium and
+// WebKit, every language, at a wide and a phone's width.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+
+const assets = path.resolve(process.env.ASSETS || path.join(__dirname, "../assets"));
+const models = [
+ { id: "a/opus", name: "opus", providerName: "A", icon: "generic", context: 1000000, ready: true },
+ { id: "b/flash", name: "flash", providerName: "B", icon: "generic", context: 1000000, ready: true },
+ { id: "c/mini", name: "mini", providerName: "C", icon: "generic", context: 1000000, ready: true },
+];
+const member = (id) => ({ id, ready: true, context: 1000000, images: true });
+const groups = () => ({
+ models, deciders: [],
+ groups: [{ id: "main", name: "Main", members: ["a/opus", "b/flash", "c/mini"], routing: "order", ready: true,
+ rules: [{ use: "b/flash", more: ["c/mini"], images: true }], memberInfo: models.map((m) => member(m.id)) }],
+ pools: [],
+});
+const now = Date.now();
+// b/flash had nothing to take it, and the rule's next, c/mini, went first
+const route = {
+ id: 7, seq: 7, time: new Date(now - 2000).toISOString(), agent: "claude", model: "main", provider: "c", done: true, status: 200, ms: 900, tokens: 9000,
+ group: { id: "main", name: "Main", members: ["a/opus", "b/flash", "c/mini"] },
+ order: [{ id: "c", provider: "c", name: "C", kind: "key", model: "mini", known: true }, { id: "a", provider: "a", name: "A", kind: "key", model: "opus", known: true }],
+ tries: [{ id: "c", model: "mini", start: new Date(now - 2000).toISOString(), done: true, ms: 800, status: 200 }],
+ rule: { n: 1, use: "b/flash", more: ["c/mini"], instead: "c/mini", when: ["images"], turn: 2, tokens: 9000, images: true },
+};
+
+const words = {
+ en: { edit: "Edit", save: "Save", add: "Add a model it goes to next", rm: "Remove flash from this rule",
+ why: "Rule 1 matches, but b/flash has no account or key that can take this request, so c/mini, which the rule names after it, goes first." },
+ zh: { edit: "编辑", save: "保存", add: "添加下一个模型", rm: "从这条规则移除 flash",
+ why: "规则 1 命中,但 b/flash 没有能接这次请求的账号或 Key,于是由这条规则的下一个模型 c/mini 排第一。" },
+ "zh-TW": { edit: "編輯", save: "儲存", add: "新增下一個模型", rm: "從這條規則移除 flash",
+ why: "規則 1 命中,但 b/flash 沒有能接這次請求的帳號或 Key,於是由這條規則的下一個模型 c/mini 排第一。" },
+ ja: { edit: "編集", save: "保存", add: "次に使うモデルを追加", rm: "このルールから flash を外す",
+ why: "ルール 1 に一致しましたが、b/flash にはこのリクエストを受けられるアカウントやキーがないため、このルールの次のモデル c/mini が先頭です。" },
+ de: { edit: "Bearbeiten", save: "Speichern", add: "Ein Modell für danach hinzufügen", rm: "flash aus dieser Regel entfernen",
+ why: "Regel 1 trifft zu, aber b/flash hat kein Konto und keinen Schlüssel, der diese Anfrage annehmen kann, daher kommt c/mini, das die Regel danach nennt, zuerst." },
+};
+
+function serve(lang, posts) {
+ const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
+ let first = true;
+ return async (r) => {
+ const url = new URL(r.request().url());
+ const json = (data) => r.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
+ if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json(state);
+ if (url.pathname === "/api/gateway/trace") {
+ if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes
+ const routes = first ? [route] : [];
+ first = false;
+ return json({ mine: true, now: new Date().toISOString(), seq: 7, totals: { requests: 1, rerouted: 0, errors: 0 }, routes });
+ }
+ if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
+ if (url.pathname === "/api/groups") return json(groups());
+ if (url.pathname.startsWith("/api/groups/")) {
+ posts.push({ path: url.pathname, body: JSON.parse(r.request().postData() || "{}") });
+ return json(groups());
+ }
+ if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404 }); }
+ };
+}
+
+// wheel to a control, as the reader would (the page keeps still for any
+// other scroll), and click it where it is
+async function press(page, loc) {
+ const height = page.viewportSize().height;
+ await page.mouse.move(Math.min(550, (page.viewportSize().width / 2) | 0), 300);
+ for (let i = 0; i < 30; i++) {
+ const y = (await loc.boundingBox()).y;
+ if (y > 80 && y < height - 160) break;
+ await page.mouse.wheel(0, y < 80 ? -300 : 300);
+ await page.waitForTimeout(150);
+ }
+ const b = await loc.boundingBox();
+ await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
+}
+const pick = async (page, label) => {
+ const li = page.locator("#list li", { hasText: label });
+ await li.first().waitFor();
+ await li.first().click();
+};
+const chips = (row) => row.locator(".rt-then .rt-model").evaluateAll((bs) => bs.map((b) => b.textContent.trim().split(" · ")[0]));
+const saves = (posts) => posts.filter((p) => p.path === "/api/groups/save");
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of Object.keys(words)) {
+ const w = words[lang];
+ for (const width of [1100, 390]) {
+ test(`${engine} ${lang} ${width}: a rule names several models`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ t.after(() => browser.close());
+ const page = await (await browser.newContext({ viewport: { width, height: 1400 } })).newPage();
+ page.setDefaultTimeout(5000);
+ const errors = [], posts = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ await page.route("**/*", serve(lang, posts));
+ await page.goto("http://magpie.test/?view=routing");
+ const group = page.locator(".rt-group").first();
+ await group.waitFor();
+
+ // the trace: the rule's next went first
+ await page.waitForFunction((s) => document.documentElement.innerText.includes(s), w.why);
+
+ await press(page, group.locator("button", { hasText: w.edit }));
+ const ed = page.locator(".rt-gedit");
+ const row = ed.locator(".rt-rule").first();
+ await row.waitFor();
+ assert.deepEqual(await chips(row), ["flash", "mini"]);
+ assert.equal(await page.locator("select").count(), 0, "the app menu, not a native select");
+
+ // the × takes one out; the last has none
+ await row.locator(".rt-c", { has: page.locator(".rt-model", { hasText: "flash" }) }).hover();
+ await press(page, row.locator(`.rt-cx[title="${w.rm}"]`));
+ assert.deepEqual(await chips(row), ["mini"]);
+ assert.equal(await row.locator(".rt-then .rt-cx").count(), 0, "a rule's one model can't be taken out");
+
+ // the + adds the next, from those it doesn't name
+ const add = row.locator(".rt-adduse");
+ assert.equal(await add.getAttribute("title"), w.add);
+ await press(page, add);
+ assert.equal(await page.locator("#list li").count(), 2);
+ await pick(page, "flash");
+ assert.deepEqual(await chips(row), ["mini", "flash"]);
+ await press(page, row.locator(".rt-adduse"));
+ await pick(page, "opus");
+ assert.deepEqual(await chips(row), ["mini", "flash", "opus"]);
+ assert.equal(await row.locator(".rt-adduse").count(), 0, "no + once it names every model");
+
+ // a chip picks another in its place, none named twice
+ await press(page, row.locator(".rt-model").first());
+ assert.equal(await page.locator("#list li").count(), 1);
+ await pick(page, "mini");
+ // nothing past the editor's side, nor a sideways scroll
+ const over = await page.evaluate(() => {
+ const e = document.querySelector(".rt-gedit").getBoundingClientRect();
+ return [...document.querySelectorAll(".rt-rule, .rt-rule *")].filter((x) => x.offsetParent && x.getBoundingClientRect().right > e.right + 1).map((x) => x.className || x.tagName);
+ });
+ assert.deepEqual(over, []);
+ assert(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "no sideways scroll");
+
+ await press(page, ed.locator("button.primary", { hasText: w.save }));
+ await page.waitForFunction(() => !document.querySelector(".rt-gedit"));
+ const saved = saves(posts).at(-1).body.rules[0];
+ assert.deepEqual([saved.use, saved.more], ["c/mini", ["b/flash", "a/opus"]]);
+ assert.deepEqual(errors, []);
+ });
+ }
+ }
+}
diff --git a/internal/provider/autoref.go b/internal/provider/autoref.go
index dda4f185a..f8cf84722 100644
--- a/internal/provider/autoref.go
+++ b/internal/provider/autoref.go
@@ -106,7 +106,7 @@ func (g *Group) canonicalRefs() {
}
}
for i := range g.Rules {
- g.Rules[i].Use = CanonicalRef(g.Rules[i].Use)
+ g.Rules[i].MapUses(CanonicalRef)
}
g.Classifier = CanonicalRef(g.Classifier)
}
diff --git a/internal/provider/group.go b/internal/provider/group.go
index e8edea936..60ebdd06f 100644
--- a/internal/provider/group.go
+++ b/internal/provider/group.go
@@ -380,7 +380,7 @@ func cloneGroups(gs []Group) []Group {
if g.Rules != nil {
rs := make([]Rule, len(g.Rules))
for j, r := range g.Rules {
- r.Agents, r.Folders, r.ExceptFolders = slices.Clone(r.Agents), slices.Clone(r.Folders), slices.Clone(r.ExceptFolders)
+ r.Agents, r.Folders, r.ExceptFolders, r.More = slices.Clone(r.Agents), slices.Clone(r.Folders), slices.Clone(r.ExceptFolders), slices.Clone(r.More)
if r.Time != nil {
t := *r.Time
t.Days = slices.Clone(t.Days)
@@ -459,7 +459,7 @@ func withoutRemoved(g Group, have map[string]bool) Group {
if !ok || have[pid] || (g.Routing == Manual && m == g.Picked()) {
return false
}
- return !slices.ContainsFunc(g.Rules, func(r Rule) bool { return r.Use == m })
+ return !slices.ContainsFunc(g.Rules, func(r Rule) bool { return r.Names(m) })
}
if !slices.ContainsFunc(g.Members, gone) {
return g
@@ -498,6 +498,7 @@ func (g Group) own() Group {
g.Rules[i].Agents = slices.Clone(r.Agents)
g.Rules[i].Folders = slices.Clone(r.Folders)
g.Rules[i].ExceptFolders = slices.Clone(r.ExceptFolders)
+ g.Rules[i].More = slices.Clone(r.More)
if r.Time != nil {
w := *r.Time
w.Days = slices.Clone(w.Days)
@@ -1256,7 +1257,7 @@ func SaveGroup(g Group) error {
return fmt.Errorf("every model in %s is switched off: switch one on, or it has nothing to send to", g.Name)
}
for i := range g.Rules {
- g.Rules[i].Use = cleanMember(entries, strings.TrimSpace(g.Rules[i].Use))
+ g.Rules[i].MapUses(func(u string) string { return cleanMember(entries, strings.TrimSpace(u)) })
}
if err := groupsInGroup(g, groupsIn(entries)); err != nil {
return err
@@ -1656,10 +1657,13 @@ func RenameGroup(from, to string) error {
f.Groups[i].Members[j] = now
}
}
- for j, r := range f.Groups[i].Rules {
- if r.Use == old {
- f.Groups[i].Rules[j].Use = now
- }
+ for j := range f.Groups[i].Rules {
+ f.Groups[i].Rules[j].MapUses(func(u string) string {
+ if u == old {
+ return now
+ }
+ return u
+ })
}
if f.Groups[i].Pick == old {
f.Groups[i].Pick = now
diff --git a/internal/provider/group_share.go b/internal/provider/group_share.go
index 649719756..ece9ac951 100644
--- a/internal/provider/group_share.go
+++ b/internal/provider/group_share.go
@@ -205,18 +205,25 @@ func ImportGroup(text string) (Imported, error) {
in := append(slices.Clone(g.Members), matchesIn(entries, g)...)
var rules []Rule
for _, r := range g.Rules {
- use := here[strings.TrimSpace(r.Use)]
- if _, named := here[strings.TrimSpace(r.Use)]; !named && !IsPattern(r.Use) {
- // a rule may send to a model a pattern matches: one this
- // magpie has, or none
- use = memberHere(entries, all, r.Use)
- }
+ // each model it sends to as this magpie has it; one it hasn't is
+ // left out of the rule, and a rule left with none goes
+ r.MapUses(func(u string) string {
+ use := here[strings.TrimSpace(u)]
+ if _, named := here[strings.TrimSpace(u)]; !named && !IsPattern(u) {
+ // a rule may send to a model a pattern matches: one this
+ // magpie has, or none
+ use = memberHere(entries, all, u)
+ }
+ if !slices.Contains(in, use) {
+ return ""
+ }
+ return use
+ })
// a folder is the sharer's path, never this machine's
- if use == "" || !slices.Contains(in, use) || (r.Intent != "" && g.Classifier == "") || r.ByFolder() {
+ if r.Use == "" || (r.Intent != "" && g.Classifier == "") || r.ByFolder() {
res.Rules++
continue
}
- r.Use = use
rules = append(rules, r)
}
g.Rules = rules
diff --git a/internal/provider/groupfast.go b/internal/provider/groupfast.go
index 0bac707a6..1e5df8478 100644
--- a/internal/provider/groupfast.go
+++ b/internal/provider/groupfast.go
@@ -212,10 +212,13 @@ func (g *Group) RenameMember(from, to string) {
g.Members[i] = to
}
}
- for i, r := range g.Rules {
- if r.Use == from {
- g.Rules[i].Use = to
- }
+ for i := range g.Rules {
+ g.Rules[i].MapUses(func(u string) string {
+ if u == from {
+ return to
+ }
+ return u
+ })
}
if g.Pick == from {
g.Pick = to
diff --git a/internal/provider/grouprule.go b/internal/provider/grouprule.go
index c498bcaff..9085f8841 100644
--- a/internal/provider/grouprule.go
+++ b/internal/provider/grouprule.go
@@ -13,8 +13,9 @@ package provider
// A rule decides when a user's turn begins; the agent's requests within
// that turn — tool results sent back — stay with what it decided, so a
// turn is never moved to another model halfway through (the gateway keeps
-// that; see gateway/rules.go). The member a rule names goes first; the
-// group's other members stay behind it for failover.
+// that; see gateway/rules.go). The member a rule names goes first, then
+// the others it names after it (Rule.More), in order; the group's other
+// members stay behind them for failover.
//
// A pause rule (Rule.Pause) sends nothing first: while it holds, the
// member it names is out of the group altogether — not first, not a
@@ -36,10 +37,19 @@ import (
// Efforts are the reasoning levels a rule can ask for, lowest first.
var Efforts = []string{"low", "medium", "high", "xhigh", "max"}
-// Rule sends the requests it matches to one of its group's members first.
-// Every condition set must hold; a rule sets at least one.
+// Rule sends the requests it matches to one of its group's members first,
+// and then to the others it names, in order. Every condition set must
+// hold; a rule sets at least one.
type Rule struct {
Use string `json:"use"` // the member, as the group names it: "provider/model"
+ // More: the members the rule sends to after Use, in order, each tried
+ // when the ones before it can't take the request (monkeyin_92 on X: one
+ // rule for several models, not a rule each). They go before the
+ // members the matching rules after it name, and the group's others.
+ // Use stays the first, so a magpie from before More still sends the
+ // rule's requests to it, and has the others behind it as group members.
+ // A pause names one model and has none.
+ More []string `json:"more,omitempty"`
// UseEffort: the reasoning the member is asked for on the requests the
// rule sends it (one of Efforts), in place of what the agent asked —
// an intent picks the model and its effort as a pair (#2633). Like a
@@ -91,6 +101,57 @@ type Rule struct {
Pause bool `json:"pause,omitempty"`
}
+// Uses are the members the rule sends to, in order: Use, then More.
+func (r Rule) Uses() []string {
+ if r.Use == "" {
+ return slices.Clone(r.More)
+ }
+ return append([]string{r.Use}, r.More...)
+}
+
+// SetUses has the rule send to uses, in order, each once and none empty:
+// the first is Use, the rest More.
+func (r *Rule) SetUses(uses []string) {
+ var out []string
+ for _, u := range uses {
+ if u != "" && !slices.Contains(out, u) {
+ out = append(out, u)
+ }
+ }
+ r.Use, r.More = "", nil
+ if len(out) > 0 {
+ r.Use = out[0]
+ }
+ if len(out) > 1 {
+ r.More = out[1:]
+ }
+}
+
+// MapUses has each member the rule sends to go by what f makes of it:
+// a member renamed, cleaned or taken out ("").
+func (r *Rule) MapUses(f func(string) string) {
+ uses := r.Uses()
+ for i, u := range uses {
+ uses[i] = f(u)
+ }
+ r.SetUses(uses)
+}
+
+// Without is the rule with m no longer among the members it sends to;
+// false when it names no other, and goes with m.
+func (r Rule) Without(m string) (Rule, bool) {
+ r.MapUses(func(u string) string {
+ if u == m {
+ return ""
+ }
+ return u
+ })
+ return r, r.Use != ""
+}
+
+// Names reports whether the rule sends to m.
+func (r Rule) Names(m string) bool { return r.Use == m || slices.Contains(r.More, m) }
+
// TimeWindow is hours of the day, local time: From until To, "HH:MM" each,
// past midnight when To comes before From (22:00–08:00), the whole day
// when they are the same. Days, when set, are the days it holds on ("mon"
@@ -276,7 +337,7 @@ func (r Rule) Conditions() []string {
func cleanRules(rules []Rule, members []string) ([]Rule, error) {
var out []Rule
for i, r := range rules {
- r.Use = strings.TrimSpace(r.Use)
+ r.MapUses(strings.TrimSpace)
r.Effort = strings.ToLower(strings.TrimSpace(r.Effort))
r.UseEffort = strings.ToLower(strings.TrimSpace(r.UseEffort))
r.Agents = cleanList(r.Agents)
@@ -305,8 +366,11 @@ func cleanRules(rules []Rule, members []string) ([]Rule, error) {
switch {
case r.Use == "":
return nil, fmt.Errorf("rule %d: which model it sends to is missing", n)
- case !slices.Contains(members, r.Use):
- return nil, fmt.Errorf("rule %d: %s is not in the group", n, r.Use)
+ case slices.ContainsFunc(r.Uses(), func(u string) bool { return !slices.Contains(members, u) }):
+ u := r.Uses()[slices.IndexFunc(r.Uses(), func(u string) bool { return !slices.Contains(members, u) })]
+ return nil, fmt.Errorf("rule %d: %s is not in the group", n, u)
+ case r.Pause && len(r.More) > 0:
+ return nil, fmt.Errorf("rule %d: a pause names one model: give each its own rule", n)
case r.Tokens < 0:
return nil, fmt.Errorf("rule %d: tokens can't be negative", n)
case r.Effort != "" && r.Effort != "on" && !slices.Contains(Efforts, r.Effort):
@@ -403,13 +467,19 @@ func MatchRule(rules []Rule, q RuleRequest) int {
}
// ThenUses are the members the rules after the n-th (from 1) that also
-// match q send to, each once and in the rules' order, leaving out use: who
-// a request a rule sent to use fails over to before the group's others (#112).
-func ThenUses(rules []Rule, q RuleRequest, n int, use string) []string {
+// match q send to, each once and in the rules' order (a rule's own in its
+// order), leaving out those of uses: who a request a rule sent to uses
+// fails over to before the group's others (#112).
+func ThenUses(rules []Rule, q RuleRequest, n int, uses []string) []string {
var out []string
for i := n; i < len(rules); i++ {
- if u := rules[i].Use; u != use && !slices.Contains(out, u) && rules[i].Matches(q) {
- out = append(out, u)
+ if !rules[i].Matches(q) {
+ continue
+ }
+ for _, u := range rules[i].Uses() {
+ if !slices.Contains(uses, u) && !slices.Contains(out, u) {
+ out = append(out, u)
+ }
}
}
return out
@@ -472,14 +542,35 @@ func ruledEntry(e *Entry, g Group, ms []Member, entries []Entry) {
}
return out, found
}
+ // ofRule is what every member a rule sends to takes: a request it
+ // matches may go to any of them
+ ofRule := func(r Rule) (Entry, bool) {
+ var out Entry
+ for i, id := range r.Uses() {
+ x, ok := of(id)
+ if !ok {
+ return Entry{}, false
+ }
+ if i == 0 {
+ out = x
+ continue
+ }
+ if x.Context > 0 && (out.Context == 0 || x.Context < out.Context) {
+ out.Context = x.Context
+ }
+ out.Images = out.Images && x.Images
+ out.ImageInput = sharedImageInput(out.ImageInput, x.ImageInput)
+ }
+ return out, r.Use != ""
+ }
sees := func(x Entry) bool { return x.Images && (x.ImageInput == nil || *x.ImageInput) }
for i, r := range g.Rules {
- x, ok := of(r.Use)
+ x, ok := ofRule(r)
if !ok || r.Pause {
continue
}
if r.Images && r.Tokens == 0 && r.Effort == "" && len(r.Agents) == 0 && r.Intent == "" && !r.Compact && r.Time == nil && !r.ByFolder() && sees(x) && !e.Images &&
- !slices.ContainsFunc(g.Rules[:i], func(b Rule) bool { y, ok := of(b.Use); return !b.Pause && (!ok || !sees(y)) }) {
+ !slices.ContainsFunc(g.Rules[:i], func(b Rule) bool { y, ok := ofRule(b); return !b.Pause && (!ok || !sees(y)) }) {
e.Images, e.ImageInput = true, x.ImageInput
}
if r.Tokens == 0 || r.Images || r.Effort != "" || len(r.Agents) > 0 || r.Intent != "" || r.Compact || r.Time != nil || r.ByFolder() || x.Context <= e.Context {
@@ -489,7 +580,7 @@ func ruledEntry(e *Entry, g Group, ms []Member, entries []Entry) {
// it, and a longer one may still be taken by a rule before
fits := x.Context
for _, m := range ms {
- if m.ID == r.Use {
+ if r.Names(m.ID) {
continue
}
if y, ok := of(m.ID); ok && y.Context > 0 && y.Context < r.Tokens {
@@ -501,7 +592,7 @@ func ruledEntry(e *Entry, g Group, ms []Member, entries []Entry) {
if before.Pause {
continue
}
- if y, ok := of(before.Use); !ok {
+ if y, ok := ofRule(before); !ok {
fits = 0
} else if y.Context < fits {
fits = y.Context
diff --git a/internal/provider/grouprule_more_test.go b/internal/provider/grouprule_more_test.go
new file mode 100644
index 000000000..2b8395641
--- /dev/null
+++ b/internal/provider/grouprule_more_test.go
@@ -0,0 +1,179 @@
+package provider
+
+import (
+ "encoding/json"
+ "slices"
+ "strings"
+ "testing"
+)
+
+// A rule names several models (monkeyin_92 on X): each must be the
+// group's, each is named once, and a pause names one.
+func TestCleanRulesSeveralModels(t *testing.T) {
+ members := []string{"a/m", "b/big", "c/x"}
+ got, err := cleanRules([]Rule{{Use: " b/big ", More: []string{" a/m", "", "b/big", "c/x"}, Tokens: 5}}, members)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got[0].Use != "b/big" || !slices.Equal(got[0].More, []string{"a/m", "c/x"}) || !slices.Equal(got[0].Uses(), []string{"b/big", "a/m", "c/x"}) {
+ t.Fatalf("%+v", got[0])
+ }
+ if _, err := cleanRules([]Rule{{Use: "b/big", More: []string{"d/gone"}, Tokens: 5}}, members); err == nil || !strings.Contains(err.Error(), "d/gone is not in the group") {
+ t.Fatalf("a model not in the group: %v", err)
+ }
+ if _, err := cleanRules([]Rule{{Use: "b/big", More: []string{"a/m"}, Pause: true, Agents: []string{"codex"}}}, members); err == nil || !strings.Contains(err.Error(), "a pause names one model") {
+ t.Fatalf("pause: %v", err)
+ }
+ // only More, as a hand-edited file may have it: the first is Use
+ if got, err := cleanRules([]Rule{{More: []string{"c/x", "a/m"}, Tokens: 5}}, members); err != nil || got[0].Use != "c/x" || !slices.Equal(got[0].More, []string{"a/m"}) {
+ t.Fatalf("only more: %+v %v", got, err)
+ }
+}
+
+// A group saved before More reads as it was; one saved with it keeps
+// Use first, which is what a magpie from before More sends to.
+func TestRuleMoreOnDisk(t *testing.T) {
+ var old Rule
+ if err := json.Unmarshal([]byte(`{"use":"b/big","tokens":5}`), &old); err != nil || old.Use != "b/big" || old.More != nil {
+ t.Fatalf("old: %+v %v", old, err)
+ }
+ b, _ := json.Marshal(old)
+ if strings.Contains(string(b), "more") {
+ t.Fatalf("a rule of one model writes more: %s", b)
+ }
+ b, _ = json.Marshal(Rule{Use: "b/big", More: []string{"a/m"}, Tokens: 5})
+ var before struct {
+ Use string `json:"use"`
+ }
+ if err := json.Unmarshal(b, &before); err != nil || before.Use != "b/big" {
+ t.Fatalf("%s: %+v", b, before)
+ }
+}
+
+// Without takes one model out of a rule; the rule goes only with its last.
+func TestRuleWithout(t *testing.T) {
+ r := Rule{Use: "a/m", More: []string{"b/big"}, Tokens: 5}
+ if k, ok := r.Without("a/m"); !ok || k.Use != "b/big" || k.More != nil || k.Tokens != 5 {
+ t.Fatalf("%+v %v", k, ok)
+ }
+ if k, ok := r.Without("b/big"); !ok || k.Use != "a/m" || k.More != nil {
+ t.Fatalf("%+v %v", k, ok)
+ }
+ if _, ok := (Rule{Use: "a/m", Tokens: 5}).Without("a/m"); ok {
+ t.Fatal("a rule left with no model kept")
+ }
+ if !r.Names("b/big") || r.Names("c/x") {
+ t.Fatal("Names")
+ }
+}
+
+// use=a,b names several, in order; Line writes them back the same way.
+func TestParseRuleSeveralModels(t *testing.T) {
+ g := Group{ID: "g", Members: []string{"a/small", "b/big", "c/x"}}
+ r, _, _, err := ParseRule(g, []string{"use=big,small", "tokens=5000"})
+ if err != nil || r.Use != "b/big" || !slices.Equal(r.More, []string{"a/small"}) {
+ t.Fatalf("%+v %v", r, err)
+ }
+ if line := r.Line(); !strings.Contains(line, "use=b/big,a/small") {
+ t.Fatalf("line %q", line)
+ }
+ back, _, _, err := ParseRule(g, strings.Fields(r.Line()))
+ if err != nil || back.Use != r.Use || !slices.Equal(back.More, r.More) {
+ t.Fatalf("round trip %+v %v", back, err)
+ }
+ if _, _, _, err := ParseRule(g, []string{"use=big,nope", "tokens=5"}); err == nil {
+ t.Fatal("a model not in the group taken")
+ }
+ if _, _, _, err := ParseRule(g, []string{"use=", "tokens=5"}); err == nil {
+ t.Fatal("no model taken")
+ }
+ if r, _, _, err := ParseRule(g, []string{"use=big,small", "pause=c/x", "agent=codex"}); err != nil || r.More != nil || r.Use != "c/x" {
+ t.Fatalf("pause after use: %+v %v", r, err)
+ }
+}
+
+// The rules after the one that matched name who comes next, leaving out
+// every model the matching rule names itself.
+func TestThenUsesLeavesOutTheRulesOwn(t *testing.T) {
+ rules := []Rule{
+ {Use: "a/m", More: []string{"b/big"}, Tokens: 10},
+ {Use: "b/big", Tokens: 10},
+ {Use: "c/x", More: []string{"a/m", "d/y"}, Tokens: 10},
+ }
+ q := RuleRequest{Tokens: 50}
+ if got := ThenUses(rules, q, 1, rules[0].Uses()); !slices.Equal(got, []string{"c/x", "d/y"}) {
+ t.Fatal(got)
+ }
+}
+
+// What a group says it can take, as its rules have it: a rule's request
+// may go to any model it names, so it is what each of them takes.
+func TestRuledEntrySeveralModels(t *testing.T) {
+ no, yes := false, true
+ entries := []Entry{
+ {ID: "a/text", Model: "text", Provider: Provider{ID: "a"}, Context: 128000, ImageInput: &no},
+ {ID: "a/vision", Model: "vision", Provider: Provider{ID: "a"}, Context: 200000, Images: true, ImageInput: &yes},
+ {ID: "b/vision", Model: "vision", Provider: Provider{ID: "b"}, Context: 400000, Images: true, ImageInput: &yes},
+ }
+ run := func(members []string, rules []Rule) Entry {
+ var ms []Member
+ for _, id := range members {
+ p, m, _ := strings.Cut(id, "/")
+ ms = append(ms, Member{ID: id, Provider: Provider{ID: p}, Model: m})
+ }
+ e := Entry{Context: 128000}
+ ruledEntry(&e, Group{Members: members, Rules: rules}, ms, entries)
+ return e
+ }
+ if e := run([]string{"a/text", "a/vision", "b/vision"}, []Rule{{Use: "a/vision", More: []string{"b/vision"}, Images: true}}); !e.Images {
+ t.Errorf("two that see: %+v", e)
+ }
+ if e := run([]string{"a/text", "a/vision"}, []Rule{{Use: "a/vision", More: []string{"a/text"}, Images: true}}); e.Images {
+ t.Errorf("one blind: %+v", e)
+ }
+ if e := run([]string{"a/text", "a/vision", "b/vision"}, []Rule{{Use: "b/vision", More: []string{"a/vision"}, Tokens: 100000}}); e.Context != 200000 {
+ t.Errorf("long: the least of the rule's: %+v", e)
+ }
+}
+
+// A shared group's rule keeps the models this magpie has, in order; one
+// it hasn't is left out of the rule, and the rule goes when none is here.
+func TestSharedGroupRuleSeveralModels(t *testing.T) {
+ shareHome(t, Provider{ID: "mine", Name: "Mine", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x", "y"}})
+ text, _ := json.Marshal(SharedGroup{Magpie: ShareMark, Version: ShareVersion, Group: Group{
+ ID: "theirs", Name: "Theirs", Members: []string{"relay/x", "gone/z", "relay/y"}, Routing: Ordered,
+ Rules: []Rule{
+ {Use: "gone/z", More: []string{"relay/y", "relay/x"}, Tokens: 1000},
+ {Use: "gone/z", Agents: []string{"claude"}},
+ },
+ }})
+ res, err := ImportGroup(string(text))
+ if err != nil {
+ t.Fatal(err)
+ }
+ g := sharedAt(t, res.ID)
+ if res.Rules != 1 || len(g.Rules) != 1 || g.Rules[0].Use != "mine/y" || !slices.Equal(g.Rules[0].More, []string{"mine/x"}) {
+ t.Fatalf("left out %d, %+v", res.Rules, g.Rules)
+ }
+}
+
+// A model renamed, or the group's member removed, is renamed or taken out
+// in every rule naming it, More too; a member a rule's More names stays.
+func TestRuleMoreRenamedAndKept(t *testing.T) {
+ shareHome(t, Provider{ID: "relay", Name: "R", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2"}})
+ if err := SaveGroup(Group{ID: "top", Name: "Top", Members: []string{"relay/m1", "relay/m2"},
+ Rules: []Rule{{Use: "relay/m1", More: []string{"relay/m2"}, Tokens: 5}}}); err != nil {
+ t.Fatal(err)
+ }
+ if err := Rename("relay", "fast"); err != nil {
+ t.Fatal(err)
+ }
+ g, _, _ := FindGroup("group/top")
+ if g.Rules[0].Use != "fast/m1" || !slices.Equal(g.Rules[0].More, []string{"fast/m2"}) {
+ t.Fatalf("renamed: %+v", g.Rules)
+ }
+ kept := withoutRemoved(Group{Members: []string{"fast/m1", "gone/m2"}, Rules: []Rule{{Use: "fast/m1", More: []string{"gone/m2"}, Tokens: 5}}}, map[string]bool{"fast": true})
+ if !slices.Contains(kept.Members, "gone/m2") {
+ t.Fatalf("a model a rule's More names was taken out: %+v", kept)
+ }
+}
diff --git a/internal/provider/grouprule_pause_test.go b/internal/provider/grouprule_pause_test.go
index 9fe760041..91b1be1b6 100644
--- a/internal/provider/grouprule_pause_test.go
+++ b/internal/provider/grouprule_pause_test.go
@@ -28,7 +28,7 @@ func TestPauseRuleCleanAndMatch(t *testing.T) {
}
}
q := RuleRequest{Agent: "codex", At: at(2, 10, 0)}
- if MatchRule(rules, q) != -1 || len(ThenUses(rules, q, 0, "")) != 0 {
+ if MatchRule(rules, q) != -1 || len(ThenUses(rules, q, 0, nil)) != 0 {
t.Fatal("a pause rule sent a request to its model")
}
if (Group{Rules: rules}).Ruled() || !(Group{Rules: append(rules, Rule{Use: "a/x", Tokens: 1})}).Ruled() {
diff --git a/internal/provider/grouprule_test.go b/internal/provider/grouprule_test.go
index 90fc91d94..97d2345b8 100644
--- a/internal/provider/grouprule_test.go
+++ b/internal/provider/grouprule_test.go
@@ -313,10 +313,10 @@ func TestThenUses(t *testing.T) {
{Use: "ai/flash", Images: true},
}
q := RuleRequest{Images: true, Tokens: 50}
- if got := ThenUses(rules, q, 1, "codex/gpt"); !slices.Equal(got, []string{"ai/flash"}) {
+ if got := ThenUses(rules, q, 1, []string{"codex/gpt"}); !slices.Equal(got, []string{"ai/flash"}) {
t.Fatal(got)
}
- if got := ThenUses(rules, q, 4, "ai/flash"); len(got) != 0 {
+ if got := ThenUses(rules, q, 4, []string{"ai/flash"}); len(got) != 0 {
t.Fatal(got)
}
}
diff --git a/internal/provider/rename.go b/internal/provider/rename.go
index 6becafdbe..e88c05ec4 100644
--- a/internal/provider/rename.go
+++ b/internal/provider/rename.go
@@ -87,8 +87,8 @@ func Rename(from, to string) error {
for k, m := range g.Members {
g.Members[k] = renamedRef(m, from, to)
}
- for k, r := range g.Rules {
- g.Rules[k].Use = renamedRef(r.Use, from, to)
+ for k := range g.Rules {
+ g.Rules[k].MapUses(func(u string) string { return renamedRef(u, from, to) })
}
for k, m := range g.Fast {
g.Fast[k] = renamedRef(m, from, to)
diff --git a/internal/provider/ruleparse.go b/internal/provider/ruleparse.go
index db2947b89..087eb56c8 100644
--- a/internal/provider/ruleparse.go
+++ b/internal/provider/ruleparse.go
@@ -1,6 +1,6 @@
package provider
-// A rule as it is typed: use=<model> use-effort=low tokens=200k images effort=high
+// A rule as it is typed: use=<model>[,<model>…] use-effort=low tokens=200k images effort=high
// agents=a,b intent="…" compact time=09:00-18:00 days=mon-fri
// folders=~/work,"~/My Project" except-folders=~/work/old, or
// pause=<model> time=… days=… agents=… folders=… except-folders=… — by
@@ -92,17 +92,28 @@ func ParseRule(g Group, words []string) (r Rule, at int, classifier string, err
k = strings.ToLower(strings.TrimSpace(k))
switch k {
case "use", "model", "to":
- id, err := GroupMember(g, v)
- if err != nil {
- return r, 0, "", err
+ // use=a,b: a first, then b when a can't take the request
+ var uses []string
+ for _, m := range strings.Split(v, ",") {
+ if m = strings.TrimSpace(m); m == "" {
+ continue
+ }
+ id, err := GroupMember(g, m)
+ if err != nil {
+ return r, 0, "", err
+ }
+ uses = append(uses, id)
}
- r.Use = id
+ if len(uses) == 0 {
+ return r, 0, "", fmt.Errorf("use=<model> names the model it sends to, or several in order: use=a,b")
+ }
+ r.SetUses(uses)
case "pause", "skip":
id, err := GroupMember(g, v)
if err != nil {
return r, 0, "", err
}
- r.Use, r.Pause = id, true
+ r.Use, r.More, r.Pause = id, nil, true
case "tokens", "context", "longer":
n, err := ParseTokens(v)
if err != nil {
@@ -197,6 +208,9 @@ func ParseRule(g Group, words []string) (r Rule, at int, classifier string, err
if r.Use == "" {
return r, 0, "", fmt.Errorf("use=<model> (or pause=<model>) is missing: one of %s", strings.Join(g.Members, ", "))
}
+ if r.Pause && len(r.More) > 0 {
+ return r, 0, "", fmt.Errorf("pause=<model> names one model: give each its own rule")
+ }
if r.Pause && r.UseEffort != "" {
return r, 0, "", fmt.Errorf("pause=<model> sends nothing, so it takes no use-effort=")
}
@@ -274,7 +288,7 @@ func RuleWords(line string) []string {
// Line is the rule as ParseRule reads it back.
func (r Rule) Line() string {
- out := []string{"use=" + r.Use}
+ out := []string{"use=" + strings.Join(r.Uses(), ",")}
if r.Pause {
out[0] = "pause=" + r.Use
}
diff --git a/internal/tui/groupfast_test.go b/internal/tui/groupfast_test.go
index 4a9bc08fd..92d383ee1 100644
--- a/internal/tui/groupfast_test.go
+++ b/internal/tui/groupfast_test.go
@@ -64,3 +64,28 @@ func TestGroupTakeOutRefusedWhileHeld(t *testing.T) {
t.Fatalf("members %v rules %+v", g.Members, g.Rules)
}
}
+
+// A rule naming several models shows them in order; taking one out of the
+// group leaves the rule on the others.
+func TestGroupTakeOutKeepsARulesOtherModels(t *testing.T) {
+ home(t)
+ if err := provider.SaveGroup(provider.Group{ID: "pair", Name: "Pair", Members: []string{"a/m", "b/gpt-5.5"},
+ Rules: []provider.Rule{{Use: "a/m", More: []string{"b/gpt-5.5"}, Tokens: 5}}}); err != nil {
+ t.Fatal(err)
+ }
+ m := press(t, model{w: 120, h: 40}, "3", "enter")
+ if m.mode != modeGroup {
+ t.Fatalf("mode %d", m.mode)
+ }
+ if v := m.View(); !strings.Contains(v, "a/m, b/gpt-5.5") {
+ t.Fatalf("rule not shown with both models:\n%s", v)
+ }
+ m = press(t, m, "d")
+ if strings.Contains(m.flash, "its rules") {
+ t.Fatalf("flash %q: the rule keeps b/gpt-5.5", m.flash)
+ }
+ press(t, m, "d")
+ if g := group(t, "pair"); !slices.Equal(g.Members, []string{"b/gpt-5.5"}) || len(g.Rules) != 1 || g.Rules[0].Use != "b/gpt-5.5" || g.Rules[0].More != nil {
+ t.Fatalf("members %v rules %+v", g.Members, g.Rules)
+ }
+}
diff --git a/internal/tui/routing.go b/internal/tui/routing.go
index e455a1b29..cbbaa5986 100644
--- a/internal/tui/routing.go
+++ b/internal/tui/routing.go
@@ -523,7 +523,7 @@ func (m model) updateGroup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if m.confirm != "member/"+id {
m.confirm = "member/" + id
m.flash, m.flashOK = "press d again to take "+id+" out", false
- if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Use == id }) {
+ if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { _, kept := r.Without(id); return !kept }) {
m.flash += " and its rules"
}
return m, nil
@@ -532,7 +532,14 @@ func (m model) updateGroup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.gsel = clamp(m.gsel, all-1)
return m, saveGroup(g.ID, func(g *provider.Group) error {
g.Members = slices.DeleteFunc(g.Members, func(x string) bool { return x == id })
- g.Rules = slices.DeleteFunc(g.Rules, func(r provider.Rule) bool { return r.Use == id })
+ // a rule that sends to others too keeps them
+ var rules []provider.Rule
+ for _, r := range g.Rules {
+ if r, kept := r.Without(id); kept {
+ rules = append(rules, r)
+ }
+ }
+ g.Rules = rules
return nil
}, id+" taken out")
}
@@ -663,9 +670,10 @@ func (m model) viewGroup() string {
}
for i := ruleFrom; i < ruleTo; i++ {
r := g.Rules[i]
- marker, use := " ", sText.Render(r.Use)
+ uses := strings.Join(r.Uses(), ", ") // in order: the next when one can't take it
+ marker, use := " ", sText.Render(uses)
if len(g.Members)+i == m.gsel {
- marker, use = sCursor.Render("▸ "), sNameOn.Render(r.Use)
+ marker, use = sCursor.Render("▸ "), sNameOn.Render(uses)
}
arrow := " → "
if r.Pause {