fix(agent): prevent redundant final video chunks in embedding workflow

This commit is contained in:
yiling
2026-06-18 19:57:01 +08:00
parent 718d9d5b51
commit a197c83580
2 changed files with 44 additions and 1 deletions
@@ -395,4 +395,40 @@ describe('Agent Embedding Workflow', () => {
output: { error: 'AI Service Unavailable' },
})
})
it('should not create redundant chunks when video exactly matches chunk duration', async () => {
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
agent: { id: 'b1' },
asset: {
id: 'a1',
mediaType: 'video/mp4',
storageKey: { key: 'test.mp4' },
media: {
duration: 60.0,
},
},
chunkDuration: 60.0,
chunkOverlap: 5.0,
})
const task = await prisma.workflowTask.create({
data: {
type: 'ai_embedding',
status: 'pending',
assetId: 'a1',
teamId: 't1',
},
})
await agentEmbeddingMedia(task)
// Verify only 1 chunk was created instead of 2 (0-60 and 55-60)
expect(mockActivities.transcodeVideoChunkActivity).toHaveBeenCalledTimes(1)
expect(mockActivities.transcodeVideoChunkActivity).toHaveBeenCalledWith(
expect.objectContaining({
startTime: 0,
endTime: 60,
}),
)
})
})
@@ -154,7 +154,9 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
for (let start = 0.0; start < duration; start += step) {
let end = start + limit
if (end > duration) end = duration
if (end >= duration) {
end = duration
}
// Step A: Slice chunk on transcode worker and upload to S3
const chunkRes = await executeActivity(transcodeWorkerQueue, transcodeVideoChunkActivity, {
@@ -192,6 +194,11 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
key: chunkKey,
})
}
// If this chunk reached the end of the video, stop to avoid redundant overlapping chunks
if (end >= duration) {
break
}
}
}