feat: Add support for text and image clipboard content

This commit is contained in:
Paul Gauthier (aider) 2024-08-09 07:54:11 -03:00
parent b057b3043c
commit 96a4ba783b

View file

@ -7,7 +7,8 @@ from collections import OrderedDict
from pathlib import Path from pathlib import Path
import git import git
from PIL import ImageGrab import pyperclip
from PIL import ImageGrab, Image
from aider import models, prompts, voice from aider import models, prompts, voice
from aider.help import Help, install_help_extra from aider.help import Help, install_help_extra
@ -895,26 +896,34 @@ class Commands:
return text return text
def cmd_clipboard(self, args): def cmd_clipboard(self, args):
"Add an image from the clipboard to the chat" "Add content from the clipboard to the chat (supports both text and images)"
try: try:
# Check for image first
image = ImageGrab.grabclipboard() image = ImageGrab.grabclipboard()
if image is None: if isinstance(image, Image.Image):
self.io.tool_error("No image found in clipboard.") with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
return image.save(temp_file.name, "PNG")
temp_file_path = temp_file.name
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file: abs_file_path = Path(temp_file_path).resolve()
image.save(temp_file.name, "PNG") self.coder.abs_fnames.add(str(abs_file_path))
temp_file_path = temp_file.name self.io.tool_output(f"Added clipboard image to the chat: {abs_file_path}")
self.coder.check_added_files()
abs_file_path = Path(temp_file_path).resolve() return prompts.added_files.format(fnames=str(abs_file_path))
self.coder.abs_fnames.add(str(abs_file_path))
self.io.tool_output(f"Added clipboard image to the chat: {abs_file_path}") # If not an image, try to get text
self.coder.check_added_files() text = pyperclip.paste()
if text:
return prompts.added_files.format(fnames=str(abs_file_path)) self.io.tool_output("Text content found in clipboard:")
self.io.tool_output(text)
return text
self.io.tool_error("No image or text content found in clipboard.")
return
except Exception as e: except Exception as e:
self.io.tool_error(f"Error adding clipboard image: {e}") self.io.tool_error(f"Error processing clipboard content: {e}")
def expand_subdir(file_path): def expand_subdir(file_path):