Skip to content

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

gateway: a provider's listed reply headers and the vendor's request ids reach the agent (#4145)

yetonebb3565309d85
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index a262990fc..eaf688116 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -186,6 +186,7 @@ recorded as a prediction failure (`noPrediction`).
- A 2xx that can't be an API's answer is the 502 it stands for (`notAnAPIReply`, #1012): a web page (`text/html`, or a body that begins as one: a sign-in page, Cloudflare's challenge), a body with nothing in it, or one sent as JSON that doesn't begin as JSON. Every upstream request goes through it (`forwardOnce`, and the built-in clients' own: the ChatGPT backend's `/responses`), so it fails over, rests and is logged as a 502 does, its reason naming the page's title.
- An error status whose body holds no message of its own says what it was (`provider.Unsaid`, through `APIError` and the plan-quota and drawing readers): a web page by its title as plain words, a firewall's block page (`EdgeBlocked`: Alibaba Cloud's, Cloudflare's "you have been blocked", "used Cloudflare to restrict access" and `error code: 1005`–`1020`) with `BlockedHint` after it, and an empty body under an HTTP status as refused with no details, often a firewall or proxy in front of the API (`NoDetails`; an empty `response.failed` event, whose fallback is no status, still reads "upstream stream failed"). A bare "403 Forbidden" told a relay's user nothing (maralica on Discord). A JSON error still reads as its message. `TestAPIErrorUnsaid`, `TestRelayRefusalSaysWhatItWas`. Only the first bytes are peeked, which a stream waits for anyway, and a compressed body is left as it came.
- A 2xx from Zhipu or Z.ai holding the platform's refusal envelope (`{"code":1000,"msg":…,"success":false}`, which their Responses API at `/api/v1` answers a refused key) is the refusal it stands for (`provider.ZhipuEnveloped` in `forwardOnce`): 401 for a key refused, 429 for a limit or no balance, as Zhipu's error codes give, so it fails over and rests as those do and the agent is told why. `TestZhipuResponsesRefusalIsRefusal`.
+- The vendor's reply headers a provider passes on (`ReplyHeaders` and the default request ids, see [providers and accounts](providers-accounts.md), #4145) reach the agent from the reply it is given alone. Every request that can become the agent's reply goes out through `forward` (relayed, translated, a plugin's fetch, a signed-in account) or `retrieveAs` (embeddings, audio), and each tells its reply's passed headers to the try that asked through the try's context (`withReplySink`, `heardReply` in [`reply_headers.go`](../../internal/gateway/reply_headers.go)). The sink is the try's `holdWriter` (`heardUpstream`), so a try that fails and is let go takes its vendor's headers with it, a fallback's reply carries the provider that answered's, and a failure `release`d to the agent carries its own vendor's. A reply to another provider (a side model, a searcher) is not heard: the sink takes its own provider's alone. Within a try the last reply replaces the earlier ones (an ask again without a refused beta, a Mantle reroute), and once the try has its status nothing more is heard (a cut reply's continuation). count_tokens writes them straight (`passReplyHeaders`), and embeddings through `heardHeaders`. A stream whose headers `keepAlive` sent before the vendor answered (past `keepHeldAfter`) has none: they can't follow a status already sent. Not carried: images, video and the Codex backend relay, which copies its reply's headers whole already. `TestReplyHeadersPassedOn`, `TestReplyHeadersOfTheReplyGiven`, `TestReplyHeadersOnCountTokens`, `TestReplyHeadersOnEmbeddings`.
- A streamed request is never left silent while its vendor says nothing. `holdWriter.watch` runs beside each try: past `keepHeldAfter` (3s) it sends a held try's agent the stream's 200 and SSE comments (`keepAlive`), one every `keepHeldEvery` (5s), none of the try, so another candidate may still answer in the same stream. It was 15s, and a proxy between the agent and magpie that hangs up on a request with nothing back for about 10s ended every turn slower to its first event: Codex on Windows sends 127.0.0.1 through a Clash-style app's system proxy, `127.*` in its bypass list or not, and its turns ended 499 (mrten_95615 on Discord; `TestHeldStreamHeardBeforeAProxyCuts`). A failure before it is still a status; once the reply passes, it puts a comment between two of its lines after `keepaliveEvery` of quiet. It stops after `keepaliveLongest` (5 minutes) with nothing from the vendor. A translated stream (`streamTranslated`) whose upstream has begun its reply and then says nothing at all, not even a comment (a relay that sends the rest of a long reply in one burst at its end), is kept alive in the client's own protocol by a ticker beside its reader (`keepThroughSilence`): every `keepaliveEvery` it sends the client's keepalive (`enc.keepalive`: a ping for Anthropic, `response.in_progress` for Responses, a comment for Chat, nothing for Gemini), which also counts as the try heard from, so the held stream's comments go on too; it stops once the upstream has been silent for `translatedQuietLongest` (30 minutes), past the 10 minutes `upstreamHeaderWait` gives a vendor to begin and long enough for a long reasoning turn sent whole at its end, after which the client's own timeout ends the wait (youyo.p on Discord: before, Claude Code heard only comments, which its event watchdog doesn't count, and gave up 300s on; `TestTranslatedSilenceKeptAliveForABound`). An upstream that has answered its headers but sent no byte yet is still waited for under the held stream's comments alone (`notAnAPIReply` reads its first bytes before the reply is translated). An Anthropic client is sent a ping at most once each `pingEvery` (1 minute), comments between, and none before `message_start`: Claude Code's watchdog counts only 30 pings in a row as life, and a ping on every upstream keepalive spent them in the first minute; the SDKs' `MessageStream` fails on any event before `message_start` (`anthropicEncoder.keepalive`, `TestTranslatedStreamKeepsClientAlive`). A try waiting for its key's or account's slot or for room in its minute (`lanes.take`, `rpms.wait`) is kept alive the same way (`keepQueued`), for as long as it waits; once the 200 is out, the try is held, and its failure or its queue turning it away reaches the agent as the stream's error (`failTo`), not a status (coeo91 on Discord). A wait shorter than `keepHeldAfter` sends nothing, so its 429 keeps its `Retry-After`. A proxy in front such as Cloudflare (125s) otherwise ends the request (#947). Gemini streams get no comments (#934). A vendor that then ignores `stream:true` and answers 200 with its reply whole has it sent as the agent's stream (`wholeAsStream`: Chat, Responses, Anthropic), not as the stream's error, while a reply under a type that says neither JSON nor a stream is held as a stream when its first bytes are one (`sseStart`), read event by event like one that says so, so an error among its frames still goes to the next candidate (#1418: it went through at once); a 200 that is no reply of the protocol's fails as a translated reply that didn't stream does (502, "… did not stream"), for the next candidate to answer, and the last one's reaches the agent as the stream's error. A held stream that has sent nothing but the frames before a reply (`response.created`, `response.in_progress`, `codex.rate_limits`, pings, an empty part begun, and any event `streamEvent` doesn't know to carry text, reasoning, a call or a reply's end (`replyEvent`), or an item not known that isn't a call or a tool's (`streamPart.said`): `eventLead`; Aliceonly on #1418: an unknown event was taken for content and let a frames-only 200 through, and its `server_is_overloaded` reached Codex with five candidates left) stays held as long as its agent is kept alive, up to `holdLead` (4 minutes, under the 300s Codex waits for an event; `keptQuiet`), so a `server_is_overloaded` that comes after it is a failure the next candidate answers (#1418: let through at 15s, the ChatGPT backend's failure 36s in ended Codex's turn with sub2api, next in the group, never asked). A stream whose agent can't be kept alive (Gemini's) is let through after `holdLongest` (15s), as before. Codex counts only events toward that 300s, not comments, so the bound is the request's, not each try's: once Codex on Responses has heard no event for `agentQuietMost` (4 minutes) since the request began or its last event (`keptAlive.tooQuiet`), the try in hand is let through (`scan`, `keepQuiet`), and no group replan or same-one retry is taken whose pause would end past it (fadenoob on Discord: held tries and replan pauses added up past 300s and Codex reconnected on "idle timeout waiting for SSE"). A translated reply whose upstream stream stops before it said its reply had — an Anthropic one with no stop_reason and no message_stop, a Responses one with no `response.completed`, the same two a relayed one is told by (`sniff.whole`; a Chat stream may end without `[DONE]` and be whole), a Gemini or Code Assist one whose last chunk has no candidate's `finishReason` (`geminiEnds`, #1643: `usageMetadata` is no end, Vertex AI sends it on every chunk; a chunk with no candidate at all is the empty reply's to answer, #667), and a relayed Gemini one told the same way (#1644; it ends in Gemini's own `{"error":{"code":502,…,"status":"UNAVAILABLE"}}` data line from `streamFailure`, `TestGeminiRelayEndedShort`; whatever a relayed stream gets appended, that error or a filled-in `finish_reason`, begins after a blank line, two newlines when the upstream's last write stopped inside a line, so it is an event of its own rather than more of the half-written one, #2446, `TestPassthroughStreamCutMidLine`, `TestGeminiCompatFinishAfterPartialLine`), and a Chat one from Gemini's OpenAI-compatible API (`geminiCompat`) with no `finish_reason`, relayed or translated (`saysItsEnd`; Linka on Discord: the upstream hung up between a whole function call and its `finish_reason`, and Pi threw the call away as "Stream ended without finish_reason"; that API counts the usage only once the generation is done, so when the last chunk carried it only the `finish_reason` was lost and it is filled in, `tool_calls` after a call and `stop` otherwise, as a relayed chunk before `[DONE]` or a translated reply's end, and the reply counts as whole (`chatTail`); only one with no usage at its end is a cut, `TestGeminiCompatRelayEndedShort`, `TestGeminiCompatTranslatedEndedShort`) — ends the turn with "the reply ended before it was complete" (`errEndedShort`, `TestResponsesUpstreamEndedShort`, `TestGeminiUpstreamEndedShort`), asked again in place where the upstream goes on from a prefill (`continuation.go`). A client that asked for no stream is told the same 502 of such a cut (`translate`, `TestNonStreamEndedShort`, `TestGeminiNonStreamEndedShort`), rather than handed what came as its whole reply. A translated reply from an Anthropic or Responses upstream that ended but said nothing (no text, call, search or image) and counted no token at all is no reply either, whatever its end says (`repliesCounted`, #2086: an Anthropic-compatible proxy's 200 ended a Codex turn as `response.completed` with no output, no usage and no served model, and Codex didn't ask again). It is the empty reply's to answer (#667): asked again in place, then failed with "an empty reply", streamed or not, for every client (`TestUncountedEmptyReplyFails`, `TestUncountedEmptyReplyFailsEveryClient`). Claude's own empty `end_turn` counts its prompt and stays a whole reply (`TestWellFormedStreamsStillComplete`). A reply passed through in its own protocol isn't read this way.
- A vendor's error keeps its `code` and `param` on its way to an OpenAI-shaped client (`writeVendorError`), where `writeError` used to set both to null: a status error relayed or translated, and a request refused inside a Responses stream. A Responses stream's `error` event or failed response whose error is of type `invalid_request_error` is the request at fault (`requestRefused`, read by its type, not its words): its `Event` carries status 400 and the vendor's error (`Event.Said`), so a client answered whole gets the vendor's 400 with its code and param (the whole-translated path wrote 502 `api_error`), a reply cut by it isn't resumed (`continuation.again`), and a translated stream's error says the vendor's type, code and param (`saidError`), so `holdWriter` reads it as the 400 it is. Before, the hold read magpie's `api_error` as a 502: the same request was sent three times, then to the other accounts, and every one rested (magpie-releases#18: the ChatGPT backend answers `access_programs` naming a program the workspace hasn't got with a 200 stream whose error is `access_program_not_enabled`). Asked once, nobody rests ([`codex_access_programs_test.go`](../../internal/gateway/codex_access_programs_test.go)).
- An error that reaches the agent in a stream already begun can't carry its status, so `streamError` says it in the words (`saysPassing`, #2372, zminds): for a status that may pass (408, 429, 5xx) whose text names none of the words a text-judging agent retries on (Pi decides by its `errorMessage` alone), it appends `(<status>: you can retry your request)`; a request's own fault (400, 401, 404 …) and a text that already says so are left as they were. `TestStreamErrorSaysItPasses` holds Pi's own lists; `TestGroupLastFailureInStreamSaysItPasses` runs a group whose last member fails after the keepalive. A 413 byte cap in that stream says `request_too_large` in the error text, and Chat and Anthropic name that type, so Pi compacts; Gemini keeps its own status shape (`TestStreamedByteCapSaysRequestTooLargeToPi`, `TestStreamErrorByteCapInEveryShape`). Wherever the protocol says a model with the error (Chat's first chunk, Anthropic's `message_start`, Responses' `response.created` and the events after it), it is the model the agent asked for, as `serve` has it, an effort on it kept (`keptAlive.model`; on Codex's own sign-in, the one Codex wrote) (#3728, jilen: with `"model":""` fx took Chat's first chunk for a broken one and stopped there; `TestStreamErrorNamesTheModel`, `TestKeptAliveChatErrorNamesTheModel`, `TestKeptAliveChatOnCodexErrorNamesTheModel`, `TestKeptAliveCodexErrorNamesTheModel`). An error after a reply that already went, a loop cut's, still starts a message of its own after the vendor's, under a new id and no model (#3792).
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md
index 98e8ee385..d3beec4c8 100644
--- a/docs/subsystems/providers-accounts.md
+++ b/docs/subsystems/providers-accounts.md
@@ -22,6 +22,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi
| GLM Coding Plan key at pay as you go | Zhipu's and Z.ai's presets offer Pay as you go first (`/api/paas/v4`) and the Coding Plan (`/api/coding/paas/v4`, Responses at `/api/v1`); both share `/api/anthropic`, which bills the plan, so a plan's key left at Pay as you go worked in Claude Code and was refused 429 code 1113 "余额不足或无可用资源包" in Codex (@cleverpigo on X). Nothing in the key tells the two apart, so a Chat request to `open.bigmodel.cn` or `api.z.ai` at `/api/paas/v4` refused 1113 is asked once more at `/api/coding/paas/v4` (`RetryZhipuCoding`, in the gateway's `forwardOnce` and the provider test's `probe`). Served there, `LearnZhipuCoding` moves a saved provider with that one key, still at that URL, to the plan's OpenAI URL (and the preset's plan Responses URL when it has none); one with other keys stays and is asked again each time. Refused there too, the 1113 body keeps its code and gets both URLs and what to change in its message (`ZhipuNoBalanceHint`). A pay-as-you-go key with a balance is never answered 1113 and never sent to the plan's endpoint. | [`zhipu_coding.go`](../../internal/provider/zhipu_coding.go), [`test.go`](../../internal/provider/test.go) |
| Zhipu's 200 refusals | Zhipu's and Z.ai's `/api/v1` (the GLM Coding Plan's Responses API, from their Codex pages) and `/api/monitor` answer a refusal 200 in the platform's envelope, `{"code":1000,"msg":"身份验证失败。","success":false}`, where their OpenAI and Anthropic endpoints answer the same key 401 (@xanahopper on X: a refused key tested green on the Responses row only). `ZhipuEnveloped` turns a 2xx JSON body from `open.bigmodel.cn` or `api.z.ai` with `success: false` into `{"error":{"code","message"}}` at the status Zhipu's error-code table gives (1000–1005 → 401, 1113 and 1302–1399 → 429, 1220 → 403, 1210–1229, 1261 and 1301 → 400, anything else → 502), in the provider test's `probe` (endpoint tests, model tests, Detect APIs) and the gateway's `forwardOnce`. A stream, a compressed body, a body over 64 KB and every other host pass as they came. The plan's quota card for a key refused there (1000–1005, or a 401) says `<Zhipu or Z.ai> didn't take this key (<their words>): …` with the vendor's API-keys page (`zhipuRefused`), not asked again as a team's key, and the GUI shows it as that (`quotaError`) rather than "Allowance unavailable". | [`zhipu_envelope.go`](../../internal/provider/zhipu_envelope.go), [`planquota.go`](../../internal/provider/planquota.go), [`test.go`](../../internal/provider/test.go) |
| Extra body | `ExtraBody` (`extraBody` in providers.json, the editor's *Extra body*, #1604) is a JSON object whose fields `WithExtraBody` adds at the top level of every JSON body sent to the provider: Messages, Chat, Responses and Gemini, relayed as they came or translated, in the gateway's `forwardOnce` and in Test and Detect (`tested`). A field the body has already is kept; a name written `"!name"` replaces it. Values are put in as written, objects whole, not merged. `${sessionId}` in any string, at any depth, becomes the request's session: the agent's session header (`sessionOf`), else the id `conversationID` derives from the first user message (`magpie-` and its hash), so it stays the same through one conversation; Test and Detect send `magpie-test-` and 12 random letters, a new one each time. `model`, `stream` and the conversation (`messages`, `input`, `contents`, `system`, `systemInstruction`, `system_instruction`, `instructions`, `tools`, `ExtraBodyReserved`) are magpie's: Save refuses them with or without `!` (`ExtraBodyOK`), the editor says so as they are typed, and one written into providers.json by hand is dropped on read (`cleanExtraBody`). Not sent with it: `count_tokens`, embeddings, images, video and System One's decision model (other APIs), and a signed-in account's requests, except a ChatGPT (Codex) account's (magpie-releases#18): its requests are built as Codex CLI's (`codexBody`) and take the fields Codex CLI adds by choice, such as `access_programs` (OpenAI's Daybreak), while `store`, `tool_choice` and `include`, which the backend needs as `codexBody` sets them, can't be set (`codexOwned`). Its extraBody is written into providers.json by hand under the account's entry and kept onto the account by `withPicks`; the editor has no box for it. A client's own top-level `access_programs` on Responses or Chat is carried to a Responses build as it came (`Request.AccessPrograms`), so a request translated (a Codex account is streamed, so a whole Responses request is made again) keeps it as a relayed one does. It reaches only the upstreams Codex itself sends it to (`keepsAccessPrograms`, #3172, see [gateway routing](gateway-routing.md)); it is taken out before the extraBody merge, so an `access_programs` the user puts in a provider's extraBody still goes. One object goes to every API; fields for one API alone are not a thing yet. A `thinking` budget it puts in on an Anthropic endpoint is fitted to a Claude that thinks only adaptively after the merge, as an agent's is (`adaptiveThinking`, `TestExtraBodyBudgetFittedToAdaptiveModel`). | [`extrabody.go`](../../internal/provider/extrabody.go), [`gateway.go`](../../internal/gateway/gateway.go) |
+| Reply headers | `ReplyHeaders` (`replyHeaders` in providers.json, the editor's *Reply headers* under Advanced, a comma-separated list, #4145) names headers of the vendor's reply the gateway passes on to the agent, matched whatever their case and saved canonical (`cleanReplyHeaders`). `DefaultReplyHeaders` (`Request-Id`, `X-Request-Id`, `X-Oneapi-Request-Id`) go from every provider without being listed: opaque request ids, under names magpie sets nothing of its own. Never passed on, listed or not (`ReplyHeaderPassable`): the reply's framing (`Content-*`, `Transfer-Encoding`, hop-by-hop), cookies and sign-ins (`Set-Cookie`, `Authorization`, `WWW-Authenticate`, `Proxy-*`), CORS (`Access-Control-*`), `Sec-*`, redirects and browser policy (`Location`, `Strict-Transport-Security`, `Content-Security-Policy`…), `Retry-After`/`Cache-Control`/`Date`/`Server`/`Vary` (magpie writes those) and `X-Magpie-*` (the gateway reads them as notes of the try). Save refuses such a name (`ReplyHeadersOK`), the editor says so before the save (`replyHeadersOf`, its list held to Go's by `TestReplyHeadersNeverAsTheProvidersAre`), and one written into providers.json by hand is dropped on read. A preset's provider and a signed-in account's requests take it too (`withPicks`). How the gateway carries them is in [gateway routing](gateway-routing.md). | [`replyheaders.go`](../../internal/provider/replyheaders.go), [`reply_headers.go`](../../internal/gateway/reply_headers.go) |
| Native compaction | `NativeCompaction` (`nativeCompaction` in providers.json, the editor's *Compaction* row, `magpie provider set <id> compaction=native\|magpie`; #1099) has Codex's compaction asked of the provider as Codex sent it and its sealed item returned unchanged. Off by default. Only a provider whose wire carries it can be set so (`CarriesCompaction`: a Responses API, a ChatGPT account, GitHub Copilot; not another agent's sign-in or a plugin's), and `CompactsOn(model)` also needs the model asked on Responses. `SetNativeCompaction`, the CLI and the GUI's save refuse it elsewhere. An account keeps it through `Save`'s picks (`withPicks`), and `AddCopy` carries it. The gateway's half is in [gateway-routing](gateway-routing.md). | [`native_compaction.go`](../../internal/provider/native_compaction.go) |
| providers.json | `Path` is `providers.json` in magpie's config folder. A read that fails returns `ErrUnreadable` and is never treated as empty. `store` writes atomically through a rename and drops the cached catalog (`catalog.Touched`). | [`provider.go`](../../internal/provider/provider.go) |
| A provider renamed | `Rename` (the editor's id field, `magpie provider set <id> id=<new>`) keeps the old id in `Was`, so a model still picked by it reaches the provider. Usage is recorded under the id a call went out by and never rewritten; whatever reads it names the provider by its id now through `UsageRenames().Of(id, time)`: the summary, the Requests page (its blocks, the probe rows, other computers' days), the Sessions tab's vias, window holds, `NewPricer`/`NewCoster` and the gateway's `loggedAnswers` (cybercaius on Discord: a renamed provider was listed under both ids). `Visit` hands records out as recorded, so `Of` is taken once. An id another provider takes later (a rename onto it, or `Save` of a new provider under it) moves from the first one's `Was` to its `Gave` with the time (`giveID`): calls recorded under it before then stay the first one's, later ones are the new one's. An id held by a provider or a subscription now is never renamed (`Renamed`), and `freeID` doesn't hand out an id in any `Was`, so adding a vendor again gets `-2`. Ids reused before `Gave` existed have no time and go to whoever holds the id now. A provider deleted (`Delete`) is kept in providers.json's `deleted` with its name, `Was`, `Gave` and when (`provider.Deleted`): its calls stay under its id while no provider has that id, and once one is added under it (or a later deleted one had it) they go to `Deleted.UsageID`, `<id>~<when, UTC>`, so the new provider has only its own (cybercaius on Discord). The Usage page names either by the deleted provider's name and marks it with when it was deleted (`Renames.Gone`, `Named.Deleted`). Deletes before this was kept have no record and go to whoever holds the id now. A deleted provider's calls can be merged into a provider there is now, or named by the user, from a "…" beside the Requests page's Provider filter while it is picked (`ledProviderMore` in `plugins/usage/page.js`, `POST /api/usage/deleted`, `GET /api/usage/deleted/into`; CyberCaius on Discord, who had renamed a provider onto the old id to do it). Both are kept on its `Deleted` record, not in settings and never in the log: `MergeDeleted` sets `Into` and `MergedAt`, `NameDeleted` sets `Label` (`Deleted.Shown`), so a provider added later under the old id inherits neither. `Renames.Both(id, time)` gives the id a call is shown under (`Of`, merges followed) and the one it was made to (`Own`): a merged call is listed, filtered and summed under the target, but priced, held and answered by its own (`Record.priceID`, `block_price.go`'s `as`, window holds, `loggedAnswers`), so a merge moves no cost or token. The target's face lists what is merged into it (`Named.Merged`), and the same "…" parts each again (`into` ""). Renaming the target carries the merge; deleting it ends it (a merge older than the target's deletion is not followed). A providers.json that can't be read keeps the last renames read, not none. The summary and Requests caches key on `Renames.Key`, the vias cache too. | [`rename.go`](../../internal/provider/rename.go), [`block_price.go`](../../internal/usage/block_price.go) |
@@ -313,6 +314,8 @@ node --test internal/gui/tests/account-discovery.test.cjs
go test -tags nogui ./internal/provider ./internal/gateway ./internal/gui -run ExtraBody
go test -tags nogui ./internal/provider ./internal/davsync ./internal/gui -run 'KeepsAccessKey|ProvidersFileWithBOM|AccessKeySecret|PlanCardWithoutAccessKey|KeepsVolcengineSecret'
node --test internal/gui/tests/extra-body.test.cjs
+go test -tags nogui ./internal/provider ./internal/gateway ./internal/gui -run ReplyHeaders
+node --test internal/gui/tests/reply-headers.test.cjs
```
The package's tests use a home of their own (`testenv`). Never point them at
diff --git a/internal/gateway/embeddings.go b/internal/gateway/embeddings.go
index 359d1220e..d06604d0a 100644
--- a/internal/gateway/embeddings.go
+++ b/internal/gateway/embeddings.go
@@ -237,6 +237,9 @@ func (s *Server) serveRetrieval(w http.ResponseWriter, r *http.Request, path, op
return
}
ctx, cancel := context.WithTimeout(r.Context(), retrieveTimeout)
+ // the headers of the vendor's reply passed on (#4145)
+ var heard heardHeaders
+ ctx = withReplySink(ctx, c.p.ID, heard.put)
b, got, code, err := s.retrieveAs(ctx, c.p, base+path, ctype, out)
cancel()
tried(code, err)
@@ -263,10 +266,12 @@ func (s *Server) serveRetrieval(w http.ResponseWriter, r *http.Request, path, op
if len(skipped) > 0 {
msg += " (tried first: " + call.Fallback + ")"
}
+ heard.into(w.Header())
writeError(w, provider.Chat, code, msg)
return
}
s.record(call)
+ heard.into(w.Header())
w.Header().Set("Content-Type", req.answerType(got))
w.WriteHeader(code)
w.Write(b)
@@ -326,6 +331,7 @@ func (s *Server) retrieveAs(ctx context.Context, p provider.Provider, url, ctype
return nil, "", 502, err
}
res, err := p.Do(s.client, req)
+ heardReply(ctx, p, res)
if err != nil {
if ctx.Err() != nil {
return nil, "", 504, fmt.Errorf("%s didn't answer in %s", p.Name, retrieveTimeout)
diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go
index 584ecad75..8148e9bff 100644
--- a/internal/gateway/fallback.go
+++ b/internal/gateway/fallback.go
@@ -1918,6 +1918,10 @@ type holdWriter struct {
// before it was complete" and its 429 halfway went to the summary
// as the only answer, the next account never asked
wantWhole bool
+
+ // upstreamKeys are the headers of the vendor's reply heardUpstream put
+ // in header (#4145)
+ upstreamKeys []string
}
// onLoop sets what is told of a loop before it ends the reply (reask),
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 68f50afe9..31702d662 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -1520,10 +1520,12 @@ func (s *Server) countOn(w http.ResponseWriter, r *http.Request, p provider.Prov
}
}
keepRetryOut(w.Header(), res.Header, b)
+ passReplyHeaders(w.Header(), c.p, res)
writeError(w, provider.Anthropic, res.StatusCode, c.p.Explain(c.p.Name+": "+provider.APIError(b, res.Status), res.StatusCode, b))
return
}
defer res.Body.Close()
+ passReplyHeaders(w.Header(), c.p, res)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
@@ -2971,6 +2973,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// step of its way out, for the Routing page to say whose time
// a slow turn was (TryNet)
ctx = httptrace.WithClientTrace(ctx, nt.hooks(&asking, &asked, &wrote))
+ // the headers of its vendor's reply the provider passes on go
+ // with the try's own, to the agent if it is the one it gets
+ // (#4145)
+ ctx = withReplySink(ctx, c.p.ID, hw.heardUpstream)
// a key or account with a MaxConcurrency is asked once one of
// its slots is free, in turn; the agent gone while it waits,
// nothing is sent (the 499 below)
@@ -4212,8 +4218,15 @@ func markAntigravityRefused(w http.ResponseWriter, p provider.Provider, system,
// Unexpected value(s) `x` for the `anthropic-beta` header) is asked again
// once without them, and they're left out for it from then on. An account's
// 403 or 400 it can mend (Provider.Retry) is asked again, a few times at most.
-func (s *Server) forward(ctx context.Context, p provider.Provider, to provider.Protocol, path string, body []byte, in http.Header) (*http.Response, error) {
- res, err := s.forwardOnce(ctx, p, to, path, body, in)
+func (s *Server) forward(ctx context.Context, p provider.Provider, to provider.Protocol, path string, body []byte, in http.Header) (res *http.Response, err error) {
+ // the reply it returns says which of its headers go to the agent
+ // (#4145), not one asked before it
+ defer func() {
+ if err == nil {
+ heardReply(ctx, p, res)
+ }
+ }()
+ res, err = s.forwardOnce(ctx, p, to, path, body, in)
if err == nil && mantleRoutes(p, to) {
res, err = s.mantleReroute(ctx, p, to, path, body, in, res)
}
@@ -5066,6 +5079,8 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
// other API left, a group's next member is asked, and the
// agent is told the Responses URL to set
s.markUnfit(p.ID, offRefused(model), proto)
+ // the refusal is the reply, its headers the ones passed on
+ heardReply(r.Context(), p, res)
} else {
res = off
}
diff --git a/internal/gateway/reply_headers.go b/internal/gateway/reply_headers.go
new file mode 100644
index 000000000..9d960fc4b
--- /dev/null
+++ b/internal/gateway/reply_headers.go
@@ -0,0 +1,89 @@
+package gateway
+
+// The vendor's reply headers passed on to the agent (#4145): its request
+// id by default, and whatever else the provider lists (ReplyHeaders).
+//
+// Every request that may become the agent's reply goes out through
+// forward (each API, translated or not, a plugin's fetch) or retrieveAs
+// (embeddings, audio), and both say what they heard to the try that asked
+// (heardReply). A try's writer keeps it until the try has its status:
+// the holdWriter of a candidate's try, so a try that fails and is let go
+// takes its vendor's headers with it, and the agent has those of the
+// reply it is given alone.
+
+import (
+ "context"
+ "net/http"
+ "slices"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+type replySinkKey struct{}
+
+// replySink is where a try's vendor replies' passed headers go: the
+// provider the try is of, and what takes them.
+type replySink struct {
+ id string
+ put func(http.Header)
+}
+
+// withReplySink is ctx whose requests to the provider id say the headers
+// their reply passes on to put, the last reply's in place of those before.
+func withReplySink(ctx context.Context, id string, put func(http.Header)) context.Context {
+ return context.WithValue(ctx, replySinkKey{}, replySink{id: id, put: put})
+}
+
+// heardReply tells the try of ctx the headers res passes on, when res is
+// its provider's reply: none for a request the try makes elsewhere.
+func heardReply(ctx context.Context, p provider.Provider, res *http.Response) {
+ s, ok := ctx.Value(replySinkKey{}).(replySink)
+ if !ok || res == nil || s.id != p.ID {
+ return
+ }
+ s.put(p.PassedReplyHeaders(res.Header))
+}
+
+// passReplyHeaders puts what the vendor's reply res passes on into dst,
+// for a reply written straight to the agent.
+func passReplyHeaders(dst http.Header, p provider.Provider, res *http.Response) {
+ if res == nil {
+ return
+ }
+ for k, vs := range p.PassedReplyHeaders(res.Header) {
+ dst[k] = vs
+ }
+}
+
+// heardUpstream sets the headers the vendor's reply passes on, in place of
+// those of an earlier reply of the same try (an ask again after a refused
+// beta or shape). Once the try has its status they are what it is: a
+// reply asked for after it (a cut reply's continuation) changes nothing.
+func (h *holdWriter) heardUpstream(got http.Header) {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ if h.status != 0 {
+ return
+ }
+ for _, k := range h.upstreamKeys {
+ h.header.Del(k)
+ }
+ h.upstreamKeys = h.upstreamKeys[:0]
+ for k, vs := range got {
+ h.header[k] = slices.Clone(vs)
+ h.upstreamKeys = append(h.upstreamKeys, k)
+ }
+}
+
+// heardHeaders keeps the last reply's passed headers, for a try that
+// writes the agent's reply itself.
+type heardHeaders struct{ h http.Header }
+
+func (s *heardHeaders) put(h http.Header) { s.h = h }
+
+// into puts them into dst.
+func (s *heardHeaders) into(dst http.Header) {
+ for k, vs := range s.h {
+ dst[k] = vs
+ }
+}
diff --git a/internal/gateway/reply_headers_test.go b/internal/gateway/reply_headers_test.go
new file mode 100644
index 000000000..fc79bec47
--- /dev/null
+++ b/internal/gateway/reply_headers_test.go
@@ -0,0 +1,215 @@
+package gateway
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// the vendor's reply headers, each kind of them: request ids passed on by
+// default, one the user lists in another case, and ones never passed on
+func setVendorHeaders(h http.Header, id string) {
+ h.Set("X-Oneapi-Request-Id", "oneapi-"+id)
+ h.Set("X-Request-Id", "xr-"+id)
+ h.Set("X-Trace-Token", "trace-"+id)
+ h.Set("X-Unlisted", "no")
+ h.Set("Set-Cookie", "session=secret")
+ h.Set("Access-Control-Allow-Origin", "*")
+}
+
+const (
+ rhAnthropicJSON = `{"id":"msg_1","type":"message","role":"assistant","model":"sol","content":[{"type":"text","text":"hello"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`
+ rhAnthropicSSE = "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"sol\",\"content\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\n" +
+ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n" +
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hello\"}}\n\n" +
+ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n" +
+ "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":1}}\n\n" +
+ "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+ rhChatJSON = `{"id":"c1","object":"chat.completion","model":"sol","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`
+ rhChatSSE = "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"sol\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello\"}}]}\n\n" +
+ "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"sol\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1,\"total_tokens\":2}}\n\n" +
+ "data: [DONE]\n\n"
+)
+
+// vendorReply answers as vendor does on its wire: a stream when asked for
+// one, an error with status when it isn't 200.
+func vendorReply(vendor provider.Protocol, status int, id string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ body := rhBody(r)
+ setVendorHeaders(w.Header(), id)
+ if status != 200 {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ w.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"bad request, ` + id + `"}}`))
+ return
+ }
+ stream := strings.Contains(body, `"stream":true`)
+ switch {
+ case stream:
+ w.Header().Set("Content-Type", "text/event-stream")
+ if vendor == provider.Chat {
+ w.Write([]byte(rhChatSSE))
+ } else {
+ w.Write([]byte(rhAnthropicSSE))
+ }
+ case vendor == provider.Chat:
+ w.Header().Set("Content-Type", "application/json")
+ w.Write([]byte(rhChatJSON))
+ default:
+ w.Header().Set("Content-Type", "application/json")
+ w.Write([]byte(rhAnthropicJSON))
+ }
+ }
+}
+
+func rhBody(r *http.Request) string {
+ var b strings.Builder
+ buf := make([]byte, 4096)
+ for {
+ n, err := r.Body.Read(buf)
+ b.Write(buf[:n])
+ if err != nil {
+ return b.String()
+ }
+ }
+}
+
+func rhVendor(t *testing.T, p provider.Provider, vendor provider.Protocol, h http.Handler) {
+ t.Helper()
+ up := httptest.NewServer(h)
+ t.Cleanup(up.Close)
+ if vendor == provider.Chat {
+ p.Chat = up.URL + "/v1"
+ } else {
+ p.Anthropic = up.URL
+ }
+ if err := provider.Save(p); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// checkPassed fails unless h carries the vendor's request ids and the
+// listed header of reply id, and nothing never passed on or unlisted.
+func checkPassed(t *testing.T, h http.Header, id string) {
+ t.Helper()
+ for k, want := range map[string]string{
+ "X-Oneapi-Request-Id": "oneapi-" + id,
+ "X-Request-Id": "xr-" + id,
+ "X-Trace-Token": "trace-" + id,
+ } {
+ if got := h.Get(k); got != want {
+ t.Errorf("%s = %q, want %q", k, got, want)
+ }
+ }
+ for _, k := range []string{"X-Unlisted", "Set-Cookie"} {
+ if got := h.Get(k); got != "" {
+ t.Errorf("%s = %q passed on", k, got)
+ }
+ }
+}
+
+// #4145: the vendor's request ids, and the headers the provider lists in
+// any case, reach the agent, relayed or translated, streamed or whole, and
+// on the vendor's error; never a cookie or one the user didn't list.
+func TestReplyHeadersPassedOn(t *testing.T) {
+ for _, vendor := range []provider.Protocol{provider.Anthropic, provider.Chat} {
+ for _, stream := range []bool{false, true} {
+ for _, status := range []int{200, 400} {
+ name := string(vendor)
+ if stream {
+ name += "/stream"
+ }
+ if status != 200 {
+ name += "/error"
+ }
+ t.Run(name, func(t *testing.T) {
+ fresh(t)
+ rhVendor(t, provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"sol"},
+ ReplyHeaders: []string{"x-trace-token", "set-cookie"}}, vendor, vendorReply(vendor, status, "one"))
+ body := `{"model":"fake/sol","max_tokens":100,"messages":[{"role":"user","content":"hi"}]` +
+ map[bool]string{true: `,"stream":true}`, false: `}`}[stream]
+ rec := httptest.NewRecorder()
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
+ if rec.Code != status {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ checkPassed(t, rec.Header(), "one")
+ if got := rec.Header().Get("Access-Control-Allow-Origin"); got == "*" {
+ t.Errorf("the vendor's CORS header passed on")
+ }
+ })
+ }
+ }
+ }
+}
+
+// A request magpie took to a second provider after the first refused it
+// carries the second's headers alone (#4145).
+func TestReplyHeadersOfTheReplyGiven(t *testing.T) {
+ fresh(t)
+ noRests()
+ t.Cleanup(noRests)
+ rhVendor(t, provider.Provider{ID: "ag", Name: "AG", Key: "k", Models: []string{"sol"}, Fallback: []string{"spare/sol"},
+ ReplyHeaders: []string{"X-Trace-Token"}}, provider.Anthropic, vendorReply(provider.Anthropic, 429, "first"))
+ rhVendor(t, provider.Provider{ID: "spare", Name: "Spare", Key: "k", Models: []string{"sol"}},
+ provider.Chat, vendorReply(provider.Chat, 200, "second"))
+ rec := httptest.NewRecorder()
+ body := `{"model":"ag/sol","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}`
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
+ if rec.Code != 200 {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ h := rec.Header()
+ if h.Get("X-Request-Id") != "xr-second" || h.Get("X-Oneapi-Request-Id") != "oneapi-second" {
+ t.Errorf("ids %q %q, want the second's", h.Get("X-Request-Id"), h.Get("X-Oneapi-Request-Id"))
+ }
+ // spare lists nothing: the first provider's listed header went with its try
+ if got := h.Get("X-Trace-Token"); got != "" {
+ t.Errorf("X-Trace-Token = %q, from a reply not given", got)
+ }
+}
+
+// count_tokens passes the vendor's ids on too.
+func TestReplyHeadersOnCountTokens(t *testing.T) {
+ fresh(t)
+ rhVendor(t, provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"sol"}, ReplyHeaders: []string{"X-Trace-Token"}},
+ provider.Anthropic, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ setVendorHeaders(w.Header(), "count")
+ w.Header().Set("Content-Type", "application/json")
+ w.Write([]byte(`{"input_tokens":3}`))
+ }))
+ rec := httptest.NewRecorder()
+ body := `{"model":"fake/sol","messages":[{"role":"user","content":"hi"}]}`
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages/count_tokens", strings.NewReader(body)))
+ if rec.Code != 200 {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ checkPassed(t, rec.Header(), "count")
+}
+
+// Embeddings pass the vendor's ids on, its answer's and its error's.
+func TestReplyHeadersOnEmbeddings(t *testing.T) {
+ for _, status := range []int{200, 400} {
+ fresh(t)
+ rhVendor(t, provider.Provider{ID: "lib", Name: "Lib", Key: "k", Models: []string{"embed-1"}, ReplyHeaders: []string{"X-Trace-Token"}},
+ provider.Chat, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ setVendorHeaders(w.Header(), "embed")
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ if status != 200 {
+ w.Write([]byte(`{"error":{"message":"not an embedding model"}}`))
+ return
+ }
+ w.Write([]byte(`{"object":"list","data":[{"object":"embedding","index":0,"embedding":[0.1]}],"model":"embed-1","usage":{"prompt_tokens":1,"total_tokens":1}}`))
+ }))
+ rec := httptest.NewRecorder()
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/embeddings", strings.NewReader(`{"model":"lib/embed-1","input":"hi"}`)))
+ if rec.Code != status {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ checkPassed(t, rec.Header(), "embed")
+ }
+}
diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js
index c4d4e85f6..98a0bbe71 100644
--- a/internal/gui/assets/app.js
+++ b/internal/gui/assets/app.js
@@ -7876,7 +7876,7 @@ document.addEventListener("click", (e) => {
// advancedSet says whether a setting under the editor's Advanced is set,
// which opens it: none hides behind a fold
function advancedSet(d, p) {
- return (d.headers || []).some(([k]) => (k || "").trim()) || !!(d.extraBody || "").trim() ||
+ return (d.headers || []).some(([k]) => (k || "").trim()) || !!(d.extraBody || "").trim() || !!(d.replyHeaders || "").trim() ||
!!d.proxyMode || Object.values(d.accountProxies || {}).some((x) => x?.mode) ||
[d.concurrency, d.queueLimit, d.queueWait, d.maxRPM, d.priceRate, d.contexts, d.outputs, d.compacts].some((v) => String(v ?? "").trim()) ||
!!(d.searches || d.authHeader || d.passUserAgent || d.unredacted || d.pinUpstream || d.nativeCompaction || d.balanceToken || d.limitRestOff) ||
@@ -8192,6 +8192,22 @@ const EXTRA_BODY_RESERVED = ["model", "stream", "messages", "input", "contents",
function extraBodyText(obj) {
return obj && Object.keys(obj).length ? JSON.stringify(obj, null, 2) : "";
}
+// Reply headers magpie never passes on to the agent, listed or not, as
+// provider.ReplyHeaderPassable refuses them on a save (reply-headers' Go
+// test holds the two lists the same): the reply's framing, cookies,
+// sign-ins, CORS and magpie's own (#4145).
+const REPLY_NEVER = ["alt-svc", "api-key", "authentication-info", "authorization", "cache-control", "clear-site-data", "connection", "content-security-policy", "cookie", "date", "host", "keep-alive", "link", "location", "origin", "refresh", "retry-after", "server", "set-cookie", "set-cookie2", "strict-transport-security", "te", "trailer", "transfer-encoding", "upgrade", "vary", "www-authenticate", "x-accel-buffering", "x-api-key"];
+const REPLY_NEVER_PREFIX = ["content-", "proxy-", "access-control-", "sec-", "x-magpie-"];
+// replyHeadersOf is the list a save sends, or why it can't be saved
+function replyHeadersOf(text) {
+ const value = (text || "").split(",").map((x) => x.trim()).filter(Boolean);
+ for (const n of value) {
+ if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(n)) return { value, error: t("\"{h}\" isn't a header name", { h: n }) };
+ const k = n.toLowerCase();
+ if (REPLY_NEVER.includes(k) || REPLY_NEVER_PREFIX.some((x) => k.startsWith(x))) return { value, error: t("{h} is never passed on: magpie writes it, or it would act on magpie's own address", { h: n }) };
+ }
+ return { value };
+}
// extraBodyOf is the object a save sends ({} for none), or why it can't be
function extraBodyOf(text) {
const s = (text || "").trim();
@@ -9019,7 +9035,7 @@ function baseAPIOf(p) {
// draftOf is a saved provider as its editor's form holds it.
function draftOf(p) {
- return { id: p.id, name: p.name, preset: p.preset, chat: p.chat, responses: p.responses, anthropic: p.anthropic, gemini: p.gemini || "", catalog: p.catalog, decide: p.decide || "", key: "", api: baseAPIOf(p), chosen: pickedOf(p), none: !!p.noModels, extra: [], headers: headerRows(p.headers), extraBody: extraBodyText(p.extraBody), icon: p.icon || "", fallback: [...(p.fallback || [])], unlisted: !!p.unlisted, hiddenDrawers: [...(p.hiddenDrawers || [])], searches: !!p.searches, unredacted: !!p.unredacted, insecureTLS: !!p.insecureTLS, limitRestOff: !!p.limitRestOff, nativeCompaction: !!p.nativeCompaction, passUserAgent: !!p.passUserAgent, pinUpstream: !!p.pinUpstream, authHeader: p.authHeader || "", balanceURL: p.balanceURL || "", balancePath: p.balancePath || "", balanceMethod: p.balanceMethod || "", modelsURL: p.modelsURL || "", contexts: contextsText(p.contexts), outputs: contextsText(p.outputs), compacts: contextsText(p.compacts), keysUrl: p.keysUrl || "", ...proxyDraft(p), ...concurrencyDraft(p) };
+ return { id: p.id, name: p.name, preset: p.preset, chat: p.chat, responses: p.responses, anthropic: p.anthropic, gemini: p.gemini || "", catalog: p.catalog, decide: p.decide || "", key: "", api: baseAPIOf(p), chosen: pickedOf(p), none: !!p.noModels, extra: [], headers: headerRows(p.headers), extraBody: extraBodyText(p.extraBody), replyHeaders: (p.replyHeaders || []).join(", "), icon: p.icon || "", fallback: [...(p.fallback || [])], unlisted: !!p.unlisted, hiddenDrawers: [...(p.hiddenDrawers || [])], searches: !!p.searches, unredacted: !!p.unredacted, insecureTLS: !!p.insecureTLS, limitRestOff: !!p.limitRestOff, nativeCompaction: !!p.nativeCompaction, passUserAgent: !!p.passUserAgent, pinUpstream: !!p.pinUpstream, authHeader: p.authHeader || "", balanceURL: p.balanceURL || "", balancePath: p.balancePath || "", balanceMethod: p.balanceMethod || "", modelsURL: p.modelsURL || "", contexts: contextsText(p.contexts), outputs: contextsText(p.outputs), compacts: contextsText(p.compacts), keysUrl: p.keysUrl || "", ...proxyDraft(p), ...concurrencyDraft(p) };
}
// duplicateProvider opens the Add form on a copy of p (#268): its URLs,
@@ -9536,6 +9552,11 @@ function drawEditor(p, presetID) {
}
// and JSON fields of the user's own for each request's body (#1604)
advIn.append(...field(t("Extra body"), extraBodyEditor(), t("JSON fields added to the body of every request to the vendor. A field the request has already is kept; write \"!name\" to replace it. ${sessionId} is the conversation's id.")));
+ // and headers of the vendor's reply passed on to the agent (#4145)
+ const replyIn = input(draft.replyHeaders || "", "X-Trace-Id, X-Foo");
+ replyIn.classList.add("reply-headers");
+ replyIn.oninput = () => { draft.replyHeaders = replyIn.value; };
+ advIn.append(...field(t("Reply headers"), replyIn, t("Headers of the vendor's reply passed on to the agent, separated by commas, in any case. Request IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) always are; cookies, CORS and the body's own headers never.")));
const proxies = el("div", "stack");
proxies.append(proxyPicker());
const perKey = keyProxyPicker(p);
@@ -9959,6 +9980,9 @@ function drawEditor(p, presetID) {
const xb = extraBodyOf(draft.extraBody);
if (xb.error) { shown(ed.querySelector(".extra-body textarea"))?.focus({ preventScroll: true }); return editorError(t("Extra body: {why}", { why: xb.error }), "warn"); }
body.extraBody = xb.value;
+ const rh = replyHeadersOf(draft.replyHeaders);
+ if (rh.error) { shown(ed.querySelector(".reply-headers"))?.focus({ preventScroll: true }); return editorError(t("Reply headers: {why}", { why: rh.error }), "warn"); }
+ body.replyHeaders = rh.value;
if (decides || custom) body.decide = (custom && draft.api !== "decide" && !p?.decide ? "" : draft.decide || "").trim();
if ((body.decide || "").includes(WORKSPACE)) { ed.querySelector(".workspace-id")?.focus({ preventScroll: true }); return editorError(t("Give the workspace ID your API key belongs to, or pick the Token Plan"), "warn"); }
if (custom) {
diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js
index e3475887c..12b64f4ff 100644
--- a/internal/gui/assets/i18n.js
+++ b/internal/gui/assets/i18n.js
@@ -2141,6 +2141,11 @@ const I18N = {
"Optional headers sent with every request to {p}, applied after auth.": "随每个发往 {p} 的请求发送的可选请求头,在鉴权之后应用。",
"Extra body": "额外请求体",
"JSON fields added to the body of every request to the vendor. A field the request has already is kept; write \"!name\" to replace it. ${sessionId} is the conversation's id.": "加进发往供应商的每个请求体里的 JSON 字段。请求里已有的字段会保留;写成 \"!name\" 则替换它。${sessionId} 是会话的 ID。",
+ "Reply headers": "透传响应头",
+ "\"{h}\" isn't a header name": "“{h}”不是有效的响应头名称",
+ "{h} is never passed on: magpie writes it, or it would act on magpie's own address": "{h} 永不透传:它由 magpie 自己写入,或会作用于 magpie 自身的地址",
+ "Reply headers: {why}": "透传响应头:{why}",
+ "Headers of the vendor's reply passed on to the agent, separated by commas, in any case. Request IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) always are; cookies, CORS and the body's own headers never.": "供应商响应中要透传给 agent 的响应头,用逗号分隔,不区分大小写。请求 ID(X-Request-Id、Request-Id、X-OneAPI-Request-ID)总会透传;Cookie、CORS 和响应体自身的头永不透传。",
"not valid JSON": "不是有效的 JSON",
"write a JSON object, like {\"prompt_cache_key\": \"${sessionId}\"}": "请写一个 JSON 对象,例如 {\"prompt_cache_key\": \"${sessionId}\"}",
"a field needs a name": "字段需要一个名字",
@@ -6224,6 +6229,11 @@ const I18N = {
"Optional headers sent with every request to {p}, applied after auth.": "隨每個發往 {p} 的請求傳送的可選請求頭,在鑑權之後應用。",
"Extra body": "額外請求體",
"JSON fields added to the body of every request to the vendor. A field the request has already is kept; write \"!name\" to replace it. ${sessionId} is the conversation's id.": "加進發往供應商的每個請求體裡的 JSON 欄位。請求裡已有的欄位會保留;寫成 \"!name\" 則取代它。${sessionId} 是對話的 ID。",
+ "Reply headers": "透傳回應標頭",
+ "\"{h}\" isn't a header name": "「{h}」不是有效的標頭名稱",
+ "{h} is never passed on: magpie writes it, or it would act on magpie's own address": "{h} 一律不透傳:它由 magpie 自己寫入,或會作用於 magpie 自身的位址",
+ "Reply headers: {why}": "透傳回應標頭:{why}",
+ "Headers of the vendor's reply passed on to the agent, separated by commas, in any case. Request IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) always are; cookies, CORS and the body's own headers never.": "供應商回應中要透傳給 agent 的標頭,以逗號分隔,不分大小寫。請求 ID(X-Request-Id、Request-Id、X-OneAPI-Request-ID)一律透傳;Cookie、CORS 與回應本體自身的標頭一律不透傳。",
"not valid JSON": "不是有效的 JSON",
"write a JSON object, like {\"prompt_cache_key\": \"${sessionId}\"}": "請寫一個 JSON 物件,例如 {\"prompt_cache_key\": \"${sessionId}\"}",
"a field needs a name": "欄位需要一個名稱",
@@ -10284,6 +10294,11 @@ const I18N = {
"Optional headers sent with every request to {p}, applied after auth.": "{p} へのすべてのリクエストに付ける任意のヘッダー。認証の後に適用されます。",
"Extra body": "追加ボディ",
"JSON fields added to the body of every request to the vendor. A field the request has already is kept; write \"!name\" to replace it. ${sessionId} is the conversation's id.": "ベンダーへのすべてのリクエストのボディに加える JSON フィールド。リクエストにすでにあるフィールドはそのまま残り、\"!name\" と書くと置き換えます。${sessionId} は会話の ID です。",
+ "Reply headers": "レスポンスヘッダー",
+ "\"{h}\" isn't a header name": "「{h}」はヘッダー名ではありません",
+ "{h} is never passed on: magpie writes it, or it would act on magpie's own address": "{h} は決して渡しません:magpie 自身が書くか、magpie 自身のアドレスに作用するためです",
+ "Reply headers: {why}": "レスポンスヘッダー: {why}",
+ "Headers of the vendor's reply passed on to the agent, separated by commas, in any case. Request IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) always are; cookies, CORS and the body's own headers never.": "ベンダーのレスポンスからエージェントへ渡すヘッダー。カンマ区切りで、大文字小文字は問いません。リクエスト ID(X-Request-Id、Request-Id、X-OneAPI-Request-ID)は常に渡し、Cookie、CORS、ボディ自身のヘッダーは決して渡しません。",
"not valid JSON": "有効な JSON ではありません",
"write a JSON object, like {\"prompt_cache_key\": \"${sessionId}\"}": "JSON オブジェクトを書いてください(例: {\"prompt_cache_key\": \"${sessionId}\"})",
"a field needs a name": "フィールドには名前が必要です",
@@ -14374,6 +14389,11 @@ const I18N = {
"Optional headers sent with every request to {p}, applied after auth.": "Optionale Header, die mit jeder Anfrage an {p} gesendet und nach der Authentifizierung angewendet werden.",
"Extra body": "Zusätzlicher Body",
"JSON fields added to the body of every request to the vendor. A field the request has already is kept; write \"!name\" to replace it. ${sessionId} is the conversation's id.": "JSON-Felder, die dem Body jeder Anfrage an den Anbieter hinzugefügt werden. Ein Feld, das die Anfrage schon hat, bleibt erhalten; schreib \"!name\", um es zu ersetzen. ${sessionId} ist die ID der Unterhaltung.",
+ "Reply headers": "Antwort-Header",
+ "\"{h}\" isn't a header name": "„{h}“ ist kein Header-Name",
+ "{h} is never passed on: magpie writes it, or it would act on magpie's own address": "{h} wird nie weitergegeben: magpie schreibt ihn selbst, oder er würde auf magpies eigene Adresse wirken",
+ "Reply headers: {why}": "Antwort-Header: {why}",
+ "Headers of the vendor's reply passed on to the agent, separated by commas, in any case. Request IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) always are; cookies, CORS and the body's own headers never.": "Header der Antwort des Anbieters, die an den Agenten weitergegeben werden, durch Kommas getrennt, Groß- und Kleinschreibung egal. Anfrage-IDs (X-Request-Id, Request-Id, X-OneAPI-Request-ID) immer; Cookies, CORS und die Header des Bodys selbst nie.",
"not valid JSON": "kein gültiges JSON",
"write a JSON object, like {\"prompt_cache_key\": \"${sessionId}\"}": "schreib ein JSON-Objekt, etwa {\"prompt_cache_key\": \"${sessionId}\"}",
"a field needs a name": "ein Feld braucht einen Namen",
diff --git a/internal/gui/providers.go b/internal/gui/providers.go
index d629659d0..399c8c433 100644
--- a/internal/gui/providers.go
+++ b/internal/gui/providers.go
@@ -87,6 +87,8 @@ type providerJSON struct {
Headers map[string]string `json:"headers,omitempty"`
// JSON fields added to each request's body (provider.ExtraBody, #1604)
ExtraBody provider.ExtraBody `json:"extraBody,omitempty"`
+ // headers of the vendor's reply passed on (provider.ReplyHeaders, #4145)
+ ReplyHeaders []string `json:"replyHeaders,omitempty"`
// the API the user gave a custom provider's Base URL as (BaseAPI)
BaseAPI string `json:"baseAPI,omitempty"`
// the vendor searches the web by itself (provider.Searches)
@@ -605,7 +607,7 @@ func providerInfoWith(p provider.Provider, agents []agentUse, desktop map[string
ID: p.ID, Name: p.Name, Icon: p.Icon, Preset: p.Preset, Host: p.Host(),
Chat: p.Chat, Responses: p.Responses, Anthropic: p.Anthropic, Gemini: p.Gemini, Decide: p.Decide, BaseAPI: p.BaseAPI, ModelTest: p.ModelTest(), TestsAs: p.TestClients(), DecideTest: p.AsksDecideModels(), MarksKinds: p.MarksKinds(),
Catalog: p.Catalog, Website: p.Website, KeysURL: p.KeysURL,
- Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), LimitRestOff: p.LimitRestOff, NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: p.PassUserAgent, OwnClient: p.OwnClient(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, ModelsURL: p.ModelsURL,
+ Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, ReplyHeaders: p.ReplyHeaders, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), LimitRestOff: p.LimitRestOff, NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: p.PassUserAgent, OwnClient: p.OwnClient(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, ModelsURL: p.ModelsURL,
Ready: p.Ready(), Chosen: p.Picks(), Models: []modelJSON{}, Agents: []providerAgent{},
Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, CapStop: p.CapStop, CapCareful: p.CapCareful, Unlisted: p.Unlisted, NoModels: p.ServesNone(), Off: p.Off, Contexts: p.Contexts,
MaxConcurrency: p.MaxConcurrency, PluginConcurrency: p.PluginConcurrency(), PriceRate: p.PriceRate,
@@ -1399,6 +1401,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
pr.ZhipuTeam = in.ZhipuTeam
pr.NoModels = in.NoModels
pr.ExtraBody = in.ExtraBody
+ pr.ReplyHeaders = in.ReplyHeaders
pr.SecretAccessKey = in.SecretAccessKey
pr.Vertex = in.Vertex
pr.Searches = in.Searches
@@ -1436,6 +1439,11 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
fail(rw, errors.New(why))
return
}
+ // and reply headers magpie never passes on (#4145)
+ if why := provider.ReplyHeadersOK(in.ReplyHeaders); why != "" {
+ fail(rw, errors.New(why))
+ return
+ }
cc, keepCC, err := concurrencyOf(req.MaxConcurrency)
if err != nil {
fail(rw, err)
@@ -1624,6 +1632,10 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
if in.ExtraBody == nil && old != nil {
in.ExtraBody = old.ExtraBody
}
+ // and the reply headers passed on: [] clears them
+ if in.ReplyHeaders == nil && old != nil {
+ in.ReplyHeaders = old.ReplyHeaders
+ }
// Select none likewise, while nothing is picked (#1818)
if req.NoModels == nil && old != nil {
in.NoModels = old.NoModels
diff --git a/internal/gui/reply_headers_test.go b/internal/gui/reply_headers_test.go
new file mode 100644
index 000000000..de05811f0
--- /dev/null
+++ b/internal/gui/reply_headers_test.go
@@ -0,0 +1,48 @@
+package gui
+
+import (
+ "encoding/json"
+ "os"
+ "regexp"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// The editor refuses the reply headers a save would (#4145): its lists are
+// provider.ReplyHeaderPassable's.
+func TestReplyHeadersNeverAsTheProvidersAre(t *testing.T) {
+ js, err := os.ReadFile("assets/app.js")
+ if err != nil {
+ t.Fatal(err)
+ }
+ listOf := func(name string) []string {
+ m := regexp.MustCompile(`const ` + name + ` = (\[[^\]]*\]);`).FindSubmatch(js)
+ if m == nil {
+ t.Fatalf("no %s in app.js", name)
+ }
+ var out []string
+ if err := json.Unmarshal(m[1], &out); err != nil {
+ t.Fatal(err)
+ }
+ slices.Sort(out)
+ return out
+ }
+ names, prefixes := provider.ReplyHeadersNever()
+ lower := func(xs []string) []string {
+ out := make([]string, len(xs))
+ for i, x := range xs {
+ out[i] = strings.ToLower(x)
+ }
+ slices.Sort(out)
+ return out
+ }
+ if got, want := listOf("REPLY_NEVER"), lower(names); !slices.Equal(got, want) {
+ t.Errorf("app.js REPLY_NEVER %q, provider %q", got, want)
+ }
+ if got, want := listOf("REPLY_NEVER_PREFIX"), lower(prefixes); !slices.Equal(got, want) {
+ t.Errorf("app.js REPLY_NEVER_PREFIX %q, provider %q", got, want)
+ }
+}
diff --git a/internal/gui/tests/reply-headers.test.cjs b/internal/gui/tests/reply-headers.test.cjs
new file mode 100644
index 000000000..47078a5c7
--- /dev/null
+++ b/internal/gui/tests/reply-headers.test.cjs
@@ -0,0 +1,118 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// A provider's Reply headers (#4145, codex-cn: a relay's X-OneAPI-Request-ID
+// lost on the way to the agent): the editor shows the saved list, a Save
+// sends what is typed as a list, refuses a name magpie never passes on
+// (Set-Cookie, Content-*, a name that isn't one) and says why, and an
+// emptied box clears it ([]). It fits a narrow window. In English,
+// Chinese, Traditional Chinese, Japanese and German, Chromium and WebKit.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+
+const assets = path.resolve(__dirname, "../assets");
+const relay = {
+ id: "relay", name: "Relay", preset: "", icon: "generic", chat: "https://relay.example.com/v1", responses: "", anthropic: "", catalog: "", baseAPI: "chat",
+ models: [{ id: "gpt-5.1", name: "gpt-5.1", on: true }], agents: [], fallback: [], headers: {},
+ replyHeaders: ["X-Trace-Id"],
+ key: { set: true, masked: "sk-…one" }, keyList: [{ id: "k1", name: "", masked: "sk-…one", on: true, active: true }],
+ balanceToken: { takes: false, set: false }, proxy: "",
+};
+
+function serve(lang, calls) {
+ const providers = { providers: [relay], presets: [], excluded: [], gateway: { running: true, window: true } };
+ const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
+ return async (route) => {
+ const url = new URL(route.request().url());
+ const json = (data) => route.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
+ if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json(state);
+ if (url.pathname === "/api/providers") return json(providers);
+ if (url.pathname === "/api/groups") return json({ groups: [] });
+ if (url.pathname.startsWith("/api/provider/")) {
+ calls.push([url.pathname.slice(14), route.request().postDataJSON()]);
+ if (url.pathname === "/api/provider/save") return json(providers);
+ }
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
+ };
+}
+
+const words = {
+ en: { label: "Reply headers", save: "Save", never: "is never passed on", name: "isn't a header name" },
+ zh: { label: "透传响应头", save: "保存", never: "永不透传", name: "不是有效的响应头名称" },
+ "zh-TW": { label: "透傳回應標頭", save: "儲存", never: "一律不透傳", name: "不是有效的標頭名稱" },
+ ja: { label: "レスポンスヘッダー", save: "保存", never: "決して渡しません", name: "ヘッダー名ではありません" },
+ de: { label: "Antwort-Header", save: "Speichern", never: "wird nie weitergegeben", name: "ist kein Header-Name" },
+};
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of Object.keys(words)) {
+ const w = words[lang];
+ test(`${engine} ${lang}: Reply headers are shown, refused, saved and cleared`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ t.after(() => browser.close());
+ const page = await (await browser.newContext({ viewport: { width: 900, height: 900 }, reducedMotion: "reduce" })).newPage();
+ page.setDefaultTimeout(5000);
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ const calls = [];
+ await page.route("**/*", serve(lang, calls));
+ await page.goto("http://magpie.test/?view=providers");
+ const open = async () => {
+ await page.locator(".row.provider", { hasText: "Relay" }).click();
+ const ed = page.locator(".editor");
+ await ed.locator("input.reply-headers").waitFor();
+ return ed;
+ };
+ const save = async (ed) => {
+ await ed.locator(".bar").getByRole("button", { name: w.save, exact: true }).click();
+ for (let i = 0; i < 20 && !calls.some((c) => c[0] === "save"); i++) await page.waitForTimeout(50);
+ };
+ let ed = await open();
+ const box = ed.locator("input.reply-headers");
+ assert.equal(await page.locator(`label[for="${await box.getAttribute("id")}"]`).textContent(), w.label, "labelled");
+ assert.equal(await box.inputValue(), "X-Trace-Id", "the saved list is shown");
+
+ // refused before a Save, with why, the box focused
+ for (const [text, want] of [["x-trace-id, set-cookie", w.never], ["Content-Encoding", w.never], ["Access-Control-Allow-Origin", w.never], ["x trace", w.name]]) {
+ await box.fill(text);
+ await save(ed);
+ assert.deepEqual(calls.map((c) => c[0]), [], `${text}: nothing saved`);
+ const said = await ed.locator(".editor-error").textContent();
+ assert.ok(said.includes(want) && said.includes(w.label), `${text}: ${said}`);
+ assert.ok(await box.evaluate((e) => e === document.activeElement), "the box is focused");
+ }
+
+ // the narrow window: the box stays inside the editor
+ await page.setViewportSize({ width: 420, height: 900 });
+ const fits = await page.evaluate(() => {
+ const ed = document.querySelector(".editor").getBoundingClientRect();
+ const r = document.querySelector("input.reply-headers").getBoundingClientRect();
+ return r.left >= ed.left - 0.5 && r.right <= ed.right + 0.5;
+ });
+ assert.ok(fits, "fits a narrow window");
+ assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "no sideways scroll");
+ await page.setViewportSize({ width: 900, height: 900 });
+
+ await box.fill(" x-oneapi-request-id ,X-Trace-Id,, ");
+ await save(ed);
+ const saved = calls.find((c) => c[0] === "save");
+ assert.ok(saved, "saved");
+ assert.deepEqual(saved[1].replyHeaders, ["x-oneapi-request-id", "X-Trace-Id"], "saved as a list");
+
+ // emptied: a Save clears it
+ calls.length = 0;
+ await page.locator(".editor").waitFor({ state: "hidden" });
+ ed = await open();
+ await ed.locator("input.reply-headers").fill("");
+ await save(ed);
+ assert.deepEqual(calls.find((c) => c[0] === "save")?.[1].replyHeaders, [], "an empty box clears it");
+ assert.deepEqual(errors, []);
+ });
+ }
+}
diff --git a/internal/provider/provider.go b/internal/provider/provider.go
index 82af68cb4..a8e1f62fe 100644
--- a/internal/provider/provider.go
+++ b/internal/provider/provider.go
@@ -227,6 +227,15 @@ type Provider struct {
// id (extrabody.go). Signed-in agent accounts ignore it, as Headers.
ExtraBody ExtraBody `json:"extraBody,omitempty"`
+ // ReplyHeaders are headers of the vendor's reply passed on to the
+ // agent, beside DefaultReplyHeaders, whatever their case (#4145): a
+ // relay's own request id (X-Oneapi-Request-Id), for the agent's side
+ // to match with the relay's logs. Streamed or whole, an error too, on
+ // every API, translated or not; only the reply the agent is given
+ // has its headers passed on. One magpie writes itself, or that would
+ // act on magpie's address, never is (ReplyHeaderPassable).
+ ReplyHeaders []string `json:"replyHeaders,omitempty"`
+
// Searches says the vendor answers a web search tool offered on its
// Anthropic or Responses API by itself (web_search_20250305,
// web_search): a relay in front of Anthropic's or OpenAI's API, which
@@ -674,6 +683,7 @@ func (p Provider) clone() Provider {
p.AutoInclude = slices.Clone(p.AutoInclude)
p.Headers = maps.Clone(p.Headers)
p.ExtraBody = maps.Clone(p.ExtraBody)
+ p.ReplyHeaders = slices.Clone(p.ReplyHeaders)
p.AccountProxies = maps.Clone(p.AccountProxies)
p.AccountCaps = maps.Clone(p.AccountCaps)
p.AccountNames = maps.Clone(p.AccountNames)
@@ -772,8 +782,10 @@ func withPicks(a, pk Provider) Provider {
// the extra body fields written for it, which a ChatGPT account's
// requests take (WithExtraBody, magpie-releases#18)
a.ExtraBody = pk.ExtraBody
- // and the image models unticked in its editor (HiddenDrawers)
+ // the image models unticked in its editor (HiddenDrawers)
a.HiddenDrawers = pk.HiddenDrawers
+ // and the headers of its replies passed on (#4145)
+ a.ReplyHeaders = pk.ReplyHeaders
return a
}
@@ -1461,6 +1473,7 @@ func normalize(p Provider) Provider {
// endpoints and catalog, the headers say which workspace or app it is
p.Headers = cleanHeaders(p.Headers)
p.ExtraBody = cleanExtraBody(p.ExtraBody)
+ p.ReplyHeaders = cleanReplyHeaders(p.ReplyHeaders)
if pr := Preset(p.Preset); pr != nil {
if p.Icon == "" {
p.Icon = pr.Icon
diff --git a/internal/provider/replyheaders.go b/internal/provider/replyheaders.go
new file mode 100644
index 000000000..647d38d9b
--- /dev/null
+++ b/internal/provider/replyheaders.go
@@ -0,0 +1,147 @@
+package provider
+
+// Headers of a vendor's reply the gateway passes on to the agent
+// (ReplyHeaders, #4145): the vendor's own request id, which the agent's
+// side can then match with the vendor's logs.
+
+import (
+ "net/http"
+ "slices"
+ "strings"
+)
+
+// DefaultReplyHeaders are passed on from every provider's reply, without a
+// word from the user: request ids, opaque to everyone but the vendor, which
+// magpie sets no header of its own under. OpenAI's and Anthropic's names,
+// and one-api's (new-api and the relays built on it).
+var DefaultReplyHeaders = []string{"Request-Id", "X-Request-Id", "X-Oneapi-Request-Id"}
+
+// replyHeadersNever are never passed on, listed or not: the reply's own
+// framing, which magpie writes for what it sends (a translated or
+// unpacked body has another length, encoding and type), what is said of
+// the connection, cookies and sign-ins of the vendor's that would be set
+// on magpie's address, and what tells a browser to trust or reach magpie
+// otherwise. The prefixes below go with them.
+var replyHeadersNever = map[string]bool{
+ "Connection": true, "Keep-Alive": true, "Te": true, "Trailer": true, "Transfer-Encoding": true, "Upgrade": true,
+ "Cache-Control": true, "X-Accel-Buffering": true, "Retry-After": true, "Date": true, "Server": true, "Vary": true,
+ "Set-Cookie": true, "Set-Cookie2": true, "Cookie": true, "Authorization": true, "Www-Authenticate": true,
+ "Authentication-Info": true, "X-Api-Key": true, "Api-Key": true,
+ "Location": true, "Refresh": true, "Link": true, "Alt-Svc": true, "Strict-Transport-Security": true,
+ "Content-Security-Policy": true, "Origin": true, "Host": true, "Clear-Site-Data": true,
+}
+
+// replyHeadersNeverPrefix: Content-Length, -Encoding, -Type and the rest;
+// Proxy-Authenticate and the rest; CORS's, which would let a page read
+// magpie; Sec- fetch metadata; and magpie's own, which the gateway reads
+// as said by the magpie that answered (a rest, a refusal, a key limit).
+var replyHeadersNeverPrefix = []string{"Content-", "Proxy-", "Access-Control-", "Sec-", "X-Magpie-"}
+
+// ReplyHeaderPassable says whether a header of a vendor's reply may be
+// passed on to the agent at all.
+func ReplyHeaderPassable(name string) bool {
+ k := http.CanonicalHeaderKey(strings.TrimSpace(name))
+ if !validHeaderName(k) || replyHeadersNever[k] {
+ return false
+ }
+ for _, p := range replyHeadersNeverPrefix {
+ if strings.HasPrefix(k, p) {
+ return false
+ }
+ }
+ return true
+}
+
+// validHeaderName is an HTTP token (RFC 9110): no space, colon or control
+// character.
+func validHeaderName(k string) bool {
+ if k == "" {
+ return false
+ }
+ for _, r := range k {
+ if r <= ' ' || r >= 0x7f || strings.ContainsRune("\"(),/:;<=>?@[\\]{}", r) {
+ return false
+ }
+ }
+ return true
+}
+
+// ReplyHeadersOK says what is wrong with a list of reply headers to pass
+// on, "" when nothing is: a name that isn't one, or one magpie never
+// passes on.
+func ReplyHeadersOK(names []string) string {
+ for _, n := range names {
+ n = strings.TrimSpace(n)
+ if n == "" {
+ continue
+ }
+ if !validHeaderName(n) {
+ return "\"" + n + "\" isn't a header name"
+ }
+ if !ReplyHeaderPassable(n) {
+ return n + " is never passed on: magpie writes it, or it would act on magpie's own address"
+ }
+ }
+ return ""
+}
+
+// cleanReplyHeaders is the list as saved: names trimmed and canonical,
+// each once, what can't be passed on dropped; nil when nothing is left.
+func cleanReplyHeaders(names []string) []string {
+ var out []string
+ for _, n := range names {
+ k := http.CanonicalHeaderKey(strings.TrimSpace(n))
+ if ReplyHeaderPassable(k) && !slices.Contains(out, k) {
+ out = append(out, k)
+ }
+ }
+ return out
+}
+
+// PassedReplyHeaders are the headers of the vendor's reply h that go on to
+// the agent: the defaults and the ones the user listed for the provider,
+// matched whatever their case, never one ReplyHeaderPassable refuses. nil
+// when there are none.
+func (p Provider) PassedReplyHeaders(h http.Header) http.Header {
+ if len(h) == 0 {
+ return nil
+ }
+ var out http.Header
+ for _, names := range [][]string{DefaultReplyHeaders, p.ReplyHeaders} {
+ for _, n := range names {
+ k := http.CanonicalHeaderKey(strings.TrimSpace(n))
+ if out[k] != nil || !ReplyHeaderPassable(k) {
+ continue
+ }
+ vs := h.Values(k)
+ if len(vs) == 0 {
+ // a header map filled by hand, its keys not canonical
+ for hk, hv := range h {
+ if strings.EqualFold(hk, k) {
+ vs = hv
+ break
+ }
+ }
+ }
+ if len(vs) == 0 {
+ continue
+ }
+ if out == nil {
+ out = http.Header{}
+ }
+ out[k] = slices.Clone(vs)
+ }
+ }
+ return out
+}
+
+// ReplyHeadersNever are the names and prefixes ReplyHeaderPassable
+// refuses, for the GUI's own check of a list typed (its test holds the
+// two the same).
+func ReplyHeadersNever() (names, prefixes []string) {
+ for k := range replyHeadersNever {
+ names = append(names, k)
+ }
+ slices.Sort(names)
+ return names, slices.Clone(replyHeadersNeverPrefix)
+}
diff --git a/internal/provider/replyheaders_test.go b/internal/provider/replyheaders_test.go
new file mode 100644
index 000000000..4c17090a0
--- /dev/null
+++ b/internal/provider/replyheaders_test.go
@@ -0,0 +1,38 @@
+package provider
+
+import (
+ "net/http"
+ "slices"
+ "testing"
+)
+
+// #4145: the list is kept canonical and once each, never with a header
+// magpie doesn't pass on; matching ignores case.
+func TestReplyHeadersList(t *testing.T) {
+ got := cleanReplyHeaders([]string{" x-oneapi-request-id ", "X-OneAPI-Request-ID", "set-cookie", "content-length", "access-control-allow-origin", "x-magpie-rest", "bad name", "", "x-trace"})
+ if want := []string{"X-Oneapi-Request-Id", "X-Trace"}; !slices.Equal(got, want) {
+ t.Errorf("cleaned %q, want %q", got, want)
+ }
+ for _, bad := range [][]string{{"Set-Cookie"}, {"Content-Encoding"}, {"Transfer-Encoding"}, {"Authorization"}, {"Proxy-Authenticate"}, {"X-Magpie-Note"}, {"a b"}, {"a:b"}} {
+ if ReplyHeadersOK(bad) == "" {
+ t.Errorf("%q passed as OK", bad)
+ }
+ }
+ if msg := ReplyHeadersOK([]string{"x-oneapi-request-id", " ", "X-Trace"}); msg != "" {
+ t.Errorf("OK list refused: %s", msg)
+ }
+ p := Provider{ReplyHeaders: []string{"x-trace"}}
+ h := http.Header{"X-Request-Id": {"r1"}, "x-trace": {"t1"}, "Set-Cookie": {"s"}, "X-Other": {"o"}}
+ out := p.PassedReplyHeaders(h)
+ if len(out) != 2 || out.Get("X-Request-Id") != "r1" || out.Get("X-Trace") != "t1" {
+ t.Errorf("passed %v", out)
+ }
+ // a list saved by hand with a denied name still passes nothing of it
+ p.ReplyHeaders = []string{"Set-Cookie"}
+ if out := p.PassedReplyHeaders(h); out.Get("Set-Cookie") != "" {
+ t.Errorf("passed %v", out)
+ }
+ if (Provider{}).PassedReplyHeaders(http.Header{"X-Other": {"o"}}) != nil {
+ t.Error("passed headers nobody listed")
+ }
+}