From 0170079b13379705c9946d954b3c75f7322427e1 Mon Sep 17 00:00:00 2001 From: Aman Rusia Date: Sat, 26 Oct 2024 16:23:54 +0530 Subject: [PATCH] Fixes: gpt often didn't respect relative paths, and forgot where it was. Restricting only absolute paths in write file helps with that. Major fix in bash tool: venv source or other scripts could change PS1 and/or echo properties causing failure --- gpt_instructions.txt | 9 ++++----- pyproject.toml | 4 ++-- src/wcgw/basic.py | 18 ++++++++++-------- src/wcgw/tools.py | 8 +++++++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/gpt_instructions.txt b/gpt_instructions.txt index 9004baa..73d27c8 100644 --- a/gpt_instructions.txt +++ b/gpt_instructions.txt @@ -10,23 +10,22 @@ Instructions: To execute bash commands OR write files use the provided api `wcgw.arcfu.com` Instructions for `Execute Bash`: -- Execute a bash script. This is stateful (beware with subsequent calls) -- if you do `cd`, current directory will change for subsequent calls. +- Execute a bash script. This is stateful (beware with subsequent calls). - Execute commands using `execute_command` attribute. - Do not use interactive commands like nano. Prefer writing simpler commands. - Last line will always be `(exit )` except if -- The last line is `(pending)` if the program is still running or waiting for your input. You can then send input using `send_ascii` attributes. You get status by sending new line `send_ascii: [10]`. +- The last line is `(pending)` if the program is still running or waiting for your input. You can then send input using `send_ascii` attributes. You get status by sending new line `send_ascii: ["Enter"]` or `send_ascii: [10]`. - Optionally the last line is `(won't exit)` in which case you need to kill the process if you want to run a new command. - Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands. - The first line might be `(...truncated)` if the output is too long. +- Always run `pwd` if you get any file or directory not found error to make sure you're not lost. Instructions for `Write File` - Write content to a file. Provide file path and content. Use this instead of ExecuteBash for writing files. - - --- Always critically think and debate with yourself to solve the problem. Understand the context and the code by reading as much resources as possible before writing a single piece of code. --- -Ask the user for the user_id `UUID` if they haven't provided in the first message. +Ask the user for the user_id `UUID` if they haven't provided in the first message. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 9365123..91df84d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] authors = [{ name = "Aman Rusia", email = "gapypi@arcfu.com" }] name = "wcgw" -version = "0.0.10" +version = "0.1.0" description = "What could go wrong giving full shell access to chatgpt?" readme = "README.md" requires-python = ">=3.10, <3.13" @@ -41,4 +41,4 @@ dev-dependencies = [ "types-toml>=0.10.8.20240310", "autoflake", "ipython>=8.12.3", -] \ No newline at end of file +] diff --git a/src/wcgw/basic.py b/src/wcgw/basic.py index 9b27c45..9fc36e2 100644 --- a/src/wcgw/basic.py +++ b/src/wcgw/basic.py @@ -158,14 +158,16 @@ def loop( openai.pydantic_function_tool( ExecuteBash, description=""" -Execute a bash script. Stateful (beware with subsequent calls). -Execute commands using `execute_command` attribute. -Do not use interactive commands like nano. Prefer writing simpler commands. -Last line will always be `(exit )` except if -the last line is `(pending)` if the program is still running or waiting for user inputs. You can then send input using `send_ascii` attributes. You get status by sending `send_ascii: [10]`. -Optionally the last line is `(won't exit)` in which case you need to kill the process if you want to run a new command. -Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands. -The first line might be `(...truncated)` if the output is too long.""", +- Execute a bash script. This is stateful (beware with subsequent calls). +- Execute commands using `execute_command` attribute. +- Do not use interactive commands like nano. Prefer writing simpler commands. +- Last line will always be `(exit )` except if +- The last line is `(pending)` if the program is still running or waiting for your input. You can then send input using `send_ascii` attributes. You get status by sending new line `send_ascii: ["Enter"]` or `send_ascii: [10]`. +- Optionally the last line is `(won't exit)` in which case you need to kill the process if you want to run a new command. +- Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands. +- The first line might be `(...truncated)` if the output is too long. +- Always run `pwd` if you get any file or directory not found error to make sure you're not lost. +""", ), openai.pydantic_function_tool( Writefile, diff --git a/src/wcgw/tools.py b/src/wcgw/tools.py index 0379f84..8771529 100644 --- a/src/wcgw/tools.py +++ b/src/wcgw/tools.py @@ -103,6 +103,12 @@ def _is_int(mystr: str) -> bool: def _get_exit_code() -> int: + # First reset the prompt in case venv was sourced or other reasons. + SHELL.sendline('export PS1="#@@"') + SHELL.expect("#@@") + # Reset echo also if it was enabled + SHELL.sendline("stty -icanon -echo") + SHELL.expect("#@@") SHELL.sendline("echo $?") before = "" while not _is_int(before): # Consume all previous output @@ -299,7 +305,7 @@ def write_file(writefile: Writefile) -> str: SHELL.expect("#@@") assert isinstance(SHELL.before, str) current_dir = SHELL.before.strip() - writefile.file_path = os.path.join(current_dir, writefile.file_path) + return f"Failure: Use absolute path only. FYI current working directory is '{current_dir}'" os.makedirs(os.path.dirname(writefile.file_path), exist_ok=True) try: with open(writefile.file_path, "w") as f: