Spaces:
Runtime error
Runtime error
Update backend/orchestrator.py
Browse files114 unmodified lines1151161171181191181191201211221231241251261251271281291302 unmodified lines1331341351341361371381395 unmodified lines14514614714614714814915015015115215315415515615115215315415515615715815916016115916016116216316416516616716216316416916516616716816917017117217317417517617117217317717817918018118210 unmodified lines193194195196190191192193194195197198199200201202203204205206207208198199200209210211212213214215216217218219220221222223224225209226227228229114 unmodified lines log_step("ORCHESTRATOR", "All planned tasks completed. Instructing Manager to finish project.") next_agent = "manager" project_context["current_task"] = "All tasks from the plan are complete. Finish the project." current_plan_step = project_context["detailed_plan"][project_context["current_task_index"]] else: current_plan_step = project_context["detailed_plan"][project_context["current_task_index"]] next_agent = current_plan_step["responsible_agent"] project_context["current_task"] = current_plan_step["description"] prompt = f""" Project context: - User's Goal: {project_context['initial_prompt']} - Detailed Plan: {project_context['detailed_plan']} - Current Task: {current_plan_step['description']} - Current Task: {project_context['current_task']} - Result of last action: {project_context['last_action_result']} - Last error: {project_context['error_state']} 2 unmodified lines response_text = generate_with_model(next_agent, prompt) tool_call = _extract_tool_call(response_text)
if not tool_call: project_context["error_state"] = f"Invalid response from agent '{next_agent}'. Raw output: {response_text}" next_agent = "debugger"5 unmodified lines log_step(next_agent.replace("_", " ").title(), f"Decided to perform action: `{action}` with args: `{args}`")
if action == "delegate_coder": next_agent = current_plan_step["responsible_agent"] project_context["current_task"] = args[0] # Handled implicitly by setting next_agent from plan, but kept for clarity project_context["last_action_result"] = f"Task delegated to {next_agent.replace('_', ' ')}." elif action in ["write_code", "write_test", "task_complete"]: if action == "write_code" and len(args) >= 2: create_file(project_dir, args[0], args[1]) if action == "write_test" and len(args) >= 2: create_file(project_dir, args[0], args[1]) project_context["last_action_result"] = f"Worker finished task and saved to file. Moving to next step in the plan." project_context["current_task_index"] += 1 next_agent = "manager" elif action in ["write_code", "write_test"]: if len(args) >= 2: create_file(project_dir, args[0], args[1]) project_context["last_action_result"] = f"Worker finished task and saved to file '{args[0]}'." project_context["current_task_index"] += 1 next_agent = "manager" else: project_context["error_state"] = f"Action '{action}' requires file path and content. Missing arguments." next_agent = "debugger" elif action == "run_test": if not args: raise ValueError("Missing file path for run_test.") test_file_path = args[0] test_output = run_test_in_sandbox(project_dir, test_file_path) project_context["last_action_result"] = f"Test run for {test_file_path}:\n{test_output}" log_step("TESTER", "Test run complete.", test_output) if "Sandbox Execution Error" in test_output or "Traceback (most recent call last)" in test_output: log_step("ORCHESTRATOR", "Tests failed. Handing over to Debugger.") if not args: project_context["last_action_result"] = "Error: Missing file path for run_test." next_agent = "debugger" project_context["error_state"] = test_output project_context["error_state"] = project_context["last_action_result"] else: test_file_path = args[0] test_output = run_test_in_sandbox(project_dir, test_file_path) project_context["last_action_result"] = f"Test run for {test_file_path}:\n{test_output}" log_step("TESTER", "Test run complete.", test_output) if "Sandbox Execution Error" in test_output or "Traceback (most recent call last)" in test_output: log_step("ORCHESTRATOR", "Tests failed. Handing over to Debugger.") next_agent = "debugger" project_context["error_state"] = test_output else: log_step("ORCHESTRATOR", "Tests passed. Continuing with the plan.") project_context["current_task_index"] += 1 next_agent = "manager" log_step("ORCHESTRATOR", "Tests passed. Continuing with the plan.") project_context["current_task_index"] += 1 next_agent = "manager" elif action == "run_bandit": bandit_report = run_bandit(project_dir)10 unmodified lines next_agent = "manager"
elif action == "propose_fix": if len(args) >= 3: fix_description = args[2] file_path = args[0] project_context["last_action_result"] = f"Debugger proposed a fix for {file_path}: {fix_description}" project_context["current_task"] = f"Apply the fix to {file_path} for the following issue: {fix_description}" next_agent = "manager" project_context["error_state"] = None fix_description = args[2] file_path = args[0] project_context["last_action_result"] = f"Debugger proposed a fix for {file_path}: {fix_description}" project_context["current_task"] = f"Apply the fix to {file_path} for the following issue: {fix_description}" next_agent = "manager" project_context["error_state"] = None else: project_context["error_state"] = "Action 'propose_fix' requires file path, old code, and fix description. Missing arguments." next_agent = "debugger" elif action == "ask_manager": if len(args) >= 1: project_context["last_action_result"] = f"Agent has a question: {args[0]}" project_context["current_task"] = args[0] next_agent = "manager" project_context["last_action_result"] = f"Agent has a question: {args[0]}" project_context["current_task"] = args[0] next_agent = "manager" else: project_context["error_state"] = "Action 'ask_manager' requires a question. Missing arguments." next_agent = "debugger" elif action == "scrape_web": if not args: project_context["last_action_result"] = "Error: Missing URL for scrape_web action." project_context["error_state"] = project_context["last_action_result"] next_agent = "debugger" else: url = args[0] scraped_content = scrape_web(url) project_context["last_action_result"] = f"Scraping result from {url}:\n\n{scraped_content}" # This tool call doesn't advance the plan, so we continue the loop from the current task continue continue elif action == "finish_project": log_step("MANAGER", "Project is ready for final packaging.")backend/orchestrator.py+53-36