fix: skip agent joins in blocks endpoint (#3226)

This commit is contained in:
cthomas
2025-07-08 12:38:08 -07:00
committed by GitHub
parent d0c5cd5e45
commit 5e4b094a80
2 changed files with 19 additions and 3 deletions

View File

@@ -101,6 +101,14 @@ async def retrieve_block(
@router.get("/{block_id}/agents", response_model=List[AgentState], operation_id="list_agents_for_block")
async def list_agents_for_block(
block_id: str,
include_relationships: list[str] | None = Query(
None,
description=(
"Specify which relational fields (e.g., 'tools', 'sources', 'memory') to include in the response. "
"If not provided, all relationships are loaded by default. "
"Using this can optimize performance by reducing unnecessary joins."
),
),
server: SyncServer = Depends(get_letta_server),
actor_id: Optional[str] = Header(None, alias="user_id"),
):
@@ -110,7 +118,9 @@ async def list_agents_for_block(
"""
actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)
try:
agents = await server.block_manager.get_agents_for_block_async(block_id=block_id, actor=actor)
agents = await server.block_manager.get_agents_for_block_async(
block_id=block_id, include_relationships=include_relationships, actor=actor
)
return agents
except NoResultFound:
raise HTTPException(status_code=404, detail=f"Block with id={block_id} not found")

View File

@@ -286,14 +286,20 @@ class BlockManager:
@trace_method
@enforce_types
async def get_agents_for_block_async(self, block_id: str, actor: PydanticUser) -> List[PydanticAgentState]:
async def get_agents_for_block_async(
self,
block_id: str,
actor: PydanticUser,
include_relationships: Optional[List[str]] = None,
) -> List[PydanticAgentState]:
"""
Retrieve all agents associated with a given block.
"""
async with db_registry.async_session() as session:
block = await BlockModel.read_async(db_session=session, identifier=block_id, actor=actor)
agents_orm = block.agents
return await asyncio.gather(*[agent.to_pydantic_async() for agent in agents_orm])
agents = await asyncio.gather(*[agent.to_pydantic_async(include_relationships=include_relationships) for agent in agents_orm])
return agents
@trace_method
@enforce_types