runtime.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. def _default_env_vars(sandbox_config: SandboxConfig) -> dict[str, str]:
  31. ret = {}
  32. for key in os.environ:
  33. if key.startswith('SANDBOX_ENV_'):
  34. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  35. ret[sandbox_key] = os.environ[key]
  36. if sandbox_config.enable_auto_lint:
  37. ret['ENABLE_AUTO_LINT'] = 'true'
  38. return ret
  39. class Runtime:
  40. """The runtime is how the agent interacts with the external environment.
  41. This includes a bash sandbox, a browser, and filesystem interactions.
  42. sid is the session id, which is used to identify the current user session.
  43. """
  44. sid: str
  45. config: AppConfig
  46. initial_env_vars: dict[str, str]
  47. def __init__(
  48. self,
  49. config: AppConfig,
  50. event_stream: EventStream,
  51. sid: str = 'default',
  52. plugins: list[PluginRequirement] | None = None,
  53. env_vars: dict[str, str] | None = None,
  54. status_message_callback: Callable | None = None,
  55. ):
  56. self.sid = sid
  57. self.event_stream = event_stream
  58. self.event_stream.subscribe(EventStreamSubscriber.RUNTIME, self.on_event)
  59. self.plugins = plugins if plugins is not None and len(plugins) > 0 else []
  60. self.status_message_callback = status_message_callback
  61. self.config = copy.deepcopy(config)
  62. atexit.register(self.close)
  63. self.initial_env_vars = _default_env_vars(config.sandbox)
  64. if env_vars is not None:
  65. self.initial_env_vars.update(env_vars)
  66. def setup_initial_env(self) -> None:
  67. logger.debug(f'Adding env vars: {self.initial_env_vars}')
  68. self.add_env_vars(self.initial_env_vars)
  69. def close(self) -> None:
  70. pass
  71. # ====================================================================
  72. def add_env_vars(self, env_vars: dict[str, str]) -> None:
  73. # Add env vars to the IPython shell (if Jupyter is used)
  74. if any(isinstance(plugin, JupyterRequirement) for plugin in self.plugins):
  75. code = 'import os\n'
  76. for key, value in env_vars.items():
  77. # Note: json.dumps gives us nice escaping for free
  78. code += f'os.environ["{key}"] = {json.dumps(value)}\n'
  79. code += '\n'
  80. obs = self.run_ipython(IPythonRunCellAction(code))
  81. logger.info(f'Added env vars to IPython: code={code}, obs={obs}')
  82. # Add env vars to the Bash shell
  83. cmd = ''
  84. for key, value in env_vars.items():
  85. # Note: json.dumps gives us nice escaping for free
  86. cmd += f'export {key}={json.dumps(value)}; '
  87. if not cmd:
  88. return
  89. cmd = cmd.strip()
  90. logger.debug(f'Adding env var: {cmd}')
  91. obs = self.run(CmdRunAction(cmd))
  92. if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
  93. raise RuntimeError(
  94. f'Failed to add env vars [{env_vars}] to environment: {obs.content}'
  95. )
  96. async def on_event(self, event: Event) -> None:
  97. if isinstance(event, Action):
  98. # set timeout to default if not set
  99. if event.timeout is None:
  100. event.timeout = self.config.sandbox.timeout
  101. assert event.timeout is not None
  102. observation = self.run_action(event)
  103. observation._cause = event.id # type: ignore[attr-defined]
  104. source = event.source if event.source else EventSource.AGENT
  105. self.event_stream.add_event(observation, source) # type: ignore[arg-type]
  106. def run_action(self, action: Action) -> Observation:
  107. """Run an action and return the resulting observation.
  108. If the action is not runnable in any runtime, a NullObservation is returned.
  109. If the action is not supported by the current runtime, an ErrorObservation is returned.
  110. """
  111. if not action.runnable:
  112. return NullObservation('')
  113. if (
  114. hasattr(action, 'is_confirmed')
  115. and action.is_confirmed == ActionConfirmationStatus.AWAITING_CONFIRMATION
  116. ):
  117. return NullObservation('')
  118. action_type = action.action # type: ignore[attr-defined]
  119. if action_type not in ACTION_TYPE_TO_CLASS:
  120. return ErrorObservation(f'Action {action_type} does not exist.')
  121. if not hasattr(self, action_type):
  122. return ErrorObservation(
  123. f'Action {action_type} is not supported in the current runtime.'
  124. )
  125. if (
  126. hasattr(action, 'is_confirmed')
  127. and action.is_confirmed == ActionConfirmationStatus.REJECTED
  128. ):
  129. return UserRejectObservation(
  130. 'Action has been rejected by the user! Waiting for further user input.'
  131. )
  132. observation = getattr(self, action_type)(action)
  133. return observation
  134. # ====================================================================
  135. # Context manager
  136. # ====================================================================
  137. def __enter__(self) -> 'Runtime':
  138. return self
  139. def __exit__(self, exc_type, exc_value, traceback) -> None:
  140. self.close()
  141. # ====================================================================
  142. # Action execution
  143. # ====================================================================
  144. @abstractmethod
  145. def run(self, action: CmdRunAction) -> Observation:
  146. pass
  147. @abstractmethod
  148. def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  149. pass
  150. @abstractmethod
  151. def read(self, action: FileReadAction) -> Observation:
  152. pass
  153. @abstractmethod
  154. def write(self, action: FileWriteAction) -> Observation:
  155. pass
  156. @abstractmethod
  157. def browse(self, action: BrowseURLAction) -> Observation:
  158. pass
  159. @abstractmethod
  160. def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  161. pass
  162. # ====================================================================
  163. # File operations
  164. # ====================================================================
  165. @abstractmethod
  166. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  167. raise NotImplementedError('This method is not implemented in the base class.')
  168. @abstractmethod
  169. def list_files(self, path: str | None = None) -> list[str]:
  170. """List files in the sandbox.
  171. If path is None, list files in the sandbox's initial working directory (e.g., /workspace).
  172. """
  173. raise NotImplementedError('This method is not implemented in the base class.')