Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: Add document block clean up triggers (#2552)
Matthew Zhou91cd3211cab0
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex debef6919..641b3855a 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -346,15 +346,19 @@ async def detach_source(Detach a source from an agent."""actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)- agent = await server.agent_manager.detach_source_async(agent_id=agent_id, source_id=source_id, actor=actor)- if agent.enable_sleeptime:+ agent_state = await server.agent_manager.detach_source_async(agent_id=agent_id, source_id=source_id, actor=actor)+ files = await server.source_manager.list_files(source_id, actor)+ filenames = [f.file_name for f in files]+ await server.remove_documents_from_context_window(agent_state=agent_state, filenames=filenames, actor=actor)++ if agent_state.enable_sleeptime:try:source = await server.source_manager.get_source_by_id(source_id=source_id)- block = await server.agent_manager.get_block_with_label_async(agent_id=agent.id, block_label=source.name, actor=actor)+ block = await server.agent_manager.get_block_with_label_async(agent_id=agent_state.id, block_label=source.name, actor=actor)await server.block_manager.delete_block_async(block.id, actor)except:pass- return agent+ return agent_state@router.get("/{agent_id}", response_model=AgentState, operation_id="retrieve_agent")diff --git a/letta/server/rest_api/routers/v1/sources.py b/letta/server/rest_api/routers/v1/sources.pyindex 9f90388df..c137e6150 100644--- a/letta/server/rest_api/routers/v1/sources.py+++ b/letta/server/rest_api/routers/v1/sources.py@@ -3,7 +3,7 @@ import osimport tempfilefrom typing import List, Optional-from fastapi import APIRouter, BackgroundTasks, Depends, Header, HTTPException, Query, UploadFile+from fastapi import APIRouter, Depends, Header, HTTPException, Query, UploadFilefrom starlette import statusimport letta.constants as constants@@ -140,11 +140,16 @@ async def delete_source("""actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)source = await server.source_manager.get_source_by_id(source_id=source_id, actor=actor)- agents = await server.source_manager.list_attached_agents(source_id=source_id, actor=actor)- for agent in agents:- if agent.enable_sleeptime:+ agent_states = await server.source_manager.list_attached_agents(source_id=source_id, actor=actor)+ files = await server.source_manager.list_files(source_id, actor)+ filenames = [f.file_name for f in files]++ for agent_state in agent_states:+ await server.remove_documents_from_context_window(agent_state=agent_state, filenames=filenames, actor=actor)++ if agent_state.enable_sleeptime:try:- block = await server.agent_manager.get_block_with_label_async(agent_id=agent.id, block_label=source.name, actor=actor)+ block = await server.agent_manager.get_block_with_label_async(agent_id=agent_state.id, block_label=source.name, actor=actor)await server.block_manager.delete_block_async(block.id, actor)except:pass@@ -245,7 +250,6 @@ async def list_source_files(async def delete_file_from_source(source_id: str,file_id: str,- background_tasks: BackgroundTasks,server: "SyncServer" = Depends(get_letta_server),actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present):@@ -256,7 +260,9 @@ async def delete_file_from_source(deleted_file = await server.source_manager.delete_file(file_id=file_id, actor=actor)- # TODO: make async+ # Remove blocks+ await server.remove_document_from_context_windows(source_id=source_id, filename=deleted_file.file_name, actor=actor)+asyncio.create_task(sleeptime_document_ingest_async(server, source_id, actor, clear_history=True))if deleted_file is None:raise HTTPException(status_code=404, detail=f"File with id={file_id} not found.")diff --git a/letta/server/server.py b/letta/server/server.pyindex 520d2e8f6..ce88e7e86 100644--- a/letta/server/server.py+++ b/letta/server/server.py@@ -1366,7 +1366,6 @@ class SyncServer(Server):async def _upsert_document_block(self, agent_id: str, text: str, filename: str, actor: User) -> None:"""Internal method to create or update a document block for an agent.- This is the shared logic between single and multiple document insertion."""truncated_text = text[:CORE_MEMORY_SOURCE_CHAR_LIMIT]@@ -1397,6 +1396,20 @@ class SyncServer(Server):actor=actor,)+ async def _remove_document_block(self, agent_id: str, filename: str, actor: User) -> None:+ """+ Internal method to remove a document block for an agent.+ """+ try:+ block = await self.agent_manager.get_block_with_label_async(+ agent_id=agent_id,+ block_label=filename,+ actor=actor,+ )+ await self.block_manager.delete_block_async(block_id=block.id, actor=actor)+ except NoResultFound:+ logger.info(f"Document block with label {filename} already removed, skipping...")+async def insert_document_into_context_windows(self, source_id: str, text: str, filename: str, actor: User) -> None:"""Insert the uploaded document into the context window of all agents@@ -1429,6 +1442,32 @@ class SyncServer(Server):*(self._upsert_document_block(agent_state.id, text, filename, actor) for text, filename in zip(texts, filenames)))+ async def remove_document_from_context_windows(self, source_id: str, filename: str, actor: User) -> None:+ """+ Remove the document from the context window of all agents+ attached to the given source.+ """+ agent_states = await self.source_manager.list_attached_agents(source_id=source_id, actor=actor)++ # Return early+ if not agent_states:+ return++ logger.info(f"Removing document from context window for source: {source_id}")+ logger.info(f"Attached agents: {[a.id for a in agent_states]}")++ await asyncio.gather(*(self._remove_document_block(agent_state.id, filename, actor) for agent_state in agent_states))++ async def remove_documents_from_context_window(self, agent_state: AgentState, filenames: List[str], actor: User) -> None:+ """+ Remove multiple documents from the context window of an agent+ attached to the given source.+ """+ logger.info(f"Removing documents from context window for agent_state: {agent_state.id}")+ logger.info(f"Documents to remove: {filenames}")++ await asyncio.gather(*(self._remove_document_block(agent_state.id, filename, actor) for filename in filenames))+async def create_document_sleeptime_agent_async(self, main_agent: AgentState, source: Source, actor: User, clear_history: bool = False) -> AgentState:diff --git a/tests/test_sources.py b/tests/test_sources.pyindex 4e9d4e962..0aac53ea7 100644--- a/tests/test_sources.py+++ b/tests/test_sources.py@@ -96,6 +96,15 @@ def test_file_upload_creates_source_blocks_correctly(client: LettaSDKClient, ageassert "test" in [b.value for b in blocks]assert any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)+ # Remove file from source+ client.sources.files.delete(source_id=source.id, file_id=files[0].id)++ # Get the agent state, check blocks do NOT exist+ blocks = client.agents.blocks.list(agent_id=agent_state.id)+ assert len(blocks) == 1+ assert "test" not in [b.value for b in blocks]+ assert not any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)+def test_attach_existing_files_creates_source_blocks_correctly(client: LettaSDKClient, agent_state: AgentState):# Clear existing sources@@ -136,3 +145,57 @@ def test_attach_existing_files_creates_source_blocks_correctly(client: LettaSDKCassert len(blocks) == 2assert "test" in [b.value for b in blocks]assert any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)++ # Detach the source+ client.agents.sources.detach(source_id=source.id, agent_id=agent_state.id)++ # Get the agent state, check blocks do NOT exist+ blocks = client.agents.blocks.list(agent_id=agent_state.id)+ assert len(blocks) == 1+ assert "test" not in [b.value for b in blocks]+ assert not any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)+++def test_delete_source_removes_source_blocks_correctly(client: LettaSDKClient, agent_state: AgentState):+ # Clear existing sources+ for source in client.sources.list():+ client.sources.delete(source_id=source.id)++ # Clear existing jobs+ for job in client.jobs.list():+ client.jobs.delete(job_id=job.id)++ # Create a new source+ source = client.sources.create(name="test_source", embedding="openai/text-embedding-ada-002")+ assert len(client.sources.list()) == 1++ # Attach+ client.agents.sources.attach(source_id=source.id, agent_id=agent_state.id)++ # Load files into the source+ file_path = "tests/data/test.txt"++ # Upload the files+ with open(file_path, "rb") as f:+ job = client.sources.files.upload(source_id=source.id, file=f)++ # Wait for the jobs to complete+ while job.status != "completed":+ time.sleep(1)+ job = client.jobs.retrieve(job_id=job.id)+ print("Waiting for jobs to complete...", job.status)++ # Get the agent state, check blocks exist+ blocks = client.agents.blocks.list(agent_id=agent_state.id)+ assert len(blocks) == 2+ assert "test" in [b.value for b in blocks]+ assert any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)++ # Remove file from source+ client.sources.delete(source_id=source.id)++ # Get the agent state, check blocks do NOT exist+ blocks = client.agents.blocks.list(agent_id=agent_state.id)+ assert len(blocks) == 1+ assert "test" not in [b.value for b in blocks]+ assert not any(re.fullmatch(r"test_[a-z0-9]+\.txt", b.label) for b in blocks)