ChatGPT's images API answers every drawing, a 200 too, with the image allowance it counted: x-codex-active-limit: imagegen_premium and the window in x-codex-primary-* (window-minutes 1440, reset-at), as the reporter captured on a Pro account; Codex 0.160.1 also reads it from a family named for the limit (x-imagegen-premium-primary-*, parse_all_rate_limits). magpie read none of it on the image path: the row said "Unknown" until a refusal, and a refusal whose body names no resets_at rested the image model for quotaRest's 15 minutes, so the card said "2 minutes" of a 24-hour window. - gateway/codex_image_limits.go: codexImageHeaderLimit reads the image window from the limit's own family or, when x-codex-active-limit names an image limit, from x-codex-*; heardCodexImage keeps it per account (in memory), handed to provider.ImageHeard. - Both drawing routes hear it: magpie's own (sendWith in draw.go) and Codex's relayed /images/generations and /images/edits (codex_backend.go). - codexHeaderLimits tells no chat window when the active limit is an image one, so the 1440-minute window never lands on the 5-hour or weekly row. - restDrawer: a 429 with the heard window at 100% counts as the quota (ChatGPT's "image limit reached" read as a rate limit), and with no resets_at in the body it rests until the heard reset, capped at longestQuota. - provider.WithImageLane: the row says Available (or Used up at 100%) with that reset; a rest still says Used up. "Available" translated (zh, zh-TW, ja, de). - docs/subsystems/providers-accounts.md updated. Run (mote): at base610ae1f54, go test -count=1 -timeout 45m ./internal/gateway ./internal/provider: provider ok; gateway red only in TestRefusalAfterShownThinkingGoesToTheNext/chat_content_filter_to_ responses (#5761, refusal handover, load 61). At the pushed head's tree (rebased on54b6d4d39): gofmt -l clean; go vet darwin, GOOS=windows, GOOS=linux -tags nogui ok; -count=3 of that test, the TestCodexImage*/TestCodexRelayed*/TestCodexLimits*/TestCodexDraw*/ TestWithImageLane tests ok. GUI, chromium and webkit: codex-image-row, i18n-keys, i18n-en-no-cjk ok; 57 of the 118 quota-*/usage-* files run before the run was stopped, 51 ok, reds all filed on main: quota-refresh-order (#6180), usage-agents-hide (#3414, #6003), usage-cards-hide (#6294), usage-computers (#5224), usage-focus-steady (#3501), usage-fold (#3415, #6003). app.js is unchanged. Broken by hand, each red: without the sendWith hook, TestCodexImageRowFromA200sHeaders and TestCodexImageRefusalRests- UntilItsWindow; without the relay hook, TestCodexRelayedImageWindowHeard; without the active-limit guard in codexHeaderLimits, TestCodexImage- Headers; without the heard-reset branch, the refusal test rests 15 minutes (the reporter's symptom); without the 429 gating, it doesn't rest at all. Not tried: a real ChatGPT account's drawing (the fixtures are the reporter's captured headers); Plus and other plans' limit names beyond imagegen*/image_gen; the heard window doesn't survive a restart (the row is Unknown until the next drawing); no share or count is shown.
95 lines
3.4 KiB
Go
95 lines
3.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/lazyre"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// ChatGPT's images API tells the image allowance of the account it drew on
|
|
// in every reply, a 200 as well as a 429 (#6412, hqqqqqqqh): it names the
|
|
// limit counted, x-codex-active-limit: imagegen_premium, and the window in
|
|
// the x-codex-primary-* headers (window-minutes 1440, reset-at), or, as
|
|
// Codex 0.160.1 reads it (codex-api rate_limits.rs, parse_all_rate_limits),
|
|
// in a family named for the limit: x-imagegen-premium-primary-*, or
|
|
// x-image-gen-primary-* for image_gen. That window is the image row's
|
|
// (provider.ImageLane), never the account's 5-hour or weekly one: it is
|
|
// kept apart from them (provider.NoteCodexLimits is never told it), and a
|
|
// chat reply that names an image limit tells no chat window either.
|
|
|
|
// imageLimitName is a limit id of the image allowance: imagegen_premium,
|
|
// image_gen, as x-codex-active-limit or a header family names it.
|
|
func imageLimitName(name string) bool {
|
|
n := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(name)), "-", "_")
|
|
return strings.HasPrefix(n, "imagegen") || strings.HasPrefix(n, "image_gen")
|
|
}
|
|
|
|
// imageFamily is a header of a limit's own family's window
|
|
// (x-imagegen-premium-primary-window-minutes), with the limit's name.
|
|
var imageFamily = lazyre.New(`^x-(.+)-(?:primary|secondary)-window-minutes$`)
|
|
|
|
// codexImageHeaderLimit is the image window h tells: of the family named
|
|
// for the image limit first, else, when x-codex-active-limit names an
|
|
// image limit, of the x-codex-* family; false when it tells none.
|
|
func codexImageHeaderLimit(h http.Header, now time.Time) (provider.CodexLimit, bool) {
|
|
var fams []string
|
|
for k := range h {
|
|
if m := imageFamily.FindStringSubmatch(strings.ToLower(k)); m != nil && m[1] != "codex" && imageLimitName(m[1]) {
|
|
fams = append(fams, m[1])
|
|
}
|
|
}
|
|
if imageLimitName(h.Get("x-codex-active-limit")) {
|
|
fams = append(fams, "codex")
|
|
}
|
|
for _, fam := range fams {
|
|
if ls := headerLimitsOf(h, "x-"+fam+"-", now); len(ls) > 0 {
|
|
l := ls[0]
|
|
for _, o := range ls[1:] {
|
|
// of two windows, the fuller is the one that holds
|
|
if o.Used > l.Used {
|
|
l = o
|
|
}
|
|
}
|
|
return l, true
|
|
}
|
|
}
|
|
return provider.CodexLimit{}, false
|
|
}
|
|
|
|
// codexImageHeard is the image window each ChatGPT account's last reply
|
|
// told, by agent (as its usage is read)/lower-cased user.
|
|
var codexImageHeard = struct {
|
|
sync.Mutex
|
|
m map[string]provider.CodexLimit
|
|
}{m: map[string]provider.CodexLimit{}}
|
|
|
|
func init() { provider.ImageHeard = imageHeardOf }
|
|
|
|
// heardCodexImage keeps the image window h tells of p's ChatGPT account.
|
|
func heardCodexImage(p provider.Provider, h http.Header) {
|
|
if p.Account == nil || p.Account.Agent != "codex" || p.Account.User == "" || h == nil {
|
|
return
|
|
}
|
|
l, ok := codexImageHeaderLimit(h, time.Now())
|
|
if !ok {
|
|
return
|
|
}
|
|
codexImageHeard.Lock()
|
|
codexImageHeard.m[p.Account.UsageAgent()+"/"+strings.ToLower(p.Account.User)] = l
|
|
codexImageHeard.Unlock()
|
|
}
|
|
|
|
// imageHeardOf is the image window a reply last told of agent's account
|
|
// user, while it has not renewed: one with no reset said, or one past
|
|
// it, tells nothing of now.
|
|
func imageHeardOf(agent, user string, now time.Time) (provider.CodexLimit, bool) {
|
|
codexImageHeard.Lock()
|
|
l, ok := codexImageHeard.m[agent+"/"+strings.ToLower(user)]
|
|
codexImageHeard.Unlock()
|
|
return l, ok && l.Resets.After(now)
|
|
}
|