# app/core/skills/definitions.py

SYSTEM_SKILLS = [
    {
        "name": "mesh_terminal_control",
        "description": "Execute stateful shell commands and manage terminal sessions across the agent mesh (Swarm Control).",
        "system_prompt": (
            "You are a high-level Mesh Orchestrator. When executing commands:\n"
            "1. **Parallel Execution**: Use 'node_ids' (plural) for simultaneous swarm sweeps.\n"
            "2. **Immediate Knowledge**: Calls return as soon as the task finishes. If a task takes 1s but you set timeout=60, you get the result in 1s.\n"
            "3. **Asynchronous Polling**: For background tasks, set 'no_abort=True'. If it times out, you get 'TIMEOUT_PENDING'. "
            "You can then use 'mesh_wait_tasks' with 'timeout=0' to peek at progress without blocking your turn.\n"
            "4. **Interactive Sub-shells**: Subsequent REPL inputs MUST use the `!RAW:` prefix.\n"
            "5. **Swarm Flow**: To start a background server (e.g. iperf3 -s) and move to node 2 immediately, "
            "use 'no_abort=True' and a SMALL 'timeout' (e.g. 2s). Never block your planning turn waiting for a persistent service.\n"
            "6. **Privilege-Aware Commands**: Each node's 'Privilege Level' is shown in the mesh context. "
            "Use it to decide how to run privileged operations:\n"
            "   - 'root': Run commands directly (no sudo prefix needed or available).\n"
            "   - 'standard user with passwordless sudo': Prepend sudo to privileged commands.\n"
            "   - 'standard user (sudo NOT available)': Avoid privileged ops or inform the user.\n"
            "7. **iperf3 Speed Test Pattern**: "
            "Step A: On the server node, run 'iperf3 -s -D' (daemon mode) with timeout=3, no_abort=True. "
            "Step B: On the client node, run 'iperf3 -c <server-node-IP> -t 10' with timeout=20. "
            "Node IPs are in the mesh context."
        ),
        "skill_type": "remote_grpc",
        "is_enabled": True,
        "features": ["chat", "swarm_control"],
        "config": {
            "service": "TerminalService",
            "method": "Execute",
            "capabilities": ["shell", "pty", "interactive"],
            "subagent_system_prompt": (
                "You are an autonomous Per-Node Monitoring Agent. Your goal is to watch the terminal output of a command and decide its state.\n"
                "- If the command output shows it is clearly finished or at a prompt (e.g. >>> or $), respond with action: FINISH.\n"
                "- If the output shows a prompt asking for input (e.g. [yes/no], [y/n], or 'Password:'), respond with action: EXECUTE and command: '!RAW:yes' (or appropriate response).\n"
                "- If the output shows it is still processing and making progress, respond with action: WAIT.\n"
                "- If the output has stopped for a long time or looks stuck in an infinite loop without useful output, respond with action: ABORT.\n"
                "Respond ONLY in JSON."
            ),
            "parameters": {
                        "type": "object",
                        "properties": {
                            "command": {"type": "string", "description": "Command to run. Use !RAW: prefix for REPL inputs."},
                            "node_id": {"type": "string", "description": "Target node ID."},
                            "node_ids": {
                                "type": "array",
                                "items": {"type": "string"},
                                "description": "List of node IDs for parallel swarm execution."
                            },
                            "timeout": {"type": "integer", "description": "Max seconds to wait. Default 30."},
                            "no_abort": {"type": "boolean", "description": "Internal use: If true, don't kill on timeout."},
                            "session_id": {"type": "string", "description": "Optional persistent session ID."}
                        },
                        "required": ["command"]
                    }
        },
        "is_system": True
    },
    {
        "name": "mesh_wait_tasks",
        "description": "Smartly poll or wait for background tasks.",
        "system_prompt": (
            "Wait for 'TIMEOUT_PENDING' tasks. This uses an AI sub-agent to monitor progress. "
            "It will return as soon as the sub-agent detects completion."
        ),
        "skill_type": "remote_grpc",
        "is_enabled": True,
        "features": ["chat", "swarm_control"],
        "config": {
            "subagent_system_prompt": "Monitor the provided task progress. Respond action: FINISH if done, else WAIT.",
            "parameters": {
                        "type": "object",
                        "properties": {
                            "task_map": {
                                "type": "object",
                                "additionalProperties": {"type": "string"},
                                "description": "Map of node_id -> task_id for tasks to wait on."
                            },
                            "timeout": {"type": "integer", "description": "How much longer to wait in seconds. Default 30."},
                            "no_abort": {"type": "boolean", "description": "If true, don't abort even if THIS wait times out. Default false."}
                        },
                        "required": ["task_map"]
                    }
        },
        "is_system": True
    },
    {
        "name": "browser_automation_agent",
        "description": "Perform web browsing, form filling, and UI testing on remote agent nodes using Playwright.",
        "system_prompt": "You are an AI browsing assistant. Use the Playwright tool to navigate pages, extract information, and interact with web elements. Always provide reasoning for your actions.",
        "skill_type": "remote_grpc",
        "is_enabled": True,
        "features": ["chat", "workflow", "swarm_control"],
        "config": {
            "service": "BrowserService",
            "method": "Navigate",
            "capabilities": ["browser", "screenshot", "click"],
            "parameters": {
                "type": "object",
                "properties": {
                    "url": {"type": "string", "description": "The URL to navigate to."},
                    "action": {"type": "string", "enum": ["navigate", "click", "type", "screenshot"], "description": "The browser action to perform."},
                    "node_id": {"type": "string", "description": "The target node ID."},
                    "session_id": {"type": "string", "description": "Optional session ID to persist browser state (cookies, login)."}
                },
                "required": ["url", "action", "node_id"]
            }
        },
        "is_system": True
    },
    {
        "name": "voice_interaction_handler",
        "description": "Handle real-time voice interruptions, tone analysis, and speech-to-speech feedback loops.",
        "system_prompt": "You are a voice-first AI. Keep your responses concise and conversational. Focus on natural prosody and handle interruptions gracefully.",
        "skill_type": "local",
        "is_enabled": True,
        "features": ["voice"],
        "config": {
            "interaction_mode": "speech-to-speech",
            "latency_target": 300,
            "parameters": {
                "type": "object",
                "properties": {
                    "mode": {"type": "string", "enum": ["active", "passive"], "description": "Voice interaction mode."}
                }
            }
        },
        "is_system": True
    },
    {
        "name": "mesh_file_explorer",
        "description": "List, read, and manipulate files within the decentralized mesh synchronization system.",
        "system_prompt": "You are a file management assistant. You can browse and synchronize files across different agent nodes.",
        "skill_type": "local",
        "is_enabled": True,
        "features": ["chat", "workflow", "swarm_control"],
        "config": {
            "internal_module": "app.core.grpc.core.mirror",
            "actions": ["list", "read", "write", "delete"],
            "parameters": {
                "type": "object",
                "properties": {
                    "action": {"type": "string", "enum": ["list", "read", "write", "delete"], "description": "File system action."},
                    "path": {"type": "string", "description": "Relative path to the file/directory."},
                    "node_id": {"type": "string", "description": "The target node ID."},
                    "content": {"type": "string", "description": "Optional content for write action."},
                    "session_id": {"type": "string", "description": "Target sync session workspace."}
                },
                "required": ["action", "path", "node_id"]
            }
        },

        "is_system": True
    }
]
