-
Notifications
You must be signed in to change notification settings - Fork 11.1k
feat: 视频生成接口新增 Seedance(火山 ARK)原生请求/响应兼容入口 #5737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ChenYichener
wants to merge
1
commit into
QuantumNous:main
from
ChenYichener:feat/seedance-native-compat
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/relay" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func RelaySeedanceTask(c *gin.Context) { | ||
| c.Set(common.KeySeedanceOfficialAPI, true) | ||
| RelayTask(c) | ||
| } | ||
|
|
||
| func RelaySeedanceTaskFetch(c *gin.Context) { | ||
| c.Set(common.KeySeedanceOfficialAPI, true) | ||
| respBody, taskErr := relay.SeedanceTaskFetch(c) | ||
| if taskErr != nil { | ||
| respondTaskError(c, taskErr) | ||
| return | ||
| } | ||
| c.Data(http.StatusOK, "application/json", respBody) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| relayconstant "github.com/QuantumNous/new-api/relay/constant" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func SeedanceRequestConvert() func(c *gin.Context) { | ||
| return func(c *gin.Context) { | ||
| c.Set(common.KeySeedanceOfficialAPI, true) | ||
|
|
||
| if c.Request.Method == http.MethodPost { | ||
| c.Request.URL.Path = "/v1/video/generations" | ||
| c.Set("relay_mode", relayconstant.RelayModeVideoSubmit) | ||
| } | ||
|
|
||
| c.Next() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| relayconstant "github.com/QuantumNous/new-api/relay/constant" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSeedanceRequestConvertSubmit(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| router := gin.New() | ||
| router.Use(SeedanceRequestConvert()) | ||
| router.POST("/seedance/api/v3/contents/generations/tasks", func(c *gin.Context) { | ||
| require.True(t, c.GetBool(common.KeySeedanceOfficialAPI)) | ||
| require.Equal(t, "/v1/video/generations", c.Request.URL.Path) | ||
| require.Equal(t, relayconstant.RelayModeVideoSubmit, c.GetInt("relay_mode")) | ||
| c.Status(http.StatusNoContent) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/seedance/api/v3/contents/generations/tasks", nil) | ||
| recorder := httptest.NewRecorder() | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusNoContent, recorder.Code) | ||
| } | ||
|
|
||
| func TestSeedanceRequestConvertFetchByID(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| router := gin.New() | ||
| router.Use(SeedanceRequestConvert()) | ||
| router.GET("/seedance/api/v3/contents/generations/tasks/:task_id", func(c *gin.Context) { | ||
| require.True(t, c.GetBool(common.KeySeedanceOfficialAPI)) | ||
| require.Equal(t, "/seedance/api/v3/contents/generations/tasks/task_public", c.Request.URL.Path) | ||
| require.Equal(t, "task_public", c.Param("task_id")) | ||
| c.Status(http.StatusNoContent) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/seedance/api/v3/contents/generations/tasks/task_public", nil) | ||
| recorder := httptest.NewRecorder() | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusNoContent, recorder.Code) | ||
| } | ||
|
|
||
| func TestSeedanceRequestConvertFetchList(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| router := gin.New() | ||
| router.Use(SeedanceRequestConvert()) | ||
| router.GET("/seedance/api/v3/contents/generations/tasks", func(c *gin.Context) { | ||
| require.True(t, c.GetBool(common.KeySeedanceOfficialAPI)) | ||
| require.Equal(t, "/seedance/api/v3/contents/generations/tasks", c.Request.URL.Path) | ||
| c.Status(http.StatusNoContent) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/seedance/api/v3/contents/generations/tasks", nil) | ||
| recorder := httptest.NewRecorder() | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, http.StatusNoContent, recorder.Code) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package doubao | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSeedanceOfficialBuildRequestBodyPreservesNativeContent(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| recorder := httptest.NewRecorder() | ||
| ctx, _ := gin.CreateTestContext(recorder) | ||
| ctx.Set(common.KeySeedanceOfficialAPI, true) | ||
| ctx.Request = httptest.NewRequest(http.MethodPost, "/seedance/api/v3/contents/generations/tasks", bytes.NewBufferString(`{ | ||
| "model":"doubao-seedance-1-5-pro", | ||
| "content":[ | ||
| {"type":"image_url","image_url":{"url":"https://example.com/a.png"},"role":"first_frame"}, | ||
| {"type":"text","text":"make a video"} | ||
| ], | ||
| "duration":5, | ||
| "watermark":false | ||
| }`)) | ||
| ctx.Request.Header.Set("Content-Type", "application/json") | ||
|
|
||
| adaptor := &TaskAdaptor{} | ||
| info := &relaycommon.RelayInfo{ | ||
| ChannelMeta: &relaycommon.ChannelMeta{UpstreamModelName: "upstream-seedance-model"}, | ||
| } | ||
|
|
||
| body, err := adaptor.BuildRequestBody(ctx, info) | ||
| require.NoError(t, err) | ||
|
|
||
| raw, err := io.ReadAll(body) | ||
| require.NoError(t, err) | ||
|
|
||
| var payload map[string]any | ||
| require.NoError(t, common.Unmarshal(raw, &payload)) | ||
| assert.Equal(t, "upstream-seedance-model", payload["model"]) | ||
| assert.Equal(t, float64(5), payload["duration"]) | ||
| assert.Equal(t, false, payload["watermark"]) | ||
|
|
||
| content, ok := payload["content"].([]any) | ||
| require.True(t, ok) | ||
| require.Len(t, content, 2) | ||
| first, ok := content[0].(map[string]any) | ||
| require.True(t, ok) | ||
| assert.Equal(t, "image_url", first["type"]) | ||
| assert.Equal(t, "first_frame", first["role"]) | ||
| } | ||
|
|
||
| func TestSeedanceOfficialDoResponseReturnsUpstreamTaskID(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| recorder := httptest.NewRecorder() | ||
| ctx, _ := gin.CreateTestContext(recorder) | ||
| ctx.Set(common.KeySeedanceOfficialAPI, true) | ||
|
|
||
| adaptor := &TaskAdaptor{} | ||
| resp := &http.Response{ | ||
| Body: io.NopCloser(bytes.NewBufferString(`{"id":"cgt-upstream"}`)), | ||
| } | ||
| info := &relaycommon.RelayInfo{ | ||
| TaskRelayInfo: &relaycommon.TaskRelayInfo{PublicTaskID: "task_public"}, | ||
| } | ||
|
|
||
| taskID, taskData, taskErr := adaptor.DoResponse(ctx, resp, info) | ||
| require.Nil(t, taskErr) | ||
| assert.Equal(t, "cgt-upstream", taskID) | ||
| assert.JSONEq(t, `{"id":"cgt-upstream"}`, string(taskData)) | ||
| assert.JSONEq(t, `{"id":"cgt-upstream"}`, recorder.Body.String()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package relay | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/QuantumNous/new-api/model" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSeedanceTaskIDFilters(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| recorder := httptest.NewRecorder() | ||
| ctx, _ := gin.CreateTestContext(recorder) | ||
| ctx.Request = httptest.NewRequest( | ||
| http.MethodGet, | ||
| "/seedance/api/v3/contents/generations/tasks?filter.task_ids=cgt-a,cgt-b&filter.task_ids=task_c&filter.task_ids[]=cgt-d", | ||
| nil, | ||
| ) | ||
|
|
||
| require.Equal(t, []string{"cgt-a", "cgt-b", "task_c", "cgt-d"}, seedanceTaskIDFilters(ctx)) | ||
| } | ||
|
|
||
| func TestSeedanceTaskResponseUsesUpstreamShape(t *testing.T) { | ||
| task := &model.Task{ | ||
| TaskID: "task_public", | ||
| Status: model.TaskStatusSuccess, | ||
| SubmitTime: 1710000000, | ||
| UpdatedAt: 1710000100, | ||
| Properties: model.Properties{ | ||
| OriginModelName: "doubao-seedance-1-5-pro", | ||
| }, | ||
| PrivateData: model.TaskPrivateData{ | ||
| UpstreamTaskID: "cgt-upstream", | ||
| ResultURL: "https://example.com/video.mp4", | ||
| }, | ||
| Data: json.RawMessage(`{"id":"cgt-upstream","status":"running","content":{},"service_tier":"default"}`), | ||
| } | ||
|
|
||
| resp := seedanceTaskResponse(task) | ||
| assert.Equal(t, "cgt-upstream", resp["id"]) | ||
| assert.Equal(t, "doubao-seedance-1-5-pro", resp["model"]) | ||
| assert.Equal(t, "succeeded", resp["status"]) | ||
| assert.Equal(t, int64(1710000000), resp["created_at"]) | ||
| assert.Equal(t, int64(1710000100), resp["updated_at"]) | ||
|
|
||
| content, ok := resp["content"].(map[string]any) | ||
| require.True(t, ok) | ||
| assert.Equal(t, "https://example.com/video.mp4", content["video_url"]) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 7991
🏁 Script executed:
sed -n '119,145p' relay/channel/task/doubao/adaptor.goRepository: QuantumNous/new-api
Length of output: 1116
🏁 Script executed:
sed -n '204,230p' relay/channel/task/doubao/adaptor.goRepository: QuantumNous/new-api
Length of output: 799
Ensure
info.UpstreamModelNameis populated for Seedance billing attribution.In the Seedance branch (
BuildRequestBody),info.UpstreamModelNameis only used to override the request model if it has already been set (e.g., by model mapping). However, if no mapping occurred,info.UpstreamModelNameremains empty despite the model being present in the request body.While the code correctly omits the override in this case, relying solely on implicit behavior risks missing the upstream model name for billing and usage tracking if it hasn't been populated elsewhere. It is safer to explicitly extract and assign the model from the body to
infoif it is currently unset.Consider adding the following logic before modifying
bodyMap:🤖 Prompt for AI Agents