client.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. """
  2. This is the main file for the runtime client.
  3. It is responsible for executing actions received from OpenHands backend and producing observations.
  4. NOTE: this will be executed inside the docker sandbox.
  5. """
  6. import argparse
  7. import asyncio
  8. import os
  9. import re
  10. import shutil
  11. import subprocess
  12. import time
  13. from contextlib import asynccontextmanager
  14. from pathlib import Path
  15. import pexpect
  16. from fastapi import FastAPI, HTTPException, Request, UploadFile
  17. from fastapi.exceptions import RequestValidationError
  18. from fastapi.responses import JSONResponse
  19. from pydantic import BaseModel
  20. from starlette.exceptions import HTTPException as StarletteHTTPException
  21. from uvicorn import run
  22. from openhands.core.logger import openhands_logger as logger
  23. from openhands.events.action import (
  24. Action,
  25. BrowseInteractiveAction,
  26. BrowseURLAction,
  27. CmdRunAction,
  28. FileReadAction,
  29. FileWriteAction,
  30. IPythonRunCellAction,
  31. )
  32. from openhands.events.observation import (
  33. CmdOutputObservation,
  34. ErrorObservation,
  35. FileReadObservation,
  36. FileWriteObservation,
  37. IPythonRunCellObservation,
  38. Observation,
  39. )
  40. from openhands.events.serialization import event_from_dict, event_to_dict
  41. from openhands.runtime.browser import browse
  42. from openhands.runtime.browser.browser_env import BrowserEnv
  43. from openhands.runtime.plugins import (
  44. ALL_PLUGINS,
  45. JupyterPlugin,
  46. Plugin,
  47. )
  48. from openhands.runtime.utils import split_bash_commands
  49. from openhands.runtime.utils.files import insert_lines, read_lines
  50. class ActionRequest(BaseModel):
  51. action: dict
  52. ROOT_GID = 0
  53. INIT_COMMANDS = [
  54. 'git config --global user.name "openhands" && git config --global user.email "openhands@all-hands.dev" && alias git="git --no-pager"',
  55. ]
  56. SOFT_TIMEOUT_SECONDS = 5
  57. class RuntimeClient:
  58. """RuntimeClient is running inside docker sandbox.
  59. It is responsible for executing actions received from OpenHands backend and producing observations.
  60. """
  61. def __init__(
  62. self,
  63. plugins_to_load: list[Plugin],
  64. work_dir: str,
  65. username: str,
  66. user_id: int,
  67. browsergym_eval_env: str | None,
  68. ) -> None:
  69. self.plugins_to_load = plugins_to_load
  70. self.username = username
  71. self.user_id = user_id
  72. self.pwd = work_dir # current PWD
  73. self._initial_pwd = work_dir
  74. self._init_user(self.username, self.user_id)
  75. self._init_bash_shell(self.pwd, self.username)
  76. self.lock = asyncio.Lock()
  77. self.plugins: dict[str, Plugin] = {}
  78. self.browser = BrowserEnv(browsergym_eval_env)
  79. self.start_time = time.time()
  80. self.last_execution_time = self.start_time
  81. @property
  82. def initial_pwd(self):
  83. return self._initial_pwd
  84. async def ainit(self):
  85. for plugin in self.plugins_to_load:
  86. await plugin.initialize(self.username)
  87. self.plugins[plugin.name] = plugin
  88. logger.info(f'Initializing plugin: {plugin.name}')
  89. if isinstance(plugin, JupyterPlugin):
  90. await self.run_ipython(
  91. IPythonRunCellAction(code=f'import os; os.chdir("{self.pwd}")')
  92. )
  93. # This is a temporary workaround
  94. # TODO: refactor AgentSkills to be part of JupyterPlugin
  95. # AFTER ServerRuntime is deprecated
  96. if 'agent_skills' in self.plugins and 'jupyter' in self.plugins:
  97. obs = await self.run_ipython(
  98. IPythonRunCellAction(
  99. code='from openhands.runtime.plugins.agent_skills.agentskills import *\n'
  100. )
  101. )
  102. logger.info(f'AgentSkills initialized: {obs}')
  103. await self._init_bash_commands()
  104. logger.info('Runtime client initialized.')
  105. def _init_user(self, username: str, user_id: int) -> None:
  106. """Create working directory and user if not exists.
  107. It performs the following steps effectively:
  108. * Creates the Working Directory:
  109. - Uses mkdir -p to create the directory.
  110. - Sets ownership to username:root.
  111. - Adjusts permissions to be readable and writable by group and others.
  112. * User Verification and Creation:
  113. - Checks if the user exists using id -u.
  114. - If the user exists with the correct UID, it skips creation.
  115. - If the UID differs, it logs a warning and updates self.user_id.
  116. - If the user doesn't exist, it proceeds to create the user.
  117. * Sudo Configuration:
  118. - Appends %sudo ALL=(ALL) NOPASSWD:ALL to /etc/sudoers to grant
  119. passwordless sudo access to the sudo group.
  120. - Adds the user to the sudo group with the useradd command, handling
  121. UID conflicts by incrementing the UID if necessary.
  122. """
  123. # First create the working directory, independent of the user
  124. logger.info(f'Client working directory: {self.initial_pwd}')
  125. command = f'umask 002; mkdir -p {self.initial_pwd}'
  126. output = subprocess.run(command, shell=True, capture_output=True)
  127. out_str = output.stdout.decode()
  128. command = f'chown -R {username}:root {self.initial_pwd}'
  129. output = subprocess.run(command, shell=True, capture_output=True)
  130. out_str += output.stdout.decode()
  131. command = f'chmod g+rw {self.initial_pwd}'
  132. output = subprocess.run(command, shell=True, capture_output=True)
  133. out_str += output.stdout.decode()
  134. logger.debug(f'Created working directory. Output: [{out_str}]')
  135. # Skip root since it is already created
  136. if username == 'root':
  137. return
  138. # Check if the username already exists
  139. existing_user_id = -1
  140. try:
  141. result = subprocess.run(
  142. f'id -u {username}', shell=True, check=True, capture_output=True
  143. )
  144. existing_user_id = int(result.stdout.decode().strip())
  145. # The user ID already exists, skip setup
  146. if existing_user_id == user_id:
  147. logger.debug(
  148. f'User `{username}` already has the provided UID {user_id}. Skipping user setup.'
  149. )
  150. else:
  151. logger.warning(
  152. f'User `{username}` already exists with UID {existing_user_id}. Skipping user setup.'
  153. )
  154. self.user_id = existing_user_id
  155. return
  156. except subprocess.CalledProcessError as e:
  157. # Returncode 1 indicates, that the user does not exist yet
  158. if e.returncode == 1:
  159. logger.debug(
  160. f'User `{username}` does not exist. Proceeding with user creation.'
  161. )
  162. else:
  163. logger.error(
  164. f'Error checking user `{username}`, skipping setup:\n{e}\n'
  165. )
  166. raise
  167. # Add sudoer
  168. sudoer_line = r"echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers"
  169. output = subprocess.run(sudoer_line, shell=True, capture_output=True)
  170. if output.returncode != 0:
  171. raise RuntimeError(f'Failed to add sudoer: {output.stderr.decode()}')
  172. logger.debug(f'Added sudoer successfully. Output: [{output.stdout.decode()}]')
  173. command = (
  174. f'useradd -rm -d /home/{username} -s /bin/bash '
  175. f'-g root -G sudo -u {user_id} {username}'
  176. )
  177. output = subprocess.run(command, shell=True, capture_output=True)
  178. if output.returncode == 0:
  179. logger.debug(
  180. f'Added user `{username}` successfully with UID {user_id}. Output: [{output.stdout.decode()}]'
  181. )
  182. else:
  183. raise RuntimeError(
  184. f'Failed to create user `{username}` with UID {user_id}. Output: [{output.stderr.decode()}]'
  185. )
  186. def _init_bash_shell(self, work_dir: str, username: str) -> None:
  187. self.shell = pexpect.spawn(
  188. f'su {username}',
  189. encoding='utf-8',
  190. echo=False,
  191. )
  192. self.__bash_PS1 = (
  193. r'[PEXPECT_BEGIN]\n'
  194. r'$(which python >/dev/null 2>&1 && echo "[Python Interpreter: $(which python)]\n")'
  195. r'\u@\h:\w\n'
  196. r'[PEXPECT_END]'
  197. )
  198. # This should NOT match "PS1=\u@\h:\w [PEXPECT]$" when `env` is executed
  199. self.__bash_expect_regex = r'\[PEXPECT_BEGIN\]\s*(.*?)\s*([a-z0-9_-]*)@([a-zA-Z0-9.-]*):(.+)\s*\[PEXPECT_END\]'
  200. # Set umask to allow group write permissions
  201. self.shell.sendline(f'umask 002; export PS1="{self.__bash_PS1}"; export PS2=""')
  202. self.shell.expect(self.__bash_expect_regex)
  203. self.shell.sendline(
  204. f'if [ ! -d "{work_dir}" ]; then mkdir -p "{work_dir}"; fi && cd "{work_dir}"'
  205. )
  206. self.shell.expect(self.__bash_expect_regex)
  207. logger.debug(
  208. f'Bash initialized. Working directory: {work_dir}. Output: [{self.shell.before}]'
  209. )
  210. # Ensure the group has write permissions on the working directory
  211. self.shell.sendline(f'chmod g+rw "{work_dir}"')
  212. self.shell.expect(self.__bash_expect_regex)
  213. async def _init_bash_commands(self):
  214. logger.info(f'Initializing by running {len(INIT_COMMANDS)} bash commands...')
  215. for command in INIT_COMMANDS:
  216. action = CmdRunAction(command=command)
  217. action.timeout = 300
  218. logger.debug(f'Executing init command: {command}')
  219. obs: CmdOutputObservation = await self.run(action)
  220. logger.debug(
  221. f'Init command outputs (exit code: {obs.exit_code}): {obs.content}'
  222. )
  223. assert obs.exit_code == 0
  224. logger.info('Bash init commands completed')
  225. def _get_bash_prompt_and_update_pwd(self):
  226. ps1 = self.shell.after
  227. if ps1 == pexpect.EOF:
  228. logger.error(f'Bash shell EOF! {self.shell.after=}, {self.shell.before=}')
  229. raise RuntimeError('Bash shell EOF')
  230. if ps1 == pexpect.TIMEOUT:
  231. logger.warning('Bash shell timeout')
  232. return ''
  233. # begin at the last occurrence of '[PEXPECT_BEGIN]'.
  234. # In multi-line bash commands, the prompt will be repeated
  235. # and the matched regex captures all of them
  236. # - we only want the last one (newest prompt)
  237. _begin_pos = ps1.rfind('[PEXPECT_BEGIN]')
  238. if _begin_pos != -1:
  239. ps1 = ps1[_begin_pos:]
  240. # parse the ps1 to get username, hostname, and working directory
  241. matched = re.match(self.__bash_expect_regex, ps1)
  242. assert (
  243. matched is not None
  244. ), f'Failed to parse bash prompt: {ps1}. This should not happen.'
  245. other_info, username, hostname, working_dir = matched.groups()
  246. working_dir = working_dir.rstrip()
  247. self.pwd = os.path.expanduser(working_dir)
  248. # re-assemble the prompt
  249. prompt = f'{other_info.strip()}\n{username}@{hostname}:{working_dir} '
  250. if username == 'root':
  251. prompt += '#'
  252. else:
  253. prompt += '$'
  254. return prompt + ' '
  255. def _execute_bash(
  256. self,
  257. command: str,
  258. timeout: int | None,
  259. keep_prompt: bool = True,
  260. kill_on_timeout: bool = True,
  261. ) -> tuple[str, int]:
  262. logger.debug(f'Executing command: {command}')
  263. self.shell.sendline(command)
  264. return self._continue_bash(
  265. timeout=timeout, keep_prompt=keep_prompt, kill_on_timeout=kill_on_timeout
  266. )
  267. def _interrupt_bash(self, timeout: int | None = None) -> tuple[str, int]:
  268. self.shell.sendintr() # send SIGINT to the shell
  269. self.shell.expect(self.__bash_expect_regex, timeout=timeout)
  270. output = self.shell.before
  271. exit_code = 130 # SIGINT
  272. return output, exit_code
  273. def _continue_bash(
  274. self,
  275. timeout: int | None,
  276. keep_prompt: bool = True,
  277. kill_on_timeout: bool = True,
  278. ) -> tuple[str, int]:
  279. try:
  280. self.shell.expect(self.__bash_expect_regex, timeout=timeout)
  281. output = self.shell.before
  282. # Get exit code
  283. self.shell.sendline('echo $?')
  284. logger.debug('Requesting exit code...')
  285. self.shell.expect(self.__bash_expect_regex, timeout=timeout)
  286. _exit_code_output = self.shell.before
  287. try:
  288. exit_code = int(_exit_code_output.strip().split()[0])
  289. except Exception:
  290. logger.error('Error getting exit code from bash script')
  291. # If we try to run an invalid shell script the output sometimes includes error text
  292. # rather than the error code - we assume this is an error
  293. exit_code = 2
  294. except pexpect.TIMEOUT as e:
  295. if kill_on_timeout:
  296. output, exit_code = self._interrupt_bash()
  297. output += (
  298. '\r\n\r\n'
  299. + f'[Command timed out after {timeout} seconds. SIGINT was sent to interrupt it.]'
  300. )
  301. logger.error(f'Failed to execute command. Error: {e}')
  302. else:
  303. output = self.shell.before or ''
  304. exit_code = -1
  305. finally:
  306. bash_prompt = self._get_bash_prompt_and_update_pwd()
  307. if keep_prompt:
  308. output += '\r\n' + bash_prompt
  309. # logger.debug(f'Command output:\n{output}')
  310. return output, exit_code
  311. async def run_action(self, action) -> Observation:
  312. action_type = action.action
  313. logger.debug(f'Running action:\n{action}')
  314. observation = await getattr(self, action_type)(action)
  315. logger.debug(f'Action output:\n{observation}')
  316. return observation
  317. async def run(self, action: CmdRunAction) -> CmdOutputObservation:
  318. try:
  319. assert (
  320. action.timeout is not None
  321. ), f'Timeout argument is required for CmdRunAction: {action}'
  322. commands = split_bash_commands(action.command)
  323. all_output = ''
  324. for command in commands:
  325. if command == '':
  326. output, exit_code = self._continue_bash(
  327. timeout=SOFT_TIMEOUT_SECONDS,
  328. keep_prompt=action.keep_prompt,
  329. kill_on_timeout=False,
  330. )
  331. elif command.lower() == 'ctrl+c':
  332. output, exit_code = self._interrupt_bash(
  333. timeout=SOFT_TIMEOUT_SECONDS
  334. )
  335. else:
  336. output, exit_code = self._execute_bash(
  337. command,
  338. timeout=SOFT_TIMEOUT_SECONDS
  339. if not action.blocking
  340. else action.timeout,
  341. keep_prompt=action.keep_prompt,
  342. kill_on_timeout=False if not action.blocking else True,
  343. )
  344. if all_output:
  345. # previous output already exists with prompt "user@hostname:working_dir #""
  346. # we need to add the command to the previous output,
  347. # so model knows the following is the output of another action)
  348. all_output = all_output.rstrip() + ' ' + command + '\r\n'
  349. all_output += str(output) + '\r\n'
  350. if exit_code != 0:
  351. break
  352. return CmdOutputObservation(
  353. command_id=-1,
  354. content=all_output.rstrip('\r\n'),
  355. command=action.command,
  356. exit_code=exit_code,
  357. )
  358. except UnicodeDecodeError:
  359. raise RuntimeError('Command output could not be decoded as utf-8')
  360. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  361. if 'jupyter' in self.plugins:
  362. _jupyter_plugin: JupyterPlugin = self.plugins['jupyter'] # type: ignore
  363. # This is used to make AgentSkills in Jupyter aware of the
  364. # current working directory in Bash
  365. jupyter_pwd = getattr(self, '_jupyter_pwd', None)
  366. if self.pwd != jupyter_pwd:
  367. logger.debug(f'{self.pwd} != {jupyter_pwd} -> reset Jupyter PWD')
  368. reset_jupyter_pwd_code = f'import os; os.chdir("{self.pwd}")'
  369. _aux_action = IPythonRunCellAction(code=reset_jupyter_pwd_code)
  370. _reset_obs = await _jupyter_plugin.run(_aux_action)
  371. logger.debug(
  372. f'Changed working directory in IPython to: {self.pwd}. Output: {_reset_obs}'
  373. )
  374. self._jupyter_pwd = self.pwd
  375. obs: IPythonRunCellObservation = await _jupyter_plugin.run(action)
  376. obs.content = obs.content.rstrip()
  377. obs.content += f'\n[Jupyter current working directory: {self.pwd}]'
  378. obs.content += f'\n[Jupyter Python interpreter: {_jupyter_plugin.python_interpreter_path}]'
  379. return obs
  380. else:
  381. raise RuntimeError(
  382. 'JupyterRequirement not found. Unable to run IPython action.'
  383. )
  384. def _get_working_directory(self):
  385. # NOTE: this is part of initialization, so we hard code the timeout
  386. result, exit_code = self._execute_bash('pwd', timeout=60, keep_prompt=False)
  387. if exit_code != 0:
  388. raise RuntimeError('Failed to get working directory')
  389. return result.strip()
  390. def _resolve_path(self, path: str, working_dir: str) -> str:
  391. filepath = Path(path)
  392. if not filepath.is_absolute():
  393. return str(Path(working_dir) / filepath)
  394. return str(filepath)
  395. async def read(self, action: FileReadAction) -> Observation:
  396. # NOTE: the client code is running inside the sandbox,
  397. # so there's no need to check permission
  398. working_dir = self._get_working_directory()
  399. filepath = self._resolve_path(action.path, working_dir)
  400. try:
  401. with open(filepath, 'r', encoding='utf-8') as file:
  402. lines = read_lines(file.readlines(), action.start, action.end)
  403. except FileNotFoundError:
  404. return ErrorObservation(
  405. f'File not found: {filepath}. Your current working directory is {working_dir}.'
  406. )
  407. except UnicodeDecodeError:
  408. return ErrorObservation(f'File could not be decoded as utf-8: {filepath}.')
  409. except IsADirectoryError:
  410. return ErrorObservation(
  411. f'Path is a directory: {filepath}. You can only read files'
  412. )
  413. code_view = ''.join(lines)
  414. return FileReadObservation(path=filepath, content=code_view)
  415. async def write(self, action: FileWriteAction) -> Observation:
  416. working_dir = self._get_working_directory()
  417. filepath = self._resolve_path(action.path, working_dir)
  418. insert = action.content.split('\n')
  419. try:
  420. if not os.path.exists(os.path.dirname(filepath)):
  421. os.makedirs(os.path.dirname(filepath))
  422. file_exists = os.path.exists(filepath)
  423. if file_exists:
  424. file_stat = os.stat(filepath)
  425. else:
  426. file_stat = None
  427. mode = 'w' if not file_exists else 'r+'
  428. try:
  429. with open(filepath, mode, encoding='utf-8') as file:
  430. if mode != 'w':
  431. all_lines = file.readlines()
  432. new_file = insert_lines(
  433. insert, all_lines, action.start, action.end
  434. )
  435. else:
  436. new_file = [i + '\n' for i in insert]
  437. file.seek(0)
  438. file.writelines(new_file)
  439. file.truncate()
  440. # Handle file permissions
  441. if file_exists:
  442. assert file_stat is not None
  443. # restore the original file permissions if the file already exists
  444. os.chmod(filepath, file_stat.st_mode)
  445. os.chown(filepath, file_stat.st_uid, file_stat.st_gid)
  446. else:
  447. # set the new file permissions if the file is new
  448. os.chmod(filepath, 0o664)
  449. os.chown(filepath, self.user_id, self.user_id)
  450. except FileNotFoundError:
  451. return ErrorObservation(f'File not found: {filepath}')
  452. except IsADirectoryError:
  453. return ErrorObservation(
  454. f'Path is a directory: {filepath}. You can only write to files'
  455. )
  456. except UnicodeDecodeError:
  457. return ErrorObservation(
  458. f'File could not be decoded as utf-8: {filepath}'
  459. )
  460. except PermissionError:
  461. return ErrorObservation(f'Malformed paths not permitted: {filepath}')
  462. return FileWriteObservation(content='', path=filepath)
  463. async def browse(self, action: BrowseURLAction) -> Observation:
  464. return await browse(action, self.browser)
  465. async def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  466. return await browse(action, self.browser)
  467. def close(self):
  468. self.shell.close()
  469. self.browser.close()
  470. if __name__ == '__main__':
  471. parser = argparse.ArgumentParser()
  472. parser.add_argument('port', type=int, help='Port to listen on')
  473. parser.add_argument('--working-dir', type=str, help='Working directory')
  474. parser.add_argument('--plugins', type=str, help='Plugins to initialize', nargs='+')
  475. parser.add_argument(
  476. '--username', type=str, help='User to run as', default='openhands'
  477. )
  478. parser.add_argument('--user-id', type=int, help='User ID to run as', default=1000)
  479. parser.add_argument(
  480. '--browsergym-eval-env',
  481. type=str,
  482. help='BrowserGym environment used for browser evaluation',
  483. default=None,
  484. )
  485. # example: python client.py 8000 --working-dir /workspace --plugins JupyterRequirement
  486. args = parser.parse_args()
  487. plugins_to_load: list[Plugin] = []
  488. if args.plugins:
  489. for plugin in args.plugins:
  490. if plugin not in ALL_PLUGINS:
  491. raise ValueError(f'Plugin {plugin} not found')
  492. plugins_to_load.append(ALL_PLUGINS[plugin]()) # type: ignore
  493. client: RuntimeClient | None = None
  494. @asynccontextmanager
  495. async def lifespan(app: FastAPI):
  496. global client
  497. client = RuntimeClient(
  498. plugins_to_load,
  499. work_dir=args.working_dir,
  500. username=args.username,
  501. user_id=args.user_id,
  502. browsergym_eval_env=args.browsergym_eval_env,
  503. )
  504. await client.ainit()
  505. yield
  506. # Clean up & release the resources
  507. client.close()
  508. app = FastAPI(lifespan=lifespan)
  509. # TODO below 3 exception handlers were recommended by Sonnet.
  510. # Are these something we should keep?
  511. @app.exception_handler(Exception)
  512. async def global_exception_handler(request: Request, exc: Exception):
  513. logger.exception('Unhandled exception occurred:')
  514. return JSONResponse(
  515. status_code=500,
  516. content={
  517. 'message': 'An unexpected error occurred. Please try again later.'
  518. },
  519. )
  520. @app.exception_handler(StarletteHTTPException)
  521. async def http_exception_handler(request: Request, exc: StarletteHTTPException):
  522. logger.error(f'HTTP exception occurred: {exc.detail}')
  523. return JSONResponse(
  524. status_code=exc.status_code, content={'message': exc.detail}
  525. )
  526. @app.exception_handler(RequestValidationError)
  527. async def validation_exception_handler(
  528. request: Request, exc: RequestValidationError
  529. ):
  530. logger.error(f'Validation error occurred: {exc}')
  531. return JSONResponse(
  532. status_code=422,
  533. content={'message': 'Invalid request parameters', 'details': exc.errors()},
  534. )
  535. @app.middleware('http')
  536. async def one_request_at_a_time(request: Request, call_next):
  537. assert client is not None
  538. async with client.lock:
  539. response = await call_next(request)
  540. return response
  541. @app.get('/server_info')
  542. async def get_server_info():
  543. assert client is not None
  544. current_time = time.time()
  545. uptime = current_time - client.start_time
  546. idle_time = current_time - client.last_execution_time
  547. return {'uptime': uptime, 'idle_time': idle_time}
  548. @app.post('/execute_action')
  549. async def execute_action(action_request: ActionRequest):
  550. assert client is not None
  551. try:
  552. action = event_from_dict(action_request.action)
  553. if not isinstance(action, Action):
  554. raise HTTPException(status_code=400, detail='Invalid action type')
  555. client.last_execution_time = time.time()
  556. observation = await client.run_action(action)
  557. return event_to_dict(observation)
  558. except Exception as e:
  559. logger.error(
  560. f'Error processing command: {str(e)}', exc_info=True, stack_info=True
  561. )
  562. raise HTTPException(status_code=500, detail=str(e))
  563. @app.post('/upload_file')
  564. async def upload_file(
  565. file: UploadFile, destination: str = '/', recursive: bool = False
  566. ):
  567. assert client is not None
  568. try:
  569. # Ensure the destination directory exists
  570. if not os.path.isabs(destination):
  571. raise HTTPException(
  572. status_code=400, detail='Destination must be an absolute path'
  573. )
  574. full_dest_path = destination
  575. if not os.path.exists(full_dest_path):
  576. os.makedirs(full_dest_path, exist_ok=True)
  577. if recursive:
  578. # For recursive uploads, we expect a zip file
  579. if not file.filename.endswith('.zip'):
  580. raise HTTPException(
  581. status_code=400, detail='Recursive uploads must be zip files'
  582. )
  583. zip_path = os.path.join(full_dest_path, file.filename)
  584. with open(zip_path, 'wb') as buffer:
  585. shutil.copyfileobj(file.file, buffer)
  586. # Extract the zip file
  587. shutil.unpack_archive(zip_path, full_dest_path)
  588. os.remove(zip_path) # Remove the zip file after extraction
  589. logger.info(
  590. f'Uploaded file {file.filename} and extracted to {destination}'
  591. )
  592. else:
  593. # For single file uploads
  594. file_path = os.path.join(full_dest_path, file.filename)
  595. with open(file_path, 'wb') as buffer:
  596. shutil.copyfileobj(file.file, buffer)
  597. logger.info(f'Uploaded file {file.filename} to {destination}')
  598. return JSONResponse(
  599. content={
  600. 'filename': file.filename,
  601. 'destination': destination,
  602. 'recursive': recursive,
  603. },
  604. status_code=200,
  605. )
  606. except Exception as e:
  607. raise HTTPException(status_code=500, detail=str(e))
  608. @app.get('/alive')
  609. async def alive():
  610. return {'status': 'ok'}
  611. # ================================
  612. # File-specific operations for UI
  613. # ================================
  614. @app.post('/list_files')
  615. async def list_files(request: Request):
  616. """List files in the specified path.
  617. This function retrieves a list of files from the agent's runtime file store,
  618. excluding certain system and hidden files/directories.
  619. To list files:
  620. ```sh
  621. curl http://localhost:3000/api/list-files
  622. ```
  623. Args:
  624. request (Request): The incoming request object.
  625. path (str, optional): The path to list files from. Defaults to '/'.
  626. Returns:
  627. list: A list of file names in the specified path.
  628. Raises:
  629. HTTPException: If there's an error listing the files.
  630. """
  631. assert client is not None
  632. # get request as dict
  633. request_dict = await request.json()
  634. path = request_dict.get('path', None)
  635. # Get the full path of the requested directory
  636. if path is None:
  637. full_path = client.initial_pwd
  638. elif os.path.isabs(path):
  639. full_path = path
  640. else:
  641. full_path = os.path.join(client.initial_pwd, path)
  642. if not os.path.exists(full_path):
  643. # if user just removed a folder, prevent server error 500 in UI
  644. return []
  645. try:
  646. # Check if the directory exists
  647. if not os.path.exists(full_path) or not os.path.isdir(full_path):
  648. return []
  649. entries = os.listdir(full_path)
  650. # Separate directories and files
  651. directories = []
  652. files = []
  653. for entry in entries:
  654. # Remove leading slash and any parent directory components
  655. entry_relative = entry.lstrip('/').split('/')[-1]
  656. # Construct the full path by joining the base path with the relative entry path
  657. full_entry_path = os.path.join(full_path, entry_relative)
  658. if os.path.exists(full_entry_path):
  659. is_dir = os.path.isdir(full_entry_path)
  660. if is_dir:
  661. # add trailing slash to directories
  662. # required by FE to differentiate directories and files
  663. entry = entry.rstrip('/') + '/'
  664. directories.append(entry)
  665. else:
  666. files.append(entry)
  667. # Sort directories and files separately
  668. directories.sort(key=lambda s: s.lower())
  669. files.sort(key=lambda s: s.lower())
  670. # Combine sorted directories and files
  671. sorted_entries = directories + files
  672. return sorted_entries
  673. except Exception as e:
  674. logger.error(f'Error listing files: {e}', exc_info=True)
  675. return []
  676. logger.info('Runtime client initialized.')
  677. logger.info(f'Starting action execution API on port {args.port}')
  678. run(app, host='0.0.0.0', port=args.port)