Keeby-smilyai commited on
Commit
fc7720c
·
verified ·
1 Parent(s): 9eb402c

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +7 -10
backend.py CHANGED
@@ -140,7 +140,7 @@ def generate_with_model(role: str, prompt: str) -> str:
140
  print(f"Error during model generation for role {role}: {e}")
141
  return f'{{"error": "Failed to generate response: {str(e)}"}}'
142
 
143
- # ------------------------------ THE AGENT CHAIN EXECUTOR (REWRITTEN FOR RELIABILITY) ------------------------------
144
  def run_agent_chain(project_id, user_id, initial_prompt):
145
  project_dir = get_project_dir(user_id, project_id)
146
  log_entries = []
@@ -152,8 +152,7 @@ def run_agent_chain(project_id, user_id, initial_prompt):
152
  update_project_status(project_id, "running", logs="".join(log_entries))
153
 
154
  try:
155
- log_step("SYSTEM", f"Initializing project directory...")
156
- update_project_status(project_id, "running", logs="Agent team is assembling...")
157
 
158
  # 1. PLANNER
159
  log_step("PLANNER", "Analyzing user request and creating a project plan...")
@@ -161,12 +160,11 @@ def run_agent_chain(project_id, user_id, initial_prompt):
161
  plan_data = _extract_json(plan_response)
162
  if not plan_data or "files" not in plan_data: raise ValueError("Planner failed to create a valid JSON plan with a 'files' key.")
163
 
164
- ## ROBUSTNESS FIX: Handle cases where the LLM returns [{"file": "path"}] instead of ["path"]
165
  if plan_data["files"] and isinstance(plan_data["files"][0], dict):
166
- log_step("SYSTEM", "Planner returned a list of objects. Normalizing to a list of strings.")
167
  plan_data["files"] = [item["file"] for item in plan_data["files"] if "file" in item]
168
 
169
- log_step("PLANNER", "Plan created successfully.", json.dumps(plan_data, indent=2))
170
 
171
  # 2. ARCHITECT
172
  log_step("ARCHITECT", "Creating initial file skeletons...")
@@ -193,11 +191,10 @@ def run_agent_chain(project_id, user_id, initial_prompt):
193
  if not code_content: continue
194
  review_prompt = f"Review this code from `{file_path}`:\n\n{code_content}"
195
  review_response = generate_with_model("reviewer", review_prompt)
196
- review_data = _extract_json(review_response)
197
- log_step("REVIEWER", f"Review of `{file_path}` complete.", json.dumps(review_data, indent=2))
198
 
199
  # 5. TESTER
200
- log_step("TESTER", "Writing unit tests for all source code...")
201
  for file_path in source_files:
202
  code_content = read_file(project_dir, file_path)
203
  if not code_content: continue
@@ -221,7 +218,7 @@ def run_agent_chain(project_id, user_id, initial_prompt):
221
  log_step("SYSTEM", "Packaging project into a ZIP file...")
222
  zip_path = zip_project(project_dir, project_id)
223
  update_project_status(project_id, "completed", logs="".join(log_entries), zip_path=zip_path)
224
- log_step("SYSTEM", "Project completed successfully!", f"Download available.")
225
 
226
  except Exception as e:
227
  tb_str = traceback.format_exc()
 
140
  print(f"Error during model generation for role {role}: {e}")
141
  return f'{{"error": "Failed to generate response: {str(e)}"}}'
142
 
143
+ # ------------------------------ THE AGENT CHAIN EXECUTOR ------------------------------
144
  def run_agent_chain(project_id, user_id, initial_prompt):
145
  project_dir = get_project_dir(user_id, project_id)
146
  log_entries = []
 
152
  update_project_status(project_id, "running", logs="".join(log_entries))
153
 
154
  try:
155
+ log_step("SYSTEM", "Initializing project...")
 
156
 
157
  # 1. PLANNER
158
  log_step("PLANNER", "Analyzing user request and creating a project plan...")
 
160
  plan_data = _extract_json(plan_response)
161
  if not plan_data or "files" not in plan_data: raise ValueError("Planner failed to create a valid JSON plan with a 'files' key.")
162
 
 
163
  if plan_data["files"] and isinstance(plan_data["files"][0], dict):
164
+ log_step("SYSTEM", "Correcting malformed file list from Planner...")
165
  plan_data["files"] = [item["file"] for item in plan_data["files"] if "file" in item]
166
 
167
+ log_step("PLANNER", "Plan created.", json.dumps(plan_data, indent=2))
168
 
169
  # 2. ARCHITECT
170
  log_step("ARCHITECT", "Creating initial file skeletons...")
 
191
  if not code_content: continue
192
  review_prompt = f"Review this code from `{file_path}`:\n\n{code_content}"
193
  review_response = generate_with_model("reviewer", review_prompt)
194
+ log_step("REVIEWER", f"Review of `{file_path}` complete.", review_response)
 
195
 
196
  # 5. TESTER
197
+ log_step("TESTER", "Writing unit tests...")
198
  for file_path in source_files:
199
  code_content = read_file(project_dir, file_path)
200
  if not code_content: continue
 
218
  log_step("SYSTEM", "Packaging project into a ZIP file...")
219
  zip_path = zip_project(project_dir, project_id)
220
  update_project_status(project_id, "completed", logs="".join(log_entries), zip_path=zip_path)
221
+ log_step("SYSTEM", "Project completed successfully!")
222
 
223
  except Exception as e:
224
  tb_str = traceback.format_exc()