Skip to content

Commit

Permalink
Fixes: gpt often didn't respect relative paths, and forgot where it w…
Browse files Browse the repository at this point in the history
…as. 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
  • Loading branch information
Aman Rusia committed Oct 26, 2024
1 parent bf9a03c commit 0170079
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
9 changes: 4 additions & 5 deletions gpt_instructions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int code>)` 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.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
authors = [{ name = "Aman Rusia", email = "[email protected]" }]
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"
Expand Down Expand Up @@ -41,4 +41,4 @@ dev-dependencies = [
"types-toml>=0.10.8.20240310",
"autoflake",
"ipython>=8.12.3",
]
]
18 changes: 10 additions & 8 deletions src/wcgw/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int code>)` 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 <int code>)` 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,
Expand Down
8 changes: 7 additions & 1 deletion src/wcgw/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0170079

Please sign in to comment.