agent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from abc import ABC, abstractmethod
  2. from typing import TYPE_CHECKING, Type
  3. if TYPE_CHECKING:
  4. from openhands.controller.state.state import State
  5. from openhands.core.config import AgentConfig
  6. from openhands.events.action import Action
  7. from openhands.core.exceptions import (
  8. AgentAlreadyRegisteredError,
  9. AgentNotRegisteredError,
  10. )
  11. from openhands.llm.llm import LLM
  12. from openhands.runtime.plugins import PluginRequirement
  13. class Agent(ABC):
  14. DEPRECATED = False
  15. """
  16. This abstract base class is an general interface for an agent dedicated to
  17. executing a specific instruction and allowing human interaction with the
  18. agent during execution.
  19. It tracks the execution status and maintains a history of interactions.
  20. """
  21. _registry: dict[str, Type['Agent']] = {}
  22. sandbox_plugins: list[PluginRequirement] = []
  23. def __init__(
  24. self,
  25. llm: LLM,
  26. config: 'AgentConfig',
  27. ):
  28. self.llm = llm
  29. self.config = config
  30. self._complete = False
  31. @property
  32. def complete(self) -> bool:
  33. """Indicates whether the current instruction execution is complete.
  34. Returns:
  35. - complete (bool): True if execution is complete; False otherwise.
  36. """
  37. return self._complete
  38. @abstractmethod
  39. def step(self, state: 'State') -> 'Action':
  40. """Starts the execution of the assigned instruction. This method should
  41. be implemented by subclasses to define the specific execution logic.
  42. """
  43. pass
  44. def reset(self) -> None:
  45. """Resets the agent's execution status and clears the history. This method can be used
  46. to prepare the agent for restarting the instruction or cleaning up before destruction.
  47. """
  48. # TODO clear history
  49. self._complete = False
  50. if self.llm:
  51. self.llm.reset()
  52. @property
  53. def name(self):
  54. return self.__class__.__name__
  55. @classmethod
  56. def register(cls, name: str, agent_cls: Type['Agent']):
  57. """Registers an agent class in the registry.
  58. Parameters:
  59. - name (str): The name to register the class under.
  60. - agent_cls (Type['Agent']): The class to register.
  61. Raises:
  62. - AgentAlreadyRegisteredError: If name already registered
  63. """
  64. if name in cls._registry:
  65. raise AgentAlreadyRegisteredError(name)
  66. cls._registry[name] = agent_cls
  67. @classmethod
  68. def get_cls(cls, name: str) -> Type['Agent']:
  69. """Retrieves an agent class from the registry.
  70. Parameters:
  71. - name (str): The name of the class to retrieve
  72. Returns:
  73. - agent_cls (Type['Agent']): The class registered under the specified name.
  74. Raises:
  75. - AgentNotRegisteredError: If name not registered
  76. """
  77. if name not in cls._registry:
  78. raise AgentNotRegisteredError(name)
  79. return cls._registry[name]
  80. @classmethod
  81. def list_agents(cls) -> list[str]:
  82. """Retrieves the list of all agent names from the registry.
  83. Raises:
  84. - AgentNotRegisteredError: If no agent is registered
  85. """
  86. if not bool(cls._registry):
  87. raise AgentNotRegisteredError()
  88. return list(cls._registry.keys())