Gearclickist turned on "Codex subagents on any model", but when Codex dispatched subagents only a few models could be picked. Codex itself caps the list: spawn_agent's description names only the first five show_in_picker models of its catalog (codex-rs child_config.rs MAX_SPAWN_AGENT_MODEL_OVERRIDES, multi_agents_spec.rs spawn_agent_models_description), Codex's own ahead of magpie's. Yet it takes any catalog model by name (find_spawn_agent_model_name). So the lead saw none of magpie's models. spawnAgentModels (codex_spawn_models.go) adds the rest of the catalog Codex was handed after Codex's own list, under a line of its own, at most 200. It covers spawn_agent at the top level and in Codex's multi_agent_v1 namespace, at CodexPath (codexBackend) and at /v1/responses for Codex (handle). - At CodexPath the list is Codex's own listed models less those hidden on the Agents page, then CodexListed unless Codex is held to its own. - At /v1 it is CodexCatalog under the key's allowance. - A lead the ChatGPT backend answers in multi-agent V2 seals its subagents' tasks for a ChatGPT account (#619), so it is offered Codex's own only. Only the tool's description changes, never base_instructions. With V1 on, it does change in requests to the ChatGPT backend too. Not covered: the model_catalog_in_context feature (under development, off by default) puts a five-model catalog in context, and it is not widened. Run: - go test ./internal/gateway ./internal/agent -count=1 under a sandbox HOME: ok. - TestSpawnAgentNamesEveryModel uses 40 models on a real Codex 0.162.0 request (testdata/workdir/codex-0.162.0.json). Its 4 subtests are CodexPath, /v1, a sealed V2 lead and a V1 lead, and all pass. - With spawnAgentModels returning body by hand: CodexPath, /v1 and V1 go red. - With the sealed rule taken out: V2 goes red. - go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows; gofmt -l is clean. Not verified: a real Codex lead picking one of magpie's models past the fifth from the longer list.
197 lines
6.0 KiB
Go
197 lines
6.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"cmp"
|
|
"encoding/json"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/codexcat"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Codex's spawn_agent tool names only the first five models of its
|
|
// catalog to its lead (codex-rs core/src/agent/child_config.rs
|
|
// MAX_SPAWN_AGENT_MODEL_OVERRIDES, in multi_agents_spec.rs
|
|
// spawn_agent_models_description and, with model_catalog_in_context,
|
|
// context/world_state/model_catalog.rs), Codex's own ahead of magpie's,
|
|
// though it takes any model of the catalog named in spawn_agent's model
|
|
// argument (find_spawn_agent_model_name). So a lead could put its
|
|
// subagents on five models at most, with "Codex subagents on any model"
|
|
// on or not (Gearclickist on Discord). spawnAgentModels has the tool name
|
|
// the rest of the catalog too, the models a subagent spawned from that
|
|
// lead can work on: every one, but only a ChatGPT account's when the lead
|
|
// is answered by the ChatGPT backend in multi-agent V2, which seals its
|
|
// subagents' tasks for a ChatGPT account alone (#619).
|
|
|
|
// spawnMore heads the models spawnAgentModels adds to spawn_agent's
|
|
// description; one that has it already is left as it is.
|
|
const spawnMore = "More models that may be set as `model` (Codex names only five above; magpie serves these too):"
|
|
|
|
// spawnMost is the most models spawnAgentModels adds, to keep the tool's
|
|
// description in bounds.
|
|
const spawnMost = 200
|
|
|
|
type spawnChoice struct{ slug, name string }
|
|
|
|
// spawnAgentModels is body, a Codex request at CodexPath (codexPath) or at
|
|
// /v1/responses (Codex with magpie as its model provider), with its
|
|
// spawn_agent tool naming every model of the catalog that Codex was handed
|
|
// there, not only the first five. lead is the request's model.
|
|
func spawnAgentModels(r *http.Request, body []byte, lead string, codexPath bool) []byte {
|
|
if !bytes.Contains(body, []byte(`"spawn_agent"`)) || !bytes.Contains(body, []byte("Available model overrides")) {
|
|
return body
|
|
}
|
|
choices := spawnChoices(r, lead, codexPath)
|
|
if len(choices) == 0 {
|
|
return body
|
|
}
|
|
var q map[string]json.RawMessage
|
|
if json.Unmarshal(body, &q) != nil {
|
|
return body
|
|
}
|
|
var tools []map[string]json.RawMessage
|
|
if json.Unmarshal(q["tools"], &tools) != nil {
|
|
return body
|
|
}
|
|
changed := false
|
|
for _, t := range tools {
|
|
if moreSpawnModels(t, choices) {
|
|
changed = true
|
|
continue
|
|
}
|
|
// Codex's collaboration namespace holds it
|
|
var kind string
|
|
json.Unmarshal(t["type"], &kind)
|
|
var inner []map[string]json.RawMessage
|
|
if kind != "namespace" || json.Unmarshal(t["tools"], &inner) != nil {
|
|
continue
|
|
}
|
|
in := false
|
|
for _, it := range inner {
|
|
in = moreSpawnModels(it, choices) || in
|
|
}
|
|
if in {
|
|
t["tools"] = rawJSON(inner)
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed {
|
|
return body
|
|
}
|
|
q["tools"] = rawJSON(tools)
|
|
return rawJSON(q)
|
|
}
|
|
|
|
// rawJSON is v encoded as it is, its < > & left as they are.
|
|
func rawJSON(v any) []byte {
|
|
var b bytes.Buffer
|
|
enc := json.NewEncoder(&b)
|
|
enc.SetEscapeHTML(false)
|
|
if enc.Encode(v) != nil {
|
|
return nil
|
|
}
|
|
return bytes.TrimRight(b.Bytes(), "\n")
|
|
}
|
|
|
|
// moreSpawnModels adds to t, when it is spawn_agent with a model
|
|
// argument and Codex's list of models in its description, the choices
|
|
// that list doesn't name.
|
|
func moreSpawnModels(t map[string]json.RawMessage, choices []spawnChoice) bool {
|
|
var name, kind, desc string
|
|
json.Unmarshal(t["name"], &name)
|
|
json.Unmarshal(t["type"], &kind)
|
|
json.Unmarshal(t["description"], &desc)
|
|
if name != "spawn_agent" || kind != "function" || !strings.Contains(desc, "Available model overrides") || strings.Contains(desc, spawnMore) {
|
|
return false
|
|
}
|
|
var params struct {
|
|
Properties map[string]json.RawMessage `json:"properties"`
|
|
}
|
|
if json.Unmarshal(t["parameters"], ¶ms) != nil || params.Properties["model"] == nil {
|
|
return false
|
|
}
|
|
var lines []string
|
|
for _, c := range choices {
|
|
if strings.Contains(desc, "`"+c.slug+"`") {
|
|
continue
|
|
}
|
|
line := "- `" + c.slug + "`"
|
|
if c.name != "" && c.name != c.slug {
|
|
line += ": " + c.name
|
|
}
|
|
lines = append(lines, line)
|
|
if len(lines) == spawnMost {
|
|
break
|
|
}
|
|
}
|
|
if len(lines) == 0 {
|
|
return false
|
|
}
|
|
// after Codex's own list, its header's line and a "- `slug`: …" line
|
|
// each, ahead of the text that follows it
|
|
end := strings.Index(desc, "Available model overrides")
|
|
for {
|
|
nl := strings.IndexByte(desc[end:], '\n')
|
|
if nl < 0 {
|
|
end = len(desc)
|
|
break
|
|
}
|
|
end += nl
|
|
if !strings.HasPrefix(desc[end+1:], "- `") {
|
|
break
|
|
}
|
|
end++
|
|
}
|
|
add := "\n" + spawnMore + "\n" + strings.Join(lines, "\n")
|
|
t["description"] = rawJSON(desc[:end] + add + desc[end:])
|
|
return true
|
|
}
|
|
|
|
// spawnChoices is the models of the catalog Codex was handed, at CodexPath
|
|
// or at /v1, that a subagent of a lead on lead can work on, in the
|
|
// catalog's order.
|
|
func spawnChoices(r *http.Request, lead string, codexPath bool) []spawnChoice {
|
|
sealed := !codexcat.V1() && (codexPath && !strings.Contains(lead, "/") || provider.CodexAccountModel(lead))
|
|
var out []spawnChoice
|
|
if codexPath {
|
|
// Codex's own, as its backend lists them to it, less the ones
|
|
// taken out of its list on the Agents page
|
|
hidden := provider.CodexNativeHidden()
|
|
type own struct {
|
|
spawnChoice
|
|
at int
|
|
}
|
|
var owns []own
|
|
for slug, e := range codexcat.CacheEntries() {
|
|
if v, _ := e["visibility"].(string); v != "list" || hidden[slug] {
|
|
continue
|
|
}
|
|
name, _ := e["display_name"].(string)
|
|
at, _ := e["priority"].(float64)
|
|
owns = append(owns, own{spawnChoice{slug, name}, int(at)})
|
|
}
|
|
slices.SortFunc(owns, func(a, b own) int { return cmp.Or(a.at-b.at, strings.Compare(a.slug, b.slug)) })
|
|
for _, o := range owns {
|
|
out = append(out, o.spawnChoice)
|
|
}
|
|
if sealed || codexOwnOnly() {
|
|
return out
|
|
}
|
|
for _, m := range provider.CodexListed() {
|
|
out = append(out, spawnChoice{m.ID, m.Name})
|
|
}
|
|
return out
|
|
}
|
|
shown, _ := provider.CatalogFor("codex")
|
|
for _, m := range provider.CodexCatalog(keyAllowed(r, shown)) {
|
|
if sealed && !provider.CodexAccountModel(m.ID) {
|
|
continue
|
|
}
|
|
out = append(out, spawnChoice{m.ID, m.Name})
|
|
}
|
|
return out
|
|
}
|