mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 14:38:21 +00:00
fix(agent): trigger post-upload transcode and AI workflows for agent-created assets
This commit is contained in:
@@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user