Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

Gracefully catch errors when running agent.step() (#216)

Vivian Fangd6c3f0ea0f97
diff --git a/memgpt/main.py b/memgpt/main.py
index ecbc0ed08..57ea9e8bb 100644
--- a/memgpt/main.py
+++ b/memgpt/main.py
@@ -7,6 +7,7 @@ import glob
import os
import sys
import pickle
+import traceback
import questionary
import typer
@@ -512,24 +513,32 @@ async def run_agent_loop(memgpt_agent, first, no_verify=False, cfg=None, legacy=
skip_next_user_input = False
- with console.status("[bold cyan]Thinking...") as status:
- (
- new_messages,
- heartbeat_request,
- function_failed,
- token_warning,
- ) = await memgpt_agent.step(user_message, first_message=False, skip_verify=no_verify)
-
- # Skip user inputs if there's a memory warning, function execution failed, or the agent asked for control
- if token_warning:
- user_message = system.get_token_limit_warning()
- skip_next_user_input = True
- elif function_failed:
- user_message = system.get_heartbeat(constants.FUNC_FAILED_HEARTBEAT_MESSAGE)
- skip_next_user_input = True
- elif heartbeat_request:
- user_message = system.get_heartbeat(constants.REQ_HEARTBEAT_MESSAGE)
- skip_next_user_input = True
+ while True:
+ try:
+ with console.status("[bold cyan]Thinking...") as status:
+ (
+ new_messages,
+ heartbeat_request,
+ function_failed,
+ token_warning,
+ ) = await memgpt_agent.step(user_message, first_message=False, skip_verify=no_verify)
+
+ # Skip user inputs if there's a memory warning, function execution failed, or the agent asked for control
+ if token_warning:
+ user_message = system.get_token_limit_warning()
+ skip_next_user_input = True
+ elif function_failed:
+ user_message = system.get_heartbeat(constants.FUNC_FAILED_HEARTBEAT_MESSAGE)
+ skip_next_user_input = True
+ elif heartbeat_request:
+ user_message = system.get_heartbeat(constants.REQ_HEARTBEAT_MESSAGE)
+ skip_next_user_input = True
+ except Exception as e:
+ print("An exception ocurred when running agent.step(): ")
+ traceback.print_exc()
+ retry = await questionary.confirm("Retry agent.step()?").ask_async()
+ if not retry:
+ break
counter += 1