feat(agent_service, agent_builder, schemas): add agent_tools field to agent configuration and implement processing logic

This commit is contained in:
Davidson Gomes 2025-05-09 21:19:33 -03:00
parent b32a044cac
commit c93fe6a8db
3 changed files with 40 additions and 1 deletions

View File

@ -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"
)

View File

@ -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")

View File

@ -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 = []