Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat(asyncify): multi agent tool (#2525)
cthomasf76aaf8ca7a7
diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.pyindex b86413d10..bd5d5821e 100644--- a/letta/agents/letta_agent.py+++ b/letta/agents/letta_agent.py@@ -1,7 +1,7 @@import asyncioimport jsonimport uuid-from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple, Union+from typing import AsyncGenerator, Dict, List, Optional, Tuple, Unionfrom openai import AsyncStreamfrom openai.types.chat import ChatCompletionChunk@@ -22,7 +22,6 @@ from letta.log import get_loggerfrom letta.orm.enums import ToolTypefrom letta.schemas.agent import AgentStatefrom letta.schemas.enums import MessageRole, MessageStreamStatus-from letta.schemas.letta_message import AssistantMessagefrom letta.schemas.letta_message_content import OmittedReasoningContent, ReasoningContent, RedactedReasoningContent, TextContentfrom letta.schemas.letta_response import LettaResponsefrom letta.schemas.llm_config import LLMConfig@@ -840,58 +839,6 @@ class LettaAgent(BaseAgent):log_event(name=f"finish_{tool_name}_execution", attributes=tool_args)return tool_execution_result- @trace_method- async def _send_message_to_agents_matching_tags(- self, message: str, match_all: List[str], match_some: List[str]- ) -> List[Dict[str, Any]]:- # Find matching agents- matching_agents = self.agent_manager.list_agents_matching_tags(actor=self.actor, match_all=match_all, match_some=match_some)- if not matching_agents:- return []-- async def process_agent(agent_state: AgentState, message: str) -> Dict[str, Any]:- try:- letta_agent = LettaAgent(- agent_id=agent_state.id,- message_manager=self.message_manager,- agent_manager=self.agent_manager,- block_manager=self.block_manager,- passage_manager=self.passage_manager,- actor=self.actor,- )-- augmented_message = (- "[Incoming message from external Letta agent - to reply to this message, "- "make sure to use the 'send_message' at the end, and the system will notify "- "the sender of your response] "- f"{message}"- )-- letta_response = await letta_agent.step(- [MessageCreate(role=MessageRole.system, content=[TextContent(text=augmented_message)])]- )- messages = letta_response.messages-- send_message_content = [message.content for message in messages if isinstance(message, AssistantMessage)]-- return {- "agent_id": agent_state.id,- "agent_name": agent_state.name,- "response": send_message_content if send_message_content else ["<no response>"],- }-- except Exception as e:- return {- "agent_id": agent_state.id,- "agent_name": agent_state.name,- "error": str(e),- "type": type(e).__name__,- }-- tasks = [asyncio.create_task(process_agent(agent_state=agent_state, message=message)) for agent_state in matching_agents]- results = await asyncio.gather(*tasks)- return results-@trace_methoddef _load_last_function_response(self, in_context_messages: List[Message]):"""Load the last function response from message history"""diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 6bfbc4c4f..344bd2217 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -988,6 +988,50 @@ class AgentManager:return list(session.execute(query).scalars())+ @enforce_types+ @trace_method+ async def list_agents_matching_tags_async(+ self,+ actor: PydanticUser,+ match_all: List[str],+ match_some: List[str],+ limit: Optional[int] = 50,+ ) -> List[PydanticAgentState]:+ """+ Retrieves agents in the same organization that match all specified `match_all` tags+ and at least one tag from `match_some`. The query is optimized for efficiency by+ leveraging indexed filtering and aggregation.++ Args:+ actor (PydanticUser): The user requesting the agent list.+ match_all (List[str]): Agents must have all these tags.+ match_some (List[str]): Agents must have at least one of these tags.+ limit (Optional[int]): Maximum number of agents to return.++ Returns:+ List[PydanticAgentState: The filtered list of matching agents.+ """+ async with db_registry.async_session() as session:+ query = select(AgentModel).where(AgentModel.organization_id == actor.organization_id)++ if match_all:+ # Subquery to find agent IDs that contain all match_all tags+ subquery = (+ select(AgentsTags.agent_id)+ .where(AgentsTags.tag.in_(match_all))+ .group_by(AgentsTags.agent_id)+ .having(func.count(AgentsTags.tag) == literal(len(match_all)))+ )+ query = query.where(AgentModel.id.in_(subquery))++ if match_some:+ # Ensures agents match at least one tag in match_some+ query = query.join(AgentsTags).where(AgentsTags.tag.in_(match_some))++ query = query.distinct(AgentModel.id).order_by(AgentModel.id).limit(limit)+ result = await session.execute(query)+ return await asyncio.gather(*[agent.to_pydantic_async() for agent in result.scalars()])+@trace_methoddef size(self,diff --git a/letta/services/tool_executor/tool_executor.py b/letta/services/tool_executor/tool_executor.pyindex e8a5fc391..7e9701468 100644--- a/letta/services/tool_executor/tool_executor.py+++ b/letta/services/tool_executor/tool_executor.py@@ -548,7 +548,7 @@ class LettaMultiAgentToolExecutor(ToolExecutor):function_map = {"send_message_to_agent_and_wait_for_reply": self.send_message_to_agent_and_wait_for_reply,"send_message_to_agent_async": self.send_message_to_agent_async,- "send_message_to_agents_matching_tags": self.send_message_to_agents_matching_tags,+ "send_message_to_agents_matching_tags": self.send_message_to_agents_matching_tags_async,}if function_name not in function_map:@@ -586,11 +586,13 @@ class LettaMultiAgentToolExecutor(ToolExecutor):return "Successfully sent message"- async def send_message_to_agents_matching_tags(+ async def send_message_to_agents_matching_tags_async(self, agent_state: AgentState, message: str, match_all: List[str], match_some: List[str]) -> str:# Find matching agents- matching_agents = self.agent_manager.list_agents_matching_tags(actor=self.actor, match_all=match_all, match_some=match_some)+ matching_agents = await self.agent_manager.list_agents_matching_tags_async(+ actor=self.actor, match_all=match_all, match_some=match_some+ )if not matching_agents:return str([])diff --git a/tests/test_managers.py b/tests/test_managers.pyindex 337a0700e..721fceeca 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -1224,8 +1224,9 @@ async def test_list_attached_agents_nonexistent_source(server: SyncServer, defau# ======================================================================================================================-def test_list_agents_matching_all_tags(server: SyncServer, default_user, agent_with_tags):- agents = server.agent_manager.list_agents_matching_tags(+async def test_list_agents_matching_all_tags(server: SyncServer, default_user, agent_with_tags, event_loop):+ agents = await server.agent_manager.list_agents_matching_tags_async(actor=default_user,match_all=["primary_agent", "benefit_1"],match_some=[],@@ -1234,8 +1235,9 @@ def test_list_agents_matching_all_tags(server: SyncServer, default_user, agent_wassert {a.name for a in agents} == {"agent1", "agent3"}-def test_list_agents_matching_some_tags(server: SyncServer, default_user, agent_with_tags):- agents = server.agent_manager.list_agents_matching_tags(+async def test_list_agents_matching_some_tags(server: SyncServer, default_user, agent_with_tags, event_loop):+ agents = await server.agent_manager.list_agents_matching_tags_async(actor=default_user,match_all=["primary_agent"],match_some=["benefit_1", "benefit_2"],@@ -1244,8 +1246,9 @@ def test_list_agents_matching_some_tags(server: SyncServer, default_user, agent_assert {a.name for a in agents} == {"agent1", "agent2", "agent3"}-def test_list_agents_matching_all_and_some_tags(server: SyncServer, default_user, agent_with_tags):- agents = server.agent_manager.list_agents_matching_tags(+async def test_list_agents_matching_all_and_some_tags(server: SyncServer, default_user, agent_with_tags, event_loop):+ agents = await server.agent_manager.list_agents_matching_tags_async(actor=default_user,match_all=["primary_agent", "benefit_1"],match_some=["benefit_2", "nonexistent"],@@ -1254,8 +1257,9 @@ def test_list_agents_matching_all_and_some_tags(server: SyncServer, default_userassert agents[0].name == "agent3"-def test_list_agents_matching_no_tags(server: SyncServer, default_user, agent_with_tags):- agents = server.agent_manager.list_agents_matching_tags(+async def test_list_agents_matching_no_tags(server: SyncServer, default_user, agent_with_tags, event_loop):+ agents = await server.agent_manager.list_agents_matching_tags_async(actor=default_user,match_all=["primary_agent", "nonexistent_tag"],match_some=["benefit_1", "benefit_2"],