30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
import subprocess
|
|
from .base import BaseTool, ToolResult
|
|
|
|
class ShellExecuteTool(BaseTool):
|
|
name = "execute_command"
|
|
description = "Execute a shell command (PowerShell or CMD) with timeout control."
|
|
|
|
def execute(self, command: str, cwd: str = ".", timeout_seconds: int = 60) -> ToolResult:
|
|
try:
|
|
result = subprocess.run(
|
|
command,
|
|
shell=True,
|
|
cwd=cwd,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout_seconds
|
|
)
|
|
output = result.stdout
|
|
error = result.stderr
|
|
if result.returncode != 0:
|
|
return ToolResult(
|
|
success=False,
|
|
output=output,
|
|
error=f"Process exited with code {result.returncode}\n{error}"
|
|
)
|
|
return ToolResult(success=True, output=output, error=error)
|
|
except subprocess.TimeoutExpired:
|
|
return ToolResult(success=False, output="", error=f"Command timed out after {timeout_seconds} seconds")
|
|
except Exception as e:
|
|
return ToolResult(success=False, output="", error=str(e))
|