Clean up exception handling.
This commit is contained in:
parent
6591fdae65
commit
21611953d7
|
|
@ -12,6 +12,7 @@ from ra_aid import print_stage_header, print_error, print_interrupt
|
||||||
from ra_aid.tools.agent import CANCELLED_BY_USER_REASON
|
from ra_aid.tools.agent import CANCELLED_BY_USER_REASON
|
||||||
from ra_aid.tools.human import ask_human
|
from ra_aid.tools.human import ask_human
|
||||||
from ra_aid.agent_utils import (
|
from ra_aid.agent_utils import (
|
||||||
|
AgentInterrupt,
|
||||||
run_agent_with_retry,
|
run_agent_with_retry,
|
||||||
run_research_agent,
|
run_research_agent,
|
||||||
run_planning_agent
|
run_planning_agent
|
||||||
|
|
@ -227,7 +228,7 @@ def main():
|
||||||
config=config
|
config=config
|
||||||
)
|
)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except (KeyboardInterrupt, AgentInterrupt):
|
||||||
print()
|
print()
|
||||||
print(" 👋 Bye!")
|
print(" 👋 Bye!")
|
||||||
print()
|
print()
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ import threading
|
||||||
import time
|
import time
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
class AgentInterrupt(Exception):
|
||||||
|
"""Exception raised when an agent's execution is interrupted.
|
||||||
|
|
||||||
|
This exception is used for internal agent interruption handling,
|
||||||
|
separate from KeyboardInterrupt which is reserved for top-level handling.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
from langgraph.prebuilt import create_react_agent
|
from langgraph.prebuilt import create_react_agent
|
||||||
from ra_aid.console.formatting import print_stage_header, print_error
|
from ra_aid.console.formatting import print_stage_header, print_error
|
||||||
from ra_aid.console.output import print_agent_output
|
from ra_aid.console.output import print_agent_output
|
||||||
|
|
@ -52,6 +60,7 @@ from ra_aid.prompts import (
|
||||||
HUMAN_PROMPT_SECTION_RESEARCH
|
HUMAN_PROMPT_SECTION_RESEARCH
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
def run_research_agent(
|
def run_research_agent(
|
||||||
|
|
@ -287,7 +296,9 @@ def _request_interrupt(signum, frame):
|
||||||
_INTERRUPT_CONTEXT = _CONTEXT_STACK[-1]
|
_INTERRUPT_CONTEXT = _CONTEXT_STACK[-1]
|
||||||
|
|
||||||
if _FEEDBACK_MODE:
|
if _FEEDBACK_MODE:
|
||||||
|
print()
|
||||||
print(" 👋 Bye!")
|
print(" 👋 Bye!")
|
||||||
|
print()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
class InterruptibleSection:
|
class InterruptibleSection:
|
||||||
|
|
@ -300,7 +311,7 @@ class InterruptibleSection:
|
||||||
|
|
||||||
def check_interrupt():
|
def check_interrupt():
|
||||||
if _CONTEXT_STACK and _INTERRUPT_CONTEXT is _CONTEXT_STACK[-1]:
|
if _CONTEXT_STACK and _INTERRUPT_CONTEXT is _CONTEXT_STACK[-1]:
|
||||||
raise KeyboardInterrupt("Interrupt requested")
|
raise AgentInterrupt("Interrupt requested")
|
||||||
|
|
||||||
def run_agent_with_retry(agent, prompt: str, config: dict) -> Optional[str]:
|
def run_agent_with_retry(agent, prompt: str, config: dict) -> Optional[str]:
|
||||||
original_handler = None
|
original_handler = None
|
||||||
|
|
@ -324,7 +335,7 @@ def run_agent_with_retry(agent, prompt: str, config: dict) -> Optional[str]:
|
||||||
check_interrupt()
|
check_interrupt()
|
||||||
print_agent_output(chunk)
|
print_agent_output(chunk)
|
||||||
return "Agent run completed successfully"
|
return "Agent run completed successfully"
|
||||||
except KeyboardInterrupt:
|
except (KeyboardInterrupt, AgentInterrupt):
|
||||||
raise
|
raise
|
||||||
except (InternalServerError, APITimeoutError, RateLimitError, APIError) as e:
|
except (InternalServerError, APITimeoutError, RateLimitError, APIError) as e:
|
||||||
if attempt == max_retries - 1:
|
if attempt == max_retries - 1:
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
from langchain_core.tools import tool
|
from langchain_core.tools import tool
|
||||||
from typing import Dict, Any, Union, List
|
from typing import Dict, Any, Union, List
|
||||||
from typing_extensions import TypeAlias
|
from typing_extensions import TypeAlias
|
||||||
|
from ..agent_utils import AgentInterrupt
|
||||||
|
|
||||||
ResearchResult = Dict[str, Union[str, bool, Dict[int, Any], List[Any], None]]
|
ResearchResult = Dict[str, Union[str, bool, Dict[int, Any], List[Any], None]]
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
@ -61,15 +62,13 @@ def request_research(query: str) -> ResearchResult:
|
||||||
hil=config.get('hil', False),
|
hil=config.get('hil', False),
|
||||||
console_message=query
|
console_message=query
|
||||||
)
|
)
|
||||||
except KeyboardInterrupt:
|
except AgentInterrupt:
|
||||||
try:
|
|
||||||
print()
|
print()
|
||||||
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
||||||
success = False
|
success = False
|
||||||
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
||||||
except Exception:
|
except KeyboardInterrupt:
|
||||||
success = False
|
raise
|
||||||
reason = CANCELLED_BY_USER_REASON
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print_error(f"Error during research: {str(e)}")
|
print_error(f"Error during research: {str(e)}")
|
||||||
success = False
|
success = False
|
||||||
|
|
@ -123,15 +122,13 @@ def request_research_and_implementation(query: str) -> Dict[str, Any]:
|
||||||
|
|
||||||
success = True
|
success = True
|
||||||
reason = None
|
reason = None
|
||||||
except KeyboardInterrupt:
|
except AgentInterrupt:
|
||||||
try:
|
|
||||||
print()
|
print()
|
||||||
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
||||||
success = False
|
success = False
|
||||||
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
||||||
except Exception:
|
except KeyboardInterrupt:
|
||||||
success = False
|
raise
|
||||||
reason = CANCELLED_BY_USER_REASON
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
console.print(f"\n[red]Error during research: {str(e)}[/red]")
|
console.print(f"\n[red]Error during research: {str(e)}[/red]")
|
||||||
success = False
|
success = False
|
||||||
|
|
@ -194,15 +191,13 @@ def request_task_implementation(task_spec: str) -> Dict[str, Any]:
|
||||||
|
|
||||||
success = True
|
success = True
|
||||||
reason = None
|
reason = None
|
||||||
except KeyboardInterrupt:
|
except AgentInterrupt:
|
||||||
try:
|
|
||||||
print()
|
print()
|
||||||
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
||||||
success = False
|
success = False
|
||||||
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
||||||
except Exception:
|
except KeyboardInterrupt:
|
||||||
success = False
|
raise
|
||||||
reason = CANCELLED_BY_USER_REASON
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print_error(f"Error during task implementation: {str(e)}")
|
print_error(f"Error during task implementation: {str(e)}")
|
||||||
success = False
|
success = False
|
||||||
|
|
@ -255,15 +250,13 @@ def request_implementation(task_spec: str) -> Dict[str, Any]:
|
||||||
|
|
||||||
success = True
|
success = True
|
||||||
reason = None
|
reason = None
|
||||||
except KeyboardInterrupt:
|
except AgentInterrupt:
|
||||||
try:
|
|
||||||
print()
|
print()
|
||||||
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
response = ask_human.invoke({"question": "Why did you interrupt me?"})
|
||||||
success = False
|
success = False
|
||||||
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
reason = response if response.strip() else CANCELLED_BY_USER_REASON
|
||||||
except Exception:
|
except KeyboardInterrupt:
|
||||||
success = False
|
raise
|
||||||
reason = CANCELLED_BY_USER_REASON
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print_error(f"Error during planning: {str(e)}")
|
print_error(f"Error during planning: {str(e)}")
|
||||||
success = False
|
success = False
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue