Skip to content

Commit

Permalink
fix: minor misc command fixes, improved integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 26, 2023
1 parent c23a5ce commit f77a34a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dist
*.cast
*.png
prof
tests/output
6 changes: 5 additions & 1 deletion gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ def loop(
# If last message was from the user (such as from crash/edited log),
# then skip asking for input and generate response
last_msg = log[-1] if log else None
if not last_msg or (last_msg.role in ["assistant"]):
if (
not last_msg
or (last_msg.role in ["assistant"])
or last_msg.content == "Interrupted"
):
inquiry = prompt_user()
if not inquiry:
# Empty command, ask for input again
Expand Down
11 changes: 6 additions & 5 deletions gptme/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import sleep
from typing import Generator, Literal

from .constants import CMDFIX
from .logmanager import LogManager
from .message import (
Message,
Expand All @@ -15,7 +16,6 @@
from .tools.context import _gen_context_msg
from .tools.summarize import summarize
from .tools.useredit import edit_text_with_editor
from .constants import CMDFIX

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -91,9 +91,11 @@ def handle_cmd(
log.undo(1, quiet=True)
log.print(show_hidden="--hidden" in args)
case "rename":
log.undo(1, quiet=True)
# rename the conversation
new_name = args[0] if args else input("New name: ")
log.rename(new_name)
print(f"Renamed conversation to {new_name}")
case "fork":
# fork the conversation
new_name = args[0] if args else input("New name: ")
Expand All @@ -105,9 +107,7 @@ def handle_cmd(
print(f"Summary: {summary}")
case "edit":
# edit previous messages

# first undo the '/edit' command itself
assert log.log[-1].content == f"{CMDFIX}edit"
log.undo(1, quiet=True)

# generate editable toml of all messages
Expand All @@ -128,10 +128,10 @@ def handle_cmd(
log.write()
# now we need to redraw the log so the user isn't seeing stale messages in their buffer
# log.print()
logger.info("Applied edited messages")
print("Applied edited messages, write /log to see the result")
case "context":
# print context msg
print(_gen_context_msg())
yield _gen_context_msg()
case "undo":
# undo the '/undo' command itself
log.undo(1, quiet=True)
Expand Down Expand Up @@ -179,6 +179,7 @@ def handle_cmd(
print("Unknown command")
# undo the '/help' command itself
log.undo(1, quiet=True)
log.write()

print("Available commands:")
for cmd, desc in action_descriptions.items():
Expand Down
1 change: 1 addition & 0 deletions gptme/logmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def get_last_code_block(self) -> str | None:
def rename(self, name: str) -> None:
# rename the conversation and log file
# if you want to keep the old log, use fork()
(LOGSDIR / name).mkdir(parents=True, exist_ok=True)
self.logfile.rename(LOGSDIR / name / "conversation.jsonl")
self.logfile = LOGSDIR / name / "conversation.jsonl"

Expand Down
3 changes: 2 additions & 1 deletion gptme/tools/useredit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tool that lets the user edit something in a temporary file using their $EDITOR.
"""

import logging
import os
import subprocess
Expand All @@ -20,7 +21,7 @@ def edit_text_with_editor(initial_text: str, ext=None) -> str:
editor = os.environ.get("EDITOR", "nano")

# Open the file in the user's editor.
print("Running editor:", [editor, temp_filename])
logger.debug("Running editor:", [editor, temp_filename])
p = subprocess.run([editor, temp_filename])
# now, we wait

Expand Down
18 changes: 16 additions & 2 deletions tests/test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@
set -e
set -x

# set pwd to the output directory under this script
cd "$(dirname "$0")"
mkdir -p output
cd output

# set this to indicate tests are run (non-interactive)
export PYTEST_CURRENT_TEST=1

# test stdin and cli-provided prompt
echo "The project mascot is a flying pig" | gptme "What is the project mascot?"

# test python command
gptme ".python print('hello world')"
gptme "/python print('hello world')"

# test shell command
gptme ".shell echo 'hello world'"
gptme "/shell echo 'hello world'"

# interactive matplotlib
gptme 'plot an x^2 graph'

# matplotlib to file
gptme 'plot up to the 3rd degree taylor expansion of sin(x), save to sin.png'

# interactive curses
gptme 'write a snake game with curses'

0 comments on commit f77a34a

Please sign in to comment.