Skip to content

Commit f77a34a

Browse files
committed
fix: minor misc command fixes, improved integration tests
1 parent c23a5ce commit f77a34a

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist
1010
*.cast
1111
*.png
1212
prof
13+
tests/output

gptme/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ def loop(
262262
# If last message was from the user (such as from crash/edited log),
263263
# then skip asking for input and generate response
264264
last_msg = log[-1] if log else None
265-
if not last_msg or (last_msg.role in ["assistant"]):
265+
if (
266+
not last_msg
267+
or (last_msg.role in ["assistant"])
268+
or last_msg.content == "Interrupted"
269+
):
266270
inquiry = prompt_user()
267271
if not inquiry:
268272
# Empty command, ask for input again

gptme/commands.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import sleep
55
from typing import Generator, Literal
66

7+
from .constants import CMDFIX
78
from .logmanager import LogManager
89
from .message import (
910
Message,
@@ -15,7 +16,6 @@
1516
from .tools.context import _gen_context_msg
1617
from .tools.summarize import summarize
1718
from .tools.useredit import edit_text_with_editor
18-
from .constants import CMDFIX
1919

2020
logger = logging.getLogger(__name__)
2121

@@ -91,9 +91,11 @@ def handle_cmd(
9191
log.undo(1, quiet=True)
9292
log.print(show_hidden="--hidden" in args)
9393
case "rename":
94+
log.undo(1, quiet=True)
9495
# rename the conversation
9596
new_name = args[0] if args else input("New name: ")
9697
log.rename(new_name)
98+
print(f"Renamed conversation to {new_name}")
9799
case "fork":
98100
# fork the conversation
99101
new_name = args[0] if args else input("New name: ")
@@ -105,9 +107,7 @@ def handle_cmd(
105107
print(f"Summary: {summary}")
106108
case "edit":
107109
# edit previous messages
108-
109110
# first undo the '/edit' command itself
110-
assert log.log[-1].content == f"{CMDFIX}edit"
111111
log.undo(1, quiet=True)
112112

113113
# generate editable toml of all messages
@@ -128,10 +128,10 @@ def handle_cmd(
128128
log.write()
129129
# now we need to redraw the log so the user isn't seeing stale messages in their buffer
130130
# log.print()
131-
logger.info("Applied edited messages")
131+
print("Applied edited messages, write /log to see the result")
132132
case "context":
133133
# print context msg
134-
print(_gen_context_msg())
134+
yield _gen_context_msg()
135135
case "undo":
136136
# undo the '/undo' command itself
137137
log.undo(1, quiet=True)
@@ -179,6 +179,7 @@ def handle_cmd(
179179
print("Unknown command")
180180
# undo the '/help' command itself
181181
log.undo(1, quiet=True)
182+
log.write()
182183

183184
print("Available commands:")
184185
for cmd, desc in action_descriptions.items():

gptme/logmanager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def get_last_code_block(self) -> str | None:
135135
def rename(self, name: str) -> None:
136136
# rename the conversation and log file
137137
# if you want to keep the old log, use fork()
138+
(LOGSDIR / name).mkdir(parents=True, exist_ok=True)
138139
self.logfile.rename(LOGSDIR / name / "conversation.jsonl")
139140
self.logfile = LOGSDIR / name / "conversation.jsonl"
140141

gptme/tools/useredit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Tool that lets the user edit something in a temporary file using their $EDITOR.
33
"""
4+
45
import logging
56
import os
67
import subprocess
@@ -20,7 +21,7 @@ def edit_text_with_editor(initial_text: str, ext=None) -> str:
2021
editor = os.environ.get("EDITOR", "nano")
2122

2223
# Open the file in the user's editor.
23-
print("Running editor:", [editor, temp_filename])
24+
logger.debug("Running editor:", [editor, temp_filename])
2425
p = subprocess.run([editor, temp_filename])
2526
# now, we wait
2627

tests/test-integration.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
set -e
44
set -x
55

6+
# set pwd to the output directory under this script
7+
cd "$(dirname "$0")"
8+
mkdir -p output
9+
cd output
10+
611
# set this to indicate tests are run (non-interactive)
712
export PYTEST_CURRENT_TEST=1
813

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

1217
# test python command
13-
gptme ".python print('hello world')"
18+
gptme "/python print('hello world')"
1419

1520
# test shell command
16-
gptme ".shell echo 'hello world'"
21+
gptme "/shell echo 'hello world'"
22+
23+
# interactive matplotlib
24+
gptme 'plot an x^2 graph'
25+
26+
# matplotlib to file
27+
gptme 'plot up to the 3rd degree taylor expansion of sin(x), save to sin.png'
28+
29+
# interactive curses
30+
gptme 'write a snake game with curses'

0 commit comments

Comments
 (0)