base.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import atexit
  2. import copy
  3. import json
  4. import os
  5. from abc import abstractmethod
  6. from typing import Callable
  7. from openhands.core.config import AppConfig, SandboxConfig
  8. from openhands.core.logger import openhands_logger as logger
  9. from openhands.events import EventSource, EventStream, EventStreamSubscriber
  10. from openhands.events.action import (
  11. Action,
  12. ActionConfirmationStatus,
  13. BrowseInteractiveAction,
  14. BrowseURLAction,
  15. CmdRunAction,
  16. FileReadAction,
  17. FileWriteAction,
  18. IPythonRunCellAction,
  19. )
  20. from openhands.events.event import Event
  21. from openhands.events.observation import (
  22. CmdOutputObservation,
  23. ErrorObservation,
  24. NullObservation,
  25. Observation,
  26. UserRejectObservation,
  27. )
  28. from openhands.events.serialization.action import ACTION_TYPE_TO_CLASS
  29. from openhands.runtime.plugins import JupyterRequirement, PluginRequirement
  30. from openhands.runtime.utils.edit import FileEditRuntimeMixin
  31. from openhands.utils.async_utils import call_sync_from_async
  32. def _default_env_vars(sandbox_config: SandboxConfig) -> dict[str, str]:
  33. ret = {}
  34. for key in os.environ:
  35. if key.startswith('SANDBOX_ENV_'):
  36. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  37. ret[sandbox_key] = os.environ[key]
  38. if sandbox_config.enable_auto_lint:
  39. ret['ENABLE_AUTO_LINT'] = 'true'
  40. return ret
  41. class Runtime(FileEditRuntimeMixin):
  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. config: AppConfig
  48. initial_env_vars: dict[str, str]
  49. attach_to_existing: bool
  50. def __init__(
  51. self,
  52. config: AppConfig,
  53. event_stream: EventStream,
  54. sid: str = 'default',
  55. plugins: list[PluginRequirement] | None = None,
  56. env_vars: dict[str, str] | None = None,
  57. status_message_callback: Callable | None = None,
  58. attach_to_existing: bool = False,
  59. ):
  60. self.sid = sid
  61. self.event_stream = event_stream
  62. self.event_stream.subscribe(EventStreamSubscriber.RUNTIME, self.on_event)
  63. self.plugins = plugins if plugins is not None and len(plugins) > 0 else []
  64. self.status_message_callback = status_message_callback
  65. self.attach_to_existing = attach_to_existing
  66. self.config = copy.deepcopy(config)
  67. atexit.register(self.close)
  68. self.initial_env_vars = _default_env_vars(config.sandbox)
  69. if env_vars is not None:
  70. self.initial_env_vars.update(env_vars)
  71. # Load mixins
  72. FileEditRuntimeMixin.__init__(self)
  73. def setup_initial_env(self) -> None:
  74. if self.attach_to_existing:
  75. return
  76. logger.debug(f'Adding env vars: {self.initial_env_vars}')
  77. self.add_env_vars(self.initial_env_vars)
  78. if self.config.sandbox.runtime_startup_env_vars:
  79. self.add_env_vars(self.config.sandbox.runtime_startup_env_vars)
  80. def close(self) -> None:
  81. pass
  82. def log(self, level: str, message: str) -> None:
  83. message = f'[runtime {self.sid}] {message}'
  84. getattr(logger, level)(message)
  85. # ====================================================================
  86. def add_env_vars(self, env_vars: dict[str, str]) -> None:
  87. # Add env vars to the IPython shell (if Jupyter is used)
  88. if any(isinstance(plugin, JupyterRequirement) for plugin in self.plugins):
  89. code = 'import os\n'
  90. for key, value in env_vars.items():
  91. # Note: json.dumps gives us nice escaping for free
  92. code += f'os.environ["{key}"] = {json.dumps(value)}\n'
  93. code += '\n'
  94. obs = self.run_ipython(IPythonRunCellAction(code))
  95. self.log('debug', f'Added env vars to IPython: code={code}, obs={obs}')
  96. # Add env vars to the Bash shell
  97. cmd = ''
  98. for key, value in env_vars.items():
  99. # Note: json.dumps gives us nice escaping for free
  100. cmd += f'export {key}={json.dumps(value)}; '
  101. if not cmd:
  102. return
  103. cmd = cmd.strip()
  104. logger.debug(f'Adding env var: {cmd}')
  105. obs = self.run(CmdRunAction(cmd))
  106. if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
  107. raise RuntimeError(
  108. f'Failed to add env vars [{env_vars}] to environment: {obs.content}'
  109. )
  110. async def on_event(self, event: Event) -> None:
  111. if isinstance(event, Action):
  112. # set timeout to default if not set
  113. if event.timeout is None:
  114. event.timeout = self.config.sandbox.timeout
  115. assert event.timeout is not None
  116. observation: Observation = await call_sync_from_async(
  117. self.run_action, event
  118. )
  119. observation._cause = event.id # type: ignore[attr-defined]
  120. observation.tool_call_metadata = event.tool_call_metadata
  121. # this might be unnecessary, since source should be set by the event stream when we're here
  122. source = event.source if event.source else EventSource.AGENT
  123. await self.event_stream.async_add_event(observation, source) # type: ignore[arg-type]
  124. def run_action(self, action: Action) -> Observation:
  125. """Run an action and return the resulting observation.
  126. If the action is not runnable in any runtime, a NullObservation is returned.
  127. If the action is not supported by the current runtime, an ErrorObservation is returned.
  128. """
  129. if not action.runnable:
  130. return NullObservation('')
  131. if (
  132. hasattr(action, 'confirmation_state')
  133. and action.confirmation_state
  134. == ActionConfirmationStatus.AWAITING_CONFIRMATION
  135. ):
  136. return NullObservation('')
  137. action_type = action.action # type: ignore[attr-defined]
  138. if action_type not in ACTION_TYPE_TO_CLASS:
  139. return ErrorObservation(f'Action {action_type} does not exist.')
  140. if not hasattr(self, action_type):
  141. return ErrorObservation(
  142. f'Action {action_type} is not supported in the current runtime.'
  143. )
  144. if (
  145. getattr(action, 'confirmation_state', None)
  146. == ActionConfirmationStatus.REJECTED
  147. ):
  148. return UserRejectObservation(
  149. 'Action has been rejected by the user! Waiting for further user input.'
  150. )
  151. observation = getattr(self, action_type)(action)
  152. return observation
  153. # ====================================================================
  154. # Context manager
  155. # ====================================================================
  156. def __enter__(self) -> 'Runtime':
  157. return self
  158. def __exit__(self, exc_type, exc_value, traceback) -> None:
  159. self.close()
  160. @abstractmethod
  161. async def connect(self) -> None:
  162. pass
  163. # ====================================================================
  164. # Action execution
  165. # ====================================================================
  166. @abstractmethod
  167. def run(self, action: CmdRunAction) -> Observation:
  168. pass
  169. @abstractmethod
  170. def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  171. pass
  172. @abstractmethod
  173. def read(self, action: FileReadAction) -> Observation:
  174. pass
  175. @abstractmethod
  176. def write(self, action: FileWriteAction) -> Observation:
  177. pass
  178. @abstractmethod
  179. def browse(self, action: BrowseURLAction) -> Observation:
  180. pass
  181. @abstractmethod
  182. def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  183. pass
  184. # ====================================================================
  185. # File operations
  186. # ====================================================================
  187. @abstractmethod
  188. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  189. raise NotImplementedError('This method is not implemented in the base class.')
  190. @abstractmethod
  191. def list_files(self, path: str | None = None) -> list[str]:
  192. """List files in the sandbox.
  193. If path is None, list files in the sandbox's initial working directory (e.g., /workspace).
  194. """
  195. raise NotImplementedError('This method is not implemented in the base class.')
  196. @abstractmethod
  197. def copy_from(self, path: str) -> bytes:
  198. """Zip all files in the sandbox and return as a stream of bytes."""
  199. raise NotImplementedError('This method is not implemented in the base class.')