Repository navigation
feat: add ali wan video - #2141
Conversation
# Conflicts: # controller/video_proxy.go
WalkthroughThis PR adds support for Ali video generation tasks through a new channel adaptor, refactors form data handling to use a helper function, expands TaskSubmitReq with JSON unmarshalling support, enhances Task properties with upstream/origin model names, updates video proxy routing for Gemini and Ali channels, and modifies request validation to use JSON parsing. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant ValidateMultipartDirect as ValidateMultipartDirect<br/>(relay_utils.go)
participant TaskSubmitReq as TaskSubmitReq<br/>(UnmarshalJSON)
participant GetTaskAdaptor as GetTaskAdaptor<br/>(relay_adaptor.go)
participant AliAdaptor as Ali TaskAdaptor<br/>(adaptor.go)
participant AliAPI as Ali API
Client->>ValidateMultipartDirect: POST video task request
Note over ValidateMultipartDirect: Parse JSON body
ValidateMultipartDirect->>TaskSubmitReq: Unmarshal into TaskSubmitReq
TaskSubmitReq->>TaskSubmitReq: Custom UnmarshalJSON<br/>extracts metadata (string or object)
ValidateMultipartDirect->>GetTaskAdaptor: ChannelTypeAli
GetTaskAdaptor->>AliAdaptor: Instantiate Ali adaptor
AliAdaptor->>AliAdaptor: Init, ValidateRequest
AliAdaptor->>AliAdaptor: BuildRequestURL<br/>BuildRequestHeader<br/>BuildRequestBody
AliAdaptor->>AliAPI: DoRequest (POST)
AliAPI-->>AliAdaptor: Response with taskId, status
AliAdaptor->>AliAdaptor: DoResponse (parse, map status)
AliAdaptor-->>Client: OpenAI-format video response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
relay/common/relay_utils.go (1)
122-171: Don't drop client-requested size
sizestays empty because we never assignreq.Sizeinto the local variable anymore. The Sora block then always overwrites it with720x1280, so callers lose their requested resolution and we silently skip the validation meant to guard invalid sizes. Please propagate the parsed value before the defaults run.- model = req.Model - seconds, _ = strconv.Atoi(req.Seconds) + model = req.Model + size = req.Size + seconds, _ = strconv.Atoi(req.Seconds)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
common/gin.go(4 hunks)controller/video_proxy.go(2 hunks)dto/openai_video.go(1 hunks)model/task.go(2 hunks)relay/channel/task/ali/adaptor.go(1 hunks)relay/channel/task/ali/constants.go(1 hunks)relay/common/relay_info.go(3 hunks)relay/common/relay_utils.go(1 hunks)relay/relay_adaptor.go(2 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: Sh1n3zZ
Repo: QuantumNous/new-api PR: 1659
File: relay/relay_task.go:285-305
Timestamp: 2025-08-26T09:59:00.337Z
Learning: In controller/task_video.go, the redactVideoResponseBody function sanitizes video task responses by removing bytesBase64Encoded fields and truncating base64 strings to 256 characters to prevent large binary data from being stored in task.Data.
📚 Learning: 2025-08-08T17:12:43.157Z
Learnt from: RedwindA
Repo: QuantumNous/new-api PR: 1537
File: relay/gemini_handler.go:330-342
Timestamp: 2025-08-08T17:12:43.157Z
Learning: In the new-api repository, the `GeminiEmbeddingHandler` function in `relay/gemini_handler.go` is designed specifically for native Gemini embedding requests and therefore does not require the `ConvertGeminiRequest` step that is used in the chat handler. The embedding requests are already in the native Gemini format and don't need conversion.
Applied to files:
controller/video_proxy.go
📚 Learning: 2025-08-26T09:59:00.337Z
Learnt from: Sh1n3zZ
Repo: QuantumNous/new-api PR: 1659
File: relay/relay_task.go:285-305
Timestamp: 2025-08-26T09:59:00.337Z
Learning: In controller/task_video.go, data: URLs (containing base64 encoded video data) are prevented from being stored in task.FailReason by checking if the URL starts with "data:" before assignment. This same pattern should be applied consistently across the codebase.
Applied to files:
controller/video_proxy.go
📚 Learning: 2025-08-26T09:59:00.337Z
Learnt from: Sh1n3zZ
Repo: QuantumNous/new-api PR: 1659
File: relay/relay_task.go:285-305
Timestamp: 2025-08-26T09:59:00.337Z
Learning: In controller/task_video.go, the redactVideoResponseBody function sanitizes video task responses by removing bytesBase64Encoded fields and truncating base64 strings to 256 characters to prevent large binary data from being stored in task.Data.
Applied to files:
controller/video_proxy.go
🧬 Code graph analysis (7)
relay/relay_adaptor.go (3)
constant/channel.go (1)
ChannelTypeAli(21-21)relay/channel/task/ali/adaptor.go (1)
TaskAdaptor(107-111)relay/channel/adapter.go (1)
TaskAdaptor(34-53)
model/task.go (2)
relay/common/relay_info.go (1)
ChannelMeta(56-74)constant/channel.go (1)
ChannelTypeGemini(28-28)
relay/common/relay_utils.go (2)
relay/common/relay_info.go (1)
TaskSubmitReq(488-499)common/gin.go (1)
UnmarshalBodyReusable(33-58)
relay/common/relay_info.go (1)
common/json.go (1)
Unmarshal(9-11)
common/gin.go (1)
common/json.go (2)
Unmarshal(9-11)Marshal(21-23)
controller/video_proxy.go (1)
constant/channel.go (2)
ChannelTypeGemini(28-28)ChannelTypeAli(21-21)
relay/channel/task/ali/adaptor.go (11)
model/task.go (9)
TaskStatus(13-13)TaskStatusQueued(35-35)TaskStatusInProgress(36-36)TaskStatusSuccess(38-38)TaskStatusFailure(37-37)Task(42-63)Task(297-301)Task(303-307)Properties(75-79)relay/common/relay_info.go (3)
RelayInfo(76-123)TaskSubmitReq(488-499)TaskInfo(541-551)relay/common/relay_utils.go (1)
ValidateMultipartDirect(110-176)common/gin.go (1)
UnmarshalBodyReusable(33-58)common/json.go (2)
Marshal(21-23)Unmarshal(9-11)relay/channel/api_request.go (1)
DoTaskApiRequest(301-323)service/error.go (1)
TaskErrorWrapper(140-157)dto/openai_video.go (7)
NewOpenAIVideo(43-47)OpenAIVideoError(49-52)VideoStatusQueued(10-10)VideoStatusInProgress(11-11)VideoStatusCompleted(12-12)VideoStatusFailed(13-13)VideoStatusUnknown(9-9)common/utils.go (1)
GetTimestamp(266-268)service/http_client.go (1)
GetHttpClient(49-51)relay/channel/task/ali/constants.go (2)
ModelList(3-9)ChannelName(11-11)
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
There was a problem hiding this comment.
Hook up the new seconds field.
Right now req.Seconds is ignored, so clients that pass only seconds get a hard-coded 5‑second clip no matter what they asked for. Please parse the string and fall back to it whenever duration is zero.
@@
-import (
- "bytes"
- "fmt"
- "io"
- "net/http"
- "strings"
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "net/http"
+ "strconv"
+ "strings"
)
@@
- if req.Duration > 0 {
- aliReq.Parameters.Duration = req.Duration
- } else {
- aliReq.Parameters.Duration = 5 // 默认5秒
- }
+ duration := req.Duration
+ if duration == 0 {
+ trimmedSeconds := strings.TrimSpace(req.Seconds)
+ if trimmedSeconds != "" {
+ if parsed, err := strconv.Atoi(trimmedSeconds); err == nil {
+ duration = parsed
+ }
+ }
+ }
+ if duration > 0 {
+ aliReq.Parameters.Duration = duration
+ } else {
+ aliReq.Parameters.Duration = 5 // 默认5秒
+ }Also applies to: 186-190
🤖 Prompt for AI Agents
In relay/channel/task/ali/adaptor.go around lines 4-9 (and also apply same fix
at lines 186-190), the new req.Seconds field is currently ignored causing a
default 5-second clip even when clients supply seconds; update the parsing logic
to read req.Seconds (string), parse it into an integer or duration (handle
strconv.Atoi or strconv.ParseFloat as appropriate), and if the parsed value is >
0 and the existing duration (from req.Duration or similar) is zero, set the clip
duration from the parsed seconds value; ensure to handle parse errors by falling
back to the default behavior and avoid panics by validating input.
| if req.Size != "" { | ||
| resolution := strings.ToUpper(req.Size) | ||
| // 支持 480p, 720p, 1080p 或 480P, 720P, 1080P | ||
| if !strings.HasSuffix(resolution, "P") { | ||
| resolution = resolution + "P" | ||
| } | ||
| aliReq.Parameters.Resolution = resolution | ||
| } else { | ||
| // 根据模型设置默认分辨率 | ||
| if strings.HasPrefix(req.Model, "wan2.5") { | ||
| aliReq.Parameters.Resolution = "1080P" | ||
| } else if strings.HasPrefix(req.Model, "wan2.2-i2v-flash") { | ||
| aliReq.Parameters.Resolution = "720P" | ||
| } else if strings.HasPrefix(req.Model, "wan2.2-i2v-plus") { | ||
| aliReq.Parameters.Resolution = "1080P" | ||
| } else { | ||
| aliReq.Parameters.Resolution = "720P" | ||
| } | ||
| } |
There was a problem hiding this comment.
Stop treating size as a resolution.
Line 169 turns values such as "832*480" into "832*480P" and pushes them into parameters.resolution. Ali expects text-to-video payloads to carry that "832*480" value under parameters.size, so these requests are rejected even though the client supplied valid input. Please keep dimension-style tokens on size and reserve the resolution path for values that actually look like 480/720/1080p.
@@
- if req.Size != "" {
- resolution := strings.ToUpper(req.Size)
- // 支持 480p, 720p, 1080p 或 480P, 720P, 1080P
- if !strings.HasSuffix(resolution, "P") {
- resolution = resolution + "P"
- }
- aliReq.Parameters.Resolution = resolution
- } else {
+ if req.Size != "" {
+ rawSize := strings.TrimSpace(req.Size)
+ upperSize := strings.ToUpper(rawSize)
+ if strings.ContainsAny(upperSize, "X*") {
+ aliReq.Parameters.Size = rawSize
+ } else {
+ if !strings.HasSuffix(upperSize, "P") {
+ upperSize += "P"
+ }
+ aliReq.Parameters.Resolution = upperSize
+ }
+ } else {🤖 Prompt for AI Agents
In relay/channel/task/ali/adaptor.go around lines 165 to 183, the code currently
treats any req.Size as a resolution (appending "P" and storing in
Parameters.Resolution), which corrupts dimension-style values like "832*480";
instead detect whether req.Size is a resolution token (e.g., matches digits
optionally followed by "P" or ends with "P" like 480/480P/720P/1080P) and only
then normalize/uppercase and write to aliReq.Parameters.Resolution; if req.Size
contains a dimension pattern (e.g., contains '*' or matches WIDTHxHEIGHT)
preserve it as-is and assign it to aliReq.Parameters.Size (do not append "P" or
change it), and if req.Size is empty keep the existing model-based
default-resolution logic unchanged.
* feat: add ali wan video * refactor: use same UnmarshalBodyReusable * feat: enhance request body metadata * feat: opt wan convertToOpenAIVideo * feat: add wan support other param via json metadata * refactor: remove unused code * fix ali --------- Co-authored-by: feitianbubu <[email protected]>
* feat: add ali wan video * refactor: use same UnmarshalBodyReusable * feat: enhance request body metadata * feat: opt wan convertToOpenAIVideo * feat: add wan support other param via json metadata * refactor: remove unused code * fix ali --------- Co-authored-by: feitianbubu <[email protected]>
* feat: add ali wan video * refactor: use same UnmarshalBodyReusable * feat: enhance request body metadata * feat: opt wan convertToOpenAIVideo * feat: add wan support other param via json metadata * refactor: remove unused code * fix ali --------- Co-authored-by: feitianbubu <[email protected]>
Summary by CodeRabbit
New Features
Improvements