action_execution_server.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 base64
  9. import io
  10. import json
  11. import mimetypes
  12. import os
  13. import re
  14. import shutil
  15. import tempfile
  16. import time
  17. import traceback
  18. from contextlib import asynccontextmanager
  19. from pathlib import Path
  20. from zipfile import ZipFile
  21. from fastapi import Depends, FastAPI, HTTPException, Request, UploadFile
  22. from fastapi.exceptions import RequestValidationError
  23. from fastapi.responses import JSONResponse, StreamingResponse
  24. from fastapi.security import APIKeyHeader
  25. from openhands_aci.utils.diff import get_diff
  26. from pydantic import BaseModel
  27. from starlette.exceptions import HTTPException as StarletteHTTPException
  28. from uvicorn import run
  29. from openhands.core.logger import openhands_logger as logger
  30. from openhands.events.action import (
  31. Action,
  32. BrowseInteractiveAction,
  33. BrowseURLAction,
  34. CmdRunAction,
  35. FileReadAction,
  36. FileWriteAction,
  37. IPythonRunCellAction,
  38. )
  39. from openhands.events.event import FileEditSource, FileReadSource
  40. from openhands.events.observation import (
  41. CmdOutputObservation,
  42. ErrorObservation,
  43. FileEditObservation,
  44. FileReadObservation,
  45. FileWriteObservation,
  46. IPythonRunCellObservation,
  47. Observation,
  48. )
  49. from openhands.events.serialization import event_from_dict, event_to_dict
  50. from openhands.runtime.browser import browse
  51. from openhands.runtime.browser.browser_env import BrowserEnv
  52. from openhands.runtime.plugins import ALL_PLUGINS, JupyterPlugin, Plugin, VSCodePlugin
  53. from openhands.runtime.utils.bash import BashSession
  54. from openhands.runtime.utils.files import insert_lines, read_lines
  55. from openhands.runtime.utils.runtime_init import init_user_and_working_directory
  56. from openhands.runtime.utils.system import check_port_available
  57. from openhands.runtime.utils.system_stats import get_system_stats
  58. from openhands.utils.async_utils import call_sync_from_async, wait_all
  59. class ActionRequest(BaseModel):
  60. action: dict
  61. ROOT_GID = 0
  62. INIT_COMMANDS = [
  63. 'git config --global user.name "openhands" && git config --global user.email "openhands@all-hands.dev" && alias git="git --no-pager"',
  64. ]
  65. SESSION_API_KEY = os.environ.get('SESSION_API_KEY')
  66. api_key_header = APIKeyHeader(name='X-Session-API-Key', auto_error=False)
  67. def verify_api_key(api_key: str = Depends(api_key_header)):
  68. if SESSION_API_KEY and api_key != SESSION_API_KEY:
  69. raise HTTPException(status_code=403, detail='Invalid API Key')
  70. return api_key
  71. class ActionExecutor:
  72. """ActionExecutor is running inside docker sandbox.
  73. It is responsible for executing actions received from OpenHands backend and producing observations.
  74. """
  75. def __init__(
  76. self,
  77. plugins_to_load: list[Plugin],
  78. work_dir: str,
  79. username: str,
  80. user_id: int,
  81. browsergym_eval_env: str | None,
  82. ) -> None:
  83. self.plugins_to_load = plugins_to_load
  84. self._initial_pwd = work_dir
  85. self.username = username
  86. self.user_id = user_id
  87. _updated_user_id = init_user_and_working_directory(
  88. username=username, user_id=self.user_id, initial_pwd=work_dir
  89. )
  90. if _updated_user_id is not None:
  91. self.user_id = _updated_user_id
  92. self.bash_session = BashSession(
  93. work_dir=work_dir,
  94. username=username,
  95. )
  96. self.lock = asyncio.Lock()
  97. self.plugins: dict[str, Plugin] = {}
  98. self.browser = BrowserEnv(browsergym_eval_env)
  99. self.start_time = time.time()
  100. self.last_execution_time = self.start_time
  101. @property
  102. def initial_pwd(self):
  103. return self._initial_pwd
  104. async def ainit(self):
  105. await wait_all(
  106. (self._init_plugin(plugin) for plugin in self.plugins_to_load),
  107. timeout=30,
  108. )
  109. # This is a temporary workaround
  110. # TODO: refactor AgentSkills to be part of JupyterPlugin
  111. # AFTER ServerRuntime is deprecated
  112. if 'agent_skills' in self.plugins and 'jupyter' in self.plugins:
  113. obs = await self.run_ipython(
  114. IPythonRunCellAction(
  115. code='from openhands.runtime.plugins.agent_skills.agentskills import *\n'
  116. )
  117. )
  118. logger.debug(f'AgentSkills initialized: {obs}')
  119. await self._init_bash_commands()
  120. logger.debug('Runtime client initialized.')
  121. async def _init_plugin(self, plugin: Plugin):
  122. await plugin.initialize(self.username)
  123. self.plugins[plugin.name] = plugin
  124. logger.debug(f'Initializing plugin: {plugin.name}')
  125. if isinstance(plugin, JupyterPlugin):
  126. await self.run_ipython(
  127. IPythonRunCellAction(
  128. code=f'import os; os.chdir("{self.bash_session.pwd}")'
  129. )
  130. )
  131. async def _init_bash_commands(self):
  132. logger.debug(f'Initializing by running {len(INIT_COMMANDS)} bash commands...')
  133. for command in INIT_COMMANDS:
  134. action = CmdRunAction(command=command)
  135. action.timeout = 300
  136. logger.debug(f'Executing init command: {command}')
  137. obs = await self.run(action)
  138. assert isinstance(obs, CmdOutputObservation)
  139. logger.debug(
  140. f'Init command outputs (exit code: {obs.exit_code}): {obs.content}'
  141. )
  142. assert obs.exit_code == 0
  143. logger.debug('Bash init commands completed')
  144. async def run_action(self, action) -> Observation:
  145. async with self.lock:
  146. action_type = action.action
  147. logger.debug(f'Running action:\n{action}')
  148. observation = await getattr(self, action_type)(action)
  149. logger.debug(f'Action output:\n{observation}')
  150. return observation
  151. async def run(
  152. self, action: CmdRunAction
  153. ) -> CmdOutputObservation | ErrorObservation:
  154. obs = await call_sync_from_async(self.bash_session.run, action)
  155. return obs
  156. async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
  157. if 'jupyter' in self.plugins:
  158. _jupyter_plugin: JupyterPlugin = self.plugins['jupyter'] # type: ignore
  159. # This is used to make AgentSkills in Jupyter aware of the
  160. # current working directory in Bash
  161. jupyter_pwd = getattr(self, '_jupyter_pwd', None)
  162. if self.bash_session.pwd != jupyter_pwd:
  163. logger.debug(
  164. f'{self.bash_session.pwd} != {jupyter_pwd} -> reset Jupyter PWD'
  165. )
  166. reset_jupyter_pwd_code = (
  167. f'import os; os.chdir("{self.bash_session.pwd}")'
  168. )
  169. _aux_action = IPythonRunCellAction(code=reset_jupyter_pwd_code)
  170. _reset_obs: IPythonRunCellObservation = await _jupyter_plugin.run(
  171. _aux_action
  172. )
  173. logger.debug(
  174. f'Changed working directory in IPython to: {self.bash_session.pwd}. Output: {_reset_obs}'
  175. )
  176. self._jupyter_pwd = self.bash_session.pwd
  177. obs: IPythonRunCellObservation = await _jupyter_plugin.run(action)
  178. obs.content = obs.content.rstrip()
  179. matches = re.findall(
  180. r'<oh_aci_output_[0-9a-f]{32}>(.*?)</oh_aci_output_[0-9a-f]{32}>',
  181. obs.content,
  182. re.DOTALL,
  183. )
  184. if matches:
  185. results: list[str] = []
  186. if len(matches) == 1:
  187. # Use specific actions/observations types
  188. match = matches[0]
  189. try:
  190. result_dict = json.loads(match)
  191. if result_dict.get('path'): # Successful output
  192. if (
  193. result_dict['new_content'] is not None
  194. ): # File edit commands
  195. diff = get_diff(
  196. old_contents=result_dict['old_content']
  197. or '', # old_content is None when file is created
  198. new_contents=result_dict['new_content'],
  199. filepath=result_dict['path'],
  200. )
  201. return FileEditObservation(
  202. content=diff,
  203. path=result_dict['path'],
  204. old_content=result_dict['old_content'],
  205. new_content=result_dict['new_content'],
  206. prev_exist=result_dict['prev_exist'],
  207. impl_source=FileEditSource.OH_ACI,
  208. formatted_output_and_error=result_dict[
  209. 'formatted_output_and_error'
  210. ],
  211. )
  212. else: # File view commands
  213. return FileReadObservation(
  214. content=result_dict['formatted_output_and_error'],
  215. path=result_dict['path'],
  216. impl_source=FileReadSource.OH_ACI,
  217. )
  218. else: # Error output
  219. results.append(result_dict['formatted_output_and_error'])
  220. except json.JSONDecodeError:
  221. # Handle JSON decoding errors if necessary
  222. results.append(
  223. f"Invalid JSON in 'openhands-aci' output: {match}"
  224. )
  225. else:
  226. for match in matches:
  227. try:
  228. result_dict = json.loads(match)
  229. results.append(result_dict['formatted_output_and_error'])
  230. except json.JSONDecodeError:
  231. # Handle JSON decoding errors if necessary
  232. results.append(
  233. f"Invalid JSON in 'openhands-aci' output: {match}"
  234. )
  235. # Combine the results (e.g., join them) or handle them as required
  236. obs.content = '\n'.join(str(result) for result in results)
  237. if action.include_extra:
  238. obs.content += (
  239. f'\n[Jupyter current working directory: {self.bash_session.pwd}]'
  240. )
  241. obs.content += f'\n[Jupyter Python interpreter: {_jupyter_plugin.python_interpreter_path}]'
  242. return obs
  243. else:
  244. raise RuntimeError(
  245. 'JupyterRequirement not found. Unable to run IPython action.'
  246. )
  247. def _resolve_path(self, path: str, working_dir: str) -> str:
  248. filepath = Path(path)
  249. if not filepath.is_absolute():
  250. return str(Path(working_dir) / filepath)
  251. return str(filepath)
  252. async def read(self, action: FileReadAction) -> Observation:
  253. if action.impl_source == FileReadSource.OH_ACI:
  254. return await self.run_ipython(
  255. IPythonRunCellAction(
  256. code=action.translated_ipython_code,
  257. include_extra=False,
  258. )
  259. )
  260. # NOTE: the client code is running inside the sandbox,
  261. # so there's no need to check permission
  262. working_dir = self.bash_session.workdir
  263. filepath = self._resolve_path(action.path, working_dir)
  264. try:
  265. if filepath.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
  266. with open(filepath, 'rb') as file:
  267. image_data = file.read()
  268. encoded_image = base64.b64encode(image_data).decode('utf-8')
  269. mime_type, _ = mimetypes.guess_type(filepath)
  270. if mime_type is None:
  271. mime_type = 'image/png' # default to PNG if mime type cannot be determined
  272. encoded_image = f'data:{mime_type};base64,{encoded_image}'
  273. return FileReadObservation(path=filepath, content=encoded_image)
  274. elif filepath.lower().endswith('.pdf'):
  275. with open(filepath, 'rb') as file:
  276. pdf_data = file.read()
  277. encoded_pdf = base64.b64encode(pdf_data).decode('utf-8')
  278. encoded_pdf = f'data:application/pdf;base64,{encoded_pdf}'
  279. return FileReadObservation(path=filepath, content=encoded_pdf)
  280. elif filepath.lower().endswith(('.mp4', '.webm', '.ogg')):
  281. with open(filepath, 'rb') as file:
  282. video_data = file.read()
  283. encoded_video = base64.b64encode(video_data).decode('utf-8')
  284. mime_type, _ = mimetypes.guess_type(filepath)
  285. if mime_type is None:
  286. mime_type = 'video/mp4' # default to MP4 if MIME type cannot be determined
  287. encoded_video = f'data:{mime_type};base64,{encoded_video}'
  288. return FileReadObservation(path=filepath, content=encoded_video)
  289. with open(filepath, 'r', encoding='utf-8') as file:
  290. lines = read_lines(file.readlines(), action.start, action.end)
  291. except FileNotFoundError:
  292. return ErrorObservation(
  293. f'File not found: {filepath}. Your current working directory is {working_dir}.'
  294. )
  295. except UnicodeDecodeError:
  296. return ErrorObservation(f'File could not be decoded as utf-8: {filepath}.')
  297. except IsADirectoryError:
  298. return ErrorObservation(
  299. f'Path is a directory: {filepath}. You can only read files'
  300. )
  301. code_view = ''.join(lines)
  302. return FileReadObservation(path=filepath, content=code_view)
  303. async def write(self, action: FileWriteAction) -> Observation:
  304. working_dir = self.bash_session.workdir
  305. filepath = self._resolve_path(action.path, working_dir)
  306. insert = action.content.split('\n')
  307. try:
  308. if not os.path.exists(os.path.dirname(filepath)):
  309. os.makedirs(os.path.dirname(filepath))
  310. file_exists = os.path.exists(filepath)
  311. if file_exists:
  312. file_stat = os.stat(filepath)
  313. else:
  314. file_stat = None
  315. mode = 'w' if not file_exists else 'r+'
  316. try:
  317. with open(filepath, mode, encoding='utf-8') as file:
  318. if mode != 'w':
  319. all_lines = file.readlines()
  320. new_file = insert_lines(
  321. insert, all_lines, action.start, action.end
  322. )
  323. else:
  324. new_file = [i + '\n' for i in insert]
  325. file.seek(0)
  326. file.writelines(new_file)
  327. file.truncate()
  328. # Handle file permissions
  329. if file_exists:
  330. assert file_stat is not None
  331. # restore the original file permissions if the file already exists
  332. os.chmod(filepath, file_stat.st_mode)
  333. os.chown(filepath, file_stat.st_uid, file_stat.st_gid)
  334. else:
  335. # set the new file permissions if the file is new
  336. os.chmod(filepath, 0o664)
  337. os.chown(filepath, self.user_id, self.user_id)
  338. except FileNotFoundError:
  339. return ErrorObservation(f'File not found: {filepath}')
  340. except IsADirectoryError:
  341. return ErrorObservation(
  342. f'Path is a directory: {filepath}. You can only write to files'
  343. )
  344. except UnicodeDecodeError:
  345. return ErrorObservation(
  346. f'File could not be decoded as utf-8: {filepath}'
  347. )
  348. except PermissionError:
  349. return ErrorObservation(f'Malformed paths not permitted: {filepath}')
  350. return FileWriteObservation(content='', path=filepath)
  351. async def browse(self, action: BrowseURLAction) -> Observation:
  352. return await browse(action, self.browser)
  353. async def browse_interactive(self, action: BrowseInteractiveAction) -> Observation:
  354. return await browse(action, self.browser)
  355. def close(self):
  356. self.bash_session.close()
  357. self.browser.close()
  358. if __name__ == '__main__':
  359. parser = argparse.ArgumentParser()
  360. parser.add_argument('port', type=int, help='Port to listen on')
  361. parser.add_argument('--working-dir', type=str, help='Working directory')
  362. parser.add_argument('--plugins', type=str, help='Plugins to initialize', nargs='+')
  363. parser.add_argument(
  364. '--username', type=str, help='User to run as', default='openhands'
  365. )
  366. parser.add_argument('--user-id', type=int, help='User ID to run as', default=1000)
  367. parser.add_argument(
  368. '--browsergym-eval-env',
  369. type=str,
  370. help='BrowserGym environment used for browser evaluation',
  371. default=None,
  372. )
  373. # example: python client.py 8000 --working-dir /workspace --plugins JupyterRequirement
  374. args = parser.parse_args()
  375. os.environ['VSCODE_PORT'] = str(int(args.port) + 1)
  376. assert check_port_available(int(os.environ['VSCODE_PORT']))
  377. plugins_to_load: list[Plugin] = []
  378. if args.plugins:
  379. for plugin in args.plugins:
  380. if plugin not in ALL_PLUGINS:
  381. raise ValueError(f'Plugin {plugin} not found')
  382. plugins_to_load.append(ALL_PLUGINS[plugin]()) # type: ignore
  383. client: ActionExecutor | None = None
  384. @asynccontextmanager
  385. async def lifespan(app: FastAPI):
  386. global client
  387. client = ActionExecutor(
  388. plugins_to_load,
  389. work_dir=args.working_dir,
  390. username=args.username,
  391. user_id=args.user_id,
  392. browsergym_eval_env=args.browsergym_eval_env,
  393. )
  394. await client.ainit()
  395. yield
  396. # Clean up & release the resources
  397. client.close()
  398. app = FastAPI(lifespan=lifespan)
  399. # TODO below 3 exception handlers were recommended by Sonnet.
  400. # Are these something we should keep?
  401. @app.exception_handler(Exception)
  402. async def global_exception_handler(request: Request, exc: Exception):
  403. logger.exception('Unhandled exception occurred:')
  404. return JSONResponse(
  405. status_code=500,
  406. content={'detail': 'An unexpected error occurred. Please try again later.'},
  407. )
  408. @app.exception_handler(StarletteHTTPException)
  409. async def http_exception_handler(request: Request, exc: StarletteHTTPException):
  410. logger.error(f'HTTP exception occurred: {exc.detail}')
  411. return JSONResponse(status_code=exc.status_code, content={'detail': exc.detail})
  412. @app.exception_handler(RequestValidationError)
  413. async def validation_exception_handler(
  414. request: Request, exc: RequestValidationError
  415. ):
  416. logger.error(f'Validation error occurred: {exc}')
  417. return JSONResponse(
  418. status_code=422,
  419. content={'detail': 'Invalid request parameters', 'errors': exc.errors()},
  420. )
  421. @app.middleware('http')
  422. async def authenticate_requests(request: Request, call_next):
  423. if request.url.path != '/alive' and request.url.path != '/server_info':
  424. try:
  425. verify_api_key(request.headers.get('X-Session-API-Key'))
  426. except HTTPException as e:
  427. return e
  428. response = await call_next(request)
  429. return response
  430. @app.get('/server_info')
  431. async def get_server_info():
  432. assert client is not None
  433. current_time = time.time()
  434. uptime = current_time - client.start_time
  435. idle_time = current_time - client.last_execution_time
  436. response = {
  437. 'uptime': uptime,
  438. 'idle_time': idle_time,
  439. 'resources': get_system_stats(),
  440. }
  441. logger.info('Server info endpoint response: %s', response)
  442. return response
  443. @app.post('/execute_action')
  444. async def execute_action(action_request: ActionRequest):
  445. assert client is not None
  446. try:
  447. action = event_from_dict(action_request.action)
  448. if not isinstance(action, Action):
  449. raise HTTPException(status_code=400, detail='Invalid action type')
  450. client.last_execution_time = time.time()
  451. observation = await client.run_action(action)
  452. return event_to_dict(observation)
  453. except Exception as e:
  454. logger.error(
  455. f'Error processing command: {str(e)}', exc_info=True, stack_info=True
  456. )
  457. raise HTTPException(
  458. status_code=500,
  459. detail=traceback.format_exc(),
  460. )
  461. @app.post('/upload_file')
  462. async def upload_file(
  463. file: UploadFile, destination: str = '/', recursive: bool = False
  464. ):
  465. assert client is not None
  466. try:
  467. # Ensure the destination directory exists
  468. if not os.path.isabs(destination):
  469. raise HTTPException(
  470. status_code=400, detail='Destination must be an absolute path'
  471. )
  472. full_dest_path = destination
  473. if not os.path.exists(full_dest_path):
  474. os.makedirs(full_dest_path, exist_ok=True)
  475. if recursive or file.filename.endswith('.zip'):
  476. # For recursive uploads, we expect a zip file
  477. if not file.filename.endswith('.zip'):
  478. raise HTTPException(
  479. status_code=400, detail='Recursive uploads must be zip files'
  480. )
  481. zip_path = os.path.join(full_dest_path, file.filename)
  482. with open(zip_path, 'wb') as buffer:
  483. shutil.copyfileobj(file.file, buffer)
  484. # Extract the zip file
  485. shutil.unpack_archive(zip_path, full_dest_path)
  486. os.remove(zip_path) # Remove the zip file after extraction
  487. logger.debug(
  488. f'Uploaded file {file.filename} and extracted to {destination}'
  489. )
  490. else:
  491. # For single file uploads
  492. file_path = os.path.join(full_dest_path, file.filename)
  493. with open(file_path, 'wb') as buffer:
  494. shutil.copyfileobj(file.file, buffer)
  495. logger.debug(f'Uploaded file {file.filename} to {destination}')
  496. return JSONResponse(
  497. content={
  498. 'filename': file.filename,
  499. 'destination': destination,
  500. 'recursive': recursive,
  501. },
  502. status_code=200,
  503. )
  504. except Exception as e:
  505. raise HTTPException(status_code=500, detail=str(e))
  506. @app.get('/download_files')
  507. async def download_file(path: str):
  508. logger.debug('Downloading files')
  509. try:
  510. if not os.path.isabs(path):
  511. raise HTTPException(
  512. status_code=400, detail='Path must be an absolute path'
  513. )
  514. if not os.path.exists(path):
  515. raise HTTPException(status_code=404, detail='File not found')
  516. with tempfile.TemporaryFile() as temp_zip:
  517. with ZipFile(temp_zip, 'w') as zipf:
  518. for root, _, files in os.walk(path):
  519. for file in files:
  520. file_path = os.path.join(root, file)
  521. zipf.write(
  522. file_path, arcname=os.path.relpath(file_path, path)
  523. )
  524. temp_zip.seek(0) # Rewind the file to the beginning after writing
  525. content = temp_zip.read()
  526. # Good for small to medium-sized files. For very large files, streaming directly from the
  527. # file chunks may be more memory-efficient.
  528. zip_stream = io.BytesIO(content)
  529. return StreamingResponse(
  530. content=zip_stream,
  531. media_type='application/zip',
  532. headers={'Content-Disposition': f'attachment; filename={path}.zip'},
  533. )
  534. except Exception as e:
  535. raise HTTPException(status_code=500, detail=str(e))
  536. @app.get('/alive')
  537. async def alive():
  538. return {'status': 'ok'}
  539. # ================================
  540. # VSCode-specific operations
  541. # ================================
  542. @app.get('/vscode/connection_token')
  543. async def get_vscode_connection_token():
  544. assert client is not None
  545. if 'vscode' in client.plugins:
  546. plugin: VSCodePlugin = client.plugins['vscode'] # type: ignore
  547. return {'token': plugin.vscode_connection_token}
  548. else:
  549. return {'token': None}
  550. # ================================
  551. # File-specific operations for UI
  552. # ================================
  553. @app.post('/list_files')
  554. async def list_files(request: Request):
  555. """List files in the specified path.
  556. This function retrieves a list of files from the agent's runtime file store,
  557. excluding certain system and hidden files/directories.
  558. To list files:
  559. ```sh
  560. curl http://localhost:3000/api/list-files
  561. ```
  562. Args:
  563. request (Request): The incoming request object.
  564. path (str, optional): The path to list files from. Defaults to '/'.
  565. Returns:
  566. list: A list of file names in the specified path.
  567. Raises:
  568. HTTPException: If there's an error listing the files.
  569. """
  570. assert client is not None
  571. # get request as dict
  572. request_dict = await request.json()
  573. path = request_dict.get('path', None)
  574. # Get the full path of the requested directory
  575. if path is None:
  576. full_path = client.initial_pwd
  577. elif os.path.isabs(path):
  578. full_path = path
  579. else:
  580. full_path = os.path.join(client.initial_pwd, path)
  581. if not os.path.exists(full_path):
  582. # if user just removed a folder, prevent server error 500 in UI
  583. return []
  584. try:
  585. # Check if the directory exists
  586. if not os.path.exists(full_path) or not os.path.isdir(full_path):
  587. return []
  588. entries = os.listdir(full_path)
  589. # Separate directories and files
  590. directories = []
  591. files = []
  592. for entry in entries:
  593. # Remove leading slash and any parent directory components
  594. entry_relative = entry.lstrip('/').split('/')[-1]
  595. # Construct the full path by joining the base path with the relative entry path
  596. full_entry_path = os.path.join(full_path, entry_relative)
  597. if os.path.exists(full_entry_path):
  598. is_dir = os.path.isdir(full_entry_path)
  599. if is_dir:
  600. # add trailing slash to directories
  601. # required by FE to differentiate directories and files
  602. entry = entry.rstrip('/') + '/'
  603. directories.append(entry)
  604. else:
  605. files.append(entry)
  606. # Sort directories and files separately
  607. directories.sort(key=lambda s: s.lower())
  608. files.sort(key=lambda s: s.lower())
  609. # Combine sorted directories and files
  610. sorted_entries = directories + files
  611. return sorted_entries
  612. except Exception as e:
  613. logger.error(f'Error listing files: {e}', exc_info=True)
  614. return []
  615. logger.debug(f'Starting action execution API on port {args.port}')
  616. run(app, host='0.0.0.0', port=args.port)