From be7e3fd3773258af849af17a7186718de31ee6d7 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 18 Apr 2025 09:17:13 +1000 Subject: [PATCH] feat: enhance image path detection in input processing (#189) I wanted to be able to drag and drop images while in the chat. Here it is. I have read the CLA Document and I hereby sign the CLA --- .../components/chat/terminal-chat-input.tsx | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/codex-cli/src/components/chat/terminal-chat-input.tsx b/codex-cli/src/components/chat/terminal-chat-input.tsx index ae107077..8fd0c63a 100644 --- a/codex-cli/src/components/chat/terminal-chat-input.tsx +++ b/codex-cli/src/components/chat/terminal-chat-input.tsx @@ -234,13 +234,34 @@ export default function TerminalChatInput({ return; } + // detect image file paths for dynamic inclusion const images: Array = []; - const text = inputValue - .replace(/!\[[^\]]*?\]\(([^)]+)\)/g, (_m, p1: string) => { + let text = inputValue; + // markdown-style image syntax: ![alt](path) + text = text.replace(/!\[[^\]]*?\]\(([^)]+)\)/g, (_m, p1: string) => { + images.push(p1.startsWith("file://") ? fileURLToPath(p1) : p1); + return ""; + }); + // quoted file paths ending with common image extensions (e.g. '/path/to/img.png') + text = text.replace( + /['"]([^'"]+?\.(?:png|jpe?g|gif|bmp|webp|svg))['"]/gi, + (_m, p1: string) => { images.push(p1.startsWith("file://") ? fileURLToPath(p1) : p1); return ""; - }) - .trim(); + }, + ); + // bare file paths ending with common image extensions + text = text.replace( + // eslint-disable-next-line no-useless-escape + /\b(?:\.[\/\\]|[\/\\]|[A-Za-z]:[\/\\])?[\w-]+(?:[\/\\][\w-]+)*\.(?:png|jpe?g|gif|bmp|webp|svg)\b/gi, + (match: string) => { + images.push( + match.startsWith("file://") ? fileURLToPath(match) : match, + ); + return ""; + }, + ); + text = text.trim(); const inputItem = await createInputItem(text, images); submitInput([inputItem]);