runtime.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import asyncio
  2. from abc import abstractmethod
  3. from opendevin.core.config import config
  4. from opendevin.events import EventSource, EventStream, EventStreamSubscriber
  5. from opendevin.events.action import (
  6. Action,
  7. AgentRecallAction,
  8. BrowseInteractiveAction,
  9. BrowseURLAction,
  10. CmdKillAction,
  11. CmdRunAction,
  12. FileReadAction,
  13. FileWriteAction,
  14. IPythonRunCellAction,
  15. )
  16. from opendevin.events.event import Event
  17. from opendevin.events.observation import (
  18. CmdOutputObservation,
  19. ErrorObservation,
  20. NullObservation,
  21. Observation,
  22. )
  23. from opendevin.events.serialization.action import ACTION_TYPE_TO_CLASS
  24. from opendevin.runtime import (
  25. DockerExecBox,
  26. DockerSSHBox,
  27. E2BBox,
  28. LocalBox,
  29. Sandbox,
  30. )
  31. from opendevin.runtime.browser.browser_env import BrowserEnv
  32. from opendevin.runtime.plugins import PluginRequirement
  33. from opendevin.storage import FileStore, InMemoryFileStore
  34. def create_sandbox(sid: str = 'default', sandbox_type: str = 'exec') -> Sandbox:
  35. if sandbox_type == 'exec':
  36. return DockerExecBox(sid=sid)
  37. elif sandbox_type == 'local':
  38. return LocalBox()
  39. elif sandbox_type == 'ssh':
  40. return DockerSSHBox(sid=sid)
  41. elif sandbox_type == 'e2b':
  42. return E2BBox()
  43. else:
  44. raise ValueError(f'Invalid sandbox type: {sandbox_type}')
  45. class Runtime:
  46. """
  47. The runtime is how the agent interacts with the external environment.
  48. This includes a bash sandbox, a browser, and filesystem interactions.
  49. sid is the session id, which is used to identify the current user session.
  50. """
  51. sid: str
  52. file_store: FileStore
  53. def __init__(
  54. self,
  55. event_stream: EventStream,
  56. sid: str = 'default',
  57. sandbox: Sandbox | None = None,
  58. ):
  59. self.sid = sid
  60. if sandbox is None:
  61. self.sandbox = create_sandbox(sid, config.sandbox_type)
  62. self._is_external_sandbox = False
  63. else:
  64. self.sandbox = sandbox
  65. self._is_external_sandbox = True
  66. self.browser = BrowserEnv()
  67. self.file_store = InMemoryFileStore()
  68. self.event_stream = event_stream
  69. self.event_stream.subscribe(EventStreamSubscriber.RUNTIME, self.on_event)
  70. self._bg_task = asyncio.create_task(self._start_background_observation_loop())
  71. def close(self):
  72. if not self._is_external_sandbox:
  73. self.sandbox.close()
  74. self.browser.close()
  75. self._bg_task.cancel()
  76. def init_sandbox_plugins(self, plugins: list[PluginRequirement]) -> None:
  77. self.sandbox.init_plugins(plugins)
  78. async def on_event(self, event: Event) -> None:
  79. if isinstance(event, Action):
  80. observation = await self.run_action(event)
  81. observation._cause = event.id # type: ignore[attr-defined]
  82. source = event.source if event.source else EventSource.AGENT
  83. await self.event_stream.add_event(observation, source)
  84. async def run_action(self, action: Action) -> Observation:
  85. """
  86. Run an action and return the resulting observation.
  87. If the action is not runnable in any runtime, a NullObservation is returned.
  88. If the action is not supported by the current runtime, an ErrorObservation is returned.
  89. """
  90. if not action.runnable:
  91. return NullObservation('')
  92. action_type = action.action # type: ignore[attr-defined]
  93. if action_type not in ACTION_TYPE_TO_CLASS:
  94. return ErrorObservation(f'Action {action_type} does not exist.')
  95. if not hasattr(self, action_type):
  96. return ErrorObservation(
  97. f'Action {action_type} is not supported in the current runtime.'
  98. )
  99. observation = await getattr(self, action_type)(action)
  100. observation._parent = action.id # type: ignore[attr-defined]
  101. return observation
  102. async def _start_background_observation_loop(self):
  103. while True:
  104. await self.submit_background_obs()
  105. await asyncio.sleep(1)
  106. async def submit_background_obs(self):
  107. """
  108. Returns all observations that have accumulated in the runtime's background.
  109. Right now, this is just background commands, but could include e.g. asynchronous
  110. events happening in the browser.
  111. """
  112. for _id, cmd in self.sandbox.background_commands.items():
  113. output = cmd.read_logs()
  114. if output:
  115. await self.event_stream.add_event(
  116. CmdOutputObservation(
  117. content=output, command_id=_id, command=cmd.command
  118. ),
  119. EventSource.AGENT, # FIXME: use the original action's source
  120. )
  121. await asyncio.sleep(1)
  122. @abstractmethod
  123. async def run(self, action: CmdRunAction) -> Observation:
  124. pass
  125. @abstractmethod
  126. async def kill(self, action: CmdKillAction) -> Observation:
  127. pass
  128. @abstractmethod
  129. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  130. pass
  131. @abstractmethod
  132. async def read(self, action: FileReadAction) -> Observation:
  133. pass
  134. @abstractmethod
  135. async def write(self, action: FileWriteAction) -> Observation:
  136. pass
  137. @abstractmethod
  138. async def browse(self, action: BrowseURLAction) -> Observation:
  139. pass
  140. @abstractmethod
  141. async def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  142. pass
  143. @abstractmethod
  144. async def recall(self, action: AgentRecallAction) -> Observation:
  145. pass