agent.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from typing import Optional
  2. from opendevin.controller import AgentController
  3. from opendevin.controller.agent import Agent
  4. from opendevin.controller.state.state import State
  5. from opendevin.core.config import AppConfig, LLMConfig
  6. from opendevin.core.logger import opendevin_logger as logger
  7. from opendevin.events.stream import EventStream
  8. from opendevin.runtime import get_runtime_cls
  9. from opendevin.runtime.runtime import Runtime
  10. from opendevin.storage.files import FileStore
  11. class AgentSession:
  12. """Represents a session with an agent.
  13. Attributes:
  14. controller: The AgentController instance for controlling the agent.
  15. """
  16. sid: str
  17. event_stream: EventStream
  18. file_store: FileStore
  19. controller: Optional[AgentController] = None
  20. runtime: Optional[Runtime] = None
  21. _closed: bool = False
  22. def __init__(self, sid: str, file_store: FileStore):
  23. """Initializes a new instance of the Session class."""
  24. self.sid = sid
  25. self.event_stream = EventStream(sid, file_store)
  26. self.file_store = file_store
  27. async def start(
  28. self,
  29. runtime_name: str,
  30. config: AppConfig,
  31. agent: Agent,
  32. confirmation_mode: bool,
  33. max_iterations: int,
  34. max_budget_per_task: float | None = None,
  35. agent_to_llm_config: dict[str, LLMConfig] | None = None,
  36. ):
  37. """Starts the agent session.
  38. Args:
  39. start_event: The start event data (optional).
  40. """
  41. if self.controller or self.runtime:
  42. raise Exception(
  43. 'Session already started. You need to close this session and start a new one.'
  44. )
  45. await self._create_runtime(runtime_name, config, agent)
  46. await self._create_controller(
  47. agent,
  48. confirmation_mode,
  49. max_iterations,
  50. max_budget_per_task=max_budget_per_task,
  51. agent_to_llm_config=agent_to_llm_config,
  52. )
  53. async def close(self):
  54. if self._closed:
  55. return
  56. if self.controller is not None:
  57. end_state = self.controller.get_state()
  58. end_state.save_to_session(self.sid, self.file_store)
  59. await self.controller.close()
  60. if self.runtime is not None:
  61. await self.runtime.close()
  62. self._closed = True
  63. async def _create_runtime(self, runtime_name: str, config: AppConfig, agent: Agent):
  64. """Creates a runtime instance."""
  65. if self.runtime is not None:
  66. raise Exception('Runtime already created')
  67. logger.info(f'Using runtime: {runtime_name}')
  68. runtime_cls = get_runtime_cls(runtime_name)
  69. self.runtime = runtime_cls(
  70. config=config,
  71. event_stream=self.event_stream,
  72. sid=self.sid,
  73. plugins=agent.sandbox_plugins,
  74. )
  75. await self.runtime.ainit()
  76. async def _create_controller(
  77. self,
  78. agent: Agent,
  79. confirmation_mode: bool,
  80. max_iterations: int,
  81. max_budget_per_task: float | None = None,
  82. agent_to_llm_config: dict[str, LLMConfig] | None = None,
  83. ):
  84. """Creates an AgentController instance."""
  85. if self.controller is not None:
  86. raise Exception('Controller already created')
  87. if self.runtime is None:
  88. raise Exception('Runtime must be initialized before the agent controller')
  89. logger.info(f'Creating agent {agent.name} using LLM {agent.llm.config.model}')
  90. self.controller = AgentController(
  91. sid=self.sid,
  92. event_stream=self.event_stream,
  93. agent=agent,
  94. max_iterations=int(max_iterations),
  95. max_budget_per_task=max_budget_per_task,
  96. agent_to_llm_config=agent_to_llm_config,
  97. confirmation_mode=confirmation_mode,
  98. # AgentSession is designed to communicate with the frontend, so we don't want to
  99. # run the agent in headless mode.
  100. headless_mode=False,
  101. )
  102. try:
  103. agent_state = State.restore_from_session(self.sid, self.file_store)
  104. self.controller.set_initial_state(
  105. agent_state, max_iterations, confirmation_mode
  106. )
  107. logger.info(f'Restored agent state from session, sid: {self.sid}')
  108. except Exception as e:
  109. print('Error restoring state', e)