feat(embedding): defer embedding workflow, use transcoded assets, and execute chunks in separate activities

This commit is contained in:
yiling
2026-06-18 10:57:22 +08:00
parent 4af081f128
commit f399d2c469
11 changed files with 451 additions and 95 deletions
+72 -1
View File
@@ -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 },
},
},
})
}