Files
magpie/internal/gateway/extrabody_test.go
T
yetone 863af7abc8 provider: a provider's Extra body adds JSON fields to every request's body, ${sessionId} as the session (#1604, Luc-xz)
What: a provider has an Extra body (extraBody in providers.json, a JSON
object, beside Headers; an "Extra body" box in the editor under Headers).
Its fields are added at the top level of every JSON body sent to that
provider, in the gateway's forwardOnce: Messages, Chat, Responses and
Gemini, relayed as they came or translated from another API. Test models
and Detect send them too (provider.tested), so a relay that
refuses a request without them can be tested before a Save.

- A field the request already has is kept; "!name" replaces it.
- ${sessionId}, in any string at any depth, becomes the session id: the
  agent's session header (sessionOf), else the id conversationID derives
  from the first user message (magpie-<hash>), stable through one
  conversation. Test and Detect send magpie-test-<12 random letters>.
- model, stream and the conversation (messages, input, contents, system,
  systemInstruction, system_instruction, instructions, tools) are never
  set from it: Save refuses them (ExtraBodyOK), the editor says why as
  they are typed and holds the Save, and one written by hand into
  providers.json is dropped on read (cleanExtraBody).
- Not sent: count_tokens, embeddings, images, video, the decision model,
  and signed-in accounts' requests. One object goes to every API;
  per-API sections aren't done.
- A save that leaves extraBody out keeps the saved one; {} clears it.

Why: Luc-xz's relay turns Responses requests away without a
prompt_cache_key, and headers alone couldn't add one.

Docs: providers-accounts.md has an Extra body row and its tests.

Run:
- go test -tags nogui ./internal/provider ./internal/gateway ./internal/gui
  -run ExtraBody: pass. The tests are TestExtraBodyResponsesRelayed,
  ChatDerivedSession, Translated (messages→chat, chat→responses),
  Anthropic (count_tokens left alone), Gemini (relayed and from chat),
  TestExtraBodyTested/OK/ReadByHand, TestWithExtraBody and
  TestProviderExtraBodySaved.
- Broken by hand, each FAIL recorded, then restored:
  - WithExtraBody removed from forwardOnce: all 5 gateway tests FAIL.
  - the countsTokens guard dropped: TestExtraBodyAnthropic FAIL.
  - tested() without the extra body: TestExtraBodyTested FAIL.
  - Save's ExtraBodyOK skipped: TestProviderExtraBodySaved FAIL.
  - keep-when-omitted skipped: TestProviderExtraBodySaved FAIL.
- internal/gui/tests/extra-body.test.cjs: en, zh, ja and de, in Chromium
  and WebKit, 8/8 pass. It covers live errors, Save held and focused, Test
  sending the typed object, the 420px fit, Save as an object, and empty
  saving {}.
- Also run: gui-de, gui-ja, gui-zh-tw, i18n-keys, provider-*, providers-*,
  balance-fix, duplicate-pause, editor-short-window, ux-safety and
  vertex-editor.
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows on
  provider, gateway and gui; the full go test ./... after the rebase.

Not tried: a real relay that needs prompt_cache_key; the fixtures are
httptest upstreams.
2026-10-11 22:41:11 +08:00

208 lines
8.6 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// withExtraBody gives the saved provider id the extraBody written.
func withExtraBody(t *testing.T, id, extra string) {
t.Helper()
p, err := provider.Find(id)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(extra), &p.ExtraBody); err != nil {
t.Fatal(err)
}
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
}
// postWith is post with the client's own headers.
func postWith(t *testing.T, path, body string, head map[string]string) (int, string) {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
for k, v := range head {
req.Header.Set(k, v)
}
New().Handler().ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}
const (
chatReply = "data: {\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"OK\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n"
responsesReply = `{"id":"r","object":"response","status":"completed","output":[]}`
)
func anthropicReply() string {
return sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"m1","usage":{"input_tokens":7}}}`,
`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"OK"}}`,
`data: {"type":"content_block_stop","index":0}`,
`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
`data: {"type":"message_stop"}`)
}
// Luc-xz's relay (#1604) turns a Responses request away without a
// prompt_cache_key: the provider's extraBody adds one, the session's id,
// filling in only what the client didn't send, unless a name is forced.
func TestExtraBodyResponsesRelayed(t *testing.T) {
f := &fake{t: t, reply: responsesReply, ctype: "application/json"}
setup(t, provider.Responses, f)
withExtraBody(t, "fake", `{"prompt_cache_key":"${sessionId}","metadata":{"who":"agent-${sessionId}"},"!service_tier":"flex","model":"other"}`)
code, body := postWith(t, "/v1/responses", `{"model":"m1","input":"hi","service_tier":"auto"}`, map[string]string{"session_id": "sess-42"})
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if got := gjson.GetBytes(f.got, "prompt_cache_key").String(); got != "sess-42" {
t.Errorf("prompt_cache_key %q in %s", got, f.got)
}
if got := gjson.GetBytes(f.got, "metadata.who").String(); got != "agent-sess-42" {
t.Errorf("metadata.who %q in %s", got, f.got)
}
if got := gjson.GetBytes(f.got, "service_tier").String(); got != "flex" {
t.Errorf("forced service_tier %q in %s", got, f.got)
}
if got := gjson.GetBytes(f.got, "model").String(); got != "m1" {
t.Errorf("model %q in %s", got, f.got)
}
if gjson.GetBytes(f.got, "input").String() != "hi" {
t.Errorf("input changed: %s", f.got)
}
// the client's own key is kept
code, body = postWith(t, "/v1/responses", `{"model":"m1","input":"hi","prompt_cache_key":"mine"}`, map[string]string{"session_id": "sess-42"})
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if got := gjson.GetBytes(f.got, "prompt_cache_key").String(); got != "mine" {
t.Errorf("client's prompt_cache_key replaced by %q", got)
}
}
// Without a session header, ${sessionId} is the id magpie derives from the
// conversation's first user message: never the literal, the same on every
// turn of one conversation, different for another.
func TestExtraBodyChatDerivedSession(t *testing.T) {
f := &fake{t: t, reply: chatReply}
setup(t, provider.Chat, f)
withExtraBody(t, "fake", `{"user":"${sessionId}","temperature":0.5}`)
ask := func(msgs string) string {
t.Helper()
code, body := post(t, "/v1/chat/completions", `{"model":"m1","messages":`+msgs+`}`)
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if gjson.GetBytes(f.got, "temperature").Raw != "0.5" {
t.Errorf("temperature in %s", f.got)
}
return gjson.GetBytes(f.got, "user").String()
}
one := ask(`[{"role":"user","content":"first"}]`)
again := ask(`[{"role":"user","content":"first"},{"role":"assistant","content":"OK"},{"role":"user","content":"next"}]`)
other := ask(`[{"role":"user","content":"another"}]`)
if !strings.HasPrefix(one, "magpie-") || one != again || one == other {
t.Errorf("derived ids: %q %q %q", one, again, other)
}
}
// A request magpie translates gets the fields too, on top of the
// translated body: Messages to a Chat provider, Chat to a Responses one.
func TestExtraBodyTranslated(t *testing.T) {
t.Run("messages to chat", func(t *testing.T) {
f := &fake{t: t, reply: chatReply}
setup(t, provider.Chat, f)
withExtraBody(t, "fake", `{"prompt_cache_key":"${sessionId}"}`)
code, body := postWith(t, "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, map[string]string{"X-Claude-Code-Session-Id": "cc-1"})
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if f.path != "/v1/chat/completions" || gjson.GetBytes(f.got, "prompt_cache_key").String() != "cc-1" {
t.Errorf("%s: %s", f.path, f.got)
}
})
t.Run("chat to responses", func(t *testing.T) {
f := &fake{t: t, reply: sse(`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)}
setup(t, provider.Responses, f)
withExtraBody(t, "fake", `{"prompt_cache_key":"${sessionId}"}`)
code, body := postWith(t, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, map[string]string{"x-session-id": "s-9"})
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if f.path != "/v1/responses" || gjson.GetBytes(f.got, "prompt_cache_key").String() != "s-9" {
t.Errorf("%s: %s", f.path, f.got)
}
})
}
// An Anthropic provider gets them on Messages, and not on count_tokens,
// which asks for no reply.
func TestExtraBodyAnthropic(t *testing.T) {
f := &fake{t: t, reply: anthropicReply()}
setup(t, provider.Anthropic, f)
withExtraBody(t, "fake", `{"metadata":{"user_id":"${sessionId}"},"!max_tokens":99}`)
code, body := postWith(t, "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, map[string]string{"session_id": "a-1"})
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
if gjson.GetBytes(f.got, "metadata.user_id").String() != "a-1" || gjson.GetBytes(f.got, "max_tokens").Int() != 99 {
t.Errorf("messages body %s", f.got)
}
f.reply, f.ctype = `{"input_tokens":3}`, "application/json"
code, body = postWith(t, "/v1/messages/count_tokens", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, map[string]string{"session_id": "a-1"})
if code != 200 {
t.Fatalf("count_tokens %d: %s", code, body)
}
if f.path != "/v1/messages/count_tokens" || gjson.GetBytes(f.got, "metadata").Exists() {
t.Errorf("count_tokens %s: %s", f.path, f.got)
}
}
// A Gemini provider gets them on a Gemini client's relayed request and on
// one translated from Chat.
func TestExtraBodyGemini(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var got []byte
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, _ = io.ReadAll(r.Body)
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"OK"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}`))
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "gem", Name: "Gem", Key: "AIza-test", Gemini: up.URL + "/v1beta", Models: []string{"gemini-2.5-pro"}}); err != nil {
t.Fatal(err)
}
withExtraBody(t, "gem", `{"cachedContent":"cachedContents/${sessionId}","contents":[]}`)
code, body := postWith(t, "/v1beta/models/gem/gemini-2.5-pro:generateContent", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`, map[string]string{"session_id": "g-1"})
if code != 200 {
t.Fatalf("gemini %d: %s", code, body)
}
if gjson.GetBytes(got, "cachedContent").String() != "cachedContents/g-1" || !strings.Contains(gjson.GetBytes(got, "contents").Raw, "hi") {
t.Errorf("gemini body %s", got)
}
code, body = postWith(t, "/v1/chat/completions", `{"model":"gem/gemini-2.5-pro","messages":[{"role":"user","content":"hi"}]}`, map[string]string{"session_id": "g-2"})
if code != 200 {
t.Fatalf("chat %d: %s", code, body)
}
if gjson.GetBytes(got, "cachedContent").String() != "cachedContents/g-2" {
t.Errorf("chat to gemini body %s", got)
}
}