agent.py 3.3 KB

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