"""
Lab 7.8: Meta-Agent — Agent That Builds Agents

Objective: Generate a new agent persona from documentation.
"""

import json
import os


# TODO 1: Define the meta-agent's tool surface
# The meta-agent needs:
# - read_file: to read documentation
# - search_web: to find latest docs
# - write_file: to write the new agent files
# - generate_agent: special tool that creates agent files

GENERATE_AGENT_TOOL = {
    "name": "generate_agent",
    "description": "Generate a new agent with system prompt, tools, and skills",
    "input_schema": {
        "type": "object",
        "properties": {
            "agent_name": {"type": "string"},
            "persona": {"type": "string", "description": "Agent personality and expertise area"},
            "tools": {"type": "array", "items": {"type": "string"}},
            "system_prompt": {"type": "string"},
            "skills": {"type": "array", "items": {"type": "string"}},
            "reasoning": {"type": "string"}
        },
        "required": ["agent_name", "persona", "tools", "system_prompt", "reasoning"]
    }
}


# TODO 2: Implement the meta-agent loop
class MetaAgent:
    """
    An agent that builds other agents.
    
    Given a description of what the target agent should do:
    1. Research the domain
    2. Read relevant documentation
    3. Design the agent persona
    4. Generate agent files (system prompt, tools, skills)
    5. Validate the generated agent
    """
    
    def build_agent(self, description: str, output_dir: str = "./generated-agents"):
        """
        Build a new agent from a natural language description.
        
        Example:
        > description: "An agent that monitors API uptime and alerts on failures"
        
        Should generate:
        - output_dir/api-monitor/system-prompt.md
        - output_dir/api-monitor/tools.py
        - output_dir/api-monitor/skills.yaml
        - output_dir/api-monitor/mental-model.yaml
        """
        pass  # TODO


# TODO 3: Define the agent directory structure
AGENT_TEMPLATE = {
    "system-prompt.md": """
# {agent_name}

## Persona
{persona}

## Behavioral Rules
1. Always check the mental model before starting work
2. Log all tool calls with reasoning
3. Escalate to human if confidence < 70%
4. Never run destructive operations

## Domain
{tools_description}

## Output Format
Structured reports with confidence levels.
""",
    "mental-model.yaml": """
expertise:
  - topic: "initial setup"
    notes: "Freshly created agent"
    last_updated: "{date}"
""",
    "skills.yaml": """
skills:
  - path: skills/conversational-response.md
    use-when: Always
  - path: skills/domain-expertise.md
    use-when: Working in domain
"""
}


if __name__ == "__main__":
    meta = MetaAgent()
    
    description = """
    Build a security audit agent that:
    - Reviews code for common vulnerabilities
    - Checks for exposed secrets in codebase
    - Generates security reports
    - Has read-only access to the filesystem
    """
    
    result = meta.build_agent(description)
    print(json.dumps(result, indent=2))
