"""
Run command line tool
"""
import subprocess
def run_shell_command(command: str, safe_mode: bool = True):
"""Execute shell commands (use with caution)."""
if safe_mode:
# Whitelist of safe commands
safe_commands = ['ls', 'pwd', 'date', 'whoami', 'df', 'free', 'uptime']
cmd_start = command.split()[0] if command.split() else ""
if cmd_start not in safe_commands:
return {"error": f"Command '{cmd_start}' not allowed in safe mode"}
try:
result = subprocess.run(
command.split(),
capture_output=True,
text=True,
timeout=10
)
return {
"command": command,
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode
}
except Exception as e:
return {"error": f"Command execution failed: {str(e)}"}
TOOL_INFO = {
"name": "run_shell_command",
"description": "Run whitelisted command line prompts",
"function": run_shell_command,
"parameters": {
"command": {
"type": "str",
"required": True,
"description": "CLI Commands to run"
},
"safe_mode": {
"type": "boolean",
"required": False,
"description": "Turns off safe mode and allows for any cmd commands"
}
}
}