feat(volcengine): Add support for image editing for Volcengine channel - #1442
feat(volcengine): Add support for image editing for Volcengine channel#14420daysseus wants to merge 1 commit into
Conversation
WalkthroughTwo new fields, Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant APIHandler
participant VolcengineAdaptor
Client->>APIHandler: POST /image with multipart form (includes guidance_scale, seed)
APIHandler->>APIHandler: Parse guidance_scale and seed
APIHandler->>VolcengineAdaptor: ConvertImageRequest (with parsed fields)
VolcengineAdaptor->>VolcengineAdaptor: Extract image, encode as base64
VolcengineAdaptor->>VolcengineAdaptor: Build JSON request with fields and image
VolcengineAdaptor->>APIHandler: Return JSON request
APIHandler->>Volcengine: Forward JSON request
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~15 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (2)
relay/image_handler.go (1)
56-63: Consider adding validation for guidance_scale range.The parsing logic is correct, but you may want to validate that
guidance_scalefalls within an acceptable range for Volcengine's API to provide better error messages to users.guidanceScaleStr := formData.Get("guidance_scale") if guidanceScaleStr != "" { guidanceScale, err := strconv.ParseFloat(guidanceScaleStr, 64) if err != nil { return nil, fmt.Errorf("invalid guidance_scale parameter: %v", err) } + // Add validation based on Volcengine's API requirements + if guidanceScale < 0 || guidanceScale > 20 { + return nil, fmt.Errorf("guidance_scale must be between 0 and 20") + } imageRequest.GuidanceScale = guidanceScale }relay/channel/volcengine/adaptor.go (1)
134-159: Consider making the file size limit configurable.The 10MB file size limit is hardcoded. Consider making this configurable or at least documenting it in the function comment to help future maintainers.
+// fileHeaderToBase64 converts an uploaded file to a data URI string. +// The maximum allowed file size is 10MB. func fileHeaderToBase64(fileHeader *multipart.FileHeader) (string, error) { // Define maximum allowed file size (10MB) const maxFileSize = 10 << 20 // 10MB
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
dto/dalle.go(1 hunks)relay/channel/volcengine/adaptor.go(3 hunks)relay/image_handler.go(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
relay/image_handler.go (2)
Learnt from: feitianbubu
PR: #1228
File: router/main.go:28-36
Timestamp: 2025-06-15T12:38:11.806Z
Learning: gin.Context implements context.Context interface since Gin v1.8.0, providing the methods Deadline(), Done(), Err(), and Value(). When using Gin v1.8.0 or later, gin.Context can be passed directly to functions expecting context.Context without needing to extract c.Request.Context().
Learnt from: feitianbubu
PR: #1228
File: router/main.go:28-36
Timestamp: 2025-06-15T12:38:11.806Z
Learning: gin.Context implements context.Context interface since Gin v1.8.0, providing the methods Deadline(), Done(), Err(), and Value(). When using Gin v1.8.0 or later, gin.Context can be passed directly to functions expecting context.Context without needing to extract c.Request.Context().
🧬 Code Graph Analysis (2)
relay/image_handler.go (1)
common/str.go (1)
String2Int(63-69)
relay/channel/volcengine/adaptor.go (3)
relay/channel/adapter.go (1)
Adaptor(13-29)relay/common/relay_info.go (1)
RelayInfo(62-112)relay/channel/api_request.go (1)
SetupApiRequestHeader(23-35)
🔇 Additional comments (1)
dto/dalle.go (1)
19-20: LGTM! Well-structured field additions.The new
SeedandGuidanceScalefields are properly defined with appropriate types and JSON tags. The use ofomitemptyensures backward compatibility for requests that don't include these optional parameters.
| case constant.RelayModeImagesEdits: | ||
| return info.BaseUrl + "/api/v3/images/generations", nil |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Verify that Volcengine uses the same endpoint for image generation and editing.
The change to use /api/v3/images/generations for both generation and editing modes should be verified against Volcengine's API documentation.
🌐 Web query:
What is the correct API endpoint for Volcengine image editing? Does Volcengine use the same endpoint for both image generation and image editing at /api/v3/images/generations?
💡 Result:
Volcengine provides APIs for both image generation and image editing, each with distinct endpoints. The endpoint /api/v3/images/generations is specifically designed for image generation tasks. For image editing functionalities, Volcengine offers the ImageX service, which includes various image processing capabilities.
To utilize the ImageX service, you can refer to the official documentation for detailed information on available endpoints and usage guidelines. (volcengine.com)
In summary, Volcengine uses separate endpoints for image generation and image editing, and the /api/v3/images/generations endpoint is dedicated to image generation tasks.
Citations:
Use the correct Volcengine image-editing endpoint
Volcengine’s /api/v3/images/generations endpoint only supports image generation. Image editing belongs to their separate ImageX service and must use a dedicated editing endpoint (see Volcengine docs¹).
Please update the code in relay/channel/volcengine/adaptor.go so that under case constant.RelayModeImagesEdits: you point to the proper ImageX edit URL instead of /api/v3/images/generations.
• File: relay/channel/volcengine/adaptor.go
• Lines: around 195–196
case constant.RelayModeImagesEdits:
- return info.BaseUrl + "/api/v3/images/generations", nil
+ return info.BaseUrl + "<correct-editing-endpoint>", nil¹ https://www.volcengine.com/docs/508/1263359?utm_source=openai
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In relay/channel/volcengine/adaptor.go around lines 195 to 196, the URL for the
case constant.RelayModeImagesEdits incorrectly points to the image generation
endpoint /api/v3/images/generations. Update this to use the correct Volcengine
ImageX service editing endpoint as specified in their documentation, replacing
the URL string with the dedicated image editing endpoint URL.
Signed-off-by: 0daysseus <74554363+0daysseus@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (2)
relay/channel/volcengine/adaptor.go (2)
86-113: Type-specific parsing addresses previous concerns, but watermark handling needs refinement.This implementation correctly addresses the previous review comment about avoiding generic JSON parsing by explicitly handling known fields that require type conversion.
However, the watermark parsing on line 107 only treats
"true"as true and everything else as false. This doesn't distinguish between explicit"false"and missing values, which could be problematic for API semantics.Consider improving watermark parsing to handle explicit boolean values:
-case "watermark": - jsonRequest[key] = values[0] == "true" +case "watermark": + if v, err := strconv.ParseBool(values[0]); err == nil { + jsonRequest[key] = v + }
201-202: Incorrect endpoint - this duplicates a previous review concern.As noted in previous reviews, Volcengine uses separate endpoints for image generation and image editing. The
/api/v3/images/generationsendpoint is only for generation, not editing.Please update to use the correct Volcengine ImageX service editing endpoint instead of reusing the generations endpoint.
🧹 Nitpick comments (1)
relay/channel/volcengine/adaptor.go (1)
39-62: Good refactoring to support JSON format for image editing.The conversion from multipart form data to JSON format aligns with Volcengine's API requirements. The error handling and flow control are appropriate.
Consider making the
maxMemoryconstant consistent with the file size limit used infileHeaderToBase64(10MB vs 32MB).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
dto/dalle.go(1 hunks)relay/channel/volcengine/adaptor.go(3 hunks)relay/image_handler.go(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- dto/dalle.go
- relay/image_handler.go
🔇 Additional comments (5)
relay/channel/volcengine/adaptor.go (5)
5-6: LGTM! Import additions support the new JSON conversion functionality.The new imports (
encoding/base64,encoding/json,strconv) are appropriately added to support the conversion from multipart form data to JSON with base64-encoded images.Also applies to: 19-19
64-84: Well-structured helper function for building JSON requests.The function clearly separates concerns by delegating to specific helper functions for form field processing and image extraction. The error handling is appropriate.
115-138: Comprehensive image file extraction with good fallback logic.The function handles multiple common field naming conventions for image uploads, including array notation and indexed fields. The fallback mechanism ensures robust file detection.
140-165: Robust file processing with appropriate size limits and error handling.The function properly enforces file size limits, handles file I/O operations safely with deferred cleanup, and creates correctly formatted data URI strings. The error messages are descriptive and helpful for debugging.
210-210: Appropriate Content-Type header for JSON requests.Setting the Content-Type to
application/jsoncorrectly supports the new JSON request format for image editing operations.
This commit introduces support for the image editing feature (RelayModeImagesEdits) for the Volcengine channel. Previously, this functionality was not implemented.
The key changes include:
ConvertImageRequestto handle image edit requests by converting them to theapplication/jsonformat required by the Volcengine API, instead of the incorrectmultipart/form-data.seed,guidance_scale, andwatermark.dto.ImageRequestand theimage_handlerto recognize and process these new parameters.Summary by CodeRabbit
New Features
Bug Fixes
Chores