Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat(asyncify): migrate list sources (#2276)
cthomasc84b90cea896
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex d0fd25452..7e0ec54a2 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -369,7 +369,7 @@ def delete_agent(@router.get("/{agent_id}/sources", response_model=List[Source], operation_id="list_agent_sources")-def list_agent_sources(+async def list_agent_sources(agent_id: str,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@@ -377,8 +377,8 @@ def list_agent_sources("""Get the sources associated with an agent."""- actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.agent_manager.list_attached_sources(agent_id=agent_id, actor=actor)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)+ return await server.agent_manager.list_attached_sources_async(agent_id=agent_id, actor=actor)# TODO: remove? can also get with agent blocksdiff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 326dd30f2..e32c8dfb2 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -1568,6 +1568,25 @@ class AgentManager:# Use the lazy-loaded relationship to get sourcesreturn [source.to_pydantic() for source in agent.sources]+ @enforce_types+ async def list_attached_sources_async(self, agent_id: str, actor: PydanticUser) -> List[PydanticSource]:+ """+ Lists all sources attached to an agent.++ Args:+ agent_id: ID of the agent to list sources for+ actor: User performing the action++ Returns:+ List[str]: List of source IDs attached to the agent+ """+ async with db_registry.async_session() as session:+ # Verify agent exists and user has permission to access it+ agent = await AgentModel.read_async(db_session=session, identifier=agent_id, actor=actor)++ # Use the lazy-loaded relationship to get sources+ return [source.to_pydantic() for source in agent.sources]+@enforce_typesdef detach_source(self, agent_id: str, source_id: str, actor: PydanticUser) -> PydanticAgentState:"""diff --git a/tests/test_managers.py b/tests/test_managers.pyindex ca7fa6eab..99fda9fd9 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -1110,10 +1110,11 @@ async def test_attach_source(server: SyncServer, sarah_agent, default_source, deassert len([s for s in agent.sources if s.id == default_source.id]) == 1-def test_list_attached_source_ids(server: SyncServer, sarah_agent, default_source, other_source, default_user):+async def test_list_attached_source_ids(server: SyncServer, sarah_agent, default_source, other_source, default_user, event_loop):"""Test listing source IDs attached to an agent."""# Initially should have no sources- sources = server.agent_manager.list_attached_sources(sarah_agent.id, actor=default_user)+ sources = await server.agent_manager.list_attached_sources_async(sarah_agent.id, actor=default_user)assert len(sources) == 0# Attach sources@@ -1121,7 +1122,7 @@ def test_list_attached_source_ids(server: SyncServer, sarah_agent, default_sourcserver.agent_manager.attach_source(sarah_agent.id, other_source.id, actor=default_user)# List sources and verify- sources = server.agent_manager.list_attached_sources(sarah_agent.id, actor=default_user)+ sources = await server.agent_manager.list_attached_sources_async(sarah_agent.id, actor=default_user)assert len(sources) == 2source_ids = [s.id for s in sources]assert default_source.id in source_ids@@ -1167,10 +1168,11 @@ def test_detach_source_nonexistent_agent(server: SyncServer, default_source, defserver.agent_manager.detach_source(agent_id="nonexistent-agent-id", source_id=default_source.id, actor=default_user)-def test_list_attached_source_ids_nonexistent_agent(server: SyncServer, default_user):+async def test_list_attached_source_ids_nonexistent_agent(server: SyncServer, default_user, event_loop):"""Test listing sources for a nonexistent agent."""with pytest.raises(NoResultFound):- server.agent_manager.list_attached_sources(agent_id="nonexistent-agent-id", actor=default_user)+ await server.agent_manager.list_attached_sources_async(agent_id="nonexistent-agent-id", actor=default_user)def test_list_attached_agents(server: SyncServer, sarah_agent, charles_agent, default_source, default_user):