gateway: a provider's listed reply headers and the vendor's request ids reach the agent (#4145)
codex-cn (#4145, and smtmsnyw on X: "Magpie代理把我原始模型响应头给吃了"): a relay's X-OneAPI-Request-ID never reached the agent, so a failed or slow call couldn't be matched with the relay's logs. What changes - provider.ReplyHeaders (`replyHeaders` in providers.json, the editor's Advanced > Reply headers, comma-separated): headers of the vendor's reply passed on to the agent, matched whatever their case, saved canonical. Request-Id, X-Request-Id and X-Oneapi-Request-Id go from every provider without being listed (DefaultReplyHeaders): opaque ids, under names magpie sets nothing of its own, and passthrough already passed the first two on a 2xx. Nothing the user set is overridden: the list only adds. - Never passed on, listed or not (ReplyHeaderPassable): Content-*, hop-by-hop and Transfer-Encoding (magpie writes the body it sends), Set-Cookie/Cookie/Authorization/WWW-Authenticate/Proxy-*/API keys, Access-Control-* and Sec-* (would let a page read magpie), Location, HSTS, CSP, Alt-Svc and the like, Retry-After/Cache-Control/Date/ Server/Vary/X-Accel-Buffering (magpie's own), and X-Magpie-* (the gateway reads those as notes of a try). Save refuses such a name (ReplyHeadersOK); the editor says why before the save in every language (replyHeadersOf), its list held to Go's by TestReplyHeadersNeverAsTheProvidersAre; a name written by hand is dropped on read. Where it's applied - One choke point: forward (every model API, relayed or translated, a plugin's fetch, a signed-in account) and retrieveAs (embeddings, audio) tell the reply they return to the try that asked, through the try's context (withReplySink/heardReply). The sink is the try's holdWriter, so a try that fails and is let go takes its vendor's headers with it: a fallback's reply carries the answering provider's alone, and a failure released to the agent carries its own. Only the try's own provider is heard (a side model or searcher isn't); within a try the last reply replaces earlier ones (a refused beta asked again, a Mantle reroute); after the try has its status nothing more is heard (a cut reply's continuation). count_tokens writes them directly, and the passthrough effort-none probe's kept refusal counts as the reply. - Not covered: images and video (#4211), and the Codex backend relay (which copies its reply's headers whole already). A stream whose headers keepAlive sent before the vendor answered (past keepHeldAfter) can't carry them. - Docs: providers-accounts.md (Reply headers row), gateway-routing.md. What was run - New tests: TestReplyHeadersPassedOn (Anthropic relayed and Chat translated x whole and stream x 200 and 400), TestReplyHeadersOfThe- ReplyGiven (429 then fallback to another provider), TestReplyHeadersOnCountTokens, TestReplyHeadersOnEmbeddings (200 and 400), TestReplyHeadersList, TestReplyHeadersNeverAsTheProvidersAre: pass on mote (macOS), on xiaomi-gpu (win-run) and in WSL (wsl-run). - Broken by hand: without the main loop's withReplySink every TestReplyHeadersPassedOn subtest and the fallback test go red (ids ""); with the deny map ignored, Set-Cookie passes and 8 subtests go red; without count_tokens' passReplyHeaders its test goes red. - gofmt -l clean; go vet ./internal/... for darwin, linux (-tags nogui) and windows: ok. - At this commit rebased on5f6a46630(165a97b35; the plugin-move fix, a GUI test file only, came after the Go run) (wsl-run, magpie-desktop): gofmt -l clean; GOOS=linux -tags nogui and GOOS=windows go vet of gateway, provider and gui: ok. go test -tags nogui -count=1 -timeout 45m: internal/gateway ok 710s, internal/gui ok 1194s, internal/provider has one red, TestClaudeAccountServesModelsListedSinceItsFetch, which is #3174 (red in a full run, green alone). It passes 3 of 3 alone here. - Earlier, atc1660481con WSL: provider ok 235s, gui ok 1062s, gateway ok 1071s. win-run on xiaomi-gpu: the new tests ok. - GUI at this head (WSL, chromium and webkit, en zh zh-TW ja de where the file runs them): the 32 files that reach the provider editor (Advanced, extra body, i18n), reply-headers.test.cjs and image-models-hide: 752 pass, 2 fail. The fails were plugin-move webkit en and zh: the taller Advanced section made WebKit's last wheel land after the test read the scroll (367, then 397). Fixed in the commit before this one. provider-gemini webkit is red alone on origin/main95b40fb2ftoo, in a different language each run: #4210. Earlier reds in a 32-file run (insecure-tls, native-compaction, limit-rest, unmasked, vertex-editor) passed alone. - The mote GUI and Go run sat behind a load of 230-330 for 1.5h and was stopped with no result, so these ran on WSL. - At the pushed tree (rebased ond95822eb3, 30 more files from main; app.js, i18n.js and provider.go among them), wsl-run: gofmt clean, vet ok, go test -run 'ReplyHeaders|Budget|I18n|Keys' of gateway, provider and gui ok. The GUI check of reply-headers, plugin-move and i18n-keys was still running when this was pushed. - Real use: magpie serve (go build -tags nogui) in a sandbox HOME on mote with a fake relay and replyHeaders ["x-trace"]: /v1/messages translated to Chat, a streamed /v1/chat/completions and a 400 each answered with X-Oneapi-Request-Id, X-Request-Id and X-Trace, and no Set-Cookie or unlisted header. Not tried: a plugin-served provider's reply (goes through forward like the rest, no test with a real plugin host), a real one-api/new-api relay, the whole GUI suite (32 files that touch the provider editor's Advanced section, extra body and i18n were run), and a full ./... run.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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, []);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user