runtime.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from abc import abstractmethod
  2. from opendevin.core.config import config
  3. from opendevin.events.action import (
  4. ACTION_TYPE_TO_CLASS,
  5. Action,
  6. AgentRecallAction,
  7. BrowseURLAction,
  8. CmdKillAction,
  9. CmdRunAction,
  10. FileReadAction,
  11. FileWriteAction,
  12. IPythonRunCellAction,
  13. )
  14. from opendevin.events.observation import (
  15. CmdOutputObservation,
  16. ErrorObservation,
  17. NullObservation,
  18. Observation,
  19. )
  20. from opendevin.runtime import (
  21. DockerExecBox,
  22. DockerSSHBox,
  23. E2BBox,
  24. LocalBox,
  25. Sandbox,
  26. )
  27. from opendevin.runtime.browser.browser_env import BrowserEnv
  28. from opendevin.runtime.plugins import PluginRequirement
  29. def create_sandbox(sid: str = 'default', sandbox_type: str = 'exec') -> Sandbox:
  30. if sandbox_type == 'exec':
  31. return DockerExecBox(sid=sid, timeout=config.sandbox_timeout)
  32. elif sandbox_type == 'local':
  33. return LocalBox(timeout=config.sandbox_timeout)
  34. elif sandbox_type == 'ssh':
  35. return DockerSSHBox(sid=sid, timeout=config.sandbox_timeout)
  36. elif sandbox_type == 'e2b':
  37. return E2BBox(timeout=config.sandbox_timeout)
  38. else:
  39. raise ValueError(f'Invalid sandbox type: {sandbox_type}')
  40. class Runtime:
  41. """
  42. The runtime is how the agent interacts with the external environment.
  43. This includes a bash sandbox, a browser, and filesystem interactions.
  44. sid is the session id, which is used to identify the current user session.
  45. """
  46. sid: str
  47. sandbox: Sandbox
  48. def __init__(
  49. self,
  50. sid: str = 'default',
  51. ):
  52. self.sid = sid
  53. self.sandbox = create_sandbox(sid, config.sandbox_type)
  54. self.browser = BrowserEnv()
  55. def init_sandbox_plugins(self, plugins: list[PluginRequirement]) -> None:
  56. self.sandbox.init_plugins(plugins)
  57. async def run_action(self, action: Action) -> Observation:
  58. """
  59. Run an action and return the resulting observation.
  60. If the action is not runnable in any runtime, a NullObservation is returned.
  61. If the action is not supported by the current runtime, an ErrorObservation is returned.
  62. """
  63. if not action.runnable:
  64. return NullObservation('')
  65. action_id = action.action # type: ignore[attr-defined]
  66. if action_id not in ACTION_TYPE_TO_CLASS:
  67. return ErrorObservation(f'Action {action_id} does not exist.')
  68. if not hasattr(self, action_id):
  69. return ErrorObservation(
  70. f'Action {action_id} is not supported in the current runtime.'
  71. )
  72. observation = await getattr(self, action_id)(action)
  73. return observation
  74. def get_background_obs(self) -> list[CmdOutputObservation]:
  75. """
  76. Returns all observations that have accumulated in the runtime's background.
  77. Right now, this is just background commands, but could include e.g. asyncronous
  78. events happening in the browser.
  79. """
  80. obs = []
  81. for _id, cmd in self.sandbox.background_commands.items():
  82. output = cmd.read_logs()
  83. if output is not None and output != '':
  84. obs.append(
  85. CmdOutputObservation(
  86. content=output, command_id=_id, command=cmd.command
  87. )
  88. )
  89. return obs
  90. @abstractmethod
  91. async def run(self, action: CmdRunAction) -> Observation:
  92. pass
  93. @abstractmethod
  94. async def kill(self, action: CmdKillAction) -> Observation:
  95. pass
  96. @abstractmethod
  97. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  98. pass
  99. @abstractmethod
  100. async def read(self, action: FileReadAction) -> Observation:
  101. pass
  102. @abstractmethod
  103. async def write(self, action: FileWriteAction) -> Observation:
  104. pass
  105. @abstractmethod
  106. async def browse(self, action: BrowseURLAction) -> Observation:
  107. pass
  108. @abstractmethod
  109. async def recall(self, action: AgentRecallAction) -> Observation:
  110. pass