Skip to content

Commit 0c7ef11

Browse files
committed
Refactor project generation logic to remove unnecessary status saves and improve file content handling
1 parent f9ac41f commit 0c7ef11

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

app/main.py

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,6 @@ async def generate_project_sync(request: ProjectRequest):
552552

553553
# Ensure essential files exist
554554
if "Cargo.toml" not in files:
555-
# Create a basic Cargo.toml if it's missing
556555
project_name = request.description.lower().replace(" ", "_").replace("-", "_")[:20]
557556
files["Cargo.toml"] = f"""[package]
558557
name = "{project_name}"
@@ -563,7 +562,6 @@ async def generate_project_sync(request: ProjectRequest):
563562
"""
564563

565564
if "src/main.rs" not in files and "src\\main.rs" not in files:
566-
# Create a basic main.rs if it's missing
567565
files["src/main.rs"] = """fn main() {
568566
println!("Hello, world!");
569567
}
@@ -577,7 +575,9 @@ async def generate_project_sync(request: ProjectRequest):
577575
"message": "Compiling project",
578576
"files": file_paths
579577
})
580-
save_status(temp_dir, status)
578+
579+
# DON'T save status here - remove this line
580+
# save_status(temp_dir, status)
581581

582582
# Compile the project
583583
success, output = compiler.build_project(temp_dir)
@@ -589,7 +589,9 @@ async def generate_project_sync(request: ProjectRequest):
589589
"message": "Compilation failed, attempting to fix",
590590
"build_output": output
591591
})
592-
save_status(temp_dir, status)
592+
593+
# DON'T save status here - remove this line
594+
# save_status(temp_dir, status)
593595

594596
# Extract error context
595597
error_context = compiler.extract_error_context(output)
@@ -604,16 +606,15 @@ async def generate_project_sync(request: ProjectRequest):
604606
similar_errors = vector_store.search("error_examples", error_embedding, limit=3)
605607
except Exception as e:
606608
print(f"Vector search error (non-critical): {e}")
607-
# Continue without vector search results
608609

609-
# Generate fix prompt
610-
fix_examples = ""
611-
if similar_errors:
612-
fix_examples = "Here are some examples of similar errors and their fixes:\n\n"
613-
for i, err in enumerate(similar_errors):
614-
fix_examples += f"Example {i+1}:\n{err['error']}\nFix: {err['solution']}\n\n"
610+
# Generate fix prompt
611+
fix_examples = ""
612+
if similar_errors:
613+
fix_examples = "Here are some examples of similar errors and their fixes:\n\n"
614+
for i, err in enumerate(similar_errors):
615+
fix_examples += f"Example {i+1}:\n{err['error']}\nFix: {err['solution']}\n\n"
615616

616-
fix_prompt = f"""
617+
fix_prompt = f"""
617618
Here is a Rust project that failed to compile. Help me fix the compilation errors.
618619
619620
Project description: {request.description}
@@ -626,55 +627,58 @@ async def generate_project_sync(request: ProjectRequest):
626627
Please provide the fixed code for all affected files.
627628
"""
628629

629-
# Get fix from LLM
630-
fix_response = llm_client.generate_text(fix_prompt)
630+
# Get fix from LLM
631+
fix_response = llm_client.generate_text(fix_prompt)
632+
633+
# Parse and apply fixes
634+
fixed_files = parser.parse_response(fix_response)
635+
for filename, content in fixed_files.items():
636+
file_path = os.path.join(temp_dir, filename)
637+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
638+
with open(file_path, 'w') as f:
639+
f.write(content)
640+
files[filename] = content # Update the files dictionary
641+
642+
# Try compiling again
643+
success, output = compiler.build_project(temp_dir)
631644

632-
# Parse and apply fixes
633-
fixed_files = parser.parse_response(fix_response)
634-
for filename, content in fixed_files.items():
635-
file_path = os.path.join(temp_dir, filename)
636-
with open(file_path, 'w') as f:
637-
f.write(content)
645+
# Create the response content while the tempdir is still available
646+
all_files_content = ""
647+
for f in file_paths:
648+
try:
649+
file_path = os.path.join(temp_dir, f)
650+
if os.path.exists(file_path):
651+
with open(file_path, 'r') as file:
652+
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
653+
except Exception as e:
654+
print(f"Error reading file {f}: {e}")
638655

639-
# Try compiling again
640-
success, output = compiler.build_project(temp_dir)
641-
642-
# Create the response content while the tempdir is still available
643-
all_files_content = ""
644-
for f in file_paths:
645-
try:
646-
file_path = os.path.join(temp_dir, f)
647-
if os.path.exists(file_path):
648-
with open(file_path, 'r') as file:
649-
all_files_content += f"[filename: {f}]\n{file.read()}\n\n"
650-
except Exception as e:
651-
print(f"Error reading file {f}: {e}")
652-
653-
if success:
654-
# Project compiled successfully
655-
status.update({
656-
"status": "completed",
657-
"message": "Project generated successfully",
658-
"build_output": output
659-
})
656+
if success:
657+
# Project compiled successfully
658+
status.update({
659+
"status": "completed",
660+
"message": "Project generated successfully",
661+
"build_output": output
662+
})
663+
664+
# Add build status
665+
all_files_content += "\n# Build succeeded\n"
666+
else:
667+
# Build failed
668+
status.update({
669+
"status": "failed",
670+
"message": "Failed to generate working project",
671+
"build_output": output
672+
})
673+
674+
# Add build status
675+
all_files_content += "\n# Build failed\n"
660676

661-
# Add build status
662-
all_files_content += "\n# Build succeeded\n"
663-
else:
664-
# Build failed
665-
status.update({
666-
"status": "failed",
667-
"message": "Failed to generate working project",
668-
"build_output": output
669-
})
677+
# DON'T save status here - remove this line
678+
# save_status(temp_dir, status)
670679

671-
# Add build status
672-
all_files_content += "\n# Build failed\n"
673-
674-
save_status(temp_dir, status)
675-
676-
# Return the response while still inside the with block
677-
return PlainTextResponse(content=all_files_content)
680+
# Return the response while still inside the with block
681+
return PlainTextResponse(content=all_files_content)
678682

679683
except Exception as e:
680684
raise HTTPException(status_code=500, detail=f"Error generating project: {str(e)}")

0 commit comments

Comments
 (0)