| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { secureFs } from '@automaker/platform'; |
| import path from 'path'; |
| import type { ImageData, ImageContentBlock } from '@automaker/types'; |
|
|
| |
| |
| |
| const IMAGE_MIME_TYPES: Record<string, string> = { |
| '.jpg': 'image/jpeg', |
| '.jpeg': 'image/jpeg', |
| '.png': 'image/png', |
| '.gif': 'image/gif', |
| '.webp': 'image/webp', |
| } as const; |
|
|
| |
| |
| |
| |
| |
| |
| export function getMimeTypeForImage(imagePath: string): string { |
| const ext = path.extname(imagePath).toLowerCase(); |
| return IMAGE_MIME_TYPES[ext] || 'image/png'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function readImageAsBase64(imagePath: string): Promise<ImageData> { |
| const imageBuffer = (await secureFs.readFile(imagePath)) as Buffer; |
| const base64Data = imageBuffer.toString('base64'); |
| const mimeType = getMimeTypeForImage(imagePath); |
|
|
| return { |
| base64: base64Data, |
| mimeType, |
| filename: path.basename(imagePath), |
| originalPath: imagePath, |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function convertImagesToContentBlocks( |
| imagePaths: string[], |
| workDir?: string |
| ): Promise<ImageContentBlock[]> { |
| const blocks: ImageContentBlock[] = []; |
|
|
| for (const imagePath of imagePaths) { |
| try { |
| |
| const absolutePath = |
| workDir && !path.isAbsolute(imagePath) ? path.join(workDir, imagePath) : imagePath; |
|
|
| const imageData = await readImageAsBase64(absolutePath); |
|
|
| blocks.push({ |
| type: 'image', |
| source: { |
| type: 'base64', |
| media_type: imageData.mimeType, |
| data: imageData.base64, |
| }, |
| }); |
| } catch (error) { |
| console.error(`[ImageHandler] Failed to load image ${imagePath}:`, error); |
| |
| } |
| } |
|
|
| return blocks; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function formatImagePathsForPrompt(imagePaths: string[]): string { |
| if (imagePaths.length === 0) { |
| return ''; |
| } |
|
|
| let text = '\n\nAttached images:\n'; |
| for (const imagePath of imagePaths) { |
| text += `- ${imagePath}\n`; |
| } |
| return text; |
| } |
|
|