Files
magpie/internal/provider/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

149 lines
5.0 KiB
Go

package provider
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// The editor's Test and Detect ask with the extra fields too, so a relay
// that turns a request away without a prompt_cache_key (#1604) answers
// them, on a session id of the test's own.
func TestExtraBodyTested(t *testing.T) {
detectHome(t)
const model = "gpt-test"
var keys []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method + " " + r.URL.Path {
case "GET /v1/models":
io.WriteString(w, `{"data":[{"id":"`+model+`"}]}`)
case "POST /v1/responses":
var in struct {
Key string `json:"prompt_cache_key"`
}
if json.NewDecoder(r.Body).Decode(&in) != nil || in.Key == "" {
http.Error(w, `{"error":{"message":"prompt_cache_key is required"}}`, http.StatusBadRequest)
return
}
keys = append(keys, in.Key)
io.WriteString(w, `{"object":"response","status":"completed","output":[]}`)
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
p := Provider{ID: "relay", Key: "sk-relay", Responses: srv.URL + "/v1", Models: []string{model},
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)
}
rs, err := p.Detect(ctx, srv.URL, model)
if err != nil || len(rs) != len(detectProtocols) || !rs[1].Result.OK {
t.Fatalf("Detect: %v, %+v", err, rs)
}
if len(keys) != 3 || !strings.HasPrefix(keys[0], "magpie-test-") || keys[0] == keys[1] {
t.Errorf("asked with %q", keys)
}
}
func TestExtraBodyOK(t *testing.T) {
for _, c := range []struct {
in, want string
}{
{`{}`, ""},
{`{"prompt_cache_key":"${sessionId}","!service_tier":"flex","metadata":{"a":[1,2]}}`, ""},
{`{"model":"x"}`, `can't set "model"`},
{`{"!stream":true}`, `can't set "stream"`},
{`{"messages":[]}`, `can't set "messages"`},
{`{"input":"x"}`, `can't set "input"`},
{`{"contents":[]}`, `can't set "contents"`},
{`{"!":1}`, "needs a name"},
{`{"user":"a","!user":"b"}`, `names "user" twice`},
} {
var e ExtraBody
if err := json.Unmarshal([]byte(c.in), &e); err != nil {
t.Fatal(err)
}
got := ExtraBodyOK(e)
if (c.want == "") != (got == "") || !strings.Contains(got, c.want) {
t.Errorf("%s: %q, want %q", c.in, got, c.want)
}
}
if got := ExtraBodyOK(ExtraBody{"a": json.RawMessage(`{`)}); !strings.Contains(got, "not JSON") {
t.Errorf("bad value: %q", got)
}
}
// A providers file written by hand with something other than an object, or
// with fields extraBody can't set, still reads: the rest of it is kept.
func TestExtraBodyReadByHand(t *testing.T) {
var ps []Provider
if err := json.Unmarshal([]byte(`[{"id":"a","extraBody":"oops"},{"id":"b","extraBody":{"model":"x","messages":[],"user":"u"," !tier ":"t"}}]`), &ps); err != nil {
t.Fatal(err)
}
if ps[0].ExtraBody != nil {
t.Errorf("a string read as %v", ps[0].ExtraBody)
}
got := cleanExtraBody(ps[1].ExtraBody)
if len(got) != 2 || string(got["user"]) != `"u"` || string(got["!tier"]) != `"t"` {
t.Errorf("cleaned to %v", got)
}
if cleanExtraBody(ExtraBody{"model": json.RawMessage(`1`)}) != nil {
t.Error("nothing left isn't nil")
}
}
func TestWithExtraBody(t *testing.T) {
p := Provider{ExtraBody: ExtraBody{
"prompt_cache_key": json.RawMessage(`"${sessionId}"`),
"metadata": json.RawMessage(`{"tags":["s-${sessionId}"],"n":1}`),
"!service_tier": json.RawMessage(`"flex"`),
"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")
var m map[string]any
if err := json.Unmarshal(out, &m); err != nil {
t.Fatalf("%s: %v", out, err)
}
if m["model"] != "m" || m["prompt_cache_key"] != "mine" || m["service_tier"] != "flex" || m["input"] != "a<b>&c" {
t.Errorf("merged %s", out)
}
if !strings.Contains(string(out), `"tags":["s-S1"]`) || !strings.Contains(string(out), `"big":12345678901234567890`) {
t.Errorf("values %s", out)
}
if !strings.Contains(string(out), `"a<b>&c"`) {
t.Errorf("escaped %s", out)
}
out = p.WithExtraBody([]byte(`{"model":"m"}`), "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 came
for _, b := range []string{`[1]`, `not json`, ``} {
if got := string(p.WithExtraBody([]byte(b), "S")); got != b {
t.Errorf("%q became %q", b, got)
}
}
// a signed-in account's body is the agent's own
acct := p
acct.Account = &Account{}
if got := string(acct.WithExtraBody([]byte(`{"model":"m"}`), "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}` {
t.Errorf("unchanged body %s", got)
}
}