File size: 6,313 Bytes
e6853d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | """RemoteTriggerTool - Trigger actions on remote agents for Stack 2.9"""
import json
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
from .base import BaseTool, ToolResult
from .registry import tool_registry
REMOTES_FILE = Path.home() / ".stack-2.9" / "remotes.json"
TRIGGERS_FILE = Path.home() / ".stack-2.9" / "triggers.json"
def _load_remotes() -> Dict[str, Any]:
"""Load remote configurations."""
REMOTES_FILE.parent.mkdir(parents=True, exist_ok=True)
if REMOTES_FILE.exists():
return json.loads(REMOTES_FILE.read_text())
return {"remotes": {}}
def _save_remotes(data: Dict[str, Any]) -> None:
"""Save remote configurations."""
REMOTES_FILE.write_text(json.dumps(data, indent=2))
def _load_triggers() -> Dict[str, Any]:
"""Load trigger history."""
TRIGGERS_FILE.parent.mkdir(parents=True, exist_ok=True)
if TRIGGERS_FILE.exists():
return json.loads(TRIGGERS_FILE.read_text())
return {"triggers": []}
def _save_triggers(data: Dict[str, Any]) -> None:
"""Save trigger history."""
TRIGGERS_FILE.write_text(json.dumps(data, indent=2))
class RemoteAddTool(BaseTool):
"""Add a remote agent endpoint."""
name = "remote_add"
description = "Add a remote agent configuration"
input_schema = {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Remote agent name"
},
"url": {
"type": "string",
"description": "Remote agent URL or endpoint"
},
"api_key": {
"type": "string",
"description": "API key for authentication"
},
"capabilities": {
"type": "array",
"items": {"type": "string"},
"description": "Remote agent capabilities"
}
},
"required": ["name", "url"]
}
async def execute(self, name: str, url: str, api_key: Optional[str] = None, capabilities: Optional[List[str]] = None) -> ToolResult:
"""Add remote."""
data = _load_remotes()
data["remotes"][name] = {
"url": url,
"api_key": api_key,
"capabilities": capabilities or [],
"status": "active",
"added_at": datetime.now().isoformat()
}
_save_remotes(data)
return ToolResult(success=True, data={
"remote": name,
"status": "added",
"capabilities": capabilities or []
})
class RemoteListTool(BaseTool):
"""List all remote agents."""
name = "remote_list"
description = "List all configured remote agents"
input_schema = {
"type": "object",
"properties": {},
"required": []
}
async def execute(self) -> ToolResult:
"""List remotes."""
data = _load_remotes()
remotes = data.get("remotes", {})
return ToolResult(success=True, data={
"remotes": [
{"name": name, "url": info["url"], "status": info.get("status"), "capabilities": info.get("capabilities", [])}
for name, info in remotes.items()
],
"count": len(remotes)
})
class RemoteTriggerTool(BaseTool):
"""Trigger an action on a remote agent."""
name = "remote_trigger"
description = "Trigger an action on a remote agent"
input_schema = {
"type": "object",
"properties": {
"remote": {
"type": "string",
"description": "Remote agent name"
},
"action": {
"type": "string",
"description": "Action to trigger"
},
"parameters": {
"type": "object",
"description": "Action parameters"
},
"wait_for_response": {
"type": "boolean",
"default": True,
"description": "Wait for response or fire-and-forget"
}
},
"required": ["remote", "action"]
}
async def execute(self, remote: str, action: str, parameters: Optional[Dict] = None, wait_for_response: bool = True) -> ToolResult:
"""Trigger remote action."""
data = _load_remotes()
remotes = data.get("remotes", {})
if remote not in remotes:
return ToolResult(success=False, error=f"Remote '{remote}' not found")
trigger_id = str(uuid.uuid4())[:12]
trigger = {
"id": trigger_id,
"remote": remote,
"action": action,
"parameters": parameters or {},
"status": "triggered",
"triggered_at": datetime.now().isoformat()
}
trigger_log = _load_triggers()
trigger_log["triggers"].append(trigger)
_save_triggers(trigger_log)
return ToolResult(success=True, data={
"trigger_id": trigger_id,
"remote": remote,
"action": action,
"status": "triggered",
"triggered_at": trigger["triggered_at"],
"note": f"Action '{action}' triggered on '{remote}'"
})
class RemoteRemoveTool(BaseTool):
"""Remove a remote agent."""
name = "remote_remove"
description = "Remove a remote agent configuration"
input_schema = {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Remote agent name to remove"
}
},
"required": ["name"]
}
async def execute(self, name: str) -> ToolResult:
"""Remove remote."""
data = _load_remotes()
if name not in data["remotes"]:
return ToolResult(success=False, error=f"Remote '{name}' not found")
del data["remotes"][name]
_save_remotes(data)
return ToolResult(success=True, data={
"remote": name,
"status": "removed"
})
# Register tools
tool_registry.register(RemoteAddTool())
tool_registry.register(RemoteListTool())
tool_registry.register(RemoteTriggerTool())
tool_registry.register(RemoteRemoveTool())
|