|
| 1 | +import { existsSync } from 'fs' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +import { getProjectRoot } from '../project-files' |
| 5 | +import { getSystemMessage } from '../utils/message-history' |
| 6 | +import { |
| 7 | + SUPPORTED_IMAGE_EXTENSIONS, |
| 8 | + isImageFile, |
| 9 | +} from '../utils/image-handler' |
| 10 | + |
| 11 | +import type { PostUserMessageFn } from '../types/contracts/send-message' |
| 12 | + |
| 13 | +/** |
| 14 | + * Handle the /image command to attach an image file. |
| 15 | + * Usage: /image <path> [message] |
| 16 | + * Example: /image ./screenshot.png please analyze this |
| 17 | + */ |
| 18 | +export function handleImageCommand(args: string): { |
| 19 | + postUserMessage: PostUserMessageFn |
| 20 | + transformedPrompt?: string |
| 21 | +} { |
| 22 | + const trimmedArgs = args.trim() |
| 23 | + |
| 24 | + if (!trimmedArgs) { |
| 25 | + // No path provided - show usage help |
| 26 | + const postUserMessage: PostUserMessageFn = (prev) => [ |
| 27 | + ...prev, |
| 28 | + getSystemMessage( |
| 29 | + `📸 **Image Command Usage**\n\n` + |
| 30 | + ` /image <path> [message]\n\n` + |
| 31 | + `**Examples:**\n` + |
| 32 | + ` /image ./screenshot.png\n` + |
| 33 | + ` /image ~/Desktop/error.png please help debug this\n` + |
| 34 | + ` /image assets/diagram.jpg explain this architecture\n\n` + |
| 35 | + `**Supported formats:** ${Array.from(SUPPORTED_IMAGE_EXTENSIONS).join(', ')}\n\n` + |
| 36 | + `**Tip:** You can also include images directly in your message:\n` + |
| 37 | + ` "Please analyze ./image.png and tell me what you see"`, |
| 38 | + ), |
| 39 | + ] |
| 40 | + return { postUserMessage } |
| 41 | + } |
| 42 | + |
| 43 | + // Parse the path and optional message |
| 44 | + // The path is the first argument (up to first space or the whole string) |
| 45 | + const parts = trimmedArgs.match(/^(\S+)(?:\s+(.*))?$/) |
| 46 | + if (!parts) { |
| 47 | + const postUserMessage: PostUserMessageFn = (prev) => [ |
| 48 | + ...prev, |
| 49 | + getSystemMessage('❌ Invalid image command format. Use: /image <path> [message]'), |
| 50 | + ] |
| 51 | + return { postUserMessage } |
| 52 | + } |
| 53 | + |
| 54 | + const [, imagePath, message] = parts |
| 55 | + const projectRoot = getProjectRoot() |
| 56 | + |
| 57 | + // Resolve the path relative to project root |
| 58 | + let resolvedPath = imagePath |
| 59 | + if (!path.isAbsolute(imagePath) && !imagePath.startsWith('~')) { |
| 60 | + resolvedPath = path.resolve(projectRoot, imagePath) |
| 61 | + } else if (imagePath.startsWith('~')) { |
| 62 | + resolvedPath = path.resolve( |
| 63 | + process.env.HOME || process.env.USERPROFILE || '', |
| 64 | + imagePath.slice(1), |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + // Check if file exists |
| 69 | + if (!existsSync(resolvedPath)) { |
| 70 | + const postUserMessage: PostUserMessageFn = (prev) => [ |
| 71 | + ...prev, |
| 72 | + getSystemMessage(`❌ Image file not found: ${imagePath}`), |
| 73 | + ] |
| 74 | + return { postUserMessage } |
| 75 | + } |
| 76 | + |
| 77 | + // Check if it's a supported image format |
| 78 | + if (!isImageFile(imagePath)) { |
| 79 | + const ext = path.extname(imagePath).toLowerCase() |
| 80 | + const postUserMessage: PostUserMessageFn = (prev) => [ |
| 81 | + ...prev, |
| 82 | + getSystemMessage( |
| 83 | + `❌ Unsupported image format: ${ext}\n` + |
| 84 | + `Supported formats: ${Array.from(SUPPORTED_IMAGE_EXTENSIONS).join(', ')}`, |
| 85 | + ), |
| 86 | + ] |
| 87 | + return { postUserMessage } |
| 88 | + } |
| 89 | + |
| 90 | + // Transform the command into a prompt with the image path |
| 91 | + // The image-handler will auto-detect paths like ./image.png or @image.png |
| 92 | + const transformedPrompt = message |
| 93 | + ? `${message} ${imagePath}` |
| 94 | + : `Please analyze this image: ${imagePath}` |
| 95 | + |
| 96 | + const postUserMessage: PostUserMessageFn = (prev) => prev |
| 97 | + |
| 98 | + return { |
| 99 | + postUserMessage, |
| 100 | + transformedPrompt, |
| 101 | + } |
| 102 | +} |
0 commit comments