| from __future__ import annotations | |
| import json | |
| import os | |
| import shutil | |
| import subprocess | |
| import tempfile | |
| from typing import Any, Dict, List | |
| from .registry import ToolRegistry | |
| def _write_temp(content: str, suffix: str) -> str: | |
| fd, path = tempfile.mkstemp(suffix=suffix) | |
| with os.fdopen(fd, "w") as f: | |
| f.write(content) | |
| return path | |
| def t_bash_run(args: Dict[str, Any]) -> str: | |
| script = args.get("code") or args.get("script") | |
| if not isinstance(script, str) or not script: | |
| return json.dumps({"error": "code/script required"}) | |
| path = _write_temp(script, ".sh") | |
| try: | |
| proc = subprocess.run(["bash", path], capture_output=True, text=True, timeout=int(args.get("timeout", 300))) | |
| return json.dumps({"returncode": proc.returncode, "stdout": proc.stdout[-8000:], "stderr": proc.stderr[-8000:]}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def t_node_run(args: Dict[str, Any]) -> str: | |
| code = args.get("code") | |
| if not isinstance(code, str) or not code: | |
| return json.dumps({"error": "code required"}) | |
| if not shutil.which("node"): | |
| return json.dumps({"error": "node not installed"}) | |
| path = _write_temp(code, ".mjs") | |
| try: | |
| proc = subprocess.run(["node", path], capture_output=True, text=True, timeout=int(args.get("timeout", 300))) | |
| return json.dumps({"returncode": proc.returncode, "stdout": proc.stdout[-8000:], "stderr": proc.stderr[-8000:]}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def t_pip_install(args: Dict[str, Any]) -> str: | |
| pkgs: List[str] = args.get("packages") or [] | |
| if not pkgs: | |
| return json.dumps({"error": "packages required"}) | |
| try: | |
| proc = subprocess.run(["pip", "install", "--no-cache-dir", *pkgs], capture_output=True, text=True, timeout=int(args.get("timeout", 1800))) | |
| return json.dumps({"returncode": proc.returncode, "stdout": proc.stdout[-4000:], "stderr": proc.stderr[-4000:]}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def t_npm_install(args: Dict[str, Any]) -> str: | |
| pkgs: List[str] = args.get("packages") or [] | |
| cwd = args.get("cwd") or None | |
| if not pkgs: | |
| return json.dumps({"error": "packages required"}) | |
| if not shutil.which("npm"): | |
| return json.dumps({"error": "npm not installed"}) | |
| try: | |
| proc = subprocess.run(["npm", "install", *pkgs], cwd=cwd, capture_output=True, text=True, timeout=int(args.get("timeout", 1800))) | |
| return json.dumps({"returncode": proc.returncode, "stdout": proc.stdout[-4000:], "stderr": proc.stderr[-4000:]}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def register_tools(reg: ToolRegistry) -> None: | |
| reg.register( | |
| name="bash_run", | |
| description="Run a Bash script snippet.", | |
| parameters={"type": "object", "properties": {"code": {"type": "string"}, "timeout": {"type": "integer"}}, "required": ["code"]}, | |
| handler=t_bash_run, | |
| ) | |
| reg.register( | |
| name="node_run", | |
| description="Run a Node.js snippet.", | |
| parameters={"type": "object", "properties": {"code": {"type": "string"}, "timeout": {"type": "integer"}}, "required": ["code"]}, | |
| handler=t_node_run, | |
| ) | |
| reg.register( | |
| name="pip_install", | |
| description="Install Python packages via pip.", | |
| parameters={"type": "object", "properties": {"packages": {"type": "array", "items": {"type": "string"}}, "timeout": {"type": "integer"}}, "required": ["packages"]}, | |
| handler=t_pip_install, | |
| ) | |
| reg.register( | |
| name="npm_install", | |
| description="Install Node packages via npm.", | |
| parameters={"type": "object", "properties": {"packages": {"type": "array", "items": {"type": "string"}}, "cwd": {"type": "string"}, "timeout": {"type": "integer"}}, "required": ["packages"]}, | |
| handler=t_npm_install, | |
| ) | |