exceptions.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # ============================================
  2. # Agent Exceptions
  3. # ============================================
  4. class AgentError(Exception):
  5. """Base class for all agent exceptions."""
  6. pass
  7. class AgentNoInstructionError(AgentError):
  8. def __init__(self, message='Instruction must be provided'):
  9. super().__init__(message)
  10. class AgentEventTypeError(AgentError):
  11. def __init__(self, message='Event must be a dictionary'):
  12. super().__init__(message)
  13. class AgentAlreadyRegisteredError(AgentError):
  14. def __init__(self, name=None):
  15. if name is not None:
  16. message = f"Agent class already registered under '{name}'"
  17. else:
  18. message = 'Agent class already registered'
  19. super().__init__(message)
  20. class AgentNotRegisteredError(AgentError):
  21. def __init__(self, name=None):
  22. if name is not None:
  23. message = f"No agent class registered under '{name}'"
  24. else:
  25. message = 'No agent class registered'
  26. super().__init__(message)
  27. class AgentStuckInLoopError(AgentError):
  28. def __init__(self, message='Agent got stuck in a loop'):
  29. super().__init__(message)
  30. # ============================================
  31. # Agent Controller Exceptions
  32. # ============================================
  33. class TaskInvalidStateError(Exception):
  34. def __init__(self, state=None):
  35. if state is not None:
  36. message = f'Invalid state {state}'
  37. else:
  38. message = 'Invalid state'
  39. super().__init__(message)
  40. # ============================================
  41. # LLM Exceptions
  42. # ============================================
  43. # This exception gets sent back to the LLM
  44. # It might be malformed JSON
  45. class LLMMalformedActionError(Exception):
  46. def __init__(self, message='Malformed response'):
  47. self.message = message
  48. super().__init__(message)
  49. def __str__(self):
  50. return self.message
  51. # This exception gets sent back to the LLM
  52. # For some reason, the agent did not return an action
  53. class LLMNoActionError(Exception):
  54. def __init__(self, message='Agent must return an action'):
  55. super().__init__(message)
  56. # This exception gets sent back to the LLM
  57. # The LLM output did not include an action, or the action was not the expected type
  58. class LLMResponseError(Exception):
  59. def __init__(self, message='Failed to retrieve action from LLM response'):
  60. super().__init__(message)
  61. class UserCancelledError(Exception):
  62. def __init__(self, message='User cancelled the request'):
  63. super().__init__(message)
  64. class MicroAgentValidationError(Exception):
  65. def __init__(self, message='Micro agent validation failed'):
  66. super().__init__(message)
  67. class OperationCancelled(Exception):
  68. """Exception raised when an operation is cancelled (e.g. by a keyboard interrupt)."""
  69. def __init__(self, message='Operation was cancelled'):
  70. super().__init__(message)
  71. class CloudFlareBlockageError(Exception):
  72. """Exception raised when a request is blocked by CloudFlare."""
  73. pass
  74. # ============================================
  75. # LLM function calling Exceptions
  76. # ============================================
  77. class FunctionCallConversionError(Exception):
  78. """Exception raised when FunctionCallingConverter failed to convert a non-function call message to a function call message.
  79. This typically happens when there's a malformed message (e.g., missing <function=...> tags). But not due to LLM output.
  80. """
  81. def __init__(self, message):
  82. super().__init__(message)
  83. class FunctionCallValidationError(Exception):
  84. """Exception raised when FunctionCallingConverter failed to validate a function call message.
  85. This typically happens when the LLM outputs unrecognized function call / parameter names / values.
  86. """
  87. def __init__(self, message):
  88. super().__init__(message)
  89. class FunctionCallNotExistsError(Exception):
  90. """Exception raised when an LLM call a tool that is not registered."""
  91. def __init__(self, message):
  92. super().__init__(message)
  93. # ============================================
  94. # Agent Runtime Exceptions
  95. # ============================================
  96. class AgentRuntimeError(Exception):
  97. """Base class for all agent runtime exceptions."""
  98. pass
  99. class AgentRuntimeBuildError(AgentRuntimeError):
  100. """Exception raised when an agent runtime build operation fails."""
  101. pass
  102. class AgentRuntimeTimeoutError(AgentRuntimeError):
  103. """Exception raised when an agent runtime operation times out."""
  104. pass
  105. class AgentRuntimeUnavailableError(AgentRuntimeError):
  106. """Exception raised when an agent runtime is unavailable."""
  107. pass
  108. class AgentRuntimeNotReadyError(AgentRuntimeUnavailableError):
  109. """Exception raised when an agent runtime is not ready."""
  110. pass
  111. class AgentRuntimeDisconnectedError(AgentRuntimeUnavailableError):
  112. """Exception raised when an agent runtime is disconnected."""
  113. pass
  114. class AgentRuntimeNotFoundError(AgentRuntimeUnavailableError):
  115. """Exception raised when an agent runtime is not found."""
  116. pass
  117. # ============================================
  118. # Browser Exceptions
  119. # ============================================
  120. class BrowserInitException(Exception):
  121. def __init__(self, message='Failed to initialize browser environment'):
  122. super().__init__(message)
  123. class BrowserUnavailableException(Exception):
  124. def __init__(
  125. self,
  126. message='Browser environment is not available, please check if has been initialized',
  127. ):
  128. super().__init__(message)