Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add pagination to agent folders endpoint (#5085)
cthomas416af626cc36
diff --git a/fern/openapi.json b/fern/openapi.jsonindex 4dcca95af..9d8de4614 100644--- a/fern/openapi.json+++ b/fern/openapi.json@@ -4708,6 +4708,87 @@"type": "string","title": "Agent Id"}+ },+ {+ "name": "before",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order",+ "title": "Before"+ },+ "description": "Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order"+ },+ {+ "name": "after",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order",+ "title": "After"+ },+ "description": "Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order"+ },+ {+ "name": "limit",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "integer"+ },+ {+ "type": "null"+ }+ ],+ "description": "Maximum number of sources to return",+ "default": 100,+ "title": "Limit"+ },+ "description": "Maximum number of sources to return"+ },+ {+ "name": "order",+ "in": "query",+ "required": false,+ "schema": {+ "enum": ["asc", "desc"],+ "type": "string",+ "description": "Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first",+ "default": "desc",+ "title": "Order"+ },+ "description": "Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first"+ },+ {+ "name": "order_by",+ "in": "query",+ "required": false,+ "schema": {+ "const": "created_at",+ "type": "string",+ "description": "Field to sort by",+ "default": "created_at",+ "title": "Order By"+ },+ "description": "Field to sort by"}],"responses": {@@ -4753,6 +4834,87 @@"type": "string","title": "Agent Id"}+ },+ {+ "name": "before",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order",+ "title": "Before"+ },+ "description": "Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order"+ },+ {+ "name": "after",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "description": "Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order",+ "title": "After"+ },+ "description": "Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order"+ },+ {+ "name": "limit",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ {+ "type": "integer"+ },+ {+ "type": "null"+ }+ ],+ "description": "Maximum number of sources to return",+ "default": 100,+ "title": "Limit"+ },+ "description": "Maximum number of sources to return"+ },+ {+ "name": "order",+ "in": "query",+ "required": false,+ "schema": {+ "enum": ["asc", "desc"],+ "type": "string",+ "description": "Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first",+ "default": "desc",+ "title": "Order"+ },+ "description": "Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first"+ },+ {+ "name": "order_by",+ "in": "query",+ "required": false,+ "schema": {+ "const": "created_at",+ "type": "string",+ "description": "Field to sort by",+ "default": "created_at",+ "title": "Order By"+ },+ "description": "Field to sort by"}],"responses": {diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 37fea8ad7..d86911199 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -734,12 +734,30 @@ async def list_agent_sources(agent_id: str,server: "SyncServer" = Depends(get_letta_server),headers: HeaderParams = Depends(get_headers),+ before: Optional[str] = Query(+ None, description="Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order"+ ),+ after: Optional[str] = Query(+ None, description="Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order"+ ),+ limit: Optional[int] = Query(100, description="Maximum number of sources to return"),+ order: Literal["asc", "desc"] = Query(+ "desc", description="Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first"+ ),+ order_by: Literal["created_at"] = Query("created_at", description="Field to sort by"),):"""Get the sources associated with an agent."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- return await server.agent_manager.list_attached_sources_async(agent_id=agent_id, actor=actor)+ return await server.agent_manager.list_attached_sources_async(+ agent_id=agent_id,+ actor=actor,+ before=before,+ after=after,+ limit=limit,+ ascending=(order == "asc"),+ )@router.get("/{agent_id}/folders", response_model=list[Source], operation_id="list_agent_folders")@@ -747,12 +765,30 @@ async def list_agent_folders(agent_id: str,server: "SyncServer" = Depends(get_letta_server),headers: HeaderParams = Depends(get_headers),+ before: Optional[str] = Query(+ None, description="Source ID cursor for pagination. Returns sources that come before this source ID in the specified sort order"+ ),+ after: Optional[str] = Query(+ None, description="Source ID cursor for pagination. Returns sources that come after this source ID in the specified sort order"+ ),+ limit: Optional[int] = Query(100, description="Maximum number of sources to return"),+ order: Literal["asc", "desc"] = Query(+ "desc", description="Sort order for sources by creation time. 'asc' for oldest first, 'desc' for newest first"+ ),+ order_by: Literal["created_at"] = Query("created_at", description="Field to sort by"),):"""Get the folders associated with an agent."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- return await server.agent_manager.list_attached_sources_async(agent_id=agent_id, actor=actor)+ return await server.agent_manager.list_attached_sources_async(+ agent_id=agent_id,+ actor=actor,+ before=before,+ after=after,+ limit=limit,+ ascending=(order == "asc"),+ )@router.get("/{agent_id}/files", response_model=PaginatedAgentFiles, operation_id="list_agent_files")diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 5091b12dd..202403382 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -1549,16 +1549,28 @@ class AgentManager:@enforce_types@trace_method- async def list_attached_sources_async(self, agent_id: str, actor: PydanticUser) -> List[PydanticSource]:+ async def list_attached_sources_async(+ self,+ agent_id: str,+ actor: PydanticUser,+ before: Optional[str] = None,+ after: Optional[str] = None,+ limit: Optional[int] = None,+ ascending: bool = False,+ ) -> List[PydanticSource]:"""- Lists all sources attached to an agent.+ Lists all sources attached to an agent with pagination.Args:agent_id: ID of the agent to list sources foractor: User performing the action+ before: Source ID cursor for pagination. Returns sources that come before this source ID.+ after: Source ID cursor for pagination. Returns sources that come after this source ID.+ limit: Maximum number of sources to return.+ ascending: Sort order by creation time.Returns:- List[str]: List of source IDs attached to the agent+ List[PydanticSource]: List of sources attached to the agentRaises:NoResultFound: If agent doesn't exist or user doesn't have access@@ -1577,9 +1589,24 @@ class AgentManager:SourceModel.organization_id == actor.organization_id,SourceModel.is_deleted == False,)- .order_by(SourceModel.created_at.desc(), SourceModel.id))+ # Apply cursor-based pagination+ if before:+ query = query.where(SourceModel.id < before)+ if after:+ query = query.where(SourceModel.id > after)++ # Apply sorting+ if ascending:+ query = query.order_by(SourceModel.created_at.asc(), SourceModel.id.asc())+ else:+ query = query.order_by(SourceModel.created_at.desc(), SourceModel.id.desc())++ # Apply limit+ if limit:+ query = query.limit(limit)+result = await session.execute(query)sources = result.scalars().all()