Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: add pagination to folders files endpoint (#2946)

cthomase44a3bfcc157
diff --git a/letta/server/rest_api/routers/v1/folders.py b/letta/server/rest_api/routers/v1/folders.py
index 56375c4ef..b6d9f28ae 100644
--- a/letta/server/rest_api/routers/v1/folders.py
+++ b/letta/server/rest_api/routers/v1/folders.py
@@ -397,8 +397,19 @@ async def list_folder_passages(
@router.get("/{folder_id}/files", response_model=List[FileMetadata], operation_id="list_folder_files")
async def list_folder_files(
folder_id: str,
- limit: int = Query(1000, description="Number of files to return"),
- after: Optional[str] = Query(None, description="Pagination cursor to fetch the next set of results"),
+ before: Optional[str] = Query(
+ None,
+ description="File ID cursor for pagination. Returns files that come before this file ID in the specified sort order",
+ ),
+ after: Optional[str] = Query(
+ None,
+ description="File ID cursor for pagination. Returns files that come after this file ID in the specified sort order",
+ ),
+ limit: Optional[int] = Query(1000, description="Maximum number of files to return"),
+ order: Literal["asc", "desc"] = Query(
+ "desc", description="Sort order for files by creation time. 'asc' for oldest first, 'desc' for newest first"
+ ),
+ order_by: Literal["created_at"] = Query("created_at", description="Field to sort by"),
include_content: bool = Query(False, description="Whether to include full file content"),
server: "SyncServer" = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
@@ -409,8 +420,10 @@ async def list_folder_files(
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
return await server.file_manager.list_files(
source_id=folder_id,
- limit=limit,
+ before=before,
after=after,
+ limit=limit,
+ ascending=(order == "asc"),
actor=actor,
include_content=include_content,
strip_directory_prefix=True, # TODO: Reconsider this. This is purely for aesthetics.
diff --git a/letta/services/file_manager.py b/letta/services/file_manager.py
index 3cac0400f..75ae0466c 100644
--- a/letta/services/file_manager.py
+++ b/letta/services/file_manager.py
@@ -404,8 +404,10 @@ class FileManager:
self,
source_id: str,
actor: PydanticUser,
+ before: Optional[str] = None,
after: Optional[str] = None,
limit: Optional[int] = 50,
+ ascending: Optional[bool] = True,
include_content: bool = False,
strip_directory_prefix: bool = False,
check_status_updates: bool = False,
@@ -415,8 +417,10 @@ class FileManager:
Args:
source_id: Source to list files from
actor: User performing the request
+ before: Before filter
after: Pagination cursor
limit: Maximum number of files to return
+ ascending: Sort by ascending or descending order
include_content: Whether to include file content
strip_directory_prefix: Whether to strip directory prefix from filenames
check_status_updates: Whether to check and update status for timeout and embedding completion
@@ -429,8 +433,10 @@ class FileManager:
files = await FileMetadataModel.list_async(
db_session=session,
+ before=before,
after=after,
limit=limit,
+ ascending=ascending,
organization_id=actor.organization_id,
source_id=source_id,
query_options=options,