fix(video): PR #492 - 合并统一轮询并保留本地出站安全策略
This commit is contained in:
commit
b1fa44ae6f
17 files changed
+1219
-227
No files matched your search
@@ -662,10 +662,18 @@ func (s *Service) LogAPICall(log model.ApiCallLog) error {
|
||||
var nextPollAt *time.Time
|
||||
if stage == "create" && log.Status == model.ApiCallStatusSucceeded && log.ProviderRequestID != "" {
|
||||
stage = "accepted"
|
||||
next := time.Now().Add(2 * time.Second)
|
||||
delay := 2 * time.Second
|
||||
if log.Capability == "video" {
|
||||
delay = defaultVideoPollInterval
|
||||
}
|
||||
next := time.Now().Add(delay)
|
||||
nextPollAt = &next
|
||||
} else if stage == "poll" {
|
||||
next := time.Now().Add(5 * time.Second)
|
||||
delay := 5 * time.Second
|
||||
if log.Capability == "video" {
|
||||
delay = defaultVideoPollInterval
|
||||
}
|
||||
next := time.Now().Add(delay)
|
||||
nextPollAt = &next
|
||||
}
|
||||
if err := s.repo.UpdateTaskProviderState(log.TaskID, log.ProviderRequestID, stage, nextPollAt); err != nil {
|
||||
|
||||
@@ -337,7 +337,11 @@ func TestDeclarativeProtocolRuntimeExecutesCreatePollAndDownload(t *testing.T) {
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "test-model", APIFormat: "openai", InterfaceType: "test-declarative-video-runtime"}
|
||||
ctx := context.Background()
|
||||
ctx = withProtocolRegistry(ctx, center.registrySnapshot())
|
||||
result, err := runDeclarativeProtocolTask(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config})
|
||||
adapter, ok := declarativeProtocolAdapterForContext(ctx, config.InterfaceType)
|
||||
if !ok {
|
||||
t.Fatal("declarative adapter is unavailable")
|
||||
}
|
||||
result, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -346,6 +350,113 @@ func TestDeclarativeProtocolRuntimeExecutesCreatePollAndDownload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolPollingPolicyOnlyChangesVideoBehavior(t *testing.T) {
|
||||
video := declarativeProtocolPollPolicy("video")
|
||||
if video.InitialDelay != defaultVideoPollInterval || !video.RetryTransient {
|
||||
t.Fatalf("video polling policy = %#v, want delayed resilient polling", video)
|
||||
}
|
||||
|
||||
image := declarativeProtocolPollPolicy("image")
|
||||
if image.InitialDelay != 0 || image.Interval != 2500*time.Millisecond || image.RetryTransient {
|
||||
t.Fatalf("image polling policy = %#v, want legacy immediate fail-fast polling", image)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolPollRecoversFromTransientGatewayFailure(t *testing.T) {
|
||||
allowLoopbackProviderTest(t)
|
||||
adapter, err := protocol.LoadManifest([]byte(`{
|
||||
"apiVersion":"yingce.plugin/v1",
|
||||
"id":"test-declarative-video-retry","version":"1.0.0","name":"Test Declarative Video Retry","author":"Test","documentation":"# Test",
|
||||
"contributes":{"providers":[{"id":"test-declarative-video-retry","label":"Test Declarative Video Retry","capabilities":["video"],"scopes":["canvas"],"create":{"method":"POST","path":"/tasks","fields":{"model":"request.model"}},"poll":{"method":"GET","path":"/tasks/{{taskId}}"},"response":{"taskIdPaths":["id"],"statusPaths":["status"],"resultPaths":["video_url"],"resultKind":"video"}}]}
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
createCalls := 0
|
||||
pollCalls := 0
|
||||
var server *httptest.Server
|
||||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/tasks":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/tasks/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if pollCalls == 1 {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
_, _ = w.Write([]byte(`{"message":"temporary gateway failure"}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"succeeded","video_url":"` + server.URL + `/media.mp4"}`))
|
||||
case "/media.mp4":
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "test-declarative-video-retry"}
|
||||
ctx := context.Background()
|
||||
result, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || createCalls != 1 || pollCalls != 2 {
|
||||
t.Fatalf("result = %#v, create calls = %d, poll calls = %d", result, createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolRetriesResultDownloadWithoutRepolling(t *testing.T) {
|
||||
allowLoopbackProviderTest(t)
|
||||
adapter, err := protocol.LoadManifest([]byte(`{
|
||||
"apiVersion":"yingce.plugin/v1",
|
||||
"id":"test-declarative-download-retry","version":"1.0.0","name":"Test Declarative Download Retry","author":"Test","documentation":"# Test",
|
||||
"contributes":{"providers":[{"id":"test-declarative-download-retry","label":"Test Declarative Download Retry","capabilities":["video"],"scopes":["canvas"],"create":{"method":"POST","path":"/tasks","fields":{"model":"request.model"}},"poll":{"method":"GET","path":"/tasks/{{taskId}}"},"response":{"taskIdPaths":["id"],"statusPaths":["status"],"resultPaths":["video_url"],"resultKind":"video"}}]}
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pollCalls := 0
|
||||
downloadCalls := 0
|
||||
var server *httptest.Server
|
||||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/tasks":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/tasks/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"succeeded","video_url":"` + server.URL + `/media.mp4"}`))
|
||||
case "/media.mp4":
|
||||
downloadCalls++
|
||||
if downloadCalls == 1 {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "test-declarative-download-retry"}
|
||||
ctx := context.Background()
|
||||
result, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || pollCalls != 1 || downloadCalls != 2 {
|
||||
t.Fatalf("result = %#v, poll calls = %d, download calls = %d", result, pollCalls, downloadCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolRuntimeGeneratesPerCreateIdempotencyKey(t *testing.T) {
|
||||
allowLoopbackProviderTest(t)
|
||||
manifest := []byte(`{
|
||||
@@ -395,8 +506,12 @@ func TestDeclarativeProtocolRuntimeGeneratesPerCreateIdempotencyKey(t *testing.T
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "test-model", APIFormat: "openai", InterfaceType: "test-idempotency-key-runtime"}
|
||||
ctx := context.Background()
|
||||
ctx = withProtocolRegistry(ctx, center.registrySnapshot())
|
||||
adapter, ok := declarativeProtocolAdapterForContext(ctx, config.InterfaceType)
|
||||
if !ok {
|
||||
t.Fatal("declarative adapter is unavailable")
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
result, err := runDeclarativeProtocolTask(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config})
|
||||
result, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -441,12 +556,7 @@ func TestDeclarativeNewAPIChannel2TaskNotExistRetry(t *testing.T) {
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "newapi-channel-2"}
|
||||
ctx := context.Background()
|
||||
result, err := runProtocolAdapterTaskWithTiming(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, protocolPollTiming{
|
||||
InitialDelay: time.Millisecond,
|
||||
PollInterval: time.Millisecond,
|
||||
TaskNotExistWindow: time.Second,
|
||||
TaskNotExistRetryDelays: []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond},
|
||||
})
|
||||
result, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -479,43 +589,34 @@ func TestDeclarativeNewAPIChannel2TaskNotExistExhaustion(t *testing.T) {
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "newapi-channel-2"}
|
||||
ctx := context.Background()
|
||||
_, err := runProtocolAdapterTaskWithTiming(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, protocolPollTiming{
|
||||
InitialDelay: time.Millisecond,
|
||||
PollInterval: time.Millisecond,
|
||||
TaskNotExistWindow: time.Second,
|
||||
TaskNotExistRetryDelays: []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond},
|
||||
})
|
||||
var pendingErr providerStatePendingError
|
||||
_, err := runProtocolAdapterTaskWithPolicy(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, fastVideoPollPolicy())
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &pendingErr) || pendingErr.TaskID != "provider-task-1" || !errors.As(err, &httpErr) {
|
||||
t.Fatalf("error = %#v, want typed provider pending error wrapping HTTP error", err)
|
||||
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("error = %#v, want provider HTTP error", err)
|
||||
}
|
||||
if createCalls != 1 || pollCalls != 5 {
|
||||
t.Fatalf("create calls = %d, poll calls = %d, want 1 and 5", createCalls, pollCalls)
|
||||
if createCalls != 1 || pollCalls != 3 {
|
||||
t.Fatalf("create calls = %d, poll calls = %d, want 1 and 3", createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistStrictClassification(t *testing.T) {
|
||||
func TestProviderTaskNotReadyStrictClassification(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
interfaceType string
|
||||
taskID string
|
||||
err error
|
||||
want bool
|
||||
name string
|
||||
err providerHTTPError
|
||||
want bool
|
||||
}{
|
||||
{name: "error code", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}, want: true},
|
||||
{name: "error message", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"message":"task_not_exist"}`}, want: true},
|
||||
{name: "other interface", interfaceType: "newapi-channel-1", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "missing task id", interfaceType: "newapi-channel-2", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "other status", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 404, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "unstructured body", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: "task_not_exist"}},
|
||||
{name: "partial match", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist_later"}`}},
|
||||
{name: "other provider error", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"invalid_parameter"}`}},
|
||||
{name: "error code", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}, want: true},
|
||||
{name: "error message", err: providerHTTPError{StatusCode: 400, Body: `{"message":"task_not_exist"}`}, want: true},
|
||||
{name: "not found spelling", err: providerHTTPError{StatusCode: 404, Body: `{"code":"task_not_found"}`}, want: true},
|
||||
{name: "other status", err: providerHTTPError{StatusCode: 502, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "unstructured body", err: providerHTTPError{StatusCode: 400, Body: "task_not_exist"}},
|
||||
{name: "partial match", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist_later"}`}},
|
||||
{name: "other provider error", err: providerHTTPError{StatusCode: 400, Body: `{"code":"invalid_parameter"}`}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := isNewAPIChannel2TaskNotReady(test.interfaceType, test.taskID, test.err); got != test.want {
|
||||
t.Fatalf("isNewAPIChannel2TaskNotReady() = %v, want %v", got, test.want)
|
||||
if got := isProviderTaskNotReadyError(test.err); got != test.want {
|
||||
t.Fatalf("isProviderTaskNotReadyError() = %v, want %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ type providerConfig struct {
|
||||
}
|
||||
|
||||
const providerHTTPTimeout = 5 * time.Minute
|
||||
const videoPollTimeout = 30 * time.Minute
|
||||
const videoPollTimeout = time.Hour
|
||||
const maxProviderResponseBytes int64 = 64 << 20
|
||||
|
||||
type providerMedia struct {
|
||||
@@ -143,6 +143,19 @@ type providerHTTPError struct {
|
||||
RetryAfter time.Duration
|
||||
}
|
||||
|
||||
type providerResponseDecodeError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e providerResponseDecodeError) Error() string { return e.Err.Error() }
|
||||
func (e providerResponseDecodeError) Unwrap() error { return e.Err }
|
||||
|
||||
type providerCircuitOpenError struct{}
|
||||
|
||||
func (providerCircuitOpenError) Error() string {
|
||||
return "当前渠道连续失败,已暂时熔断,请稍后重试"
|
||||
}
|
||||
|
||||
type providerStatePendingError struct {
|
||||
TaskID string
|
||||
Cause error
|
||||
|
||||
@@ -188,10 +188,10 @@ func doJSON(req *http.Request, target interface{}) error {
|
||||
return err
|
||||
}
|
||||
if !strings.Contains(mimeType, "json") && !json.Valid(data) {
|
||||
return fmt.Errorf("接口返回非 JSON 内容:%s", mimeType)
|
||||
return providerResponseDecodeError{Err: fmt.Errorf("接口返回非 JSON 内容:%s", mimeType)}
|
||||
}
|
||||
if err := json.Unmarshal(data, target); err != nil {
|
||||
return err
|
||||
return providerResponseDecodeError{Err: err}
|
||||
}
|
||||
if payload, ok := target.(*imageResponse); ok {
|
||||
if payload.Error != nil && payload.Error.Message != "" {
|
||||
@@ -247,7 +247,7 @@ func doBinaryWithConsumer(req *http.Request, onChunk func(string, []byte)) ([]by
|
||||
return nil, "", fmt.Errorf("读取渠道熔断状态失败:%w", err)
|
||||
}
|
||||
if open {
|
||||
return nil, "", errors.New("当前渠道连续失败,已暂时熔断,请稍后重试")
|
||||
return nil, "", providerCircuitOpenError{}
|
||||
}
|
||||
slotID := channelID
|
||||
if slotID == "" {
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/protocol"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -40,34 +39,28 @@ func runDeclarativeProtocolTask(ctx context.Context, input canvasGenerationInput
|
||||
return runProtocolAdapterTask(ctx, input, adapter)
|
||||
}
|
||||
|
||||
type protocolPollTiming struct {
|
||||
InitialDelay time.Duration
|
||||
PollInterval time.Duration
|
||||
TaskNotExistWindow time.Duration
|
||||
TaskNotExistMaxMisses int
|
||||
TaskNotExistRetryDelays []time.Duration
|
||||
}
|
||||
|
||||
var defaultProtocolPollTiming = protocolPollTiming{
|
||||
InitialDelay: 2 * time.Second,
|
||||
PollInterval: 2500 * time.Millisecond,
|
||||
TaskNotExistWindow: 15 * time.Second,
|
||||
TaskNotExistMaxMisses: 5,
|
||||
TaskNotExistRetryDelays: []time.Duration{2 * time.Second, 3 * time.Second, 5 * time.Second, 5 * time.Second},
|
||||
}
|
||||
|
||||
func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter) (map[string]interface{}, error) {
|
||||
return runProtocolAdapterTaskWithTiming(ctx, input, adapter, defaultProtocolPollTiming)
|
||||
return runProtocolAdapterTaskWithPolicy(ctx, input, adapter, declarativeProtocolPollPolicy(input.Mode))
|
||||
}
|
||||
|
||||
func runProtocolAdapterTaskWithTiming(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, timing protocolPollTiming) (map[string]interface{}, error) {
|
||||
func declarativeProtocolPollPolicy(mode string) videoPollPolicy {
|
||||
if mode == "video" {
|
||||
return defaultVideoPollPolicy()
|
||||
}
|
||||
return videoPollPolicy{
|
||||
Interval: 2500 * time.Millisecond,
|
||||
MaxNotFoundMisses: 1,
|
||||
MaxDownloadTries: 3,
|
||||
Sleep: sleepContext,
|
||||
}
|
||||
}
|
||||
|
||||
func runProtocolAdapterTaskWithPolicy(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, policy videoPollPolicy) (map[string]interface{}, error) {
|
||||
// create、poll、download 是同一个外部任务的三个阶段:任一阶段失败都向上返回真实错误,
|
||||
// 不把“已提交但结果未知”伪装成成功,也不把下载失败降级成空结果。
|
||||
request := protocolRequestFromInput(input)
|
||||
taskID := resumedProviderRequestID(ctx)
|
||||
var created protocol.CreateResult
|
||||
createdProviderTask := false
|
||||
syncWindowStartedAt := time.Now()
|
||||
if taskID == "" {
|
||||
// 幂等键只存在于宿主请求元数据中,声明式插件可以把它映射到 Header,
|
||||
// 但不能把宿主控制字段泄漏到供应商 JSON body。恢复已有 taskID 时不会进入 create 分支。
|
||||
@@ -100,71 +93,38 @@ func runProtocolAdapterTaskWithTiming(ctx context.Context, input canvasGeneratio
|
||||
return nil, protocolResultError(created.Message, taskID)
|
||||
}
|
||||
if created.Status == protocol.StatusSucceeded {
|
||||
return finishProtocolAdapterResult(ctx, input, adapter, request, taskID, created.Result)
|
||||
return finishProtocolAdapterResult(ctx, input, adapter, request, taskID, created.Result, policy)
|
||||
}
|
||||
if taskID == "" {
|
||||
return nil, errors.New("声明式协议创建请求没有返回任务 ID")
|
||||
}
|
||||
createdProviderTask = true
|
||||
syncWindowStartedAt = time.Now()
|
||||
}
|
||||
if createdProviderTask && input.Config.InterfaceType == string(model.ChannelInterfaceNewAPIChannel2) {
|
||||
if err := sleepContext(ctx, timing.InitialDelay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
taskNotExistMisses := 0
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
return runVideoPollLoop(ctx, taskID, policy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
spec, err := adapter.BuildPoll(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
body, err := executeProtocolRequest(withProviderRequestKind(ctx, "poll"), input.Config, spec)
|
||||
if err != nil {
|
||||
if isNewAPIChannel2TaskNotReady(input.Config.InterfaceType, taskID, err) {
|
||||
// NewAPI Channel 2 创建后存在短暂同步窗口。窗口内的 task_not_exist 只表示尚未可查;
|
||||
// 超过窗口仍不可查时返回 pending error,让上层保留任务 ID,而不是误判成功或失败。
|
||||
taskNotExistMisses++
|
||||
maxMisses := timing.TaskNotExistMaxMisses
|
||||
if maxMisses <= 0 {
|
||||
maxMisses = len(timing.TaskNotExistRetryDelays) + 1
|
||||
}
|
||||
if time.Since(syncWindowStartedAt) >= timing.TaskNotExistWindow || taskNotExistMisses >= maxMisses {
|
||||
return nil, providerStatePendingError{TaskID: taskID, Cause: err}
|
||||
}
|
||||
delayIndex := min(taskNotExistMisses-1, len(timing.TaskNotExistRetryDelays)-1)
|
||||
if delayIndex >= 0 {
|
||||
if sleepErr := sleepContext(ctx, timing.TaskNotExistRetryDelays[delayIndex]); sleepErr != nil {
|
||||
return nil, sleepErr
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
taskNotExistMisses = 0
|
||||
state, err := adapter.ParsePoll(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID}, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
if state.TaskID != "" {
|
||||
taskID = state.TaskID
|
||||
}
|
||||
switch state.Status {
|
||||
case protocol.StatusSucceeded:
|
||||
return finishProtocolAdapterResult(ctx, input, adapter, request, taskID, state.Result)
|
||||
result, err := finishProtocolAdapterResult(ctx, input, adapter, request, taskID, state.Result, policy)
|
||||
return videoPollOutcome{Done: err == nil, Result: result}, err
|
||||
case protocol.StatusFailed, protocol.StatusCancelled:
|
||||
return nil, protocolResultError(state.Message, taskID)
|
||||
return videoPollOutcome{}, protocolResultError(state.Message, taskID)
|
||||
}
|
||||
if err := sleepContext(ctx, timing.PollInterval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("声明式协议任务超时(任务 %s)", taskID)
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func extractProviderTaskID(body []byte) (string, error) {
|
||||
@@ -188,22 +148,6 @@ func extractProviderTaskID(body []byte) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
func isNewAPIChannel2TaskNotReady(interfaceType string, taskID string, err error) bool {
|
||||
if strings.TrimSpace(interfaceType) != string(model.ChannelInterfaceNewAPIChannel2) || strings.TrimSpace(taskID) == "" {
|
||||
return false
|
||||
}
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusBadRequest {
|
||||
return false
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal([]byte(httpErr.Body), &payload) != nil {
|
||||
return false
|
||||
}
|
||||
code, message := providerFailureDetails(payload)
|
||||
return strings.EqualFold(strings.TrimSpace(code), "task_not_exist") || strings.EqualFold(strings.TrimSpace(message), "task_not_exist")
|
||||
}
|
||||
|
||||
// queryProtocolAdapterVideoTask 只读取一次已有的声明式 Provider 任务。
|
||||
// 人工恢复走此路径,因此检查本地失败任务时绝不会创建第二个计费任务。
|
||||
func queryProtocolAdapterVideoTask(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, taskID string) (map[string]interface{}, string, error) {
|
||||
@@ -224,7 +168,7 @@ func queryProtocolAdapterVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
providerStatus := string(state.Status)
|
||||
switch state.Status {
|
||||
case protocol.StatusSucceeded:
|
||||
result, err := finishProtocolAdapterResult(ctx, input, adapter, request, taskID, state.Result)
|
||||
result, err := finishProtocolAdapterResult(ctx, input, adapter, request, taskID, state.Result, defaultVideoPollPolicy())
|
||||
return result, providerStatus, err
|
||||
case protocol.StatusFailed, protocol.StatusCancelled:
|
||||
return nil, providerStatus, protocolResultError(state.Message, taskID)
|
||||
@@ -803,7 +747,7 @@ func appendProtocolQuery(rawURL string, values map[string][]string) (string, err
|
||||
return parsed.String(), nil
|
||||
}
|
||||
|
||||
func finishProtocolResult(ctx context.Context, config providerConfig, mode string, result *protocol.Result) (map[string]interface{}, error) {
|
||||
func finishProtocolResult(ctx context.Context, config providerConfig, mode string, taskID string, result *protocol.Result, pollPolicy videoPollPolicy) (map[string]interface{}, error) {
|
||||
if result == nil {
|
||||
return nil, errors.New("声明式协议已完成但没有返回结果")
|
||||
}
|
||||
@@ -830,7 +774,16 @@ func finishProtocolResult(ctx context.Context, config providerConfig, mode strin
|
||||
}
|
||||
items := make([]interface{}, 0, len(references))
|
||||
for _, reference := range references {
|
||||
data, mimeType, err := protocolMediaBytes(ctx, config, reference)
|
||||
var data []byte
|
||||
var mimeType string
|
||||
var err error
|
||||
if mode == "video" {
|
||||
data, mimeType, err = runVideoDownload(ctx, taskID, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return protocolMediaBytesOnce(ctx, config, reference)
|
||||
})
|
||||
} else {
|
||||
data, mimeType, err = protocolMediaBytes(ctx, config, reference)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -850,20 +803,29 @@ func finishProtocolResult(ctx context.Context, config providerConfig, mode strin
|
||||
// finishProtocolAdapterResult 优先消费 create/poll 已返回的内联或 URL 结果,只有插件明确声明独立结果端点时才下载。
|
||||
// 空响应或下载失败必须向上失败,不能生成伪素材;application/octet-stream 仅表示传输层未知类型,
|
||||
// 不会把未知内容伪装成具体图片、视频或音频 MIME。
|
||||
func finishProtocolAdapterResult(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, request protocol.GenerationRequest, taskID string, result *protocol.Result) (map[string]interface{}, error) {
|
||||
func finishProtocolAdapterResult(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, request protocol.GenerationRequest, taskID string, result *protocol.Result, pollPolicy videoPollPolicy) (map[string]interface{}, error) {
|
||||
if protocolResultHasOutput(input.Mode, result) {
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, result)
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, taskID, result, pollPolicy)
|
||||
}
|
||||
resultAdapter, ok := adapter.(protocol.ResultAdapter)
|
||||
capability, hasCapability := adapter.(protocol.ResultCapability)
|
||||
if !ok || !hasCapability || !capability.ResultAvailable() {
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, result)
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, taskID, result, pollPolicy)
|
||||
}
|
||||
spec, err := resultAdapter.BuildResult(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, mimeType, err := executeProtocolBinaryRequest(withProviderRequestKind(ctx, "download"), input.Config, spec)
|
||||
download := func(ctx context.Context) ([]byte, string, error) {
|
||||
return executeProtocolBinaryRequest(withProviderRequestKind(ctx, "download"), input.Config, spec)
|
||||
}
|
||||
var data []byte
|
||||
var mimeType string
|
||||
if input.Mode == "video" {
|
||||
data, mimeType, err = runVideoDownload(ctx, taskID, pollPolicy, download)
|
||||
} else {
|
||||
data, mimeType, err = download(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("声明式协议结果下载失败:%w", err)
|
||||
}
|
||||
@@ -886,7 +848,7 @@ func finishProtocolAdapterResult(ctx context.Context, input canvasGenerationInpu
|
||||
default:
|
||||
return nil, fmt.Errorf("声明式协议结果下载不支持生成模式 %s", input.Mode)
|
||||
}
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, downloaded)
|
||||
return finishProtocolResult(ctx, input.Config, input.Mode, taskID, downloaded, pollPolicy)
|
||||
}
|
||||
|
||||
func protocolResultHasOutput(mode string, result *protocol.Result) bool {
|
||||
@@ -908,21 +870,13 @@ func protocolResultHasOutput(mode string, result *protocol.Result) bool {
|
||||
}
|
||||
|
||||
func protocolMediaBytes(ctx context.Context, config providerConfig, reference protocol.MediaReference) ([]byte, string, error) {
|
||||
if strings.TrimSpace(reference.DataURL) != "" {
|
||||
mimeType, data, err := decodeProviderDataURL(reference.DataURL)
|
||||
return data, mimeType, err
|
||||
}
|
||||
value := strings.TrimSpace(reference.URL)
|
||||
if value == "" {
|
||||
return nil, "", errors.New("声明式协议媒体结果地址为空")
|
||||
}
|
||||
var data []byte
|
||||
var mimeType string
|
||||
var err error
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
data, mimeType, err = getProviderExternalBinary(withProviderRequestKind(ctx, "download"), config, value)
|
||||
data, mimeType, err = protocolMediaBytesOnce(ctx, config, reference)
|
||||
if err == nil {
|
||||
return data, normalizedMediaMimeType(mimeType, data), nil
|
||||
return data, mimeType, nil
|
||||
}
|
||||
if attempt == 2 || !retryableProtocolMediaDownload(err) {
|
||||
break
|
||||
@@ -934,6 +888,19 @@ func protocolMediaBytes(ctx context.Context, config providerConfig, reference pr
|
||||
return nil, "", fmt.Errorf("声明式协议媒体结果下载失败:%w", err)
|
||||
}
|
||||
|
||||
func protocolMediaBytesOnce(ctx context.Context, config providerConfig, reference protocol.MediaReference) ([]byte, string, error) {
|
||||
if strings.TrimSpace(reference.DataURL) != "" {
|
||||
mimeType, data, err := decodeProviderDataURL(reference.DataURL)
|
||||
return data, mimeType, err
|
||||
}
|
||||
value := strings.TrimSpace(reference.URL)
|
||||
if value == "" {
|
||||
return nil, "", errors.New("声明式协议媒体结果地址为空")
|
||||
}
|
||||
data, mimeType, err := getProviderExternalBinary(withProviderRequestKind(ctx, "download"), config, value)
|
||||
return data, normalizedMediaMimeType(mimeType, data), err
|
||||
}
|
||||
|
||||
func retryableProtocolMediaDownload(err error) bool {
|
||||
if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return false
|
||||
|
||||
@@ -213,7 +213,7 @@ func TestRunVideoTaskUsesDeclarativeAgnesJSONProtocol(t *testing.T) {
|
||||
defer server.Close()
|
||||
|
||||
ctx := withProtocolRegistry(context.Background(), center.registrySnapshot())
|
||||
result, err := runVideoTask(ctx, canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(ctx, canvasGenerationInput{
|
||||
Mode: "video",
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", InterfaceType: "agnes-video", Model: "agnes-video-2.5", VideoSeconds: "5", Size: "16:9", VQuality: "720P"},
|
||||
@@ -261,7 +261,7 @@ func TestRunVideoTaskDownloadsAuthenticatedDeclarativeResult(t *testing.T) {
|
||||
defer server.Close()
|
||||
|
||||
ctx := withProtocolRegistry(context.Background(), center.registrySnapshot())
|
||||
result, err := runVideoTask(ctx, canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(ctx, canvasGenerationInput{
|
||||
Mode: "video", Prompt: "cinematic shot",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", InterfaceType: "newapi", Model: "sora-2", VideoSeconds: "5", Size: "1280x720"},
|
||||
})
|
||||
@@ -932,7 +932,7 @@ func TestProviderPayloadErrorCategoryPrefersProviderCodeOverModerationWording(t
|
||||
|
||||
func TestRunVideoTaskRequiresOfficialPluginWhenRegistryEmpty(t *testing.T) {
|
||||
ctx := withProtocolRegistry(context.Background(), emptyProtocolRegistry)
|
||||
_, err := runVideoTask(ctx, canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(ctx, canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: "https://example.com", APIKey: "key", Model: "MiniMax-H3", InterfaceType: "minimax-video"},
|
||||
})
|
||||
@@ -1629,7 +1629,7 @@ func TestRunVideoTaskUsesNewAPIForAnyVideoModel(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "custom-video-v1"},
|
||||
})
|
||||
@@ -1646,6 +1646,85 @@ func TestRunVideoTaskUsesNewAPIForAnyVideoModel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskRetriesTransientPollFailureWithoutRecreating(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
createCalls := 0
|
||||
pollCalls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "POST /v1/videos":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"video-1","status":"queued"}`))
|
||||
case "GET /v1/videos/video-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if pollCalls == 1 {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = w.Write([]byte(`{"message":"temporary outage"}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"id":"video-1","status":"completed"}`))
|
||||
case "GET /v1/videos/video-1/content":
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTaskWithPolicy(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "custom-video-v1"},
|
||||
}, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || createCalls != 1 || pollCalls != 2 {
|
||||
t.Fatalf("result = %#v, create calls = %d, poll calls = %d", result, createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskRetriesDownloadWithoutRepolling(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
pollCalls := 0
|
||||
downloadCalls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "POST /v1/videos":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"video-1","status":"queued"}`))
|
||||
case "GET /v1/videos/video-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"video-1","status":"completed"}`))
|
||||
case "GET /v1/videos/video-1/content":
|
||||
downloadCalls++
|
||||
if downloadCalls == 1 {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTaskWithPolicy(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "custom-video-v1"},
|
||||
}, fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || pollCalls != 1 || downloadCalls != 2 {
|
||||
t.Fatalf("result = %#v, poll calls = %d, download calls = %d", result, pollCalls, downloadCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskSendsOnlyDeclaredResolutionName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -1689,7 +1768,7 @@ func TestRunVideoTaskSendsOnlyDeclaredResolutionName(t *testing.T) {
|
||||
if test.withoutProfile {
|
||||
profile = nil
|
||||
}
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "synthetic prompt",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "public-video", VideoSeconds: "8", Size: "16:9", VQuality: test.quality},
|
||||
VideoCapability: profile,
|
||||
@@ -1731,7 +1810,7 @@ func TestRunVideoTaskUsesNestedURLBeforeResultURL(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "grok-imagine-video-1.5-1080p", VideoSeconds: "15"},
|
||||
})
|
||||
@@ -1782,7 +1861,7 @@ func TestRunVideoTaskUsesJSONForGrokVideo(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "grok-video", VideoSeconds: "10"},
|
||||
ReferenceImages: []providerMedia{{ID: "image-1", DataURL: testReferenceImageDataURL}},
|
||||
@@ -1840,7 +1919,7 @@ func TestRunVideoTaskUsesXAIVideoGenerationEndpoint(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{
|
||||
BaseURL: server.URL + "/v1",
|
||||
@@ -1882,7 +1961,7 @@ func TestRunVideoTaskXAIVideoRejectsUnreachableResultURL(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{
|
||||
BaseURL: server.URL + "/v1",
|
||||
@@ -2061,7 +2140,7 @@ func TestVolcengineArkVideoProtocolUsesContentTaskAndDownloadsResult(t *testing.
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/api/v3", APIKey: "test-key", Model: "doubao-seedance-test", InterfaceType: "volcengine-ark-video"},
|
||||
ReferenceImages: []providerMedia{{ID: "start", URL: server.URL + "/reference.png"}},
|
||||
@@ -2257,7 +2336,7 @@ func TestRunNewAPIChannel1VideoTaskDownloadsSucceededObject(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "seedance-2.0", InterfaceType: "newapi-channel-1"},
|
||||
})
|
||||
@@ -2321,7 +2400,7 @@ func TestRunNewAPIChannel2VideoTaskDownloadsTemporaryResult(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi-channel-2", "grok-image-video").Video
|
||||
profile.Resolutions = []string{"720p"}
|
||||
profile.DefaultResolution = "720p"
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "grok-image-video", InterfaceType: "newapi-channel-2", VideoSeconds: "15", Size: "9:16", VQuality: "720", VideoGenerateAudio: "true"},
|
||||
ReferenceImages: []providerMedia{
|
||||
@@ -2376,7 +2455,7 @@ func TestRunNewAPIChannel2VideoTaskResumesOriginalProviderTaskWithoutAnotherPost
|
||||
ctx := withProviderAnalytics(context.Background(), nil, model.Task{
|
||||
ID: "task-1", Type: "canvas_video", ProviderRequestID: "existing-provider-task", InputJSON: string(inputJSON),
|
||||
})
|
||||
result, err := runVideoTask(ctx, input)
|
||||
result, err := runVideoTaskForTest(ctx, input)
|
||||
if err != nil {
|
||||
t.Fatalf("runVideoTask() error = %v", err)
|
||||
}
|
||||
@@ -2393,7 +2472,7 @@ func TestRunNewAPIChannel2VideoTaskReturnsTypedDeadlineWhenPollingWindowEnds(t *
|
||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
|
||||
defer cancel()
|
||||
ctx = withProviderAnalytics(ctx, nil, model.Task{ID: "task-1", Type: "canvas_video", ProviderRequestID: "existing-provider-task"})
|
||||
_, err := runVideoTask(ctx, canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(ctx, canvasGenerationInput{
|
||||
Mode: "video",
|
||||
Config: providerConfig{InterfaceType: string(model.ChannelInterfaceNewAPIChannel2), Model: "video-model", BaseURL: "https://example.com", APIKey: "key"},
|
||||
})
|
||||
@@ -2427,7 +2506,7 @@ func TestRunGeminiVeoVideoTaskUsesLongRunningOperation(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", APIFormat: "gemini", Model: "veo-test", InterfaceType: "gemini-veo", VideoSeconds: "6", Size: "16:9", VQuality: "720"},
|
||||
})
|
||||
@@ -2632,7 +2711,7 @@ func TestRunNovitaVideoTaskDownloadsSucceededVideo(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "kling2.5_turbo_pro_t2v", InterfaceType: "novita-video", VideoSeconds: "5", Size: "16:9"},
|
||||
})
|
||||
@@ -2665,7 +2744,7 @@ func TestRunNovitaVideoTaskReturnsFailureReason(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "kling2.5_turbo_pro_t2v", InterfaceType: "novita-video"},
|
||||
})
|
||||
@@ -2711,7 +2790,7 @@ func TestRunMiniMaxVideoTaskCreatesPollsAndDownloads(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
result, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Mode: "video",
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "MiniMax-H3", InterfaceType: "minimax-video", VideoSeconds: "5", VQuality: "768P", Size: "16:9"},
|
||||
@@ -2747,7 +2826,7 @@ func TestRunMiniMaxVideoTaskSendsOfficial2KResolution(t *testing.T) {
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Mode: "video", Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "MiniMax-H3", InterfaceType: "minimax-video", VideoSeconds: "6", VQuality: "2K"},
|
||||
})
|
||||
@@ -2792,7 +2871,7 @@ func TestRunMiniMaxVideoTaskUsesExplicitReferenceRoles(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Mode: "video",
|
||||
Prompt: "保持角色一致",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "MiniMax-H3", InterfaceType: "minimax-video", VideoSeconds: "6", VQuality: "768P", Size: "16:9"},
|
||||
@@ -2821,7 +2900,7 @@ func TestRunMiniMaxVideoTaskReturnsFailureReason(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
_, err := runVideoTaskForTest(context.Background(), canvasGenerationInput{
|
||||
Mode: "video",
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "MiniMax-H3", InterfaceType: "minimax-video", VideoSeconds: "6"},
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
@@ -52,6 +51,10 @@ func (s *Service) validateResolvedVideoCapability(input *canvasGenerationInput)
|
||||
}
|
||||
|
||||
func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
return runVideoTaskWithPolicy(ctx, input, defaultVideoPollPolicy())
|
||||
}
|
||||
|
||||
func runVideoTaskWithPolicy(ctx context.Context, input canvasGenerationInput, pollPolicy videoPollPolicy) (map[string]interface{}, error) {
|
||||
// 路由顺序是协议边界,不是“哪个请求先试”:官方声明式接口必须由已注册适配器执行,
|
||||
// 缺少适配器时直接失败,不能偷偷退回遗留手写协议;只有未声明为官方插件的旧渠道才继续走兼容分支。
|
||||
if strings.TrimSpace(input.Mode) == "" {
|
||||
@@ -60,17 +63,17 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
// 已有官方声明式插件的 InterfaceType 只走适配器。未注入 registry 时补官方包,
|
||||
// 显式空 registry 则报“插件未安装”,不再回退到手写协议。
|
||||
ctx = ensureOfficialProtocolAdapter(ctx, input.Config.InterfaceType)
|
||||
if _, ok := declarativeProtocolAdapterForContext(ctx, input.Config.InterfaceType); ok {
|
||||
return runDeclarativeProtocolTask(ctx, input)
|
||||
if adapter, ok := declarativeProtocolAdapterForContext(ctx, input.Config.InterfaceType); ok {
|
||||
return runProtocolAdapterTaskWithPolicy(ctx, input, adapter, pollPolicy)
|
||||
}
|
||||
if label, official := officialDeclarativeVideoInterface(input.Config.InterfaceType); official {
|
||||
return nil, fmt.Errorf("%s 视频插件未安装", label)
|
||||
}
|
||||
if isArkPlanVideoConfig(input.Config) {
|
||||
return runSeedanceAgentPlanVideoTask(ctx, input)
|
||||
return runSeedanceAgentPlanVideoTask(ctx, input, pollPolicy)
|
||||
}
|
||||
if isSeedanceVideoConfig(input.Config) {
|
||||
return runSeedanceVideosTask(ctx, input)
|
||||
return runSeedanceVideosTask(ctx, input, pollPolicy)
|
||||
}
|
||||
if len(input.ReferenceVideos) > 0 || len(input.ReferenceAudios) > 0 {
|
||||
return nil, errors.New("OpenAI 风格视频接口不支持参考视频或参考音频,请切换到 Seedance / Agent Plan 渠道")
|
||||
@@ -131,10 +134,10 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
if id == "" {
|
||||
return nil, errors.New("视频接口没有返回任务 ID")
|
||||
}
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
return runVideoPollLoop(ctx, id, pollPolicy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
var state map[string]interface{}
|
||||
if err := getJSON(ctx, input.Config, "/videos/"+id, &state); err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
if data, ok := state["data"].(map[string]interface{}); ok {
|
||||
state = data
|
||||
@@ -142,27 +145,28 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
status := strings.ToLower(stringField(state, "status"))
|
||||
if status == "completed" || status == "succeeded" || status == "success" || status == "done" {
|
||||
if videoURL := newAPIVideoResultURL(state); videoURL != "" {
|
||||
data, mimeType, err := getProviderExternalBinary(withProviderRequestKind(ctx, "download"), input.Config, videoURL)
|
||||
data, mimeType, err := runVideoDownload(ctx, id, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getProviderExternalBinary(withProviderRequestKind(ctx, "download"), input.Config, videoURL)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("视频结果下载失败(任务 %s):%w", id, err)
|
||||
return videoPollOutcome{}, fmt.Errorf("视频结果下载失败(任务 %s):%w", id, err)
|
||||
}
|
||||
mimeType = normalizedMediaMimeType(mimeType, data)
|
||||
return map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}, nil
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}}, nil
|
||||
}
|
||||
data, mimeType, err := getBinary(ctx, input.Config, "/videos/"+id+"/content")
|
||||
data, mimeType, err := runVideoDownload(ctx, id, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getBinary(withProviderRequestKind(ctx, "download"), input.Config, "/videos/"+id+"/content")
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
return map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}, nil
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}}, nil
|
||||
}
|
||||
if status == "failed" || status == "cancelled" {
|
||||
return nil, errors.New("视频生成失败")
|
||||
return videoPollOutcome{}, errors.New("视频生成失败")
|
||||
}
|
||||
if err := sleepContext(ctx, 2500*time.Millisecond); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, errors.New("视频生成超时")
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func newAPIVideoResultURL(state map[string]interface{}) string {
|
||||
@@ -221,7 +225,7 @@ func grokVideoBody(input canvasGenerationInput) (map[string]interface{}, error)
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func runSeedanceVideosTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
func runSeedanceVideosTask(ctx context.Context, input canvasGenerationInput, pollPolicy videoPollPolicy) (map[string]interface{}, error) {
|
||||
// 恢复任务已有 provider ID 时只能继续查询,绝不能重新 create,否则会产生第二个计费任务。
|
||||
// create 成功后的 poll/download 任一失败都保留失败或结果未知语义,不降级成“成功但无内容”。
|
||||
id := resumedProviderRequestID(ctx)
|
||||
@@ -246,10 +250,10 @@ func runSeedanceVideosTask(ctx context.Context, input canvasGenerationInput) (ma
|
||||
if id == "" {
|
||||
return nil, errors.New("Seedance 接口没有返回任务 ID")
|
||||
}
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
return runVideoPollLoop(ctx, id, pollPolicy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
var state map[string]interface{}
|
||||
if err := getJSON(ctx, input.Config, "/videos/"+id, &state); err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
if data, ok := state["data"].(map[string]interface{}); ok {
|
||||
state = data
|
||||
@@ -258,29 +262,30 @@ func runSeedanceVideosTask(ctx context.Context, input canvasGenerationInput) (ma
|
||||
if status == "completed" || status == "succeeded" {
|
||||
videoURL := stringField(state, "video_url")
|
||||
if videoURL != "" {
|
||||
data, mimeType, err := getExternalBinary(withProviderRequestKind(ctx, "download"), videoURL)
|
||||
data, mimeType, err := runVideoDownload(ctx, id, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getExternalBinary(withProviderRequestKind(ctx, "download"), videoURL)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("视频结果下载失败:%w", err)
|
||||
return videoPollOutcome{}, fmt.Errorf("视频结果下载失败:%w", err)
|
||||
}
|
||||
return map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}, nil
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}}, nil
|
||||
}
|
||||
data, mimeType, err := getBinary(ctx, input.Config, "/videos/"+id+"/content")
|
||||
data, mimeType, err := runVideoDownload(ctx, id, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getBinary(withProviderRequestKind(ctx, "download"), input.Config, "/videos/"+id+"/content")
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Seedance 任务成功但未返回视频 URL,备用内容下载失败:%w", err)
|
||||
return videoPollOutcome{}, fmt.Errorf("Seedance 任务成功但未返回视频 URL,备用内容下载失败:%w", err)
|
||||
}
|
||||
return map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}, nil
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}}, nil
|
||||
}
|
||||
if status == "failed" || status == "cancelled" || status == "expired" {
|
||||
return nil, errors.New(defaultString(seedanceErrorMessage(state), "Seedance 视频生成失败"))
|
||||
return videoPollOutcome{}, errors.New(defaultString(seedanceErrorMessage(state), "Seedance 视频生成失败"))
|
||||
}
|
||||
if err := sleepContext(ctx, 5*time.Second); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, errors.New("Seedance 视频生成超时")
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationInput, pollPolicy videoPollPolicy) (map[string]interface{}, error) {
|
||||
providerName := "Seedance"
|
||||
if input.Config.InterfaceType == string(model.ChannelInterfaceVolcengineArkVideo) {
|
||||
providerName = "火山方舟"
|
||||
@@ -331,10 +336,10 @@ func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
if id == "" {
|
||||
return nil, fmt.Errorf("%s接口没有返回任务 ID", providerName)
|
||||
}
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
return runVideoPollLoop(ctx, id, pollPolicy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
var state map[string]interface{}
|
||||
if err := getJSON(ctx, input.Config, "/contents/generations/tasks/"+id, &state); err != nil {
|
||||
return nil, err
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
if data, ok := state["data"].(map[string]interface{}); ok {
|
||||
state = data
|
||||
@@ -344,22 +349,21 @@ func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
content, _ := state["content"].(map[string]interface{})
|
||||
videoURL := stringField(content, "video_url")
|
||||
if videoURL == "" {
|
||||
return nil, fmt.Errorf("%s任务成功但没有返回视频 URL", providerName)
|
||||
return videoPollOutcome{}, fmt.Errorf("%s任务成功但没有返回视频 URL", providerName)
|
||||
}
|
||||
data, mimeType, err := getExternalBinary(withProviderRequestKind(ctx, "download"), videoURL)
|
||||
data, mimeType, err := runVideoDownload(ctx, id, pollPolicy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getExternalBinary(withProviderRequestKind(ctx, "download"), videoURL)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("视频结果下载失败:%w", err)
|
||||
return videoPollOutcome{}, fmt.Errorf("视频结果下载失败:%w", err)
|
||||
}
|
||||
return map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}, nil
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video", "video": map[string]interface{}{"dataUrl": dataURL(mimeType, data), "mimeType": mimeType}}}, nil
|
||||
}
|
||||
if status == "failed" || status == "cancelled" || status == "expired" {
|
||||
return nil, fmt.Errorf("%s视频生成失败", providerName)
|
||||
return videoPollOutcome{}, fmt.Errorf("%s视频生成失败", providerName)
|
||||
}
|
||||
if err := sleepContext(ctx, 5*time.Second); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("%s视频生成超时", providerName)
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func seedanceContent(input canvasGenerationInput) ([]map[string]interface{}, error) {
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultVideoPollInterval = 30 * time.Second
|
||||
|
||||
type videoPollPolicy struct {
|
||||
InitialDelay time.Duration
|
||||
Interval time.Duration
|
||||
MaxNotFoundMisses int
|
||||
MaxMalformedResponses int
|
||||
MaxDownloadTries int
|
||||
RetryTransient bool
|
||||
Sleep func(context.Context, time.Duration) error
|
||||
Notify func(context.Context, string, videoPollEvent, error)
|
||||
}
|
||||
|
||||
type videoPollEvent string
|
||||
|
||||
const (
|
||||
videoPollEventRetrying videoPollEvent = "retrying"
|
||||
videoPollEventRecovered videoPollEvent = "recovered"
|
||||
)
|
||||
|
||||
type videoPollOutcome struct {
|
||||
Done bool
|
||||
Result map[string]interface{}
|
||||
}
|
||||
|
||||
type videoDownloadError struct {
|
||||
TaskID string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e videoDownloadError) Error() string {
|
||||
if strings.TrimSpace(e.TaskID) == "" {
|
||||
return fmt.Sprintf("视频结果下载失败:%v", e.Cause)
|
||||
}
|
||||
return fmt.Sprintf("视频结果下载失败(任务 %s):%v", e.TaskID, e.Cause)
|
||||
}
|
||||
|
||||
func (e videoDownloadError) Unwrap() error { return e.Cause }
|
||||
|
||||
func defaultVideoPollPolicy() videoPollPolicy {
|
||||
return videoPollPolicy{
|
||||
InitialDelay: defaultVideoPollInterval,
|
||||
Interval: defaultVideoPollInterval,
|
||||
MaxNotFoundMisses: 3,
|
||||
MaxMalformedResponses: 3,
|
||||
MaxDownloadTries: 3,
|
||||
RetryTransient: true,
|
||||
Sleep: sleepContext,
|
||||
Notify: notifyTaskVideoPollEvent,
|
||||
}
|
||||
}
|
||||
|
||||
func runVideoPollLoop(ctx context.Context, taskID string, policy videoPollPolicy, query func(context.Context) (videoPollOutcome, error)) (map[string]interface{}, error) {
|
||||
policy = normalizeVideoPollPolicy(policy)
|
||||
deadline := providerPollingDeadline(ctx)
|
||||
pollContext, cancel := context.WithDeadline(ctx, deadline)
|
||||
defer cancel()
|
||||
ctx = pollContext
|
||||
nextDelay := policy.InitialDelay
|
||||
notFoundMisses := 0
|
||||
malformedResponses := 0
|
||||
retrying := false
|
||||
for time.Now().Before(deadline) {
|
||||
if nextDelay > 0 {
|
||||
if err := policy.Sleep(ctx, nextDelay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
outcome, err := query(ctx)
|
||||
if err != nil {
|
||||
if !policy.RetryTransient {
|
||||
return nil, err
|
||||
}
|
||||
retry, notFound := retryableVideoPollError(ctx, err)
|
||||
if !retry {
|
||||
return nil, err
|
||||
}
|
||||
malformed := isTransientResponseDecodeError(err)
|
||||
if malformed {
|
||||
malformedResponses++
|
||||
notFoundMisses = 0
|
||||
if malformedResponses >= policy.MaxMalformedResponses {
|
||||
return nil, err
|
||||
}
|
||||
} else if notFound {
|
||||
malformedResponses = 0
|
||||
notFoundMisses++
|
||||
if notFoundMisses >= policy.MaxNotFoundMisses {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
malformedResponses = 0
|
||||
notFoundMisses = 0
|
||||
}
|
||||
if !retrying {
|
||||
retrying = true
|
||||
policy.Notify(ctx, taskID, videoPollEventRetrying, err)
|
||||
}
|
||||
nextDelay = max(policy.Interval, providerRetryAfter(err))
|
||||
continue
|
||||
}
|
||||
notFoundMisses = 0
|
||||
malformedResponses = 0
|
||||
nextDelay = policy.Interval
|
||||
if retrying {
|
||||
retrying = false
|
||||
policy.Notify(ctx, taskID, videoPollEventRecovered, nil)
|
||||
}
|
||||
if outcome.Done {
|
||||
return outcome.Result, nil
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, context.DeadlineExceeded
|
||||
}
|
||||
|
||||
func normalizeVideoPollPolicy(policy videoPollPolicy) videoPollPolicy {
|
||||
if policy.Interval <= 0 {
|
||||
policy.Interval = defaultVideoPollInterval
|
||||
}
|
||||
if policy.MaxNotFoundMisses <= 0 {
|
||||
policy.MaxNotFoundMisses = 3
|
||||
}
|
||||
if policy.MaxMalformedResponses <= 0 {
|
||||
policy.MaxMalformedResponses = 3
|
||||
}
|
||||
if policy.MaxDownloadTries <= 0 {
|
||||
policy.MaxDownloadTries = 3
|
||||
}
|
||||
if policy.Sleep == nil {
|
||||
policy.Sleep = sleepContext
|
||||
}
|
||||
if policy.Notify == nil {
|
||||
policy.Notify = func(context.Context, string, videoPollEvent, error) {}
|
||||
}
|
||||
return policy
|
||||
}
|
||||
|
||||
func runVideoDownload(ctx context.Context, taskID string, policy videoPollPolicy, download func(context.Context) ([]byte, string, error)) ([]byte, string, error) {
|
||||
policy = normalizeVideoPollPolicy(policy)
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= policy.MaxDownloadTries; attempt++ {
|
||||
data, mimeType, err := download(ctx)
|
||||
if err == nil {
|
||||
return data, mimeType, nil
|
||||
}
|
||||
lastErr = err
|
||||
retry, _ := retryableVideoPollError(ctx, err)
|
||||
if !retry || attempt == policy.MaxDownloadTries {
|
||||
return nil, "", videoDownloadError{TaskID: taskID, Cause: err}
|
||||
}
|
||||
delay := max(policy.Interval, providerRetryAfter(err))
|
||||
if err := policy.Sleep(ctx, delay); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
}
|
||||
return nil, "", videoDownloadError{TaskID: taskID, Cause: lastErr}
|
||||
}
|
||||
|
||||
func retryableVideoPollError(ctx context.Context, err error) (retry bool, notFound bool) {
|
||||
if err == nil || ctx.Err() != nil || errors.Is(err, context.Canceled) {
|
||||
return false, false
|
||||
}
|
||||
var downloadError videoDownloadError
|
||||
if errors.As(err, &downloadError) {
|
||||
return false, false
|
||||
}
|
||||
if code, _ := ChannelSlotFailureDetails(err); code != "" {
|
||||
return true, false
|
||||
}
|
||||
var circuitOpen providerCircuitOpenError
|
||||
if errors.As(err, &circuitOpen) {
|
||||
return true, false
|
||||
}
|
||||
if isTransientResponseDecodeError(err) {
|
||||
return true, false
|
||||
}
|
||||
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.ErrClosedPipe) {
|
||||
return true, false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return true, false
|
||||
}
|
||||
var httpErr providerHTTPError
|
||||
if errors.As(err, &httpErr) {
|
||||
if isProviderTaskNotReadyError(httpErr) {
|
||||
return true, true
|
||||
}
|
||||
if httpErr.StatusCode == http.StatusNotFound {
|
||||
return true, true
|
||||
}
|
||||
switch httpErr.StatusCode {
|
||||
case http.StatusRequestTimeout, http.StatusConflict, http.StatusTooEarly, http.StatusTooManyRequests:
|
||||
return true, false
|
||||
default:
|
||||
return httpErr.StatusCode >= http.StatusInternalServerError, false
|
||||
}
|
||||
}
|
||||
var networkError net.Error
|
||||
if errors.As(err, &networkError) {
|
||||
return networkError.Timeout() || networkError.Temporary(), false
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
func isTransientResponseDecodeError(err error) bool {
|
||||
var decodeError providerResponseDecodeError
|
||||
if errors.As(err, &decodeError) {
|
||||
return true
|
||||
}
|
||||
var syntaxError *json.SyntaxError
|
||||
return errors.As(err, &syntaxError)
|
||||
}
|
||||
|
||||
func notifyTaskVideoPollEvent(ctx context.Context, _ string, event videoPollEvent, eventErr error) {
|
||||
metadata, ok := ctx.Value(providerAnalyticsKey{}).(providerAnalyticsContext)
|
||||
if !ok || metadata.Service == nil || metadata.Service.repo == nil || metadata.TaskID == "" {
|
||||
return
|
||||
}
|
||||
message := "上游视频查询暂时异常,将继续轮询原任务"
|
||||
level := "warn"
|
||||
payload := ""
|
||||
if eventErr != nil {
|
||||
payload = eventErr.Error()
|
||||
}
|
||||
if event == videoPollEventRecovered {
|
||||
message = "上游视频查询已恢复"
|
||||
level = "info"
|
||||
}
|
||||
_ = metadata.Service.log(metadata.UserID, metadata.TaskID, level, message, payload)
|
||||
}
|
||||
|
||||
func isProviderTaskNotReadyError(httpErr providerHTTPError) bool {
|
||||
if httpErr.StatusCode != http.StatusBadRequest && httpErr.StatusCode != http.StatusNotFound {
|
||||
return false
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal([]byte(httpErr.Body), &payload) != nil {
|
||||
return false
|
||||
}
|
||||
code, message := providerFailureDetails(payload)
|
||||
for _, value := range []string{code, message} {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "task_not_exist", "task_not_found", "task not exist", "task not found":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func providerRetryAfter(err error) time.Duration {
|
||||
var httpErr providerHTTPError
|
||||
if errors.As(err, &httpErr) && httpErr.RetryAfter > 0 {
|
||||
return httpErr.RetryAfter
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestVideoPollRecoversFromTransientHTTPError(t *testing.T) {
|
||||
attempts := 0
|
||||
result, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
switch attempts {
|
||||
case 1:
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusBadGateway, Status: "502 Bad Gateway"}
|
||||
case 2:
|
||||
return videoPollOutcome{}, nil
|
||||
default:
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if attempts != 3 || result["mode"] != "video" {
|
||||
t.Fatalf("attempts = %d, result = %#v", attempts, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollWaitsBeforeFirstQueryAndUsesRetryAfter(t *testing.T) {
|
||||
var waits []time.Duration
|
||||
policy := fastVideoPollPolicy()
|
||||
policy.InitialDelay = 10 * time.Millisecond
|
||||
policy.Interval = 10 * time.Millisecond
|
||||
policy.Sleep = func(_ context.Context, delay time.Duration) error {
|
||||
waits = append(waits, delay)
|
||||
return nil
|
||||
}
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", policy, func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
if attempts == 1 {
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusTooManyRequests, RetryAfter: 20 * time.Millisecond}
|
||||
}
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []time.Duration{10 * time.Millisecond, 20 * time.Millisecond}; !reflect.DeepEqual(waits, want) {
|
||||
t.Fatalf("waits = %v, want %v", waits, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollStopsImmediatelyOnAuthenticationError(t *testing.T) {
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusUnauthorized, Status: "401 Unauthorized"}
|
||||
})
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("error = %#v, want 401 provider error", err)
|
||||
}
|
||||
if attempts != 1 {
|
||||
t.Fatalf("attempts = %d, want 1", attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollStopsAfterThreeConsecutiveNotFoundResponses(t *testing.T) {
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusNotFound, Status: "404 Not Found"}
|
||||
})
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("error = %#v, want 404 provider error", err)
|
||||
}
|
||||
if attempts != 3 {
|
||||
t.Fatalf("attempts = %d, want 3", attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollSuccessfulPendingResponseResetsNotFoundCounter(t *testing.T) {
|
||||
attempts := 0
|
||||
result, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
switch attempts {
|
||||
case 1, 3:
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusNotFound, Status: "404 Not Found"}
|
||||
case 2:
|
||||
return videoPollOutcome{}, nil
|
||||
default:
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if attempts != 4 || result["mode"] != "video" {
|
||||
t.Fatalf("attempts = %d, result = %#v", attempts, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollRetriesMalformedResponseTwiceThenSucceeds(t *testing.T) {
|
||||
attempts := 0
|
||||
result, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
if attempts <= 2 {
|
||||
return videoPollOutcome{}, &json.SyntaxError{Offset: int64(attempts)}
|
||||
}
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
})
|
||||
if err != nil || attempts != 3 || result["mode"] != "video" {
|
||||
t.Fatalf("result = %#v, error = %v, attempts = %d", result, err, attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollStopsAfterThreeMalformedResponses(t *testing.T) {
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
return videoPollOutcome{}, &json.SyntaxError{Offset: int64(attempts)}
|
||||
})
|
||||
var syntaxError *json.SyntaxError
|
||||
if !errors.As(err, &syntaxError) || attempts != 3 {
|
||||
t.Fatalf("error = %#v, attempts = %d, want three attempts and syntax error", err, attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollTreatsCircuitAndInterruptedReadsAsTransient(t *testing.T) {
|
||||
for _, err := range []error{providerCircuitOpenError{}, io.ErrUnexpectedEOF, io.ErrClosedPipe} {
|
||||
retry, notFound := retryableVideoPollError(context.Background(), err)
|
||||
if !retry || notFound {
|
||||
t.Fatalf("error %T classified as retry=%v notFound=%v", err, retry, notFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollNotifiesOnlyWhenRetryStartsAndRecovers(t *testing.T) {
|
||||
var events []videoPollEvent
|
||||
policy := fastVideoPollPolicy()
|
||||
policy.Notify = func(_ context.Context, _ string, event videoPollEvent, _ error) {
|
||||
events = append(events, event)
|
||||
}
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", policy, func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
switch attempts {
|
||||
case 1, 2:
|
||||
return videoPollOutcome{}, providerHTTPError{StatusCode: http.StatusBadGateway}
|
||||
case 3:
|
||||
return videoPollOutcome{}, nil
|
||||
default:
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []videoPollEvent{videoPollEventRetrying, videoPollEventRecovered}
|
||||
if len(events) != len(want) || events[0] != want[0] || events[1] != want[1] {
|
||||
t.Fatalf("events = %#v, want %#v", events, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollContextCancellationInterruptsInitialWait(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
called := false
|
||||
_, err := runVideoPollLoop(ctx, "provider-task-1", defaultVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
called = true
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error = %v, want context canceled", err)
|
||||
}
|
||||
if called {
|
||||
t.Fatal("query ran after cancellation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollAppliesFallbackDeadlineToWaitAndQuery(t *testing.T) {
|
||||
policy := fastVideoPollPolicy()
|
||||
waitHadDeadline := false
|
||||
queryHadDeadline := false
|
||||
policy.Sleep = func(ctx context.Context, _ time.Duration) error {
|
||||
_, waitHadDeadline = ctx.Deadline()
|
||||
return nil
|
||||
}
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", policy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
_, queryHadDeadline = ctx.Deadline()
|
||||
return videoPollOutcome{Done: true, Result: map[string]interface{}{"mode": "video"}}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !waitHadDeadline || !queryHadDeadline {
|
||||
t.Fatalf("wait deadline = %v, query deadline = %v", waitHadDeadline, queryHadDeadline)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoDownloadRetriesTransientFailure(t *testing.T) {
|
||||
attempts := 0
|
||||
waits := 0
|
||||
policy := fastVideoPollPolicy()
|
||||
policy.Sleep = func(context.Context, time.Duration) error {
|
||||
waits++
|
||||
return nil
|
||||
}
|
||||
data, mimeType, err := runVideoDownload(context.Background(), "provider-task-1", policy, func(context.Context) ([]byte, string, error) {
|
||||
attempts++
|
||||
if attempts == 1 {
|
||||
return nil, "", providerHTTPError{StatusCode: http.StatusBadGateway, Status: "502 Bad Gateway"}
|
||||
}
|
||||
return []byte("video"), "video/mp4", nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "video" || mimeType != "video/mp4" || attempts != 2 || waits != 1 {
|
||||
t.Fatalf("data = %q, mime = %q, attempts = %d, waits = %d", data, mimeType, attempts, waits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoDownloadStopsAfterThreeTransientFailures(t *testing.T) {
|
||||
attempts := 0
|
||||
_, _, err := runVideoDownload(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) ([]byte, string, error) {
|
||||
attempts++
|
||||
return nil, "", providerHTTPError{StatusCode: http.StatusServiceUnavailable, Status: "503 Service Unavailable"}
|
||||
})
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &httpErr) || attempts != 3 {
|
||||
t.Fatalf("error = %#v, attempts = %d, want three attempts and provider error", err, attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoPollDoesNotRetryExhaustedDownload(t *testing.T) {
|
||||
attempts := 0
|
||||
_, err := runVideoPollLoop(context.Background(), "provider-task-1", fastVideoPollPolicy(), func(context.Context) (videoPollOutcome, error) {
|
||||
attempts++
|
||||
return videoPollOutcome{}, videoDownloadError{TaskID: "provider-task-1", Cause: providerHTTPError{StatusCode: http.StatusBadGateway}}
|
||||
})
|
||||
var downloadError videoDownloadError
|
||||
if !errors.As(err, &downloadError) || attempts != 1 {
|
||||
t.Fatalf("error = %#v, poll attempts = %d, want one exhausted download", err, attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func fastVideoPollPolicy() videoPollPolicy {
|
||||
policy := defaultVideoPollPolicy()
|
||||
policy.InitialDelay = time.Millisecond
|
||||
policy.Interval = time.Millisecond
|
||||
policy.Sleep = func(context.Context, time.Duration) error { return nil }
|
||||
return policy
|
||||
}
|
||||
|
||||
func runVideoTaskForTest(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
return runVideoTaskWithPolicy(ctx, input, fastVideoPollPolicy())
|
||||
}
|
||||
@@ -26,6 +26,25 @@ func TestVideoTaskTimeoutHasFiveMinuteSafetyFloor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultVideoTaskTimeoutIsOneHour(t *testing.T) {
|
||||
policy := defaultRuntimePolicy().Task
|
||||
if policy.VideoTimeoutMinutes != 60 {
|
||||
t.Fatalf("default video timeout = %d minutes, want 60", policy.VideoTimeoutMinutes)
|
||||
}
|
||||
if got := taskExecutionTimeoutWithPolicy("canvas_video", policy); got != time.Hour {
|
||||
t.Fatalf("video execution timeout = %s, want 1h", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderPollingDeadlineDefaultsToOneHour(t *testing.T) {
|
||||
startedAt := time.Now()
|
||||
deadline := providerPollingDeadline(context.Background())
|
||||
remaining := deadline.Sub(startedAt)
|
||||
if remaining < time.Hour || remaining > time.Hour+time.Second {
|
||||
t.Fatalf("provider polling deadline = %s, want approximately 1h", remaining)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyResumableNewAPIChannel2VideoDeadlinesStayRunning(t *testing.T) {
|
||||
svc := &Service{}
|
||||
input, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://example.com", InterfaceType: string(model.ChannelInterfaceNewAPIChannel2)}})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
@@ -12,6 +13,43 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestVideoPollLogSchedulesNextCheckThirtySecondsLater(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:provider-poll-schedule?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Task{}, &model.ApiCallLog{}, &model.ModelPricing{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task := model.Task{ID: "task-1", UserID: "user-1", Type: "canvas_video", Status: model.TaskStatusRunning}
|
||||
if err := db.Create(&task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := model.ApiCallLog{ID: "api-log-1", UserID: task.UserID, TaskID: task.ID, Capability: "video", RequestKind: "create", ProviderRequestID: "provider-task-1", CreatedAt: time.Now()}
|
||||
if err := db.Create(&root).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := &Service{repo: repository.New(db)}
|
||||
startedAt := time.Now()
|
||||
if err := service.LogAPICall(model.ApiCallLog{
|
||||
ID: "poll-log-1", UserID: task.UserID, TaskID: task.ID, Capability: "video", RequestKind: "poll",
|
||||
ProviderRequestID: "provider-task-1", Status: model.ApiCallStatusSucceeded, StatusCode: 200, CreatedAt: startedAt,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stored model.Task
|
||||
if err := db.First(&stored, "id = ?", task.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.NextPollAt == nil {
|
||||
t.Fatal("next poll time is nil")
|
||||
}
|
||||
delay := stored.NextPollAt.Sub(startedAt)
|
||||
if delay < 30*time.Second || delay > 31*time.Second {
|
||||
t.Fatalf("next poll delay = %s, want approximately 30s", delay)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureFailedProviderAttemptLoggedFillsPreflightGapOnce(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:provider-preflight-log?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
|
||||
@@ -375,7 +375,7 @@ func (s *Service) runRunningHubWorkflow(ctx context.Context, input canvasGenerat
|
||||
root := runningHubRootURL(input.Config.BaseURL)
|
||||
apiKey := runningHubAPIKey(input.Config)
|
||||
if resumed := resumedProviderRequestID(ctx); resumed != "" {
|
||||
return s.pollRunningHubWorkflow(ctx, input.Config, root, resumed)
|
||||
return s.pollRunningHubWorkflow(ctx, input.Config, root, resumed, input.Mode)
|
||||
}
|
||||
workflowID := strings.TrimSpace(input.Config.WorkflowID)
|
||||
webappID := strings.TrimSpace(input.Config.WebappID)
|
||||
@@ -459,7 +459,7 @@ func (s *Service) runRunningHubWorkflow(ctx context.Context, input canvasGenerat
|
||||
metadata, _ := ctx.Value(providerAnalyticsKey{}).(providerAnalyticsContext)
|
||||
_ = s.log(metadata.UserID, metadata.TaskID, "error", "RunningHub 请求状态保存失败", taskID+":"+err.Error())
|
||||
}
|
||||
return s.pollRunningHubWorkflow(ctx, input.Config, root, taskID)
|
||||
return s.pollRunningHubWorkflow(ctx, input.Config, root, taskID, input.Mode)
|
||||
}
|
||||
|
||||
func runningHubWorkflowFailureMessage(response map[string]any) string {
|
||||
@@ -1641,7 +1641,59 @@ func (s *Service) runningHubJSON(ctx context.Context, config providerConfig, end
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) pollRunningHubWorkflow(ctx context.Context, config providerConfig, root string, taskID string) (map[string]interface{}, error) {
|
||||
func (s *Service) pollRunningHubWorkflow(ctx context.Context, config providerConfig, root string, taskID string, mode string) (map[string]interface{}, error) {
|
||||
if mode == "video" {
|
||||
return s.pollRunningHubVideoWorkflowWithPolicy(ctx, config, root, taskID, defaultVideoPollPolicy())
|
||||
}
|
||||
return s.pollRunningHubWorkflowLegacy(ctx, config, root, taskID)
|
||||
}
|
||||
|
||||
func (s *Service) pollRunningHubVideoWorkflowWithPolicy(ctx context.Context, config providerConfig, root string, taskID string, policy videoPollPolicy) (map[string]interface{}, error) {
|
||||
return runVideoPollLoop(ctx, taskID, policy, func(ctx context.Context) (videoPollOutcome, error) {
|
||||
var response map[string]any
|
||||
err := s.runningHubJSON(withProviderRequestKind(ctx, "poll"), config, root+"/task/openapi/outputs", map[string]any{"apiKey": runningHubAPIKey(config), "taskId": taskID}, &response)
|
||||
if err != nil {
|
||||
return videoPollOutcome{}, fmt.Errorf("RunningHub 查询任务失败:%w", err)
|
||||
}
|
||||
code, validCode := runningHubPayloadCode(response)
|
||||
if !validCode {
|
||||
validCode = len(runningHubOutputURLs(response["data"])) > 0
|
||||
code = 0
|
||||
}
|
||||
if !validCode {
|
||||
return videoPollOutcome{}, errors.New("RunningHub 查询响应缺少可识别状态")
|
||||
}
|
||||
if code == 0 {
|
||||
urls := runningHubOutputURLs(response["data"])
|
||||
if len(urls) == 0 {
|
||||
return videoPollOutcome{}, errors.New("RunningHub 任务成功但没有返回产物")
|
||||
}
|
||||
for index, rawURL := range urls {
|
||||
urls[index] = resolveRunningHubOutputURL(root, rawURL)
|
||||
}
|
||||
result, err := s.downloadWorkflowVideoOutputs(ctx, urls, taskID, policy)
|
||||
if err != nil {
|
||||
return videoPollOutcome{}, err
|
||||
}
|
||||
_ = s.updateWorkflowProviderState(ctx, taskID, "succeeded", nil)
|
||||
return videoPollOutcome{Done: true, Result: result}, nil
|
||||
}
|
||||
if code == 805 || code == 806 {
|
||||
return videoPollOutcome{}, fmt.Errorf("RunningHub 任务失败:%s", runningHubFailureMessage(response))
|
||||
}
|
||||
stage := "running"
|
||||
if code == 813 {
|
||||
stage = "queued"
|
||||
} else if code != 804 {
|
||||
stage = "pending"
|
||||
}
|
||||
next := time.Now().Add(policy.Interval)
|
||||
_ = s.updateWorkflowProviderState(ctx, taskID, stage, &next)
|
||||
return videoPollOutcome{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) pollRunningHubWorkflowLegacy(ctx context.Context, config providerConfig, root string, taskID string) (map[string]interface{}, error) {
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
var response map[string]any
|
||||
err := s.runningHubJSON(withProviderRequestKind(ctx, "poll"), config, root+"/task/openapi/outputs", map[string]any{"apiKey": runningHubAPIKey(config), "taskId": taskID}, &response)
|
||||
@@ -1692,6 +1744,14 @@ func runningHubAPIKey(config providerConfig) string {
|
||||
}
|
||||
|
||||
func (s *Service) downloadWorkflowOutputs(ctx context.Context, urls []string) (map[string]interface{}, error) {
|
||||
return s.downloadWorkflowOutputsWithPolicy(ctx, urls, "", nil)
|
||||
}
|
||||
|
||||
func (s *Service) downloadWorkflowVideoOutputs(ctx context.Context, urls []string, taskID string, policy videoPollPolicy) (map[string]interface{}, error) {
|
||||
return s.downloadWorkflowOutputsWithPolicy(ctx, urls, taskID, &policy)
|
||||
}
|
||||
|
||||
func (s *Service) downloadWorkflowOutputsWithPolicy(ctx context.Context, urls []string, taskID string, policy *videoPollPolicy) (map[string]interface{}, error) {
|
||||
images := make([]map[string]interface{}, 0)
|
||||
var video, audio map[string]interface{}
|
||||
for _, rawURL := range urls {
|
||||
@@ -1717,7 +1777,16 @@ func (s *Service) downloadWorkflowOutputs(ctx context.Context, urls []string) (m
|
||||
if !isPublicMediaURL(rawURL) {
|
||||
continue
|
||||
}
|
||||
data, mimeType, err := getExternalBinary(withProviderRequestKind(ctx, "download"), rawURL)
|
||||
var data []byte
|
||||
var mimeType string
|
||||
var err error
|
||||
if policy == nil {
|
||||
data, mimeType, err = getExternalBinary(withProviderRequestKind(ctx, "download"), rawURL)
|
||||
} else {
|
||||
data, mimeType, err = runVideoDownload(ctx, taskID, *policy, func(ctx context.Context) ([]byte, string, error) {
|
||||
return getExternalBinary(withProviderRequestKind(ctx, "download"), rawURL)
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("下载 RunningHub 产物失败:%w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunningHubVideoPollRecoversFromTransientGatewayFailure(t *testing.T) {
|
||||
allowLoopbackProviderTest(t)
|
||||
pollCalls := 0
|
||||
var server *httptest.Server
|
||||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/task/openapi/outputs":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if pollCalls == 1 {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"code":0,"data":[{"fileUrl":"` + server.URL + `/result.mp4"}]}`))
|
||||
case "/result.mp4":
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL, APIKey: "test-key"}
|
||||
ctx := context.Background()
|
||||
result, err := (&Service{}).pollRunningHubVideoWorkflowWithPolicy(ctx, config, server.URL, "provider-task-1", fastVideoPollPolicy())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || pollCalls != 2 {
|
||||
t.Fatalf("result = %#v, poll calls = %d", result, pollCalls)
|
||||
}
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func DefaultRuntimePolicy() RuntimePolicySetting {
|
||||
ImageTimeoutMinutes: 8,
|
||||
TextTimeoutMinutes: 8,
|
||||
AudioTimeoutMinutes: 8,
|
||||
VideoTimeoutMinutes: 30,
|
||||
VideoTimeoutMinutes: 60,
|
||||
StoryboardTimeoutMinutes: 20,
|
||||
DefaultTimeoutMinutes: 10,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user