diff --git a/packages/agent/src/workflows/agent-embedding.test.ts b/packages/agent/src/workflows/agent-embedding.test.ts index f628ff47..c409159b 100644 --- a/packages/agent/src/workflows/agent-embedding.test.ts +++ b/packages/agent/src/workflows/agent-embedding.test.ts @@ -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, + }), + ) + }) }) diff --git a/packages/agent/src/workflows/agent-embedding.ts b/packages/agent/src/workflows/agent-embedding.ts index 17479843..07f77f50 100644 --- a/packages/agent/src/workflows/agent-embedding.ts +++ b/packages/agent/src/workflows/agent-embedding.ts @@ -154,7 +154,9 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise { 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 { key: chunkKey, }) } + + // If this chunk reached the end of the video, stop to avoid redundant overlapping chunks + if (end >= duration) { + break + } } }