Files
magpie/internal/agent/route.go
T
yetone b6467c9551 codex: Ultra on GPT-6.1 Sol wherever it is served, Copilot's too, with Codex's agents taking parts of the task (#656) — Codex offers Ultra on a model whose entry lists "ultra", and on a thread at multi-agent V2 hands parts of the task to agents of its own; its catalog (models-manager/models.json) lists it for gpt-6.1-sol, gpt-6-sol, gpt-6-astra, gpt-5.6-sol and gpt-5.6-terra, not the Lunas, which stop at max. magpie listed Copilot's gpt-6.1-sol only up to max, so Codex had no Ultra for it. Now an entry for one of those models (by the id the vendor serves: openai/…, dated snapshots) that reaches max lists ultra too, in Codex's catalog and /v1/models, and its Codex entry says multi_agent_version v2, as Codex's own does; GPT-6 Luna still stops at max. A ChatGPT account's entry stays as the backend lists it, and a group with a ChatGPT account in it offers ultra but isn't told V2 (its lead's subagent tasks come sealed, #141); the "subagents on any model" setting still stamps v1 on OpenAI's entries. Ultra is no API's level: the gateway now always sends max for it (fitEffort), even to a model whose list says ultra. Subagent tasks with web_search kept (the translated path) were fixed in v0.1.689 (#613); TestCopilotUltraSubagentWithWebSearch now covers it against a stub Copilot: the lead's spawn_agent comes back with plain arguments and encrypted_function_args [], no "encrypted" goes upstream, and the subagent's agent_message reaches Copilot as text. TestCopilotSolOffersUltra (sol has ultra in /v1/models and Codex's catalog with v2, luna not; ultra goes to Copilot as max, with and without web_search) and TestUltraIsSentAsMax fail on the old code; TestUltraOnModelsThatOfferIt and TestUltraEntriesSayV2 cover entries, groups and the catalog. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried in the Codex App with a real Copilot account.
2026-10-03 16:08:26 +08:00

135 lines
4.4 KiB
Go

package agent
import (
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// Every agent's model picker has two halves: the models the agent reaches
// on its own (its sign-in, its own keys), and magpie's catalog — every model
// of every provider the user added, reached through the local gateway.
// A catalog model is spelled "provider/model"; an agent that sends that to
// the gateway gets the vendor's reply in whichever API the agent speaks.
// magpieID is the provider id agents know the gateway by.
const magpieID = "magpie"
// RoutingGroups is the picker's group of routing groups.
const RoutingGroups = "Routing groups"
// viaMagpie lists the catalog as agent is shown it, for a picker: the
// routing groups first, in one group of their own, then one group per
// provider.
func viaMagpie(agent, prefix string) []Option {
var out, groups []Option
shown, _ := provider.CatalogFor(agent)
// own: whether a provider is the account the agent is signed in to,
// asked once a provider (OwnPaused reads the saved logins); a plugin's
// for the agent's own vendor (Grok moved onto its plugin) is when it
// is the account the agent itself is signed in to
own := map[string]bool{}
for _, e := range shown {
a := e.Provider.Account
if a == nil || a.StandIn() {
continue
}
if _, ok := own[e.Provider.ID]; ok {
continue
}
if a.Agent == agent {
own[e.Provider.ID] = !e.Provider.OwnPaused()
} else if e.Provider.PluginProvider() == agent && a.User != "" {
own[e.Provider.ID] = a.User == provider.AgentUser(agent)
}
}
for _, e := range shown {
if e.Group != "" {
groups = append(groups, Option{Value: prefix + e.ID, Label: e.Name, Note: "routing group · via magpie",
Icon: e.Provider.Icon, Icons: e.Icons, Group: RoutingGroups, Ref: e.ID})
continue
}
note := e.Provider.Name + " · via magpie"
if a := e.Provider.Account; a != nil {
note = a.User + " · via magpie"
}
out = append(out, Option{Value: prefix + e.ID, Label: e.Name, Note: note,
Icon: e.Provider.Icon, Group: e.Provider.Name, Ref: e.ID, Free: e.Free, Rate: e.Rate, RateWas: e.RateWas, Context: e.Context, own: own[e.Provider.ID]})
}
return append(groups, out...)
}
// viaMagpieFor is viaMagpie without the agent's own account: Codex CLI going
// through magpie to its own ChatGPT login would only add a hop.
func viaMagpieFor(agentID, prefix string) []Option {
var out []Option
for _, o := range viaMagpie(agentID, prefix) {
if !strings.HasPrefix(o.Value, prefix+agentID+"/") {
out = append(out, o)
}
}
return out
}
// isMagpie reports whether a model value is a catalog reference.
func isMagpie(v string) bool {
_, _, ok := provider.Resolve(v)
return ok && strings.Contains(v, "/")
}
func firstOf(xs []string) string {
if len(xs) > 0 {
return xs[0]
}
return ""
}
// magpieModels is the catalog as agent is shown it, as catalog.Models, for
// agents that keep their own model files.
func magpieModels(agent string) []catalog.Model {
var out []catalog.Model
shown, _ := provider.CatalogFor(agent)
labels := provider.Labels(shown)
// a model magpie describes images to takes them (provider.Described)
seen := provider.Described != nil && provider.Described()
for i, e := range shown {
m := catalog.Model{ID: e.ID, Name: labels[i], Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images || seen, ImageInput: e.ImageInput, Context: e.Context, Output: e.Output, AgentsV2: e.AgentsV2}
if seen && !e.Images {
yes := true
m.ImageInput = &yes
}
// APIs is the one to ask it on for the gateway to relay the request
// as it is; none for a group, whose members may each want another
if e.Group == "" {
if n := e.Provider.Native(e.Model); n != "" {
m.APIs = []string{string(n)}
}
}
out = append(out, m)
}
return out
}
// maxTokens is the output limit an agent is handed for m, kept within the
// context window it is handed with it: models.dev lists some models' output
// above their window (deepseek-chat's 384000 against 128000). An unknown
// window leaves the output as it is.
func maxTokens(m catalog.Model) int {
if m.Context > 0 && m.Output > m.Context {
return m.Context
}
return m.Output
}
// group tags every option with a group name.
func group(name string, opts []Option) []Option {
for i := range opts {
opts[i].Group = name
}
return opts
}
func gatewayV1() string { return gateway.URL() + "/v1" }