fix(agent): restrict pageCount and duration recommendations to matching media types (#397)

This commit is contained in:
Yiling-J
2026-09-08 14:52:37 +08:00
committed by GitHub
parent 04fd571bd9
commit 4a61e87a27
5 changed files with 183 additions and 10 deletions
@@ -125,7 +125,7 @@ describe('Agent Autofill Workflow', () => {
assetId: 'a1',
assetName: 'test.png',
mediaType: 'image/png',
duration: 10,
duration: undefined,
pageCount: undefined,
projectId: 'p1',
fields: [
@@ -276,4 +276,86 @@ describe('Agent Autofill Workflow', () => {
output: { error: 'Asset or project not found' },
})
})
it('should not pass pageCount for video assets when calling autofillAiActivity', async () => {
mockActivities.getAssetActivity.mockResolvedValue({
id: 'v1',
name: 'test.mp4',
projectId: 'p1',
storageKey: { key: 'asset-video-key' },
project: { id: 'p1', teamId: 't1' },
mediaType: 'video/mp4',
media: {
proxyType: 'video',
duration: 10.1,
frames: 300,
metadata: { totalFrames: 300 },
},
})
const task = await prisma.workflowTask.create({
data: {
type: 'ai_metadata_autofill',
status: 'pending',
assetId: 'v1',
payload: {
projectId: 'p1',
agent: { sessionId: 's1', agentId: 'agent-1' },
},
},
})
await agentAutofillMedia(task)
expect(mockActivities.autofillAiActivity).toHaveBeenCalledWith(
expect.objectContaining({
assetId: 'v1',
assetName: 'test.mp4',
mediaType: 'video/mp4',
duration: 10.1,
pageCount: undefined,
}),
)
})
it('should pass pageCount and no duration for pdf assets when calling autofillAiActivity', async () => {
mockActivities.getAssetActivity.mockResolvedValue({
id: 'doc1',
name: 'test.pdf',
projectId: 'p1',
storageKey: { key: 'asset-doc-key' },
project: { id: 'p1', teamId: 't1' },
mediaType: 'application/pdf',
media: {
proxyType: 'pdf',
duration: 0,
frames: 25,
metadata: { totalFrames: 25 },
},
})
const task = await prisma.workflowTask.create({
data: {
type: 'ai_metadata_autofill',
status: 'pending',
assetId: 'doc1',
payload: {
projectId: 'p1',
agent: { sessionId: 's1', agentId: 'agent-1' },
},
},
})
await agentAutofillMedia(task)
expect(mockActivities.autofillAiActivity).toHaveBeenCalledWith(
expect.objectContaining({
assetId: 'doc1',
assetName: 'test.pdf',
mediaType: 'application/pdf',
duration: undefined,
pageCount: 25,
}),
)
})
})
+15 -6
View File
@@ -1,6 +1,7 @@
import type { WorkflowTask } from '@shumai/db'
import { executeActivity, getActivities, TaskQueueAgent } from '@shumai/workflow-core'
import { ApplicationFailure } from '@temporalio/workflow'
import { getProxyType } from '@shumai/core/src/utils/mime'
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
const {
@@ -74,13 +75,21 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
// 4. Call AI Service (inspects asset on demand via read_asset)
const mediaInfo = asset.media as PrismaJson.MediaInfo | null
const duration = typeof mediaInfo?.duration === 'number' ? mediaInfo.duration : undefined
const proxyType = mediaInfo?.proxyType || getProxyType(asset.mediaType, asset.name) || undefined
const duration =
(proxyType === 'video' || proxyType === 'audio') && typeof mediaInfo?.duration === 'number'
? mediaInfo.duration
: undefined
const pageCount =
typeof mediaInfo?.metadata?.totalFrames === 'number'
? mediaInfo.metadata.totalFrames
: typeof mediaInfo?.frames === 'number'
? mediaInfo.frames
: undefined
proxyType === 'pdf'
? typeof mediaInfo?.metadata?.totalFrames === 'number'
? mediaInfo.metadata.totalFrames
: typeof mediaInfo?.frames === 'number'
? mediaInfo.frames
: undefined
: undefined
const aiResult = await executeActivity(agentWorkerQueue, autofillAiActivity, {
teamId,