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 list archives endpoint (#2965)

Matthew Zhou57dde4187f67
diff --git a/letta/server/rest_api/routers/v1/archives.py b/letta/server/rest_api/routers/v1/archives.py
index 45ebc9a26..45f883114 100644
--- a/letta/server/rest_api/routers/v1/archives.py
+++ b/letta/server/rest_api/routers/v1/archives.py
@@ -1,6 +1,6 @@
-from typing import Optional
+from typing import List, Literal, Optional
-from fastapi import APIRouter, Body, Depends, HTTPException
+from fastapi import APIRouter, Body, Depends, HTTPException, Query
from pydantic import BaseModel
from letta.orm.errors import NoResultFound
@@ -51,6 +51,44 @@ async def create_archive(
raise HTTPException(status_code=500, detail=str(e))
[email protected]("/", response_model=List[PydanticArchive], operation_id="list_archives")
+async def list_archives(
+ before: Optional[str] = Query(
+ None,
+ description="Archive ID cursor for pagination. Returns archives that come before this archive ID in the specified sort order",
+ ),
+ after: Optional[str] = Query(
+ None,
+ description="Archive ID cursor for pagination. Returns archives that come after this archive ID in the specified sort order",
+ ),
+ limit: Optional[int] = Query(50, description="Maximum number of archives to return"),
+ order: Literal["asc", "desc"] = Query(
+ "desc", description="Sort order for archives by creation time. 'asc' for oldest first, 'desc' for newest first"
+ ),
+ name: Optional[str] = Query(None, description="Filter by archive name (exact match)"),
+ agent_id: Optional[str] = Query(None, description="Only archives attached to this agent ID"),
+ server: "SyncServer" = Depends(get_letta_server),
+ headers: HeaderParams = Depends(get_headers),
+):
+ """
+ Get a list of all archives for the current organization with optional filters and pagination.
+ """
+ try:
+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
+ archives = await server.archive_manager.list_archives_async(
+ actor=actor,
+ before=before,
+ after=after,
+ limit=limit,
+ ascending=(order == "asc"),
+ name=name,
+ agent_id=agent_id,
+ )
+ return archives
+ except Exception as e:
+ raise HTTPException(status_code=500, detail=str(e))
+
+
@router.patch("/{archive_id}", response_model=PydanticArchive, operation_id="modify_archive")
async def modify_archive(
archive_id: str,
diff --git a/letta/services/archive_manager.py b/letta/services/archive_manager.py
index d5830a77a..0e54e7cdd 100644
--- a/letta/services/archive_manager.py
+++ b/letta/services/archive_manager.py
@@ -113,6 +113,53 @@ class ArchiveManager:
await archive.update_async(session, actor=actor)
return archive.to_pydantic()
+ @enforce_types
+ @trace_method
+ async def list_archives_async(
+ self,
+ *,
+ actor: PydanticUser,
+ before: Optional[str] = None,
+ after: Optional[str] = None,
+ limit: Optional[int] = 50,
+ ascending: bool = False,
+ name: Optional[str] = None,
+ agent_id: Optional[str] = None,
+ ) -> List[PydanticArchive]:
+ """List archives with pagination and optional filters.
+
+ Filters:
+ - name: exact match on name
+ - agent_id: only archives attached to given agent
+ """
+ filter_kwargs = {}
+ if name is not None:
+ filter_kwargs["name"] = name
+
+ join_model = None
+ join_conditions = None
+ if agent_id is not None:
+ join_model = ArchivesAgents
+ join_conditions = [
+ ArchivesAgents.archive_id == ArchiveModel.id,
+ ArchivesAgents.agent_id == agent_id,
+ ]
+
+ async with db_registry.async_session() as session:
+ archives = await ArchiveModel.list_async(
+ db_session=session,
+ before=before,
+ after=after,
+ limit=limit,
+ ascending=ascending,
+ actor=actor,
+ check_is_deleted=True,
+ join_model=join_model,
+ join_conditions=join_conditions,
+ **filter_kwargs,
+ )
+ return [a.to_pydantic() for a in archives]
+
@enforce_types
@trace_method
def attach_agent_to_archive(