mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-19 23:14:25 +01:00
Don't incapacitate yourself! (#1240)
* subprocesses * fix lint * fix more lint * fix merge * fix merge again
This commit is contained in:
@@ -10,7 +10,11 @@ from autogpt.config import Config
|
||||
from autogpt.commands.image_gen import generate_image
|
||||
from autogpt.commands.audio_text import read_audio_from_file
|
||||
from autogpt.commands.web_requests import scrape_links, scrape_text
|
||||
from autogpt.commands.execute_code import execute_python_file, execute_shell
|
||||
from autogpt.commands.execute_code import (
|
||||
execute_python_file,
|
||||
execute_shell,
|
||||
execute_shell_popen,
|
||||
)
|
||||
from autogpt.commands.file_operations import (
|
||||
append_to_file,
|
||||
delete_file,
|
||||
@@ -191,6 +195,15 @@ def execute_command(command_name: str, arguments):
|
||||
" shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' "
|
||||
"in your config. Do not attempt to bypass the restriction."
|
||||
)
|
||||
elif command_name == "execute_shell_popen":
|
||||
if CFG.execute_local_commands:
|
||||
return execute_shell_popen(arguments["command_line"])
|
||||
else:
|
||||
return (
|
||||
"You are not allowed to run local shell commands. To execute"
|
||||
" shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' "
|
||||
"in your config. Do not attempt to bypass the restriction."
|
||||
)
|
||||
elif command_name == "read_audio_from_file":
|
||||
return read_audio_from_file(arguments["file"])
|
||||
elif command_name == "generate_image":
|
||||
|
||||
@@ -114,6 +114,36 @@ def execute_shell(command_line: str) -> str:
|
||||
return output
|
||||
|
||||
|
||||
def execute_shell_popen(command_line):
|
||||
"""Execute a shell command with Popen and returns an english description
|
||||
of the event and the process id
|
||||
|
||||
Args:
|
||||
command_line (str): The command line to execute
|
||||
|
||||
Returns:
|
||||
str: Description of the fact that the process started and its id
|
||||
"""
|
||||
current_dir = os.getcwd()
|
||||
|
||||
if WORKING_DIRECTORY not in current_dir: # Change dir into workspace if necessary
|
||||
work_dir = os.path.join(os.getcwd(), WORKING_DIRECTORY)
|
||||
os.chdir(work_dir)
|
||||
|
||||
print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'")
|
||||
|
||||
do_not_show_output = subprocess.DEVNULL
|
||||
process = subprocess.Popen(
|
||||
command_line, shell=True, stdout=do_not_show_output, stderr=do_not_show_output
|
||||
)
|
||||
|
||||
# Change back to whatever the prior working dir was
|
||||
|
||||
os.chdir(current_dir)
|
||||
|
||||
return f"Subprocess started with PID:'{str(process.pid)}'"
|
||||
|
||||
|
||||
def we_are_running_in_a_docker_container() -> bool:
|
||||
"""Check if we are running in a Docker container
|
||||
|
||||
|
||||
@@ -38,6 +38,9 @@ def get_prompt() -> str:
|
||||
prompt_generator.add_constraint(
|
||||
'Exclusively use the commands listed in double quotes e.g. "command name"'
|
||||
)
|
||||
prompt_generator.add_constraint(
|
||||
"Use subprocesses for commands that will not terminate within a few minutes"
|
||||
)
|
||||
|
||||
# Define the command list
|
||||
commands = [
|
||||
@@ -81,6 +84,7 @@ def get_prompt() -> str:
|
||||
{"code": "<full_code_string>", "focus": "<list_of_focus_areas>"},
|
||||
),
|
||||
("Execute Python File", "execute_python_file", {"file": "<file>"}),
|
||||
("Task Complete (Shutdown)", "task_complete", {"reason": "<reason>"}),
|
||||
("Generate Image", "generate_image", {"prompt": "<prompt>"}),
|
||||
("Send Tweet", "send_tweet", {"text": "<text>"}),
|
||||
]
|
||||
@@ -104,6 +108,13 @@ def get_prompt() -> str:
|
||||
{"command_line": "<command_line>"},
|
||||
),
|
||||
)
|
||||
commands.append(
|
||||
(
|
||||
"Execute Shell Command Popen, non-interactive commands only",
|
||||
"execute_shell_popen",
|
||||
{"command_line": "<command_line>"}
|
||||
),
|
||||
)
|
||||
|
||||
# Only add the download file command if the AI is allowed to execute it
|
||||
if cfg.allow_downloads:
|
||||
|
||||
Reference in New Issue
Block a user