Skip to content

Commit 26636d7

Browse files
committed
switch to python sdk
1 parent 4112c3a commit 26636d7

File tree

1 file changed

+22
-62
lines changed

1 file changed

+22
-62
lines changed

baselines/run_aider.py

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import os
3-
import subprocess
43
from pathlib import Path
54
import hydra
65
from datasets import load_dataset
@@ -20,6 +19,9 @@
2019
from commit0.harness.constants import RUN_AIDER_LOG_DIR
2120
from commit0.harness.docker_build import setup_logger
2221

22+
from aider.coders import Coder
23+
from aider.models import Model
24+
2325
class DirContext:
2426
def __init__(self, d):
2527
self.dir = d
@@ -35,61 +37,28 @@ def __exit__(
3537
os.chdir(self.cwd)
3638

3739

38-
def get_aider_cmd(
39-
model: str,
40-
files: list[str],
41-
message_to_aider: str,
40+
def run_aider(
41+
model_name: str,
42+
fnames: list[str],
43+
message: str,
4244
test_cmd: str,
4345
lint_cmd: str,
4446
log_dir: Path,
45-
) -> str:
46-
"""Get the Aider command based on the given context."""
47-
base_cmd = f'aider --model {model} --message "{message_to_aider}"'
48-
if len(files) > 0:
49-
files = " ".join(files)
50-
base_cmd += f" --file {files}"
51-
if lint_cmd:
52-
base_cmd += f" --auto-lint --lint-cmd '{lint_cmd}'"
47+
) -> None:
5348
if test_cmd:
54-
base_cmd += f" --auto-test --test-cmd '{test_cmd}'"
55-
base_cmd += " --yes"
56-
57-
# Store Aider input and chat history in log directory
49+
auto_test = True
50+
else:
51+
auto_test = False
52+
if lint_cmd:
53+
auto_lint = True
54+
else:
55+
auto_lint = False
56+
model = Model(model_name)
5857
input_history_file = log_dir / ".aider.input.history"
5958
chat_history_file = log_dir / ".aider.chat.history.md"
60-
61-
base_cmd += f" --input-history-file {input_history_file}"
62-
base_cmd += f" --chat-history-file {chat_history_file}"
63-
return base_cmd
64-
65-
66-
def execute_aider_cmd(
67-
aider_cmd: str,
68-
logger: logging.Logger,
69-
) -> None:
70-
"""Execute the Aider command."""
71-
try:
72-
process = subprocess.Popen(
73-
aider_cmd,
74-
shell=True,
75-
stdout=subprocess.PIPE,
76-
stderr=subprocess.PIPE,
77-
universal_newlines=True,
78-
)
79-
stdout, stderr = process.communicate()
80-
logger.info(f"STDOUT: {stdout}")
81-
logger.info(f"STDERR: {stderr}")
82-
except subprocess.CalledProcessError as e:
83-
logger.error(f"Command failed with exit code {e.returncode}")
84-
logger.error(f"STDOUT: {e.stdout}")
85-
logger.error(f"STDERR: {e.stderr}")
86-
87-
except OSError as e:
88-
if e.errno == 63: # File name too long error
89-
logger.error("Command failed due to file name being too long")
90-
logger.error(f"Command: {''.join(aider_cmd)}")
91-
else:
92-
logger.error(f"OSError occurred: {e}")
59+
io = InputOutput(yes=True, input_history_file=input_history_file, chat_history_file=chat_history_file)
60+
coder = Coder.create(main_model=model, fnames=fnames, auto_lint=auto_lint, lint_cmds=[lint_cmd], io=io)
61+
coder.run(message)
9362

9463

9564
def run_aider_for_repo(
@@ -141,9 +110,9 @@ def run_aider_for_repo(
141110
log_file = log_dir / "run_aider.log"
142111
logger = setup_logger(repo_name, log_file)
143112

144-
aider_cmd = get_aider_cmd(
113+
aider_cmd = run_aider(
145114
aider_config.llm_name,
146-
[],
115+
target_edit_files,
147116
message_to_aider,
148117
test_cmd,
149118
lint_cmd,
@@ -169,7 +138,7 @@ def run_aider_for_repo(
169138
log_file = log_dir / "run_aider.log"
170139
logger = setup_logger(repo_name, log_file)
171140

172-
aider_cmd = get_aider_cmd(
141+
aider_cmd = run_aider(
173142
aider_config.llm_name,
174143
[f],
175144
message_to_aider,
@@ -184,13 +153,6 @@ def run_aider_for_repo(
184153
execute_aider_cmd(aider_cmd, logger)
185154

186155

187-
def pre_aider_processing(aider_config: AiderConfig) -> None:
188-
"""Pre-process the Aider config."""
189-
if aider_config.use_user_prompt:
190-
# get user prompt from input
191-
aider_config.user_prompt = input("Enter the user prompt: ")
192-
193-
194156
def main() -> None:
195157
"""Main function to run Aider for a given repository.
196158
@@ -226,8 +188,6 @@ def main() -> None:
226188
)
227189
]
228190

229-
pre_aider_processing(aider_config)
230-
231191
with tqdm(
232192
total=len(filtered_dataset), smoothing=0, desc="Running Aider for repos"
233193
) as pbar:

0 commit comments

Comments
 (0)