The AgentFactoryTool enables BDI agents to dynamically create new agents with full lifecycle management. It handles identity creation, Guardian validation, workspace setup, code generation, and coordinator registration.
File: tools/agent_factory_tool.py
Class: AgentFactoryTool
Version: 1.0.0
Status: ✅ Active (High Priority)
class AgentFactoryTool(BaseTool):
- memory_agent: MemoryAgent - For workspace management
- coordinator_ref: CoordinatorAgent - For agent registration
- guardian_ref: GuardianAgent - For security validation
- agent_templates_dir: Path - Agent template directory
create_agentCreates a new agent with full lifecycle management.
Parameters:
agent_type (str, required): Type of agent to createagent_id (str, required): Unique agent identifieragent_config (Dict, optional): Agent configurationReturns:
Tuple[bool, Dict[str, Any]] # (success, agent_metadata)
Agent Metadata:
{
"agent_id": str,
"agent_type": str,
"public_key": str,
"env_var_name": str,
"workspace_path": str,
"code_path": str,
"created_at": float,
"created_by": str,
"config": Dict,
"status": str
}
validate_agentValidates agent identity and workspace.
Parameters:
agent_id (str, required): Agent to validateReturns:
Tuple[bool, Dict[str, Any]] # (success, validation_result)
from tools.agent_factory_tool import AgentFactoryTool
from agents.memory_agent import MemoryAgent
from orchestration.coordinator_agent import CoordinatorAgent
from agents.guardian_agent import GuardianAgent
tool = AgentFactoryTool(
memory_agent=memory_agent,
coordinator_ref=coordinator,
guardian_ref=guardian
)
Create agent
success, metadata = await tool.execute(
action="create_agent",
agent_type="analysis_agent",
agent_id="analysis_001",
agent_config={
"description": "Specialized analysis agent",
"capabilities": ["data_analysis", "reporting"]
}
)
if success:
print(f"Agent created: {metadata['agent_id']}")
print(f"Public key: {metadata['public_key']}")
print(f"Workspace: {metadata['workspace_path']}")
# Validate existing agent
success, validation = await tool.execute(
action="validate_agent",
agent_id="analysis_001"
)
if success:
print(f"Validation: {validation['validation_status']}")
print(f"Public key: {validation['public_key']}")
The tool generates agents with:
Example Generated Code:
class Analysis001Agent:
"""Dynamically created analysis_agent agent."""
def __init__(self, agent_id: str = "analysis_001", ...):
# Initialization code
async def execute_task(self, task: str, context: Dict[str, Any] = None):
# Task execution logic
All agents must pass Guardian validation:
Secure identity creation:
Each agent gets isolated workspace:
Creates agent identities:
id_manager = await IDManagerAgent.get_instance()
public_key, env_var_name = await id_manager.create_new_wallet(entity_id=agent_id)
Validates agent security:
guardian_validation = await self._validate_with_guardian(agent_id, public_key)
Registers agents:
registration_result = await self.coordinator_ref.create_and_register_agent(
agent_type, agent_id, agent_config
)
Manages agent workspaces:
agent_workspace = self.memory_agent.get_agent_data_directory(agent_id)
# Create specialized analysis agent
success, metadata = await tool.execute(
action="create_agent",
agent_type="data_analysis_agent",
agent_id="data_analyst_001",
agent_config={
"description": "Advanced data analysis agent",
"capabilities": ["statistical_analysis", "ml_modeling", "visualization"],
"memory_limit": "10GB",
"priority": "high"
}
)
# Create multiple agents for different tasks
agents = [
("data_collection", "collector_001"),
("data_processing", "processor_001"),
("data_analysis", "analyzer_001")
]
for agent_type, agent_id in agents:
success, metadata = await tool.execute(
action="create_agent",
agent_type=agent_type,
agent_id=agent_id
)
if success:
print(f"Created {agent_id}")
core.id_manager_agent.IDManagerAgent: Identity managementagents.guardian_agent.GuardianAgent: Security validationorchestration.coordinator_agent.CoordinatorAgent: Agent registrationagents.memory_agent.MemoryAgent: Workspace managementcore.bdi_agent.BaseTool: Base tool classTemplates stored in:
agents/templates/
Generated agents saved to:
agents/{agent_id}.py
Agent metadata stored in:
data/memory/agent_workspaces/{agent_id}/agent_metadata.json