(params: {
content: string
pendingImages: PendingImageAttachment[]
projectRoot: string
processor?: typeof processImageFile
log?: typeof logger
})
| 17 | } |
| 18 | |
| 19 | export const processImagesForMessage = async (params: { |
| 20 | content: string |
| 21 | pendingImages: PendingImageAttachment[] |
| 22 | projectRoot: string |
| 23 | processor?: typeof processImageFile |
| 24 | log?: typeof logger |
| 25 | }): Promise<{ |
| 26 | attachments: { path: string; filename: string; size?: number }[] |
| 27 | messageContent: MessageContent[] | undefined |
| 28 | }> => { |
| 29 | const { |
| 30 | content, |
| 31 | pendingImages, |
| 32 | projectRoot, |
| 33 | processor = processImageFile, |
| 34 | log = logger, |
| 35 | } = params |
| 36 | |
| 37 | const attachments = pendingImages.map((img) => ({ |
| 38 | path: img.path, |
| 39 | filename: img.filename, |
| 40 | size: img.size, |
| 41 | })) |
| 42 | |
| 43 | const validImageParts: ProcessedImagePart[] = [] |
| 44 | |
| 45 | // First, use pre-processed data from pendingImages (already processed when attached) |
| 46 | // This avoids re-reading from disk, which can fail if the path is relative to a different cwd |
| 47 | const pendingImagePaths = new Set<string>() |
| 48 | for (const img of pendingImages) { |
| 49 | pendingImagePaths.add(img.path) |
| 50 | |
| 51 | if (img.processedImage) { |
| 52 | // Use the already-processed image data |
| 53 | validImageParts.push({ |
| 54 | type: 'image', |
| 55 | image: img.processedImage.base64, |
| 56 | mediaType: img.processedImage.mediaType, |
| 57 | filename: img.filename, |
| 58 | size: img.size, |
| 59 | width: img.width, |
| 60 | height: img.height, |
| 61 | path: img.path, |
| 62 | }) |
| 63 | } else if (img.status === 'ready') { |
| 64 | // Backwards compatibility: if processedImage is missing but status is ready, |
| 65 | // try to process from disk (shouldn't happen in normal flow) |
| 66 | log.warn( |
| 67 | { imagePath: img.path }, |
| 68 | 'Pending image marked ready but missing processedImage data, re-processing from disk', |
| 69 | ) |
| 70 | const result = await processor(img.path, projectRoot) |
| 71 | if (result.success && result.imagePart) { |
| 72 | validImageParts.push({ |
| 73 | type: 'image', |
| 74 | image: result.imagePart.image, |
| 75 | mediaType: result.imagePart.mediaType, |
| 76 | filename: result.imagePart.filename, |
no test coverage detected