Files
magpie/internal/gateway/codex_images_pool.go
T
yetone 70836f732b gateway: Codex's image call goes over the pool, not to the account it is signed in to (Richyisaflower on X)
Codex sends its image tool's calls to the ChatGPT backend path magpie serves
(/backend-api/codex/images/generations and /images/edits). codexBackend
relayed them as they came, signed with Codex's own sign-in. With Codex kept
signed in to account A (KeepLoginAs) and the pool ordered to put B first,
text turns went to B, but every image call still spent A's image allowance,
so once A's was used up no image could be drawn while B had room.

Now, when magpie has more accounts than the sign-in (and the call carries
no API key, names no account, and is signed by the sign-in), the call is
served by the images handler on codex/<model>: drawOnAccounts goes over the
accounts in the pool's order, past one whose image allowance is spent
(restDrawer), signed with that account's own token and chatgpt-account-id.
A sign-in alone, an API key, a request signed by an account magpie lacks
and a model with a namespace are relayed as before.

Run on mote: TestDrawingFollowsThePoolNotTheKeptLogin (two fake accounts,
acct-1 answering usage_limit_reached, pool order spare first, Codex kept on
acct-1) red on the old code for the /backend-api/codex path (asked acct-1,
429), green now for it and for /v1/images/generations; TestCodexImagesRelayedWithTheSignInAlone
green. go vet darwin, windows, linux nogui ok; gofmt clean. internal/gateway
-run "Codex|Draw|Image|Keep|Relay" ran: two reds, TestAffinityKeepsAnHourCachePastFiveMinutes and
TestClaudeConversationKeepsItsAccountsCache, both ok alone and neither near this change (see issue).
Not tried: a real Codex account; Codex's real image request body (edits shape taken
from the images API's); the full suite (started after the tag).
2026-11-12 11:55:26 +08:00

53 lines
1.9 KiB
Go

package gateway
import (
"bytes"
"io"
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// codexDrawsOnPool draws Codex's own images call (its image tool, sent to
// the ChatGPT backend's /images/generations and /images/edits) on the
// accounts the pool goes over, as the images API does, when magpie has
// more than the sign-in to ask. Relayed as it came, the call spent the
// account Codex is signed in to whatever the pool's order said, and with
// that account's image allowance used up, no image was drawn while the
// text turns went to accounts with room (Richyisaflower on X). Served here
// it goes to the first account that draws, past the ones whose allowance
// is spent (drawOnAccounts), signed with that account's own token and id.
// False leaves the call to the relay: an API key, a request signed by an
// account magpie doesn't have, one named account, or a sign-in alone.
func (s *Server) codexDrawsOnPool(w http.ResponseWriter, r *http.Request, rest string, body []byte) bool {
if apiKey(r.Header) || r.Header.Get(AccountHeader) != "" {
return false
}
if acct := r.Header.Get("chatgpt-account-id"); acct != "" && acct != provider.CodexSignedInAccount() {
return false
}
p, err := provider.Find("codex")
if err != nil || p.Account == nil || p.Account.Agent != "codex" || len(p.AlsoOn()) == 0 {
return false
}
model := modelOf(body)
if model == "" {
model = "gpt-image-2"
}
if strings.Contains(model, "/") || !strings.Contains(r.Header.Get("Content-Type"), "json") {
return false
}
body = withModel(body, "codex/"+model)
r2 := r.Clone(r.Context())
r2.URL.Path, r2.URL.RawPath = "/v1"+rest, ""
r2.RequestURI = ""
r2.Body = io.NopCloser(bytes.NewReader(body))
r2.ContentLength = int64(len(body))
r2.Header.Del("Content-Encoding")
r2.Header.Del("Content-Length")
r2.Header.Set("Content-Type", "application/json")
s.images(rest == "/images/edits")(w, r2)
return true
}