Files
magpie/internal/provider/native_endpoints.go
T
yetone 8564238065 remote magpie: a model is asked on the API the remote asks it on best (OoopsHh on X)
With another magpie as the provider, Pi's models.json had only codex/*
on openai-responses; an OpenAI key's openai/* and a routing group's
auto/* went on openai-completions, and effort max was turned away there
while Responses takes it.

Cause: the remote's /v1/models gives native_endpoints, every API the
model goes on as it is, in its provider's speaking order (Chat first),
and nothing for a routing group. Here Provider.Native took the first one
the remote magpie provider speaks: Chat, for every model but a ChatGPT
sign-in's (listed on /v1/responses alone). The remote's own choice
(Native: ResponsesFirst or the Base URL's API; a group's GroupAPI) was
lost on the wire.

Now the remote's list (X-Magpie-Drawers) says magpie_prefers, the path
it asks the model on best (provider.PreferredEndpoint). The local magpie
keeps it as catalog.Model.Prefers and Native takes it first for a remote
magpie (RemotePrefers), so every agent wired one API per model (Pi, omp,
droid, dsh, grok, hermes, liveagent through catalog.Model.APIs) asks it
there, and the gateway's usable translates another API's request to it
once instead of to Chat. A user's own API set on the model still wins;
a remote from before says nothing and stays as it was.

Not changed: a Chat request a client sends itself for such a model is
still relayed as Chat to the remote, which decides as it does for its
own clients.

Run (mote, at this tree):
- TestRemoteMagpiePrefersResponses (gateway: two magpies on one
  gateway, a fake vendor that turns away reasoning_effort max on Chat)
  and TestPiAsksARemoteMagpiesModelWhereItPrefers (agent): green; both
  red with the RemotePrefers check in Native and usable turned off
  (openai/* and group/auto on chat, Pi's api nil, a Messages request
  relayed as Chat).
- Two real magpie serve processes in sandbox HOMEs, a fake vendor, an
  OpenAI key there with its Base URL given as Responses, a group auto
  and the first magpie as the second's Remote magpie: Pi's models.json
  has office/openai/gpt-6.2-astra and office/group/auto on
  openai-responses with max, and /v1/responses effort max reaches the
  vendor's /v1/responses for both. The same script at the base commit
  wrote both with no api (the provider's openai-completions).
- At base 9bf5848c1: go test ./internal/catalog ./internal/provider
  ./internal/agent ./internal/cli: ok. ./internal/gateway: two reds,
  neither this change's. TestKeptBodyIsOnlyItsCallers is fixed on main
  by e2e8ea253 and passes at this head. TestOpenCodeGoOwnFreeModel-
  GetsNothingOfOpenCodes is red the same on origin/main 975799a06, 3 of
  3 (#7117). gofmt -l: nothing. go vet, GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: ok.
- At this head, rebased on 975799a06: the tests above, plus the TestPi,
  TestRemoteMagpie, Native, Prefers, Unique and TestKeptBody runs of
  gateway, agent, catalog and provider: ok.

Not tried: the whole gateway package at this head, the full ./... suite (mote stayed at load 30+ with other
sessions' runs, mica's disk is full), a real OpenAI key, and the
vendor's own Chat refusal of max, which a fake stands in for. Pi
itself was not run; its models.json was read.
2026-11-19 17:28:26 +08:00

110 lines
3.7 KiB
Go

package provider
import "slices"
// endpointOf is the gateway path a protocol is asked at.
var endpointOf = map[Protocol]string{Chat: "/v1/chat/completions", Responses: "/v1/responses", Anthropic: "/v1/messages"}
// NativeEndpoints are the paths a request for the model is relayed on to
// its provider as it is (/v1/models' and an agent plugin's connect's
// "native_endpoints"): the APIs the provider serves it on. A routing
// group's are GroupEndpoints, which need its members; none here. None
// either for a model every request to is translated anyway (a
// subscription served through its agent's own API).
func NativeEndpoints(e Entry) []string {
if e.Group != "" {
return nil
}
return endpointsOf(relayed(*e.Provider, e.Model))
}
// PreferredEndpoint is the path a request for the model is best asked on
// (/v1/models' magpie_prefers, for another magpie that has this one as its
// provider): its provider's Native API, or for a routing group the one
// GroupAPI wires an agent to, Responses where its members are best there.
// "" for a model translated anyway, or a group that stays on Chat.
// members is nil when the group's aren't known.
func PreferredEndpoint(e Entry, members func(id string) []Member) string {
if e.Group == "" {
return endpointOf[e.Provider.Native(e.Model)]
}
if members == nil {
return ""
}
return endpointOf[GroupAPI(members(e.ID))]
}
// relayed are the APIs p takes a request for model on as it is, in the
// order p speaks them: none for a model every request to is translated
// anyway.
func relayed(p Provider, model string) []Protocol {
if p.Native(model) == "" {
return nil
}
apis := p.APIs(model)
var out []Protocol
for _, pr := range p.Speaks() {
if slices.Contains(Protocols, pr) && (apis == nil || slices.Contains(apis, pr)) {
out = append(out, pr)
}
}
return out
}
func endpointsOf(prs []Protocol) []string {
var out []string
for _, pr := range prs {
out = append(out, endpointOf[pr])
}
return out
}
// GroupEndpoints are the paths a request for a routing group is relayed
// on as it is to whichever of its members ms answers it: the APIs every
// member takes it on. None when a member translates every request anyway,
// or the members share no API.
func GroupEndpoints(ms []Member) []string {
var common []Protocol
for i, m := range ms {
r := relayed(m.Provider, m.Model)
if i == 0 {
common = r
continue
}
common = slices.DeleteFunc(common, func(pr Protocol) bool { return !slices.Contains(r, pr) })
}
return endpointsOf(common)
}
// GroupAPI is the API an agent that asks each model on one API is wired
// to ask a routing group on, "" for Chat. It is Responses when a member
// is served best there (a ChatGPT sign-in, GPT on OpenAI's API or
// Copilot's, a relay given as a Responses URL) and every other member
// takes a Responses request as it is too, or translates every request
// anyway (a Claude Code subscription): no member is then translated that
// Chat would have relayed, and the ones best on Responses are relayed
// instead of translated from Chat (zyh6036 on X: a group of
// subscriptions that all serve Responses was asked on Chat, as only a
// group whose members were all best there went on Responses, #1594). A
// group with a member Responses doesn't reach (a Chat-only key, a
// Messages-only Claude) stays on Chat, so that member keeps being
// relayed; a Messages-only group stays on Chat too, as the thinking a
// Claude member takes is known by its own model, not by the group's id.
func GroupAPI(ms []Member) Protocol {
best := false
for _, m := range ms {
n := m.Provider.Native(m.Model)
if n == "" {
continue
}
if !slices.Contains(relayed(m.Provider, m.Model), Responses) {
return ""
}
best = best || n == Responses
}
if best {
return Responses
}
return ""
}