diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index 6b39fd2c4..6df9b0672 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -597,6 +597,9 @@ type drawing struct { N int Images []picture // to edit, or draw from Mask *picture + // Markup is OpenAI's markup layer: a transparent PNG laid over the + // first image to steer the edit, never beside a Mask (#5364) + Markup *picture // Extra are the request's fields magpie doesn't read, which a // vendor's own images API that takes them is sent as they came // (Ark's sequential_image_generation, optimize_prompt_options, @@ -607,7 +610,7 @@ type drawing struct { } // drawingRead are the fields of an images request magpie reads itself. -var drawingRead = []string{"model", "prompt", "size", "quality", "background", "output_format", "response_format", "n", "image", "image[]", "images", "mask", "user"} +var drawingRead = []string{"model", "prompt", "size", "quality", "background", "output_format", "response_format", "n", "image", "image[]", "images", "mask", "markup", "user"} // extraOf keeps v as one of d's Extra under k, unless magpie reads k. func (d *drawing) extraOf(k string, v any) { @@ -949,6 +952,13 @@ func readDrawing(r *http.Request) (drawing, error) { } d.Mask = &pic } + if fhs := f.File["markup"]; len(fhs) > 0 { + pic, err := readPart(fhs[0]) + if err != nil { + return d, err + } + d.Markup = &pic + } } else { body, err := io.ReadAll(r.Body) if err != nil { @@ -965,6 +975,7 @@ func readDrawing(r *http.Request) (drawing, error) { Image json.RawMessage `json:"image"` Images json.RawMessage `json:"images"` Mask json.RawMessage `json:"mask"` + Markup json.RawMessage `json:"markup"` ResponseFormat string `json:"response_format"` } if err := json.Unmarshal(body, &in); err != nil { @@ -992,6 +1003,16 @@ func readDrawing(r *http.Request) (drawing, error) { } d.Mask = &pic } + if srcs := imageSources(in.Markup); len(srcs) > 0 { + pic, err := pictureOf(srcs[0]) + if err != nil { + return d, err + } + d.Markup = &pic + } + } + if d.Mask != nil && d.Markup != nil { + return d, errors.New("give a mask or a markup layer, not both: an edit takes one") } d.Model = strings.TrimSpace(d.Model) // Ark's layer_decomposition splits the image it is given into layers, @@ -1575,6 +1596,9 @@ func (s *Server) drawImages(ctx context.Context, p provider.Provider, model stri if d.Mask != nil { req["mask"] = map[string]string{"image_url": d.Mask.dataURL()} } + if d.Markup != nil { + req["markup"] = map[string]string{"image_url": d.Markup.dataURL()} + } body, _ = json.Marshal(req) ct, url = "application/json", base+"/images/edits" } else { @@ -1600,6 +1624,9 @@ func (s *Server) drawImages(ctx context.Context, p provider.Provider, model stri if d.Mask != nil { pics, fields = append(pics, *d.Mask), append(fields, "mask") } + if d.Markup != nil { + pics, fields = append(pics, *d.Markup), append(fields, "markup") + } for i, pic := range pics { if pic.URL != "" { b, mt, err := s.fetchPicture(ctx, pic.URL) @@ -1639,6 +1666,9 @@ func (s *Server) drawOpenRouterImages(ctx context.Context, p provider.Provider, if d.Mask != nil { return drawn{}, http.StatusBadRequest, errors.New("OpenRouter's images API does not support mask edits") } + if d.Markup != nil { + return drawn{}, http.StatusBadRequest, errors.New("OpenRouter's images API does not support a markup layer") + } req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N} for k, v := range map[string]string{"size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} { if v != "" { diff --git a/internal/gateway/draw_dashscope.go b/internal/gateway/draw_dashscope.go index 9b4cbf8d8..8a6ae50a4 100644 --- a/internal/gateway/draw_dashscope.go +++ b/internal/gateway/draw_dashscope.go @@ -71,6 +71,9 @@ func dashScopeBody(model string, d drawing) ([]byte, error) { if d.Mask != nil { return nil, errors.New("DashScope's image models take no mask: say in the prompt which part of the image to change") } + if d.Markup != nil { + return nil, errors.New("DashScope's image models take no markup layer: say in the prompt what to change") + } size, err := dashScopeSize(d.Size) if err != nil { return nil, err diff --git a/internal/gateway/draw_markup_test.go b/internal/gateway/draw_markup_test.go new file mode 100644 index 000000000..8b73af56d --- /dev/null +++ b/internal/gateway/draw_markup_test.go @@ -0,0 +1,199 @@ +package gateway + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "mime/multipart" + "net/http/httptest" + "net/textproto" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// markupPNG stands for the transparent layer: other bytes than pngBytes, +// so a test can tell which picture went where. +var markupPNG = []byte("\x89PNG\r\n\x1a\n markup layer") + +func markupURL() string { + return "data:image/png;base64," + base64.StdEncoding.EncodeToString(markupPNG) +} + +func sourceURL() string { + return "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes) +} + +// #5364 (kongjianguan): /v1/images/edits takes OpenAI's markup layer, a +// transparent PNG laid over the first image to steer the edit. magpie +// read only the mask: a JSON markup stopped in drawing.Extra, a +// multipart one was dropped, and neither the ChatGPT backend's JSON edit +// nor the plain multipart edit carried it. +func TestCodexEditSendsTheMarkup(t *testing.T) { + fresh(t) + e := &easel{} + up := httptest.NewServer(e) + t.Cleanup(up.Close) + p := provider.Provider{ID: "art", Name: "Art", Responses: up.URL + "/v1", Key: "key", + Account: &provider.Account{Agent: "codex", User: "u@example.com"}} + if !drawsCodex(p) { + t.Fatal("the provider is not a ChatGPT account") + } + s := New() + r := httptest.NewRequest("POST", "/v1/images/edits", strings.NewReader( + `{"model":"art/gpt-image-1","prompt":"apply the annotation","images":[{"image_url":"`+sourceURL()+`"}],"markup":{"image_url":"`+markupURL()+`"}}`)) + r.Header.Set("Content-Type", "application/json") + d, err := readDrawing(r) + if err != nil { + t.Fatal(err) + } + if _, ok := d.Extra["markup"]; ok { + t.Error("the markup read as a layer is kept as an extra field too") + } + if _, code, err := s.drawImages(context.Background(), p, "gpt-image-1", d); code != 200 || err != nil { + t.Fatalf("%d %v", code, err) + } + sent := e.got("/v1/images/edits") + if len(sent) != 1 { + t.Fatalf("vendor was sent %v", sent) + } + var req struct { + Markup struct { + ImageURL string `json:"image_url"` + } `json:"markup"` + Mask json.RawMessage `json:"mask"` + } + if err := json.Unmarshal([]byte(sent[0]), &req); err != nil { + t.Fatalf("vendor got non-JSON: %v (%s)", err, sent[0]) + } + if req.Markup.ImageURL != markupURL() { + t.Errorf("the ChatGPT edit's markup is %q, want the layer as it came: %s", req.Markup.ImageURL, sent[0]) + } + if req.Mask != nil { + t.Errorf("a markup went out as a mask: %s", sent[0]) + } +} + +func (e *easel) ctOf(path string) string { + e.mu.Lock() + defer e.mu.Unlock() + return e.ct[path] +} + +// partNamed is the body of the multipart part named name in a request +// sent with the given Content-Type. +func partNamed(t *testing.T, ct, body, name string) (string, bool) { + t.Helper() + _, boundary, ok := strings.Cut(ct, "boundary=") + if !ok { + t.Fatalf("not multipart: %s", ct) + } + mr := multipart.NewReader(strings.NewReader(body), boundary) + for { + part, err := mr.NextPart() + if err != nil { + return "", false + } + if part.FormName() == name { + var b bytes.Buffer + b.ReadFrom(part) + return b.String(), true + } + } +} + +// the plain OpenAI edit carries the layer as its own multipart part, +// whether it came as JSON or as a part of a multipart form. +func TestEditSendsTheMarkup(t *testing.T) { + t.Run("json", func(t *testing.T) { + s, e := easeled(t) + code, _, raw := postImages(t, s, "/v1/images/edits", "application/json", + `{"model":"art/gpt-image-1","prompt":"apply the annotation","images":[{"image_url":"`+sourceURL()+`"}],"markup":{"image_url":"`+markupURL()+`"}}`) + if code != 200 { + t.Fatalf("%d %s", code, raw) + } + sent := e.got("/v1/images/edits") + if len(sent) != 1 { + t.Fatalf("vendor was sent %v", sent) + } + got, ok := partNamed(t, e.ctOf("/v1/images/edits"), sent[0], "markup") + if !ok || got != string(markupPNG) { + t.Fatalf("the markup layer never reached the vendor whole (part %v, %q)", ok, got) + } + if _, ok := partNamed(t, e.ctOf("/v1/images/edits"), sent[0], "mask"); ok { + t.Error("a markup went out as a mask") + } + }) + t.Run("multipart", func(t *testing.T) { + s, e := easeled(t) + var buf bytes.Buffer + mw := multipart.NewWriter(&buf) + mw.WriteField("model", "art/gpt-image-1") + mw.WriteField("prompt", "apply the annotation") + for name, b := range map[string][]byte{"image": pngBytes, "markup": markupPNG} { + h := textproto.MIMEHeader{} + h.Set("Content-Disposition", `form-data; name="`+name+`"; filename="`+name+`.png"`) + h.Set("Content-Type", "image/png") + part, _ := mw.CreatePart(h) + part.Write(b) + } + mw.Close() + code, _, raw := postImages(t, s, "/v1/images/edits", mw.FormDataContentType(), buf.String()) + if code != 200 { + t.Fatalf("%d %s", code, raw) + } + sent := e.got("/v1/images/edits") + if len(sent) != 1 { + t.Fatalf("vendor was sent %v", sent) + } + got, ok := partNamed(t, e.ctOf("/v1/images/edits"), sent[0], "markup") + if !ok || got != string(markupPNG) { + t.Fatalf("the form's markup part never reached the vendor whole (part %v, %q)", ok, got) + } + if img, _ := partNamed(t, e.ctOf("/v1/images/edits"), sent[0], "image"); img != string(pngBytes) { + t.Errorf("the source image went out as %q", img) + } + }) +} + +// a markup that can't be read is the caller's 400, before anything is +// drawn; a 200 that left the layer out can't be told from one that used +// it. OpenAI's edit takes a markup or a mask, never both. +func TestEditWithAMarkupItCantSendIsRefused(t *testing.T) { + for name, extra := range map[string]string{ + "not base64": `"markup":{"image_url":"data:image/png;base64,%%%"}`, + "beside a mask": `"markup":{"image_url":"` + markupURL() + `"},"mask":{"image_url":"` + markupURL() + `"}`, + } { + t.Run(name, func(t *testing.T) { + s, e := easeled(t) + code, _, raw := postImages(t, s, "/v1/images/edits", "application/json", + `{"model":"art/gpt-image-1","prompt":"x","images":[{"image_url":"`+sourceURL()+`"}],`+extra+`}`) + if code != 400 { + t.Fatalf("%s: %d, want 400 (%s)", name, code, raw) + } + if sent := e.got("/v1/images/edits"); len(sent) != 0 { + t.Fatalf("%s: the vendor was asked anyway: %v", name, sent) + } + }) + } +} + +// an images API that takes a mask nowhere takes no markup either: the +// caller hears so rather than an edit that left the layer out. +func TestMarkupRefusedWhereNoLayerIsTaken(t *testing.T) { + mk := picture{Mime: "image/png", Data: markupPNG} + d := drawing{Prompt: "apply the annotation", N: 1, Images: []picture{{Mime: "image/png", Data: pngBytes}}, Markup: &mk} + if _, err := arkBody("doubao-seedream-4-0", d); err == nil || !strings.Contains(err.Error(), "markup") { + t.Errorf("Ark: %v, want a refusal naming the markup", err) + } + if _, err := dashScopeBody("qwen-image-edit", d); err == nil || !strings.Contains(err.Error(), "markup") { + t.Errorf("DashScope: %v, want a refusal naming the markup", err) + } + s := New() + p := provider.Provider{ID: "or", Name: "OpenRouter", Chat: "http://127.0.0.1:1/api/v1", Key: "key"} + if _, code, err := s.drawOpenRouterImages(context.Background(), p, "openai/gpt-image-1", d); code != 400 || err == nil || !strings.Contains(err.Error(), "markup") { + t.Errorf("OpenRouter: %d %v, want 400 naming the markup", code, err) + } +} diff --git a/internal/gateway/draw_volcengine.go b/internal/gateway/draw_volcengine.go index ccec7eb30..ffbd8f5e3 100644 --- a/internal/gateway/draw_volcengine.go +++ b/internal/gateway/draw_volcengine.go @@ -81,6 +81,9 @@ func arkBody(model string, d drawing) ([]byte, error) { if d.Mask != nil { return nil, errors.New("Volcengine's images API takes no mask: say in the prompt which part of the image to change") } + if d.Markup != nil { + return nil, errors.New("Volcengine's images API takes no markup layer: say in the prompt what to change") + } size, err := arkSize(d.Size) if err != nil { return nil, err