Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPERIMENTAL - add syntax check to reviewer #637

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions pilot/helpers/agents/CodeMonkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def implement_code_changes(
files,
)

if content and content != file_content:
content = self.format_file(file_name, content)

# Review the changes and only apply changes that are useful/approved
if content and content != file_content:
content = self.review_change(convo, code_change_description, file_name, file_content, content)
Expand Down Expand Up @@ -172,6 +175,46 @@ def identify_file_to_change(self, code_changes_description: str, files: list[dic
}, GET_FILE_TO_MODIFY)
return llm_response["file"]

def format_file(
self,
file_name: str,
content: str,
) -> str:
"""
Format the file using an available formatter, if any.
"""
def prettier(name: str) -> list[str]:
return ["npx", "prettier", "--stdin-filepath", name]

FORMATTERS = {
".js": prettier,
".json": prettier,
".ts": prettier,
".jsx": prettier,
".vue": prettier,
".tsx": prettier,
".css": prettier,
".scss": prettier,
".py": lambda name: ["ruff", "format", "--stdin-filename", name],
".go": lambda _: ["gofmt"],
}

_, file_ext = os.path.splitext(file_name)
formatter = FORMATTERS.get(file_ext, None)
if not formatter:
return

from subprocess import check_output, CalledProcessError
format_cmd = formatter(file_name)
try:
return check_output(format_cmd, input=content, text=True)
except FileNotFoundError:
# formatter not installer, silently ignore and do nothing
return content
except CalledProcessError as e:
raise ValueError(f"Syntax check failed with:\n{e.stderr}")


def review_change(
self,
convo: AgentConvo,
Expand Down