Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: Add file status to core memory jinja template (#2604)
Matthew Zhou2d9cbf38305d
diff --git a/letta/orm/files_agents.py b/letta/orm/files_agents.pyindex ac9d9e340..19b25bca1 100644--- a/letta/orm/files_agents.py+++ b/letta/orm/files_agents.py@@ -58,13 +58,11 @@ class FileAgent(SqlalchemyBase, OrganizationMixin):)# TODO: This is temporary as we figure out if we want FileBlock as a first class citizen- def to_pydantic_block(self) -> Optional[PydanticBlock]:- if self.is_open:- return PydanticBlock(- organization_id=self.organization_id,- value=self.visible_content if self.visible_content else "",- label=self.file.file_name,- read_only=True,- )- else:- return None+ def to_pydantic_block(self) -> PydanticBlock:+ visible_content = self.visible_content if self.visible_content and self.is_open else ""+ return PydanticBlock(+ organization_id=self.organization_id,+ value=visible_content,+ label=self.file.file_name,+ read_only=True,+ )diff --git a/letta/schemas/agent.py b/letta/schemas/agent.pyindex b94fd93aa..32e9cf331 100644--- a/letta/schemas/agent.py+++ b/letta/schemas/agent.py@@ -8,6 +8,7 @@ from letta.helpers import ToolRulesSolverfrom letta.schemas.block import CreateBlockfrom letta.schemas.embedding_config import EmbeddingConfigfrom letta.schemas.environment_variables import AgentEnvironmentVariable+from letta.schemas.file import FileStatusfrom letta.schemas.group import Groupfrom letta.schemas.letta_base import OrmMetadataBasefrom letta.schemas.llm_config import LLMConfig@@ -311,7 +312,9 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{{ block.description }}\n""</description>\n""<metadata>"- "{% if block.read_only %}\n- read_only=true{% endif %}\n- chars_current={{ block.value|length }}\n- chars_limit={{ block.limit }}\n"+ "{% if block.read_only %}\n- read_only=true{% endif %}\n"+ "- chars_current={{ block.value|length }}\n"+ "- chars_limit={{ block.limit }}\n""</metadata>\n""<value>\n"f"{CORE_MEMORY_LINE_NUMBER_WARNING}\n"@@ -323,14 +326,17 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% if not loop.last %}\n{% endif %}""{% endfor %}""\n</memory_blocks>"- "<files>\nThe following memory blocks are currently accessible in your core memory unit:\n\n"+ "<files>\nThe following memory files are currently accessible:\n\n""{% for block in file_blocks %}"+ f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\">\n""<{{ block.label }}>\n""<description>\n""{{ block.description }}\n""</description>\n""<metadata>"- "{% if block.read_only %}\n- read_only=true{% endif %}\n- chars_current={{ block.value|length }}\n- chars_limit={{ block.limit }}\n"+ "{% if block.read_only %}\n- read_only=true{% endif %}\n"+ "- chars_current={{ block.value|length }}\n"+ "- chars_limit={{ block.limit }}\n""</metadata>\n""<value>\n"f"{CORE_MEMORY_LINE_NUMBER_WARNING}\n"@@ -339,11 +345,12 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% endfor %}""</value>\n""</{{ block.label }}>\n"+ "</file>\n""{% if not loop.last %}\n{% endif %}""{% endfor %}"- "\n</memory_blocks>"- "</files>"+ "\n</files>")+# Default setup (MemGPT), no line numberselse:return (@@ -354,7 +361,9 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{{ block.description }}\n""</description>\n""<metadata>"- "{% if block.read_only %}\n- read_only=true{% endif %}\n- chars_current={{ block.value|length }}\n- chars_limit={{ block.limit }}\n"+ "{% if block.read_only %}\n- read_only=true{% endif %}\n"+ "- chars_current={{ block.value|length }}\n"+ "- chars_limit={{ block.limit }}\n""</metadata>\n""<value>\n""{{ block.value }}\n"@@ -364,18 +373,22 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% endfor %}""\n</memory_blocks>""<files>\nThe following memory files are currently accessible:\n\n"- "{% for block in file_blocks%}"+ "{% for block in file_blocks %}"+ f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\">\n""<{{ block.label }}>\n""<description>\n""{{ block.description }}\n""</description>\n""<metadata>"- "{% if block.read_only %}\n- read_only=true{% endif %}\n- chars_current={{ block.value|length }}\n- chars_limit={{ block.limit }}\n"+ "{% if block.read_only %}\n- read_only=true{% endif %}\n"+ "- chars_current={{ block.value|length }}\n"+ "- chars_limit={{ block.limit }}\n""</metadata>\n""<value>\n""{{ block.value }}\n""</value>\n""</{{ block.label }}>\n"+ "</file>\n""{% if not loop.last %}\n{% endif %}""{% endfor %}""\n</files>"diff --git a/letta/schemas/file.py b/letta/schemas/file.pyindex f537485dd..ecd9fe0a6 100644--- a/letta/schemas/file.py+++ b/letta/schemas/file.py@@ -1,4 +1,5 @@from datetime import datetime+from enum import Enumfrom typing import Optionalfrom pydantic import Field@@ -6,6 +7,15 @@ from pydantic import Fieldfrom letta.schemas.letta_base import LettaBase+class FileStatus(str, Enum):+ """+ Enum to represent the state of a file.+ """++ open = "open"+ closed = "closed"++class FileMetadataBase(LettaBase):"""Base class for FileMetadata schemas"""diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 2808b03ad..1a7b60ab9 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -2622,7 +2622,7 @@ class AgentManager:return resultsasync def get_context_window(self, agent_id: str, actor: PydanticUser) -> ContextWindowOverview:- agent_state = await self.get_agent_by_id_async(agent_id=agent_id, actor=actor)+ agent_state = await self.rebuild_system_prompt_async(agent_id=agent_id, actor=actor, force=True)calculator = ContextWindowCalculator()if os.getenv("LETTA_ENVIRONMENT") == "PRODUCTION" or agent_state.llm_config.model_endpoint_type == "anthropic":diff --git a/tests/helpers/utils.py b/tests/helpers/utils.pyindex 7674ee1a0..2dfa5cca4 100644--- a/tests/helpers/utils.py+++ b/tests/helpers/utils.py@@ -236,5 +236,5 @@ def validate_context_window_overview(overview: ContextWindowOverview, attached_f# 16. Check attached file is visibleif attached_file:assert attached_file.visible_content in overview.core_memory- assert "<file>" in overview.core_memory+ assert '<file status="open">' in overview.core_memoryassert "</file>" in overview.core_memorydiff --git a/tests/test_managers.py b/tests/test_managers.pyindex 2f2eee285..f8a42d824 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -696,7 +696,7 @@ async def test_get_context_window_basic(server: SyncServer, comprehensive_test_acomprehensive_agent_checks(created_agent, create_agent_request, actor=default_user)# Attach a file- await server.file_agent_manager.attach_file(+ assoc = await server.file_agent_manager.attach_file(agent_id=created_agent.id,file_id=default_file.id,actor=default_user,@@ -705,7 +705,7 @@ async def test_get_context_window_basic(server: SyncServer, comprehensive_test_a# Get context window and check for basic appearancescontext_window_overview = await server.agent_manager.get_context_window(agent_id=created_agent.id, actor=default_user)- validate_context_window_overview(context_window_overview)+ validate_context_window_overview(context_window_overview, assoc)# Test deleting the agentserver.agent_manager.delete_agent(created_agent.id, default_user)@@ -5851,7 +5851,9 @@ async def test_attach_is_idempotent(server, default_user, sarah_agent, default_fsarah_agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)file_blocks = sarah_agent.memory.file_blocks- assert len(file_blocks) == 0 # Is not open+ assert len(file_blocks) == 1+ assert file_blocks[0].value == "" # not open+ assert file_blocks[0].label == default_file.file_name@pytest.mark.asyncio@@ -5915,10 +5917,7 @@ async def test_list_files_and_agents(sarah_agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)file_blocks = sarah_agent.memory.file_blocks- assert len(file_blocks) == 1- assert file_blocks[0].value == ""- assert file_blocks[0].label == default_file.file_name-+ assert len(file_blocks) == 2charles_agent = await server.agent_manager.get_agent_by_id_async(agent_id=charles_agent.id, actor=default_user)file_blocks = charles_agent.memory.file_blocksassert len(file_blocks) == 1