Skip to content

Commit 12f9e7e

Browse files
committed
Refactor project generation response handling to improve file reading and error management
1 parent 4dc9d6e commit 12f9e7e

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

app/main.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ async def generate_project_sync(request: ProjectRequest):
569569
}
570570
"""
571571

572+
# Write files
572573
file_paths = parser.write_files(files, temp_dir)
573574

574575
status.update({
@@ -581,9 +582,6 @@ async def generate_project_sync(request: ProjectRequest):
581582
# Compile the project
582583
success, output = compiler.build_project(temp_dir)
583584

584-
# Initialize similar_errors here to avoid the "referenced before assignment" error
585-
similar_errors = []
586-
587585
if not success:
588586
# Project failed to compile, try to fix errors
589587
status.update({
@@ -652,9 +650,21 @@ async def generate_project_sync(request: ProjectRequest):
652650
"run_output": run_output if run_success else "Failed to run project"
653651
})
654652

655-
# Return all files as text with build success marker
656-
all_files_content = "\n".join([f"[filename: {f}]\n{open(os.path.join(temp_dir, f)).read()}\n" for f in file_paths])
657-
all_files_content += "\n# Build succeeded\n"
653+
# Create the response content INSIDE the tempdir context
654+
all_files_content = ""
655+
for f in file_paths:
656+
try:
657+
file_path = os.path.join(temp_dir, f)
658+
if os.path.exists(file_path):
659+
with open(file_path, 'r') as file:
660+
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
661+
except Exception as e:
662+
print(f"Error reading file {f}: {e}")
663+
664+
# Add build status
665+
all_files_content += "\n# Build " + ("succeeded" if success else "failed") + "\n"
666+
667+
# Return the response while still inside the context
658668
return PlainTextResponse(content=all_files_content)
659669
else:
660670
status.update({

0 commit comments

Comments
 (0)