mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +00:00
fix(agent): trigger post-upload transcode and AI workflows for agent-created assets
This commit is contained in:
@@ -237,83 +237,100 @@ export class UploadService {
|
|||||||
if (!team) return
|
if (!team) return
|
||||||
if (!asset.projectId) throw new Error('Asset project ID is missing')
|
if (!asset.projectId) throw new Error('Asset project ID is missing')
|
||||||
|
|
||||||
// Ai Tasks
|
await this.triggerPostUploadWorkflows(tx, asset.id, team.id, asset.projectId)
|
||||||
const autofillAgent = await tx.agent.findFirst({
|
})
|
||||||
where: {
|
}
|
||||||
type: 'autofill',
|
|
||||||
enabled: true,
|
async triggerPostUploadWorkflows(
|
||||||
user: { teamMembers: { some: { teamId: team.id } } },
|
tx: Prisma.TransactionClient,
|
||||||
|
assetId: string,
|
||||||
|
teamId: string,
|
||||||
|
projectId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
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({
|
await tx.workflowTask.create({
|
||||||
data: {
|
data: {
|
||||||
assetId: asset.id,
|
assetId: asset.id,
|
||||||
type: WorkflowTaskType.ai_metadata_autofill,
|
type: WorkflowTaskType.ai_embedding,
|
||||||
status: WorkflowTaskStatus.pending,
|
status: WorkflowTaskStatus.pending,
|
||||||
teamId: team.id,
|
teamId: team.id,
|
||||||
projectId: asset.projectId,
|
projectId,
|
||||||
payload: {
|
payload: {
|
||||||
projectId: asset.projectId!,
|
projectId,
|
||||||
agent: { agentId: autofillAgent.id },
|
agent: { agentId: embeddingAgent.id },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Ai Embedding if enabled via agent
|
const isVideo = asset.mediaType?.startsWith('video/')
|
||||||
const embeddingAgent = await tx.agent.findFirst({
|
const isImage = asset.mediaType?.startsWith('image/')
|
||||||
where: {
|
if (!isVideo && !isImage) {
|
||||||
type: 'embedding',
|
await tx.asset.update({
|
||||||
enabled: true,
|
where: { id: asset.id },
|
||||||
user: { teamMembers: { some: { teamId: team.id } } },
|
data: { status: AssetStatus.processed },
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
if (embeddingAgent) {
|
const settings = team.settings as PrismaJson.Settings | null
|
||||||
if (asset.mediaType?.startsWith('video/') || asset.mediaType?.startsWith('image/')) {
|
if (isVideo) {
|
||||||
await tx.workflowTask.create({
|
const strategy = settings?.transcode?.videoStrategy || 'best_match'
|
||||||
data: {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
assetId: asset.id,
|
await new VideoTranscoder(tx as any, asset.id, team.id, projectId)
|
||||||
type: WorkflowTaskType.ai_embedding,
|
.setStrategy(strategy)
|
||||||
status: WorkflowTaskStatus.pending,
|
.withSprite()
|
||||||
teamId: team.id,
|
.withPoster()
|
||||||
projectId: asset.projectId,
|
.submit()
|
||||||
payload: {
|
} else if (isImage) {
|
||||||
projectId: asset.projectId!,
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
agent: { agentId: embeddingAgent.id },
|
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(
|
async listUploadTasks(
|
||||||
|
|||||||
@@ -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 type { AgentExecutionContext } from './agent'
|
||||||
import { assetService } from '@/services/asset/asset'
|
import { assetService } from '@/services/asset/asset'
|
||||||
import { VersionStackService } from '@/services/versionStack/versionStack'
|
import { VersionStackService } from '@/services/versionStack/versionStack'
|
||||||
|
import { uploadService } from '@/services/upload/upload'
|
||||||
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
||||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||||
import { paginateQuery, encodeCursor } from '@/services/pagination'
|
import { paginateQuery, encodeCursor } from '@/services/pagination'
|
||||||
@@ -814,6 +815,24 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
|||||||
id: parent,
|
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
|
// Create asset via assetService
|
||||||
const newFile = await assetService.createAsset({
|
const newFile = await assetService.createAsset({
|
||||||
name,
|
name,
|
||||||
@@ -833,6 +852,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
|||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
await assetService.updateAncestorsSize(tx as any, parent, fileSize)
|
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 {
|
return {
|
||||||
@@ -859,7 +886,7 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
|||||||
// Fetch parent file and cast to any due to Prisma type resolution limits
|
// Fetch parent file and cast to any due to Prisma type resolution limits
|
||||||
/* prettier-ignore */
|
/* prettier-ignore */
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// 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) {
|
if (!parentFile) {
|
||||||
throw ApplicationFailure.create({
|
throw ApplicationFailure.create({
|
||||||
message: `Parent file not found with ID: ${parent}`,
|
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)
|
// Authz check: verify Edit permission on the parent file (resource ID is parent file id)
|
||||||
await authzService.hasPermission({
|
await authzService.hasPermission({
|
||||||
user,
|
user,
|
||||||
@@ -916,6 +950,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
await assetService.updateAncestorsSize(tx as any, updatedStack.parentId, fileSize)
|
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 {
|
return {
|
||||||
@@ -957,6 +999,14 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
|||||||
// Update ancestors' size (add size of the new file version)
|
// Update ancestors' size (add size of the new file version)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
await assetService.updateAncestorsSize(tx as any, folderParentId!, fileSize)
|
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 {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user