Skip to content

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

gateway, provider: a model AgentRouter turns away on chat in Chinese is asked, and tested, on the next API (#3766)

JoeyWu-tech32ea16c5a4ef
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 692fb072a..731011d7c 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -38,7 +38,7 @@ backfilled; see [Gateway Session Aggregation](gateway-sessions.md).
| 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, or until its rest is lifted by hand (Try now, `Unrest`/`UnrestAll` call `unsink`), which puts it back in its place (`TestTryNowPutsASunkAccountBack`). 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. 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`. A conversation is kept by its id (`conversationID`), and a Claude Code subagent's apart from its lead's by `x-claude-code-agent-id` (`stickKey`): Claude Code sends the session's id with its subagents' requests too, and a subagent's answer took the lead's stick over, so the lead's next turn was judged by the subagent's cache and moved to another account, where its whole conversation was written again (slkassinthehood on X, `TestClaudeConversationKeepsItsAccountsCache`). Behind a remote magpie, the key or account is the remote's key and the provider there that serves the model, its id's first part (`remoteUpstream`, `candidate.vendor`, `sameVendor`): the remote is one key here, but each of its providers keeps its own cache and seals, so a conversation is kept, held strictly, told its sealed reasoning was refused and given its native compaction's reader by that pair, as the same group on the remote does (fremontdango on Discord: a group of one model from two of the remote's providers kept a conversation on the second as the same account when the first was gone, and a strict one failed over to it; `TestRemoteMagpieStrictKeepsTheRemotesProvider`, `TestRemoteMagpieOtherProviderIsNotTheSameAccount`). A remote's group is one vendor by its whole id, its own routing deciding inside it; a model named without a provider is the key alone. Lanes and requests a minute still go by the key here, the limits the user set on it. | [`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) |
+| 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`, `catalog.WrongEndpoint`: the English wordings and AgentRouter's Chinese one, #3766, read escaped too; a model's test reads them as well, see [Providers and accounts](providers-accounts.md)) 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) |
| Prompt | What each request's prompt held, part by part: the agent's system prompt, tool definitions (an MCP server's tools as one item), instruction files and skills, files its tools read, their other output, and the conversation. `inspectPrompt` reads it from the body as the agent sent it, before translation; `calibrated` scales the estimate to the prompt the vendor counted and sets the window of the model that answered. The route keeps names and sizes only, never the text, so the routing history on disk holds none. For the card's text (masaka on Discord), the trace keeps the last `promptBodies` (10) request bodies in memory only (at most 32 MiB each, 64 MiB together), masked as the vendor got them; for a model let have it unmasked, the masked copy is kept all the same. `PromptText` reads a part's items and their text from that body again (`prompt_text.go`), served to the GUI as `/api/context/text`; once a body is dropped, or magpie quits, its text is gone. It goes on the trace as `Route.Prompt`, with `Route.Conv` naming a conversation that has no session id. The GUI's `/api/context` scores each agent from it (`internal/gui/context.go`). A session's line and its compactions are its own turns': a call with a `Kind`, a Claude Code subagent's (`Route.Sub`, from `x-claude-code-agent-id`), and, in a session whose turns carry tool definitions, a request without any (auto mode's classifier, the title, the quota check) are left out, so a classifier's 40K after a 100K turn is no compaction (`toolSessions`, `TestContextSideCallsAreNoCompaction`). | [`prompt.go`](../../internal/gateway/prompt.go) |
| Vertex AI | A [Google Vertex AI provider](providers-accounts.md#responsibilities-and-sources-of-truth) speaks Gemini alone. A request goes to `<base>/publishers/google/models/<model>:streamGenerateContent?alt=sse` (`provider.VertexPath`, `upstreamPath`), the model named as the user's upstream name for it says (`UpstreamNameIn`, which the usage log's endpoint names too), with no `model` or `stream` field in its body (`passthrough`, `forwardOnce`, `geminiRequest`). A Gemini client's request is relayed as it came, its own `thinkingConfig`, `safetySettings` and the rest kept: streamed there, asked whole to `:generateContent` (`provider.VertexWholePath`), which answers one JSON body (Factory's rule of translating a whole request, as its route only streams, isn't Vertex AI's). Every other API's is translated (`buildFor`, `buildVertex`) and its stream read by `codeAssistDecoder`, which gives thought parts as reasoning, never as the answer's text. Effort: `vertexEffort` fits it to the model's levels, reasoning turned off going as `minimal` fitted the same way, and holds a model whose levels aren't known to `low`…`high`. `vertexThinking` sends Gemini 3 a `thinkingLevel` (`includeThoughts` false with reasoning off) and 2.5 a `thinkingBudget` in the model's range (`vertexBudgets`): turned off, the budget that stops its thinking, or Pro's least, as Pro can't stop. Thought signatures are every Gemini path's (#1445): a call's signature reaches the client in its id (`signedID`) and goes back on the call it came on, while a call with none in a step Vertex AI signed goes without; a step with nothing to give back has every call sent `skip_thought_signature_validator`. A 400 `Invalid thought signature.` is asked again with every call sent so (`withoutSignatures`), for that request only. A 400 refusing `minimal` (`geminiRefusedLevel`) is asked again at the model's lowest other level (`onEffort`), and `markUnfit(offRefused)` keeps that level until magpie restarts. Both retries are the translated path's. Vertex AI's own Google Search grounding isn't used: `searchHosts` names only `generativelanguage.googleapis.com` for Gemini, so magpie searches for it (`search.go`). | [`vertex.go`](../../internal/gateway/vertex.go), [`gateway.go`](../../internal/gateway/gateway.go), [`codeassist.go`](../../internal/gateway/codeassist.go), [`gemini.go`](../../internal/gateway/gemini.go), [`gemini_signature.go`](../../internal/gateway/gemini_signature.go), [`search.go`](../../internal/gateway/search.go) |
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md
index f1d28bc2c..b161f8ef5 100644
--- a/docs/subsystems/providers-accounts.md
+++ b/docs/subsystems/providers-accounts.md
@@ -60,7 +60,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi
| Last served | `served.json` records when each account, plan or key last answered, shown as `lastServedAt` (#570) on `/v1/magpie/quotas` and on the Usage page's own cards (`Quotas` → `cardsServed`, this computer's cards only, the caches left alone). The Usage page and the tray panel show the last-answered account in full, and a menu bar card that follows the account in use (`codex|*`) takes the one that answered within the last hour, else `InUseLogin`'s guess, else the one that answered last at all (a plugin's accounts, #1516) (`trayInUseCard`; okingkee on X: A spent, B answering, the menu bar stayed on A). `NoteServed` calls `OnServedMoved` when a provider's answers move to another account or key, and `SwitchLogin` after a switch, so the menu bar reads its cards again at once. | [`served.go`](../../internal/provider/served.go), [`trayusage.go`](../../internal/gui/trayusage.go) |
| Partners | Services that pay to be listed first in the add sheet, under a heading of their own ("Partners · magpie's sponsors"). The list is `site/partners.js`, served at `usemagpie.ai/api/partners`: a change reaches users with a deploy, not a release. `Partners` never waits: it returns what was fetched last and fetches again behind the call on a process's first ask, whatever the kept list's age, and hourly after (10 minutes after a failure), keeping the list in `partners.json` in the cache folder. The magpie serving the gateway fetches it when it starts and whenever it is due, asked for or not (`KeepPartnersFresh`), so an app left open on another page still learns of a partner added. The file also says when the last fetch started (`tried`) and, when it failed, why (`error`, logged once until a fetch gets the list again), so a list that stops changing tells a fetch that fails from one that never came back (`tried` after `at`, no error) and from none asked (#3362). Each entry is checked again on every read (`validPartners`): its own id, never a built-in preset's, a name, an endpoint, https only, at most 12; what only magpie's presets may set (a decision API, no key, a user endpoint) is cleared. Its logo is fetched through `FetchIcon` and kept as an uploaded icon, used as it is while its `iconUrl` stays the same; the prune after a save of `providers.json` (`pruneIcons`) leaves the logos of the partners listed now (`keptPartnerIcons`), as it leaves a plugin's. A partner is a preset to the rest of magpie (`Preset`, `FromPreset`, the editor), and one no longer listed still is to the providers added from it (`Seen`), without the sponsored mark. The window lists a partner in the languages it names (`langs`) only and shows its tagline in the page's language (`notes`, else `en`). `MAGPIE_PARTNERS` points it elsewhere, `off` lists none; tests list none unless they name a list. How often each listed partner was shown, opened (its row, its key page) and added is counted per UTC day (`CountPartner`, capped at 20 of each and 5 adds, kept in `partner-counts.json` in the cache folder) and sent with the next day's stats event as `magpie partner` events (`internal/stats`); nothing is counted with the stats or their usage part off. A listed partner the add sheet (or the TUI's, or `magpie providers presets`) hasn't shown yet is new (`NoticePartners` keeps those shown, as `noticed` in `partners.json`, whatever the stats); the window's add button carries a small dot for it, with its name in the title, until the sheet shows it. | [`partners.go`](../../internal/provider/partners.go), [`partner_events.go`](../../internal/provider/partner_events.go), [`site/partners.js`](../../site/partners.js) |
| Local servers | A preset of `KindLocal` (Ollama, LM Studio, oMLX, MLX-Serve) is at its server's default port on this machine. Another address — a port, or another computer — is given in the window's editor, in its Server address field, shown first above the key and never folded (a folded one read as an LM Studio whose base URL can't be changed, mengxiangziyou on X), or in the TUI (asked on adding, `w` after); on the command line with `address=` (`atAddress` in `providers_cli.go`: every URL moves, from the preset's when the provider was saved with none, and `address=` alone puts a local server back at its preset's), or the URLs themselves (`url=`, `anthropic=`, `responses=`). `AtAddress` replaces only each URL's scheme, host and port, each API keeping its path; `AddressOf` is the address an editor starts from. The window sends the URLs moved only when the address was changed, so a Save doesn't move URLs set apart (another app's import). A provider of the user's own (no preset) has `w` in the TUI too: each base URL it has is asked in turn, typed in as it is, Enter alone keeping one (`customURLsAsk`). | [`local_address.go`](../../internal/provider/local_address.go), [`providers_cli.go`](../../internal/cli/providers_cli.go) |
-| Protocol detection | Works out which of Chat, Responses, Anthropic Messages and Gemini generateContent a base URL speaks when a provider is added. Each is asked at the URL as it takes it (`DetectBase`); Gemini's at the `…/v1beta` beside a typed `…/v1` or bare host, a Gemini model from the list picked for it, with the key in `x-goog-api-key` (#1346). Detection, endpoint tests and model tests share `probe`, which adds `anthropic-version` only for Anthropic Messages; OpenAI probes omit that default so a relay that refuses it can answer. User-supplied headers still apply through `Sign`. The editor asks with the form as it stands, before any Save (#1545): the URL, key and models typed (`picked`, else the saved picks, else the vendor list); a key not typed again is the saved provider's, or for an unsaved copy the copied one's (`copyOf`). The probe writes nothing and adds no provider, and the route stays behind `magpie web`'s key. | [`detect.go`](../../internal/provider/detect.go), [`test.go`](../../internal/provider/test.go) |
+| Protocol detection | Works out which of Chat, Responses, Anthropic Messages and Gemini generateContent a base URL speaks when a provider is added. Each is asked at the URL as it takes it (`DetectBase`); Gemini's at the `…/v1beta` beside a typed `…/v1` or bare host, a Gemini model from the list picked for it, with the key in `x-goog-api-key` (#1346). Detection, endpoint tests and model tests share `probe`, which adds `anthropic-version` only for Anthropic Messages; OpenAI probes omit that default so a relay that refuses it can answer. A model's test that the vendor turns away with its word that the model is served on another API (`catalog.WrongEndpoint`, the gateway's words) asks the next API the provider has a URL for, as the gateway's `usable` takes it (the model's picked or listed API, else each API it isn't named-only on), with a key that sees the model there (`keySeeing`); that API's answer is the model's, and with none left, or every one saying it, the first stays. As Codex the next API is asked with Codex's headers, as the gateway sends them on Responses and Chat; as Claude Code it isn't, since the gateway sends Claude Code's on Messages alone (#3766, `TestTestModelsMovesToTheAPIServingIt`, `TestTestModelsKeepsAModelOnItsAPI`, `TestTestModelsAsAClientMovesAsTheGatewayDoes`). User-supplied headers still apply through `Sign`. The editor asks with the form as it stands, before any Save (#1545): the URL, key and models typed (`picked`, else the saved picks, else the vendor list); a key not typed again is the saved provider's, or for an unsaved copy the copied one's (`copyOf`). The probe writes nothing and adds no provider, and the route stays behind `magpie web`'s key. | [`detect.go`](../../internal/provider/detect.go), [`test.go`](../../internal/provider/test.go) |
| A pasted curl command | A curl command from a vendor's docs pasted into a custom provider's Base URL or API key fills the editor in (@mark_li21 on X). `curlOf` reads it as a shell would (lines joined by `\`, cmd's `^` or PowerShell's backtick; `curl.exe`; quotes and escapes), and takes: the API its URL's endpoint names (`…/chat/completions` Chat, `…/messages` Anthropic Messages, `…/responses` Responses) with the base URL before it, spelled as that API wants it (`respellURL`: Anthropic at its root, `/v1` after a bare host); the key from `Authorization: Bearer`, `x-api-key` or `api-key`, unless it only stands for one (`$OPENAI_API_KEY`, `${X}`, `%X%`, `<token>`, `YOUR_API_KEY`), when the key's box is left empty and focused; and the `model` of its JSON body, added to a new provider's models (a saved one's picks are left alone, since adding to none would narrow what it serves). A preset's editor takes only the key. A note (`editorError`, kind `note`) says what was taken, a placeholder like `{WorkspaceId}` left in the URL, and a URL with no known endpoint. Text that isn't a curl command pastes as before, several keys included. Tested on the docs' own examples (`fixtures/curl`). | [`app.js`](../../internal/gui/assets/app.js) (`curlOf`, `fromCurl`), [`provider-curl-paste.test.cjs`](../../internal/gui/tests/provider-curl-paste.test.cjs) |
## Remote magpie
diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go
index 26c220bb7..d003083bb 100644
--- a/internal/catalog/catalog.go
+++ b/internal/catalog/catalog.go
@@ -4,6 +4,7 @@
package catalog
import (
+ "bytes"
"context"
"encoding/json"
"fmt"
@@ -1596,6 +1597,87 @@ func PointsToChat(text string) bool {
return strings.Contains(strings.ToLower(text), "chat/completions")
}
+// WrongEndpoint recognizes the errors OpenAI-compatible servers give when a
+// model exists but isn't served on the endpoint asked: the gateway asks the
+// model on another API the provider has then, and so does a model's test.
+func WrongEndpoint(status int, body []byte) bool {
+ if status < 400 || status >= 500 {
+ return false
+ }
+ // the words are matched decoded, too (#1669)
+ msg := strings.ToLower(string(Words(body)))
+ for _, phrase := range []string{
+ "not a chat model",
+ "not supported in the v1/chat/completions",
+ "not supported in /v1/chat/completions",
+ "not supported in the v1/responses",
+ "not supported in /v1/responses",
+ "only supported in v1/responses",
+ "only supported in /v1/responses",
+ "use v1/completions",
+ "use /v1/completions",
+ "use v1/responses",
+ "use /v1/responses",
+ "use v1/chat/completions",
+ "use /v1/chat/completions",
+ "not accessible via the", // Copilot
+ "unsupported_api_for_model",
+ // Copilot's /v1/messages, for a model it serves on other APIs
+ // alone (GPT-4.1, Gemini, GPT-5.6: #754); a model it doesn't
+ // serve at all is model_not_supported there
+ "no model endpoints available given user constraints",
+ "is not supported for format", // OpenCode: "Model grok-4.7 is not supported for format anthropic"
+ "does not support this protocol", // OpenCode, with a key: "Model does not support this protocol"
+ // a relay serving a model with tools on Responses alone (#1308:
+ // "gpt-6.1-sol requires Responses for tool calls; this account
+ // only supports Chat Completions")
+ "requires responses",
+ // a relay that says it in Chinese (#3766, char1eslu: AgentRouter's
+ // "模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用
+ // /v1/responses(OpenAI Responses API)。")
+ "不支持 /v1/chat/completions 接口",
+ "改用 /v1/responses",
+ } {
+ if strings.Contains(msg, phrase) {
+ return true
+ }
+ }
+ return false
+}
+
+// Words is an error body as its words are matched: a JSON body that
+// escapes what isn't ASCII as \uXXXX (Python's json.dumps does by default)
+// has its strings decoded and put after it, one to a line, so "用量",
+// "频率" or "将在 … 重置" are read whether the vendor escaped them or not
+// (#1669). Any other body is matched as it is.
+func Words(body []byte) []byte {
+ if !bytes.Contains(body, []byte(`\u`)) {
+ return body
+ }
+ var v any
+ if json.Unmarshal(body, &v) != nil {
+ return body
+ }
+ out := append([]byte(nil), body...)
+ var walk func(any)
+ walk = func(x any) {
+ switch x := x.(type) {
+ case string:
+ out = append(append(out, '\n'), x...)
+ case map[string]any:
+ for _, y := range x {
+ walk(y)
+ }
+ case []any:
+ for _, y := range x {
+ walk(y)
+ }
+ }
+ }
+ walk(v)
+ return out
+}
+
// Drawers are the models of one models.dev provider that make images,
// newest first.
func Drawers(id string) []Model {
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 72a647623..b244270e4 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -6082,46 +6082,9 @@ func badRequest(status int) bool {
return status == http.StatusBadRequest || status == http.StatusUnprocessableEntity
}
-// wrongEndpoint recognizes the errors OpenAI-compatible servers give when a
-// model exists but isn't served on the endpoint asked.
-func wrongEndpoint(status int, body []byte) bool {
- if status < 400 || status >= 500 {
- return false
- }
- msg := strings.ToLower(string(body))
- for _, phrase := range []string{
- "not a chat model",
- "not supported in the v1/chat/completions",
- "not supported in /v1/chat/completions",
- "not supported in the v1/responses",
- "not supported in /v1/responses",
- "only supported in v1/responses",
- "only supported in /v1/responses",
- "use v1/completions",
- "use /v1/completions",
- "use v1/responses",
- "use /v1/responses",
- "use v1/chat/completions",
- "use /v1/chat/completions",
- "not accessible via the", // Copilot
- "unsupported_api_for_model",
- // Copilot's /v1/messages, for a model it serves on other APIs
- // alone (GPT-4.1, Gemini, GPT-5.6: #754); a model it doesn't
- // serve at all is model_not_supported there
- "no model endpoints available given user constraints",
- "is not supported for format", // OpenCode: "Model grok-4.7 is not supported for format anthropic"
- "does not support this protocol", // OpenCode, with a key: "Model does not support this protocol"
- // a relay serving a model with tools on Responses alone (#1308:
- // "gpt-6.1-sol requires Responses for tool calls; this account
- // only supports Chat Completions")
- "requires responses",
- } {
- if strings.Contains(msg, phrase) {
- return true
- }
- }
- return false
-}
+// wrongEndpoint is catalog.WrongEndpoint, whose words a model's test
+// reads too (#3766).
+func wrongEndpoint(status int, body []byte) bool { return catalog.WrongEndpoint(status, body) }
// wrongAPINote goes after an upstream's word that the model isn't served
// on the API it was asked on (wrongEndpoint), when no other API is left to
@@ -6165,8 +6128,8 @@ func wrongAPINote(p provider.Provider, model string, res *http.Response, body []
// with what was asked, on OpenAI's Responses API: where a provider has no
// URL for it, wrongAPINote says to set one.
func asksResponses(body []byte) bool {
- msg := strings.ToLower(string(body))
- for _, phrase := range []string{"requires responses", "use v1/responses", "use /v1/responses", "only supported in v1/responses", "only supported in /v1/responses"} {
+ msg := strings.ToLower(string(wordsOf(body)))
+ for _, phrase := range []string{"requires responses", "use v1/responses", "use /v1/responses", "only supported in v1/responses", "only supported in /v1/responses", "改用 /v1/responses"} {
if strings.Contains(msg, phrase) {
return true
}
diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go
index 1329a40c5..d8aa040c3 100644
--- a/internal/gateway/gateway_test.go
+++ b/internal/gateway/gateway_test.go
@@ -12,6 +12,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "strconv"
"strings"
"testing"
"time"
@@ -127,6 +128,7 @@ func events(body string) []map[string]any {
}
type fallbackFake struct {
+ refusal string // chat's, an English one when empty
chatBody []byte
responsesBody []byte
chatCalls int
@@ -141,6 +143,10 @@ func (f *fallbackFake) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.chatBody = body
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
+ if f.refusal != "" {
+ io.WriteString(w, f.refusal)
+ return
+ }
io.WriteString(w, `{"error":{"message":"This model is not supported in the v1/chat/completions endpoint. Use v1/responses."}}`)
case "/v1/responses":
f.responseCalls++
@@ -240,6 +246,52 @@ func TestChatClientFallsBackToResponses(t *testing.T) {
}
}
+// agentRouterSaid is AgentRouter's word that a model is served on
+// /v1/responses, as #3766 quotes it.
+const agentRouterSaid = "模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用 /v1/responses(OpenAI Responses API)。"
+
+// agentRouterRefusal is that word in an error body. The report has the
+// message only, so the JSON around it is a guess, and so is how it is
+// encoded: agentRouterEscaped has what isn't ASCII escaped, as Python's
+// json.dumps writes it (#1669).
+var (
+ agentRouterRefusal = `{"error":{"message":"` + agentRouterSaid + `","type":"invalid_request_error"}}`
+ agentRouterEscaped = `{"error":{"message":` + strconv.QuoteToASCII(agentRouterSaid) + `,"type":"invalid_request_error"}}`
+)
+
+// #3766 (char1eslu): AgentRouter turns its Responses-only models away on
+// /v1/chat/completions in Chinese, which wrongEndpoint didn't read, so a
+// provider with both URLs answered a Chat or a Messages client 400 though
+// /v1/responses serves the model. Both now get its reply from there.
+func TestChineseRefusalFallsBackToResponses(t *testing.T) {
+ for _, c := range []struct{ name, path, body, refusal string }{
+ {"chat", "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hello"}]}`, agentRouterRefusal},
+ {"messages", "/v1/messages", `{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"hello"}]}`, agentRouterRefusal},
+ {"chat, escaped", "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hello"}]}`, agentRouterEscaped},
+ {"messages, escaped", "/v1/messages", `{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"hello"}]}`, agentRouterEscaped},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+ f := &fallbackFake{refusal: c.refusal}
+ up := httptest.NewServer(f)
+ t.Cleanup(up.Close)
+ if err := provider.Save(provider.Provider{ID: "agentrouter", Name: "AgentRouter", Key: "k", Models: []string{"m1"},
+ Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
+ t.Fatal(err)
+ }
+ rec := httptest.NewRecorder()
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", c.path, strings.NewReader(c.body)))
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "fallback ok") {
+ t.Fatalf("status %d: %s", rec.Code, rec.Body)
+ }
+ if f.chatCalls != 1 || f.responseCalls != 1 {
+ t.Fatalf("calls: chat=%d responses=%d", f.chatCalls, f.responseCalls)
+ }
+ })
+ }
+}
+
// The other way round: a model served only on /chat/completions (Copilot's
// Claude models), asked for by a Responses client (Codex).
func TestResponsesClientFallsBackToChat(t *testing.T) {
@@ -385,6 +437,13 @@ func TestWrongEndpoint(t *testing.T) {
{400, `{"type":"error","error":{"type":"invalid_request_error","message":"Model does not support this protocol."}}`, true},
{400, "no model endpoints available given user constraints\n", true}, // Copilot's /v1/messages (#754)
{400, toolsOnChatRefused, true}, // a relay (#1308)
+ {400, agentRouterRefusal, true}, // in Chinese (#3766)
+ {400, agentRouterEscaped, true}, // escaped (#1669)
+ {400, `{"error":{"message":"模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口"}}`, true}, // its first clause alone
+ {400, `{"error":{"message":"请改用 /v1/responses(OpenAI Responses API)。"}}`, true}, // its second alone
+ // new-api's words for a model no channel serves (written from
+ // memory, not from the report): the model, not the API
+ {400, `{"error":{"message":"当前分组 default 下对于模型 gone 无可用渠道"}}`, false},
{400, `{"error":{"message":"The requested model is not supported.","code":"model_not_supported"}}`, false},
{429, `rate limit`, false},
{500, `use v1/responses`, false},
diff --git a/internal/gateway/opencode_wire_test.go b/internal/gateway/opencode_wire_test.go
index 50637a86c..be9367414 100644
--- a/internal/gateway/opencode_wire_test.go
+++ b/internal/gateway/opencode_wire_test.go
@@ -164,4 +164,14 @@ func TestWrongAPINote(t *testing.T) {
if got := wrongAPINote(p, "glm-5.3", res, nil); got != " (asked at https://opencode.ai/zen/go/v1/chat/completions)" {
t.Errorf("glm note %q", got)
}
+ // a relay with its chat URL alone that names Responses in Chinese:
+ // the note says to set that URL (#3766)
+ ar := provider.Provider{ID: "agentrouter", Name: "AgentRouter", Chat: "https://agentrouter.org/v1"}
+ u, _ = url.Parse("https://agentrouter.org/v1/chat/completions")
+ res = &http.Response{Request: &http.Request{URL: u}}
+ for _, body := range []string{agentRouterRefusal, agentRouterEscaped} {
+ if got := wrongAPINote(ar, "gpt-5.6-sol", res, []byte(body)); got != " (asked at https://agentrouter.org/v1/chat/completions; gpt-5.6-sol is served on /v1/responses, which AgentRouter has no URL for)" {
+ t.Errorf("AgentRouter note %q for %s", got, body)
+ }
+ }
}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index 7e3e05fd7..fcc80bfd6 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -25,6 +25,7 @@ import (
"sync"
"time"
+ "github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/routes"
"github.com/yetone/magpie/internal/usage"
@@ -339,38 +340,9 @@ const reloopedError = "the model's reasoning was stuck in a loop before it said
// over HTTP (proxyconnect) or SOCKS (socks connect).
var proxyDown = regexp.MustCompile(`proxyconnect |socks connect `)
-// wordsOf is an error body as its words are matched: a JSON body that
-// escapes what isn't ASCII as \uXXXX (Python's json.dumps does by default)
-// has its strings decoded and put after it, one to a line, so "用量",
-// "频率" or "将在 … 重置" are read whether the vendor escaped them or not
-// (#1669). Any other body is matched as it is.
-func wordsOf(body []byte) []byte {
- if !bytes.Contains(body, []byte(`\u`)) {
- return body
- }
- var v any
- if json.Unmarshal(body, &v) != nil {
- return body
- }
- out := append([]byte(nil), body...)
- var walk func(any)
- walk = func(x any) {
- switch x := x.(type) {
- case string:
- out = append(append(out, '\n'), x...)
- case map[string]any:
- for _, y := range x {
- walk(y)
- }
- case []any:
- for _, y := range x {
- walk(y)
- }
- }
- }
- walk(v)
- return out
-}
+// wordsOf is catalog.Words: an error body as its words are matched, with
+// the strings of a JSON body that escapes what isn't ASCII decoded (#1669).
+func wordsOf(body []byte) []byte { return catalog.Words(body) }
// failure says why a reply failed.
func failure(status int, body []byte) string {
diff --git a/internal/provider/test.go b/internal/provider/test.go
index e401a18ad..4d67752f7 100644
--- a/internal/provider/test.go
+++ b/internal/provider/test.go
@@ -30,6 +30,9 @@ type Result struct {
Millis int64 `json:"ms"`
Model string `json:"model,omitempty"`
Error string `json:"error,omitempty"`
+ // otherAPI: the vendor said the model isn't served on the API asked
+ // (catalog.WrongEndpoint), so another may be asked instead
+ otherAPI bool
}
// Test sends the smallest possible request to each endpoint the vendor
@@ -315,20 +318,7 @@ func (p Provider) testOne(ctx context.Context, model string) Result {
proto = Anthropic
}
// a key made for that endpoint, else one made for any, that sees it
- q, ok := p, true
- if keys := p.KeysOn(); len(keys) > 0 {
- ok = false
- serves := p.Serving(model)
- pick:
- for _, want := range []Protocol{proto, ""} {
- for _, k := range keys {
- if k.Protocol == want && serves(k) {
- q, ok = p.WithKey(k), true
- break pick
- }
- }
- }
- }
+ q, ok := p.keySeeing(proto, model)
if !ok {
return Result{Protocol: proto, Model: model, Error: "no key that's on sees this model"}
}
@@ -350,11 +340,7 @@ func (p Provider) testOne(ctx context.Context, model string) Result {
}
return r
}
- url, body := tiny(q, proto, UpstreamName(p, model))
- if client != "" {
- body = asClient(client, proto, body)
- }
- r := probe(ctx, q, proto, url, q.tested(body), model, testWait)
+ r := q.testOn(ctx, client, proto, model, UpstreamName(p, model))
if !r.OK && client == "" && proto == Chat && r.Status == 400 && catalog.DrawsID(model) && catalog.PointsToImagesAPI(r.Error) {
// an image model its name doesn't place on the images API
// (agnes-image-2.5-flash, #1837), which the vendor turns away in
@@ -365,9 +351,61 @@ func (p Provider) testOne(ctx context.Context, model string) Result {
return d
}
}
+ if !r.OK && r.otherAPI && client != ClientClaudeCode {
+ // the vendor's word that the model isn't served on that API: it
+ // is asked on the next one that may serve it, as the gateway asks
+ // a request there (usable), and that one's answer is the model's
+ // (#3766, char1eslu: models AgentRouter turns away on chat
+ // completions were red there while the gateway answered them on
+ // Responses). As Codex it is asked with Codex's headers, as the
+ // gateway sends them on Responses and Chat; as Claude Code it
+ // stays, since the gateway sends Claude Code's on Messages alone.
+ apis := p.APIs(model)
+ for _, next := range protos {
+ if next == proto || !(apis == nil && !p.NamedOnlyOn(next) || slices.Contains(apis, next)) {
+ continue
+ }
+ q, ok := p.keySeeing(next, model)
+ if !ok {
+ continue
+ }
+ if n := q.testOn(ctx, client, next, model, UpstreamName(p, model)); n.OK || !n.otherAPI {
+ return n
+ }
+ }
+ }
return r
}
+// keySeeing is p with the key a test of model on proto goes with: one made
+// for that API, else one made for any, that sees the model. A provider
+// without keys is left as it is.
+func (p Provider) keySeeing(proto Protocol, model string) (Provider, bool) {
+ keys := p.KeysOn()
+ if len(keys) == 0 {
+ return p, true
+ }
+ serves := p.Serving(model)
+ for _, want := range []Protocol{proto, ""} {
+ for _, k := range keys {
+ if k.Protocol == want && serves(k) {
+ return p.WithKey(k), true
+ }
+ }
+ }
+ return p, false
+}
+
+// testOn is model's test on proto's endpoint with p's key, asked for by
+// its upstream name, as client would ask it when one is named (TestAs).
+func (p Provider) testOn(ctx context.Context, client string, proto Protocol, model, upstream string) Result {
+ url, body := tiny(p, proto, upstream)
+ if client != "" {
+ body = asClient(client, proto, body)
+ }
+ return probe(ctx, p, proto, url, p.tested(body), model, testWait)
+}
+
// keyFor is p using the first key on that works with proto: one made for
// it, else one made for any. A provider without keys is left as it is.
func (p Provider) keyFor(proto Protocol) (Provider, bool) {
@@ -567,6 +605,7 @@ func probe(ctx context.Context, p Provider, proto Protocol, url string, body []b
}
b, _ := io.ReadAll(io.LimitReader(res.Body, 4096))
r.Error = APIError(b, res.Status)
+ r.otherAPI = catalog.WrongEndpoint(res.StatusCode, b)
return r
}
diff --git a/internal/provider/test_test.go b/internal/provider/test_test.go
index 200f39462..7bc513dcf 100644
--- a/internal/provider/test_test.go
+++ b/internal/provider/test_test.go
@@ -7,9 +7,12 @@ import (
"net/http/httptest"
"path/filepath"
"slices"
+ "strconv"
"strings"
"sync"
"testing"
+
+ "github.com/yetone/magpie/internal/catalog"
)
// Each endpoint is tried with a key made for it, and the Anthropic one
@@ -219,3 +222,192 @@ func TestTestModelsGoesWhereTheVendorPointsIt(t *testing.T) {
t.Fatalf("asked %v, want %v", paths, want)
}
}
+
+// #3766 (char1eslu): AgentRouter serves gpt-5.6-sol on /v1/responses and
+// turns it away on /v1/chat/completions, in Chinese. Test models asked
+// chat only, so the model stayed red while the gateway answered it on
+// Responses. On that word the test now asks the next API the provider has,
+// with that API's key, as the gateway would; another refusal stays as it
+// was, and so does the word when no other API is left or every API says
+// it, the first API's answer then.
+func TestTestModelsMovesToTheAPIServingIt(t *testing.T) {
+ oneHome(t)
+ said := "模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用 /v1/responses(OpenAI Responses API)。"
+ // the report has the message only: the JSON around it is a guess, and
+ // so is how it is encoded (escaped as Python's json.dumps writes it)
+ refusal := `{"error":{"message":"` + said + `","type":"invalid_request_error"}}`
+ escaped := `{"error":{"message":` + strconv.QuoteToASCII(said) + `,"type":"invalid_request_error"}}`
+ var mu sync.Mutex
+ var asked []string
+ chatSays := refusal
+ srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ var in struct{ Model string }
+ _ = json.NewDecoder(r.Body).Decode(&in)
+ mu.Lock()
+ asked = append(asked, in.Model+" "+r.URL.Path+" "+r.Header.Get("Authorization"))
+ chat := chatSays
+ mu.Unlock()
+ switch {
+ case in.Model == "gone":
+ // new-api's words for a model no channel serves, written from
+ // memory, not from the report
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(`{"error":{"message":"当前分组 default 下对于模型 gone 无可用渠道"}}`))
+ case r.URL.Path == "/v1/chat/completions":
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(chat))
+ case in.Model == "nowhere":
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(`{"error":{"message":"This model is not supported in the v1/responses endpoint."}}`))
+ default:
+ _, _ = rw.Write([]byte(`{}`))
+ }
+ }))
+ defer srv.Close()
+ took := func() []string {
+ mu.Lock()
+ defer mu.Unlock()
+ out := asked
+ asked = nil
+ return out
+ }
+ p := Provider{ID: "agentrouter", Name: "AgentRouter", Chat: srv.URL + "/v1", Responses: srv.URL + "/v1",
+ Key: "sk-chat", KeyProtocol: Chat, Keys: []KeyAccount{{Key: "sk-responses", Protocol: Responses}}}
+ for _, chat := range []string{refusal, escaped} {
+ mu.Lock()
+ chatSays = chat
+ mu.Unlock()
+ rs := p.TestModels(context.Background(), []string{"gpt-5.6-sol"})
+ if len(rs) != 1 || !rs[0].OK || rs[0].Protocol != Responses {
+ t.Fatalf("%s: results %+v", chat, rs)
+ }
+ want := []string{"gpt-5.6-sol /v1/chat/completions Bearer sk-chat", "gpt-5.6-sol /v1/responses Bearer sk-responses"}
+ if got := took(); !slices.Equal(got, want) {
+ t.Fatalf("%s: asked %v, want %v", chat, got, want)
+ }
+ }
+ mu.Lock()
+ chatSays = refusal
+ mu.Unlock()
+ // a refusal about the model is its answer, on the first API alone
+ rs := p.TestModels(context.Background(), []string{"gone"})
+ if len(rs) != 1 || rs[0].OK || rs[0].Protocol != Chat || rs[0].Status != 400 {
+ t.Fatalf("gone: results %+v", rs)
+ }
+ if got := took(); len(got) != 1 {
+ t.Fatalf("gone: asked %v, want chat once", got)
+ }
+ // with chat alone the vendor's word is the answer
+ chatOnly := Provider{ID: "agentrouter", Name: "AgentRouter", Chat: srv.URL + "/v1", Key: "sk-chat"}
+ rs = chatOnly.TestModels(context.Background(), []string{"gpt-5.6-sol"})
+ if len(rs) != 1 || rs[0].OK || rs[0].Protocol != Chat || rs[0].Status != 400 || !strings.Contains(rs[0].Error, "请改用 /v1/responses") {
+ t.Fatalf("chat only: results %+v", rs)
+ }
+ if got := took(); len(got) != 1 {
+ t.Fatalf("chat only: asked %v, want chat once", got)
+ }
+ // every API says it: each is asked, and the first one's answer stays
+ rs = p.TestModels(context.Background(), []string{"nowhere"})
+ if len(rs) != 1 || rs[0].OK || rs[0].Protocol != Chat || !strings.Contains(rs[0].Error, "请改用 /v1/responses") {
+ t.Fatalf("nowhere: results %+v", rs)
+ }
+ if got := took(); len(got) != 2 {
+ t.Fatalf("nowhere: asked %v, want chat and responses", got)
+ }
+}
+
+// The API the user picked for a model, or the only one the vendor's list
+// names, keeps its test there, as usable keeps the gateway's request:
+// chat turning it away is the answer, not a reason to ask Responses
+// (#3766).
+func TestTestModelsKeepsAModelOnItsAPI(t *testing.T) {
+ oneHome(t)
+ const refusal = `{"error":{"message":"模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用 /v1/responses(OpenAI Responses API)。","type":"invalid_request_error"}}`
+ var mu sync.Mutex
+ var asked []string
+ srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ mu.Lock()
+ asked = append(asked, r.URL.Path)
+ mu.Unlock()
+ if r.URL.Path == "/v1/chat/completions" {
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(refusal))
+ return
+ }
+ _, _ = rw.Write([]byte(`{}`))
+ }))
+ defer srv.Close()
+ p := Provider{ID: "agentrouter", Name: "AgentRouter", Key: "k", Chat: srv.URL + "/v1", Responses: srv.URL + "/v1", Models: []string{"picked", "listed"}}
+ if err := Save(p); err != nil {
+ t.Fatal(err)
+ }
+ if err := catalog.SaveLive(p.ID, p.Chat, []catalog.Model{{ID: "picked"}, {ID: "listed", APIs: []string{"chat"}}}); err != nil {
+ t.Fatal(err)
+ }
+ if err := SetModelAPI("agentrouter/picked", "chat"); err != nil {
+ t.Fatal(err)
+ }
+ q, err := Find(p.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, m := range []string{"picked", "listed"} {
+ mu.Lock()
+ asked = nil
+ mu.Unlock()
+ rs := q.TestModels(context.Background(), []string{m})
+ mu.Lock()
+ n := len(asked)
+ mu.Unlock()
+ if len(rs) != 1 || rs[0].OK || rs[0].Protocol != Chat || n != 1 {
+ t.Errorf("%s: results %+v after %d asks, want red on chat after one", m, rs, n)
+ }
+ }
+}
+
+// Asked as Codex, a model the vendor turns away on Responses with its word
+// that Chat serves it is asked there with Codex's headers, as the gateway
+// sends them on both; asked as Claude Code it stays red on Messages, where
+// alone the gateway sends Claude Code's (#3766).
+func TestTestModelsAsAClientMovesAsTheGatewayDoes(t *testing.T) {
+ oneHome(t)
+ SawClient(ClientClaudeCode, http.Header{"User-Agent": {"claude-cli/2.1.0 (external, cli)"}, "X-App": {"cli"}})
+ var mu sync.Mutex
+ var asked []string
+ srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ mu.Lock()
+ asked = append(asked, r.URL.Path+" "+r.Header.Get("User-Agent"))
+ mu.Unlock()
+ switch r.URL.Path {
+ case "/v1/responses":
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(`{"error":{"message":"This model is not supported in the v1/responses endpoint. Use v1/chat/completions."}}`))
+ case "/v1/messages":
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"Model gpt-x is not supported for format anthropic"}}`))
+ default:
+ rw.Header().Set("Content-Type", "text/event-stream")
+ _, _ = rw.Write([]byte("data: {}\n\n"))
+ }
+ }))
+ defer srv.Close()
+ p := Provider{ID: "relay", Name: "Relay", Key: "k", Chat: srv.URL + "/v1", Responses: srv.URL + "/v1", Anthropic: srv.URL}
+ took := func() []string {
+ mu.Lock()
+ defer mu.Unlock()
+ out := asked
+ asked = nil
+ return out
+ }
+ rs := p.TestModels(TestAs(context.Background(), ClientCodex), []string{"gpt-x"})
+ got := took()
+ if len(rs) != 1 || !rs[0].OK || rs[0].Protocol != Chat || len(got) != 2 || !strings.HasPrefix(got[0], "/v1/responses ") ||
+ !strings.HasPrefix(got[1], "/v1/chat/completions codex_cli_rs/") {
+ t.Errorf("as Codex: results %+v, asked %q", rs, got)
+ }
+ rs = p.TestModels(TestAs(context.Background(), ClientClaudeCode), []string{"gpt-x"})
+ got = took()
+ if len(rs) != 1 || rs[0].OK || rs[0].Protocol != Anthropic || len(got) != 1 || got[0] != "/v1/messages claude-cli/2.1.0 (external, cli)" {
+ t.Errorf("as Claude Code: results %+v, asked %q", rs, got)
+ }
+}