runtime.py 7.0 KB

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