fix(video): NewAPI Channel 2 - 修复轮询与结果解析
创建成功后的短暂 task_not_exist 按退避策略重试,并在短期窗口耗尽后保留 running 状态,继续回查原 Provider Task ID。 补齐嵌套任务 ID 与视频结果地址解析,更新生成插件、协议文档和专项回归测试。
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -14,6 +14,8 @@ docs/content/docs/*
|
||||
!docs/content/docs/plugins/
|
||||
docs/content/docs/plugins/*
|
||||
!docs/content/docs/plugins/ai-art-critique.mdx
|
||||
!docs/content/docs/backend/
|
||||
!docs/content/docs/backend/newapi-channel-2.mdx
|
||||
.env*
|
||||
!.env.example
|
||||
data/
|
||||
|
||||
@@ -377,6 +377,55 @@ func TestOfficialRollDekWanVideoUsesVideosLifecycle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIVideoGenerationsParsesNestedTaskIDs(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "newapi-video-generations-v1.yingce-plugin", "newapi-channel-2")
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
wantID string
|
||||
}{
|
||||
{name: "snake case", payload: `{"data":{"task_id":"task-snake","status":"queued"}}`, wantID: "task-snake"},
|
||||
{name: "camel case", payload: `{"data":{"taskId":"task-camel","status":"queued"}}`, wantID: "task-camel"},
|
||||
{name: "nested upstream id wins over wrapper id", payload: `{"id":"49137","data":{"task_id":"task-upstream","status":"queued"}}`, wantID: "task-upstream"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
created, err := adapter.ParseCreate(context.Background(), []byte(test.payload))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if created.TaskID != test.wantID || created.Status != StatusPending {
|
||||
t.Fatalf("created = %#v, want task ID %q and pending status", created, test.wantID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIVideoGenerationsParsesNestedVideoResults(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "newapi-video-generations-v1.yingce-plugin", "newapi-channel-2")
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
}{
|
||||
{name: "channel result URL", payload: `{"code":"success","data":{"task_id":"task-upstream","status":"SUCCESS","result_url":"https://cdn.example/channel-result.mp4"}}`},
|
||||
{name: "provider nested video URL", payload: `{"code":"success","data":{"task_id":"task-upstream","status":"SUCCESS","data":{"status":"completed","video_url":"https://cdn.example/provider-result.mp4"}}}`},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
state, err := adapter.ParsePoll(context.Background(), PollContext{TaskID: "task-upstream"}, []byte(test.payload))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if state.Status != StatusSucceeded || state.Result == nil || len(state.Result.Videos) != 1 {
|
||||
t.Fatalf("state = %#v, want one completed video", state)
|
||||
}
|
||||
if state.Result.Videos[0].URL == "" {
|
||||
t.Fatalf("video = %#v, want a result URL", state.Result.Videos[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficialOpenAIVideosDeclaresAuthenticatedResultDownload(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "openai-videos.yingce-plugin", "newapi")
|
||||
capability, ok := adapter.(ResultCapability)
|
||||
|
||||
@@ -1000,7 +1000,11 @@ func (s *Service) enrichAPICallLogFailureSummary(log *model.ApiCallLog, response
|
||||
}
|
||||
|
||||
func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[string]any) {
|
||||
nestedTaskID := ""
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/v1/video/generations") {
|
||||
nestedTaskID = firstNonEmpty(stringField(data, "task_id"), stringField(data, "taskId"))
|
||||
}
|
||||
for key, value := range data {
|
||||
if _, exists := payload[key]; !exists {
|
||||
payload[key] = value
|
||||
@@ -1064,7 +1068,7 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
}
|
||||
log.CachedTokens = firstInt64(usageMetadata, "cachedContentTokenCount")
|
||||
}
|
||||
log.ProviderRequestID = firstNonEmpty(stringField(payload, "task_id"), stringField(payload, "id"), stringField(payload, "request_id"), stringField(payload, "name"), log.ProviderRequestID)
|
||||
log.ProviderRequestID = firstNonEmpty(nestedTaskID, stringField(payload, "task_id"), stringField(payload, "id"), stringField(payload, "request_id"), stringField(payload, "name"), log.ProviderRequestID)
|
||||
log.ProviderStatus = strings.ToLower(firstNonEmpty(stringField(payload, "status"), log.ProviderStatus))
|
||||
if log.ProviderStatus == "failed" || log.ProviderStatus == "cancelled" || log.ProviderStatus == "expired" {
|
||||
log.Status = model.ApiCallStatusFailed
|
||||
|
||||
@@ -86,6 +86,25 @@ func TestEnrichAPICallLogReadsArkVideoUsage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogPrefersNewAPIChannel2NestedTaskID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
}{
|
||||
{name: "snake case", payload: `{"id":"wrapper-id","data":{"task_id":"task-snake"}}`},
|
||||
{name: "camel case", payload: `{"id":"wrapper-id","data":{"taskId":"task-camel"}}`},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
log := &model.ApiCallLog{Capability: "video", Path: "/v1/video/generations"}
|
||||
(&Service{}).EnrichAPICallLog(log, []byte(test.payload))
|
||||
if !strings.HasPrefix(log.ProviderRequestID, "task-") {
|
||||
t.Fatalf("ProviderRequestID = %q, want nested upstream task ID", log.ProviderRequestID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogKeepsUserFacingAndUpstreamFailureDetails(t *testing.T) {
|
||||
log := &model.ApiCallLog{
|
||||
Status: model.ApiCallStatusFailed,
|
||||
|
||||
@@ -5,12 +5,14 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/protocol"
|
||||
)
|
||||
@@ -342,6 +344,129 @@ func TestDeclarativeProtocolRuntimeExecutesCreatePollAndDownload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistRetry(t *testing.T) {
|
||||
adapter := newDeclarativeNewAPIChannel2TestAdapter(t)
|
||||
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/video/generations":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/video/generations/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if pollCalls <= 2 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"code":"task_not_exist","data":null,"message":"task_not_exist"}`))
|
||||
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: "newapi-channel-2", AllowLocalChannel: true}
|
||||
ctx := withProviderOutboundPolicy(context.Background(), config)
|
||||
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},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || createCalls != 1 || pollCalls != 3 {
|
||||
t.Fatalf("result = %#v, create calls = %d, poll calls = %d", result, createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistExhaustion(t *testing.T) {
|
||||
adapter := newDeclarativeNewAPIChannel2TestAdapter(t)
|
||||
createCalls := 0
|
||||
pollCalls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/video/generations":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/video/generations/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"code":"task_not_exist","message":"task_not_exist"}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "newapi-channel-2", AllowLocalChannel: true}
|
||||
ctx := withProviderOutboundPolicy(context.Background(), config)
|
||||
_, 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
|
||||
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 createCalls != 1 || pollCalls != 5 {
|
||||
t.Fatalf("create calls = %d, poll calls = %d, want 1 and 5", createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistStrictClassification(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
interfaceType string
|
||||
taskID string
|
||||
err error
|
||||
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"}`}},
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newDeclarativeNewAPIChannel2TestAdapter(t *testing.T) protocol.Adapter {
|
||||
t.Helper()
|
||||
adapter, err := protocol.LoadManifest([]byte(`{
|
||||
"apiVersion":"yingce.plugin/v1",
|
||||
"id":"newapi-channel-2","version":"1.0.0","name":"NewAPI Channel 2","author":"Test","documentation":"# Test",
|
||||
"contributes":{"providers":[{"id":"newapi-channel-2","label":"NewAPI Channel 2","capabilities":["video"],"scopes":["canvas"],"create":{"method":"POST","path":"/video/generations","fields":{"model":"request.model"}},"poll":{"method":"GET","path":"/video/generations/{{taskId}}"},"response":{"taskIdPaths":["id"],"statusPaths":["status"],"resultPaths":["video_url"],"resultKind":"video"}}]}
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return adapter
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolRecoveryQueriesExistingTaskWithoutCreating(t *testing.T) {
|
||||
manifest := []byte(`{
|
||||
"apiVersion":"yingce.plugin/v2",
|
||||
|
||||
@@ -150,6 +150,17 @@ type providerHTTPError struct {
|
||||
RetryAfter time.Duration
|
||||
}
|
||||
|
||||
type providerStatePendingError struct {
|
||||
TaskID string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e providerStatePendingError) Error() string {
|
||||
return fmt.Sprintf("上游任务状态尚未同步,将继续查询原任务(任务 %s)", e.TaskID)
|
||||
}
|
||||
|
||||
func (e providerStatePendingError) Unwrap() error { return e.Cause }
|
||||
|
||||
type providerAnalyticsKey struct{}
|
||||
type providerOutboundPolicyKey struct{}
|
||||
|
||||
@@ -2297,10 +2308,32 @@ 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)
|
||||
}
|
||||
|
||||
func runProtocolAdapterTaskWithTiming(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, timing protocolPollTiming) (map[string]interface{}, error) {
|
||||
request := protocolRequestFromInput(input)
|
||||
taskID := resumedProviderRequestID(ctx)
|
||||
var created protocol.CreateResult
|
||||
createdProviderTask := false
|
||||
syncWindowStartedAt := time.Now()
|
||||
if taskID == "" {
|
||||
spec, err := adapter.BuildCreate(ctx, protocol.RequestContext{BaseURL: input.Config.BaseURL, Request: request})
|
||||
if err != nil {
|
||||
@@ -2324,8 +2357,16 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
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); {
|
||||
spec, err := adapter.BuildPoll(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID})
|
||||
if err != nil {
|
||||
@@ -2333,8 +2374,26 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
}
|
||||
body, err := executeProtocolRequest(withProviderRequestKind(ctx, "poll"), input.Config, spec)
|
||||
if err != nil {
|
||||
if isNewAPIChannel2TaskNotReady(input.Config.InterfaceType, taskID, err) {
|
||||
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
|
||||
}
|
||||
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
|
||||
@@ -2348,13 +2407,29 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
case protocol.StatusFailed, protocol.StatusCancelled:
|
||||
return nil, protocolResultError(state.Message, taskID)
|
||||
}
|
||||
if err := sleepContext(ctx, 2500*time.Millisecond); err != nil {
|
||||
if err := sleepContext(ctx, timing.PollInterval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("声明式协议任务超时(任务 %s)", taskID)
|
||||
}
|
||||
|
||||
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 performs exactly one read of an existing
|
||||
// declarative provider task. Manual recovery uses this path so it can never
|
||||
// create a second billable generation while checking a failed local task.
|
||||
|
||||
@@ -35,12 +35,22 @@ func TestOnlyResumableNewAPIChannel2VideoDeadlinesStayRunning(t *testing.T) {
|
||||
if !svc.shouldDeferVideoProviderTask(base, string(input), context.DeadlineExceeded) {
|
||||
t.Fatal("resumable NewAPI Channel 2 deadline should remain running")
|
||||
}
|
||||
pendingErr := providerStatePendingError{TaskID: base.ProviderRequestID, Cause: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}}
|
||||
if !svc.shouldDeferVideoProviderTask(base, string(input), pendingErr) {
|
||||
t.Fatal("resumable NewAPI Channel 2 provider sync delay should remain running")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(model.Task{ID: "task-2", Type: "canvas_video"}, string(input), context.DeadlineExceeded) {
|
||||
t.Fatal("missing provider task id must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(model.Task{ID: "task-2", Type: "canvas_video"}, string(input), pendingErr) {
|
||||
t.Fatal("provider sync delay without persisted provider task id must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(base, string(input), context.Canceled) {
|
||||
t.Fatal("explicit cancellation must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(base, string(input), providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}) {
|
||||
t.Fatal("untyped provider error must not be deferred")
|
||||
}
|
||||
other, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://example.com", InterfaceType: string(model.ChannelInterfaceNewAPIVideo)}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -180,15 +180,22 @@ func (w *taskWorkerCoordinator) processClaimedTask(task *model.Task) error {
|
||||
return leaseErr
|
||||
default:
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
decryptedInput, decryptErr := s.decryptTaskInputJSON(task.InputJSON)
|
||||
if decryptErr == nil && s.shouldDeferVideoProviderTask(*task, decryptedInput, err) {
|
||||
if deferErr := s.repo.DeferRunningTaskForProviderPoll(task.ID, task.LeaseOwner, "后台仍在生成", 15*time.Second); deferErr != nil {
|
||||
return deferErr
|
||||
}
|
||||
_ = s.log(task.UserID, task.ID, "info", "前台等待结束,上游视频仍在生成,将继续回查原任务", task.PollStage)
|
||||
return nil
|
||||
decryptedInput, decryptErr := s.decryptTaskInputJSON(task.InputJSON)
|
||||
if decryptErr == nil && s.shouldDeferVideoProviderTask(*task, decryptedInput, err) {
|
||||
stage := "后台仍在生成"
|
||||
message := "前台等待结束,上游视频仍在生成,将继续回查原任务"
|
||||
var pendingErr providerStatePendingError
|
||||
if errors.As(err, &pendingErr) {
|
||||
stage = "等待上游任务同步"
|
||||
message = "上游任务状态暂未同步,将继续回查原任务"
|
||||
}
|
||||
if deferErr := s.repo.DeferRunningTaskForProviderPoll(task.ID, task.LeaseOwner, stage, 15*time.Second); deferErr != nil {
|
||||
return deferErr
|
||||
}
|
||||
_ = s.log(task.UserID, task.ID, "info", message, task.PollStage)
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
err = errors.New(taskTimeoutMessage(task.Type))
|
||||
}
|
||||
return terminal.handleExecutionFailure(task, err, providerSucceeded, channelSlotFailedBeforeRequest)
|
||||
@@ -252,7 +259,16 @@ func taskExecutionTimeoutWithPolicy(taskType string, policy RuntimeTaskPolicy) t
|
||||
}
|
||||
|
||||
func (s *Service) shouldDeferVideoProviderTask(task model.Task, decryptedInput string, err error) bool {
|
||||
if !errors.Is(err, context.DeadlineExceeded) || strings.TrimSpace(task.ProviderRequestID) == "" || (!strings.HasPrefix(task.Type, "canvas_video") && !strings.HasPrefix(task.Type, "video_")) {
|
||||
providerRequestID := strings.TrimSpace(task.ProviderRequestID)
|
||||
if providerRequestID == "" || (!strings.HasPrefix(task.Type, "canvas_video") && !strings.HasPrefix(task.Type, "video_")) {
|
||||
return false
|
||||
}
|
||||
deferSignal := errors.Is(err, context.DeadlineExceeded)
|
||||
var pendingErr providerStatePendingError
|
||||
if errors.As(err, &pendingErr) {
|
||||
deferSignal = strings.TrimSpace(pendingErr.TaskID) == providerRequestID
|
||||
}
|
||||
if !deferSignal {
|
||||
return false
|
||||
}
|
||||
var input canvasGenerationInput
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"local-development",
|
||||
"code-map",
|
||||
"backend-database",
|
||||
"newapi-channel-2",
|
||||
"system-update",
|
||||
"protocol-plugins",
|
||||
"canvas-data-structure"
|
||||
|
||||
49
docs/content/docs/backend/newapi-channel-2.mdx
Normal file
49
docs/content/docs/backend/newapi-channel-2.mdx
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: NewAPI Video Generations Channel 2
|
||||
description: Channel 2 视频任务创建、轮询与上游任务同步延迟处理
|
||||
---
|
||||
|
||||
# NewAPI Video Generations Channel 2
|
||||
|
||||
## 请求流程
|
||||
|
||||
声明式插件使用以下接口:
|
||||
|
||||
- 创建:`POST /v1/video/generations`
|
||||
- 查询:`GET /v1/video/generations/{taskId}`
|
||||
|
||||
创建响应支持顶层 `id`、`task_id`、`taskId`,以及 `data.id`、`data.task_id`、`data.taskId`。当中转包装 ID 与真实上游任务 ID 同时存在时,按 `data.task_id`、`data.taskId`、顶层 `task_id`、顶层 `taskId`、`data.id`、顶层 `id` 的顺序解析,避免把中转内部记录 ID 用作上游查询 ID。宿主把解析出的值保存为 `provider_request_id`,后续查询始终使用该值,并对路径参数进行 URL 编码。
|
||||
|
||||
查询响应进入成功状态后,插件会从 `data.result_url`、`data.video_url`、`data.output_url`、`data.url`、`data.metadata.url` 以及对应的 `data.data.*` 嵌套字段读取视频地址,再回退到兼容的顶层结果字段。只返回成功状态而没有任何结果地址时,宿主仍按协议异常处理,不会创建空资源。
|
||||
|
||||
## 上游同步延迟
|
||||
|
||||
创建接口返回任务 ID 后,Channel 2 可能在数百毫秒到数秒内仍对查询接口返回结构化的 `400`:
|
||||
|
||||
```json
|
||||
{"code":"task_not_exist","message":"task_not_exist"}
|
||||
```
|
||||
|
||||
宿主只在同时满足以下条件时把该响应视为“任务状态尚未同步”:
|
||||
|
||||
1. 渠道接口类型精确为 `newapi-channel-2`;
|
||||
2. 已经保存非空 Provider Task ID;
|
||||
3. HTTP 状态为 `400`;
|
||||
4. JSON 的 `code` 或 `message` 精确等于 `task_not_exist`。
|
||||
|
||||
不符合条件的 HTTP 错误、非 JSON 正文或其他错误码仍立即按失败语义处理。
|
||||
|
||||
## 轮询与延期
|
||||
|
||||
新任务首次查询前等待 `2` 秒;同步错误在同一次执行中按 `2` 秒、`3` 秒、`5` 秒、`5` 秒退避,最多短期查询 5 次。短期窗口耗尽后,Worker 不发送新的创建请求,也不切换渠道或退款,而是:
|
||||
|
||||
- 保持本地任务状态为 `running`;
|
||||
- 保留原 `provider_request_id` 和计费状态;
|
||||
- 释放当前租约并把 `next_poll_at` 延后 15 秒;
|
||||
- 下一轮从已保存的 Provider Task ID 恢复,只发送查询请求。
|
||||
|
||||
因此,上游生成耗时 500 至 600 秒时,本地会持续以低频查询等待完成,不会把正常排队误报为“模型服务拒绝了请求”。视频真正返回失败状态时,仍按供应商失败处理。
|
||||
|
||||
## 兼容边界
|
||||
|
||||
该容错只适用于 `newapi-channel-2` 视频任务。其他 NewAPI 接口、其他 Provider、缺少 Provider Task ID 的任务,以及真实参数错误不进入延期路径。
|
||||
@@ -5,6 +5,14 @@ description: 当前项目已实现功能的手工验证清单与未确认边界
|
||||
|
||||
# 待测试
|
||||
|
||||
## NewAPI Video Generations Channel 2 轮询恢复
|
||||
|
||||
- [x] 官方插件兼容从 `data.task_id` 和 `data.taskId` 读取上游任务 ID;Channel 2 创建成功后首次查询等待 2 秒,仅将结构化 `400 task_not_exist` 识别为上游同步延迟,并按 2 秒、3 秒、5 秒、5 秒退避复查同一任务。
|
||||
- [x] 短期重试耗尽后,Worker 保留本地任务 `running`、原 `provider_request_id` 和计费状态,延后 15 秒继续查询;恢复过程不重复发送创建请求、不切换渠道,也不立即失败或退款。其他接口类型、HTTP 状态和错误码仍按原失败语义处理。
|
||||
- [x] Channel 2 终态响应兼容 `data.result_url`、`data.video_url`、`data.output_url`、`data.url`、`data.metadata.url` 及对应的 `data.data.*` 嵌套路径;API 调用日志在该接口下优先记录嵌套真实任务 ID。协议与日志富化专项回归测试通过。
|
||||
- [ ] 使用真实 Channel 2 分别验证上游任务 ID 短暂不可见和生成耗时 500 至 600 秒的任务:本地不再提示“模型服务拒绝了请求”,创建请求只出现一次,后续始终查询同一 Provider Task ID,最终视频正常回写;真实参数错误仍应及时失败。
|
||||
- [ ] 使用返回 `SUCCESS` 且视频地址位于 `data.result_url` 或 `data.data.video_url` 的真实任务,确认不再提示“声明式协议已完成但没有返回结果”,并在当前 OSS 配置下成功下载并持久化为本地资源。
|
||||
|
||||
## 编辑器(M2 命令状态机)
|
||||
|
||||
- [x] 命令协议黄金文件:`editor-commands` 8 个黄金 op(addClip/moveClip/trimClip/splitClip/removeClip/setClipProperty/addSubtitle/removeSubtitle)全量快照比对通过;未知 op、非法 payload 一律抛错(fail-closed)且输入不可变。
|
||||
|
||||
@@ -357,7 +357,15 @@ add({
|
||||
video_urls: omit(map({ $sortByOrder: ref("request.videos") }, "media", ref("media.value"))),
|
||||
audio_urls: omit(map({ $sortByOrder: ref("request.audios") }, "media", ref("media.value")))
|
||||
}),
|
||||
poll: { method: "GET", path: "/v1/video/generations/{{taskId}}" }, response: asyncResponse("video")
|
||||
poll: { method: "GET", path: "/v1/video/generations/{{taskId}}" },
|
||||
response: asyncResponse("video", {
|
||||
taskId: coalesce(ref("response.data.task_id"), ref("response.data.taskId"), ref("response.task_id"), ref("response.taskId"), ref("response.data.id"), ref("response.id"), ref("taskId")),
|
||||
videos: coalesce(
|
||||
ref("response.data.result_url"), ref("response.data.video_url"), ref("response.data.output_url"), ref("response.data.url"), ref("response.data.metadata.url"),
|
||||
ref("response.data.data.video_url"), ref("response.data.data.output_url"), ref("response.data.data.result_url"), ref("response.data.data.url"), ref("response.data.data.metadata.url"),
|
||||
ref("response.video_url"), ref("response.videoUrl"), ref("response.result_url"), ref("response.output_url"), ref("response.url"), ref("response.metadata.url"), ref("response.output.url")
|
||||
)
|
||||
})
|
||||
});
|
||||
|
||||
add({
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user