fix(piagent): upload local and private reference images as multipart
When URL input transport is configured, the remote image API cannot fetch localhost or private-network reference URLs. Detect such hosts and fall back to multipart file upload so those references still reach the model. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -234,6 +234,60 @@ func TestImageClientSendsReferenceImagesByURLWhenConfigured(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageClientUploadsLocalhostReferenceImagesEvenWhenURLTransportConfigured(t *testing.T) {
|
||||
var sawMultipartEdit bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/reference.png":
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
_, _ = w.Write([]byte("local image bytes"))
|
||||
case "/v1/images/edits":
|
||||
sawMultipartEdit = true
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") {
|
||||
t.Errorf("expected multipart edit request for local image, got %s", r.Header.Get("Content-Type"))
|
||||
}
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
t.Errorf("parse multipart form: %v", err)
|
||||
return
|
||||
}
|
||||
file, header, err := r.FormFile("image")
|
||||
if err != nil {
|
||||
t.Errorf("expected image file field: %v", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("read image file: %v", err)
|
||||
return
|
||||
}
|
||||
if header.Filename != "reference.png" || header.Header.Get("Content-Type") != "image/png" || string(data) != "local image bytes" {
|
||||
t.Errorf("unexpected uploaded image: filename=%q content_type=%q data=%q", header.Filename, header.Header.Get("Content-Type"), string(data))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[{"url":"https://example.com/edited.png"}]}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewImageClient(ImageClientOptions{BaseURL: server.URL, APIKey: "test-key", InputImageTransport: "url"})
|
||||
image, err := client.Generate(context.Background(), "edit the local image", ImageRequestOptions{
|
||||
Model: "gpt-image-2",
|
||||
Images: []string{server.URL + "/reference.png"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if image.URL != "https://example.com/edited.png" {
|
||||
t.Fatalf("expected edited image, got %#v", image)
|
||||
}
|
||||
if !sawMultipartEdit {
|
||||
t.Fatal("expected multipart image edit request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageClientUploadsInternalReferenceImagesAsMultipart(t *testing.T) {
|
||||
var sawEdit bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user