Skip to content

Commit c7301e5

Browse files
committed
fix: fixed behavior for keyboard interrupts and user commands
1 parent b6f337a commit c7301e5

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

gptme/chat.py

+21-29
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import re
55
import sys
66
import termios
7-
from typing import cast
87
import urllib.parse
98
from collections.abc import Generator
109
from pathlib import Path
10+
from typing import cast
1111

1212
from .commands import action_descriptions, execute_cmd
1313
from .config import get_config
@@ -19,12 +19,12 @@
1919
from .message import Message
2020
from .prompts import get_workspace_prompt
2121
from .tools import (
22+
ConfirmFunc,
2223
ToolFormat,
2324
ToolUse,
24-
has_tool,
25-
get_tools,
2625
execute_msg,
27-
ConfirmFunc,
26+
get_tools,
27+
has_tool,
2828
set_tool_format,
2929
)
3030
from .tools.browser import read_url
@@ -126,41 +126,31 @@ def confirm_func(msg) -> bool:
126126
if prompt_msgs:
127127
while prompt_msgs:
128128
msg = prompt_msgs.pop(0)
129-
if not msg.content.startswith("/") and msg.role == "user":
130-
msg = _include_paths(msg, workspace)
131-
manager.append(msg)
132129
# if prompt is a user-command, execute it
133-
if msg.role == "user" and execute_cmd(msg, manager, confirm_func):
130+
if execute_cmd(msg, manager, confirm_func):
134131
continue
132+
# else, collect paths
133+
msg = _include_paths(msg, workspace)
134+
manager.append(msg)
135135

136136
# Generate and execute response for this prompt
137137
while True:
138138
try:
139139
set_interruptible()
140-
response_msgs = list(
141-
step(
142-
manager.log,
143-
stream,
144-
confirm_func,
145-
tool_format=tool_format_with_default,
146-
workspace=workspace,
147-
)
148-
)
149-
except KeyboardInterrupt:
150-
console.log("Interrupted. Stopping current execution.")
151-
manager.append(Message("system", INTERRUPT_CONTENT))
152-
break
140+
for msg in step(
141+
manager.log,
142+
stream,
143+
confirm_func,
144+
tool_format=tool_format_with_default,
145+
workspace=workspace,
146+
):
147+
manager.append(msg)
148+
# run any user-commands, if msg is from user
149+
if execute_cmd(msg, manager, confirm_func):
150+
break
153151
finally:
154152
clear_interruptible()
155153

156-
for response_msg in response_msgs:
157-
manager.append(response_msg)
158-
# run any user-commands, if msg is from user
159-
if response_msg.role == "user" and execute_cmd(
160-
response_msg, manager, confirm_func
161-
):
162-
break
163-
164154
# Check if there are any runnable tools left
165155
last_content = next(
166156
(
@@ -229,6 +219,8 @@ def step(
229219
msg = Message("user", inquiry, quiet=True)
230220
msg = _include_paths(msg, workspace)
231221
yield msg
222+
if msg.content.startswith("/") and msg.role == "user":
223+
return
232224
log = log.append(msg)
233225

234226
# generate response and run tools

gptme/commands.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@
6060

6161
def execute_cmd(msg: Message, log: LogManager, confirm: ConfirmFunc) -> bool:
6262
"""Executes any user-command, returns True if command was executed."""
63-
assert msg.role == "user"
64-
65-
# if message starts with / treat as command
66-
# when command has been run,
67-
if msg.content[:1] in ["/"]:
63+
# if message is from user and starts with / treat as command
64+
if msg.role == "user" and msg.content.startswith("/"):
6865
for resp in handle_cmd(msg.content, log, confirm):
6966
log.append(resp)
7067
return True
@@ -156,19 +153,18 @@ def handle_cmd(
156153
# Export the chat
157154
export_chat_to_html(manager.name, manager.log, output_path)
158155
print(f"Exported conversation to {output_path}")
156+
case "help":
157+
# undo the '/help' command itself
158+
manager.undo(1, quiet=True)
159+
manager.write()
160+
help()
159161
case _:
160162
# the case for python, shell, and other block_types supported by tools
161163
tooluse = ToolUse(name, [], full_args)
162164
if tooluse.is_runnable:
163165
yield from tooluse.execute(confirm)
164166
else:
165-
if manager.log[-1].content.strip() == "/help":
166-
# undo the '/help' command itself
167-
manager.undo(1, quiet=True)
168-
manager.write()
169-
help()
170-
else:
171-
print("Unknown command")
167+
print(f"Unknown command: {name}")
172168

173169

174170
def edit(manager: LogManager) -> Generator[Message, None, None]: # pragma: no cover

0 commit comments

Comments
 (0)