agent.py 3.4 KB

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