( fullPath: string, supportsImages: boolean, maxImageFileSize: number, maxTotalImageSize: number, currentTotalMemoryUsed: number, )
| 94 | * Validates if an image can be processed based on size limits and model support |
| 95 | */ |
| 96 | export async function validateImageForProcessing( |
| 97 | fullPath: string, |
| 98 | supportsImages: boolean, |
| 99 | maxImageFileSize: number, |
| 100 | maxTotalImageSize: number, |
| 101 | currentTotalMemoryUsed: number, |
| 102 | ): Promise<ImageValidationResult> { |
| 103 | // Check if model supports images |
| 104 | if (!supportsImages) { |
| 105 | return { |
| 106 | isValid: false, |
| 107 | reason: "unsupported_model", |
| 108 | notice: "Image file detected but current model does not support images. Skipping image processing.", |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | const imageStats = await fs.stat(fullPath) |
| 113 | const imageSizeInMB = imageStats.size / (1024 * 1024) |
| 114 | |
| 115 | // Check individual file size limit |
| 116 | if (imageStats.size > maxImageFileSize * 1024 * 1024) { |
| 117 | const imageSizeFormatted = prettyBytes(imageStats.size) |
| 118 | return { |
| 119 | isValid: false, |
| 120 | reason: "size_limit", |
| 121 | notice: t("tools:readFile.imageTooLarge", { |
| 122 | size: imageSizeFormatted, |
| 123 | max: maxImageFileSize, |
| 124 | }), |
| 125 | sizeInMB: imageSizeInMB, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Check total memory limit |
| 130 | if (currentTotalMemoryUsed + imageSizeInMB > maxTotalImageSize) { |
| 131 | const currentMemoryFormatted = prettyBytes(currentTotalMemoryUsed * 1024 * 1024) |
| 132 | const fileMemoryFormatted = prettyBytes(imageStats.size) |
| 133 | return { |
| 134 | isValid: false, |
| 135 | reason: "memory_limit", |
| 136 | notice: `Image skipped to avoid size limit (${maxTotalImageSize}MB). Current: ${currentMemoryFormatted} + this file: ${fileMemoryFormatted}. Try fewer or smaller images.`, |
| 137 | sizeInMB: imageSizeInMB, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return { |
| 142 | isValid: true, |
| 143 | sizeInMB: imageSizeInMB, |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Processes an image file and returns the result |
no test coverage detected