Skip to content

Commit

Permalink
fix: support piping stdin into gptme -r (resume) (#344)
Browse files Browse the repository at this point in the history
* support piping stdin into gptme -r (resume)

* clean code

---------

Co-authored-by: Jamesb <[email protected]>
  • Loading branch information
bjsi and Jamesb authored Dec 18, 2024
1 parent 803d6e5 commit 621ffd8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gptme/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ def confirm_func(msg) -> bool:
if prompt_msgs:
while prompt_msgs:
msg = prompt_msgs.pop(0)
if not msg.content.startswith("/"):
if not msg.content.startswith("/") and msg.role == "user":
msg = _include_paths(msg, workspace)
manager.append(msg)
# if prompt is a user-command, execute it
if execute_cmd(msg, manager, confirm_func):
if msg.role == "user" and execute_cmd(msg, manager, confirm_func):
continue

# Generate and execute response for this prompt
Expand Down
11 changes: 7 additions & 4 deletions gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,11 @@ def main(

# if stdin is not a tty, we might be getting piped input, which we should include in the prompt
was_piped = False
piped_input = None
if not sys.stdin.isatty():
# fetch prompt from stdin
prompt_stdin = _read_stdin()
if prompt_stdin:
# TODO: also append if existing convo loaded/resumed
initial_msgs += [Message("system", f"```stdin\n{prompt_stdin}\n```")]
piped_input = _read_stdin()
if piped_input:
was_piped = True

# Attempt to switch to interactive mode
Expand Down Expand Up @@ -241,6 +240,8 @@ def main(

if resume:
logdir = get_logdir_resume()
if piped_input:
prompt_msgs.append(Message("system", f"```stdin\n{piped_input}\n```"))
# don't run pick in tests/non-interactive mode, or if the user specifies a name
elif (
interactive
Expand All @@ -252,6 +253,8 @@ def main(
logdir = pick_log()
else:
logdir = get_logdir(name)
if piped_input:
initial_msgs.append(Message("system", f"```stdin\n{piped_input}\n```"))

if workspace == "@log":
workspace_path: Path | None = logdir / "workspace"
Expand Down

0 comments on commit 621ffd8

Please sign in to comment.