AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
AutoGPT: Handle Agent self-termination gracefully
Reinier van der Leerfac52a6bc642
diff --git a/autogpts/autogpt/autogpt/app/agent_protocol_server.py b/autogpts/autogpt/autogpt/app/agent_protocol_server.pyindex b6131ae651..4bfc1940ef 100644--- a/autogpts/autogpt/autogpt/app/agent_protocol_server.py+++ b/autogpts/autogpt/autogpt/app/agent_protocol_server.py@@ -29,6 +29,8 @@ from autogpt.agent_factory.configurators import configure_agent_with_statefrom autogpt.agent_factory.generators import generate_agent_for_taskfrom autogpt.agent_manager import AgentManagerfrom autogpt.config import Config+from autogpt.commands.system import finish+from autogpt.commands.user_interaction import ask_userfrom autogpt.core.resource.model_providers import ChatModelProviderfrom autogpt.file_workspace import FileWorkspacefrom autogpt.models.action_history import ActionSuccessResult@@ -171,15 +173,13 @@ class AgentProtocolServer:relative_path=str(path),)- # Save step request- step = await self.db.create_step(task_id=task_id, input=step_request)-# According to the Agent Protocol spec, the first execute_step request contains# the same task input as the parent create_task request.# To prevent this from interfering with the agent's process, we ignore the input# of this first step request, and just generate the first step proposal.is_init_step = not bool(agent.event_history)execute_command, execute_command_args, execute_result = None, None, None+ execute_approved = Falseif is_init_step:step_request.input = ""elif (@@ -188,11 +188,38 @@ class AgentProtocolServer:):execute_command = agent.event_history.current_episode.action.nameexecute_command_args = agent.event_history.current_episode.action.args+ execute_approved = not step_request.input or step_request.input == "y"++ logger.debug(+ f"Agent proposed command"+ f" {execute_command}({fmt_kwargs(execute_command_args)})."+ f" User input/feedback: {repr(step_request.input)}"+ )++ # Save step request+ step = await self.db.create_step(+ task_id=task_id,+ input=step_request,+ is_last=execute_command == finish.__name__ and execute_approved,+ )++ # Execute previously proposed action+ if execute_command:+ assert execute_command_args is not None++ if step.is_last and execute_command == finish.__name__:+ assert execute_command_args+ step = await self.db.update_step(+ task_id=task_id,+ step_id=step.step_id,+ output=execute_command_args["reason"],+ )+ return step- if agent.event_history.current_episode.action.name == "ask_user": # HACK+ if execute_command == ask_user.__name__: # HACKexecute_result = ActionSuccessResult(outputs=step_request.input)agent.event_history.register_result(execute_result)- elif not step_request.input or step_request.input == "y":+ elif execute_approved:step = await self.db.update_step(task_id=task_id,step_id=step.step_id,@@ -204,6 +231,7 @@ class AgentProtocolServer:command_args=execute_command_args,)else:+ assert step_request.inputexecute_result = await agent.execute(command_name="human_feedback", # HACKcommand_args={},diff --git a/autogpts/autogpt/autogpt/commands/system.py b/autogpts/autogpt/autogpt/commands/system.pyindex 8b14354592..d1a879c139 100644--- a/autogpts/autogpt/autogpt/commands/system.py+++ b/autogpts/autogpt/autogpt/commands/system.py@@ -12,7 +12,7 @@ if TYPE_CHECKING:from autogpt.agents.agent import Agentfrom autogpt.agents.features.context import get_agent_context-from autogpt.agents.utils.exceptions import InvalidArgumentError+from autogpt.agents.utils.exceptions import AgentTerminated, InvalidArgumentErrorfrom autogpt.command_decorator import commandfrom autogpt.core.utils.json_schema import JSONSchema@@ -42,8 +42,7 @@ def finish(reason: str, agent: Agent) -> None:A result string from create chat completion. A list of suggestions toimprove the code."""- logger.info(reason, extra={"title": "Shutting down...\n"})- quit()+ raise AgentTerminated(reason)@command(