Skip to content

Commit

Permalink
fix(agent): Handle action_history-related exceptions gracefully (Si…
Browse files Browse the repository at this point in the history
…gnificant-Gravitas#6990)

Fix resume-related exceptions

- CLI: prevent resumed agent to register action on already existing one
- Server: prevent trying to json() command without result
  • Loading branch information
kcze authored Mar 12, 2024
1 parent 89cf015 commit fd2c261
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
14 changes: 13 additions & 1 deletion autogpts/autogpt/autogpt/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
from autogpt.core.runner.client_lib.logging.helpers import dump_prompt
from autogpt.file_storage.base import FileStorage
from autogpt.llm.providers.openai import get_openai_command_specs
from autogpt.models.action_history import ActionResult, EpisodicActionHistory
from autogpt.models.action_history import (
ActionResult,
ActionSuccessResult,
EpisodicActionHistory,
)
from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -173,6 +177,14 @@ def __init__(
self.legacy_config = legacy_config
"""LEGACY: Monolithic application configuration."""

# In case the agent is resumed, cursor is set to the last episode
if self.event_history:
# To prevent errors, when the last action is "finish", we register a result
# And move cursor to the next action
if self.event_history.current_episode.action.name == "finish":
self.event_history.register_result(ActionSuccessResult())
self.event_history.cursor = len(self.event_history)

self.llm_provider = llm_provider

self.prompt_strategy = prompt_strategy
Expand Down
16 changes: 10 additions & 6 deletions autogpts/autogpt/autogpt/app/agent_protocol_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,16 @@ async def execute_step(self, task_id: str, step_request: StepRequestBody) -> Ste
"name": execute_command,
"args": execute_command_args,
"result": (
orjson.loads(execute_result.json())
if not isinstance(execute_result, ActionErrorResult)
else {
"error": str(execute_result.error),
"reason": execute_result.reason,
}
""
if execute_result is None
else (
orjson.loads(execute_result.json())
if not isinstance(execute_result, ActionErrorResult)
else {
"error": str(execute_result.error),
"reason": execute_result.reason,
}
)
),
},
}
Expand Down
8 changes: 3 additions & 5 deletions autogpts/autogpt/autogpt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,14 @@ async def run_auto_gpt(
# Resume an Existing Agent #
############################
if load_existing_agent:
agent_state = agent_manager.load_agent_state(load_existing_agent)
agent_state = None
while True:
answer = await clean_input(config, "Resume? [Y/n]")
if answer.lower() == "y":
if answer == "" or answer.lower() == "y":
agent_state = agent_manager.load_agent_state(load_existing_agent)
break
elif answer.lower() == "n":
agent_state = None
break
else:
print("Please respond with 'y' or 'n'")

if agent_state:
agent = configure_agent_with_state(
Expand Down

0 comments on commit fd2c261

Please sign in to comment.