Merge pull request #8 from CyrusAuyeung/agent/add-sub2api-grok-video
fix(media): Grok 视频 - 支持 xAI 与 Sub2API 生成协议
This commit is contained in:
13 files changed
+129
-37
No files matched your search
@@ -93,7 +93,7 @@ func interfaceAllowsProxyPath(interfaceType model.ChannelInterfaceType, requestP
|
||||
return requestPath == "/responses"
|
||||
case model.ChannelInterfaceOpenAIImage:
|
||||
return requestPath == "/images/generations" || requestPath == "/images/edits"
|
||||
case model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2:
|
||||
case model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2, model.ChannelInterfaceXAIVideo:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
|
||||
@@ -48,10 +48,12 @@ func TestAuthorizeSystemProxyRestrictsConfiguredInterfaceType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeSystemProxyBlocksNewAPIChannel2DirectBrowserProxy(t *testing.T) {
|
||||
func TestAuthorizeSystemProxyBlocksBackendOnlyVideoInterfaces(t *testing.T) {
|
||||
body := []byte(`{"model":"grok-image-video"}`)
|
||||
channel := &model.ModelChannel{APIFormat: "openai", InterfaceType: model.ChannelInterfaceNewAPIChannel2, ModelsJSON: `["grok-image-video"]`}
|
||||
if err := authorizeSystemProxy(channel, http.MethodPost, "/video/generations", "application/json", body); err == nil {
|
||||
t.Fatal("authorizeSystemProxy() error = nil for backend-only video interface")
|
||||
for _, interfaceType := range []model.ChannelInterfaceType{model.ChannelInterfaceNewAPIChannel2, model.ChannelInterfaceXAIVideo} {
|
||||
channel := &model.ModelChannel{APIFormat: "openai", InterfaceType: interfaceType, ModelsJSON: `["grok-image-video"]`}
|
||||
if err := authorizeSystemProxy(channel, http.MethodPost, "/video/generations", "application/json", body); err == nil {
|
||||
t.Fatalf("authorizeSystemProxy() error = nil for backend-only interface %q", interfaceType)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,7 @@ const (
|
||||
ChannelInterfaceNewAPIVideo ChannelInterfaceType = "newapi"
|
||||
ChannelInterfaceNewAPIChannel1 ChannelInterfaceType = "newapi-channel-1"
|
||||
ChannelInterfaceNewAPIChannel2 ChannelInterfaceType = "newapi-channel-2"
|
||||
ChannelInterfaceXAIVideo ChannelInterfaceType = "xai-video"
|
||||
|
||||
ApiCallStatusSucceeded ApiCallStatus = "succeeded"
|
||||
ApiCallStatusFailed ApiCallStatus = "failed"
|
||||
|
||||
@@ -558,7 +558,7 @@ func mergeChannelRequest(req ChannelRequest, channel model.ModelChannel) Channel
|
||||
|
||||
func validChannelInterfaceType(value model.ChannelInterfaceType) bool {
|
||||
switch value {
|
||||
case model.ChannelInterfaceChatCompletion, model.ChannelInterfaceOpenAIResponse, model.ChannelInterfaceOpenAIImage, model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2:
|
||||
case model.ChannelInterfaceChatCompletion, model.ChannelInterfaceOpenAIResponse, model.ChannelInterfaceOpenAIImage, model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2, model.ChannelInterfaceXAIVideo:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
@@ -31,6 +31,26 @@ func TestChannelFromRequestStoresInterfaceType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelFromRequestStoresXAIVideoInterfaceType(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer server.Close()
|
||||
|
||||
channel, err := channelFromRequest(ChannelRequest{
|
||||
Name: "xAI Video",
|
||||
BaseURL: server.URL + "/v1",
|
||||
APIKey: "secret",
|
||||
InterfaceType: "xai-video",
|
||||
Models: []string{"grok-imagine-video-1.5"},
|
||||
}, model.ModelChannel{})
|
||||
if err != nil {
|
||||
t.Fatalf("channelFromRequest() error = %v", err)
|
||||
}
|
||||
if channel.InterfaceType != model.ChannelInterfaceXAIVideo {
|
||||
t.Fatalf("InterfaceType = %q", channel.InterfaceType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeChannelRequestSupportsEnabledOnlyPatch(t *testing.T) {
|
||||
enabled := false
|
||||
req := mergeChannelRequest(ChannelRequest{Enabled: &enabled}, model.ModelChannel{
|
||||
|
||||
@@ -200,7 +200,7 @@ func capabilityForChannel(channel model.ModelChannel) string {
|
||||
switch channel.InterfaceType {
|
||||
case model.ChannelInterfaceOpenAIImage:
|
||||
return "image"
|
||||
case model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2:
|
||||
case model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2, model.ChannelInterfaceXAIVideo:
|
||||
return "video"
|
||||
default:
|
||||
return "text"
|
||||
|
||||
@@ -574,12 +574,16 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
}
|
||||
id := resumedProviderRequestID(ctx)
|
||||
var created map[string]interface{}
|
||||
if id == "" && isGrokVideoConfig(input.Config) {
|
||||
if id == "" && (input.Config.InterfaceType == "xai-video" || isGrokVideoConfig(input.Config)) {
|
||||
requestBody, err := grokVideoBody(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := postJSON(ctx, input.Config, "/videos", requestBody, &created); err != nil {
|
||||
createPath := "/videos"
|
||||
if input.Config.InterfaceType == "xai-video" {
|
||||
createPath = "/videos/generations"
|
||||
}
|
||||
if err := postJSON(ctx, input.Config, createPath, requestBody, &created); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if id == "" {
|
||||
@@ -608,17 +612,11 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
}
|
||||
}
|
||||
if id == "" {
|
||||
id = stringField(created, "id")
|
||||
if id == "" {
|
||||
id = stringField(created, "task_id")
|
||||
}
|
||||
id = firstNonEmptyString(stringField(created, "id"), stringField(created, "request_id"), stringField(created, "task_id"))
|
||||
}
|
||||
if id == "" {
|
||||
if data, ok := created["data"].(map[string]interface{}); ok {
|
||||
id = stringField(data, "id")
|
||||
if id == "" {
|
||||
id = stringField(data, "task_id")
|
||||
}
|
||||
id = firstNonEmptyString(stringField(data, "id"), stringField(data, "request_id"), stringField(data, "task_id"))
|
||||
}
|
||||
}
|
||||
if id == "" {
|
||||
@@ -633,7 +631,7 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
state = data
|
||||
}
|
||||
status := strings.ToLower(stringField(state, "status"))
|
||||
if status == "completed" || status == "succeeded" || status == "success" {
|
||||
if status == "completed" || status == "succeeded" || status == "success" || status == "done" {
|
||||
if videoURL := newAPIVideoResultURL(state); videoURL != "" {
|
||||
data, mimeType, err := getExternalBinary(withProviderRequestKind(ctx, "download"), videoURL)
|
||||
if err != nil {
|
||||
@@ -664,7 +662,7 @@ func newAPIVideoResultURL(state map[string]interface{}) string {
|
||||
|
||||
func nestedNewAPIVideoResultURL(payload map[string]interface{}, allowResultURL bool, depth int) string {
|
||||
if depth < 2 {
|
||||
for _, key := range []string{"data", "result"} {
|
||||
for _, key := range []string{"data", "result", "video"} {
|
||||
if nested, ok := payload[key].(map[string]interface{}); ok {
|
||||
if videoURL := nestedNewAPIVideoResultURL(nested, true, depth+1); videoURL != "" {
|
||||
return videoURL
|
||||
@@ -986,7 +984,7 @@ func validateGenerationInterface(mode string, interfaceType string) error {
|
||||
allowed := map[string]map[string]bool{
|
||||
"text": {"chat-completion": true, "openai-response": true},
|
||||
"image": {"openai-image": true},
|
||||
"video": {"newapi": true, "newapi-channel-1": true, "newapi-channel-2": true},
|
||||
"video": {"newapi": true, "newapi-channel-1": true, "newapi-channel-2": true, "xai-video": true},
|
||||
}
|
||||
if allowed[mode] != nil && !allowed[mode][interfaceType] {
|
||||
return fmt.Errorf("接口类型 %s 不支持%s生成", interfaceType, mode)
|
||||
|
||||
@@ -369,6 +369,64 @@ func TestRunVideoTaskUsesJSONForGrokVideo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskUsesXAIVideoGenerationEndpoint(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
paths := make([]string, 0, 3)
|
||||
var server *httptest.Server
|
||||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.Method+" "+r.URL.Path)
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "POST /v1/videos/generations":
|
||||
if contentType := r.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "application/json") {
|
||||
t.Errorf("Content-Type = %q, want application/json", contentType)
|
||||
}
|
||||
var body map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode request: %v", err)
|
||||
}
|
||||
if body["model"] != "grok-imagine-video-1.5" || body["prompt"] != "make it move" {
|
||||
t.Errorf("request body = %#v", body)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"request_id":"video-1"}`))
|
||||
case "GET /v1/videos/video-1":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"status":"done","video":{"url":"` + server.URL + `/files/video.mp4"}}`))
|
||||
case "GET /files/video.mp4":
|
||||
if authorization := r.Header.Get("Authorization"); authorization != "" {
|
||||
t.Errorf("file Authorization = %q, want empty", authorization)
|
||||
}
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{
|
||||
BaseURL: server.URL + "/v1",
|
||||
APIKey: "test-key",
|
||||
Model: "grok-imagine-video-1.5",
|
||||
InterfaceType: "xai-video",
|
||||
VideoSeconds: "10",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("runVideoTask() error = %v", err)
|
||||
}
|
||||
video, ok := result["video"].(map[string]interface{})
|
||||
if !ok || video["dataUrl"] != "data:video/mp4;base64,dmlkZW8=" {
|
||||
t.Fatalf("video = %#v", result["video"])
|
||||
}
|
||||
want := "POST /v1/videos/generations,GET /v1/videos/video-1,GET /files/video.mp4"
|
||||
if got := strings.Join(paths, ","); got != want {
|
||||
t.Fatalf("paths = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIVideoPromptKeepsTextOnlyPromptUnchanged(t *testing.T) {
|
||||
input := canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
@@ -622,6 +680,9 @@ func TestValidateGenerationInterfaceRejectsMismatchedType(t *testing.T) {
|
||||
if err := validateGenerationInterface("video", "newapi-channel-2"); err != nil {
|
||||
t.Fatalf("validateGenerationInterface() error = %v", err)
|
||||
}
|
||||
if err := validateGenerationInterface("video", "xai-video"); err != nil {
|
||||
t.Fatalf("validateGenerationInterface() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessTaskValidatesInterfaceBeforeHydratingMedia(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user