diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 58fba5cb3..7064fbe17 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -649,9 +649,13 @@ key's video id ends with the key's fingerprint, as a plan key's does, and its poll and download go on that key (`videoAsker` checks the calling key may use it). `volcengineVideoBody` turns OpenAI's request into Ark's: the prompt as a `text` item in `content`, `input_reference` as an `image_url` -item with role `first_frame`, `reference_images` as `reference_image`, -`seconds` as `duration`, and `size` (WIDTHxHEIGHT) as `ratio` and -`resolution`. Ark's `succeeded`/`running`/`failed`/`expired` and its +item with role `first_frame`, `last_frame` as `last_frame`, +`reference_images` as `reference_image`, `seconds` as `duration`, and +`size` (WIDTHxHEIGHT) as `ratio` and `resolution`. The request's fields +OpenAI's API doesn't have (Ark's `generate_audio`, `seed`, `camera_fixed`, +`watermark`, `ratio: "adaptive"`…) go to Ark as sent, masked as the prompt +is; a multipart form's `"false"` or `"42"` goes as `false` or `42` +(`TestArkVideoTakesLastFrameAndItsOwnFields`). Ark's `succeeded`/`running`/`failed`/`expired` and its `content.video_url` are read back into OpenAI's video object (`videoObject`), and `/content` fetches that URL (`vendorClient`). `TestArkKeyMakesSeedanceVideos`. diff --git a/internal/gateway/video.go b/internal/gateway/video.go index 7b7aa4341..7769acc00 100644 --- a/internal/gateway/video.go +++ b/internal/gateway/video.go @@ -483,17 +483,42 @@ func pixelResolution(size string) string { } // volcengineVideoBody is f as Ark's content-generation API takes it: text -// and images in content, with image roles distinguishing the first frame from -// subject references, plus duration, ratio and resolution as separate fields. +// and images in content, each image's role saying what it is (the first +// frame, the last frame, a subject to draw from), duration, ratio and +// resolution as fields of their own, and the request's other fields, which +// OpenAI's API doesn't have, as the caller sent them: Ark's own +// (generate_audio, seed, camera_fixed, watermark…), which a caller of +// OpenAI's shape can only send that way. func volcengineVideoBody(model string, f filming) []byte { content := []map[string]any{{"type": "text", "text": f.Prompt}} + frame := func(pic *picture, role string) { + content = append(content, map[string]any{"type": "image_url", "image_url": map[string]string{"url": pic.dataURL()}, "role": role}) + } if f.Start != nil { - content = append(content, map[string]any{"type": "image_url", "image_url": map[string]string{"url": f.Start.dataURL()}, "role": "first_frame"}) + frame(f.Start, "first_frame") } - for _, pic := range f.References { - content = append(content, map[string]any{"type": "image_url", "image_url": map[string]string{"url": pic.dataURL()}, "role": "reference_image"}) + if f.End != nil { + frame(f.End, "last_frame") } - req := map[string]any{"model": model, "content": content} + for i := range f.References { + frame(&f.References[i], "reference_image") + } + req := map[string]any{} + for k, v := range f.Extra { + // a multipart form's fields are text: "false" and "42" are what + // Ark takes as false and 42 + if str, ok := v.(string); ok { + var typed any + if json.Unmarshal([]byte(str), &typed) == nil { + switch typed.(type) { + case bool, float64: + v = typed + } + } + } + req[k] = v + } + req["model"], req["content"] = model, content if f.Seconds != "" { req["duration"], _ = strconv.Atoi(f.Seconds) } diff --git a/internal/gateway/video_ark_key_test.go b/internal/gateway/video_ark_key_test.go index b0eab1e9a..a15ce8557 100644 --- a/internal/gateway/video_ark_key_test.go +++ b/internal/gateway/video_ark_key_test.go @@ -1,8 +1,10 @@ package gateway import ( + "bytes" "encoding/json" "io" + "mime/multipart" "net/http" "net/http/httptest" "slices" @@ -133,3 +135,50 @@ func TestArkKeyMakesSeedanceVideos(t *testing.T) { t.Fatalf("sent %s", a.bodies[0]) } } + +// OpenAI's videos request, as its SDK sends it (a multipart form, the +// first frame a file in input_reference), reaches Seedance as Ark's: the +// last frame with role last_frame, and the fields OpenAI's API doesn't +// have (Ark's generate_audio, seed) passed on, typed as Ark takes them. +func TestArkVideoTakesLastFrameAndItsOwnFields(t *testing.T) { + fresh(t) + a := newArkKeyVideos(t) + was := isArk + isArk = func(p provider.Provider) bool { return p.ID == "ark" } + t.Cleanup(func() { isArk = was }) + if err := provider.Save(provider.Provider{ID: "ark", Name: "Volcengine Ark", Chat: a.URL + "/api/v3", Key: "key", + Models: []string{"doubao-seedance-1-0-pro-250528"}}); err != nil { + t.Fatal(err) + } + var buf bytes.Buffer + mw := multipart.NewWriter(&buf) + mw.WriteField("model", "ark/doubao-seedance-1-0-pro-250528") + mw.WriteField("prompt", "花开") + mw.WriteField("seconds", "5") + mw.WriteField("generate_audio", "false") + mw.WriteField("seed", "42") + mw.WriteField("ratio", "adaptive") + for _, k := range []string{"input_reference", "last_frame"} { + part, _ := mw.CreateFormFile(k, k+".png") + part.Write(pngBytes) + } + mw.Close() + code, _, raw, _ := request(t, New(), http.MethodPost, "/v1/videos", mw.FormDataContentType(), buf.String()) + if code != 200 { + t.Fatalf("create: %d %s", code, raw) + } + a.mu.Lock() + defer a.mu.Unlock() + var sent map[string]any + if err := json.Unmarshal([]byte(a.bodies[0]), &sent); err != nil { + t.Fatal(err) + } + content, _ := sent["content"].([]any) + var roles []string + for _, c := range content[1:] { + roles = append(roles, c.(map[string]any)["role"].(string)) + } + if !slices.Equal(roles, []string{"first_frame", "last_frame"}) || sent["generate_audio"] != false || sent["seed"] != float64(42) || sent["ratio"] != "adaptive" || sent["duration"] != float64(5) { + t.Fatalf("sent %s", a.bodies[0]) + } +}