mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 09:36:58 +00:00
fix(embedding): resolve transcoded MIME type and validate EMBEDDING_CHUNK_DURATION positive limits
This commit is contained in:
@@ -279,6 +279,68 @@ describe('AI Database Activities Integration', () => {
|
||||
expect(ctx.asset.id).toBe(asset.id)
|
||||
expect(ctx.asset.mediaType).toBe('video/mp4')
|
||||
})
|
||||
|
||||
it('should parse valid positive chunk duration and fallback on invalid or negative ones', async () => {
|
||||
// Create embedding agent if not already present
|
||||
const existingAgent = await prisma.agent.findUnique({ where: { id: user.id } })
|
||||
if (!existingAgent) {
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'embedding',
|
||||
enabled: true,
|
||||
config: {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Test default/fallback
|
||||
delete process.env.EMBEDDING_CHUNK_DURATION
|
||||
let ctx = await getEmbeddingContextActivity({
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
expect(ctx.chunkDuration).toBe(60.0)
|
||||
|
||||
// Test valid positive
|
||||
process.env.EMBEDDING_CHUNK_DURATION = '45.5'
|
||||
ctx = await getEmbeddingContextActivity({
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
expect(ctx.chunkDuration).toBe(45.5)
|
||||
|
||||
// Test negative
|
||||
process.env.EMBEDDING_CHUNK_DURATION = '-10.0'
|
||||
ctx = await getEmbeddingContextActivity({
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
expect(ctx.chunkDuration).toBe(60.0)
|
||||
|
||||
// Test zero
|
||||
process.env.EMBEDDING_CHUNK_DURATION = '0'
|
||||
ctx = await getEmbeddingContextActivity({
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
expect(ctx.chunkDuration).toBe(60.0)
|
||||
|
||||
// Test non-numeric
|
||||
process.env.EMBEDDING_CHUNK_DURATION = 'invalid'
|
||||
ctx = await getEmbeddingContextActivity({
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
})
|
||||
expect(ctx.chunkDuration).toBe(60.0)
|
||||
|
||||
// Cleanup env
|
||||
delete process.env.EMBEDDING_CHUNK_DURATION
|
||||
})
|
||||
})
|
||||
|
||||
describe('saveAssetEmbeddingsActivity', () => {
|
||||
|
||||
@@ -309,7 +309,8 @@ export async function getEmbeddingContextActivity(params: { teamId: string; asse
|
||||
throw ApplicationFailure.create({ message: 'asset has no media type', nonRetryable: true })
|
||||
}
|
||||
|
||||
const chunkDuration = parseFloat(process.env.EMBEDDING_CHUNK_DURATION || '60.0') || 60.0
|
||||
const parsedDuration = parseFloat(process.env.EMBEDDING_CHUNK_DURATION || '')
|
||||
const chunkDuration = Number.isFinite(parsedDuration) && parsedDuration > 0 ? parsedDuration : 60.0
|
||||
|
||||
return {
|
||||
agent,
|
||||
|
||||
@@ -148,6 +148,38 @@ describe('Agent Embedding Workflow', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should run agent embedding workflow successfully for image with transcoded format', async () => {
|
||||
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
|
||||
agent: { id: 'b1' },
|
||||
asset: {
|
||||
id: 'a1',
|
||||
mediaType: 'image/png',
|
||||
storageKey: { key: 'test.png' },
|
||||
media: {
|
||||
imageTranscodes: [{ key: 'test-transcoded.webp', isRaw: false, format: 'webp' }]
|
||||
}
|
||||
},
|
||||
chunkDuration: 60.0,
|
||||
})
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'ai_embedding',
|
||||
status: 'pending',
|
||||
assetId: 'a1',
|
||||
teamId: 't1',
|
||||
},
|
||||
})
|
||||
|
||||
await agentEmbeddingMedia(task)
|
||||
|
||||
expect(mockActivities.generateImageEmbeddingActivity).toHaveBeenCalledWith({
|
||||
teamId: 't1',
|
||||
assetKey: 'test-transcoded.webp',
|
||||
mediaType: 'image/webp',
|
||||
})
|
||||
})
|
||||
|
||||
it('should run agent embedding workflow successfully for video in chunks', async () => {
|
||||
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
|
||||
agent: { id: 'b1' },
|
||||
|
||||
@@ -69,15 +69,15 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
|
||||
// Resolve the S3 key to use for embedding (preferring transcoded version)
|
||||
let embeddingKey = asset.storageKey?.key
|
||||
let embeddingMediaType = asset.mediaType
|
||||
if (asset.media) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const media = asset.media as any
|
||||
if (isVideo) {
|
||||
if (media.videoTranscodes && media.videoTranscodes.length > 0) {
|
||||
// Find first non-raw transcode or fallback to first transcode
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const transcode =
|
||||
media.videoTranscodes.find((t: any) => !t.isRaw) || media.videoTranscodes[0]
|
||||
media.videoTranscodes.find((t: PrismaJson.VideoTranscode) => !t.isRaw) || media.videoTranscodes[0]
|
||||
if (transcode?.key) {
|
||||
embeddingKey = transcode.key
|
||||
}
|
||||
@@ -85,11 +85,13 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
} else if (isImage) {
|
||||
if (media.imageTranscodes && media.imageTranscodes.length > 0) {
|
||||
// Find first non-raw transcode or fallback to first transcode
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const transcode =
|
||||
media.imageTranscodes.find((t: any) => !t.isRaw) || media.imageTranscodes[0]
|
||||
media.imageTranscodes.find((t: PrismaJson.ImageTranscode) => !t.isRaw) || media.imageTranscodes[0]
|
||||
if (transcode?.key) {
|
||||
embeddingKey = transcode.key
|
||||
if (transcode.format) {
|
||||
embeddingMediaType = `image/${transcode.format}`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,7 +112,7 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
const result = await executeActivity(agentWorkerQueue, generateImageEmbeddingActivity, {
|
||||
teamId: task.teamId,
|
||||
assetKey: embeddingKey,
|
||||
mediaType: asset.mediaType,
|
||||
mediaType: embeddingMediaType,
|
||||
})
|
||||
results.push({ embedding: result.embedding })
|
||||
if (result.usage) {
|
||||
|
||||
Reference in New Issue
Block a user