File size: 12,208 Bytes
67b6ab0 | 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | import json
import os
import subprocess
import sys
from datetime import datetime, timezone
from helpers.api import ApiHandler, Request, Response
from helpers import plugins, files, extension
class Plugins(ApiHandler):
"""
Core plugin management API.
Actions: get_config, save_config
"""
async def process(self, input: dict, request: Request) -> dict | Response:
action = input.get("action", "")
if action == "get_config":
return self._get_config(input)
if action == "get_toggle_status":
return self._get_toggle_status(input)
if action == "list_configs":
return self._list_configs(input)
if action == "delete_config":
return self._delete_config(input)
if action == "delete_plugin":
return self._delete_plugin(input)
if action == "get_default_config":
return self._get_default_config(input)
if action == "save_config":
return self._save_config(input)
if action == "toggle_plugin":
return self._toggle_plugin(input)
if action == "get_doc":
return self._get_doc(input)
if action == "run_execute_script":
return self._run_execute_script(input)
if action == "get_execute_record":
return self._get_execute_record(input)
return Response(status=400, response=f"Unknown action: {action}")
@extension.extensible
def _get_config(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
project_name = input.get("project_name", "")
agent_profile = input.get("agent_profile", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
result = plugins.find_plugin_assets(
plugins.CONFIG_FILE_NAME,
plugin_name=plugin_name,
project_name=project_name,
agent_profile=agent_profile,
only_first=True,
)
if result:
entry = result[0]
path = entry.get("path", "")
settings = files.read_file_json(path) if path else {}
loaded_project_name = entry.get("project_name", "")
loaded_agent_profile = entry.get("agent_profile", "")
else:
settings = plugins.get_plugin_config(plugin_name, agent=None) or {}
default_path = files.get_abs_path(
plugins.find_plugin_dir(plugin_name), plugins.CONFIG_DEFAULT_FILE_NAME
)
path = default_path if files.exists(default_path) else ""
loaded_project_name = ""
loaded_agent_profile = ""
return {
"ok": True,
"loaded_path": path,
"loaded_project_name": loaded_project_name,
"loaded_agent_profile": loaded_agent_profile,
"data": settings,
}
@extension.extensible
def _get_toggle_status(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
project_name = input.get("project_name", "")
agent_profile = input.get("agent_profile", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
meta = plugins.get_plugin_meta(plugin_name)
if not meta:
return Response(status=404, response="Plugin not found")
if meta.always_enabled:
return {
"ok": True,
"status": "enabled",
"loaded_project_name": project_name,
"loaded_agent_profile": agent_profile,
"loaded_path": "",
}
result = plugins.find_plugin_assets(
plugins.TOGGLE_FILE_PATTERN,
plugin_name=plugin_name,
project_name=project_name,
agent_profile=agent_profile,
only_first=True,
)
if result:
entry = result[0]
path = entry.get("path", "")
status = (
"enabled" if path.endswith(plugins.ENABLED_FILE_NAME) else "disabled"
)
return {
"ok": True,
"status": status,
"loaded_project_name": entry.get("project_name", ""),
"loaded_agent_profile": entry.get("agent_profile", ""),
"loaded_path": path,
}
return {
"ok": True,
"status": "enabled",
"loaded_project_name": "",
"loaded_agent_profile": "",
"loaded_path": "",
}
@extension.extensible
def _list_configs(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
asset_type = input.get("asset_type", "config")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
configs = plugins.find_plugin_assets(
(
plugins.CONFIG_FILE_NAME
if asset_type == "config"
else plugins.TOGGLE_FILE_PATTERN
),
plugin_name=plugin_name,
project_name="*",
agent_profile="*",
only_first=False,
)
return {"ok": True, "data": configs}
@extension.extensible
def _delete_config(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
path = input.get("path", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
if not path:
return Response(status=400, response="Missing path")
configs = plugins.find_plugin_assets(
plugins.CONFIG_FILE_NAME,
plugin_name=plugin_name,
project_name="*",
agent_profile="*",
only_first=False,
)
toggles = plugins.find_plugin_assets(
plugins.TOGGLE_FILE_PATTERN,
plugin_name=plugin_name,
project_name="*",
agent_profile="*",
only_first=False,
)
allowed_paths = {c.get("path", "") for c in configs + toggles}
if path not in allowed_paths:
return Response(status=400, response="Invalid path")
if not files.exists(path):
return {"ok": True}
try:
os.remove(path)
except Exception as e:
return Response(status=500, response=f"Failed to delete config: {str(e)}")
return {"ok": True}
@extension.extensible
def _delete_plugin(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
try:
plugins.uninstall_plugin(plugin_name)
except FileNotFoundError as e:
return Response(status=404, response=str(e))
except ValueError as e:
return Response(status=400, response=str(e))
except Exception as e:
return Response(status=500, response=f"Failed to delete plugin: {str(e)}")
return {"ok": True}
@extension.extensible
def _get_default_config(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
settings = plugins.get_default_plugin_config(plugin_name)
return {"ok": True, "data": settings or {}}
@extension.extensible
def _save_config(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
project_name = input.get("project_name", "")
agent_profile = input.get("agent_profile", "")
settings = input.get("settings", {})
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
if not isinstance(settings, dict):
return Response(status=400, response="settings must be an object")
plugins.save_plugin_config(plugin_name, project_name, agent_profile, settings)
return {"ok": True}
@extension.extensible
def _toggle_plugin(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
enabled = input.get("enabled")
project_name = input.get("project_name", "")
agent_profile = input.get("agent_profile", "")
clear_overrides = bool(input.get("clear_overrides", False))
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
if enabled is None:
return Response(status=400, response="Missing enabled state")
plugins.toggle_plugin(
plugin_name, bool(enabled), project_name, agent_profile, clear_overrides
)
return {"ok": True}
@extension.extensible
def _get_doc(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
doc = input.get("doc", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
if doc not in ("readme", "license"):
return Response(status=400, response="doc must be 'readme' or 'license'")
plugin_dir = plugins.find_plugin_dir(plugin_name)
if not plugin_dir:
return Response(status=404, response="Plugin not found")
filename = "README.md" if doc == "readme" else "LICENSE"
file_path = files.get_abs_path(plugin_dir, filename)
if not files.exists(file_path):
return Response(status=404, response=f"{filename} not found")
return {"ok": True, "content": files.read_file(file_path), "filename": filename}
@extension.extensible
def _run_execute_script(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
plugin_dir = plugins.find_plugin_dir(plugin_name)
if not plugin_dir:
return Response(status=404, response="Plugin not found")
execute_script = files.get_abs_path(plugin_dir, "execute.py")
if not files.exists(execute_script):
return Response(status=404, response="execute.py not found")
executed_at = datetime.now(timezone.utc).isoformat()
try:
result = subprocess.run(
[sys.executable, execute_script],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
cwd=plugin_dir,
timeout=120,
)
exit_code = result.returncode
output = result.stdout or ""
except subprocess.TimeoutExpired:
exit_code = -1
output = "Error: script timed out after 120 seconds"
except Exception as e:
exit_code = -1
output = f"Error: {str(e)}"
execute_record = {"executed_at": executed_at, "exit_code": exit_code}
execute_record_path = plugins.determine_plugin_asset_path(
plugin_name, "", "", "execute_record.json"
)
if execute_record_path:
files.write_file(execute_record_path, json.dumps(execute_record))
return {
"ok": exit_code == 0,
"output": output,
"exit_code": exit_code,
"executed_at": executed_at,
}
@extension.extensible
def _get_execute_record(self, input: dict) -> dict | Response:
plugin_name = input.get("plugin_name", "")
if not plugin_name:
return Response(status=400, response="Missing plugin_name")
execute_record_path = plugins.determine_plugin_asset_path(
plugin_name, "", "", "execute_record.json"
)
if execute_record_path and files.exists(execute_record_path):
try:
data = json.loads(files.read_file(execute_record_path))
return {"ok": True, "data": data}
except Exception:
pass
return {"ok": True, "data": None}
|