agent.py 3.3 KB

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