Repository navigation
Exp 8000 and file tree
/
Copy pathbash.py
More file actions
38 lines (34 loc) · 1.41 KB
/
Copy pathbash.py
File metadata and controls
38 lines (34 loc) · 1.41 KB
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
import subprocess
from .base import Tool, ToolResult
_DEFAULT_TIMEOUT = 120
class BashTool(Tool):
name = "Bash"
description = (
"Execute a bash command. Returns stdout + stderr. "
"Timeout defaults to 120s. Avoid interactive commands."
)
input_schema = {
"type": "object",
"properties": {
"command": {"type": "string", "description": "The bash command to execute"},
"timeout": {"type": "integer", "description": "Timeout in seconds", "default": 120},
},
"required": ["command"],
}
def execute(self, command: str, timeout: int = _DEFAULT_TIMEOUT) -> ToolResult:
try:
result = subprocess.run(
command, shell=True, capture_output=True, text=True, timeout=timeout
)
parts = []
if result.stdout:
parts.append(result.stdout.rstrip())
if result.stderr:
parts.append(f"[stderr]\n{result.stderr.rstrip()}")
if result.returncode != 0:
parts.append(f"[exit code: {result.returncode}]")
return ToolResult(content="\n".join(parts) if parts else "(no output)")
except subprocess.TimeoutExpired:
return ToolResult(content=f"Error: Command timed out after {timeout}s", is_error=True)
except Exception as e:
return ToolResult(content=f"Error: {e}", is_error=True)