Spaces:
Runtime error
Runtime error
Update backend/orchestrator.py
Browse files114 unmodified lines1151161171181191201211221231241251261271282 unmodified lines1311321331341351361375 unmodified lines14314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517610 unmodified lines187188189190191192193194195196197198199200201202203204205206207208209210211212114 unmodified lineslog_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"]]prompt = f"""Project context:- User's Goal: {project_context['initial_prompt']}- Detailed Plan: {project_context['detailed_plan']}- Current Task: {current_plan_step['description']}- Result of last action: {project_context['last_action_result']}- Last error: {project_context['error_state']}2 unmodified linesresponse_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 lineslog_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]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"] += 1next_agent = "manager"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.")next_agent = "debugger"project_context["error_state"] = test_outputelse:log_step("ORCHESTRATOR", "Tests passed. Continuing with the plan.")project_context["current_task_index"] += 1next_agent = "manager"elif action == "run_bandit":bandit_report = run_bandit(project_dir)10 unmodified linesnext_agent = "manager"elif action == "propose_fix":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"] = Noneelif action == "ask_manager":project_context["last_action_result"] = f"Agent has a question: {args[0]}"project_context["current_task"] = args[0]next_agent = "manager"elif action == "scrape_web":if not args:project_context["last_action_result"] = "Error: Missing URL for scrape_web action."else:url = args[0]scraped_content = scrape_web(url)project_context["last_action_result"] = f"Scraping result from {url}:\n\n{scraped_content}"continueelif action == "finish_project":log_step("MANAGER", "Project is ready for final packaging.")114 unmodified lines1151161171181191201211221231241251261271281291302 unmodified lines1331341351361371381395 unmodified lines14514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118210 unmodified lines193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229114 unmodified lineslog_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."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: {project_context['current_task']}- Result of last action: {project_context['last_action_result']}- Last error: {project_context['error_state']}2 unmodified linesresponse_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 lineslog_step(next_agent.replace("_", " ").title(), f"Decided to perform action: `{action}` with args: `{args}`")if action == "delegate_coder":# Handled implicitly by setting next_agent from plan, but kept for clarityproject_context["last_action_result"] = f"Task delegated to {next_agent.replace('_', ' ')}."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"] += 1next_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:project_context["last_action_result"] = "Error: Missing file path for run_test."next_agent = "debugger"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_outputelse:log_step("ORCHESTRATOR", "Tests passed. Continuing with the plan.")project_context["current_task_index"] += 1next_agent = "manager"elif action == "run_bandit":bandit_report = run_bandit(project_dir)10 unmodified linesnext_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"] = Noneelse: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"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 taskcontinueelif action == "finish_project":log_step("MANAGER", "Project is ready for final packaging.")
backend/orchestrator.py+53-36