From 28d1feacf525b55f9e02f4066b2ba8dc5b7af069 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Tue, 26 Nov 2024 19:49:53 -0800 Subject: [PATCH] refactor: simplify image file processing to use provided filenames directly --- aider/coders/base_coder.py | 40 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 0579b5d1c..bec667830 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -728,27 +728,29 @@ class Coder: return None image_messages = [] - for fname, content in self.get_abs_fnames_content(): - if is_image_file(fname): - mime_type, _ = mimetypes.guess_type(fname) - if not mime_type: - continue + for fname in fnames: + if not is_image_file(fname): + continue - with open(fname, "rb") as image_file: - encoded_string = base64.b64encode(image_file.read()).decode("utf-8") - image_url = f"data:{mime_type};base64,{encoded_string}" - rel_fname = self.get_rel_fname(fname) + mime_type, _ = mimetypes.guess_type(fname) + if not mime_type: + continue - if mime_type.startswith("image/") and supports_images: - image_messages += [ - {"type": "text", "text": f"Image file: {rel_fname}"}, - {"type": "image_url", "image_url": {"url": image_url, "detail": "high"}}, - ] - elif mime_type == "application/pdf" and supports_pdfs: - image_messages += [ - {"type": "text", "text": f"PDF file: {rel_fname}"}, - {"type": "image_url", "image_url": image_url}, - ] + with open(fname, "rb") as image_file: + encoded_string = base64.b64encode(image_file.read()).decode("utf-8") + image_url = f"data:{mime_type};base64,{encoded_string}" + rel_fname = self.get_rel_fname(fname) + + if mime_type.startswith("image/") and supports_images: + image_messages += [ + {"type": "text", "text": f"Image file: {rel_fname}"}, + {"type": "image_url", "image_url": {"url": image_url, "detail": "high"}}, + ] + elif mime_type == "application/pdf" and supports_pdfs: + image_messages += [ + {"type": "text", "text": f"PDF file: {rel_fname}"}, + {"type": "image_url", "image_url": image_url}, + ] if not image_messages: return None