Codex's lead names each subagent's model itself in spawn_agent, and
Codex's [agents] config can't overrule it. The Agents page's Codex row
now has a "subagent model" square (the app's model picker, Default
first). Its options are only models a ChatGPT account in magpie serves,
because a subagent's task is sealed for ChatGPT accounts.
- settings.CodexSubagentModel holds it. It is an agent Field
(subagent_model), so the TUI/CLI, profiles, davsync's "agents" part
and WSL twins carry it too. The setter refuses another provider's
model. It keeps a ChatGPT account's model that isn't served now, so a
backup restored on another computer still applies (ApplyFields stops
at the first failed Set). POST /api/settings keeps it.
- The gateway (codexSubagentPick) moves a collab_spawn request onto it.
codexBackend does this for Codex's own bare models; serve() does it
for any other model. When no ChatGPT account on in magpie serves it,
or a held gateway key may not use it, the request stays on the model
asked for. Route.Subagent records asked/to/kept. The lead's turns,
titles, guardian and the rest are never moved. Off (the default)
changes nothing.
- Routing page: kindWhy says the subagent was put on the model set, or
stayed on its own and why. "only account on for" names the model it
went on. Strings added in zh, zh-TW, ja and de.
- docs/subsystems: gateway-routing.md and agent-wiring.md describe it.
Run (darwin, rebased on origin/main):
- gofmt -l internal/: clean.
- go vet -tags nogui on gateway, provider, agent, gui and settings:
darwin, GOOS=linux and GOOS=windows.
- go build ./...
- HOME=<temp> go test -tags nogui ./...: all ok.
TestSettingsSaveKeepsWhatItDoesNotSend flagged the new field; it is
now set there.
- GUI tests, Chromium + WebKit:
- codex-subagent-model.test.cjs: 20/20 (en, zh, zh-TW, ja, de at
1100 and 560 px).
- Also green: routing-kind, subagent-effort, codex-memories,
codex-memories-settings, i18n-keys, gui-ja, gui-de, gui-zh-tw and
click-scroll.
Failing without the change (codexSubagentPick returning nil):
- TestCodexSubagentPutOnTheModelSet
- TestCodexSubagentOnAnotherProviderPutOnTheModelSet
- TestCodexSubagentModelNotServedKeepsTheAsked
- TestCodexSubagentModelHeldKeyKeepsTheAsked
TestCodexSubagentModelLeavesOtherRequests guards the other side: the
lead's turns, titles and guardian stay untouched.
Not tried with a real Codex CLI and ChatGPT account. A subagent spawn
needs a signed-in Codex home, and this session may not touch the live
~/.codex. The fake ChatGPT backend tests cover both routes.
66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// SubagentPick is a Codex subagent's request put on the model the user
|
|
// set for Codex's subagents (provider.CodexSubagentModel), in place of
|
|
// the one its lead asked for in spawn_agent (willz on Discord). Kept says
|
|
// why it went on the model asked for instead, "" when it was put on To.
|
|
type SubagentPick struct {
|
|
Asked string `json:"asked"`
|
|
To string `json:"to"`
|
|
Kept string `json:"kept,omitempty"`
|
|
}
|
|
|
|
// Moved says the request went on To.
|
|
func (p *SubagentPick) Moved() bool { return p != nil && p.Kept == "" }
|
|
|
|
type subagentPickKey struct{}
|
|
|
|
func withSubagentPick(r *http.Request, p *SubagentPick) *http.Request {
|
|
if p == nil {
|
|
return r
|
|
}
|
|
return r.WithContext(context.WithValue(r.Context(), subagentPickKey{}, p))
|
|
}
|
|
|
|
func subagentPickOf(r *http.Request) *SubagentPick {
|
|
p, _ := r.Context().Value(subagentPickKey{}).(*SubagentPick)
|
|
return p
|
|
}
|
|
|
|
// codexSubagentPick is what is done with the model of a request: nil when
|
|
// it isn't a Codex subagent's or no model is set for them, so the request
|
|
// goes on as asked. The model set must still be served by a ChatGPT
|
|
// account (a subagent's task is sealed for them) and be one the gateway
|
|
// key may use; when it isn't, the request goes on the model asked for and
|
|
// the pick says why.
|
|
func codexSubagentPick(r *http.Request, agent, kind, asked string) *SubagentPick {
|
|
// Codex by its token or by its User-Agent (fromCodex: its CLI, exec,
|
|
// the IDE extension, the desktop app)
|
|
if agent != "codex" && !fromCodex(r.Header) || usage.PurposeOf(kind) != "kind:collab_spawn" {
|
|
return nil
|
|
}
|
|
to := provider.CodexSubagentModel()
|
|
if to == "" || to == asked {
|
|
return nil
|
|
}
|
|
pick := &SubagentPick{Asked: asked, To: to}
|
|
if why := provider.CodexSubagentUnserved(to); why != "" {
|
|
pick.Kept = why
|
|
return pick
|
|
}
|
|
if who, held := keyHolds(r); held {
|
|
if p, model, ok := provider.Resolve(to); !ok || !modelAllowed(who, p, model) {
|
|
pick.Kept = "the gateway key may not use it"
|
|
}
|
|
}
|
|
return pick
|
|
}
|