Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Fix open/close/close-all files (#3512)
Matthew Zhou5249cf70b464
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex f6c6e46b3..f5230297d 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -387,7 +387,7 @@ async def close_all_open_files("""actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)- return server.file_agent_manager.close_all_other_files(agent_id=agent_id, keep_file_names=[], actor=actor)+ return await server.file_agent_manager.close_all_other_files(agent_id=agent_id, keep_file_names=[], actor=actor)@router.patch("/{agent_id}/files/{file_id}/open", response_model=List[str], operation_id="open_file")diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex e8a5961a3..b7b029097 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -95,7 +95,7 @@ from letta.services.passage_manager import PassageManagerfrom letta.services.source_manager import SourceManagerfrom letta.services.tool_manager import ToolManagerfrom letta.settings import DatabaseChoice, settings-from letta.utils import enforce_types, united_diff+from letta.utils import calculate_file_defaults_based_on_context_window, enforce_types, united_difflogger = get_logger(__name__)@@ -3139,7 +3139,29 @@ class AgentManager:if row is None:raise ValueError(f"Agent {agent_id} not found")- return row[0], row[1]+ per_file_limit, max_files = row[0], row[1]++ # Handle None values by calculating defaults based on context window+ if per_file_limit is None or max_files is None:+ # Get the agent's model context window to calculate appropriate defaults+ model_result = await session.execute(+ select(AgentModel.llm_config)+ .where(AgentModel.id == agent_id)+ .where(AgentModel.organization_id == actor.organization_id)+ .where(AgentModel.is_deleted == False)+ )+ model_row = model_result.one_or_none()+ context_window = model_row[0].context_window if model_row and model_row[0] else None++ default_max_files, default_per_file_limit = calculate_file_defaults_based_on_context_window(context_window)++ # Use calculated defaults for None values+ if per_file_limit is None:+ per_file_limit = default_per_file_limit+ if max_files is None:+ max_files = default_max_files++ return per_file_limit, max_files@enforce_types@trace_methoddiff --git a/tests/test_sources.py b/tests/test_sources.pyindex b7e64e2c6..aee7f9749 100644--- a/tests/test_sources.py+++ b/tests/test_sources.py@@ -987,3 +987,68 @@ def test_pinecone_lifecycle_file_and_source_deletion(client: LettaSDKClient):), f"All source records should be removed from Pinecone after source deletion, but found {len(records_after)}"print("✓ Pinecone lifecycle verified - namespace is clean after source deletion")+++def test_agent_open_file(disable_pinecone, client: LettaSDKClient, agent_state: AgentState):+ """Test client.agents.open_file() function"""+ # Create a new source+ source = client.sources.create(name="test_source", embedding="openai/text-embedding-3-small")++ # Attach source to agent+ client.agents.sources.attach(source_id=source.id, agent_id=agent_state.id)++ # Upload a file+ file_path = "tests/data/test.txt"+ file_metadata = upload_file_and_wait(client, source.id, file_path)++ # Basic test open_file function+ closed_files = client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)+ assert len(closed_files) == 0+++def test_agent_close_file(disable_pinecone, client: LettaSDKClient, agent_state: AgentState):+ """Test client.agents.close_file() function"""+ # Create a new source+ source = client.sources.create(name="test_source", embedding="openai/text-embedding-3-small")++ # Attach source to agent+ client.agents.sources.attach(source_id=source.id, agent_id=agent_state.id)++ # Upload a file+ file_path = "tests/data/test.txt"+ file_metadata = upload_file_and_wait(client, source.id, file_path)++ # First open the file+ client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)++ # Test close_file function+ client.agents.close_file(agent_id=agent_state.id, file_id=file_metadata.id)++ # Result can be None or any type based on the signature+ # Just verify the function executes without error+ assert True, "close_file should execute without error"+++def test_agent_close_all_open_files(disable_pinecone, client: LettaSDKClient, agent_state: AgentState):+ """Test client.agents.close_all_open_files() function"""+ # Create a new source+ source = client.sources.create(name="test_source", embedding="openai/text-embedding-3-small")++ # Attach source to agent+ client.agents.sources.attach(source_id=source.id, agent_id=agent_state.id)++ # Upload multiple files+ file_paths = ["tests/data/test.txt", "tests/data/test.md"]+ file_metadatas = []+ for file_path in file_paths:+ file_metadata = upload_file_and_wait(client, source.id, file_path)+ file_metadatas.append(file_metadata)+ # Open each file+ client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)++ # Test close_all_open_files function+ result = client.agents.close_all_open_files(agent_id=agent_state.id)++ # Verify result is a list of strings+ assert isinstance(result, list), f"Expected list, got {type(result)}"+ assert all(isinstance(item, str) for item in result), "All items in result should be strings"