|
1 | 1 | package provider |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "strings" |
6 | 7 |
|
@@ -461,6 +462,122 @@ type imageGenerationRequest struct { |
461 | 462 | Size string `json:"size,omitempty"` |
462 | 463 | } |
463 | 464 |
|
| 465 | +type imageInputURL struct { |
| 466 | + URL string `json:"url,omitempty"` |
| 467 | + ImageURL *chatMessageContentImageUrl `json:"image_url,omitempty"` |
| 468 | +} |
| 469 | + |
| 470 | +func (i *imageInputURL) UnmarshalJSON(data []byte) error { |
| 471 | + // Support a plain string payload, e.g. "data:image/png;base64,..." |
| 472 | + var rawURL string |
| 473 | + if err := json.Unmarshal(data, &rawURL); err == nil { |
| 474 | + i.URL = rawURL |
| 475 | + i.ImageURL = nil |
| 476 | + return nil |
| 477 | + } |
| 478 | + |
| 479 | + type alias imageInputURL |
| 480 | + var value alias |
| 481 | + if err := json.Unmarshal(data, &value); err != nil { |
| 482 | + return err |
| 483 | + } |
| 484 | + *i = imageInputURL(value) |
| 485 | + return nil |
| 486 | +} |
| 487 | + |
| 488 | +func (i *imageInputURL) GetURL() string { |
| 489 | + if i == nil { |
| 490 | + return "" |
| 491 | + } |
| 492 | + if i.ImageURL != nil && i.ImageURL.Url != "" { |
| 493 | + return i.ImageURL.Url |
| 494 | + } |
| 495 | + return i.URL |
| 496 | +} |
| 497 | + |
| 498 | +type imageEditRequest struct { |
| 499 | + Model string `json:"model"` |
| 500 | + Prompt string `json:"prompt"` |
| 501 | + Image *imageInputURL `json:"image,omitempty"` |
| 502 | + Images []imageInputURL `json:"images,omitempty"` |
| 503 | + ImageURL *imageInputURL `json:"image_url,omitempty"` |
| 504 | + Mask *imageInputURL `json:"mask,omitempty"` |
| 505 | + MaskURL *imageInputURL `json:"mask_url,omitempty"` |
| 506 | + Background string `json:"background,omitempty"` |
| 507 | + Moderation string `json:"moderation,omitempty"` |
| 508 | + OutputCompression int `json:"output_compression,omitempty"` |
| 509 | + OutputFormat string `json:"output_format,omitempty"` |
| 510 | + Quality string `json:"quality,omitempty"` |
| 511 | + ResponseFormat string `json:"response_format,omitempty"` |
| 512 | + Style string `json:"style,omitempty"` |
| 513 | + N int `json:"n,omitempty"` |
| 514 | + Size string `json:"size,omitempty"` |
| 515 | +} |
| 516 | + |
| 517 | +func (r *imageEditRequest) GetImageURLs() []string { |
| 518 | + urls := make([]string, 0, len(r.Images)+2) |
| 519 | + for _, image := range r.Images { |
| 520 | + if url := image.GetURL(); url != "" { |
| 521 | + urls = append(urls, url) |
| 522 | + } |
| 523 | + } |
| 524 | + if r.Image != nil { |
| 525 | + if url := r.Image.GetURL(); url != "" { |
| 526 | + urls = append(urls, url) |
| 527 | + } |
| 528 | + } |
| 529 | + if r.ImageURL != nil { |
| 530 | + if url := r.ImageURL.GetURL(); url != "" { |
| 531 | + urls = append(urls, url) |
| 532 | + } |
| 533 | + } |
| 534 | + return urls |
| 535 | +} |
| 536 | + |
| 537 | +func (r *imageEditRequest) HasMask() bool { |
| 538 | + if r.Mask != nil && r.Mask.GetURL() != "" { |
| 539 | + return true |
| 540 | + } |
| 541 | + return r.MaskURL != nil && r.MaskURL.GetURL() != "" |
| 542 | +} |
| 543 | + |
| 544 | +type imageVariationRequest struct { |
| 545 | + Model string `json:"model"` |
| 546 | + Prompt string `json:"prompt,omitempty"` |
| 547 | + Image *imageInputURL `json:"image,omitempty"` |
| 548 | + Images []imageInputURL `json:"images,omitempty"` |
| 549 | + ImageURL *imageInputURL `json:"image_url,omitempty"` |
| 550 | + Background string `json:"background,omitempty"` |
| 551 | + Moderation string `json:"moderation,omitempty"` |
| 552 | + OutputCompression int `json:"output_compression,omitempty"` |
| 553 | + OutputFormat string `json:"output_format,omitempty"` |
| 554 | + Quality string `json:"quality,omitempty"` |
| 555 | + ResponseFormat string `json:"response_format,omitempty"` |
| 556 | + Style string `json:"style,omitempty"` |
| 557 | + N int `json:"n,omitempty"` |
| 558 | + Size string `json:"size,omitempty"` |
| 559 | +} |
| 560 | + |
| 561 | +func (r *imageVariationRequest) GetImageURLs() []string { |
| 562 | + urls := make([]string, 0, len(r.Images)+2) |
| 563 | + for _, image := range r.Images { |
| 564 | + if url := image.GetURL(); url != "" { |
| 565 | + urls = append(urls, url) |
| 566 | + } |
| 567 | + } |
| 568 | + if r.Image != nil { |
| 569 | + if url := r.Image.GetURL(); url != "" { |
| 570 | + urls = append(urls, url) |
| 571 | + } |
| 572 | + } |
| 573 | + if r.ImageURL != nil { |
| 574 | + if url := r.ImageURL.GetURL(); url != "" { |
| 575 | + urls = append(urls, url) |
| 576 | + } |
| 577 | + } |
| 578 | + return urls |
| 579 | +} |
| 580 | + |
464 | 581 | type imageGenerationData struct { |
465 | 582 | URL string `json:"url,omitempty"` |
466 | 583 | B64 string `json:"b64_json,omitempty"` |
|
0 commit comments