Move file-related endpoints to api_files.py for better modularity

This commit is contained in:
openhands 2024-11-09 16:37:51 +00:00
parent 004d10b116
commit f5f2846ef5
2 changed files with 51 additions and 9 deletions

View File

@ -0,0 +1,49 @@
from fastapi import APIRouter, Request, HTTPException, status
from fastapi.responses import JSONResponse
from openhands.runtime.base import Runtime
from openhands.utils.async_utils import call_sync_from_async
import os
router = APIRouter()
FILES_TO_IGNORE = [
'.git/',
'.DS_Store',
'node_modules/',
'__pycache__/',
]
@router.get('/list-files')
async def list_files(request: Request, path: str | None = None):
"""List files in the specified path.
This function retrieves a list of files from the agent's runtime file store,
excluding certain system and hidden files/directories.
To list files:
```sh
curl http://localhost:3000/api/list-files
```
Args:
request (Request): The incoming request object.
path (str, optional): The path to list files from. Defaults to None.
Returns:
list: A list of file names in the specified path.
Raises:
HTTPException: If there's an error listing the files.
"""
if not request.state.conversation.runtime:
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={'error': 'Runtime not yet initialized'},
)
runtime: Runtime = request.state.conversation.runtime
file_list = await call_sync_from_async(runtime.list_files, path)
if path:
file_list = [os.path.join(path, f) for f in file_list]
return [f for f in file_list if not any(ignored in f for ignored in FILES_TO_IGNORE)]

View File

@ -4,6 +4,7 @@ from openhands.server.config import config, session_manager
from openhands.server.middleware import LocalhostCORSMiddleware, NoCacheMiddleware, attach_session
from openhands.server.websocket import websocket_endpoint
from openhands.server.api_options import router as api_options_router
from openhands.server.api_files import router as api_files_router
app = FastAPI()
app.add_middleware(
@ -16,18 +17,10 @@ app.add_middleware(NoCacheMiddleware)
app.middleware('http')(attach_session)
app.websocket('/ws')(websocket_endpoint)
app.include_router(api_options_router, prefix='/api/options')
app.include_router(api_files_router, prefix='/api')
security_scheme = HTTPBearer()
FILES_TO_IGNORE = [
'.git/',
'.DS_Store',
'node_modules/',
'__pycache__/',
]
@app.get('/api/list-files')
async def list_files(request: Request, path: str | None = None):
"""List files in the specified path.