新增支持 Sora-2-Pro 1080P,支持 豆包视频 官方原生路由 - #4175
Conversation
[pull] main from QuantumNous:main
WalkthroughAdds Doubao-native request/response conversion: new Gin middleware translating Doubao video requests to internal /v1/video/generations format, router endpoints for Doubao-native API, Doubao-native response conversion path, and Sora size validation and billing ratio updates. Changes
Sequence DiagramsequenceDiagram
participant Client as Doubao Client
participant Router as Video Router
participant Middleware as DoubaoRequestConvert
participant Service as Internal Service (/v1/video/generations)
participant Adaptor as Channel Adaptor (Sora / Doubao)
Client->>Router: POST/GET /api/v3/contents/generations/...
Router->>Middleware: pass request
Middleware->>Middleware: parse body/path, extract prompt/images, normalize duration, build metadata
Middleware->>Service: rewrite path/body -> /v1/video/generations
Service->>Adaptor: create/relay task
Adaptor->>Adaptor: estimate billing (size ratio) / process
Adaptor-->>Service: task created / status
Service-->>Client: response (middleware flag controls Doubao-native conversion on fetch)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@middleware/doubao_adapter.go`:
- Around line 82-90: The loop over nativeReq.Content currently overwrites prompt
for each "text" item (var prompt), losing earlier chunks; change the logic in
the switch case for item.Type == "text" to append each item.Text to prompt
(e.g., use strings.Builder or join with a separator) instead of assigning,
ensuring empty texts are skipped and preserving ordering; update any related
uses of prompt after the loop (the variables prompt and
nativeReq.Content/item.Text are the key identifiers to modify).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d9bcaeb3-dd18-418e-96d1-7490a2c4a7c9
📒 Files selected for processing (4)
middleware/doubao_adapter.gorelay/channel/task/sora/adaptor.gorelay/common/relay_utils.gorouter/video-router.go
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
relay/channel/task/doubao/adaptor.go (1)
428-442:TaskStatusUnknownmaps to"pending"via default case.The
internalStatusToDoubaoStatushelper defaults unknown statuses (includingmodel.TaskStatusUnknown) to"pending". This is reasonable for most cases, but you may want to explicitly handleTaskStatusUnknownfor clarity.📝 Optional: Explicit handling of TaskStatusUnknown
func internalStatusToDoubaoStatus(status model.TaskStatus) string { switch status { case model.TaskStatusQueued, model.TaskStatusSubmitted, model.TaskStatusNotStart: return "pending" case model.TaskStatusInProgress: return "processing" case model.TaskStatusSuccess: return "succeeded" case model.TaskStatusFailure: return "failed" + case model.TaskStatusUnknown: + return "pending" // Treat unknown as pending default: return "pending" } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/channel/task/doubao/adaptor.go` around lines 428 - 442, Add an explicit case for model.TaskStatusUnknown in the internalStatusToDoubaoStatus function so it maps to "pending" (instead of relying on the default case); update the switch in internalStatusToDoubaoStatus to include case model.TaskStatusUnknown: return "pending" for clarity and future-proofing.middleware/doubao_adapter.go (1)
216-245: Consider handling negative duration values.Both
normalizeDurationandextractDurationSecondsaccept any numeric input without validation. If a negative value is passed (e.g.,{"value": -5}), it will be silently converted and used.🛡️ Optional: Add non-negative validation
func extractDurationSeconds(v interface{}) int { switch d := v.(type) { case float64: + if d < 0 { + return 0 + } return int(d) case int: + if d < 0 { + return 0 + } return d case map[string]interface{}: if val, ok := d["value"]; ok { return extractDurationSeconds(val) } } return 0 }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@middleware/doubao_adapter.go` around lines 216 - 245, The functions normalizeDuration and extractDurationSeconds currently convert numeric values without validation; update both to guard against negative durations by detecting negative numbers (including when nested via map[string]interface{} recursion) and returning a non-negative value instead—e.g., clamp negative results to 0 (normalizeDuration should return 0 for negative inputs and extractDurationSeconds should return 0) so downstream code never receives a negative duration; preserve the existing recursion behavior when unwrapping {"value": ...}.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/relay_task.go`:
- Around line 387-412: The fallback step currently ignores errors from
OpenAIVideoConverter.ConvertToOpenAIVideo; update the block that obtains adaptor
via GetTaskAdaptor(originTask.Platform) so that if adaptor implements
channel.OpenAIVideoConverter and ConvertToOpenAIVideo returns a non-nil error
you either log the error and/or set taskResp =
service.TaskErrorWrapper(convertErr, "convert_to_openai_video_failed",
http.StatusInternalServerError) and return (similar to how
ConvertToDoubaoNativeResponse errors are handled), ensuring the function does
not silently fall through and that respBody/taskResp is set consistently when
conversion fails.
---
Nitpick comments:
In `@middleware/doubao_adapter.go`:
- Around line 216-245: The functions normalizeDuration and
extractDurationSeconds currently convert numeric values without validation;
update both to guard against negative durations by detecting negative numbers
(including when nested via map[string]interface{} recursion) and returning a
non-negative value instead—e.g., clamp negative results to 0 (normalizeDuration
should return 0 for negative inputs and extractDurationSeconds should return 0)
so downstream code never receives a negative duration; preserve the existing
recursion behavior when unwrapping {"value": ...}.
In `@relay/channel/task/doubao/adaptor.go`:
- Around line 428-442: Add an explicit case for model.TaskStatusUnknown in the
internalStatusToDoubaoStatus function so it maps to "pending" (instead of
relying on the default case); update the switch in internalStatusToDoubaoStatus
to include case model.TaskStatusUnknown: return "pending" for clarity and
future-proofing.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 867e15ae-71fa-47eb-8697-ecc3dc96d7ef
📒 Files selected for processing (4)
middleware/doubao_adapter.gorelay/channel/adapter.gorelay/channel/task/doubao/adaptor.gorelay/relay_task.go
|
已测试通过 |
Important
📝 变更描述 / Description
(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
(请在此粘贴截图、关键日志或测试报告,以证明变更生效)
Summary by CodeRabbit
New Features
Bug Fixes / Improvements