From 07cb648183182237b001c057178600afd6a5c893 Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 30 Aug 2026 15:44:59 +0800 Subject: [PATCH] fix(agent): use total_frames for video and total_pages for pdf in asset context --- .../src/context/serialize-context.test.ts | 20 +++++ .../agent/src/workflows/agent-chat.test.ts | 83 +++++++++++++++++++ packages/agent/src/workflows/agent-chat.ts | 6 +- packages/dtos/src/chat.ts | 1 + packages/dtos/src/context.ts | 3 + 5 files changed, 112 insertions(+), 1 deletion(-) diff --git a/packages/agent/src/context/serialize-context.test.ts b/packages/agent/src/context/serialize-context.test.ts index 3006de46..11f1b52a 100644 --- a/packages/agent/src/context/serialize-context.test.ts +++ b/packages/agent/src/context/serialize-context.test.ts @@ -134,6 +134,26 @@ describe('serializeContextToXml', () => { expect(serializeContextToXml(context)).toBe(expected) }) + it('serializes Video context with total_frames and duration_seconds', () => { + const context: ShumaiMessageContext = { + currentAsset: { + id: 'vid_1', + name: 'clip.mp4', + type: 'file', + mediaType: 'video', + mimeType: 'video/mp4', + durationSeconds: 9, + totalFrames: 268, + }, + } + + const expected = ` + +` + + expect(serializeContextToXml(context)).toBe(expected) + }) + it('serializes navigation context with navigated="true" and ancestors hierarchy', () => { const context: ShumaiMessageContext = { currentAsset: { diff --git a/packages/agent/src/workflows/agent-chat.test.ts b/packages/agent/src/workflows/agent-chat.test.ts index 2446d447..74f00699 100644 --- a/packages/agent/src/workflows/agent-chat.test.ts +++ b/packages/agent/src/workflows/agent-chat.test.ts @@ -651,4 +651,87 @@ describe('Agent Chat Workflow', () => { }), ) }) + + it('should set totalFrames for video assets and totalPages for pdf assets in currentAsset context', async () => { + mockActivities.getAssetActivity.mockImplementation(async (id: string) => { + if (id === 'video-asset-1') { + return { + id: 'video-asset-1', + project: { teamId: 't1' }, + type: 'file', + name: 'clip.mp4', + mediaType: 'video/mp4', + media: { proxyType: 'video', duration: 9, frames: 268 }, + parentId: 'f1', + } + } + if (id === 'pdf-asset-1') { + return { + id: 'pdf-asset-1', + project: { teamId: 't1' }, + type: 'file', + name: 'doc.pdf', + mediaType: 'application/pdf', + media: { proxyType: 'pdf', frames: 15 }, + parentId: 'f1', + } + } + return null + }) + + // Test Video asset + const videoTask = await prisma.workflowTask.create({ + data: { + type: 'chat', + status: 'pending', + assetId: 'video-asset-1', + payload: { + projectId: 'p1', + agent: { prompt: 'Analyze video', agentId: 'b1', sessionId: 'session-video-1' }, + }, + }, + }) + await agentChat(videoTask) + + expect(mockActivities.agentChatActivity).toHaveBeenCalledWith( + expect.objectContaining({ + messageContext: expect.objectContaining({ + currentAsset: expect.objectContaining({ + id: 'video-asset-1', + mediaType: 'video', + durationSeconds: 9, + totalFrames: 268, + totalPages: undefined, + }), + }), + }), + ) + + // Test PDF asset + const pdfTask = await prisma.workflowTask.create({ + data: { + type: 'chat', + status: 'pending', + assetId: 'pdf-asset-1', + payload: { + projectId: 'p1', + agent: { prompt: 'Analyze pdf', agentId: 'b1', sessionId: 'session-pdf-1' }, + }, + }, + }) + await agentChat(pdfTask) + + expect(mockActivities.agentChatActivity).toHaveBeenCalledWith( + expect.objectContaining({ + messageContext: expect.objectContaining({ + currentAsset: expect.objectContaining({ + id: 'pdf-asset-1', + mediaType: 'pdf', + totalPages: 15, + totalFrames: undefined, + }), + }), + }), + ) + }) }) diff --git a/packages/agent/src/workflows/agent-chat.ts b/packages/agent/src/workflows/agent-chat.ts index ba30e9d4..117512fd 100644 --- a/packages/agent/src/workflows/agent-chat.ts +++ b/packages/agent/src/workflows/agent-chat.ts @@ -146,8 +146,11 @@ export async function agentChat(task: WorkflowTask): Promise { // eslint-disable-next-line @typescript-eslint/no-explicit-any const mediaInfo = asset.media as any const duration = mediaInfo?.duration - const totalPages = mediaInfo?.frames || mediaInfo?.metadata?.totalFrames const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType + const totalPages = + proxyType === 'pdf' ? mediaInfo?.frames || mediaInfo?.metadata?.totalFrames : undefined + const totalFrames = + proxyType === 'video' ? mediaInfo?.frames || mediaInfo?.metadata?.totalFrames : undefined // Resolve Attached Files Details const attachedFiles: ShumaiAttachedFileContext[] = [] @@ -254,6 +257,7 @@ export async function agentChat(task: WorkflowTask): Promise { parentId: asset.parentId || undefined, path: assetPath || undefined, durationSeconds: duration !== undefined ? duration : undefined, + totalFrames: totalFrames !== undefined ? totalFrames : undefined, totalPages: totalPages !== undefined ? totalPages : undefined, navigated: payload.agent?.hasAssetChanged === true ? true : undefined, ancestors: ancestors.length > 0 ? ancestors : undefined, diff --git a/packages/dtos/src/chat.ts b/packages/dtos/src/chat.ts index 3e85c056..541b4254 100644 --- a/packages/dtos/src/chat.ts +++ b/packages/dtos/src/chat.ts @@ -48,6 +48,7 @@ export const shumaiAssetContextSchema = z.object({ parentId: z.string().optional(), path: z.string().optional(), durationSeconds: z.number().optional(), + totalFrames: z.number().optional(), totalPages: z.number().optional(), navigated: z.boolean().optional(), ancestors: z.array(z.object({ id: z.string(), name: z.string() })).optional(), diff --git a/packages/dtos/src/context.ts b/packages/dtos/src/context.ts index f32ce0ed..5081c5c2 100644 --- a/packages/dtos/src/context.ts +++ b/packages/dtos/src/context.ts @@ -60,6 +60,9 @@ export function serializeContextToXml(context?: ShumaiMessageContext | null): st if (ca.durationSeconds !== undefined) { assetAttrs += ` duration_seconds="${ca.durationSeconds.toFixed(2)}"` } + if (ca.totalFrames !== undefined) { + assetAttrs += ` total_frames="${Math.round(ca.totalFrames)}"` + } if (ca.totalPages !== undefined) { assetAttrs += ` total_pages="${Math.round(ca.totalPages)}"` }