feat: support openAI sdk retrieve videos#2000
Conversation
WalkthroughPropagates video task response data into local task objects and adjusts relay response construction. For /v1/videos/ URIs, the relay now returns originTask.Data directly; otherwise it marshals a success DTO. Adds t.Data assignment during task update. No exported APIs changed. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant Relay as Relay (relay_task.go)
participant Controller as Controller (task_video.go)
participant Origin as Origin Service
Client->>Relay: GET /v1/videos/{id}
Relay->>Origin: Fetch task by ID
Origin-->>Relay: originTask (includes Data)
alt URI starts with /v1/videos/
Relay-->>Client: originTask.Data (JSON)
else Other URIs
Relay->>Relay: Marshal TaskModel2Dto(originTask)
Relay-->>Client: Success response (DTO)
end
note over Controller,Origin: During updates (new API), Controller assigns t.Data -> task.Data
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 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
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
controller/task_video.go(1 hunks)relay/relay_task.go(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-26T09:59:00.337Z
Learnt from: Sh1n3zZ
PR: QuantumNous/new-api#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:
relay/relay_task.gocontroller/task_video.go
🧬 Code graph analysis (1)
relay/relay_task.go (1)
service/error.go (1)
TaskErrorWrapper(139-155)
🔇 Additional comments (2)
relay/relay_task.go (2)
365-367: LGTM: Early return pattern improves code clarity.The early return when
respBodyis already populated simplifies control flow and avoids unnecessary processing.
373-379: LGTM: Proper error handling for marshaling failures.The fallback logic correctly marshals a TaskResponse and handles JSON marshaling errors appropriately.
| taskResult.Url = t.FailReason | ||
| taskResult.Progress = t.Progress | ||
| taskResult.Reason = t.FailReason | ||
| task.Data = t.Data |
There was a problem hiding this comment.
🧩 Analysis chain
Verify data sanitization for New API response format.
The Data field is now propagated from the New API response, but unlike the alternative parsing path (line 104) which applies redactVideoResponseBody to sanitize large binary data, this path stores t.Data directly without sanitization. This inconsistency could lead to large base64-encoded video data being stored in the database when processing New API format responses.
Verify whether the New API response format already sanitizes the Data field or if sanitization should be applied here as well:
Based on learnings.
🏁 Script executed:
#!/bin/bash
# Description: Check if there are any other locations where New API response data is sanitized
# or if the New API format is expected to return pre-sanitized data
# Search for sanitization patterns in New API response handling
rg -n "dto\.TaskResponse\[model\.Task\]" -A 10 -B 5
# Check if there's documentation or comments about New API data format
rg -n "New API.*Data" -C 3Length of output: 1122
🏁 Script executed:
#!/bin/bash
# Locate the sanitization function
rg -n "func redactVideoResponseBody" -C3
# Inspect the legacy parsing path to see if it applies sanitization
rg -n "func ParseTaskResult" -C10Length of output: 354
🏁 Script executed:
#!/bin/bash
# Show imports in controller/task_video.go to locate adaptor package path
sed -n '1,50p' controller/task_video.go
# Search for all definitions and references of ParseTaskResult
rg -n "ParseTaskResult\(" -C3Length of output: 6385
Sanitize video Data for New API response
The New API path at controller/task_video.go:100 assigns t.Data directly, bypassing redactVideoResponseBody. This can store unbounded base64-encoded video data in the database. Apply redactVideoResponseBody (or equivalent truncation) here as well.
🤖 Prompt for AI Agents
In controller/task_video.go around line 100, the New API path assigns task.Data
= t.Data directly, which bypasses redactVideoResponseBody and can store
unbounded base64 video blobs; replace that direct assignment by passing t.Data
through redactVideoResponseBody (or an equivalent truncation/sanitization
function) and assign the sanitized result to task.Data so only a
truncated/redacted payload is stored.
| if strings.HasPrefix(c.Request.RequestURI, "/v1/videos/") { | ||
| respBody = originTask.Data | ||
| return | ||
| } |
There was a problem hiding this comment.
Validate originTask.Data before returning.
Directly returning originTask.Data for /v1/videos/ URIs assumes the data is valid, non-nil JSON. However, if originTask.Data is nil, empty, or contains invalid JSON (due to database corruption, incomplete task updates, or sanitization issues), the response will fail with cryptic errors since the Content-Type is set to "application/json" in line 207.
Apply this diff to add validation:
if strings.HasPrefix(c.Request.RequestURI, "/v1/videos/") {
+ if len(originTask.Data) == 0 {
+ taskResp = service.TaskErrorWrapperLocal(errors.New("task data is empty"), "task_data_empty", http.StatusInternalServerError)
+ return
+ }
+ // Validate it's valid JSON
+ if !json.Valid(originTask.Data) {
+ taskResp = service.TaskErrorWrapperLocal(errors.New("task data is not valid JSON"), "invalid_task_data", http.StatusInternalServerError)
+ return
+ }
respBody = originTask.Data
return
}🤖 Prompt for AI Agents
In relay/relay_task.go around lines 369 to 372, the code returns originTask.Data
directly for requests under /v1/videos/ without checking it; validate
originTask.Data before returning: if originTask.Data is nil or empty, set
respBody to a safe JSON payload (e.g., []byte("null")) or return a proper JSON
error response; otherwise use json.Valid(originTask.Data) to ensure it is valid
JSON and if not valid, log a warning and again set respBody to a safe JSON
payload or return a 5xx JSON error; ensure you do not change the existing
Content-Type and avoid panic by never returning raw nil/invalid bytes.
…-video-sdk feat: support openAI sdk retrieve videos
支持openai sdk获取Sora视频
参考: https://platform.openai.com/docs/api-reference/videos/retrieve
Summary by CodeRabbit