Update backend/orchestrator.py
Browse files- backend/orchestrator.py +53 -36
backend/orchestrator.py
CHANGED
|
@@ -115,14 +115,16 @@ def run_agent_chain(project_id, user_id, initial_prompt):
|
|
| 115 |
log_step("ORCHESTRATOR", "All planned tasks completed. Instructing Manager to finish project.")
|
| 116 |
next_agent = "manager"
|
| 117 |
project_context["current_task"] = "All tasks from the plan are complete. Finish the project."
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
|
| 121 |
prompt = f"""
|
| 122 |
Project context:
|
| 123 |
- User's Goal: {project_context['initial_prompt']}
|
| 124 |
- Detailed Plan: {project_context['detailed_plan']}
|
| 125 |
-
- Current Task: {
|
| 126 |
- Result of last action: {project_context['last_action_result']}
|
| 127 |
- Last error: {project_context['error_state']}
|
| 128 |
|
|
@@ -131,7 +133,7 @@ def run_agent_chain(project_id, user_id, initial_prompt):
|
|
| 131 |
|
| 132 |
response_text = generate_with_model(next_agent, prompt)
|
| 133 |
tool_call = _extract_tool_call(response_text)
|
| 134 |
-
|
| 135 |
if not tool_call:
|
| 136 |
project_context["error_state"] = f"Invalid response from agent '{next_agent}'. Raw output: {response_text}"
|
| 137 |
next_agent = "debugger"
|
|
@@ -143,34 +145,38 @@ def run_agent_chain(project_id, user_id, initial_prompt):
|
|
| 143 |
log_step(next_agent.replace("_", " ").title(), f"Decided to perform action: `{action}` with args: `{args}`")
|
| 144 |
|
| 145 |
if action == "delegate_coder":
|
| 146 |
-
next_agent
|
| 147 |
-
project_context["current_task"] = args[0]
|
| 148 |
project_context["last_action_result"] = f"Task delegated to {next_agent.replace('_', ' ')}."
|
| 149 |
|
| 150 |
-
elif action in ["write_code", "write_test"
|
| 151 |
-
if
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
elif action == "run_test":
|
| 159 |
-
if not args:
|
| 160 |
-
|
| 161 |
-
test_output = run_test_in_sandbox(project_dir, test_file_path)
|
| 162 |
-
|
| 163 |
-
project_context["last_action_result"] = f"Test run for {test_file_path}:\n{test_output}"
|
| 164 |
-
log_step("TESTER", "Test run complete.", test_output)
|
| 165 |
-
|
| 166 |
-
if "Sandbox Execution Error" in test_output or "Traceback (most recent call last)" in test_output:
|
| 167 |
-
log_step("ORCHESTRATOR", "Tests failed. Handing over to Debugger.")
|
| 168 |
next_agent = "debugger"
|
| 169 |
-
project_context["error_state"] =
|
| 170 |
else:
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
elif action == "run_bandit":
|
| 176 |
bandit_report = run_bandit(project_dir)
|
|
@@ -187,26 +193,37 @@ def run_agent_chain(project_id, user_id, initial_prompt):
|
|
| 187 |
next_agent = "manager"
|
| 188 |
|
| 189 |
elif action == "propose_fix":
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
elif action == "ask_manager":
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
elif action == "scrape_web":
|
| 203 |
if not args:
|
| 204 |
project_context["last_action_result"] = "Error: Missing URL for scrape_web action."
|
|
|
|
|
|
|
| 205 |
else:
|
| 206 |
url = args[0]
|
| 207 |
scraped_content = scrape_web(url)
|
| 208 |
project_context["last_action_result"] = f"Scraping result from {url}:\n\n{scraped_content}"
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
elif action == "finish_project":
|
| 212 |
log_step("MANAGER", "Project is ready for final packaging.")
|
|
|
|
| 115 |
log_step("ORCHESTRATOR", "All planned tasks completed. Instructing Manager to finish project.")
|
| 116 |
next_agent = "manager"
|
| 117 |
project_context["current_task"] = "All tasks from the plan are complete. Finish the project."
|
| 118 |
+
else:
|
| 119 |
+
current_plan_step = project_context["detailed_plan"][project_context["current_task_index"]]
|
| 120 |
+
next_agent = current_plan_step["responsible_agent"]
|
| 121 |
+
project_context["current_task"] = current_plan_step["description"]
|
| 122 |
|
| 123 |
prompt = f"""
|
| 124 |
Project context:
|
| 125 |
- User's Goal: {project_context['initial_prompt']}
|
| 126 |
- Detailed Plan: {project_context['detailed_plan']}
|
| 127 |
+
- Current Task: {project_context['current_task']}
|
| 128 |
- Result of last action: {project_context['last_action_result']}
|
| 129 |
- Last error: {project_context['error_state']}
|
| 130 |
|
|
|
|
| 133 |
|
| 134 |
response_text = generate_with_model(next_agent, prompt)
|
| 135 |
tool_call = _extract_tool_call(response_text)
|
| 136 |
+
|
| 137 |
if not tool_call:
|
| 138 |
project_context["error_state"] = f"Invalid response from agent '{next_agent}'. Raw output: {response_text}"
|
| 139 |
next_agent = "debugger"
|
|
|
|
| 145 |
log_step(next_agent.replace("_", " ").title(), f"Decided to perform action: `{action}` with args: `{args}`")
|
| 146 |
|
| 147 |
if action == "delegate_coder":
|
| 148 |
+
# Handled implicitly by setting next_agent from plan, but kept for clarity
|
|
|
|
| 149 |
project_context["last_action_result"] = f"Task delegated to {next_agent.replace('_', ' ')}."
|
| 150 |
|
| 151 |
+
elif action in ["write_code", "write_test"]:
|
| 152 |
+
if len(args) >= 2:
|
| 153 |
+
create_file(project_dir, args[0], args[1])
|
| 154 |
+
project_context["last_action_result"] = f"Worker finished task and saved to file '{args[0]}'."
|
| 155 |
+
project_context["current_task_index"] += 1
|
| 156 |
+
next_agent = "manager"
|
| 157 |
+
else:
|
| 158 |
+
project_context["error_state"] = f"Action '{action}' requires file path and content. Missing arguments."
|
| 159 |
+
next_agent = "debugger"
|
| 160 |
|
| 161 |
elif action == "run_test":
|
| 162 |
+
if not args:
|
| 163 |
+
project_context["last_action_result"] = "Error: Missing file path for run_test."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
next_agent = "debugger"
|
| 165 |
+
project_context["error_state"] = project_context["last_action_result"]
|
| 166 |
else:
|
| 167 |
+
test_file_path = args[0]
|
| 168 |
+
test_output = run_test_in_sandbox(project_dir, test_file_path)
|
| 169 |
+
project_context["last_action_result"] = f"Test run for {test_file_path}:\n{test_output}"
|
| 170 |
+
log_step("TESTER", "Test run complete.", test_output)
|
| 171 |
+
|
| 172 |
+
if "Sandbox Execution Error" in test_output or "Traceback (most recent call last)" in test_output:
|
| 173 |
+
log_step("ORCHESTRATOR", "Tests failed. Handing over to Debugger.")
|
| 174 |
+
next_agent = "debugger"
|
| 175 |
+
project_context["error_state"] = test_output
|
| 176 |
+
else:
|
| 177 |
+
log_step("ORCHESTRATOR", "Tests passed. Continuing with the plan.")
|
| 178 |
+
project_context["current_task_index"] += 1
|
| 179 |
+
next_agent = "manager"
|
| 180 |
|
| 181 |
elif action == "run_bandit":
|
| 182 |
bandit_report = run_bandit(project_dir)
|
|
|
|
| 193 |
next_agent = "manager"
|
| 194 |
|
| 195 |
elif action == "propose_fix":
|
| 196 |
+
if len(args) >= 3:
|
| 197 |
+
fix_description = args[2]
|
| 198 |
+
file_path = args[0]
|
| 199 |
+
project_context["last_action_result"] = f"Debugger proposed a fix for {file_path}: {fix_description}"
|
| 200 |
+
project_context["current_task"] = f"Apply the fix to {file_path} for the following issue: {fix_description}"
|
| 201 |
+
next_agent = "manager"
|
| 202 |
+
project_context["error_state"] = None
|
| 203 |
+
else:
|
| 204 |
+
project_context["error_state"] = "Action 'propose_fix' requires file path, old code, and fix description. Missing arguments."
|
| 205 |
+
next_agent = "debugger"
|
| 206 |
|
| 207 |
elif action == "ask_manager":
|
| 208 |
+
if len(args) >= 1:
|
| 209 |
+
project_context["last_action_result"] = f"Agent has a question: {args[0]}"
|
| 210 |
+
project_context["current_task"] = args[0]
|
| 211 |
+
next_agent = "manager"
|
| 212 |
+
else:
|
| 213 |
+
project_context["error_state"] = "Action 'ask_manager' requires a question. Missing arguments."
|
| 214 |
+
next_agent = "debugger"
|
| 215 |
|
| 216 |
elif action == "scrape_web":
|
| 217 |
if not args:
|
| 218 |
project_context["last_action_result"] = "Error: Missing URL for scrape_web action."
|
| 219 |
+
project_context["error_state"] = project_context["last_action_result"]
|
| 220 |
+
next_agent = "debugger"
|
| 221 |
else:
|
| 222 |
url = args[0]
|
| 223 |
scraped_content = scrape_web(url)
|
| 224 |
project_context["last_action_result"] = f"Scraping result from {url}:\n\n{scraped_content}"
|
| 225 |
+
# This tool call doesn't advance the plan, so we continue the loop from the current task
|
| 226 |
+
continue
|
| 227 |
|
| 228 |
elif action == "finish_project":
|
| 229 |
log_step("MANAGER", "Project is ready for final packaging.")
|