mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 09:37:20 +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 },
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user