From c2fbd3ed531db9c98d970fbbf6fb617c5df39b1e Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 31 May 2026 09:38:18 +0800 Subject: [PATCH] fix(agent): trigger post-upload transcode and AI workflows for agent-created assets --- src/services/upload/upload.ts | 139 ++++++++++++++++------------- src/workflow/activities/db.test.ts | 82 +++++++++++++++++ src/workflow/activities/db.ts | 52 ++++++++++- 3 files changed, 211 insertions(+), 62 deletions(-) diff --git a/src/services/upload/upload.ts b/src/services/upload/upload.ts index 21d72400..3b960fd7 100644 --- a/src/services/upload/upload.ts +++ b/src/services/upload/upload.ts @@ -237,83 +237,100 @@ export class UploadService { if (!team) return if (!asset.projectId) throw new Error('Asset project ID is missing') - // Ai Tasks - const autofillAgent = await tx.agent.findFirst({ - where: { - type: 'autofill', - enabled: true, - user: { teamMembers: { some: { teamId: team.id } } }, + await this.triggerPostUploadWorkflows(tx, asset.id, team.id, asset.projectId) + }) + } + + async triggerPostUploadWorkflows( + tx: Prisma.TransactionClient, + assetId: string, + teamId: string, + projectId: string, + ): Promise { + const asset = await tx.asset.findUnique({ + where: { id: assetId }, + }) + if (!asset) throw new Error('Asset not found') + + const team = await tx.team.findUnique({ + where: { id: teamId }, + }) + if (!team) throw new Error('Team not found') + + // Ai Tasks + const autofillAgent = await tx.agent.findFirst({ + where: { + type: 'autofill', + enabled: true, + user: { teamMembers: { some: { teamId: team.id } } }, + }, + }) + + if (autofillAgent) { + await tx.workflowTask.create({ + data: { + assetId: asset.id, + type: WorkflowTaskType.ai_metadata_autofill, + status: WorkflowTaskStatus.pending, + teamId: team.id, + projectId, + payload: { + projectId, + agent: { agentId: autofillAgent.id }, + }, }, }) + } - if (autofillAgent) { + // Ai Embedding if enabled via agent + const embeddingAgent = await tx.agent.findFirst({ + where: { + type: 'embedding', + enabled: true, + user: { teamMembers: { some: { teamId: team.id } } }, + }, + }) + + if (embeddingAgent) { + if (asset.mediaType?.startsWith('video/') || asset.mediaType?.startsWith('image/')) { await tx.workflowTask.create({ data: { assetId: asset.id, - type: WorkflowTaskType.ai_metadata_autofill, + type: WorkflowTaskType.ai_embedding, status: WorkflowTaskStatus.pending, teamId: team.id, - projectId: asset.projectId, + projectId, payload: { - projectId: asset.projectId!, - agent: { agentId: autofillAgent.id }, + projectId, + agent: { agentId: embeddingAgent.id }, }, }, }) } + } - // Ai Embedding if enabled via agent - const embeddingAgent = await tx.agent.findFirst({ - where: { - type: 'embedding', - enabled: true, - user: { teamMembers: { some: { teamId: team.id } } }, - }, + const isVideo = asset.mediaType?.startsWith('video/') + const isImage = asset.mediaType?.startsWith('image/') + if (!isVideo && !isImage) { + await tx.asset.update({ + where: { id: asset.id }, + data: { status: AssetStatus.processed }, }) - - if (embeddingAgent) { - if (asset.mediaType?.startsWith('video/') || asset.mediaType?.startsWith('image/')) { - await tx.workflowTask.create({ - data: { - assetId: asset.id, - type: WorkflowTaskType.ai_embedding, - status: WorkflowTaskStatus.pending, - teamId: team.id, - projectId: asset.projectId, - payload: { - projectId: asset.projectId!, - agent: { agentId: embeddingAgent.id }, - }, - }, - }) - } + } else { + const settings = team.settings as PrismaJson.Settings | null + if (isVideo) { + const strategy = settings?.transcode?.videoStrategy || 'best_match' + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await new VideoTranscoder(tx as any, asset.id, team.id, projectId) + .setStrategy(strategy) + .withSprite() + .withPoster() + .submit() + } else if (isImage) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await new ImageTranscoder(tx as any, asset.id, team.id, projectId).withThumbnail().submit() } - - const isVideo = asset.mediaType?.startsWith('video/') - const isImage = asset.mediaType?.startsWith('image/') - if (!isVideo && !isImage) { - await tx.asset.update({ - where: { id: asset.id }, - data: { status: AssetStatus.processed }, - }) - } else { - const settings = team.settings as PrismaJson.Settings | null - if (isVideo) { - const strategy = settings?.transcode?.videoStrategy || 'best_match' - // eslint-disable-next-line @typescript-eslint/no-explicit-any - await new VideoTranscoder(tx as any, asset.id, team.id, asset.projectId) - .setStrategy(strategy) - .withSprite() - .withPoster() - .submit() - } else if (isImage) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - await new ImageTranscoder(tx as any, asset.id, team.id, asset.projectId) - .withThumbnail() - .submit() - } - } - }) + } } async listUploadTasks( diff --git a/src/workflow/activities/db.test.ts b/src/workflow/activities/db.test.ts index 2fbdb952..e4ae528a 100644 --- a/src/workflow/activities/db.test.ts +++ b/src/workflow/activities/db.test.ts @@ -485,5 +485,87 @@ describe('Database Activities', () => { } } }) + + it('should trigger post-upload workflows (transcode, AI) when creating file or version via agent tool', async () => { + const team = await prisma.team.create({ data: { name: 'Workflow Team' } }) + const user = await prisma.user.create({ + data: { name: 'Workflow User', email: 'wfuser@example.com' }, + }) + await prisma.teamMember.create({ + data: { teamId: team.id, userId: user.id, role: 'owner' }, + }) + const project = await prisma.project.create({ + data: { name: 'Workflow Project', teamId: team.id }, + }) + const parentFolder = await prisma.asset.create({ + data: { + name: 'Parent Folder', + type: 'folder', + projectId: project.id, + status: 'uploaded', + }, + }) + + // Setup agents + const bot1 = await prisma.user.create({ + data: { name: 'Autofill Bot', email: 'autobot@example.com', type: 'agent' }, + }) + await prisma.teamMember.create({ + data: { teamId: team.id, userId: bot1.id, role: 'editor' }, + }) + await prisma.agent.create({ + data: { + user: { connect: { id: bot1.id } }, + type: 'autofill', + enabled: true, + team: { connect: { id: team.id } }, + config: { provider: 'google', model: 'gemini' }, + }, + }) + + const bot2 = await prisma.user.create({ + data: { name: 'Embedding Bot', email: 'emb@example.com', type: 'agent' }, + }) + await prisma.teamMember.create({ + data: { teamId: team.id, userId: bot2.id, role: 'editor' }, + }) + await prisma.agent.create({ + data: { + user: { connect: { id: bot2.id } }, + type: 'embedding', + enabled: true, + team: { connect: { id: team.id } }, + config: { provider: 'google', model: 'gemini' }, + }, + }) + + // Execute create_file with a video file type + const fileResult = await executeAgentToolActivity({ + taskId: 'wf-task-1', + toolName: 'create_file', + args: { + parent: parentFolder.id, + s3Key: 'video/some-key.mp4', + name: 'test-video.mp4', + size: 1024 * 1024, + contentType: 'video/mp4', + }, + userId: user.id, + }) + + expect(fileResult.id).toBeDefined() + + // Fetch WorkflowTasks created for the asset + const tasks = await prisma.workflowTask.findMany({ + where: { assetId: fileResult.id }, + }) + + // Assert post-upload workflows are triggered + expect(tasks.length).toBe(3) + const types = tasks.map((t) => t.type) + expect(types).toContain('transcode') + expect(types).toContain('ai_metadata_autofill') + expect(types).toContain('ai_embedding') + }) }) }) diff --git a/src/workflow/activities/db.ts b/src/workflow/activities/db.ts index 6f9febaf..9ccc6281 100644 --- a/src/workflow/activities/db.ts +++ b/src/workflow/activities/db.ts @@ -8,6 +8,7 @@ import { WorkflowTaskStatus, AssetStatus, Prisma, AssetType } from '@/generated/ import type { AgentExecutionContext } from './agent' import { assetService } from '@/services/asset/asset' import { VersionStackService } from '@/services/versionStack/versionStack' +import { uploadService } from '@/services/upload/upload' import { authzService, Permission, ResourceType } from '@/services/authz/authz' import { generateKeyBetween } from 'jittered-fractional-indexing' import { paginateQuery, encodeCursor } from '@/services/pagination' @@ -814,6 +815,24 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): id: parent, }) + // Fetch parent folder to resolve projectId and teamId + const parentAsset = await prisma.asset.findUnique({ + where: { id: parent }, + include: { project: true }, + }) + if (!parentAsset) { + throw ApplicationFailure.create({ + message: `Parent folder not found with ID: ${parent}`, + nonRetryable: true, + }) + } + if (!parentAsset.projectId || !parentAsset.project?.teamId) { + throw ApplicationFailure.create({ + message: `Parent folder ${parent} has no project or team associated`, + nonRetryable: true, + }) + } + // Create asset via assetService const newFile = await assetService.createAsset({ name, @@ -833,6 +852,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): }) // eslint-disable-next-line @typescript-eslint/no-explicit-any await assetService.updateAncestorsSize(tx as any, parent, fileSize) + + // Trigger post-upload transcode and AI workflows + await uploadService.triggerPostUploadWorkflows( + tx, + newFile.id, + parentAsset.project!.teamId, + parentAsset.projectId!, + ) }) return { @@ -859,7 +886,7 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): // Fetch parent file and cast to any due to Prisma type resolution limits /* prettier-ignore */ // eslint-disable-next-line @typescript-eslint/no-explicit-any - const parentFile = (await prisma.asset.findUnique({ where: { id: parent }, include: { parent: true } })) as any + const parentFile = (await prisma.asset.findUnique({ where: { id: parent }, include: { parent: true, project: true } })) as any if (!parentFile) { throw ApplicationFailure.create({ message: `Parent file not found with ID: ${parent}`, @@ -867,6 +894,13 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): }) } + if (!parentFile.projectId || !parentFile.project?.teamId) { + throw ApplicationFailure.create({ + message: `Parent file ${parent} has no project or team associated`, + nonRetryable: true, + }) + } + // Authz check: verify Edit permission on the parent file (resource ID is parent file id) await authzService.hasPermission({ user, @@ -916,6 +950,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): // eslint-disable-next-line @typescript-eslint/no-explicit-any await assetService.updateAncestorsSize(tx as any, updatedStack.parentId, fileSize) } + + // Trigger post-upload transcode and AI workflows + await uploadService.triggerPostUploadWorkflows( + tx, + newFile.id, + parentFile.project.teamId, + parentFile.projectId, + ) }) return { @@ -957,6 +999,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): // Update ancestors' size (add size of the new file version) // eslint-disable-next-line @typescript-eslint/no-explicit-any await assetService.updateAncestorsSize(tx as any, folderParentId!, fileSize) + + // Trigger post-upload transcode and AI workflows + await uploadService.triggerPostUploadWorkflows( + tx, + newFile.id, + parentFile.project.teamId, + parentFile.projectId!, + ) }) return {