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:
@@ -21,6 +21,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"img_infinite_canvas/internal/domain/design"
|
||||
"img_infinite_canvas/internal/infrastructure/netutil"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -219,7 +220,7 @@ func (c *ImageClient) Generate(ctx context.Context, prompt string, opts ImageReq
|
||||
|
||||
func (c *ImageClient) imageRequestBody(ctx context.Context, model string, prompt string, size string, count int, images []string, stream bool) ([]byte, string, error) {
|
||||
if len(images) > 0 {
|
||||
if c.inputImageTransport == "url" {
|
||||
if c.inputImageTransport == "url" && imageInputsCanUseURLTransport(images) {
|
||||
return jsonImageEditBody(model, prompt, size, count, images, stream)
|
||||
}
|
||||
return c.multipartImageEditBody(ctx, model, prompt, size, count, images, stream)
|
||||
@@ -242,6 +243,38 @@ func (c *ImageClient) imageRequestBody(ctx context.Context, model string, prompt
|
||||
return body, "application/json", err
|
||||
}
|
||||
|
||||
func imageInputsCanUseURLTransport(images []string) bool {
|
||||
for _, image := range images {
|
||||
if imageInputRequiresFileUpload(image) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func imageInputRequiresFileUpload(value string) bool {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(value, "data:image/") || strings.HasPrefix(value, "/") {
|
||||
return true
|
||||
}
|
||||
parsed, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
scheme := strings.ToLower(parsed.Scheme)
|
||||
if scheme != "http" && scheme != "https" {
|
||||
return true
|
||||
}
|
||||
host := strings.ToLower(parsed.Hostname())
|
||||
if host == "" {
|
||||
return true
|
||||
}
|
||||
return netutil.IsLocalOrPrivateHost(host)
|
||||
}
|
||||
|
||||
func jsonImageEditBody(model string, prompt string, size string, count int, images []string, stream bool) ([]byte, string, error) {
|
||||
requestPayload := map[string]any{
|
||||
"model": model,
|
||||
|
||||
Reference in New Issue
Block a user