base.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import atexit
  2. import copy
  3. import json
  4. import os
  5. from abc import abstractmethod
  6. from pathlib import Path
  7. from typing import Callable
  8. from requests.exceptions import ConnectionError
  9. from openhands.core.config import AppConfig, SandboxConfig
  10. from openhands.core.exceptions import AgentRuntimeDisconnectedError
  11. from openhands.core.logger import openhands_logger as logger
  12. from openhands.events import EventSource, EventStream, EventStreamSubscriber
  13. from openhands.events.action import (
  14. Action,
  15. ActionConfirmationStatus,
  16. BrowseInteractiveAction,
  17. BrowseURLAction,
  18. CmdRunAction,
  19. FileReadAction,
  20. FileWriteAction,
  21. IPythonRunCellAction,
  22. )
  23. from openhands.events.event import Event
  24. from openhands.events.observation import (
  25. CmdOutputObservation,
  26. ErrorObservation,
  27. NullObservation,
  28. Observation,
  29. UserRejectObservation,
  30. )
  31. from openhands.events.serialization.action import ACTION_TYPE_TO_CLASS
  32. from openhands.runtime.plugins import (
  33. JupyterRequirement,
  34. PluginRequirement,
  35. VSCodeRequirement,
  36. )
  37. from openhands.runtime.utils.edit import FileEditRuntimeMixin
  38. from openhands.utils.async_utils import call_sync_from_async
  39. STATUS_MESSAGES = {
  40. 'STATUS$STARTING_RUNTIME': 'Starting runtime...',
  41. 'STATUS$STARTING_CONTAINER': 'Starting container...',
  42. 'STATUS$PREPARING_CONTAINER': 'Preparing container...',
  43. 'STATUS$CONTAINER_STARTED': 'Container started.',
  44. 'STATUS$WAITING_FOR_CLIENT': 'Waiting for client...',
  45. }
  46. def _default_env_vars(sandbox_config: SandboxConfig) -> dict[str, str]:
  47. ret = {}
  48. for key in os.environ:
  49. if key.startswith('SANDBOX_ENV_'):
  50. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  51. ret[sandbox_key] = os.environ[key]
  52. if sandbox_config.enable_auto_lint:
  53. ret['ENABLE_AUTO_LINT'] = 'true'
  54. return ret
  55. class Runtime(FileEditRuntimeMixin):
  56. """The runtime is how the agent interacts with the external environment.
  57. This includes a bash sandbox, a browser, and filesystem interactions.
  58. sid is the session id, which is used to identify the current user session.
  59. """
  60. sid: str
  61. config: AppConfig
  62. initial_env_vars: dict[str, str]
  63. attach_to_existing: bool
  64. status_callback: Callable | None
  65. def __init__(
  66. self,
  67. config: AppConfig,
  68. event_stream: EventStream,
  69. sid: str = 'default',
  70. plugins: list[PluginRequirement] | None = None,
  71. env_vars: dict[str, str] | None = None,
  72. status_callback: Callable | None = None,
  73. attach_to_existing: bool = False,
  74. headless_mode: bool = False,
  75. ):
  76. self.sid = sid
  77. self.event_stream = event_stream
  78. self.event_stream.subscribe(
  79. EventStreamSubscriber.RUNTIME, self.on_event, self.sid
  80. )
  81. self.plugins = (
  82. copy.deepcopy(plugins) if plugins is not None and len(plugins) > 0 else []
  83. )
  84. # add VSCode plugin if not in headless mode
  85. if not headless_mode:
  86. self.plugins.append(VSCodeRequirement())
  87. self.status_callback = status_callback
  88. self.attach_to_existing = attach_to_existing
  89. self.config = copy.deepcopy(config)
  90. atexit.register(self.close)
  91. self.initial_env_vars = _default_env_vars(config.sandbox)
  92. if env_vars is not None:
  93. self.initial_env_vars.update(env_vars)
  94. self._vscode_enabled = any(
  95. isinstance(plugin, VSCodeRequirement) for plugin in self.plugins
  96. )
  97. # Load mixins
  98. FileEditRuntimeMixin.__init__(self)
  99. def setup_initial_env(self) -> None:
  100. if self.attach_to_existing:
  101. return
  102. logger.debug(f'Adding env vars: {self.initial_env_vars}')
  103. self.add_env_vars(self.initial_env_vars)
  104. if self.config.sandbox.runtime_startup_env_vars:
  105. self.add_env_vars(self.config.sandbox.runtime_startup_env_vars)
  106. def close(self) -> None:
  107. pass
  108. def log(self, level: str, message: str) -> None:
  109. message = f'[runtime {self.sid}] {message}'
  110. getattr(logger, level)(message, stacklevel=2)
  111. def send_status_message(self, message_id: str):
  112. """Sends a status message if the callback function was provided."""
  113. if self.status_callback:
  114. msg = STATUS_MESSAGES.get(message_id, '')
  115. self.status_callback('info', message_id, msg)
  116. def send_error_message(self, message_id: str, message: str):
  117. if self.status_callback:
  118. self.status_callback('error', message_id, message)
  119. # ====================================================================
  120. def add_env_vars(self, env_vars: dict[str, str]) -> None:
  121. # Add env vars to the IPython shell (if Jupyter is used)
  122. if any(isinstance(plugin, JupyterRequirement) for plugin in self.plugins):
  123. code = 'import os\n'
  124. for key, value in env_vars.items():
  125. # Note: json.dumps gives us nice escaping for free
  126. code += f'os.environ["{key}"] = {json.dumps(value)}\n'
  127. code += '\n'
  128. obs = self.run_ipython(IPythonRunCellAction(code))
  129. self.log('debug', f'Added env vars to IPython: code={code}, obs={obs}')
  130. # Add env vars to the Bash shell
  131. cmd = ''
  132. for key, value in env_vars.items():
  133. # Note: json.dumps gives us nice escaping for free
  134. cmd += f'export {key}={json.dumps(value)}; '
  135. if not cmd:
  136. return
  137. cmd = cmd.strip()
  138. logger.debug(f'Adding env var: {cmd}')
  139. obs = self.run(CmdRunAction(cmd))
  140. if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
  141. raise RuntimeError(
  142. f'Failed to add env vars [{env_vars}] to environment: {obs.content}'
  143. )
  144. async def on_event(self, event: Event) -> None:
  145. if isinstance(event, Action):
  146. # set timeout to default if not set
  147. if event.timeout is None:
  148. event.timeout = self.config.sandbox.timeout
  149. assert event.timeout is not None
  150. try:
  151. observation: Observation = await call_sync_from_async(
  152. self.run_action, event
  153. )
  154. except Exception as e:
  155. err_id = ''
  156. if isinstance(e, ConnectionError) or isinstance(
  157. e, AgentRuntimeDisconnectedError
  158. ):
  159. err_id = 'STATUS$ERROR_RUNTIME_DISCONNECTED'
  160. logger.error(
  161. 'Unexpected error while running action',
  162. exc_info=True,
  163. stack_info=True,
  164. )
  165. self.log('error', f'Problematic action: {str(event)}')
  166. self.send_error_message(err_id, str(e))
  167. self.close()
  168. return
  169. observation._cause = event.id # type: ignore[attr-defined]
  170. observation.tool_call_metadata = event.tool_call_metadata
  171. # this might be unnecessary, since source should be set by the event stream when we're here
  172. source = event.source if event.source else EventSource.AGENT
  173. self.event_stream.add_event(observation, source) # type: ignore[arg-type]
  174. def clone_repo(self, github_token: str | None, selected_repository: str | None):
  175. if not github_token or not selected_repository:
  176. return
  177. url = f'https://{github_token}@github.com/{selected_repository}.git'
  178. dir_name = selected_repository.split('/')[1]
  179. action = CmdRunAction(
  180. command=f'git clone {url} {dir_name} ; cd {dir_name} ; git checkout -b openhands-workspace'
  181. )
  182. self.log('info', f'Cloning repo: {selected_repository}')
  183. self.run_action(action)
  184. def get_custom_microagents(self, selected_repository: str | None) -> list[str]:
  185. custom_microagents_content = []
  186. custom_microagents_dir = Path('.openhands') / 'microagents'
  187. dir_name = str(custom_microagents_dir)
  188. if selected_repository:
  189. dir_name = str(
  190. Path(selected_repository.split('/')[1]) / custom_microagents_dir
  191. )
  192. oh_instructions_header = '---\nname: openhands_instructions\nagent: CodeActAgent\ntriggers:\n- ""\n---\n'
  193. obs = self.read(FileReadAction(path='.openhands_instructions'))
  194. if isinstance(obs, ErrorObservation):
  195. self.log('error', 'Failed to read openhands_instructions')
  196. else:
  197. openhands_instructions = oh_instructions_header + obs.content
  198. self.log('info', f'openhands_instructions: {openhands_instructions}')
  199. custom_microagents_content.append(openhands_instructions)
  200. files = self.list_files(dir_name)
  201. self.log('info', f'Found {len(files)} custom microagents.')
  202. for fname in files:
  203. content = self.read(
  204. FileReadAction(path=str(custom_microagents_dir / fname))
  205. ).content
  206. custom_microagents_content.append(content)
  207. return custom_microagents_content
  208. def run_action(self, action: Action) -> Observation:
  209. """Run an action and return the resulting observation.
  210. If the action is not runnable in any runtime, a NullObservation is returned.
  211. If the action is not supported by the current runtime, an ErrorObservation is returned.
  212. """
  213. if not action.runnable:
  214. return NullObservation('')
  215. if (
  216. hasattr(action, 'confirmation_state')
  217. and action.confirmation_state
  218. == ActionConfirmationStatus.AWAITING_CONFIRMATION
  219. ):
  220. return NullObservation('')
  221. action_type = action.action # type: ignore[attr-defined]
  222. if action_type not in ACTION_TYPE_TO_CLASS:
  223. return ErrorObservation(f'Action {action_type} does not exist.')
  224. if not hasattr(self, action_type):
  225. return ErrorObservation(
  226. f'Action {action_type} is not supported in the current runtime.'
  227. )
  228. if (
  229. getattr(action, 'confirmation_state', None)
  230. == ActionConfirmationStatus.REJECTED
  231. ):
  232. return UserRejectObservation(
  233. 'Action has been rejected by the user! Waiting for further user input.'
  234. )
  235. observation = getattr(self, action_type)(action)
  236. return observation
  237. # ====================================================================
  238. # Context manager
  239. # ====================================================================
  240. def __enter__(self) -> 'Runtime':
  241. return self
  242. def __exit__(self, exc_type, exc_value, traceback) -> None:
  243. self.close()
  244. @abstractmethod
  245. async def connect(self) -> None:
  246. pass
  247. # ====================================================================
  248. # Action execution
  249. # ====================================================================
  250. @abstractmethod
  251. def run(self, action: CmdRunAction) -> Observation:
  252. pass
  253. @abstractmethod
  254. def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  255. pass
  256. @abstractmethod
  257. def read(self, action: FileReadAction) -> Observation:
  258. pass
  259. @abstractmethod
  260. def write(self, action: FileWriteAction) -> Observation:
  261. pass
  262. @abstractmethod
  263. def browse(self, action: BrowseURLAction) -> Observation:
  264. pass
  265. @abstractmethod
  266. def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  267. pass
  268. # ====================================================================
  269. # File operations
  270. # ====================================================================
  271. @abstractmethod
  272. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  273. raise NotImplementedError('This method is not implemented in the base class.')
  274. @abstractmethod
  275. def list_files(self, path: str | None = None) -> list[str]:
  276. """List files in the sandbox.
  277. If path is None, list files in the sandbox's initial working directory (e.g., /workspace).
  278. """
  279. raise NotImplementedError('This method is not implemented in the base class.')
  280. @abstractmethod
  281. def copy_from(self, path: str) -> Path:
  282. """Zip all files in the sandbox and return a path in the local filesystem."""
  283. raise NotImplementedError('This method is not implemented in the base class.')
  284. # ====================================================================
  285. # VSCode
  286. # ====================================================================
  287. @property
  288. def vscode_enabled(self) -> bool:
  289. return self._vscode_enabled
  290. @property
  291. def vscode_url(self) -> str | None:
  292. raise NotImplementedError('This method is not implemented in the base class.')