From c93fe6a8db6f3f07ac571bbe2242daf046215200 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 9 May 2025 21:19:33 -0300 Subject: [PATCH] feat(agent_service, agent_builder, schemas): add agent_tools field to agent configuration and implement processing logic --- src/schemas/agent_config.py | 3 +++ src/services/agent_builder.py | 18 +++++++++++++++++- src/services/agent_service.py | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/schemas/agent_config.py b/src/schemas/agent_config.py index a74bfc82..b3ac9040 100644 --- a/src/schemas/agent_config.py +++ b/src/schemas/agent_config.py @@ -137,6 +137,9 @@ class LLMConfig(BaseModel): custom_mcp_servers: Optional[List[CustomMCPServerConfig]] = Field( default=None, description="List of custom MCP servers with URL and headers" ) + agent_tools: Optional[List[UUID]] = Field( + default=None, description="List of IDs of sub-agents" + ) sub_agents: Optional[List[UUID]] = Field( default=None, description="List of IDs of sub-agents" ) diff --git a/src/services/agent_builder.py b/src/services/agent_builder.py index 6713b455..a8ad3738 100644 --- a/src/services/agent_builder.py +++ b/src/services/agent_builder.py @@ -2,6 +2,7 @@ from typing import List, Optional, Tuple from google.adk.agents.llm_agent import LlmAgent from google.adk.agents import SequentialAgent, ParallelAgent, LoopAgent, BaseAgent from google.adk.models.lite_llm import LiteLlm +from google.adk.tools.agent_tool import AgentTool from src.utils.logger import setup_logger from src.core.exceptions import AgentNotFoundError from src.services.agent_service import get_agent @@ -26,6 +27,18 @@ class AgentBuilder: self.custom_tool_builder = CustomToolBuilder() self.mcp_service = MCPService() + async def _agent_tools_builder(self, agent) -> List[AgentTool]: + """Build the tools for an agent.""" + agent_tools_ids = agent.config.get("agent_tools") + agent_tools = [] + if agent_tools_ids and isinstance(agent_tools_ids, list): + for agent_tool_id in agent_tools_ids: + sub_agent = get_agent(self.db, agent_tool_id) + llm_agent, _ = await self.build_llm_agent(sub_agent) + if llm_agent: + agent_tools.append(AgentTool(agent=llm_agent)) + return agent_tools + async def _create_llm_agent( self, agent ) -> Tuple[LlmAgent, Optional[AsyncExitStack]]: @@ -42,8 +55,11 @@ class AgentBuilder: agent.config, self.db ) + # Get agent tools + agent_tools = await self._agent_tools_builder(agent) + # Combine all tools - all_tools = custom_tools + mcp_tools + all_tools = custom_tools + mcp_tools + agent_tools now = datetime.now() current_datetime = now.strftime("%d/%m/%Y %H:%M") diff --git a/src/services/agent_service.py b/src/services/agent_service.py index 1d7f4c1d..516ada64 100644 --- a/src/services/agent_service.py +++ b/src/services/agent_service.py @@ -224,6 +224,9 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent: if "custom_tools" in config: processed_config["custom_tools"] = config["custom_tools"] + if "agent_tools" in config: + processed_config["agent_tools"] = config["agent_tools"] + if "sub_agents" in config: processed_config["sub_agents"] = config["sub_agents"] @@ -236,6 +239,7 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent: "tools", "custom_tools", "sub_agents", + "agent_tools", "custom_mcp_servers", "mcp_servers", ]: @@ -303,6 +307,12 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent: str(agent_id) for agent_id in config["sub_agents"] ] + # Process agent tools + if "agent_tools" in config and config["agent_tools"] is not None: + processed_config["agent_tools"] = [ + str(agent_id) for agent_id in config["agent_tools"] + ] + # Process tools if "tools" in config and config["tools"] is not None: processed_tools = [] @@ -484,6 +494,9 @@ async def update_agent( if "sub_agents" in config: processed_config["sub_agents"] = config["sub_agents"] + if "agent_tools" in config: + processed_config["agent_tools"] = config["agent_tools"] + if "custom_mcp_servers" in config: processed_config["custom_mcp_servers"] = config["custom_mcp_servers"] @@ -493,6 +506,7 @@ async def update_agent( "tools", "custom_tools", "sub_agents", + "agent_tools", "custom_mcp_servers", "mcp_servers", ]: @@ -563,6 +577,12 @@ async def update_agent( str(agent_id) for agent_id in config["sub_agents"] ] + # Process agent tools + if "agent_tools" in config and config["agent_tools"] is not None: + processed_config["agent_tools"] = [ + str(agent_id) for agent_id in config["agent_tools"] + ] + # Process tools if "tools" in config and config["tools"] is not None: processed_tools = []