Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: ${sessionId} and ${threadId} in a provider's Headers are the conversation's ids
yetone2d11cde056d6
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex 78bc1b3c5..a0034aaaa 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -21,7 +21,8 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi| Anthropic key header | A provider of the user's own on a third-party Anthropic API is sent its key as both `x-api-key` and `Authorization: Bearer` (`AuthHeaders`); Anthropic's own API, Bedrock, Azure, Vertex and every subscription account have their own and never this choice (`BothAuth` is false). Some services refuse the two together with a 401 (Aloha on Discord: "鉴权 header, x-api-key 和 Authorization 不可以同时存在"), so `Provider.AuthHeader` (`authHeader`: empty for both, `x-api-key` or `bearer`; anything else is read as empty) sends one alone, picked in the editor's Key header row (shown while `bothAuth` is true, or a custom provider's Anthropic URL is a third party's). Left at both, a 401 whose body `catalog.BothAuthRefused` tells as that refusal (it names both headers and says they can't be together, in Chinese or English) is asked once more with x-api-key alone, then Bearer alone (`RetryOneAuth`, in the gateway's `forwardOnce` and the provider test's `probe`; `catalog.fetchOne` for the model list through `listAuth`); the first not answered 401 or 403 is kept with `LearnAuthHeader`, which never changes a pick the user made. A request carrying the user's own `Authorization` or `x-api-key` header is never retried. | [`authheader.go`](../../internal/provider/authheader.go), [`test.go`](../../internal/provider/test.go), [`catalog/authheader.go`](../../internal/catalog/authheader.go), [`catalog/live.go`](../../internal/catalog/live.go) || 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) |+| 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. `${threadId}` is the thread the agent names within that session: Codex's `thread_id` (its turn metadata or `thread-id` header, `threadOf`), which a subagent has of its own while it shares its lead's session; read only when the provider's headers or extraBody name it (`AsksThread`). No other agent names a thread, and there `${threadId}` is the session's id. `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) |+| Headers' conversation ids | `${sessionId}` and `${threadId}` in a value of the provider's `Headers` (Axel Burks on Discord: a gateway in front of several Azure accounts routes one conversation to one account by a session header) are the ids extraBody gets, on every request `Sign` signs: the gateway carries the conversation to it in the request's context (`WithConversation`), on every API and `count_tokens` too. A request that is no turn of a conversation (a model list, a balance, an embedding, image or video, System One's decision model, a search) gets an id of its own, `magpie-` and 12 random letters, new each time (`OwnConversation`); Test and Detect's is their `magpie-test-` id, the same in headers and body. A literal placeholder is never sent. In a header the ids go without control characters (`headerSafe`): an agent's session id with CR or LF in it would split the header, or Go would fail the request. A value with neither placeholder goes as written (`SentHeaders`). | [`conversation.go`](../../internal/provider/conversation.go), [`account.go`](../../internal/provider/account.go), [`gateway.go`](../../internal/gateway/gateway.go), [`codex_resume.go`](../../internal/gateway/codex_resume.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) |@@ -313,6 +314,8 @@ go test -tags nogui -run 'Vertex' .go test -tags nogui ./internal/provider ./internal/agent -run 'TestAccountDiscovery'node --test internal/gui/tests/account-discovery.test.cjsgo test -tags nogui ./internal/provider ./internal/gateway ./internal/gui -run ExtraBody+go test -tags nogui ./internal/provider ./internal/gateway -run 'TestHeadersTakeTheConversation|TestSentHeaders|TestExtraBodyThread|TestHeadersCarry|TestHeadersDerived|TestThreadIDIsCodexThread|TestHeaderSessionWithoutCRLF'+node --test internal/gui/tests/header-session-hint.test.cjsgo test -tags nogui ./internal/provider ./internal/davsync ./internal/gui -run 'KeepsAccessKey|ProvidersFileWithBOM|AccessKeySecret|PlanCardWithoutAccessKey|KeepsVolcengineSecret'node --test internal/gui/tests/extra-body.test.cjsgo test -tags nogui ./internal/provider ./internal/gateway ./internal/gui -run ReplyHeadersdiff --git a/internal/gateway/codex_resume.go b/internal/gateway/codex_resume.goindex 52fd6ea5f..d5602a8e1 100644--- a/internal/gateway/codex_resume.go+++ b/internal/gateway/codex_resume.go@@ -80,6 +80,19 @@ func codexSubagentThread(h http.Header, meta sessionMetadata) string {return ""}+// threadOf is the thread a request names apart from its session, for+// ${threadId} (provider/conversation.go): Codex's thread_id, in its turn+// metadata or its thread-id header. "" when it names none — no other+// agent does — and ${threadId} is then the session's.+func threadOf(h http.Header, body []byte) string {+ for _, t := range []string{requestSessionMetadata(h, body).Thread, h.Get("thread-id")} {+ if t = strings.TrimSpace(t); t != "" && len(t) <= 128 {+ return t+ }+ }+ return ""+}+// codexResumeTo is the model a Codex subagent's request goes on instead// of asked: the one its thread was spawned on, when it asks for another.// "" leaves the request as it came: not a Codex subagent's, a thread notdiff --git a/internal/gateway/conversation_headers_test.go b/internal/gateway/conversation_headers_test.gonew file mode 100644index 000000000..ba1d28230--- /dev/null+++ b/internal/gateway/conversation_headers_test.go@@ -0,0 +1,126 @@+package gateway++import (+ "strings"+ "testing"++ "github.com/tidwall/gjson"+ "github.com/yetone/magpie/internal/provider"+)++// withHeaders gives the saved provider id the headers.+func withHeaders(t *testing.T, id string, h map[string]string) {+ t.Helper()+ p, err := provider.Find(id)+ if err != nil {+ t.Fatal(err)+ }+ p.Headers = h+ if err := provider.Save(*p); err != nil {+ t.Fatal(err)+ }+}++// Axel Burks on Discord: a gateway in front of several Azure accounts+// routes one conversation to one account by a session id in a header,+// so its reasoning can be read back. ${sessionId} in a provider's header+// is the conversation's id, as in its extraBody — on every API, streamed+// or whole, relayed or translated, and on count_tokens.+func TestHeadersCarryTheSession(t *testing.T) {+ head := map[string]string{"X-Session-Id": "${sessionId}", "X-Route": "acct/${sessionId}/${threadId}"}+ for _, c := range []struct {+ name, path, body string+ proto provider.Protocol+ reply, ctype string+ }{+ {"chat streamed", "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`, provider.Chat, chatReply, ""},+ {"responses whole", "/v1/responses", `{"model":"m1","input":"hi"}`, provider.Responses, responsesReply, "application/json"},+ {"messages", "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, provider.Anthropic, anthropicReply(), ""},+ {"messages to chat", "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, provider.Chat, chatReply, ""},+ {"count_tokens", "/v1/messages/count_tokens", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, provider.Anthropic, `{"input_tokens":3}`, "application/json"},+ } {+ t.Run(c.name, func(t *testing.T) {+ f := &fake{t: t, reply: c.reply, ctype: c.ctype}+ setup(t, c.proto, f)+ withHeaders(t, "fake", head)+ code, body := postWith(t, c.path, c.body, map[string]string{"session_id": "sess-42"})+ if code != 200 {+ t.Fatalf("status %d: %s", code, body)+ }+ if got := f.head.Get("X-Session-Id"); got != "sess-42" {+ t.Errorf("X-Session-Id %q", got)+ }+ if got := f.head.Get("X-Route"); got != "acct/sess-42/sess-42" {+ t.Errorf("X-Route %q", got)+ }+ })+ }+}++// Without a session header, the header's id is the one extraBody gets:+// derived from the first user message, the same every turn, never the+// literal.+func TestHeadersDerivedSessionMatchesExtraBody(t *testing.T) {+ f := &fake{t: t, reply: chatReply}+ setup(t, provider.Chat, f)+ withExtraBody(t, "fake", `{"user":"${sessionId}"}`)+ withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}"})+ ask := func(msgs string) string {+ t.Helper()+ if code, body := post(t, "/v1/chat/completions", `{"model":"m1","messages":`+msgs+`}`); code != 200 {+ t.Fatalf("status %d: %s", code, body)+ }+ h := f.head.Get("X-Session-Id")+ if b := gjson.GetBytes(f.got, "user").String(); h != b {+ t.Errorf("header %q, body %q", h, b)+ }+ return h+ }+ one := ask(`[{"role":"user","content":"first"}]`)+ again := ask(`[{"role":"user","content":"first"},{"role":"assistant","content":"OK"},{"role":"user","content":"next"}]`)+ if !strings.HasPrefix(one, "magpie-") || one != again {+ t.Errorf("ids %q %q", one, again)+ }+}++// Codex names a thread apart from its session: a subagent shares its+// lead's session_id and has a thread_id of its own. ${threadId} is it, in+// headers and extraBody; ${sessionId} stays the session's.+func TestThreadIDIsCodexThread(t *testing.T) {+ f := &fake{t: t, reply: responsesReply, ctype: "application/json"}+ setup(t, provider.Responses, f)+ withExtraBody(t, "fake", `{"metadata":{"thread":"${threadId}"}}`)+ withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}", "X-Thread-Id": "${threadId}"})+ for _, c := range []struct {+ name string+ head map[string]string+ want string+ }{+ {"turn metadata", map[string]string{"session_id": "root-1", "x-codex-turn-metadata": `{"session_id":"root-1","thread_id":"worker-1"}`}, "worker-1"},+ {"thread-id header", map[string]string{"session-id": "root-1", "thread-id": "worker-2"}, "worker-2"},+ {"none named", map[string]string{"session_id": "root-1"}, "root-1"},+ } {+ code, body := postWith(t, "/v1/responses", `{"model":"m1","input":"hi"}`, c.head)+ if code != 200 {+ t.Fatalf("%s: status %d: %s", c.name, code, body)+ }+ if s, th, b := f.head.Get("X-Session-Id"), f.head.Get("X-Thread-Id"), gjson.GetBytes(f.got, "metadata.thread").String(); s != "root-1" || th != c.want || b != c.want {+ t.Errorf("%s: session %q, thread %q, body %q", c.name, s, th, b)+ }+ }+}++// A session id is the agent's: a CR or LF in it never reaches a header,+// where it would split it or fail the request.+func TestHeaderSessionWithoutCRLF(t *testing.T) {+ f := &fake{t: t, reply: chatReply}+ setup(t, provider.Chat, f)+ withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}"})+ code, body := postWith(t, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, map[string]string{"x-session-id": "a\r\nX-Evil: 1"})+ if code != 200 {+ t.Fatalf("status %d: %s", code, body)+ }+ if got := f.head.Get("X-Session-Id"); got != "aX-Evil: 1" || f.head.Get("X-Evil") != "" {+ t.Errorf("X-Session-Id %q, headers %q", got, f.head)+ }+}diff --git a/internal/gateway/draw.go b/internal/gateway/draw.goindex 0c74c502b..6f3126a99 100644--- a/internal/gateway/draw.go+++ b/internal/gateway/draw.go@@ -1157,7 +1157,7 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url,} else {// Google's API keys go in their own headerreq.Header.Set("x-goog-api-key", p.Key)- for k, v := range p.Headers {+ for k, v := range p.SentHeaders(ctx) {catalog.PutUserHeader(req.Header, k, v)}}diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex bf1e4fa62..5fea4c7ac 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -4373,11 +4373,18 @@ func (s *Server) forwardOnce(ctx context.Context, p provider.Provider, to provid// auto mode's review goes to Claude alone (automode.go)betas, body = s.serverReview(p, asked, body)}+ // the conversation ${sessionId} and ${threadId} stand for in the+ // user's own fields and headers for the provider (conversation.go)+ conv := provider.Conversation{Session: cmp.Or(sessionOf(in), conversationID(in, body))}+ if p.AsksThread() {+ conv.Thread = threadOf(in, body)+ }+ ctx = provider.WithConversation(ctx, conv)if !countsTokens(path) {// the user's own fields for the provider (#1604), last, so the// body is as it goes out when they fill in what it lacks, and// before it is signed- extra := p.WithExtraBody(body, cmp.Or(sessionOf(in), conversationID(in, body)))+ extra := p.WithExtraBody(body, conv)if to == provider.Anthropic && !bytes.Equal(extra, body) {// a thinking budget the user's fields put in is fitted to a// model that thinks only adaptively as the agent's is, itsdiff --git a/internal/gateway/opencode_session.go b/internal/gateway/opencode_session.goindex a4b3b7751..465415e62 100644--- a/internal/gateway/opencode_session.go+++ b/internal/gateway/opencode_session.go@@ -51,8 +51,9 @@ func logOpenCodeRefusal(p provider.Provider, req *http.Request, status int) {session := "missing"if v := req.Header.Get("x-opencode-session"); v != "" {session = v[:min(len(v), 8)] + "…"- for k := range p.Headers {- if strings.EqualFold(k, "x-opencode-session") {+ for k, v := range p.Headers {+ // ${sessionId} in it is each conversation's own+ if strings.EqualFold(k, "x-opencode-session") && !strings.Contains(v, provider.SessionPlaceholder) && !strings.Contains(v, provider.ThreadPlaceholder) {session += " (the provider's own header: one session for every conversation)"}}diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.jsindex 2844d50d9..28af32a32 100644--- a/internal/gui/assets/app.js+++ b/internal/gui/assets/app.js@@ -9606,12 +9606,12 @@ function drawEditor(p, presetID) {// accounts returned above use the agent's own authentication headers.if (custom) {if (!draft.headers.length) draft.headers.push(["", ""]);- advIn.append(...field(t("Headers"), headerEditor(), t("Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.")));+ advIn.append(...field(t("Headers"), headerEditor(), t("Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.")));} else {- advIn.append(...field(t("Headers"), headerEditor(pr?.headerHints || []), t("Optional headers sent with every request to {p}, applied after auth.", { p: pr?.name || p?.name })));+ advIn.append(...field(t("Headers"), headerEditor(pr?.headerHints || []), t("Optional headers sent with every request to {p}, applied after auth. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.", { p: pr?.name || p?.name })));}// 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.")));+ 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; ${threadId} is the thread's, where the agent names one (Codex), else the same.")));// 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");diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.jsindex 3d5c47efb..cf92dbb24 100644--- a/internal/gui/assets/i18n.js+++ b/internal/gui/assets/i18n.js@@ -2133,15 +2133,15 @@ const I18N = {"its sign-in is gone; sign in again": "登录信息已不存在,请重新登录","Claude Code could not authenticate this account; sign in again in magpie": "Claude Code 无法认证此账号,请在 magpie 中重新登录","Tested {user}: {result}": "已测试 {user}:{result}",- "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.": "发给供应商的额外 HTTP 请求头,在鉴权后应用,用于需要私有鉴权方案的网关。",+ "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "发给供应商的额外 HTTP 请求头,在鉴权后应用,用于需要私有鉴权方案的网关。值里的 ${sessionId} 是会话的 ID;${threadId} 是线程的 ID(智能体单独给出时,如 Codex),否则与会话 ID 相同。","Header-Name": "请求头名称","value": "值","Remove header": "删除请求头","+ Add header": "+ 添加请求头","Add the {h} header; its value is yours to fill in": "添加 {h} 请求头,值由你填写",- "Optional headers sent with every request to {p}, applied after auth.": "随每个发往 {p} 的请求发送的可选请求头,在鉴权之后应用。",+ "Optional headers sent with every request to {p}, applied after auth. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "随每个发往 {p} 的请求发送的可选请求头,在鉴权之后应用。值里的 ${sessionId} 是会话的 ID;${threadId} 是线程的 ID(智能体单独给出时,如 Codex),否则与会话 ID 相同。","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。",+ "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; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "加进发往供应商的每个请求体里的 JSON 字段。请求里已有的字段会保留;写成 \"!name\" 则替换它。${sessionId} 是会话的 ID;${threadId} 是线程的 ID(智能体单独给出时,如 Codex),否则与会话 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 自身的地址",@@ -6233,15 +6233,15 @@ const I18N = {"its sign-in is gone; sign in again": "登入資訊已不存在,請重新登入","Claude Code could not authenticate this account; sign in again in magpie": "Claude Code 無法認證此帳號,請在 magpie 中重新登入","Tested {user}: {result}": "已測試 {user}:{result}",- "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.": "發給供應商的額外 HTTP 請求頭,在鑑權後應用,用於需要私有鑑權方案的閘道。",+ "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "發給供應商的額外 HTTP 請求頭,在鑑權後應用,用於需要私有鑑權方案的閘道。值裡的 ${sessionId} 是對話的 ID;${threadId} 是執行緒的 ID(代理單獨給出時,如 Codex),否則與對話 ID 相同。","Header-Name": "請求頭名稱","value": "值","Remove header": "刪除請求頭","+ Add header": "+ 新增請求頭","Add the {h} header; its value is yours to fill in": "新增 {h} 請求頭,值由你填寫",- "Optional headers sent with every request to {p}, applied after auth.": "隨每個發往 {p} 的請求傳送的可選請求頭,在鑑權之後應用。",+ "Optional headers sent with every request to {p}, applied after auth. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "隨每個發往 {p} 的請求傳送的可選請求頭,在鑑權之後應用。值裡的 ${sessionId} 是對話的 ID;${threadId} 是執行緒的 ID(代理單獨給出時,如 Codex),否則與對話 ID 相同。","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。",+ "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; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "加進發往供應商的每個請求體裡的 JSON 欄位。請求裡已有的欄位會保留;寫成 \"!name\" 則取代它。${sessionId} 是對話的 ID;${threadId} 是執行緒的 ID(代理單獨給出時,如 Codex),否則與對話 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 自身的位址",@@ -10310,15 +10310,15 @@ const I18N = {"its sign-in is gone; sign in again": "サインイン情報がありません。再度サインインしてください","Claude Code could not authenticate this account; sign in again in magpie": "Claude Code はこのアカウントを認証できませんでした。magpie で再度サインインしてください","Tested {user}: {result}": "{user} をテストしました:{result}",- "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.": "認証の後に適用される、ベンダーに送る追加の HTTP ヘッダー。独自の方式が必要なゲートウェイ向けです。",+ "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "認証の後に適用される、ベンダーに送る追加の HTTP ヘッダー。独自の方式が必要なゲートウェイ向けです。値の中の ${sessionId} は会話の ID、${threadId} はスレッドの ID です(エージェントが別に示す場合。Codex など)。示さない場合は会話の ID と同じです。","Header-Name": "Header-Name","value": "値","Remove header": "ヘッダーを削除","+ Add header": "+ ヘッダーを追加","Add the {h} header; its value is yours to fill in": "{h} ヘッダーを追加します。値は自分で入力してください",- "Optional headers sent with every request to {p}, applied after auth.": "{p} へのすべてのリクエストに付ける任意のヘッダー。認証の後に適用されます。",+ "Optional headers sent with every request to {p}, applied after auth. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "{p} へのすべてのリクエストに付ける任意のヘッダー。認証の後に適用されます。値の中の ${sessionId} は会話の ID、${threadId} はスレッドの ID です(エージェントが別に示す場合。Codex など)。示さない場合は会話の ID と同じです。","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 です。",+ "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; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "ベンダーへのすべてのリクエストのボディに加える JSON フィールド。リクエストにすでにあるフィールドはそのまま残り、\"!name\" と書くと置き換えます。${sessionId} は会話の ID、${threadId} はスレッドの ID です(エージェントが別に示す場合。Codex など)。示さない場合は会話の 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 自身のアドレスに作用するためです",@@ -14417,15 +14417,15 @@ const I18N = {"its sign-in is gone; sign in again": "die Anmeldung ist nicht mehr vorhanden; melden Sie sich erneut an","Claude Code could not authenticate this account; sign in again in magpie": "Claude Code konnte dieses Konto nicht authentifizieren; melden Sie sich in magpie erneut an","Tested {user}: {result}": "{user} getestet: {result}",- "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme.": "Zusätzliche HTTP-Header, die an den Anbieter gesendet und nach der Authentifizierung angewendet werden. Für Zugangspunkte mit eigenem Schema.",+ "Extra HTTP headers sent to the vendor, applied after auth. For gateways that need a private scheme. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "Zusätzliche HTTP-Header, die an den Anbieter gesendet und nach der Authentifizierung angewendet werden. Für Zugangspunkte mit eigenem Schema.${sessionId} in einem Wert ist die ID der Unterhaltung; ${threadId} die des Threads, wo der Agent einen eigenen nennt (Codex), sonst dieselbe.","Header-Name": "Header-Name","value": "Wert","Remove header": "Header entfernen","+ Add header": "+ Header hinzufügen","Add the {h} header; its value is yours to fill in": "Header {h} hinzufügen; den Wert tragen Sie selbst ein",- "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.",+ "Optional headers sent with every request to {p}, applied after auth. ${sessionId} in a value is the conversation's id; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "Optionale Header, die mit jeder Anfrage an {p} gesendet und nach der Authentifizierung angewendet werden.${sessionId} in einem Wert ist die ID der Unterhaltung; ${threadId} die des Threads, wo der Agent einen eigenen nennt (Codex), sonst dieselbe.","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.",+ "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; ${threadId} is the thread's, where the agent names one (Codex), else the same.": "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; ${threadId} die des Threads, wo der Agent einen eigenen nennt (Codex), sonst dieselbe.","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",diff --git a/internal/gui/tests/header-session-hint.test.cjs b/internal/gui/tests/header-session-hint.test.cjsnew file mode 100644index 000000000..db161b95c--- /dev/null+++ b/internal/gui/tests/header-session-hint.test.cjs@@ -0,0 +1,101 @@+// Run with Node's test runner and Playwright on the module path; see README.md.+// ${sessionId} and ${threadId} in a provider's Headers (Axel Burks on+// Discord: a gateway in front of several Azure accounts routes a+// conversation by a session id in a header). The editor's hint for+// Headers says what they stand for, as Extra body's does, and a value+// with one is saved as typed, for the gateway to fill in on each request.+// In English, Chinese, Traditional Chinese, Japanese and German, Chromium+// and WebKit, and at a narrow width.+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 gw = {+ id: "azgw", name: "Azure Gateway", preset: "", icon: "generic", chat: "", responses: "https://gw.example.com/v1", anthropic: "", catalog: "", baseAPI: "responses",+ models: [{ id: "gpt-5.1", name: "gpt-5.1", on: true }], agents: [], fallback: [], headers: { "X-Session-Id": "${sessionId}" },+ 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: [gw], 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: "" }); }+ };+}++// a word of each language's hint, so an untranslated one fails+const words = {+ en: { save: "Save", said: "conversation's id" },+ zh: { save: "保存", said: "会话的 ID" },+ "zh-TW": { save: "儲存", said: "對話的 ID" },+ ja: { save: "保存", said: "会話の ID" },+ de: { save: "Speichern", said: "ID der Unterhaltung" },+};++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}: Headers say what \${sessionId} and \${threadId} are, and keep them as typed`, 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");+ await page.locator(".row.provider", { hasText: "Azure Gateway" }).click();+ const ed = page.locator(".editor");+ await ed.locator(".headers").waitFor({ state: "attached" });++ const hintOf = (sel) => ed.locator(sel).locator("xpath=..").locator(":scope > .hint").textContent();+ for (const [what, sel] of [["Headers", ".headers"], ["Extra body", ".extra-body"]]) {+ const hint = await hintOf(sel);+ assert.ok(hint.includes("${sessionId}") && hint.includes("${threadId}"), `${what}'s hint: ${hint}`);+ assert.ok(hint.includes(w.said), `${what}'s hint in ${lang}: ${hint}`);+ }+ const value = ed.locator(".headers .pair input").nth(1);+ assert.equal(await value.inputValue(), "${sessionId}", "the saved value is shown as typed");+ await value.fill("route/${sessionId}/${threadId}");++ // the narrow window: the hint stays inside the editor+ await page.setViewportSize({ width: 420, height: 900 });+ const fits = await page.evaluate(() => {+ const ed = document.querySelector(".editor").getBoundingClientRect();+ const h = document.querySelector(".editor .headers").parentElement.querySelector(":scope > .hint");+ const r = h.getBoundingClientRect();+ return r.width === 0 || (r.left >= ed.left - 0.5 && r.right <= ed.right + 0.5);+ });+ assert.ok(fits, "the hint 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 ed.locator(".bar").getByRole("button", { name: w.save, exact: true }).click();+ for (let i = 0; i < 60 && !calls.some((c) => c[0] === "save"); i++) await page.waitForTimeout(50);+ const saved = calls.find((c) => c[0] === "save");+ assert.ok(saved, "saved");+ assert.deepEqual(saved[1].headers, { "X-Session-Id": "route/${sessionId}/${threadId}" }, "saved as typed");+ assert.deepEqual(errors, []);+ });+ }+}diff --git a/internal/provider/account.go b/internal/provider/account.goindex 3b628af8e..f228c8ef2 100644--- a/internal/provider/account.go+++ b/internal/provider/account.go@@ -232,7 +232,9 @@ func (p Provider) Sign(ctx context.Context, req *http.Request, proto Protocol, b// other case (PutUserHeader), so it is sent once, theirs.// A list the request has its own of (anthropic-beta) is added to, not// replaced: the user's betas go after those the agent asked.- for k, v := range p.Headers {+ // ${sessionId} and ${threadId} in a value are the conversation's+ // (conversation.go).+ for k, v := range p.SentHeaders(ctx) {if ListHeader(k) {MergeList(req.Header, k, v)continuediff --git a/internal/provider/azure.go b/internal/provider/azure.goindex ad0f118d3..3332b0cdd 100644--- a/internal/provider/azure.go+++ b/internal/provider/azure.go@@ -137,7 +137,7 @@ func (p Provider) azureModels(ctx context.Context) ([]catalog.Model, string, errfor k, v := range AuthHeaders(p, Chat) {headers[k] = v}- for k, v := range p.Headers {+ for k, v := range p.SentHeaders(ctx) {// the user's api-key, in any case, is the one sentfor had := range headers {if strings.EqualFold(had, k) {diff --git a/internal/provider/balance.go b/internal/provider/balance.goindex 67002f6fc..a9f364206 100644--- a/internal/provider/balance.go+++ b/internal/provider/balance.go@@ -1246,7 +1246,7 @@ func balanceRead(ctx context.Context, p Provider) (amount string, parts, split [// a named endpoint still gets the provider's headers, which is// where a new-api relay's New-Api-User goesif p.BalanceURL != "" {- for k, v := range p.Headers {+ for k, v := range p.SentHeaders(ctx) {req.Header.Set(k, withBalanceKey(v, p.Key, false))}}@@ -1255,7 +1255,7 @@ func balanceRead(ctx context.Context, p Provider) (amount string, parts, split [for k, v := range AuthHeaders(p, Chat) {req.Header.Set(k, v)}- for k, v := range p.Headers {+ for k, v := range p.SentHeaders(ctx) {req.Header.Set(k, withBalanceKey(v, p.Key, false))}}diff --git a/internal/provider/conversation.go b/internal/provider/conversation.gonew file mode 100644index 000000000..4399d06fb--- /dev/null+++ b/internal/provider/conversation.go@@ -0,0 +1,134 @@+package provider++// The conversation a request is part of, for ${sessionId} and ${threadId}+// in a provider's Headers and ExtraBody (Axel Burks on Discord: a gateway+// in front of several Azure accounts routes a conversation to one account+// by a session id in a header, so its sealed reasoning can be read back).+//+// - ${sessionId} is the session the agent names (its session header),+// else the id magpie derives from the conversation's first user+// message: the same for every turn of it.+// - ${threadId} is the thread the agent names within that session, where+// one names a thread apart from its session: Codex's thread_id (its+// turn metadata, or its thread-id header), which a subagent has of its+// own while it shares its lead's session_id. No other agent names one,+// and there a conversation is one thread: ${threadId} is ${sessionId}.+// - A request that is no turn of a conversation — a model list, a+// balance, an embedding or image, the editor's Test and Detect — goes+// on a conversation of its own: a new id each time ("magpie-" and 12+// random letters; Test and Detect's "magpie-test-…", the same one in+// its headers and body). A literal "${sessionId}" is never sent.+// - In a header the ids go without control characters (CR, LF, tab…):+// an id is the agent's, and a header can't carry them.++import (+ "context"+ "crypto/rand"+ "maps"+ "strings"+)++// ThreadPlaceholder is what a header or an extraBody string writes for the+// thread's id.+const ThreadPlaceholder = "${threadId}"++// Conversation is what ${sessionId} and ${threadId} stand for in a+// request: Thread "" is the session's own thread.+type Conversation struct{ Session, Thread string }++type conversationCtxKey struct{}++// WithConversation is ctx carrying the conversation its requests are part+// of, for the headers Sign puts on them.+func WithConversation(ctx context.Context, c Conversation) context.Context {+ return context.WithValue(ctx, conversationCtxKey{}, c)+}++// conversationOf is the conversation ctx carries, else one of the+// request's own.+func conversationOf(ctx context.Context) Conversation {+ if c, ok := ctx.Value(conversationCtxKey{}).(Conversation); ok && c.Session != "" {+ return c+ }+ return OwnConversation("magpie-")+}++// OwnConversation is a conversation of a request's own: prefix and 12+// random letters, its thread the same.+func OwnConversation(prefix string) Conversation {+ return Conversation{Session: prefix + strings.ToLower(rand.Text()[:12])}+}++// expand is s with the placeholders as c's ids.+func (c Conversation) expand(s string) string {+ if !strings.Contains(s, "${") {+ return s+ }+ thread := c.Thread+ if thread == "" {+ thread = c.Session+ }+ return strings.NewReplacer(SessionPlaceholder, c.Session, ThreadPlaceholder, thread).Replace(s)+}++// headerSafe is id without the bytes a header value can't carry: control+// characters, CR and LF among them.+func headerSafe(id string) string {+ return strings.Map(func(r rune) rune {+ if r < 0x20 || r == 0x7f {+ return -1+ }+ return r+ }, id)+}++// placeheld says s names the conversation.+func placeheld(s string) bool {+ return strings.Contains(s, SessionPlaceholder) || strings.Contains(s, ThreadPlaceholder)+}++// AsksThread says p's headers or extraBody name ${threadId}, which the+// gateway reads out of the request only then.+func (p Provider) AsksThread() bool {+ for _, v := range p.Headers {+ if strings.Contains(v, ThreadPlaceholder) {+ return true+ }+ }+ for _, v := range p.ExtraBody {+ if strings.Contains(string(v), ThreadPlaceholder) {+ return true+ }+ }+ return false+}++// SentHeaders are p's Headers as they go out on a request of ctx's+// conversation (conversationOf): the placeholders in their values as its+// ids, without control characters. p.Headers itself when no value has+// one.+func (p Provider) SentHeaders(ctx context.Context) map[string]string {+ return expandHeaders(p.Headers, func() Conversation { return conversationOf(ctx) })+}++// expandHeaders is h with the placeholders in its values as the ids of+// the conversation conv gives, asked only when a value has one.+func expandHeaders(h map[string]string, conv func() Conversation) map[string]string {+ var out map[string]string+ var c Conversation+ for k, v := range h {+ if !placeheld(v) {+ continue+ }+ if out == nil {+ out = maps.Clone(h)+ c = conv()+ c = Conversation{Session: headerSafe(c.Session), Thread: headerSafe(c.Thread)}+ }+ out[k] = c.expand(v)+ }+ if out == nil {+ return h+ }+ return out+}diff --git a/internal/provider/conversation_test.go b/internal/provider/conversation_test.gonew file mode 100644index 000000000..51775a7e5--- /dev/null+++ b/internal/provider/conversation_test.go@@ -0,0 +1,148 @@+package provider++import (+ "context"+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync"+ "testing"+)++// Axel Burks on Discord: a gateway in front of several Azure accounts+// routes a conversation by a session id in a header. ${sessionId} and+// ${threadId} in a provider's Headers are the conversation's, as in its+// extraBody; Test and Detect send their own test session, the same one in+// the header as in the body; a model list sends an id of its own. A+// literal placeholder never goes out.+func TestHeadersTakeTheConversation(t *testing.T) {+ detectHome(t)+ const model = "gpt-test"+ var mu sync.Mutex+ type seen struct{ path, session, route, plain, key string }+ var got []seen+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ var in struct {+ Key string `json:"prompt_cache_key"`+ }+ json.NewDecoder(r.Body).Decode(&in)+ mu.Lock()+ got = append(got, seen{r.Method + " " + r.URL.Path, r.Header.Get("X-Session-Id"), r.Header.Get("X-Route"), r.Header.Get("X-Plain"), in.Key})+ mu.Unlock()+ switch r.Method + " " + r.URL.Path {+ case "GET /v1/models":+ io.WriteString(w, `{"data":[{"id":"`+model+`"}]}`)+ case "POST /v1/responses":+ io.WriteString(w, `{"object":"response","status":"completed","output":[]}`)+ default:+ http.NotFound(w, r)+ }+ }))+ defer srv.Close()+ p := Provider{ID: "azgw", Key: "sk-gw", Responses: srv.URL + "/v1", Models: []string{model},+ Headers: map[string]string{"X-Session-Id": "${sessionId}", "X-Route": "s=${sessionId};t=${threadId}", "X-Plain": "fixed"},+ ExtraBody: ExtraBody{"prompt_cache_key": json.RawMessage(`"${sessionId}"`)}}+ ctx := context.Background()+ if rs := p.Test(ctx); len(rs) != 1 || !rs[0].OK {+ t.Fatalf("Test: %+v", rs)+ }+ if rs := p.TestModels(ctx, []string{model}); len(rs) != 1 || !rs[0].OK {+ t.Fatalf("TestModels: %+v", rs)+ }+ if rs, err := p.Detect(ctx, srv.URL, model); err != nil || len(rs) != len(detectProtocols) || !rs[1].Result.OK {+ t.Fatalf("Detect: %v, %+v", err, rs)+ }+ if _, err := p.Fetch(ctx); err != nil {+ t.Fatalf("Fetch: %v", err)+ }+ mu.Lock()+ defer mu.Unlock()+ asked, listed := 0, 0+ tests := map[string]bool{}+ for _, s := range got {+ if strings.Contains(s.session+s.route, "${") || s.plain != "fixed" {+ t.Errorf("%s sent %+v", s.path, s)+ }+ switch s.path {+ case "POST /v1/responses":+ asked+++ if !strings.HasPrefix(s.session, "magpie-test-") || s.session != s.key || s.route != "s="+s.key+";t="+s.key {+ t.Errorf("a test's header isn't its body's session: %+v", s)+ }+ tests[s.session] = true+ case "GET /v1/models":+ listed+++ if !strings.HasPrefix(s.session, "magpie-") || strings.HasPrefix(s.session, "magpie-test-") || s.route != "s="+s.session+";t="+s.session {+ t.Errorf("a list's header: %+v", s)+ }+ }+ }+ if asked != 3 || len(tests) != 3 || listed == 0 {+ t.Errorf("asked %d on %d sessions, listed %d: %+v", asked, len(tests), listed, got)+ }+}++// An id is the agent's: in a header it goes without CR, LF or any other+// control character, which would split the header or fail the request.+// ${threadId} is the session's where no thread is named; a request that+// is no turn of a conversation gets an id of its own; headers without a+// placeholder go as written.+func TestSentHeaders(t *testing.T) {+ p := Provider{Headers: map[string]string{"X-S": "${sessionId}", "X-T": "${threadId}", "X-P": "a${b}"}}+ ctx := WithConversation(context.Background(), Conversation{Session: "s-1\r\nX-Evil: 1", Thread: "t\t\x00\x7f-2"})+ h := p.SentHeaders(ctx)+ if h["X-S"] != "s-1X-Evil: 1" || h["X-T"] != "t-2" || h["X-P"] != "a${b}" {+ t.Errorf("sent %q", h)+ }+ if p.Headers["X-S"] != "${sessionId}" {+ t.Errorf("the saved headers changed: %q", p.Headers)+ }+ h = p.SentHeaders(WithConversation(context.Background(), Conversation{Session: "s-2"}))+ if h["X-S"] != "s-2" || h["X-T"] != "s-2" {+ t.Errorf("no thread: %q", h)+ }+ a, b := p.SentHeaders(context.Background()), p.SentHeaders(context.Background())+ if !strings.HasPrefix(a["X-S"], "magpie-") || len(a["X-S"]) != len("magpie-")+12 || a["X-T"] != a["X-S"] || a["X-S"] == b["X-S"] {+ t.Errorf("own ids %q %q", a, b)+ }+ plain := Provider{Headers: map[string]string{"X-P": "v"}}+ if h := plain.SentHeaders(ctx); h["X-P"] != "v" || len(h) != 1 {+ t.Errorf("plain %q", h)+ }++ // signed and sent: the request goes, its header clean+ var heard http.Header+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { heard = r.Header }))+ defer srv.Close()+ q := Provider{ID: "x", Key: "k", Chat: srv.URL + "/v1", Headers: p.Headers}+ req, _ := http.NewRequestWithContext(ctx, "POST", srv.URL+"/v1/chat/completions", strings.NewReader("{}"))+ if err := q.Sign(ctx, req, Chat, nil); err != nil {+ t.Fatal(err)+ }+ res, err := http.DefaultClient.Do(req)+ if err != nil {+ t.Fatalf("sent: %v", err)+ }+ res.Body.Close()+ if heard.Get("X-S") != "s-1X-Evil: 1" || heard.Get("X-Evil") != "" {+ t.Errorf("heard %q", heard)+ }+}++// ${threadId} in extraBody is the thread's, else the session's.+func TestExtraBodyThread(t *testing.T) {+ p := Provider{ExtraBody: ExtraBody{"metadata": json.RawMessage(`{"s":"${sessionId}","t":"${threadId}"}`)}}+ out := string(p.WithExtraBody([]byte(`{"model":"m"}`), Conversation{Session: "root", Thread: "worker"}))+ if !strings.Contains(out, `"s":"root"`) || !strings.Contains(out, `"t":"worker"`) {+ t.Errorf("%s", out)+ }+ out = string(p.WithExtraBody([]byte(`{"model":"m"}`), Conversation{Session: "root"}))+ if !strings.Contains(out, `"t":"root"`) {+ t.Errorf("%s", out)+ }+ if p.AsksThread() == false || (Provider{Headers: map[string]string{"a": "${sessionId}"}}).AsksThread() {+ t.Error("AsksThread")+ }+}diff --git a/internal/provider/detect.go b/internal/provider/detect.goindex d9be4a030..4e141bada 100644--- a/internal/provider/detect.go+++ b/internal/provider/detect.go@@ -172,7 +172,8 @@ func (q Provider) detectOne(proto Protocol, m string, wait time.Duration) (Detec}return d, func(ctx context.Context) Result {u, body := tiny(k, proto, UpstreamName(q, m))- return probe(ctx, k, proto, u, k.tested(body), m, wait)+ ctx, b := k.tested(ctx, body)+ return probe(ctx, k, proto, u, b, m, wait)}}diff --git a/internal/provider/extrabody.go b/internal/provider/extrabody.goindex ac41082a3..3e34dca8d 100644--- a/internal/provider/extrabody.go+++ b/internal/provider/extrabody.go@@ -15,7 +15,8 @@ package provider// id: the session the agent names (its session header), else one// magpie derives from the conversation's first user message — the same// for every turn of it. A fixed string would put every session on one-// cache key.+// cache key. ${threadId} is the thread's (conversation.go); Headers+// take both too.// - The fields that carry the conversation, or that magpie must decide// for routing and streaming to work (ExtraBodyReserved), can't be set:// a save naming one is refused, and one written into the file by hand@@ -36,7 +37,7 @@ package providerimport ("bytes"- "crypto/rand"+ "context""encoding/json""fmt""maps"@@ -139,9 +140,9 @@ func cleanExtraBody(e ExtraBody) ExtraBody {}// WithExtraBody is body with the provider's extra fields merged in, as the-// package's comment says; session is the conversation's id for-// ${sessionId}. A body that isn't a JSON object goes as it came.-func (p Provider) WithExtraBody(body []byte, session string) []byte {+// package's comment says; c is the conversation for ${sessionId} and+// ${threadId}. A body that isn't a JSON object goes as it came.+func (p Provider) WithExtraBody(body []byte, c Conversation) []byte {owned, takes := p.extraBodyTakes()if len(p.ExtraBody) == 0 || !takes {return body@@ -167,7 +168,7 @@ func (p Provider) WithExtraBody(body []byte, session string) []byte {if d.Decode(&v) != nil {continue}- m[n], changed = withSession(v, session), true+ m[n], changed = withSession(v, c), true}if !changed {return body@@ -181,19 +182,19 @@ func (p Provider) WithExtraBody(body []byte, session string) []byte {return bytes.TrimSuffix(out.Bytes(), []byte("\n"))}-// withSession is v with ${sessionId} in its strings, at any depth, as-// session.-func withSession(v any, session string) any {+// withSession is v with ${sessionId} and ${threadId} in its strings, at+// any depth, as c's ids.+func withSession(v any, c Conversation) any {switch x := v.(type) {case string:- return strings.ReplaceAll(x, SessionPlaceholder, session)+ return c.expand(x)case map[string]any:for k, e := range x {- x[k] = withSession(e, session)+ x[k] = withSession(e, c)}case []any:for i, e := range x {- x[i] = withSession(e, session)+ x[i] = withSession(e, c)}}return v@@ -201,7 +202,9 @@ func withSession(v any, session string) any {// tested is a test's or a detection's tiny request body for p as it goes// out: the extra fields, on a session of the test's own, then what the-// backend adjusts.-func (p Provider) tested(body string) []byte {- return p.Prepare(p.WithExtraBody([]byte(body), "magpie-test-"+strings.ToLower(rand.Text()[:12])))+// backend adjusts; ctx carries the same session, for the headers Sign+// puts on it.+func (p Provider) tested(ctx context.Context, body string) (context.Context, []byte) {+ c := OwnConversation("magpie-test-")+ return WithConversation(ctx, c), p.Prepare(p.WithExtraBody([]byte(body), c))}diff --git a/internal/provider/extrabody_test.go b/internal/provider/extrabody_test.goindex 040d5c82c..31af2e197 100644--- a/internal/provider/extrabody_test.go+++ b/internal/provider/extrabody_test.go@@ -109,7 +109,7 @@ func TestWithExtraBody(t *testing.T) {"model": json.RawMessage(`"never"`),"big": json.RawMessage(`12345678901234567890`),}}- out := p.WithExtraBody([]byte(`{"model":"m","service_tier":"auto","input":"a<b>&c","prompt_cache_key":"mine"}`), "S1")+ out := p.WithExtraBody([]byte(`{"model":"m","service_tier":"auto","input":"a<b>&c","prompt_cache_key":"mine"}`), Conversation{Session: "S1"})var m map[string]anyif err := json.Unmarshal(out, &m); err != nil {t.Fatalf("%s: %v", out, err)@@ -124,25 +124,25 @@ func TestWithExtraBody(t *testing.T) {t.Errorf("escaped %s", out)}- out = p.WithExtraBody([]byte(`{"model":"m"}`), "S2")+ out = p.WithExtraBody([]byte(`{"model":"m"}`), Conversation{Session: "S2"})if !strings.Contains(string(out), `"prompt_cache_key":"S2"`) {t.Errorf("filled %s", out)}// a body that isn't an object goes as it camefor _, b := range []string{`[1]`, `not json`, ``} {- if got := string(p.WithExtraBody([]byte(b), "S")); got != b {+ if got := string(p.WithExtraBody([]byte(b), Conversation{Session: "S"})); got != b {t.Errorf("%q became %q", b, got)}}// a signed-in account's body is the agent's ownacct := pacct.Account = &Account{}- if got := string(acct.WithExtraBody([]byte(`{"model":"m"}`), "S")); got != `{"model":"m"}` {+ if got := string(acct.WithExtraBody([]byte(`{"model":"m"}`), Conversation{Session: "S"})); got != `{"model":"m"}` {t.Errorf("account body %s", got)}// none set: the body's bytes as they were- if got := string((Provider{}).WithExtraBody([]byte(`{"b":1, "a":2}`), "S")); got != `{"b":1, "a":2}` {+ if got := string((Provider{}).WithExtraBody([]byte(`{"b":1, "a":2}`), Conversation{Session: "S"})); got != `{"b":1, "a":2}` {t.Errorf("unchanged body %s", got)}}diff --git a/internal/provider/kilo.go b/internal/provider/kilo.goindex d9b3e2918..4f43eb79d 100644--- a/internal/provider/kilo.go+++ b/internal/provider/kilo.go@@ -106,7 +106,7 @@ func (p Provider) kiloModelsPage(ctx context.Context, at string) ([]catalog.Modeif p.Key != "" {req.Header.Set("Authorization", "Bearer "+p.Key)}- for k, v := range p.Headers {+ for k, v := range p.SentHeaders(ctx) {catalog.PutUserHeader(req.Header, k, v)}res, err := http.DefaultClient.Do(req)diff --git a/internal/provider/provider.go b/internal/provider/provider.goindex a8e1f62fe..13a615a77 100644--- a/internal/provider/provider.go+++ b/internal/provider/provider.go@@ -213,8 +213,9 @@ type Provider struct {// price as listed; at most three decimals (PriceRateOK).PriceRate float64 `json:"priceRate,omitempty"`- // Headers are extra HTTP request headers sent to the vendor, exactly as- // the user typed them. They ride on every request magpie makes to a plain+ // Headers are extra HTTP request headers sent to the vendor, as the user+ // typed them but for ${sessionId} and ${threadId} in a value, the+ // request's conversation ids (SentHeaders, conversation.go). They ride on every request magpie makes to a plain// key+URL provider — forwarded calls, connectivity tests, and model-list// fetches — applied after the auth headers, so the user can override those// when a gateway insists on a private scheme. Signed-in agent accounts@@ -223,8 +224,8 @@ type Provider struct {// ExtraBody are JSON fields added to the body of every request to the// vendor (#1604), filling in what the body lacks, or replacing it for a- // name written "!name"; ${sessionId} in a value is the conversation's- // id (extrabody.go). Signed-in agent accounts ignore it, as Headers.+ // name written "!name"; ${sessionId} and ${threadId} in a value are the+ // conversation's ids (extrabody.go, conversation.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 thediff --git a/internal/provider/remote_magpie.go b/internal/provider/remote_magpie.goindex b437d03b9..6f08794cf 100644--- a/internal/provider/remote_magpie.go+++ b/internal/provider/remote_magpie.go@@ -1,6 +1,7 @@package providerimport (+ "context""maps""strings"@@ -108,11 +109,14 @@ func (p *Provider) remoteMagpieEndpoints() {// listHeaders are the headers p's model list is asked with: a remote// magpie is asked for its image, video and decision models too, and its prices.+// A list is no turn of a conversation: ${sessionId} and ${threadId} in a+// value are an id of the list's own (conversation.go).func (p Provider) listHeaders() map[string]string {+ h := p.SentHeaders(context.Background())if !p.IsRemoteMagpie() {- return p.Headers+ return h}- h := maps.Clone(p.Headers)+ h = maps.Clone(h)if h == nil {h = map[string]string{}}diff --git a/internal/provider/test.go b/internal/provider/test.goindex c4c899800..94c3c5705 100644--- a/internal/provider/test.go+++ b/internal/provider/test.go@@ -60,7 +60,8 @@ func (p Provider) Test(ctx context.Context) []Result {return []Result{{Protocol: proto, Error: zenPublicNone}}}url, body := tiny(q, proto, UpstreamName(p, model))- return []Result{probe(ctx, q, proto, url, q.tested(asClient(client, proto, body)), model, testWait)}+ ctx, b := q.tested(ctx, asClient(client, proto, body))+ return []Result{probe(ctx, q, proto, url, b, model, testWait)}}if p.isClaudeAccount() {return []Result{p.testClaude(ctx, p.testModel(p, Anthropic))}@@ -88,7 +89,8 @@ func (p Provider) Test(ctx context.Context) []Result {continue}url, body := tiny(q, proto, UpstreamName(p, model))- out = append(out, probe(ctx, q, proto, url, q.tested(body), model, testWait))+ ctx, b := q.tested(ctx, body)+ out = append(out, probe(ctx, q, proto, url, b, model, testWait))}if len(out) == 0 && p.zenPublic() {out = append(out, Result{Protocol: Chat, Error: zenPublicNone})@@ -347,7 +349,8 @@ func (p Provider) testOne(ctx context.Context, model string) Result {r := probe(ctx, q, proto, url, []byte(body), model, drawWait)if !r.OK && ((r.Status == 404 || r.Status == 405) && HostOf(q.Chat) != "openrouter.ai" || r.Status == 400 && catalog.PointsToChat(r.Error)) {url, body := tiny(q, proto, model)- if c := probe(ctx, q, proto, url, q.tested(body), model, testWait); c.OK {+ ctx, b := q.tested(ctx, body)+ if c := probe(ctx, q, proto, url, b, model, testWait); c.OK {return c}}@@ -416,7 +419,8 @@ func (p Provider) testOn(ctx context.Context, client string, proto Protocol, modif client != "" {body = asClient(client, proto, body)}- return probe(ctx, p, proto, url, p.tested(body), model, testWait)+ ctx, b := p.tested(ctx, body)+ return probe(ctx, p, proto, url, b, model, testWait)}// keyFor is p using the first key on that works with proto: one made for