Skip to content

Commit

Permalink
fix: minor fixes to shell tool
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 6, 2023
1 parent c75c51b commit a13fb94
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def _gen_context_msg() -> Message:
shell = get_shell()
msgstr = ""

_, pwd, _ = shell.run_command("echo pwd: $(pwd)")
_, pwd, _ = shell.run_command("pwd")
msgstr += f"$ pwd\n{pwd.strip()}\n"

ret, git, _ = shell.run_command("git status -s")
Expand Down
13 changes: 5 additions & 8 deletions gptme/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import re
import select
import shlex
import subprocess
from typing import Generator

Expand All @@ -24,13 +23,11 @@ def __init__(self) -> None:
self.stderr_fd = self.process.stderr.fileno() # type: ignore
self.delimiter = "END_OF_COMMAND_OUTPUT"

def run_command(self, command: str | list[str]) -> tuple[int | None, str, str]:
def run_command(self, command: str) -> tuple[int | None, str, str]:
assert self.process.stdin
if isinstance(command, list):
command = " ".join(shlex.quote(arg) for arg in command)

full_command = f"{command}; echo ReturnCode:$?; echo {self.delimiter}"
self.process.stdin.write(full_command + "\n")
full_command = f"{command}; echo ReturnCode:$? {self.delimiter}\n"
self.process.stdin.write(full_command)
self.process.stdin.flush()

stdout = []
Expand All @@ -45,12 +42,12 @@ def run_command(self, command: str | list[str]) -> tuple[int | None, str, str]:
line = os.read(fd, 4096).decode("utf-8")
if "ReturnCode:" in line:
return_code_str = (
line.split("ReturnCode:")[1].split("\n")[0].strip()
line.split("ReturnCode:")[1].split(" ")[0].strip()
)
return_code = int(return_code_str)
if self.delimiter in line:
read_delimiter = True
line = line.replace(self.delimiter, "")
continue
if line:
stdout.append(line)
elif fd == self.stderr_fd:
Expand Down

0 comments on commit a13fb94

Please sign in to comment.