/** * Process @file CLI arguments into text content and image attachments */ import { existsSync, readFileSync, statSync } from "node:fs"; import { resolve } from "node:path"; import type { ImageContent } from "@oh-my-pi/pi-ai"; import chalk from "chalk"; import { resolveReadPath } from "$c/tools/path-utils"; import { formatDimensionNote, resizeImage } from "$c/utils/image-resize"; import { detectSupportedImageMimeTypeFromFile } from "$c/utils/mime"; export interface ProcessedFiles { text: string; images: ImageContent[]; } export interface ProcessFileOptions { /** Whether to auto-resize images to 2000x2000 max. Default: true */ autoResizeImages?: boolean; } /** Process @file arguments into text content and image attachments */ export async function processFileArguments(fileArgs: string[], options?: ProcessFileOptions): Promise { const _autoResizeImages = options?.autoResizeImages ?? true; let text = ""; const images: ImageContent[] = []; for (const fileArg of fileArgs) { // Expand and resolve path (handles ~ expansion and macOS screenshot Unicode spaces) const absolutePath = resolve(resolveReadPath(fileArg, process.cwd())); // Check if file exists and is not empty if (!existsSync(absolutePath)) { console.error(chalk.red(`Error: File not found: ${absolutePath}`)); process.exit(1); } const stats = statSync(absolutePath); if (stats.size === 0) { // Skip empty files continue; } const mimeType = await detectSupportedImageMimeTypeFromFile(absolutePath); if (mimeType) { // Handle image file const buffer = readFileSync(absolutePath); const base64Content = buffer.toString("base64"); let attachment: ImageContent; let dimensionNote: string | undefined; if (_autoResizeImages) { try { const resized = await resizeImage({ type: "image", data: base64Content, mimeType }); dimensionNote = formatDimensionNote(resized); attachment = { type: "image", mimeType: resized.mimeType, data: resized.data, }; } catch { // Fall back to original image on resize failure attachment = { type: "image", mimeType, data: base64Content, }; } } else { attachment = { type: "image", mimeType, data: base64Content, }; } images.push(attachment); // Add text reference to image with optional dimension note if (dimensionNote) { text += `${dimensionNote}\n`; } else { text += `\n`; } } else { // Handle text file try { const content = readFileSync(absolutePath, "utf-8"); text += `\n${content}\n\n`; } catch (error: unknown) { const message = error instanceof Error ? error.message : String(error); console.error(chalk.red(`Error: Could not read file ${absolutePath}: ${message}`)); process.exit(1); } } } return { text, images }; }