runtime.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 SandboxConfig
  9. from opendevin.core.logger import opendevin_logger as logger
  10. from opendevin.events import 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. RejectObservation,
  28. )
  29. from opendevin.events.serialization.action import ACTION_TYPE_TO_CLASS
  30. from opendevin.runtime.plugins import PluginRequirement
  31. from opendevin.runtime.tools import RuntimeTool
  32. from opendevin.storage import FileStore
  33. def _default_env_vars(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 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. sandbox_config: SandboxConfig,
  53. event_stream: EventStream,
  54. sid: str = 'default',
  55. ):
  56. self.sid = sid
  57. self.event_stream = event_stream
  58. self.event_stream.subscribe(EventStreamSubscriber.RUNTIME, self.on_event)
  59. self.sandbox_config = copy.deepcopy(sandbox_config)
  60. self.DEFAULT_ENV_VARS = _default_env_vars(self.sandbox_config)
  61. atexit.register(self.close_sync)
  62. async def ainit(self, env_vars: dict[str, str] | None = None) -> None:
  63. """
  64. Initialize the runtime (asynchronously).
  65. This method should be called after the runtime's constructor.
  66. """
  67. if self.DEFAULT_ENV_VARS:
  68. logger.debug(f'Adding default env vars: {self.DEFAULT_ENV_VARS}')
  69. await self.add_env_vars(self.DEFAULT_ENV_VARS)
  70. if env_vars is not None:
  71. logger.debug(f'Adding provided env vars: {env_vars}')
  72. await self.add_env_vars(env_vars)
  73. async def close(self) -> None:
  74. pass
  75. def close_sync(self) -> None:
  76. try:
  77. loop = asyncio.get_running_loop()
  78. except RuntimeError:
  79. # No running event loop, use asyncio.run()
  80. asyncio.run(self.close())
  81. else:
  82. # There is a running event loop, create a task
  83. if loop.is_running():
  84. loop.create_task(self.close())
  85. else:
  86. loop.run_until_complete(self.close())
  87. # ====================================================================
  88. # Methods we plan to deprecate when we move to new EventStreamRuntime
  89. # ====================================================================
  90. def init_sandbox_plugins(self, plugins: list[PluginRequirement]) -> None:
  91. # TODO: deprecate this method when we move to the new EventStreamRuntime
  92. raise NotImplementedError('This method is not implemented in the base class.')
  93. def init_runtime_tools(
  94. self,
  95. runtime_tools: list[RuntimeTool],
  96. runtime_tools_config: Optional[dict[RuntimeTool, Any]] = None,
  97. is_async: bool = True,
  98. ) -> None:
  99. # TODO: deprecate this method when we move to the new EventStreamRuntime
  100. raise NotImplementedError('This method is not implemented in the base class.')
  101. # ====================================================================
  102. async def add_env_vars(self, env_vars: dict[str, str]) -> None:
  103. cmd = ''
  104. for key, value in env_vars.items():
  105. # Note: json.dumps gives us nice escaping for free
  106. cmd += f'export {key}={json.dumps(value)}; '
  107. if not cmd:
  108. return
  109. cmd = cmd.strip()
  110. logger.debug(f'Adding env var: {cmd}')
  111. obs: Observation = await self.run(CmdRunAction(cmd))
  112. if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
  113. raise RuntimeError(
  114. f'Failed to add env vars [{env_vars}] to environment: {obs.content}'
  115. )
  116. async def on_event(self, event: Event) -> None:
  117. if isinstance(event, Action):
  118. observation = await self.run_action(event)
  119. observation._cause = event.id # type: ignore[attr-defined]
  120. self.event_stream.add_event(observation, event.source) # type: ignore[arg-type]
  121. async def run_action(self, action: Action) -> Observation:
  122. """Run an action and return the resulting observation.
  123. If the action is not runnable in any runtime, a NullObservation is returned.
  124. If the action is not supported by the current runtime, an ErrorObservation is returned.
  125. """
  126. if not action.runnable:
  127. return NullObservation('')
  128. if (
  129. hasattr(action, 'is_confirmed')
  130. and action.is_confirmed == ActionConfirmationStatus.AWAITING_CONFIRMATION
  131. ):
  132. return NullObservation('')
  133. action_type = action.action # type: ignore[attr-defined]
  134. if action_type not in ACTION_TYPE_TO_CLASS:
  135. return ErrorObservation(f'Action {action_type} does not exist.')
  136. if not hasattr(self, action_type):
  137. return ErrorObservation(
  138. f'Action {action_type} is not supported in the current runtime.'
  139. )
  140. if (
  141. hasattr(action, 'is_confirmed')
  142. and action.is_confirmed == ActionConfirmationStatus.REJECTED
  143. ):
  144. return RejectObservation(
  145. 'Action has been rejected by the user! Waiting for further user input.'
  146. )
  147. observation = await getattr(self, action_type)(action)
  148. observation._parent = action.id # type: ignore[attr-defined]
  149. return observation
  150. # ====================================================================
  151. # Implement these methods in the subclass
  152. # ====================================================================
  153. @abstractmethod
  154. async def run(self, action: CmdRunAction) -> Observation:
  155. pass
  156. @abstractmethod
  157. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  158. pass
  159. @abstractmethod
  160. async def read(self, action: FileReadAction) -> Observation:
  161. pass
  162. @abstractmethod
  163. async def write(self, action: FileWriteAction) -> Observation:
  164. pass
  165. @abstractmethod
  166. async def browse(self, action: BrowseURLAction) -> Observation:
  167. pass
  168. @abstractmethod
  169. async def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  170. pass