gateway: a bare model name goes to the asking agent's own subscription first
freefcw on X: Codex, started by Mirasim, asked for gpt-6-astra by its bare
name. Its own ChatGPT subscription serves it, but magpie sent it to Devin
through the group it found (auto-gpt-6-astra, soonest renewal first).
With found groups off it still went to another vendor: Resolve takes the
first provider in the Providers order, AutoStandIn and agent.Reseat did
the same.
Now:
- A bare model id the asking agent's own subscription in magpie serves (a
provider whose Account.Agent is the agent: Codex's ChatGPT accounts,
Claude's for Claude Code, Copilot's) goes there, found groups on or off
(gateway ownFirst, provider.OwnRoute). count_tokens counts it there too.
- Kept as they were: a group the user made or changed by that name; a found
group magpie set one of the agent's fields to (agent.SetToGroup over the
applied record, #750's bare spelling included, wired as
gateway.SetToGroup); a gateway key held to some models; ids with a
provider in them and group refs; clients magpie doesn't know as an agent.
- Found groups off: a bare id several providers serve, none of them the
agent's own, is refused for an agent that has a subscription of its own
in magpie, with a 400 naming the provider/model ids and
"magpie group auto on" (provider.OffSpread). Off, magpie picks no vendor.
- AutoStandInFor and Reseat's sameModel prefer the agent's own
subscription for a found group that is gone.
Docs: gateway-routing.md's Model resolution row and the bare-ids section.
Run (mote, at this tree on c7ebb6f8d):
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
./internal/agent ./internal/cli ./internal/gateway: all ok.
Broken by hand, each red:
- ownFirst off in serve (and AutoStandIn back):
TestBareNameGoesToTheAgentsOwnSubscription went to the relay with groups
on, and again with groups off alone (the reporter's two cases).
- OffSpread check off: TestBareNameOnSeveralVendorsRefusedWhileGroupsOff
answered 200 from the relay.
- SetToGroup check off: TestBareNameOfAGroupSetOnTheAgentStaysTheGroups.
- The user's-group and own-entry checks off: TestOwnRoute, TestOffSpread.
Not run at this head: the full ./... suite (mote's suite.lock had five
suites queued); it runs under the lock after the push, any red filed.
Not tried: a real Codex with a real Devin plugin account; Reseat's own
preference has no test of its own (needs a fetched Codex account in the
agent package's sandbox).
Reported-by: freefcw (X)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1069,3 +1069,26 @@ func whatsOff(name, file, k, v, want string) string {
|
||||
return where + " was changed, so magpie's gateway won't take its requests"
|
||||
}
|
||||
}
|
||||
|
||||
// SetToGroup reports whether magpie set one of the agent's fields to the
|
||||
// routing group gid ("group/<id>"), by its id or its model's name (#750),
|
||||
// at an effort or not: the user picked that group for the agent. The
|
||||
// gateway then keeps a bare name the agent asks for by on the group,
|
||||
// rather than on the agent's own subscription. For gateway.SetToGroup.
|
||||
func SetToGroup(agent, gid string) bool {
|
||||
if agent == "" || gid == "" {
|
||||
return false
|
||||
}
|
||||
gid = provider.CanonicalRef(gid)
|
||||
for _, v := range appliedOf(agent).Fields {
|
||||
if g := groupNamed(v); g != "" && provider.CanonicalRef(g) == gid {
|
||||
return true
|
||||
}
|
||||
if i := strings.LastIndex(v, ":"); i > 0 && slices.Contains(provider.MemberEfforts, strings.ToLower(v[i+1:])) {
|
||||
if g := groupNamed(v[:i]); g != "" && provider.CanonicalRef(g) == gid {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ func Reseat(change func() error) ([]Move, error) {
|
||||
}
|
||||
var to Option
|
||||
if f.Options != nil {
|
||||
to = sameModel(f.Options(vals), p.ref)
|
||||
to = sameModel(f.Options(vals), p.ref, p.a.ID)
|
||||
}
|
||||
m := Move{Agent: p.a.Name, Field: f.Label, From: p.ref, To: to.Ref}
|
||||
// the same model elsewhere keeps the agent's suffix after it (omp's
|
||||
@@ -148,9 +148,10 @@ func served(ref string) bool {
|
||||
// (claude-sonnet-4.5 and claude-sonnet-4-5-20250929). For a group magpie
|
||||
// found (group/auto-…), gone as found groups are turned off, a provider
|
||||
// is kept out of them, or its model is down to one provider magpie has, it
|
||||
// is that model from the first provider still serving it; none for any other routing group, which is
|
||||
// nobody else's.
|
||||
func sameModel(opts []Option, ref string) Option {
|
||||
// is that model from the agent's own subscription when it serves it (agent
|
||||
// is the agent's id; freefcw on X), else from the first provider still
|
||||
// serving it; none for any other routing group, which is nobody else's.
|
||||
func sameModel(opts []Option, ref, agent string) Option {
|
||||
// a group's id carries Claude Code's [1m] mark as a model's does, and
|
||||
// this ref comes from the agent's own settings
|
||||
if gid, ok := provider.GroupIDOf(ref); ok {
|
||||
@@ -160,7 +161,7 @@ func sameModel(opts []Option, ref string) Option {
|
||||
// a provider the user keeps out of found groups last
|
||||
// (provider.SetAutoGroupSkip): skipping it is what took the group
|
||||
skip := provider.AutoGroupSkip()
|
||||
var kept Option
|
||||
var first, kept Option
|
||||
for _, o := range opts {
|
||||
if o.Ref == "" || provider.IsGroupRef(o.Ref) || !served(o.Ref) {
|
||||
continue
|
||||
@@ -169,13 +170,19 @@ func sameModel(opts []Option, ref string) Option {
|
||||
if provider.AutoGroupOf(pid, m) != gid {
|
||||
continue
|
||||
}
|
||||
if !slices.Contains(skip, pid) {
|
||||
if provider.IsOwn(pid, agent) {
|
||||
return o
|
||||
}
|
||||
if first.Ref == "" && !slices.Contains(skip, pid) {
|
||||
first = o
|
||||
}
|
||||
if kept.Ref == "" {
|
||||
kept = o
|
||||
}
|
||||
}
|
||||
if first.Ref != "" {
|
||||
return first
|
||||
}
|
||||
return kept
|
||||
}
|
||||
pid, model, ok := strings.Cut(ref, "/")
|
||||
|
||||
@@ -45,15 +45,15 @@ func TestSameModelTakesTheOneMMarkOffAGroupRef(t *testing.T) {
|
||||
}
|
||||
}
|
||||
opts := []Option{{Value: "magpie/group/auto-gpt-5", Ref: "group/auto-gpt-5"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
|
||||
if got := sameModel(opts, "group/auto-gpt-5[1m]"); got.Ref != "other/gpt-5" {
|
||||
if got := sameModel(opts, "group/auto-gpt-5[1m]", ""); got.Ref != "other/gpt-5" {
|
||||
t.Errorf("[1m] on the group: %+v", got)
|
||||
}
|
||||
if got := sameModel(opts, "group/auto-gpt-5"); got.Ref != "other/gpt-5" {
|
||||
if got := sameModel(opts, "group/auto-gpt-5", ""); got.Ref != "other/gpt-5" {
|
||||
t.Errorf("the bare group: %+v", got)
|
||||
}
|
||||
// a group that is nobody else's still gives nothing (nothing to move to)
|
||||
none := []Option{{Value: "magpie/group/mine", Ref: "group/mine"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
|
||||
if got := sameModel(none, "group/mine[1m]"); got.Ref != "" {
|
||||
if got := sameModel(none, "group/mine[1m]", ""); got.Ref != "" {
|
||||
t.Errorf("a group of the user's own: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,6 +418,9 @@ func run(args []string) error {
|
||||
// a model Claude Code names that magpie doesn't serve goes to the one
|
||||
// it is set to use for that tier
|
||||
gateway.StandIn = agent.StandIn
|
||||
// a bare name an agent asks for goes to its own subscription, unless
|
||||
// magpie set the agent to the group magpie found by that name
|
||||
gateway.SetToGroup = agent.SetToGroup
|
||||
// and its auto mode classifier, on a sonnet tier that follows a main
|
||||
// model /model has moved, to the model it moved to
|
||||
gateway.ClassifierStandIn = agent.ClassifierStandIn
|
||||
|
||||
@@ -128,6 +128,39 @@ func startedByAgent(r *http.Request, agent string) bool {
|
||||
// "" leaves the model as asked. Set by main.
|
||||
var StandIn func(agent, model string) string
|
||||
|
||||
// SetToGroup reports whether magpie set one of agent's fields to the
|
||||
// routing group gid ("group/<id>"): the user picked that found group for
|
||||
// the agent, so a bare model's name it asks for by (#750) stays on the
|
||||
// group rather than going to the agent's own subscription (ownFirst). Set
|
||||
// by main; nil reads as no.
|
||||
var SetToGroup func(agent, gid string) bool
|
||||
|
||||
// ownFirst is where a bare model's id agent asks for goes when the agent's
|
||||
// own subscription in magpie serves it (provider.OwnRoute; freefcw on X:
|
||||
// Codex's gpt-6-astra went to Devin through the group magpie found, and
|
||||
// with found groups off to the first provider in the order). ok is false
|
||||
// for a key held to some models, whose own rules pick, and for a found
|
||||
// group magpie set the agent to.
|
||||
func ownFirst(r *http.Request, agent, m string) (string, bool) {
|
||||
if _, held := keyHolds(r); held {
|
||||
return "", false
|
||||
}
|
||||
to, group, ok := provider.OwnRoute(agent, m)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
if group != "" && SetToGroup != nil && SetToGroup(agent, group) {
|
||||
return "", false
|
||||
}
|
||||
return to, true
|
||||
}
|
||||
|
||||
// offSpreadRefused is the error for a bare model's id several providers
|
||||
// serve, none the agent's own, while found groups are off (provider.OffSpread).
|
||||
func offSpreadRefused(model string, ids []string) string {
|
||||
return fmt.Sprintf("%q is served by %s in Magpie and auto groups are off, so Magpie picks none of them: ask for one by its provider/model id, or turn auto groups on (magpie group auto on)", model, strings.Join(ids, ", "))
|
||||
}
|
||||
|
||||
// wiresKey carries the upstream names in force on a request's context: read
|
||||
// once where the request comes in rather than once per place a name is
|
||||
// looked up, since settings.Load reads and parses the whole file every call
|
||||
@@ -1840,7 +1873,9 @@ func (s *Server) countTokens(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
model, _ = askedAt(model) // counted as the model, whatever its effort
|
||||
id := unprefixed(model)
|
||||
if sid, ok := provider.AutoStandIn(id); ok {
|
||||
if own, ok := ownFirst(r, agentOf(r), id); ok {
|
||||
id = own // counted where serve sends it
|
||||
} else if sid, ok := provider.AutoStandInFor(agentOf(r), id); ok {
|
||||
id = sid
|
||||
}
|
||||
pick := desktopSubagentPick(r, agentOf(r), named, id, body)
|
||||
@@ -2409,10 +2444,15 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// spelling of a group's model (#750) — is that group, as asked for or
|
||||
// as an agent's config names its stand-in
|
||||
grouped := func(m string) string {
|
||||
if id, ok := ownFirst(r, agent, m); ok {
|
||||
// a bare name the agent's own subscription serves: there,
|
||||
// before a group magpie found or another vendor
|
||||
return id
|
||||
}
|
||||
if id, ok := keyGroupFor(r, m); ok {
|
||||
return id
|
||||
}
|
||||
if id, ok := provider.AutoStandIn(m); ok {
|
||||
if id, ok := provider.AutoStandInFor(agent, m); ok {
|
||||
// a group magpie found, while the user has those off: its
|
||||
// model from one provider, rather than refused
|
||||
return id
|
||||
@@ -2467,6 +2507,16 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
}
|
||||
// any Claude Code subagent's turn, moved or not, for the Routing page's tag
|
||||
subagentID := subagentTurnOf(r, call.Kind, body, desktopPick != nil || subagentEffort != "")
|
||||
if _, held := keyHolds(r); !held {
|
||||
// auto groups off: a bare name several vendors serve, none the
|
||||
// agent's own, goes to none of them (freefcw on X)
|
||||
if ids := provider.OffSpread(agent, asked); len(ids) > 0 {
|
||||
call.Status, call.Error = 400, "model on several providers"
|
||||
writeError(w, from, 400, offSpreadRefused(call.Model, ids))
|
||||
turnedAway()
|
||||
return
|
||||
}
|
||||
}
|
||||
p, model, ok := provider.Resolve(asked)
|
||||
// a model the user disabled (#1867) — asked by its id, the model a
|
||||
// sub-agent was put on, a group with every model in it disabled — is
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// ownFirstHome: Codex signed in to ChatGPT, whose backend serves
|
||||
// gpt-6-luna (tierAccounts), and a relay first in the Providers order that
|
||||
// serves gpt-6-luna too, and glm-6 with a second relay. It returns the
|
||||
// requests each relay answered.
|
||||
func ownFirstHome(t *testing.T) (codexTried func() []string, relayHits func() int64) {
|
||||
t.Helper()
|
||||
tried := tierAccounts(t, nil, nil)
|
||||
var hits atomic.Int64
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.ReadAll(r.Body)
|
||||
hits.Add(1)
|
||||
io.WriteString(w, sse(
|
||||
`data: {"type":"response.created","response":{"id":"r2","model":"gpt-6-luna"}}`,
|
||||
`data: {"type":"response.output_text.delta","delta":"relay"}`,
|
||||
`data: {"type":"response.completed","response":{"id":"r2","usage":{"input_tokens":7,"output_tokens":1}}}`))
|
||||
}))
|
||||
t.Cleanup(up.Close)
|
||||
for _, p := range []provider.Provider{
|
||||
{ID: "relay", Name: "Relay", Key: "k", Responses: up.URL + "/v1", Models: []string{"gpt-6-luna", "glm-6"}},
|
||||
{ID: "relay2", Name: "Relay 2", Key: "k", Responses: up.URL + "/v1", Models: []string{"glm-6"}},
|
||||
} {
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := provider.SetOrder([]string{"relay", "relay2"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tried, func() int64 { return hits.Swap(0) }
|
||||
}
|
||||
|
||||
func postCodex(s *Server, model string) *httptest.ResponseRecorder {
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"`+model+`","stream":true,"input":"ping"}`))
|
||||
req.Header.Set("Authorization", "Bearer "+TokenFor("codex"))
|
||||
s.Handler().ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
// freefcw on X: Codex asked for gpt-6-astra by its bare name, which its
|
||||
// own ChatGPT subscription serves, and magpie sent it to Devin, through
|
||||
// the group it found (on) or as the first provider in the order (off). A
|
||||
// bare name Codex's own subscription serves now goes there, found groups
|
||||
// on or off.
|
||||
func TestBareNameGoesToTheAgentsOwnSubscription(t *testing.T) {
|
||||
codexTried, relayHits := ownFirstHome(t)
|
||||
s := New()
|
||||
for _, on := range []bool{true, false} {
|
||||
if err := provider.SetAutoGroups(on); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
rec := postCodex(s, "gpt-6-luna")
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") {
|
||||
t.Fatalf("groups on %v: %d %s", on, rec.Code, rec.Body)
|
||||
}
|
||||
if n := relayHits(); n != 0 {
|
||||
t.Fatalf("groups on %v: the relay was asked %d times", on, n)
|
||||
}
|
||||
if len(codexTried()) == 0 {
|
||||
t.Fatalf("groups on %v: Codex's own account never asked", on)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Off means off: a bare name two other vendors serve, none of them Codex's
|
||||
// own subscription, is refused for Codex with what to ask for instead,
|
||||
// rather than sent to the first of them. Named by its provider, it goes.
|
||||
func TestBareNameOnSeveralVendorsRefusedWhileGroupsOff(t *testing.T) {
|
||||
_, relayHits := ownFirstHome(t)
|
||||
s := New()
|
||||
if err := provider.SetAutoGroups(false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := postCodex(s, "glm-6")
|
||||
if rec.Code != 400 || !strings.Contains(rec.Body.String(), "relay/glm-6") || !strings.Contains(rec.Body.String(), "magpie group auto on") {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if n := relayHits(); n != 0 {
|
||||
t.Fatalf("sent anyway, %d times", n)
|
||||
}
|
||||
if rec := postCodex(s, "relay2/glm-6"); rec.Code != 200 || relayHits() != 1 {
|
||||
t.Fatalf("named: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
// on, the found group has it
|
||||
if err := provider.SetAutoGroups(true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rec := postCodex(s, "glm-6"); rec.Code != 200 || relayHits() != 1 {
|
||||
t.Fatalf("on: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
|
||||
// A found group magpie set the agent to (SetToGroup), which Codex asks
|
||||
// for by its model's bare name (#750), stays the group's.
|
||||
func TestBareNameOfAGroupSetOnTheAgentStaysTheGroups(t *testing.T) {
|
||||
ownFirstHome(t)
|
||||
old := SetToGroup
|
||||
t.Cleanup(func() { SetToGroup = old })
|
||||
var asked []string
|
||||
SetToGroup = func(agent, gid string) bool {
|
||||
asked = append(asked, agent+" "+gid)
|
||||
return gid == "group/auto-gpt-6-luna"
|
||||
}
|
||||
r := httptest.NewRequest("POST", "/v1/responses", nil)
|
||||
if to, ok := ownFirst(r, "codex", "gpt-6-luna"); ok {
|
||||
t.Fatalf("set to the group, sent to %s", to)
|
||||
}
|
||||
if len(asked) != 1 || asked[0] != "codex group/auto-gpt-6-luna" {
|
||||
t.Fatalf("asked %v", asked)
|
||||
}
|
||||
SetToGroup = func(string, string) bool { return false }
|
||||
if to, ok := ownFirst(r, "codex", "gpt-6-luna"); !ok || to != "codex/gpt-6-luna" {
|
||||
t.Fatalf("not set to it: %q %v", to, ok)
|
||||
}
|
||||
}
|
||||
@@ -791,7 +791,13 @@ func mergeKey(pid, model string, same map[string]string) string {
|
||||
// provider. ok is false for any other id, a group the user has of that id,
|
||||
// one they removed, and while found groups are on, one there is or that
|
||||
// isn't gone for a provider kept out.
|
||||
func AutoStandIn(id string) (string, bool) {
|
||||
func AutoStandIn(id string) (string, bool) { return AutoStandInFor("", id) }
|
||||
|
||||
// AutoStandInFor is AutoStandIn for a request of agent's: the agent's own
|
||||
// subscription first when it serves the model (freefcw on X: off, Codex's
|
||||
// gpt-6-astra went to Devin, the first provider in the order), then the
|
||||
// first provider as AutoStandIn picks.
|
||||
func AutoStandInFor(agent, id string) (string, bool) {
|
||||
gid, ok := GroupIDOf(id)
|
||||
if !ok || !strings.HasPrefix(gid, "auto-") {
|
||||
return "", false
|
||||
@@ -810,18 +816,24 @@ func AutoStandIn(id string) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
var first string
|
||||
var first, kept string
|
||||
for _, e := range entries {
|
||||
if AutoGroupOf(e.Provider.ID, e.Model) != gid {
|
||||
continue
|
||||
}
|
||||
if !slices.Contains(f.AutoGroupSkip, e.Provider.ID) {
|
||||
if ownAgent(e.Provider, agent) {
|
||||
return e.ID, true
|
||||
}
|
||||
if kept == "" && !slices.Contains(f.AutoGroupSkip, e.Provider.ID) {
|
||||
kept = e.ID
|
||||
}
|
||||
if first == "" {
|
||||
first = e.ID
|
||||
}
|
||||
}
|
||||
if kept != "" {
|
||||
return kept, true
|
||||
}
|
||||
return first, first != ""
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// An agent's own subscription first (freefcw on X): Codex, signed in to
|
||||
// ChatGPT and started by another app (Mirasim), asked for gpt-6-astra by
|
||||
// its bare name. Devin and Cursor served it too, so the name was the group
|
||||
// magpie found of it (auto-gpt-6-astra), which put the subscription that
|
||||
// renews soonest first: Devin. With found groups off the name went to the
|
||||
// first provider in the Providers order that served it, Devin again when
|
||||
// it was added first.
|
||||
//
|
||||
// A bare model's id an agent asks for, which that agent's own subscription
|
||||
// in magpie serves (a provider whose Account.Agent is the agent: Codex's
|
||||
// ChatGPT accounts for Codex, Claude's for Claude Code), now goes to that
|
||||
// subscription, found groups on or off. What stays as it was:
|
||||
// - a group the user made, or a found one they changed (no longer Auto),
|
||||
// named by the bare id: theirs, routed as they set it;
|
||||
// - a found group magpie set one of the agent's fields to (the gateway's
|
||||
// SetToGroup asks the agent's record): the user picked it for the agent;
|
||||
// - an id with a provider in it, or a group's ref (auto/<model>,
|
||||
// group/<id>): already where the user said;
|
||||
// - an agent with no subscription of its own in magpie, or one that
|
||||
// doesn't serve the model.
|
||||
//
|
||||
// With found groups off, a bare id several providers serve, none of them
|
||||
// the asking agent's own subscription, is refused for an agent that has
|
||||
// one in magpie (OffSpread): off, magpie picks no vendor for it.
|
||||
|
||||
// ownAgent is whether a provider is agent's own subscription: one of
|
||||
// the agent's sign-ins that magpie serves. Plugins sign in as "plugin",
|
||||
// never an agent's id.
|
||||
func ownAgent(p Provider, agent string) bool {
|
||||
return agent != "" && agent != "plugin" && p.Account != nil && p.Account.Agent == agent
|
||||
}
|
||||
|
||||
// IsOwn reports whether the provider pid is agent's own subscription.
|
||||
func IsOwn(pid, agent string) bool {
|
||||
if agent == "" || agent == "plugin" {
|
||||
return false
|
||||
}
|
||||
p, err := findIn(sharedAll(), pid)
|
||||
return err == nil && ownAgent(*p, agent)
|
||||
}
|
||||
|
||||
// bareModelID is id without Claude Code's [1m] mark, and whether it is a model's
|
||||
// id with no provider in it and no group's ref.
|
||||
func bareModelID(id string) (string, bool) {
|
||||
id = strings.TrimSuffix(strings.TrimSpace(id), "[1m]")
|
||||
if id == "" || strings.Contains(id, "/") || IsGroupRef(id) {
|
||||
return id, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
// OwnRoute is where a bare model's id agent asks for goes on the agent's
|
||||
// own subscription: its "provider/model". group is the found group's ref
|
||||
// ("group/auto-…") the id named, which the request now goes past, "" when
|
||||
// it named none (found groups off, or the group removed). ok is false when
|
||||
// the id isn't bare, the user has a group of their own by it, or no
|
||||
// subscription of the agent's serves it.
|
||||
func OwnRoute(agent, id string) (to, group string, ok bool) {
|
||||
if agent == "" || agent == "plugin" {
|
||||
return "", "", false
|
||||
}
|
||||
if _, bare := bareModelID(id); !bare {
|
||||
return "", "", false
|
||||
}
|
||||
entries := providerEntries()
|
||||
return ownRouteIn(entries, groupsIn(entries), agent, id)
|
||||
}
|
||||
|
||||
func ownRouteIn(entries []Entry, all []Group, agent, id string) (to, group string, ok bool) {
|
||||
id, bare := bareModelID(id)
|
||||
if !bare {
|
||||
return "", "", false
|
||||
}
|
||||
if gid, found := groupForIn(all, id); found {
|
||||
g, _ := groupOf(all, strings.TrimPrefix(gid, GroupPrefix))
|
||||
if !g.Auto {
|
||||
return "", "", false // the user's own group: routed as they set it
|
||||
}
|
||||
group = gid
|
||||
}
|
||||
own := ownEntry(entries, agent, id)
|
||||
if own == nil {
|
||||
return "", "", false
|
||||
}
|
||||
return own.ID, group, true
|
||||
}
|
||||
|
||||
// ownEntry is the agent's own subscription's entry for the model id: by
|
||||
// that id, else by the name vendors agree on (sameModel), as a found group
|
||||
// merges it. nil for none.
|
||||
func ownEntry(entries []Entry, agent, id string) *Entry {
|
||||
var alike *Entry
|
||||
k := sameModel(id)
|
||||
for i := range entries {
|
||||
e := &entries[i]
|
||||
if e.Group != "" || !ownAgent(e.Provider, agent) {
|
||||
continue
|
||||
}
|
||||
if e.Model == id {
|
||||
return e
|
||||
}
|
||||
if alike == nil && sameModel(e.Model) == k {
|
||||
alike = e
|
||||
}
|
||||
}
|
||||
return alike
|
||||
}
|
||||
|
||||
// OffSpread is the providers' ids ("provider/model") serving a bare model
|
||||
// id that a request of agent's goes to none of, while found groups are
|
||||
// off: several providers serve it, none of them the agent's own
|
||||
// subscription, and the agent has one in magpie. Off, magpie picks none of
|
||||
// them for the agent (freefcw on X); the request names one. nil when found
|
||||
// groups are on, the id isn't bare or names a group of the user's, one
|
||||
// provider serves it, or the agent has no subscription of its own here.
|
||||
func OffSpread(agent, id string) []string {
|
||||
if agent == "" || agent == "plugin" || AutoGroupsOn() {
|
||||
return nil
|
||||
}
|
||||
if _, bare := bareModelID(id); !bare {
|
||||
return nil
|
||||
}
|
||||
if !slices.ContainsFunc(sharedAll(), func(p Provider) bool { return ownAgent(p, agent) }) {
|
||||
return nil
|
||||
}
|
||||
entries := providerEntries()
|
||||
return offSpreadIn(entries, groupsIn(entries), heldSettings().ModelSameAs, agent, id)
|
||||
}
|
||||
|
||||
func offSpreadIn(entries []Entry, all []Group, same map[string]string, agent, id string) []string {
|
||||
id, _ = bareModelID(id)
|
||||
if _, found := groupForIn(all, id); found {
|
||||
return nil
|
||||
}
|
||||
if ownEntry(entries, agent, id) != nil {
|
||||
return nil
|
||||
}
|
||||
k := sameModel(id)
|
||||
var out []string
|
||||
var seen []string
|
||||
for _, e := range entries {
|
||||
if e.Group != "" || mergeKey(e.Provider.ID, e.Model, same) != k && e.Model != id {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(seen, e.Provider.ID) {
|
||||
continue
|
||||
}
|
||||
seen = append(seen, e.Provider.ID)
|
||||
out = append(out, e.ID)
|
||||
}
|
||||
if len(out) < 2 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ownEntries: Codex's ChatGPT account, Devin's plugin and a relay all
|
||||
// serve gpt-6-astra (freefcw on X), the relay first in the order; only the
|
||||
// relay and Devin serve glm-6.
|
||||
func ownEntries() []Entry {
|
||||
codex := Provider{ID: "codex", Account: &Account{Agent: "codex", User: "a@example.com"}}
|
||||
devin := Provider{ID: "devin-plugin", Account: &Account{Agent: "plugin"}}
|
||||
relay := Provider{ID: "relay"}
|
||||
e := func(p Provider, m string) Entry { return Entry{ID: p.ID + "/" + m, Model: m, Provider: p} }
|
||||
return []Entry{
|
||||
e(relay, "gpt-6-astra"), e(relay, "glm-6"),
|
||||
e(devin, "gpt-6-astra"), e(devin, "glm-6"),
|
||||
e(codex, "gpt-6-astra"),
|
||||
}
|
||||
}
|
||||
|
||||
// A bare name the asking agent's own subscription serves goes there,
|
||||
// whether found groups are on (a group of it found) or off (none), and
|
||||
// says the found group it went past; a group the user made by that name,
|
||||
// a provider's id, a group's ref and another agent's request don't.
|
||||
func TestOwnRoute(t *testing.T) {
|
||||
entries := ownEntries()
|
||||
on := autoGroups(entries, nil)
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
groups []Group
|
||||
agent, id string
|
||||
to, group string
|
||||
}{
|
||||
{"on", on, "codex", "gpt-6-astra", "codex/gpt-6-astra", "group/auto-gpt-6-astra"},
|
||||
{"off", nil, "codex", "gpt-6-astra", "codex/gpt-6-astra", ""},
|
||||
{"1m mark", on, "codex", "gpt-6-astra[1m]", "codex/gpt-6-astra", "group/auto-gpt-6-astra"},
|
||||
{"another agent", on, "claude", "gpt-6-astra", "", ""},
|
||||
{"no agent", on, "", "gpt-6-astra", "", ""},
|
||||
{"a plugin", on, "plugin", "gpt-6-astra", "", ""},
|
||||
{"not its own", on, "codex", "glm-6", "", ""},
|
||||
{"provider named", on, "codex", "relay/gpt-6-astra", "", ""},
|
||||
{"group ref", on, "codex", "group/auto-gpt-6-astra", "", ""},
|
||||
{"auto ref", on, "codex", "auto/gpt-6-astra", "", ""},
|
||||
{"the user's group", []Group{{ID: "gpt-6-astra", Name: "Mine", Members: []string{"relay/gpt-6-astra"}}}, "codex", "gpt-6-astra", "", ""},
|
||||
} {
|
||||
to, group, ok := ownRouteIn(entries, c.groups, c.agent, c.id)
|
||||
if to != c.to || group != c.group || ok != (c.to != "") {
|
||||
t.Errorf("%s: %q %q %v, want %q %q", c.name, to, group, ok, c.to, c.group)
|
||||
}
|
||||
}
|
||||
// spelt otherwise by the vendor (sameModel), still its own
|
||||
dotted := []Entry{{ID: "codex/gpt-6.1-sol", Model: "gpt-6.1-sol", Provider: Provider{ID: "codex", Account: &Account{Agent: "codex"}}}}
|
||||
if to, _, ok := ownRouteIn(dotted, nil, "codex", "gpt-6-1-sol"); !ok || to != "codex/gpt-6.1-sol" {
|
||||
t.Errorf("by its vendor's spelling: %q %v", to, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// With found groups off, a bare name several providers serve, none of
|
||||
// them the agent's own, names them all; one the agent's own serves, one
|
||||
// provider serves, or a group of the user's has, names none.
|
||||
func TestOffSpread(t *testing.T) {
|
||||
entries := ownEntries()
|
||||
if got := offSpreadIn(entries, nil, nil, "codex", "glm-6"); !slices.Equal(got, []string{"relay/glm-6", "devin-plugin/glm-6"}) {
|
||||
t.Errorf("glm-6: %v", got)
|
||||
}
|
||||
if got := offSpreadIn(entries, nil, nil, "codex", "gpt-6-astra"); got != nil {
|
||||
t.Errorf("its own serves it: %v", got)
|
||||
}
|
||||
one := entries[:2]
|
||||
if got := offSpreadIn(one, nil, nil, "codex", "glm-6"); got != nil {
|
||||
t.Errorf("one provider: %v", got)
|
||||
}
|
||||
mine := []Group{{ID: "glm-6", Name: "Mine", Members: []string{"relay/glm-6"}}}
|
||||
if got := offSpreadIn(entries, mine, nil, "codex", "glm-6"); got != nil {
|
||||
t.Errorf("the user's group: %v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user