fix: Implement portable way to run interactive subprocesses

This commit is contained in:
Paul Gauthier (aider) 2024-08-20 19:25:09 -07:00
parent 15ebdcc45c
commit 4917054518

View file

@ -13,6 +13,8 @@ from .base_coder import Coder
from .editblock_prompts import EditBlockPrompts from .editblock_prompts import EditBlockPrompts
import os
class EditBlockCoder(Coder): class EditBlockCoder(Coder):
"""A coder that uses search/replace blocks for code modifications.""" """A coder that uses search/replace blocks for code modifications."""
@ -27,6 +29,15 @@ class EditBlockCoder(Coder):
return edits return edits
def run_interactive_subprocess(self, command):
if os.name == 'posix': # Unix-like systems (Linux, macOS)
import pty
return pty.spawn(command)
elif os.name == 'nt': # Windows
return subprocess.run(command, shell=True)
else:
raise OSError("Unsupported operating system")
def apply_edits(self, edits): def apply_edits(self, edits):
failed = [] failed = []
passed = [] passed = []
@ -39,17 +50,9 @@ class EditBlockCoder(Coder):
self.io.tool_output(f"{edit.strip()}", bold=True) self.io.tool_output(f"{edit.strip()}", bold=True)
if self.io.confirm_ask("Do you want to run this suggested shell command?"): if self.io.confirm_ask("Do you want to run this suggested shell command?"):
try: try:
result = subprocess.run( self.run_interactive_subprocess(edit.split())
edit, except Exception as e:
shell=True, self.io.tool_error(str(e))
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.DEVNULL,
)
self.io.tool_output(result.stdout)
except subprocess.CalledProcessError as e:
self.io.tool_error(e.output)
else: else:
path, original, updated = edit path, original, updated = edit
full_path = self.abs_root_path(path) full_path = self.abs_root_path(path)