runtime.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import asyncio
  2. import atexit
  3. import copy
  4. import json
  5. import os
  6. from abc import abstractmethod
  7. from typing import Any, Optional
  8. from opendevin.core.config import AppConfig, SandboxConfig
  9. from opendevin.core.logger import opendevin_logger as logger
  10. from opendevin.events import EventSource, EventStream, EventStreamSubscriber
  11. from opendevin.events.action import (
  12. Action,
  13. ActionConfirmationStatus,
  14. BrowseInteractiveAction,
  15. BrowseURLAction,
  16. CmdRunAction,
  17. FileReadAction,
  18. FileWriteAction,
  19. IPythonRunCellAction,
  20. )
  21. from opendevin.events.event import Event
  22. from opendevin.events.observation import (
  23. CmdOutputObservation,
  24. ErrorObservation,
  25. NullObservation,
  26. Observation,
  27. UserRejectObservation,
  28. )
  29. from opendevin.events.serialization.action import ACTION_TYPE_TO_CLASS
  30. from opendevin.runtime.plugins import JupyterRequirement, PluginRequirement
  31. from opendevin.runtime.tools import RuntimeTool
  32. from opendevin.storage import FileStore
  33. def _default_env_vars(sandbox_config: SandboxConfig) -> dict[str, str]:
  34. ret = {}
  35. for key in os.environ:
  36. if key.startswith('SANDBOX_ENV_'):
  37. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  38. ret[sandbox_key] = os.environ[key]
  39. if sandbox_config.enable_auto_lint:
  40. ret['ENABLE_AUTO_LINT'] = 'true'
  41. return ret
  42. class Runtime:
  43. """The runtime is how the agent interacts with the external environment.
  44. This includes a bash sandbox, a browser, and filesystem interactions.
  45. sid is the session id, which is used to identify the current user session.
  46. """
  47. sid: str
  48. file_store: FileStore
  49. DEFAULT_ENV_VARS: dict[str, str]
  50. def __init__(
  51. self,
  52. config: AppConfig,
  53. event_stream: EventStream,
  54. sid: str = 'default',
  55. plugins: list[PluginRequirement] | None = None,
  56. ):
  57. self.sid = sid
  58. self.event_stream = event_stream
  59. self.event_stream.subscribe(EventStreamSubscriber.RUNTIME, self.on_event)
  60. self.plugins = plugins if plugins is not None and len(plugins) > 0 else []
  61. self.config = copy.deepcopy(config)
  62. self.DEFAULT_ENV_VARS = _default_env_vars(config.sandbox)
  63. atexit.register(self.close_sync)
  64. logger.debug(f'Runtime `{sid}` config:\n{self.config}')
  65. async def ainit(self, env_vars: dict[str, str] | None = None) -> None:
  66. """
  67. Initialize the runtime (asynchronously).
  68. This method should be called after the runtime's constructor.
  69. """
  70. if self.DEFAULT_ENV_VARS:
  71. logger.debug(f'Adding default env vars: {self.DEFAULT_ENV_VARS}')
  72. await self.add_env_vars(self.DEFAULT_ENV_VARS)
  73. if env_vars is not None:
  74. logger.debug(f'Adding provided env vars: {env_vars}')
  75. await self.add_env_vars(env_vars)
  76. async def close(self) -> None:
  77. pass
  78. def close_sync(self) -> None:
  79. try:
  80. loop = asyncio.get_running_loop()
  81. except RuntimeError:
  82. # No running event loop, use asyncio.run()
  83. asyncio.run(self.close())
  84. else:
  85. # There is a running event loop, create a task
  86. if loop.is_running():
  87. loop.create_task(self.close())
  88. else:
  89. loop.run_until_complete(self.close())
  90. # ====================================================================
  91. # Methods we plan to deprecate when we move to new EventStreamRuntime
  92. # ====================================================================
  93. def init_runtime_tools(
  94. self,
  95. runtime_tools: list[RuntimeTool],
  96. runtime_tools_config: Optional[dict[RuntimeTool, Any]] = None,
  97. ) -> None:
  98. # TODO: deprecate this method when we move to the new EventStreamRuntime
  99. raise NotImplementedError('This method is not implemented in the base class.')
  100. # ====================================================================
  101. async def add_env_vars(self, env_vars: dict[str, str]) -> None:
  102. # Add env vars to the IPython shell (if Jupyter is used)
  103. if any(isinstance(plugin, JupyterRequirement) for plugin in self.plugins):
  104. code = 'import os\n'
  105. for key, value in env_vars.items():
  106. # Note: json.dumps gives us nice escaping for free
  107. code += f'os.environ["{key}"] = {json.dumps(value)}\n'
  108. code += '\n'
  109. obs = await self.run_ipython(IPythonRunCellAction(code))
  110. logger.info(f'Added env vars to IPython: code={code}, obs={obs}')
  111. # Add env vars to the Bash shell
  112. cmd = ''
  113. for key, value in env_vars.items():
  114. # Note: json.dumps gives us nice escaping for free
  115. cmd += f'export {key}={json.dumps(value)}; '
  116. if not cmd:
  117. return
  118. cmd = cmd.strip()
  119. logger.debug(f'Adding env var: {cmd}')
  120. obs = await self.run(CmdRunAction(cmd))
  121. if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
  122. raise RuntimeError(
  123. f'Failed to add env vars [{env_vars}] to environment: {obs.content}'
  124. )
  125. async def on_event(self, event: Event) -> None:
  126. if isinstance(event, Action):
  127. # set timeout to default if not set
  128. if event.timeout is None:
  129. event.timeout = self.config.sandbox.timeout
  130. assert event.timeout is not None
  131. observation = await self.run_action(event)
  132. observation._cause = event.id # type: ignore[attr-defined]
  133. source = event.source if event.source else EventSource.AGENT
  134. self.event_stream.add_event(observation, source) # type: ignore[arg-type]
  135. async def run_action(self, action: Action) -> Observation:
  136. """Run an action and return the resulting observation.
  137. If the action is not runnable in any runtime, a NullObservation is returned.
  138. If the action is not supported by the current runtime, an ErrorObservation is returned.
  139. """
  140. if not action.runnable:
  141. return NullObservation('')
  142. if (
  143. hasattr(action, 'is_confirmed')
  144. and action.is_confirmed == ActionConfirmationStatus.AWAITING_CONFIRMATION
  145. ):
  146. return NullObservation('')
  147. action_type = action.action # type: ignore[attr-defined]
  148. if action_type not in ACTION_TYPE_TO_CLASS:
  149. return ErrorObservation(f'Action {action_type} does not exist.')
  150. if not hasattr(self, action_type):
  151. return ErrorObservation(
  152. f'Action {action_type} is not supported in the current runtime.'
  153. )
  154. if (
  155. hasattr(action, 'is_confirmed')
  156. and action.is_confirmed == ActionConfirmationStatus.REJECTED
  157. ):
  158. return UserRejectObservation(
  159. 'Action has been rejected by the user! Waiting for further user input.'
  160. )
  161. observation = await getattr(self, action_type)(action)
  162. return observation
  163. @abstractmethod
  164. async def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  165. raise NotImplementedError('This method is not implemented in the base class.')
  166. # ====================================================================
  167. # Implement these methods in the subclass
  168. # ====================================================================
  169. @abstractmethod
  170. async def run(self, action: CmdRunAction) -> Observation:
  171. pass
  172. @abstractmethod
  173. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  174. pass
  175. @abstractmethod
  176. async def read(self, action: FileReadAction) -> Observation:
  177. pass
  178. @abstractmethod
  179. async def write(self, action: FileWriteAction) -> Observation:
  180. pass
  181. @abstractmethod
  182. async def browse(self, action: BrowseURLAction) -> Observation:
  183. pass
  184. @abstractmethod
  185. async def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  186. pass