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 agent groups endpoint (#5089)

cthomas6e3e45fb2d25
diff --git a/fern/openapi.json b/fern/openapi.json
index 922aa8f63..d2bca489d 100644
--- a/fern/openapi.json
+++ b/fern/openapi.json
@@ -6318,6 +6318,87 @@
"title": "Manager Type"
},
"description": "Manager type to filter groups by"
+ },
+ {
+ "name": "before",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Group ID cursor for pagination. Returns groups that come before this group ID in the specified sort order",
+ "title": "Before"
+ },
+ "description": "Group ID cursor for pagination. Returns groups that come before this group ID in the specified sort order"
+ },
+ {
+ "name": "after",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Group ID cursor for pagination. Returns groups that come after this group ID in the specified sort order",
+ "title": "After"
+ },
+ "description": "Group ID cursor for pagination. Returns groups that come after this group ID in the specified sort order"
+ },
+ {
+ "name": "limit",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [
+ {
+ "type": "integer"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Maximum number of groups to return",
+ "default": 100,
+ "title": "Limit"
+ },
+ "description": "Maximum number of groups to return"
+ },
+ {
+ "name": "order",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "enum": ["asc", "desc"],
+ "type": "string",
+ "description": "Sort order for groups by creation time. 'asc' for oldest first, 'desc' for newest first",
+ "default": "desc",
+ "title": "Order"
+ },
+ "description": "Sort order for groups 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.py
index 72510370a..da62df82a 100644
--- a/letta/server/rest_api/routers/v1/agents.py
+++ b/letta/server/rest_api/routers/v1/agents.py
@@ -1776,11 +1776,30 @@ async def list_agent_groups(
manager_type: str | None = Query(None, description="Manager type to filter groups by"),
server: "SyncServer" = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
+ before: Optional[str] = Query(
+ None, description="Group ID cursor for pagination. Returns groups that come before this group ID in the specified sort order"
+ ),
+ after: Optional[str] = Query(
+ None, description="Group ID cursor for pagination. Returns groups that come after this group ID in the specified sort order"
+ ),
+ limit: Optional[int] = Query(100, description="Maximum number of groups to return"),
+ order: Literal["asc", "desc"] = Query(
+ "desc", description="Sort order for groups by creation time. 'asc' for oldest first, 'desc' for newest first"
+ ),
+ order_by: Literal["created_at"] = Query("created_at", description="Field to sort by"),
):
"""Lists the groups for an agent"""
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
logger.info("in list agents with manager_type", manager_type)
- return await server.agent_manager.list_groups_async(agent_id=agent_id, manager_type=manager_type, actor=actor)
+ return await server.agent_manager.list_groups_async(
+ agent_id=agent_id,
+ manager_type=manager_type,
+ actor=actor,
+ before=before,
+ after=after,
+ limit=limit,
+ ascending=(order == "asc"),
+ )
@router.post(
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py
index 8a54ed0d9..c69197eff 100644
--- a/letta/services/agent_manager.py
+++ b/letta/services/agent_manager.py
@@ -2641,14 +2641,22 @@ class AgentManager:
agent_id: str,
actor: PydanticUser,
manager_type: Optional[str] = None,
+ before: Optional[str] = None,
+ after: Optional[str] = None,
+ limit: Optional[int] = None,
+ ascending: bool = False,
) -> List[PydanticGroup]:
"""
- List all groups that contain the specified agent (async version).
+ List all groups that contain the specified agent.
Args:
agent_id: ID of the agent to find groups for.
actor: User performing the action.
manager_type: Optional manager type to filter by.
+ before: Group ID cursor for pagination. Returns groups that come before this group ID.
+ after: Group ID cursor for pagination. Returns groups that come after this group ID.
+ limit: Maximum number of groups to return.
+ ascending: Sort order by creation time.
Returns:
List[PydanticGroup]: List of groups containing the agent.
@@ -2666,6 +2674,22 @@ class AgentManager:
if manager_type:
query = query.where(GroupModel.manager_type == manager_type)
+ # Apply cursor-based pagination
+ if before:
+ query = query.where(GroupModel.id < before)
+ if after:
+ query = query.where(GroupModel.id > after)
+
+ # Apply sorting
+ if ascending:
+ query = query.order_by(GroupModel.created_at.asc())
+ else:
+ query = query.order_by(GroupModel.created_at.desc())
+
+ # Apply limit
+ if limit:
+ query = query.limit(limit)
+
result = await session.execute(query)
groups = result.scalars().all()
return [group.to_pydantic() for group in groups]