Files
magpie/internal/gateway/codex_subagent.go
T
yetone 221e24a3c7 agents, gateway: Codex's subagent model offers every provider's models, not only ChatGPT's (#1633, laacmilan)
The Agents page's Codex "subagent model" field (settings.CodexSubagentModel,
which the gateway puts x-openai-subagent collab_spawn requests on) listed
only the ChatGPT accounts' models, and its setter refused any other
provider's. A user whose lead runs on a chat provider (Token中心, 智谱Plan)
couldn't put its subagents there.

- provider: CodexSubagentModels is Codex's shown catalog
  (CatalogFor("codex"), so hidden or unpicked models stay out), ChatGPT
  accounts first, routing groups included. SetCodexSubagentModel takes any
  provider/model or group id, refuses a bare Codex name.
  CodexSubagentUnserved says "magpie doesn't serve it now" for a non-ChatGPT
  id that isn't served. CodexAccountModel exported.
- gateway: codexSubagentPick gets the body. When the lead ran on the
  ChatGPT backend the task arrives sealed (encrypted_content), and only a
  ChatGPT account can open it: set to another provider, such a subagent
  stays on the model asked for, with Kept = sealedKept in the route trace
  (#1613's constraint; the reverse, a chat lead with a subagent on ChatGPT,
  was already handled by 149ab950e). A plain task moves, and sealed
  MESSAGE items are stripped as before.
- gui: i18n for both new reasons (zh, zh-TW, ja, de); the GUI test lists a
  chat provider's model and shows the sealed reason in every language.
- docs: agent-wiring and gateway-routing say the same.

Run:
- go test ./internal/gateway/ ./internal/agent/ ./internal/provider/
  ./internal/gui/ (ok), gofmt -l clean, GOOS=linux and windows go vet
  -tags nogui on the four packages.
- GUI node tests (codex-subagent-model, routing-kind, i18n-keys,
  codex-start-model-field, gui-ja, gui-de, gui-zh-tw) in Chromium and
  WebKit: 38/38.
- Broken by hand: without the sealed check,
  TestCodexSubagentSealedTaskStaysOffAChatProvider fails with a 400;
  options limited to ChatGPT entries, TestCodexSubagentModelTakesAnyProvider
  fails; options from Served() instead of Codex's shown catalog,
  TestAgentModelLineAllHidden and TestAgentModelsOnlyPicked fail.
- Sandbox HOME e2e with the built binary and a fake chat upstream:
  `magpie codex subagent_model token/gpt-6.1-sol` set, a bare gpt-5.5
  refused; a collab_spawn plain-task request on gpt-6-astra to
  /backend-api/codex/responses was answered by token/gpt-6.1-sol and the
  upstream got the task text.

Not tried: a real Codex CLI or ChatGPT account; the sealed case only in
unit tests.
2026-10-11 23:43:37 +08:00

91 lines
3.2 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, be able to open
// the subagent's task, 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. Any
// model opens a task handed as text (a lead on a chat provider, #1633, or
// Codex's multi-agent V1). One the ChatGPT backend sealed, its lead having
// run there, is put only on a ChatGPT account's model, as it was before
// any other could be set; on any other the subagent stays where its lead
// put it, which serve's sealed gate (sealedReader) handles as it did with
// no model set, so setting one is never worse than leaving it unset. A
// relay that sealed the lead's task (#1109) is reached that way too: the
// lead names it.
func codexSubagentPick(r *http.Request, agent, kind, asked string, body []byte) *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 !provider.CodexAccountModel(to) && sealedTaskIn(body) {
pick.Kept = sealedKept
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
}
// sealedKept is why a Codex subagent stayed on the model its lead asked
// for when the one set for its subagents can't open its task.
const sealedKept = "its task was sealed by the ChatGPT backend that answered its lead, and only a ChatGPT account can open it"
// sealedTaskIn says a request carries a sealed agent task, not only sealed
// messages: serve leaves those out with a note (withoutSealedMessages).
func sealedTaskIn(body []byte) bool {
if !hasSealedAgentMessage(body) {
return false
}
b, _ := withoutSealedMessages(body)
return hasSealedAgentMessage(b)
}