mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
fix(s3): stream downloadToFile and putObject to prevent OOM on large files (#392)
* fix(s3): stream downloadToFile and putObject to prevent OOM on large files * fix(agent): preserve file MIME detection on download_asset and align local download directory creation
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
saveAssetEmbeddingsActivity,
|
||||
generateImageEmbeddingActivity,
|
||||
generateVideoChunkEmbeddingActivity,
|
||||
generateEmbeddingActivity,
|
||||
} from './ai'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '@shumai/core'
|
||||
@@ -35,6 +36,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
putObject: vi.fn(),
|
||||
headObject: vi.fn(),
|
||||
listObjects: vi.fn(),
|
||||
downloadToFile: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -121,6 +123,32 @@ describe('AI Activities Unit Tests', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should stream video to tmp file via downloadToFile in generateEmbeddingActivity', async () => {
|
||||
vi.mocked(s3Service.downloadToFile).mockResolvedValue()
|
||||
|
||||
const res = await generateEmbeddingActivity({
|
||||
teamId: 't1',
|
||||
assetId: 'asset-123',
|
||||
context: {
|
||||
agent: {},
|
||||
asset: {
|
||||
mediaType: 'video/mp4',
|
||||
name: 'sample.mp4',
|
||||
storageKey: { key: 'files/asset-123/video.mp4' },
|
||||
media: { proxyType: 'video' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
|
||||
'shumai',
|
||||
'files/asset-123/video.mp4',
|
||||
expect.stringContaining('video-'),
|
||||
)
|
||||
expect(s3Service.getObject).not.toHaveBeenCalledWith('shumai', 'files/asset-123/video.mp4')
|
||||
expect(res.embeddings.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should generate text embedding successfully', async () => {
|
||||
const res = await generateTextEmbeddingActivity({
|
||||
text: 'Search query',
|
||||
|
||||
@@ -107,16 +107,16 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
||||
throw ApplicationFailure.create({ message: 'asset has no key', nonRetryable: true })
|
||||
}
|
||||
|
||||
const { buffer: data } = await s3Service.getObject(process.env.S3_BUCKET || 'shumai', key)
|
||||
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
const results: GeneratedEmbedding[] = []
|
||||
|
||||
if (isImage) {
|
||||
const { buffer: data } = await s3Service.getObject(bucket, key)
|
||||
const embVec = await generateMultimodalEmbedding(ai, data, asset.mediaType)
|
||||
results.push({ embedding: embVec })
|
||||
} else if (isVideo) {
|
||||
const tmpFile = path.join(os.tmpdir(), `video-${Date.now()}.mp4`)
|
||||
fs.writeFileSync(tmpFile, data)
|
||||
await s3Service.downloadToFile(bucket, key, tmpFile)
|
||||
|
||||
try {
|
||||
const { stdout } = await execFileAsync('ffprobe', [
|
||||
@@ -171,7 +171,9 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fs.unlinkSync(tmpFile)
|
||||
if (fs.existsSync(tmpFile)) {
|
||||
fs.unlinkSync(tmpFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user