Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: an Antigravity drawing out of the image model's quota goes on to the next account and rests that model there (01huadalang)

yetone3387ea615678
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 673752e0e..d58387049 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -35,7 +35,7 @@ backfilled; see [Gateway Session Aggregation](gateway-sessions.md).
| Order of keys and accounts | `Routing`: smart (""), `order`, `rotate`, `usage` (least used, with an `usageHalfLife` of 1h), `pace`, `weight` or `free` (free first). `weighRouted` reads each candidate into a `routes.Seat` and asks the strategy serving its routing (`routes.Find`, the router strategy point of [the plugin runtime](plugin-runtime.md#gateway-extension-points-p3-router-strategy)) for the order: Smart is `routes.Smart`, the core's; the others are `routes.Builtin`, registered by the built-in plugin `rotation`. With it off a routing no strategy serves is tried in its listed order, and `routingOf` treats it as `order` wherever the core asks about a routing (a turn's mark, `goesRound`, `sinks`, a rotate group's sessions, a group's heads). Smart, `usage` and `pace` weigh a subscription by its allowance (`allowances`), and a key by its own usage windows when magpie reads them (`keyAllowance`: a sub2api key given a 5-hour, day or 7-day limit, or a key bought as a plan, such as Kimi Code's or a GLM Coding Plan's); any other key has none. `free` weighs by what a model costs there and nothing else: only a strategy with `routes.Strategy.ByFree` set (`routes.FreeFirst` is the one) has `weighRouted` fill `routes.Seat.Free`, from `provider.FreeModel` — the vendor's own `catalog.Model.Free` word, else a list price of $0 on every count — so a pool routed any other way never reads a model list for it, and one routed `free` spends the members whose model costs nothing before those that spend the allowance or credits, each tier in its listed order (nothing free is left behind a paid one, and a pool with no free model behaves as `order`). A group routed `free` weighs its members whole, each over its own accounts or keys as its provider routes them. A group routed `weight` (#1910) takes its members, not their keys, by smooth weighted round robin: `planLevel` makes each model member (its keys ordered by its own provider's routing) and each nested group one unit, and `weightedUnits` puts the round's pick (`routes.Round` over `group/<id>`, keyed by the member's spelling, `Group.Weights`, 1 if unset) first and the rest in member order for failover; a unit whose head rests leaves the round, while one whose accounts are only near their limit (Smart's 90% band, not resting) keeps its share, the band ordering only its own accounts (`TestWeightedGroupGivesALowGroupItsShare`; Creat.wu on Discord read the Routing page's "kept for when the others can't" as the member passed over, so the page now tells a member behind another by a group's weights or order as such, `memberSplit` in the routing plugin's `client.js`), and a kept conversation moves the round's charge to the member that kept it (`wrrSeat`, `routes.MoveRound`). A lone key or account is weighed against nobody, but what it has left is still read into the trace (`weighRouted`), so the Routing page shows it as it does for several (#1016); all but used up, it keeps its conversation rather than being said to move (`affine`). A failure rests a candidate (`failure`, `failureOf`). | [`routing.go`](../../internal/gateway/routing.go), [`weighted.go`](../../internal/gateway/weighted.go), [`internal/routes/`](../../internal/routes), [`internal/provider/routing.go`](../../internal/provider/routing.go) |
| Limits | Each key or account (a candidate's `who()`) has at most `MaxConcurrency` requests out at once (its own `AccountConcurrency` over it); one more waits in its lane's queue, bounded by `QueueLimit` and `QueueWait` and turned away with a 429 past either (`lanes.take`). `MaxRPM` (coeo91 on Discord: OpenRouter's free models take 20 a minute) bounds how many requests it sends the vendor in any rolling 60 seconds (`rpms`), counting each for a second more (`rpmSlack`), since the vendor counts a request when it arrives and the first on a new connection arrives later by its dial: one more waits for room in the minute, in the order they came, after it has its slot and right before it is sent. A wait that would be longer than 2 minutes (or the provider's `QueueWait`, when shorter: `rpmWait`) is turned away at once with a 429 whose `Retry-After` is when the minute has room; nothing rests for it. An agent gone while it waits gives its place back. Every request that reaches the vendor counts: each try of the candidate loop (a retry, the next key, a fallback) waits in `serve`, and the requests a try makes on the way and the gateway's own asks (count_tokens, System One's decisions, embeddings, drawing, Kimi's search) are counted by `rpmTransport` on `s.client` (`metered`, `paidFor`). count_tokens never waits: a candidate with no room is skipped, and the local estimate answers. A full lane or a minute with no room gives way to a ready key or account of the provider or the group (`laneMate`, `ready`); a fallback is never asked for want of either. A plugin's account sends through its own transport, so only its tries are counted, not a plugin's own extra requests. Set in the provider editor (Concurrency, Requests per minute, Queue size, Queue wait) or with `magpie provider account-concurrency`, `queue` and `rpm`; a subscription's are kept with its picks, and every account of it keeps them, those on beside the first as well (`withPicks`, called by `All` and `AlsoOn`; #1899: a second WorkBuddy AI account had none, and queued 62s with the wait set to 1s). | [`concurrency.go`](../../internal/gateway/concurrency.go), [`rpm.go`](../../internal/gateway/rpm.go), [`internal/provider/concurrency.go`](../../internal/provider/concurrency.go) |
| Sink | With `Sink` on, a key or account that answers 429 while it still has quota goes to the back, behind every one not rate limited since. Held in memory until restart. A group in a group sinks by its own Sink, within its own order (`sinkOwn`, #2255), and the group asked for then sinks the whole by its own; `TestNestedGroupSinksItsOwnOrder`. | [`sink.go`](../../internal/gateway/sink.go) |
-| Image generation | `/v1/images/generations` and `/v1/images/edits` are translated to the selected model's image API. OpenRouter image-only models use its `/images` endpoint and send reference images as `input_references`; OpenRouter models that return images through chat stay on Chat Completions. Volcengine Ark (`isArk`: an `ark.*.volces.com` base, the preset's plans or one added by hand) is asked at `/images/generations` only, as its doc (https://www.volcengine.com/docs/82379/1541523) takes it (`drawArk`): `watermark: false`, since it marks every image otherwise; reference images as JSON `image` (a URL or data URL, an array for several), never a multipart edit; no `n`, so one request per image (`eachDrawing`); no ratio as `size`, so a ratio its doc lists is sent as that ratio's 2K size and any other is a 400 that says to put it in the prompt; no mask, no quality (JL on Discord, `TestArkDrawsWithoutWatermarkOncePerImage`). A routing group named for a drawing or a video (`drawingGroup`: `group/<id>`, or a bare id the group goes by that resolved to one of its members) is tried member by member in the order its routing plans for a chat (`drawingTries` over `planGroup`), each member over its own keys or accounts (`drawOnAccounts`, `startVideo`); the next member is asked while one fails as a chat's would move on (`retryable`: its vendor 5xx, out of quota, the model not served), never for a refusal of the request itself, and the call's `Fallback` names those tried first (#2005, tianxia3111: a group of two relays answered the first one's 502). Pause rules and a gateway key's group hold apply as for a chat. Nothing rests for a failed drawing. `TestImageGroupMovesOnToTheNextMember`, `TestVideoGroupMovesOnToTheNextMember`. An account's own list of models (#474) never bars a ChatGPT account's GPT Image, a Grok subscription's Grok Imagine or a WorkBuddy plan's image models (`listsCantName`): the list is picked from the editor's chat models, which they are never among, so a list kept for some chat models had every drawing refused as "set not to be served by any account" (heyiwei5 on X). A key's list, picked from models that include its image ones, still bars them. `TestCodexDrawsPastAccountModelLists`. A drawing is given Settings → Images → Image generation timeout (`imageTimeout`, 1 to 60 minutes, 5 when unset; neighbads on X: a slow image endpoint needed more) as its context's deadline (`drawTimeout`, `drawWithin`), and so is downloading an image answered as a link (`imageLongest`, never under `ImageLongest`). A drawing's request goes through a transport with no `ResponseHeaderTimeout` (`drawsApart`), since an images API sends its headers only once it has drawn, and every other request keeps `upstreamHeaderWait` (10 minutes). A video's submit, status and download calls keep 5 minutes each (`videoTimeout`). The `magpie mcp image` process, which may not read these settings (WSL), waits on a call up to `callLongest` (two hours and a minute), so it never cuts one the gateway still waits on. `TestLongImageTimeoutLetsASlowVendorFinish`, `TestImageTimeoutSettingReachesTheVendor`, `TestOnlyDrawingsWaitPastTheHeaderWait`. | [`draw.go`](../../internal/gateway/draw.go), [`draw_volcengine.go`](../../internal/gateway/draw_volcengine.go), [`video.go`](../../internal/gateway/video.go), [`catalog.go`](../../internal/catalog/catalog.go) |
+| Image generation | `/v1/images/generations` and `/v1/images/edits` are translated to the selected model's image API. OpenRouter image-only models use its `/images` endpoint and send reference images as `input_references`; OpenRouter models that return images through chat stay on Chat Completions. Volcengine Ark (`isArk`: an `ark.*.volces.com` base, the preset's plans or one added by hand) is asked at `/images/generations` only, as its doc (https://www.volcengine.com/docs/82379/1541523) takes it (`drawArk`): `watermark: false`, since it marks every image otherwise; reference images as JSON `image` (a URL or data URL, an array for several), never a multipart edit; no `n`, so one request per image (`eachDrawing`); no ratio as `size`, so a ratio its doc lists is sent as that ratio's 2K size and any other is a 400 that says to put it in the prompt; no mask, no quality (JL on Discord, `TestArkDrawsWithoutWatermarkOncePerImage`). A routing group named for a drawing or a video (`drawingGroup`: `group/<id>`, or a bare id the group goes by that resolved to one of its members) is tried member by member in the order its routing plans for a chat (`drawingTries` over `planGroup`), each member over its own keys or accounts (`drawOnAccounts`, `startVideo`); the next member is asked while one fails as a chat's would move on (`retryable`: its vendor 5xx, out of quota, the model not served), never for a refusal of the request itself, and the call's `Fallback` names those tried first (#2005, tianxia3111: a group of two relays answered the first one's 502). Pause rules and a gateway key's group hold apply as for a chat. A failed drawing rests no member and no key. An account out of quota for the image model (`failure` reads `failQuota`) rests that model on it alone (`restDrawer`, keyed by `restID`), until the reset its refusal names (`resetsAt`: Antigravity's "Your quota will reset after 3h19m30s.") or `quotaRest`, so the next drawing asks the accounts that still have it first (`restLast`), and the account's text models stay in. Antigravity also tells a model's quota used up as a 200 whose stream is the error; `drawCodeAssist` gives such an error the status its words stand for (`saidStatus`: quota or rate 429, credit 402, else 502), as the images API's 200 with no image does, so a spent account hands the drawing to the next one as its 429 does (01huadalang on Discord: a 502 ended the drawing on the spent account; `TestAntigravityDrawingOutOfQuotaInsideTheReplyGoesOn`, `TestAntigravityDrawingQuotaRestsTheImageModel`). `TestImageGroupMovesOnToTheNextMember`, `TestVideoGroupMovesOnToTheNextMember`. An account's own list of models (#474) never bars a ChatGPT account's GPT Image, a Grok subscription's Grok Imagine or a WorkBuddy plan's image models (`listsCantName`): the list is picked from the editor's chat models, which they are never among, so a list kept for some chat models had every drawing refused as "set not to be served by any account" (heyiwei5 on X). A key's list, picked from models that include its image ones, still bars them. `TestCodexDrawsPastAccountModelLists`. A drawing is given Settings → Images → Image generation timeout (`imageTimeout`, 1 to 60 minutes, 5 when unset; neighbads on X: a slow image endpoint needed more) as its context's deadline (`drawTimeout`, `drawWithin`), and so is downloading an image answered as a link (`imageLongest`, never under `ImageLongest`). A drawing's request goes through a transport with no `ResponseHeaderTimeout` (`drawsApart`), since an images API sends its headers only once it has drawn, and every other request keeps `upstreamHeaderWait` (10 minutes). A video's submit, status and download calls keep 5 minutes each (`videoTimeout`). The `magpie mcp image` process, which may not read these settings (WSL), waits on a call up to `callLongest` (two hours and a minute), so it never cuts one the gateway still waits on. `TestLongImageTimeoutLetsASlowVendorFinish`, `TestImageTimeoutSettingReachesTheVendor`, `TestOnlyDrawingsWaitPastTheHeaderWait`. | [`draw.go`](../../internal/gateway/draw.go), [`draw_volcengine.go`](../../internal/gateway/draw_volcengine.go), [`video.go`](../../internal/gateway/video.go), [`catalog.go`](../../internal/catalog/catalog.go) |
| Affinity | A conversation stays with the key or account that answered it, so the vendor's prompt cache is read again. Modes: auto (""), `session`, `turn`, `off`, `strict`. Stored in `affinity.json` next to `providers.json`. | [`affinity.go`](../../internal/gateway/affinity.go) |
| One try | `attempt` sends one candidate the request. It goes through the proxy of the account or key it uses, else the provider's (`p.Via`, `ProxyChoice`: `AccountProxies` holds an account's by its name, a key's by its `KeyID`). A request magpie makes for one provider while serving another's (a web search, an image description, a classifier call) takes its own provider's proxy or the global one, never the outer one: `netproxy.With` replaces a choice the context already names, "" too, and `webSearch` starts from none, so the search APIs follow the global proxy (#1522). A provider of the user's set not to check its certificate (`InsecureTLS`, #2383) has it skipped for its own base URLs' hosts alone, through `p.Via` ([Providers and accounts](providers-accounts.md)). Every request to one vendor's host shares one HTTP/2 connection (`upstreamTransport`); one with no frame for 20s is pinged, and dropped when no answer comes within 10s (`netproxy.HTTP2Health`, on `http.DefaultTransport` too), so a connection gone silent without closing (a proxy's way out died, a NAT forgot the flow) fails the requests on it, which move on as a 502 does, and the next request dials a new one. A live vendor answers a ping whatever its streams are doing, so a long wait for a first byte is not cut by it (#2843: a group's requests hung with no first byte until a restart; `TestGroupMovesOnFromSilentHTTP2Connection`). A subscription with its own client is served specially, such as Claude through the genuine Claude Code. The request is relayed as is (`passthrough`) when the model is usable on the agent's API, except that a Claude model where the provider has an Anthropic URL is translated to Messages from Chat or Responses too (`OnMessages`: OpenCode speaks Chat for every model, and a relay drops cache_control there), unless the user set the model's API or the vendor's list names its APIs. An account that only streams (`Account.Stream`: a plugin's, and most subscriptions magpie signs in to) has a request that asked for one body translated, which magpie always asks streamed, except a plugin's on Responses (`gathers`): that is relayed all the same, asked with `stream:true`, and the client given the `response` of the stream's `response.completed` (or `response.incomplete`) as one JSON body (`gathered` in [`gathered.go`](../../internal/gateway/gathered.go); its `output` filled from the stream's `response.output_item.done` items, or else its text, when that event's is empty). So the tools the vendor runs itself, and its response's `tools` and usage details, reach the client (plugins#112: a translation dropped Grok's `x_search`, and Grok answered without searching; `TestGrokPluginNonStreamKeepsHostedTools`). A stream that fails or ends before that event is a 502 (`TestGrokPluginNonStreamEndedShort`), never an empty 200. A Messages request is still translated, its stream having no event that holds the whole reply, and so is a subscription magpie signs in to itself (the ChatGPT backend's, Qoder's): its own code fits its requests. A request offering web search to a provider that doesn't search by itself is translated too, so magpie can search for it (`search.go`; a provider that searches for some of its models only, Command Code's Provider API, lists them in `searchModels` by API, and none count under its `x-cmd-zdr` header, #1590), but on the agent's own API where the provider serves it and no `ResponsesFirst`/`MessagesFirst` rule prefers another: Codex offers web_search on every turn, and each went out as Chat (#997). Otherwise it is translated (`translate`) to the first API `usable` gives: Chat, except Responses for OpenAI's models where their maker serves them (`ResponsesFirst`), Responses for every model at ModelScope, whose Chat API reports neither the tokens its cache answered nor reasoning while its Responses API reports both (`ResponsesTellsCache`, #1578; a 404 there sends the model to Chat, and only there), and Anthropic's Messages for a Claude model where the provider has an Anthropic URL (`MessagesFirst`, which keeps its cache breakpoints, #997). An API that answers the model is not served there (`wrongEndpoint`) is remembered in `unfit` until magpie restarts. A Chat API that refuses function tools with reasoning, with no other API to take them to, is asked once more with `reasoning_effort` `none` (#176), unless `none` is known refused (`turnedAway`); `none` refused naming the levels, the tools refusal is the answer, not a retry at the lowest level (#2670). When every API has turned the model away, the API the user picked for it (`ModelAPI`) is asked again, and without a pick each API the provider has a URL for (`Speaks`), Chat first. `Provider.Native`, the API agents with a per-model API (Pi, omp, droid) are wired to ask on, follows the same order. A custom provider with a Gemini URL (#1346) has Gemini last among the APIs it speaks: a Gemini client's request is relayed there, and any other is translated to it when it has no other, or when the user set Gemini as the model's API (`SetModelAPI` takes chat, responses, anthropic or gemini, each only where the provider has its URL; a model set to Gemini has no `Native`, so those agents ask it on their own API and it is translated). Upstream it is always asked `models/{model}:streamGenerateContent?alt=sse` under the base (`upstreamPath`), the model only in the path (`forwardOnce` drops the body's), so a Gemini client that asked for one JSON body is translated, as for Factory's generate route. | [`gateway.go`](../../internal/gateway/gateway.go) |
| Trace | The Routing view's record of each decision, written where the decision is made: candidates, rests, which try answered. `Route.Folder` is the folder the agent says it works in (`workDir`), its last two levels only (`shortFolder`, e.g. `workspace/magpie`), never the full path; a request that says none (a title, a topic check) takes its session's as said last (`trace.sessionFolder`), one without a session id has none (#1068). It is kept in the routing history on disk for as long as the route is (`history.go`: 30 days, 200 MB). The GUI shows it in By session's headings, a request row's tooltip, the stage's agent working in more than one folder, and the tray panel's agent tooltip. | [`trace.go`](../../internal/gateway/trace.go) |
diff --git a/internal/gateway/antigravity_draw_test.go b/internal/gateway/antigravity_draw_test.go
new file mode 100644
index 000000000..c13f17f3d
--- /dev/null
+++ b/internal/gateway/antigravity_draw_test.go
@@ -0,0 +1,120 @@
+package gateway
+
+import (
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+)
+
+// agDrawer is the image model the tests' Antigravity accounts list.
+const agDrawer = "gemini-3.1-flash-image"
+
+// drawnPNG is Code Assist's reply with one image drawn.
+const drawnPNG = `data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"inlineData":{"mimeType":"image/png","data":"iVBORw0KGgo="}}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3}}}`
+
+// antigravityDrawers is two Antigravity accounts listing agDrawer, the
+// first answering spent as spent says; the second draws. asked is who was
+// asked, in order.
+func antigravityDrawers(t *testing.T, spent func(w http.ResponseWriter)) (*Server, *[]string) {
+ t.Helper()
+ asked := new([]string)
+ s, _ := antigravityGroupFor(t, []string{"[email protected]", "[email protected]"}, agDrawer, func(w http.ResponseWriter, r *http.Request) {
+ who := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
+ *asked = append(*asked, who)
+ if who == "[email protected]" {
+ spent(w)
+ return
+ }
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse(drawnPNG))
+ })
+ return s, asked
+}
+
+func drawOn(t *testing.T, s *Server) (int, string) {
+ t.Helper()
+ rec := httptest.NewRecorder()
+ s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/images/generations",
+ strings.NewReader(`{"model":"antigravity/`+agDrawer+`","prompt":"a cat"}`)))
+ return rec.Code, rec.Body.String()
+}
+
+// Antigravity tells a model's quota used up inside the reply too: a 200
+// whose stream is the error. Told as a 502, the drawing ended on the
+// spent account while the next had its quota (01huadalang on Discord:
+// several accounts, one's image quota spent, no move to the next).
+func TestAntigravityDrawingOutOfQuotaInsideTheReplyGoesOn(t *testing.T) {
+ s, asked := antigravityDrawers(t, func(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse("data: "+antigravityQuota))
+ })
+ if code, out := drawOn(t, s); code != 200 || !strings.Contains(out, "b64_json") {
+ t.Fatalf("the drawing: %d %s (asked %v)", code, out, *asked)
+ }
+ if got := strings.Join(*asked, " "); got != "[email protected] [email protected]" {
+ t.Fatalf("asked %s", got)
+ }
+}
+
+// Any other error said inside the reply is no account's quota, and the
+// drawing still ends there: the next account isn't asked, nothing rests.
+func TestAntigravityDrawingOtherErrorInsideTheReplyStays(t *testing.T) {
+ s, asked := antigravityDrawers(t, func(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse(`data: {"error":{"code":500,"message":"Internal error encountered.","status":"INTERNAL"}}`))
+ })
+ if code, out := drawOn(t, s); code != http.StatusBadGateway {
+ t.Fatalf("the drawing: %d %s", code, out)
+ }
+ if len(*asked) != 1 {
+ t.Fatalf("asked %v", *asked)
+ }
+ if r := rests(); len(r) != 0 {
+ t.Fatalf("rests %v", r)
+ }
+}
+
+// A drawing refused for the image model's quota rests that model on the
+// account until the reset Antigravity names, not the account: the next
+// drawing asks the account that still has its quota first, where before
+// every drawing asked the spent one again, and its text models stay in.
+func TestAntigravityDrawingQuotaRestsTheImageModel(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ spent func(w http.ResponseWriter)
+ }{
+ {"as a 429", func(w http.ResponseWriter) {
+ w.WriteHeader(http.StatusTooManyRequests)
+ io.WriteString(w, antigravityQuota)
+ }},
+ {"inside the reply", func(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse("data: "+antigravityQuota))
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ s, asked := antigravityDrawers(t, tc.spent)
+ for range 2 {
+ if code, out := drawOn(t, s); code != 200 {
+ t.Fatalf("the drawing: %d %s", code, out)
+ }
+ }
+ if got := strings.Join(*asked, " "); got != "[email protected] [email protected] [email protected]" {
+ t.Fatalf("asked %s: the spent account was asked again first", got)
+ }
+ r, ok := restOf("antigravity@[email protected]/" + agDrawer)
+ if !ok || r.Why != failQuota || r.By != "resets" || r.Model != agDrawer {
+ t.Fatalf("the image model's rest: %+v %v (rests %v)", r, ok, rests())
+ }
+ if d := time.Until(r.Until); d < 3*time.Hour+19*time.Minute || d > 3*time.Hour+20*time.Minute {
+ t.Fatalf("rests %v, not until the reset Antigravity named (3h19m30s)", d)
+ }
+ if _, ok := restOf("antigravity@[email protected]"); ok {
+ t.Fatalf("the whole account rests for its image model's quota: %v", rests())
+ }
+ })
+ }
+}
diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go
index d806b75dc..16ad14a25 100644
--- a/internal/gateway/draw.go
+++ b/internal/gateway/draw.go
@@ -893,6 +893,9 @@ func (s *Server) drawOnAccounts(ctx context.Context, r *http.Request, p provider
for i, c := range cs {
p = c.p
out, code, err = s.draw(ctx, p, model, d)
+ if err != nil && ctx.Err() == nil {
+ restDrawer(c, code, err.Error())
+ }
if err == nil || ctx.Err() != nil || i+1 == len(cs) || !accountRefusedDrawing(code) && !keyLacksModel(code, err) {
break
}
@@ -900,6 +903,52 @@ func (s *Server) drawOnAccounts(ctx context.Context, r *http.Request, p provider
return p, out, code, time.Time{}, err
}
+// restDrawer rests the image model on c's account when the account is out
+// of its quota for it, until the reset the refusal names (Antigravity's
+// "Your quota will reset after 3h19m30s.", resetsAt), else quotaRest. Only
+// the model rests (restID), never the account: its text models draw on
+// allowances of their own. The next drawing then asks the accounts that
+// still have it first (restLast in candidates), and a chat on that image
+// model goes past it too, where before every drawing asked the spent
+// account first again (01huadalang on Discord). A failure of any other
+// kind rests nothing, as before: a key, a request refused, a vendor down.
+func restDrawer(c candidate, code int, msg string) {
+ if c.p.Account == nil || c.model == "" || failure(code, []byte(msg)) != failQuota {
+ return
+ }
+ now := time.Now()
+ r := Rest{Why: failQuota, Status: code, By: "quota", Key: c.restID(), Model: c.model,
+ agent: c.p.Account.UsageAgent(), user: c.p.Account.User, said: msg}
+ d := quotaRest
+ if w := resetsAt([]byte(msg), now); w > 0 {
+ d, r.By = min(w, longestQuota), "resets"
+ }
+ r.Until = now.Add(d)
+ restingUntil.Lock()
+ restingUntil.m[r.Key] = r.Until
+ restingUntil.note[r.Key] = r
+ restingUntil.Unlock()
+}
+
+// saidStatus is the status an error said inside a drawing's 200 stands
+// for, which drawOnAccounts goes by: Antigravity tells a model's quota used
+// up as the stream's error event as well as a 429 ("You have exhausted
+// your capacity on this model. Your quota will reset after 3h19m30s."),
+// and told as a 502 it ended the drawing on the spent account while the
+// next one had its quota (01huadalang on Discord). Out of quota, or
+// rate limited, it is the 429 it would have been; out of credit, the 402;
+// anything else the 502 it was, which asks no other account.
+func saidStatus(msg string) int {
+ b := wordsOf([]byte(msg))
+ switch {
+ case usedUpWords.Match(b), quotaCodes.Match(b), rateWords.Match(b):
+ return http.StatusTooManyRequests
+ case creditWords.Match(b):
+ return http.StatusPaymentRequired
+ }
+ return http.StatusBadGateway
+}
+
// keyLacksModel says a vendor answered 404 that the model isn't one this
// key or account serves: a relay's "Model "gpt-image-2" is not supported by
// any configured account in this group" for a key of its text models
@@ -1324,7 +1373,7 @@ func readImagesAnswer(p provider.Provider, url string, b []byte, code int) (draw
}
if len(out.Images) == 0 {
// an answer with no image says why, or is shown as it came
- return out, 502, fmt.Errorf("%s answered with no image%s", provider.HostOf(url), vendorSaid(vendorMessage(b)))
+ return out, saidStatus(vendorMessage(b)), fmt.Errorf("%s answered with no image%s", provider.HostOf(url), vendorSaid(vendorMessage(b)))
}
return out, code, nil
}
@@ -1667,7 +1716,7 @@ func (s *Server) drawCodeAssist(ctx context.Context, p provider.Provider, model
}
out.Text = strings.TrimSpace(out.Text)
if failed != "" && len(out.Images) == 0 {
- return out, 502, errors.New(failed)
+ return out, saidStatus(failed), errors.New(failed)
}
return out, 200, nil
})