Skip to content

新增Sora视频渠道 - #1988

Merged
seefs001 merged 3 commits into
QuantumNous:mainfrom
feitianbubu:pr/add-sora
Oct 9, 2025
Merged

seefs001 merged 3 commits into
QuantumNous:mainfrom
feitianbubu:pr/add-sora

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Oct 9, 2025 •

Copy link
Copy Markdown
Member
  1. 新增OpenAI Sora视频渠道,接口使用官方标准格式 https://platform.openai.com/docs/api-reference/videos/create, 支持文生视频,图生视频
  2. 其他视频渠道可灵,即梦,Vidu兼容Sora格式
  3. 请求示例:
curl http://localhost:3000/v1/videos \
  --request POST \
  --header 'Content-Type: multipart/form-data' \
  --form 'model=jimeng_v30_1080p' \
  --form 'prompt=angel dancing' \
  --form 'input_reference=@600ec4d1-77cf-44ac-bc71-51409c976736.png' \
  --form 'size=1280x7201'
  1. 返回示例:
{
  "task_id": "4326796003218200779"
}
  1. 任务日志:
image
  1. 视频效果:
image

Summary by CodeRabbit

  • New Features
    • Added Sora channel with selectable models (sora-2, sora-2-pro) in the UI.
    • Introduced OpenAI-compatible video APIs: POST /v1/videos to submit tasks and GET /v1/videos/:task_id/content to retrieve generated video content.
  • Improvements
    • Enhanced multipart form handling for more reliable video task submissions, including image inputs and metadata.
    • Improved task validation and routing for video operations.
    • Optimized request handling to reduce errors and improve stability when processing uploads and streaming content.

@coderabbitai

coderabbitai Bot commented Oct 9, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds Sora video channel support end-to-end: new channel constant, task adaptor, routing for POST /v1/videos and GET /v1/videos/:task_id/content, multipart parsing utilities, reusable multipart form parsing, controller proxy for video content, adaptor initialization update, and minor DTO/UI additions.

Changes

Cohort / File(s) Summary
Sora task adaptor
relay/channel/task/sora/adaptor.go, relay/channel/task/sora/constants.go
New Sora TaskAdaptor with init, request build/exec, response parsing, task fetching, model list, and status mapping; exported constants for model list and channel name.
Channel constants and adaptor registration
constant/channel.go, relay/relay_adaptor.go
Adds ChannelTypeSora (55), base URL/name mappings; registers Sora adaptor in GetTaskAdaptor.
Video routing and proxy
router/video-router.go, controller/video_proxy.go
Introduces POST /v1/videos (RelayTask) and GET /v1/videos/:task_id/content (VideoProxy); proxy streams upstream video content with auth and cache headers.
Distributor middleware
middleware/distributor.go
Adds /v1/videos path handling: sets relay mode (VideoSubmit for POST) and extracts model from multipart form.
Relay utils multipart handling
relay/common/relay_utils.go
Adds multipart validation/parsing for task submit, action inference, Vidu-specific handling; updates storeTaskRequest to use TaskSubmitReq; numeric parsing helpers and known-field filtering.
Reusable multipart parsing
common/gin.go
Adds ParseMultipartFormReusable to build a multipart.Reader from cached body and return a form without consuming the request stream.
Task video controller init
controller/task_video.go
Initializes video adaptor with RelayInfo including channel base URL prior to task processing.
DTO addition
dto/openai_response.go
Adds OpenAIVideoResponse struct for OpenAI-style video response metadata.
Web UI channel option
web/src/constants/channel.constants.js
Adds Sora (value 55) to CHANNEL_OPTIONS.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant API as API /v1/videos
  participant Dist as Distributor
  participant Adaptor as Sora TaskAdaptor
  participant Upstream as OpenAI Sora API

  Client->>API: POST /v1/videos (multipart)
  API->>Dist: Route before auth/distribute
  Dist->>Adaptor: ValidateMultipartDirect + Init(RelayInfo)
  Adaptor->>Adaptor: BuildRequestURL/Header/Body
  Adaptor->>Upstream: POST /v1/videos
  Upstream-->>Adaptor: 200 {task_id,...}
  Adaptor-->>Client: 200 {task_id}

  rect rgba(230,245,255,0.5)
  note right of Client: New flow for Sora video submit
  end
Loading
sequenceDiagram
  autonumber
  participant Client
  participant API as API /v1/videos/:task_id/content
  participant Store as Model/Channel Cache
  participant Upstream as OpenAI Sora API

  Client->>API: GET /v1/videos/:task_id/content
  API->>Store: Lookup task, channel, baseURL, key
  API->>Upstream: GET /v1/videos/{task_id}/content (Authorization)
  Upstream-->>API: Stream (status, headers, body)
  API-->>Client: Proxy stream (Cache-Control: public, max-age=86400)

  rect rgba(240,255,240,0.5)
  note right of API: New proxy endpoint for video content
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • seefs001
  • Calcium-Ion
  • xyfacai

Poem

I twitch my ears at Sora’s call,
New streams to fetch, new tasks and all—
Hop-hop, I proxy bytes in flight,
Multipart moons glow soft and bright.
Models nibble prompts like clover,
A 55-carrot channel takeover.
Thump! Release is done—game over. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely summarizes the primary change of adding the Sora video channel, directly reflecting the main purpose of the pull request and matching the detailed code modifications.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Nitpick comments (1)
controller/video_proxy.go (1)

117-124: Consider selective header forwarding and proper WriteHeader ordering.

Two improvements to consider:

  1. Selective header forwarding (lines 117-121): Copying all headers from the upstream response can include problematic headers like Transfer-Encoding, Connection, Content-Length (if body size changes), etc. Consider filtering to only forward relevant headers like Content-Type, Content-Disposition, and custom headers.

  2. WriteHeader ordering (line 124): WriteHeader is called after setting headers via c.Writer.Header().Add() (lines 117-121), but Header().Set() is also called on line 123. Ensure headers are set before calling WriteHeader to avoid issues.

Example of selective header forwarding:

-	for key, values := range resp.Header {
-		for _, value := range values {
-			c.Writer.Header().Add(key, value)
-		}
-	}
-
+	// Forward only relevant headers
+	allowedHeaders := []string{"Content-Type", "Content-Disposition", "Content-Length"}
+	for _, key := range allowedHeaders {
+		if values := resp.Header.Values(key); len(values) > 0 {
+			for _, value := range values {
+				c.Writer.Header().Add(key, value)
+			}
+		}
+	}
 	c.Writer.Header().Set("Cache-Control", "public, max-age=86400")
 	c.Writer.WriteHeader(resp.StatusCode)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e24f13a and b880094.

📒 Files selected for processing (12)
  • common/gin.go (2 hunks)
  • constant/channel.go (3 hunks)
  • controller/task_video.go (1 hunks)
  • controller/video_proxy.go (1 hunks)
  • dto/openai_response.go (1 hunks)
  • middleware/distributor.go (1 hunks)
  • relay/channel/task/sora/adaptor.go (1 hunks)
  • relay/channel/task/sora/constants.go (1 hunks)
  • relay/common/relay_utils.go (3 hunks)
  • relay/relay_adaptor.go (2 hunks)
  • router/video-router.go (1 hunks)
  • web/src/constants/channel.constants.js (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (7)
middleware/distributor.go (1)
relay/constant/relay_mode.go (2)
  • RelayModeUnknown (9-9)
  • RelayModeVideoSubmit (44-44)
controller/task_video.go (1)
relay/common/relay_info.go (2)
  • RelayInfo (74-121)
  • ChannelMeta (54-72)
relay/relay_adaptor.go (3)
constant/channel.go (1)
  • ChannelTypeSora (55-55)
relay/channel/task/sora/adaptor.go (1)
  • TaskAdaptor (61-65)
relay/channel/adapter.go (1)
  • TaskAdaptor (32-51)
relay/common/relay_utils.go (3)
relay/common/relay_info.go (2)
  • RelayInfo (74-121)
  • TaskSubmitReq (484-493)
common/gin.go (2)
  • ParseMultipartFormReusable (118-139)
  • UnmarshalBodyReusable (31-52)
constant/task.go (2)
  • TaskActionTextGenerate (15-15)
  • TaskActionGenerate (14-14)
relay/channel/task/sora/adaptor.go (7)
relay/common/relay_info.go (2)
  • RelayInfo (74-121)
  • TaskInfo (503-512)
relay/common/relay_utils.go (1)
  • ValidateMultipartDirect (108-133)
common/gin.go (1)
  • GetRequestBody (17-29)
relay/channel/api_request.go (1)
  • DoTaskApiRequest (300-322)
service/error.go (1)
  • TaskErrorWrapper (139-155)
relay/channel/task/sora/constants.go (2)
  • ModelList (3-6)
  • ChannelName (8-8)
model/task.go (4)
  • TaskStatusQueued (16-16)
  • TaskStatusInProgress (17-17)
  • TaskStatusSuccess (19-19)
  • TaskStatusFailure (18-18)
controller/video_proxy.go (3)
model/task.go (2)
  • GetByOnlyTaskId (184-196)
  • TaskStatusSuccess (19-19)
logger/logger.go (1)
  • LogError (64-66)
model/channel_cache.go (1)
  • CacheGetChannel (212-224)
router/video-router.go (3)
controller/video_proxy.go (1)
  • VideoProxy (14-129)
middleware/auth.go (1)
  • TokenAuth (178-289)
middleware/distributor.go (1)
  • Distribute (28-123)
🔇 Additional comments (4)
web/src/constants/channel.constants.js (1)

172-176: LGTM! Channel constant addition is correct.

The new Sora channel entry with value 55 correctly aligns with the backend constant ChannelTypeSora = 55 defined in constant/channel.go.

router/video-router.go (1)

12-12: Verify the security implications of the unauthenticated video proxy endpoint.

The VideoProxy route is registered before the TokenAuth() and Distribute() middleware (line 13), making it publicly accessible without authentication. While this may be intentional for serving completed video content, it means anyone with a task ID can access the video.

Please confirm:

  1. Is public access to completed video content intentional?
  2. Does the task lookup in VideoProxy (controller/video_proxy.go) provide adequate access control?
  3. Should there be additional checks (e.g., verifying the requester owns the task)?

If public access is not intended, move this route registration to after the middleware:

 	videoV1Router := router.Group("/v1")
-	videoV1Router.GET("/videos/:task_id/content", controller.VideoProxy)
 	videoV1Router.Use(middleware.TokenAuth(), middleware.Distribute())
 	{
+		videoV1Router.GET("/videos/:task_id/content", controller.VideoProxy)
 		videoV1Router.POST("/video/generations", controller.RelayTask)
constant/channel.go (1)

55-55: LGTM! Sora channel constants are correctly defined.

The new channel type constant, base URL, and name mapping follow the established pattern and are consistent with the frontend constants.

Also applies to: 116-116, 171-171

relay/common/relay_utils.go (1)

56-56: storeTaskRequest signature change confirmed safe – only caller passes a TaskSubmitReq, so existing calls compile without error.

Comment thread common/gin.go
Comment on lines +118 to +139
func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
requestBody, err := GetRequestBody(c)
if err != nil {
return nil, err
}

contentType := c.Request.Header.Get("Content-Type")
boundary := ""
if idx := strings.Index(contentType, "boundary="); idx != -1 {
boundary = contentType[idx+9:]
}

reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
form, err := reader.ReadForm(32 << 20) // 32 MB max memory
if err != nil {
return nil, err
}

// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
return form, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Improve boundary extraction to handle standard Content-Type formats.

Lines 124-128 use simplistic string manipulation to extract the boundary from the Content-Type header. This approach has several issues:

  1. Doesn't handle quoted boundaries: boundary="----WebKitFormBoundary..."
  2. Doesn't handle additional parameters after the boundary
  3. Doesn't validate if a boundary was found (when idx == -1, boundary will be empty string)
  4. Doesn't handle spaces around the equals sign

Use the standard mime.ParseMediaType function for robust parsing:

+import (
+	"mime"
+	// ... other imports
+)
+
 func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
 	requestBody, err := GetRequestBody(c)
 	if err != nil {
 		return nil, err
 	}
 
 	contentType := c.Request.Header.Get("Content-Type")
-	boundary := ""
-	if idx := strings.Index(contentType, "boundary="); idx != -1 {
-		boundary = contentType[idx+9:]
+	_, params, err := mime.ParseMediaType(contentType)
+	if err != nil {
+		return nil, fmt.Errorf("failed to parse Content-Type: %w", err)
+	}
+	boundary := params["boundary"]
+	if boundary == "" {
+		return nil, fmt.Errorf("boundary not found in Content-Type header")
 	}
 
 	reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
requestBody, err := GetRequestBody(c)
if err != nil {
return nil, err
}
contentType := c.Request.Header.Get("Content-Type")
boundary := ""
if idx := strings.Index(contentType, "boundary="); idx != -1 {
boundary = contentType[idx+9:]
}
reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
form, err := reader.ReadForm(32 << 20) // 32 MB max memory
if err != nil {
return nil, err
}
// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
return form, nil
}
// Add “mime” (and ensure “fmt” is imported) to your import block:
//
// import (
// "bytes"
// "io"
// "mime"
// "fmt"
// "mime/multipart"
// "strings"
// "github.com/gin-gonic/gin"
// )
func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
requestBody, err := GetRequestBody(c)
if err != nil {
return nil, err
}
contentType := c.Request.Header.Get("Content-Type")
_, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, fmt.Errorf("failed to parse Content-Type: %w", err)
}
boundary := params["boundary"]
if boundary == "" {
return nil, fmt.Errorf("boundary not found in Content-Type header")
}
reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
form, err := reader.ReadForm(32 << 20) // 32 MB max memory
if err != nil {
return nil, err
}
// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
return form, nil
}
🤖 Prompt for AI Agents
In common/gin.go around lines 118 to 139, the current boundary extraction uses
manual string indexing which fails for quoted boundaries, extra parameters,
spaces, and missing boundary cases; replace that logic with
mime.ParseMediaType(c.Request.Header.Get("Content-Type")) to parse the media
type and parameters, extract the "boundary" param (it will handle quotes and
spaces), validate that a non-empty boundary exists and return an error if
missing, then pass the boundary to multipart.NewReader; ensure you trim any
surrounding spaces only if needed and keep resetting c.Request.Body as before.

Comment thread controller/video_proxy.go
Comment on lines +37 to +46
if !exists || task == nil {
logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to get task %s: %s", taskID, err.Error()))
c.JSON(http.StatusNotFound, gin.H{
"error": gin.H{
"message": "Task not found",
"type": "invalid_request_error",
},
})
return
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix potential nil pointer dereference when logging error.

On line 38, the code logs err.Error() when the task is not found, but err may be nil at this point because the condition is !exists || task == nil. If the task doesn't exist (but there's no database error), err will be nil and calling err.Error() will panic.

Apply this diff to fix the issue:

 	if !exists || task == nil {
-		logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to get task %s: %s", taskID, err.Error()))
+		logger.LogError(c.Request.Context(), fmt.Sprintf("Task not found: %s", taskID))
 		c.JSON(http.StatusNotFound, gin.H{
 			"error": gin.H{
 				"message": "Task not found",
 				"type":    "invalid_request_error",
 			},
 		})
 		return
 	}
🤖 Prompt for AI Agents
In controller/video_proxy.go around lines 37 to 46, the error logging calls
err.Error() even when err may be nil due to the condition (!exists || task ==
nil); change the log to safely include error details by using a nil-safe
representation (e.g., if err != nil use err.Error() or otherwise use a
descriptive string or fmt.Sprintf("%v", err)) so the logger never dereferences a
nil error, and keep the response behavior the same.

Comment thread dto/openai_response.go
Comment on lines +236 to +244
type OpenAIVideoResponse struct {
Id string `json:"id" example:"file-abc123"`
Object string `json:"object" example:"file"`
Bytes int64 `json:"bytes" example:"120000"`
CreatedAt int64 `json:"created_at" example:"1677610602"`
ExpiresAt int64 `json:"expires_at" example:"1677614202"`
Filename string `json:"filename" example:"mydata.jsonl"`
Purpose string `json:"purpose" example:"fine-tune"`
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

Verify that OpenAIVideoResponse matches the actual API response format.

The struct fields and example tags (e.g., Purpose: "fine-tune", Object: "file") suggest this is a file metadata response rather than a video generation response. According to the PR description, the Sora API returns { "task_id": "4326796003218200779" }, which doesn't match this struct.

Please confirm:

  1. Is this struct used for the video generation response or for a different endpoint?
  2. Does the actual Sora video API response match these fields?
  3. Should there be a separate struct for the task submission response with just task_id?

Run the following script to check where this struct is used:


🏁 Script executed:

#!/bin/bash
# Search for usage of OpenAIVideoResponse in the codebase
rg -n "OpenAIVideoResponse" --type=go -C3

Length of output: 410


🏁 Script executed:

#!/bin/bash
rg -n 'task_id' -C3 --type=go

Length of output: 27951


Remove unused OpenAIVideoResponse
The struct’s fields reflect file‐upload metadata (id, object, bytes, etc.), not the Sora video task API (which returns only {"task_id": "…"}), and it isn’t referenced anywhere. Use the existing dto.VideoResponse/dto.VideoTaskResponse for video endpoints.

🤖 Prompt for AI Agents
In dto/openai_response.go around lines 236 to 244, remove the unused
OpenAIVideoResponse struct because it models file-upload metadata (id, object,
bytes, created_at, etc.) which doesn't match the Sora video task API and is not
referenced; replace any usages (if present) with the existing dto.VideoResponse
or dto.VideoTaskResponse types and update imports accordingly so no dead types
remain in the package.

Comment thread middleware/distributor.go
Comment on lines +168 to +179
} else if strings.Contains(c.Request.URL.Path, "/v1/videos") {
//curl https://api.openai.com/v1/videos \
// -H "Authorization: Bearer $OPENAI_API_KEY" \
// -F "model=sora-2" \
// -F "prompt=A calico cat playing a piano on stage"
// -F input_reference="@image.jpg"
relayMode := relayconstant.RelayModeUnknown
if c.Request.Method == http.MethodPost {
relayMode = relayconstant.RelayModeVideoSubmit
modelRequest.Model = c.PostForm("model")
}
c.Set("relay_mode", relayMode)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix model extraction for multipart/form-data requests.

On line 177, c.PostForm("model") won't correctly extract the model field from multipart/form-data requests because the multipart form hasn't been parsed yet at this point. The PostForm method only works for URL-encoded forms that are automatically parsed.

For multipart requests, you need to either:

  1. Call c.MultipartForm() first to trigger parsing, then access c.Request.MultipartForm.Value["model"][0]
  2. Use the middleware parsing approach that's already in place for other paths

Apply this diff to fix the issue:

 	} else if strings.Contains(c.Request.URL.Path, "/v1/videos") {
-		//curl https://api.openai.com/v1/videos \
-		//  -H "Authorization: Bearer $OPENAI_API_KEY" \
-		//  -F "model=sora-2" \
-		//  -F "prompt=A calico cat playing a piano on stage"
-		//	-F input_reference="@image.jpg"
 		relayMode := relayconstant.RelayModeUnknown
 		if c.Request.Method == http.MethodPost {
 			relayMode = relayconstant.RelayModeVideoSubmit
-			modelRequest.Model = c.PostForm("model")
+			if strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") {
+				if form, err := c.MultipartForm(); err == nil && form != nil {
+					if models := form.Value["model"]; len(models) > 0 {
+						modelRequest.Model = models[0]
+					}
+				}
+			} else {
+				modelRequest.Model = c.PostForm("model")
+			}
 		}
 		c.Set("relay_mode", relayMode)
🤖 Prompt for AI Agents
In middleware/distributor.go around lines 168 to 179, the code uses
c.PostForm("model") which fails for multipart/form-data; modify the POST
handling so that if the request Content-Type is multipart/form-data you call
c.MultipartForm() (handle the error), then read the model from
c.Request.MultipartForm.Value["model"][0] (with bounds checks) and set
modelRequest.Model to that value; otherwise fall back to c.PostForm("model");
ensure you still set relay_mode and handle missing/empty model safely.

Comment on lines +83 to +141
req.Header.Set("Authorization", "Bearer "+a.apiKey)
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
return nil
}

func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) {
cachedBody, err := common.GetRequestBody(c)
if err != nil {
return nil, errors.Wrap(err, "get_request_body_failed")
}
return bytes.NewReader(cachedBody), nil
}

// DoRequest delegates to common helper.
func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
return channel.DoTaskApiRequest(a, c, info, requestBody)
}

// DoResponse handles upstream response, returns taskID etc.
func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, _ *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
return
}
_ = resp.Body.Close()

// Parse Sora response
var dResp responsePayload
if err := json.Unmarshal(responseBody, &dResp); err != nil {
taskErr = service.TaskErrorWrapper(errors.Wrapf(err, "body: %s", responseBody), "unmarshal_response_body_failed", http.StatusInternalServerError)
return
}

if dResp.ID == "" {
taskErr = service.TaskErrorWrapper(fmt.Errorf("task_id is empty"), "invalid_response", http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, gin.H{"task_id": dResp.ID})
return dResp.ID, responseBody, nil
}

// FetchTask fetch task status
func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) {
taskID, ok := body["task_id"].(string)
if !ok {
return nil, fmt.Errorf("invalid task_id")
}

uri := fmt.Sprintf("%s/v1/videos/%s", baseUrl, taskID)

req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, err
}

req.Header.Set("Authorization", "Bearer "+key)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Add the required OpenAI-Beta header for all Sora video requests.

OpenAI’s /v1/videos endpoints reject calls that don’t include the OpenAI-Beta marker (e.g. video-generation=2024-09-12). The adaptor currently sets only Authorization/Content-Type, so both the create request and the status polling request will fail with upstream 4xx responses. Please add the mandated OpenAI-Beta header in BuildRequestHeader and FetchTask before we ship.

🤖 Prompt for AI Agents
In relay/channel/task/sora/adaptor.go around lines 83-141, the adaptor currently
only sets Authorization/Content-Type headers but must include the required
OpenAI-Beta marker for Sora video endpoints; add req.Header.Set("OpenAI-Beta",
"video-generation=2024-09-12") in BuildRequestHeader where headers are prepared
and also set the same header on the GET request in FetchTask before sending,
ensuring both create and polling requests include that OpenAI-Beta header.

Comment on lines +68 to +106
func validateMultipartTaskRequest(c *gin.Context, info *RelayInfo, action string) (TaskSubmitReq, error) {
var req TaskSubmitReq
if _, err := c.MultipartForm(); err != nil {
return req, err
}

formData := c.Request.PostForm
req = TaskSubmitReq{
Prompt: formData.Get("prompt"),
Model: formData.Get("model"),
Mode: formData.Get("mode"),
Image: formData.Get("image"),
Size: formData.Get("size"),
Metadata: make(map[string]interface{}),
}

if durationStr := formData.Get("seconds"); durationStr != "" {
if duration, err := strconv.Atoi(durationStr); err == nil {
req.Duration = duration
}
}

if images := formData["images"]; len(images) > 0 {
req.Images = images
}

for key, values := range formData {
if len(values) > 0 && !isKnownTaskField(key) {
if intVal, err := strconv.Atoi(values[0]); err == nil {
req.Metadata[key] = intVal
} else if floatVal, err := strconv.ParseFloat(values[0], 64); err == nil {
req.Metadata[key] = floatVal
} else {
req.Metadata[key] = values[0]
}
}
}
return req, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix multipart form data extraction to use the correct API.

Lines 70-74 have a critical bug: after calling c.MultipartForm() to parse the multipart form, the code accesses c.Request.PostForm which is for URL-encoded forms, not multipart forms. This means the form data won't be extracted correctly.

For multipart forms, you must use c.Request.MultipartForm.Value instead of c.Request.PostForm.

Apply this diff to fix the form data extraction:

 func validateMultipartTaskRequest(c *gin.Context, info *RelayInfo, action string) (TaskSubmitReq, error) {
 	var req TaskSubmitReq
-	if _, err := c.MultipartForm(); err != nil {
+	form, err := c.MultipartForm()
+	if err != nil {
 		return req, err
 	}
 
-	formData := c.Request.PostForm
+	formData := form.Value
 	req = TaskSubmitReq{
 		Prompt:   formData.Get("prompt"),
 		Model:    formData.Get("model"),

Additionally, note the inconsistency on lines 84-88: the code looks for a field named "seconds" but stores it in req.Duration. Consider standardizing on one field name (likely "duration" to match the struct field and the isKnownTaskField list on line 143).

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In relay/common/relay_utils.go around lines 68 to 106, after calling
c.MultipartForm() the code incorrectly reads c.Request.PostForm (URL-encoded
form) so multipart fields aren’t extracted; replace usage with
c.Request.MultipartForm.Value to read form values, ensure images still come from
c.Request.MultipartForm.File if needed, and update the "seconds" handling to use
a consistent "duration" field (or map "seconds" into req.Duration) to match the
TaskSubmitReq struct and isKnownTaskField list.

@seefs001
seefs001 merged commit 5f5b942 into QuantumNous:main Oct 9, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Mar 28, 2026
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants