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

Add log statements #4105

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions sweepai/agents/modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,36 @@ def generate_code_suggestions(
if fcr.filename not in modify_order:
modify_order.append(fcr.filename)

code_suggestions = []
applied_code_suggestions = []
for file_path in modify_order:
if file_path in modify_files_dict:
file_data = modify_files_dict[file_path]
if file_data["original_contents"] != file_data["contents"]:
code_suggestions.append(StatefulCodeSuggestion(
applied_code_suggestions.append(StatefulCodeSuggestion(
file_path=file_path,
original_code=file_data["original_contents"],
new_code=file_data["contents"],
file_contents=file_data["original_contents"],
state="done"
))

current_fcr_index = next((i for i, fcr in enumerate(fcrs) if not fcr.is_completed), -1)
if current_fcr_index >= 0:
for i, fcr in enumerate(fcrs):
if i < current_fcr_index:
continue
else:
parsed_fcr = parse_fcr(fcr)
try:
file_contents = cloned_repo.get_file_contents(fcr.filename)
except FileNotFoundError:
file_contents = ""
code_suggestions.append(StatefulCodeSuggestion(
file_path=fcr.filename,
original_code=parsed_fcr["original_code"][0] if parsed_fcr["original_code"] else "",
new_code=parsed_fcr["new_code"][0] if parsed_fcr["new_code"] else "",
file_contents=file_contents,
state=("processing" if i == current_fcr_index else "pending"),
error=error_messages_dict.get(i, None)
))
return code_suggestions
queued_code_suggestions = []
current_fcr_index = next((i for i, fcr in enumerate(fcrs) if not fcr.is_completed), len(fcrs))
for i, fcr in enumerate(fcrs):
parsed_fcr = parse_fcr(fcr)
try:
file_contents = cloned_repo.get_file_contents(fcr.filename)
except FileNotFoundError:
file_contents = ""
queued_code_suggestions.append(StatefulCodeSuggestion(
file_path=fcr.filename,
original_code=parsed_fcr["original_code"][0] if parsed_fcr["original_code"] else "",
new_code=parsed_fcr["new_code"][0] if parsed_fcr["new_code"] else "",
file_contents=file_contents,
state=("processing" if i == current_fcr_index else ("pending" if i > current_fcr_index else "done")),
error=error_messages_dict.get(i, None)
))
return applied_code_suggestions, queued_code_suggestions

@streamable
def modify(
Expand Down
15 changes: 11 additions & 4 deletions sweepai/chat/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ def handle_function_call(function_call: AnthropicFunctionCall, repo_name: str, s
async def autofix(
repo_name: str = Body(...),
code_suggestions: list[CodeSuggestion] = Body(...),
modify_files_dict: dict = Body({}),
branch: str = Body(None),
access_token: str = Depends(get_token_header)
):# -> dict[str, Any] | StreamingResponse:
Expand Down Expand Up @@ -867,13 +868,17 @@ async def autofix(

def stream():
try:
for stateful_code_suggestions in modify.stream(
for applied_code_suggestions, queued_code_suggestions in modify.stream(
fcrs=file_change_requests,
request="",
cloned_repo=cloned_repo,
relevant_filepaths=[code_suggestion.file_path for code_suggestion in code_suggestions],
previous_modify_files_dict=modify_files_dict
):
yield json.dumps([stateful_code_suggestion.__dict__ for stateful_code_suggestion in stateful_code_suggestions])
yield json.dumps({
"applied_code_suggestions": [stateful_code_suggestion.__dict__ for stateful_code_suggestion in applied_code_suggestions],
"queued_code_suggestions": [stateful_code_suggestion.__dict__ for stateful_code_suggestion in queued_code_suggestions]
})
except Exception as e:
yield json.dumps({"error": str(e)})
raise e
Expand Down Expand Up @@ -1060,7 +1065,9 @@ async def create_pull_metadata(
@app.post("/backend/validate_pull")
async def validate_pull(
repo_name: str = Body(...),
# branch: str = Body(...),
pull_request_number: int = Body(...),
modify_files_dict: dict = Body({}),
access_token: str = Depends(get_token_header)
):
with Timer() as timer:
Expand Down Expand Up @@ -1163,7 +1170,6 @@ async def write_message_to_disk(
repo_name: str = Body(...),
messages: list[Message] = Body(...),
snippets: list[Snippet] = Body(...),
original_code_suggestions: list = Body([]),
code_suggestions: list = Body([]),
pull_request: dict | None = Body(None),
pull_request_title: str = Body(""),
Expand All @@ -1180,7 +1186,6 @@ async def write_message_to_disk(
"repo_name": repo_name,
"messages": [message.model_dump() for message in messages],
"snippets": [snippet.model_dump() for snippet in snippets],
"original_code_suggestions": [code_suggestion.__dict__ if isinstance(code_suggestion, CodeSuggestion) else code_suggestion for code_suggestion in original_code_suggestions],
"code_suggestions": [code_suggestion.__dict__ if isinstance(code_suggestion, CodeSuggestion) else code_suggestion for code_suggestion in code_suggestions],
"pull_request": pull_request,
"user_mentioned_pull_request": user_mentioned_pull_request,
Expand All @@ -1191,6 +1196,7 @@ async def write_message_to_disk(
}
with open(f"{CACHE_DIRECTORY}/messages/{message_id}.json", "w") as file:
json.dump(data, file)
logger.info(f"Saved {len(messages)} messages to {message_id}.json")
return {"status": "success", "message": "Message written to disk successfully.", "message_id": message_id}
except Exception as e:
logger.error(f"Failed to write message to disk: {str(e)}")
Expand All @@ -1203,6 +1209,7 @@ async def read_message_from_disk(
try:
with open(f"{CACHE_DIRECTORY}/messages/{message_id}.json", "r") as file:
message_data = json.load(file)
logger.info(f"Loaded {len(message_data['messages'])} messages from {message_id}.json")
return {
"status": "success",
"message": "Message read from disk successfully.",
Expand Down
Loading