From 9c1ec9782c2a750d4c451a49102933fbec45efed Mon Sep 17 00:00:00 2001 From: cthomas Date: Sat, 19 Jul 2025 23:45:59 -0700 Subject: [PATCH] feat: remove groups selectin from agents model (#3429) --- letta/orm/agent.py | 2 +- letta/services/agent_manager.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/letta/orm/agent.py b/letta/orm/agent.py index 4504bbc0..44e8a6f0 100644 --- a/letta/orm/agent.py +++ b/letta/orm/agent.py @@ -128,7 +128,7 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs): groups: Mapped[List["Group"]] = relationship( "Group", secondary="groups_agents", - lazy="selectin", + lazy="raise", back_populates="agents", passive_deletes=True, ) diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py index a9c7e008..ff7abfba 100644 --- a/letta/services/agent_manager.py +++ b/letta/services/agent_manager.py @@ -28,7 +28,7 @@ from letta.orm import AgentPassage, AgentsTags from letta.orm import Block as BlockModel from letta.orm import BlocksAgents from letta.orm import Group as GroupModel -from letta.orm import IdentitiesAgents +from letta.orm import GroupsAgents, IdentitiesAgents from letta.orm import Source as SourceModel from letta.orm import SourcePassage, SourcesAgents from letta.orm import Tool as ToolModel @@ -1423,10 +1423,17 @@ class AgentManager: @trace_method def list_groups(self, agent_id: str, actor: PydanticUser, manager_type: Optional[str] = None) -> List[PydanticGroup]: with db_registry.session() as session: - agent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor) + query = ( + select(GroupModel) + .join(GroupsAgents, GroupModel.id == GroupsAgents.group_id) + .where(GroupsAgents.agent_id == agent_id, GroupModel.organization_id == actor.organization_id) + ) + if manager_type: - return [group.to_pydantic() for group in agent.groups if group.manager_type == manager_type] - return [group.to_pydantic() for group in agent.groups] + query = query.where(GroupModel.manager_type == manager_type) + + result = session.execute(query) + return [group.to_pydantic() for group in result.scalars()] # ====================================================================================================================== # In Context Messages Management