mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 20:48:38 +00:00
feat(embedding): defer embedding workflow, use transcoded assets, and execute chunks in separate activities
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '@shumai/core'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
@@ -468,3 +468,74 @@ export async function overlayAnnotationsActivity(params: {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateEmbeddingTaskIfEnabledParams {
|
||||
assetId: string
|
||||
teamId: string | null
|
||||
projectId: string | null
|
||||
}
|
||||
|
||||
export async function createEmbeddingTaskIfEnabledActivity(
|
||||
params: CreateEmbeddingTaskIfEnabledParams,
|
||||
): Promise<void> {
|
||||
let teamId = params.teamId
|
||||
let projectId = params.projectId
|
||||
|
||||
// Resolve teamId and projectId from asset if missing
|
||||
if (!teamId || !projectId) {
|
||||
const asset = await prisma.asset.findUnique({
|
||||
where: { id: params.assetId },
|
||||
include: {
|
||||
project: true,
|
||||
},
|
||||
})
|
||||
if (asset) {
|
||||
projectId = projectId || asset.projectId
|
||||
teamId = teamId || asset.project?.teamId || null
|
||||
}
|
||||
}
|
||||
|
||||
if (!teamId) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if there is an active embedding agent for the team
|
||||
const embeddingAgent = await prisma.agent.findFirst({
|
||||
where: {
|
||||
type: 'embedding',
|
||||
enabled: true,
|
||||
user: { teamMembers: { some: { teamId } } },
|
||||
},
|
||||
})
|
||||
|
||||
if (!embeddingAgent) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if a pending/processing embedding task already exists for this asset to avoid duplicates
|
||||
const existing = await prisma.workflowTask.findFirst({
|
||||
where: {
|
||||
assetId: params.assetId,
|
||||
type: WorkflowTaskType.ai_embedding,
|
||||
status: { in: [WorkflowTaskStatus.pending, WorkflowTaskStatus.processing] },
|
||||
},
|
||||
})
|
||||
if (existing) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create embedding task
|
||||
await prisma.workflowTask.create({
|
||||
data: {
|
||||
assetId: params.assetId,
|
||||
type: WorkflowTaskType.ai_embedding,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
teamId,
|
||||
projectId,
|
||||
payload: {
|
||||
projectId: projectId ?? '',
|
||||
agent: { agentId: embeddingAgent.id },
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ describe('Transcode Workflow', () => {
|
||||
overlayAnnotationsActivity: Object.assign(vi.fn(), {
|
||||
_activityName: 'overlayAnnotationsActivity',
|
||||
}),
|
||||
createEmbeddingTaskIfEnabledActivity: Object.assign(vi.fn(), {
|
||||
_activityName: 'createEmbeddingTaskIfEnabledActivity',
|
||||
}),
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -134,6 +137,12 @@ describe('Transcode Workflow', () => {
|
||||
status: AssetStatus.processed,
|
||||
})
|
||||
|
||||
expect(mockActivities.createEmbeddingTaskIfEnabledActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
})
|
||||
|
||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||
taskId: 'task-1',
|
||||
status: WorkflowTaskStatus.completed,
|
||||
@@ -205,6 +214,12 @@ describe('Transcode Workflow', () => {
|
||||
assetId: 'asset-image',
|
||||
status: AssetStatus.processed,
|
||||
})
|
||||
|
||||
expect(mockActivities.createEmbeddingTaskIfEnabledActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-image',
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle failures and update task status with error', async () => {
|
||||
|
||||
@@ -18,6 +18,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
cleanupTmpDirActivity,
|
||||
takeScreenshotsActivity,
|
||||
overlayAnnotationsActivity,
|
||||
createEmbeddingTaskIfEnabledActivity,
|
||||
} = getActivities()
|
||||
|
||||
let tmpDir: string | undefined
|
||||
@@ -245,6 +246,13 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
status: 'processed',
|
||||
})
|
||||
|
||||
// 11.5 Create AI embedding task if enabled
|
||||
await executeActivity(workerQueue, createEmbeddingTaskIfEnabledActivity, {
|
||||
assetId: asset.id,
|
||||
teamId: task.teamId,
|
||||
projectId: task.projectId,
|
||||
})
|
||||
|
||||
// 12. Update Task Status
|
||||
await executeActivity(workerQueue, updateTaskStatusActivity, {
|
||||
taskId: task.id,
|
||||
|
||||
Reference in New Issue
Block a user