server-personification/pers/langchain/tools/bash.py

20 lines
715 B
Python

import subprocess
from langchain_core.tools import tool
from pers.langchain.tools.tools import PRINT_USAGE, _print_func_call
@tool
def run_bash(command: str, reasoning: str) -> dict[str, str | int]:
"""Execute a Bash command on the local system."""
# TODO: config option to block all commands with "sudo" in them.
if PRINT_USAGE:
_print_func_call('bash', {'command': command, 'reasoning': reasoning})
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
return_code = process.returncode
return {'stdout': stdout.decode('utf-8'), 'stderr': stderr.decode('utf-8'), 'return_code': return_code}