Skip to content

Commit

Permalink
Don't incapacitate yourself! (Significant-Gravitas#1240)
Browse files Browse the repository at this point in the history
* subprocesses

* fix lint

* fix more lint

* fix merge

* fix merge again
  • Loading branch information
lfricken authored Apr 17, 2023
1 parent d47466d commit d4860fe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion autogpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
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,
Expand Down Expand Up @@ -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":
Expand Down
30 changes: 30 additions & 0 deletions autogpt/commands/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions autogpt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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>"}),
]
Expand All @@ -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:
Expand Down

0 comments on commit d4860fe

Please sign in to comment.