feat: add Seedance 2.0 models

This commit is contained in:
saturn
2026-04-02 19:16:00 +08:00
parent 9703714b69
commit 71ef6ff818
21 changed files with 811 additions and 38 deletions

View File

@@ -393,11 +393,32 @@ const DIRECT_CASES: ReadonlyArray<DirectRouteCase> = [
},
{
routeFile: 'src/app/api/novel-promotion/[projectId]/generate-video/route.ts',
body: { videoModel: 'fal::video-model', storyboardId: 'storyboard-1', panelIndex: 0 },
body: {
videoModel: 'ark::doubao-seedance-2-0-260128',
storyboardId: 'storyboard-1',
panelIndex: 0,
generationOptions: {
resolution: '720p',
duration: 5,
},
firstLastFrame: {
flModel: 'ark::doubao-seedance-2-0-260128',
},
},
params: { projectId: 'project-1' },
expectedTaskType: TASK_TYPE.VIDEO_PANEL,
expectedTargetType: 'NovelPromotionPanel',
expectedProjectId: 'project-1',
expectedPayloadSubset: {
videoModel: 'ark::doubao-seedance-2-0-260128',
generationOptions: {
resolution: '720p',
duration: 5,
},
firstLastFrame: {
flModel: 'ark::doubao-seedance-2-0-260128',
},
},
},
{
routeFile: 'src/app/api/novel-promotion/[projectId]/insert-panel/route.ts',

View File

@@ -0,0 +1,105 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { arkCreateVideoTask } from '@/lib/ark-api'
import { querySeedanceVideoStatus } from '@/lib/async-task-utils'
describe('provider contract - ark seedance', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.unstubAllGlobals()
})
it('submits Seedance 2.0 multimodal create payload with official request fields', async () => {
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ id: 'cgt-task-1' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}))
vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch)
const result = await arkCreateVideoTask({
model: 'doubao-seedance-2-0-260128',
content: [
{ type: 'text', text: 'reference 视频1 的运镜参考音频1 的节奏' },
{ type: 'image_url', image_url: { url: 'https://example.com/first.png' }, role: 'reference_image' },
{ type: 'video_url', video_url: { url: 'https://example.com/ref.mp4' }, role: 'reference_video' },
{ type: 'audio_url', audio_url: { url: 'https://example.com/ref.mp3' }, role: 'reference_audio' },
],
resolution: '720p',
ratio: '16:9',
duration: 15,
generate_audio: true,
watermark: true,
tools: [{ type: 'web_search' }],
}, {
apiKey: 'ark-key',
maxRetries: 1,
timeoutMs: 1000,
logPrefix: '[Ark Test]',
})
expect(result.id).toBe('cgt-task-1')
expect(fetchMock).toHaveBeenCalledTimes(1)
const firstCall = fetchMock.mock.calls[0]
expect(firstCall).toBeTruthy()
const [url, init] = firstCall as unknown as [string, RequestInit]
expect(url).toBe('https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks')
expect(init.method).toBe('POST')
expect(init.headers).toEqual({
'Content-Type': 'application/json',
'Authorization': 'Bearer ark-key',
})
expect(JSON.parse(String(init.body))).toEqual({
model: 'doubao-seedance-2-0-260128',
content: [
{ type: 'text', text: 'reference 视频1 的运镜参考音频1 的节奏' },
{ type: 'image_url', image_url: { url: 'https://example.com/first.png' }, role: 'reference_image' },
{ type: 'video_url', video_url: { url: 'https://example.com/ref.mp4' }, role: 'reference_video' },
{ type: 'audio_url', audio_url: { url: 'https://example.com/ref.mp3' }, role: 'reference_audio' },
],
resolution: '720p',
ratio: '16:9',
duration: 15,
generate_audio: true,
watermark: true,
tools: [{ type: 'web_search' }],
})
})
it('reads Ark task usage.total_tokens from status query', async () => {
const fetchMock = vi.fn(async () => new Response(JSON.stringify({
status: 'succeeded',
content: {
video_url: 'https://example.com/result.mp4',
},
usage: {
total_tokens: 108000,
completion_tokens: 108000,
},
}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}))
vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch)
const result = await querySeedanceVideoStatus('cgt-task-2', 'ark-key')
expect(result).toEqual({
status: 'completed',
videoUrl: 'https://example.com/result.mp4',
actualVideoTokens: 108000,
})
expect(fetchMock).toHaveBeenCalledWith(
'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks/cgt-task-2',
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ark-key',
},
cache: 'no-store',
},
)
})
})

View File

@@ -15,21 +15,30 @@ describe('api-config preset coming soon', () => {
expect(model?.name).toBe('Nano Banana 2')
})
it('registers Seedance 2.0 as a coming-soon preset model', () => {
const model = PRESET_MODELS.find(
(entry) => entry.provider === 'ark' && entry.modelId === 'doubao-seedance-2-0-260128',
)
expect(model).toBeDefined()
expect(model?.name).toContain('待上线')
it('registers Seedance 2.0 and Seedance 2.0 Fast as preset video models', () => {
const modelIds = PRESET_MODELS
.filter((entry) => entry.provider === 'ark' && entry.type === 'video')
.map((entry) => entry.modelId)
expect(modelIds).toEqual(expect.arrayContaining([
'doubao-seedance-2-0-260128',
'doubao-seedance-2-0-fast-260128',
]))
})
it('recognizes coming-soon model by provider/modelId and modelKey', () => {
it('does not mark live preset models as coming soon', () => {
const modelKey = encodeModelKey('ark', 'doubao-seedance-2-0-260128')
expect(isPresetComingSoonModel('ark', 'doubao-seedance-2-0-260128')).toBe(true)
expect(isPresetComingSoonModelKey(modelKey)).toBe(true)
expect(isPresetComingSoonModel('ark', 'doubao-seedance-2-0-260128')).toBe(false)
expect(isPresetComingSoonModelKey(modelKey)).toBe(false)
})
it('does not mark normal preset models as coming soon', () => {
const modelKey = encodeModelKey('ark', 'doubao-seedance-2-0-fast-260128')
expect(isPresetComingSoonModel('ark', 'doubao-seedance-2-0-fast-260128')).toBe(false)
expect(isPresetComingSoonModelKey(modelKey)).toBe(false)
})
it('keeps existing live preset models non-coming-soon', () => {
const modelKey = encodeModelKey('ark', 'doubao-seedance-1-5-pro-251215')
expect(isPresetComingSoonModel('ark', 'doubao-seedance-1-5-pro-251215')).toBe(false)
expect(isPresetComingSoonModelKey(modelKey)).toBe(false)

View File

@@ -5,6 +5,7 @@ import {
calcLipSync,
calcText,
calcVideo,
calcVideoByTokens,
calcVoice,
calcVoiceDesign,
} from '@/lib/billing/cost'
@@ -87,6 +88,37 @@ describe('billing/cost', () => {
})).toThrow('Unsupported video capability pricing')
})
it('estimates Seedance 2.0 video pricing from official token formula', () => {
const cost = calcVideo('doubao-seedance-2-0-260128', '720p', 1, {
resolution: '720p',
duration: 5,
aspectRatio: '16:9',
containsVideoInput: false,
})
expect(cost).toBeCloseTo(4.968, 8)
})
it('applies Seedance 2.0 video-input token floor for quoted pricing', () => {
const cost = calcVideo('doubao-seedance-2-0-fast-260128', '720p', 1, {
resolution: '720p',
duration: 5,
aspectRatio: '16:9',
containsVideoInput: true,
inputVideoSeconds: 2,
})
expect(cost).toBeCloseTo(4.2768, 8)
})
it('settles Seedance 2.0 videos from exact usage tokens', () => {
const cost = calcVideoByTokens('doubao-seedance-2-0-260128', 120_000, {
containsVideoInput: false,
})
expect(cost).toBeCloseTo(5.52, 8)
})
it('supports minimax capability-aware video pricing', () => {
const hailuoNormal = calcVideo('minimax-hailuo-2.3', '768p', 1, {
generationMode: 'normal',

View File

@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { calcText, calcVoice } from '@/lib/billing/cost'
import { calcText, calcVideo, calcVoice } from '@/lib/billing/cost'
import type { TaskBillingInfo } from '@/lib/task/types'
const ledgerMock = vi.hoisted(() => ({
@@ -230,6 +230,34 @@ describe('billing/service', () => {
}
}
function buildSeedance2VideoTaskInfo(
overrides: Partial<Extract<TaskBillingInfo, { billable: true }>> = {},
): Extract<TaskBillingInfo, { billable: true }> {
return {
billable: true,
source: 'task',
taskType: 'video_panel',
apiType: 'video',
model: 'doubao-seedance-2-0-260128',
quantity: 1,
unit: 'video',
maxFrozenCost: calcVideo('doubao-seedance-2-0-260128', '720p', 1, {
resolution: '720p',
duration: 5,
aspectRatio: '16:9',
containsVideoInput: false,
}),
action: 'video_panel_generate',
metadata: {
resolution: '720p',
duration: 5,
aspectRatio: '16:9',
containsVideoInput: false,
},
...overrides,
}
}
it('prepareTaskBilling handles OFF/SHADOW/ENFORCE paths', async () => {
modeMock.getBillingMode.mockResolvedValueOnce('OFF')
const off = await prepareTaskBilling({
@@ -468,6 +496,25 @@ describe('billing/service', () => {
expect((settled as Extract<TaskBillingInfo, { billable: true }>).chargedCost).toBeCloseTo(calcVoice(50), 8)
})
it('settleTaskBilling charges Seedance 2.0 videos from exact usage tokens', async () => {
ledgerMock.confirmChargeWithRecord.mockResolvedValueOnce(true)
const settled = await settleTaskBilling({
id: 'task_seedance2_actual_tokens',
userId: 'u1',
projectId: 'p1',
billingInfo: buildSeedance2VideoTaskInfo({
modeSnapshot: 'ENFORCE',
freezeId: 'freeze_seedance2_actual_tokens',
}),
}, {
result: { actualVideoTokens: 120_000 },
})
expect(ledgerMock.increasePendingFreezeAmount).toHaveBeenCalledTimes(1)
expect((settled as Extract<TaskBillingInfo, { billable: true }>).chargedCost).toBeCloseTo(5.52, 8)
})
it('settleTaskBilling keeps quoted charge when text usage has no token counts', async () => {
const quoted = calcText('anthropic/claude-sonnet-4', 500, 500)
const textBillingInfo: Extract<TaskBillingInfo, { billable: true }> = {

View File

@@ -0,0 +1,53 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const getProviderConfigMock = vi.hoisted(() =>
vi.fn(async () => ({
id: 'ark',
apiKey: 'ark-key',
})),
)
const asyncTaskUtilsMock = vi.hoisted(() => ({
queryGeminiBatchStatus: vi.fn(),
queryGoogleVideoStatus: vi.fn(),
querySeedanceVideoStatus: vi.fn(),
}))
vi.mock('@/lib/api-config', () => ({
getProviderConfig: getProviderConfigMock,
getUserModels: vi.fn(),
}))
vi.mock('@/lib/async-submit', () => ({
queryFalStatus: vi.fn(),
}))
vi.mock('@/lib/async-task-utils', () => asyncTaskUtilsMock)
import { pollAsyncTask } from '@/lib/async-poll'
describe('async poll ark task', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('passes through actual video token usage from Ark polling', async () => {
asyncTaskUtilsMock.querySeedanceVideoStatus.mockResolvedValueOnce({
status: 'completed',
videoUrl: 'https://ark.example/result.mp4',
actualVideoTokens: 108000,
})
const result = await pollAsyncTask('ARK:VIDEO:task-1', 'user-1')
expect(getProviderConfigMock).toHaveBeenCalledWith('user-1', 'ark')
expect(asyncTaskUtilsMock.querySeedanceVideoStatus).toHaveBeenCalledWith('task-1', 'ark-key')
expect(result).toEqual({
status: 'completed',
resultUrl: 'https://ark.example/result.mp4',
videoUrl: 'https://ark.example/result.mp4',
actualVideoTokens: 108000,
error: undefined,
})
})
})

View File

@@ -27,7 +27,7 @@ const utilsMock = vi.hoisted(() => ({
assertTaskActive: vi.fn(async () => undefined),
getProjectModels: vi.fn(async () => ({ videoRatio: '16:9' })),
resolveLipSyncVideoSource: vi.fn(async () => 'https://provider.example/lipsync.mp4'),
resolveVideoSourceFromGeneration: vi.fn<(...args: unknown[]) => Promise<{ url: string; downloadHeaders?: Record<string, string> }>>(async () => ({ url: 'https://provider.example/video.mp4' })),
resolveVideoSourceFromGeneration: vi.fn<(...args: unknown[]) => Promise<{ url: string; actualVideoTokens?: number; downloadHeaders?: Record<string, string> }>>(async () => ({ url: 'https://provider.example/video.mp4' })),
toSignedUrlIfCos: vi.fn((url: string | null) => (url ? `https://signed.example/${url}` : null)),
uploadVideoSourceToCos: vi.fn(async () => 'cos/lip-sync/video.mp4'),
}))
@@ -196,6 +196,34 @@ describe('worker video processor behavior', () => {
)
})
it('VIDEO_PANEL: 将 Ark 返回的实际视频 token 用量透传到任务结果', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()
utilsMock.resolveVideoSourceFromGeneration.mockResolvedValueOnce({
url: 'https://provider.example/video.mp4',
actualVideoTokens: 108000,
})
const job = buildJob({
type: TASK_TYPE.VIDEO_PANEL,
payload: {
videoModel: 'ark::doubao-seedance-2-0-260128',
generationOptions: {
duration: 5,
resolution: '720p',
},
},
})
const result = await processor!(job) as { panelId: string; videoUrl: string; actualVideoTokens: number }
expect(result).toEqual({
panelId: 'panel-1',
videoUrl: 'cos/lip-sync/video.mp4',
actualVideoTokens: 108000,
})
})
it('LIP_SYNC: 缺少 panel 时显式失败', async () => {
const processor = workerState.processor
expect(processor).toBeTruthy()