Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: Add FileBlock object (#3488)
Matthew Zhou6252e78e6782
diff --git a/letta/orm/files_agents.py b/letta/orm/files_agents.pyindex 4ae6dd3b5..2a4cfde29 100644--- a/letta/orm/files_agents.py+++ b/letta/orm/files_agents.py@@ -8,7 +8,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationshipfrom letta.constants import FILE_IS_TRUNCATED_WARNINGfrom letta.orm.mixins import OrganizationMixinfrom letta.orm.sqlalchemy_base import SqlalchemyBase-from letta.schemas.block import Block as PydanticBlock+from letta.schemas.block import FileBlock as PydanticFileBlockfrom letta.schemas.file import FileAgent as PydanticFileAgentif TYPE_CHECKING:@@ -59,7 +59,7 @@ class FileAgent(SqlalchemyBase, OrganizationMixin):String,ForeignKey("sources.id", ondelete="CASCADE"),nullable=False,- doc="ID of the source (denormalized from files.source_id)",+ doc="ID of the source",)file_name: Mapped[str] = mapped_column(@@ -86,7 +86,7 @@ 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, per_file_view_window_char_limit: int) -> PydanticBlock:+ def to_pydantic_block(self, per_file_view_window_char_limit: int) -> PydanticFileBlock:visible_content = self.visible_content if self.visible_content and self.is_open else ""# Truncate content and add warnings here when converting from FileAgent to Block@@ -95,10 +95,13 @@ class FileAgent(SqlalchemyBase, OrganizationMixin):visible_content = visible_content[: per_file_view_window_char_limit - len(truncated_warning)]visible_content += truncated_warning- return PydanticBlock(+ return PydanticFileBlock(value=visible_content,- label=self.file_name, # use denormalized file_name instead of self.file.file_name+ label=self.file_name,read_only=True,- metadata={"source_id": self.source_id}, # use denormalized source_id+ file_id=self.file_id,+ source_id=self.source_id,+ is_open=self.is_open,+ last_accessed_at=self.last_accessed_at,limit=per_file_view_window_char_limit,)diff --git a/letta/schemas/agent.py b/letta/schemas/agent.pyindex 3da7bde29..54f5796dd 100644--- a/letta/schemas/agent.py+++ b/letta/schemas/agent.py@@ -370,7 +370,7 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% endif %}""{% if file_blocks %}""{% for block in file_blocks %}"- "{% if block.metadata and block.metadata.get('source_id') == source.id %}"+ "{% if block.source_id and block.source_id == source.id %}"f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\">\n""<{{ block.label }}>\n""<description>\n"@@ -437,7 +437,7 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% endif %}""{% if file_blocks %}""{% for block in file_blocks %}"- "{% if block.metadata and block.metadata.get('source_id') == source.id %}"+ "{% if block.source_id and block.source_id == source.id %}"f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\" name=\"{{{{ block.label }}}}\">\n""{% if block.description %}""<description>\n"@@ -503,7 +503,7 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% endif %}""{% if file_blocks %}""{% for block in file_blocks %}"- "{% if block.metadata and block.metadata.get('source_id') == source.id %}"+ "{% if block.source_id and block.source_id == source.id %}"f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\" name=\"{{{{ block.label }}}}\">\n""{% if block.description %}""<description>\n"diff --git a/letta/schemas/block.py b/letta/schemas/block.pyindex ea9f12d41..ea30da771 100644--- a/letta/schemas/block.py+++ b/letta/schemas/block.py@@ -1,3 +1,4 @@+from datetime import datetimefrom typing import Optionalfrom pydantic import Field, model_validator@@ -79,6 +80,16 @@ class Block(BaseBlock):last_updated_by_id: Optional[str] = Field(None, description="The id of the user that last updated this Block.")+class FileBlock(Block):+ file_id: str = Field(..., description="Unique identifier of the file.")+ source_id: str = Field(..., description="Unique identifier of the source.")+ is_open: bool = Field(..., description="True if the agent currently has the file open.")+ last_accessed_at: Optional[datetime] = Field(+ default_factory=datetime.utcnow,+ description="UTC timestamp of the agent’s most recent access to this file. Any operations from the open, close, or search tools will update this field.",+ )++class Human(Block):"""Human block of the LLM context"""diff --git a/letta/schemas/file.py b/letta/schemas/file.pyindex 4a3c876f8..11a3356d7 100644--- a/letta/schemas/file.py+++ b/letta/schemas/file.py@@ -67,7 +67,7 @@ class FileAgentBase(LettaBase):# Core file-agent association fieldsagent_id: str = Field(..., description="Unique identifier of the agent.")file_id: str = Field(..., description="Unique identifier of the file.")- source_id: str = Field(..., description="Unique identifier of the source (denormalized from files.source_id).")+ source_id: str = Field(..., description="Unique identifier of the source.")file_name: str = Field(..., description="Name of the file.")is_open: bool = Field(True, description="True if the agent currently has the file open.")visible_content: Optional[str] = Field(diff --git a/letta/schemas/memory.py b/letta/schemas/memory.pyindex 829bf7379..bb8566763 100644--- a/letta/schemas/memory.py+++ b/letta/schemas/memory.py@@ -11,7 +11,7 @@ if TYPE_CHECKING:from openai.types.beta.function_tool import FunctionTool as OpenAIToolfrom letta.constants import CORE_MEMORY_BLOCK_CHAR_LIMIT-from letta.schemas.block import Block+from letta.schemas.block import Block, FileBlockfrom letta.schemas.message import Message@@ -66,8 +66,8 @@ class Memory(BaseModel, validate_assignment=True):# Memory.block contains the list of memory blocks in the core memoryblocks: List[Block] = Field(..., description="Memory blocks contained in the agent's in-context memory")- file_blocks: List[Block] = Field(- default_factory=list, description="Blocks representing the agent's in-context memory of an attached file"+ file_blocks: List[FileBlock] = Field(+ default_factory=list, description="Special blocks representing the agent's in-context memory of an attached file")@field_validator("file_blocks")diff --git a/letta/services/files_agents_manager.py b/letta/services/files_agents_manager.pyindex 38b981381..242ad8f98 100644--- a/letta/services/files_agents_manager.py+++ b/letta/services/files_agents_manager.py@@ -1,5 +1,5 @@from datetime import datetime, timezone-from typing import List, Optional+from typing import List, Optional, Unionfrom sqlalchemy import and_, func, select, update@@ -8,6 +8,7 @@ from letta.orm.errors import NoResultFoundfrom letta.orm.files_agents import FileAgent as FileAgentModelfrom letta.otel.tracing import trace_methodfrom letta.schemas.block import Block as PydanticBlock+from letta.schemas.block import FileBlock as PydanticFileBlockfrom letta.schemas.file import FileAgent as PydanticFileAgentfrom letta.schemas.file import FileMetadatafrom letta.schemas.user import User as PydanticUser@@ -231,7 +232,7 @@ class FileAgentManager:actor: PydanticUser,is_open_only: bool = False,return_as_blocks: bool = False,- ) -> List[PydanticFileAgent]:+ ) -> Union[List[PydanticFileAgent], List[PydanticFileBlock]]:"""Return associations for *agent_id* (filtering by `is_open` if asked)."""async with db_registry.async_session() as session:conditions = [@@ -351,7 +352,7 @@ class FileAgentManager:agent_id: ID of the agentfile_id: ID of the file to openfile_name: Name of the file to open- source_id: ID of the source (denormalized from files.source_id)+ source_id: ID of the sourceactor: User performing the actionvisible_content: Content to set for the opened file