agent.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from abc import ABC, abstractmethod
  2. from typing import TYPE_CHECKING, Type
  3. if TYPE_CHECKING:
  4. from opendevin.controller.state.state import State
  5. from opendevin.events.action import Action
  6. from opendevin.core.exceptions import (
  7. AgentAlreadyRegisteredError,
  8. AgentNotRegisteredError,
  9. )
  10. from opendevin.llm.llm import LLM
  11. from opendevin.runtime.plugins import PluginRequirement
  12. from opendevin.runtime.tools import RuntimeTool
  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. runtime_tools: list[RuntimeTool] = []
  24. def __init__(
  25. self,
  26. llm: LLM,
  27. ):
  28. self.llm = llm
  29. self._complete = False
  30. @property
  31. def complete(self) -> bool:
  32. """
  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. """
  41. Starts the execution of the assigned instruction. This method should
  42. be implemented by subclasses to define the specific execution logic.
  43. """
  44. pass
  45. @abstractmethod
  46. def search_memory(self, query: str) -> list[str]:
  47. """
  48. Searches the agent's memory for information relevant to the given query.
  49. Parameters:
  50. - query (str): The query to search for in the agent's memory.
  51. Returns:
  52. - response (str): The response to the query.
  53. """
  54. pass
  55. def reset(self) -> None:
  56. """
  57. Resets the agent's execution status and clears the history. This method can be used
  58. to prepare the agent for restarting the instruction or cleaning up before destruction.
  59. """
  60. # TODO clear history
  61. self._complete = False
  62. @property
  63. def name(self):
  64. return self.__class__.__name__
  65. @classmethod
  66. def register(cls, name: str, agent_cls: Type['Agent']):
  67. """
  68. Registers an agent class in the registry.
  69. Parameters:
  70. - name (str): The name to register the class under.
  71. - agent_cls (Type['Agent']): The class to register.
  72. Raises:
  73. - AgentAlreadyRegisteredError: If name already registered
  74. """
  75. if name in cls._registry:
  76. raise AgentAlreadyRegisteredError(name)
  77. cls._registry[name] = agent_cls
  78. @classmethod
  79. def get_cls(cls, name: str) -> Type['Agent']:
  80. """
  81. Retrieves an agent class from the registry.
  82. Parameters:
  83. - name (str): The name of the class to retrieve
  84. Returns:
  85. - agent_cls (Type['Agent']): The class registered under the specified name.
  86. Raises:
  87. - AgentNotRegisteredError: If name not registered
  88. """
  89. if name not in cls._registry:
  90. raise AgentNotRegisteredError(name)
  91. return cls._registry[name]
  92. @classmethod
  93. def list_agents(cls) -> list[str]:
  94. """
  95. Retrieves the list of all agent names from the registry.
  96. Raises:
  97. - AgentNotRegisteredError: If no agent is registered
  98. """
  99. if not bool(cls._registry):
  100. raise AgentNotRegisteredError()
  101. return list(cls._registry.keys())