agent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. """Indicates whether the current instruction execution is complete.
  33. Returns:
  34. - complete (bool): True if execution is complete; False otherwise.
  35. """
  36. return self._complete
  37. @abstractmethod
  38. def step(self, state: 'State') -> 'Action':
  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. def reset(self) -> None:
  44. """Resets the agent's execution status and clears the history. This method can be used
  45. to prepare the agent for restarting the instruction or cleaning up before destruction.
  46. """
  47. # TODO clear history
  48. self._complete = False
  49. if self.llm:
  50. self.llm.reset()
  51. @property
  52. def name(self):
  53. return self.__class__.__name__
  54. @classmethod
  55. def register(cls, name: str, agent_cls: Type['Agent']):
  56. """Registers an agent class in the registry.
  57. Parameters:
  58. - name (str): The name to register the class under.
  59. - agent_cls (Type['Agent']): The class to register.
  60. Raises:
  61. - AgentAlreadyRegisteredError: If name already registered
  62. """
  63. if name in cls._registry:
  64. raise AgentAlreadyRegisteredError(name)
  65. cls._registry[name] = agent_cls
  66. @classmethod
  67. def get_cls(cls, name: str) -> Type['Agent']:
  68. """Retrieves an agent class from the registry.
  69. Parameters:
  70. - name (str): The name of the class to retrieve
  71. Returns:
  72. - agent_cls (Type['Agent']): The class registered under the specified name.
  73. Raises:
  74. - AgentNotRegisteredError: If name not registered
  75. """
  76. if name not in cls._registry:
  77. raise AgentNotRegisteredError(name)
  78. return cls._registry[name]
  79. @classmethod
  80. def list_agents(cls) -> list[str]:
  81. """Retrieves the list of all agent names from the registry.
  82. Raises:
  83. - AgentNotRegisteredError: If no agent is registered
  84. """
  85. if not bool(cls._registry):
  86. raise AgentNotRegisteredError()
  87. return list(cls._registry.keys())