mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 15:44:28 +00:00
feat(embedding): add compose tiers, docs, and regenerate-embeddings CLI (#480)
* feat(embedding): update compose tiers, docs, and add regenerate-embeddings CLI - Add Lite, Embedding, and Full Docker Compose configurations (and Aliyun mirrors) - Set CONCURRENCY_AGENT to 1 for compose files containing embedding-server - Update README and README_CN with compose options, recommended RAM, and semantic search links - Add LiteRT-LM Embedding Server setup guide in docs/using-shumai/embedding-server.mdx - Add shumai regenerate-embeddings CLI command and EmbeddingService to regenerate embeddings across teams - Add comprehensive test coverage for EmbeddingService * fix(docker): remove basic auth for turntable-renderer in full compose * fix(agent): lock and disable embedding agent when ENABLE_EMBEDDING_AGENT is false Ensure that when ENABLE_EMBEDDING_AGENT is set to 'false', existing embedding agents are automatically disabled on startup, marked as read-only, and blocked from manual API modifications or creations. Additionally guard background task creation and semantic search when the environment variable disables embedding agents. * test(upload): remove flaky status assertion on workflow tasks
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { prisma, WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
|
||||
export interface RegenerateEmbeddingsResult {
|
||||
tasksCreated: number
|
||||
totalAssets: number
|
||||
skippedTeams: string[]
|
||||
}
|
||||
|
||||
export class EmbeddingService {
|
||||
/**
|
||||
* Regenerates vector embeddings across all teams.
|
||||
*
|
||||
* Skips teams that do not have an active (enabled) embedding agent and logs a warning.
|
||||
* For teams with an active embedding agent, purges existing embeddings and pending/processing
|
||||
* embedding tasks upfront, then creates fresh ai_embedding workflow tasks for all eligible assets.
|
||||
*/
|
||||
async regenerateEmbeddings(): Promise<RegenerateEmbeddingsResult> {
|
||||
const teams = await prisma.team.findMany({
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
|
||||
const activeAgents = await prisma.agent.findMany({
|
||||
where: {
|
||||
type: 'embedding',
|
||||
enabled: true,
|
||||
},
|
||||
select: { id: true, teamId: true },
|
||||
})
|
||||
|
||||
const activeAgentByTeamId = new Map(
|
||||
activeAgents
|
||||
.filter((a): a is typeof a & { teamId: string } => typeof a.teamId === 'string')
|
||||
.map((a) => [a.teamId, a]),
|
||||
)
|
||||
|
||||
const activeTeamIds: string[] = []
|
||||
const skippedTeams: string[] = []
|
||||
|
||||
for (const team of teams) {
|
||||
if (activeAgentByTeamId.has(team.id)) {
|
||||
activeTeamIds.push(team.id)
|
||||
} else {
|
||||
skippedTeams.push(team.name || team.id)
|
||||
logger.warn(
|
||||
{ teamId: team.id, teamName: team.name },
|
||||
'Skipping embedding regeneration for team: no active embedding agent found',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (activeTeamIds.length === 0) {
|
||||
logger.info('No teams found with an active embedding agent. 0 tasks created.')
|
||||
return { tasksCreated: 0, totalAssets: 0, skippedTeams }
|
||||
}
|
||||
|
||||
const candidateAssets = await prisma.asset.findMany({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
status: 'processed',
|
||||
type: { in: ['file', 'version_stack'] },
|
||||
project: { teamId: { in: activeTeamIds } },
|
||||
},
|
||||
include: { project: true },
|
||||
})
|
||||
|
||||
const eligibleAssets = candidateAssets.filter((asset) => {
|
||||
const isVideo = asset.mediaType?.startsWith('video/')
|
||||
const mediaInfo = asset.media as PrismaJson.MediaInfo | null
|
||||
const isImage =
|
||||
asset.mediaType?.startsWith('image/') ||
|
||||
(mediaInfo?.proxyType === '3d' && (mediaInfo.imageTranscodes?.length ?? 0) > 0)
|
||||
return isVideo || isImage
|
||||
})
|
||||
|
||||
const eligibleAssetIds = eligibleAssets.map((a) => a.id)
|
||||
|
||||
if (eligibleAssetIds.length > 0) {
|
||||
// Clean up previous embedding tasks to avoid duplicate execution
|
||||
await prisma.workflowTask.deleteMany({
|
||||
where: {
|
||||
assetId: { in: eligibleAssetIds },
|
||||
type: WorkflowTaskType.ai_embedding,
|
||||
},
|
||||
})
|
||||
|
||||
// Purge existing embeddings so everything regenerates cleanly with the configured provider
|
||||
await prisma.assetEmbedding.deleteMany({
|
||||
where: {
|
||||
assetId: { in: eligibleAssetIds },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
let tasksCreated = 0
|
||||
for (const asset of eligibleAssets) {
|
||||
const teamId = asset.project?.teamId
|
||||
if (!teamId) continue
|
||||
const agent = activeAgentByTeamId.get(teamId)
|
||||
if (!agent) continue
|
||||
|
||||
await prisma.workflowTask.create({
|
||||
data: {
|
||||
assetId: asset.id,
|
||||
type: WorkflowTaskType.ai_embedding,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
teamId,
|
||||
projectId: asset.projectId,
|
||||
payload: {
|
||||
projectId: asset.projectId ?? '',
|
||||
agent: { agentId: agent.id },
|
||||
},
|
||||
},
|
||||
})
|
||||
tasksCreated++
|
||||
}
|
||||
|
||||
logger.info(
|
||||
{
|
||||
totalAssets: eligibleAssets.length,
|
||||
tasksCreated,
|
||||
skippedTeamsCount: skippedTeams.length,
|
||||
},
|
||||
'Successfully queued embedding regeneration tasks',
|
||||
)
|
||||
|
||||
return {
|
||||
tasksCreated,
|
||||
totalAssets: eligibleAssets.length,
|
||||
skippedTeams,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const embeddingService = new EmbeddingService()
|
||||
Reference in New Issue
Block a user