Skip to content

Commit

Permalink
fix: prevent command execution from triggering unnecessary response g…
Browse files Browse the repository at this point in the history
…eneration
  • Loading branch information
ErikBjare committed Dec 2, 2024
1 parent 748be0b commit 7f66dbf
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions gptme/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,41 @@ def confirm_func(msg) -> bool:
if execute_cmd(msg, manager, confirm_func):
continue

# Check if the message is a command first
if msg.content.startswith("/"):
if execute_cmd(msg, manager, confirm_func):
continue

# Generate and execute response for this prompt
while True:
try:
set_interruptible()
response_msgs = list(step(manager.log, stream, confirm_func))
except KeyboardInterrupt:
console.log("Interrupted. Stopping current execution.")
manager.append(Message("system", "Interrupted"))
break
finally:
clear_interruptible()

for response_msg in response_msgs:
manager.append(response_msg)
# run any user-commands, if msg is from user
if response_msg.role == "user" and execute_cmd(
response_msg, manager, confirm_func
):
try:
set_interruptible()
response_msgs = list(step(manager.log, stream, confirm_func))
except KeyboardInterrupt:
console.log("Interrupted. Stopping current execution.")
manager.append(Message("system", "Interrupted"))
continue
finally:
clear_interruptible()

for response_msg in response_msgs:
manager.append(response_msg)
# If this is a command from user input during tool execution, handle it
if response_msg.role == "user" and response_msg.content.startswith(
"/"
):
if execute_cmd(response_msg, manager, confirm_func):
break

# Check if there are any runnable tools left
last_content = next(
(
m.content
for m in reversed(manager.log)
if m.role == "assistant"
),
"",
)
if not any(
tooluse.is_runnable
for tooluse in ToolUse.iter_from_content(last_content)
):
break
# Check if there are any runnable tools left
last_content = next(
(m.content for m in reversed(manager.log) if m.role == "assistant"),
"",
)
if any(
tooluse.is_runnable
for tooluse in ToolUse.iter_from_content(last_content)
):
continue # Continue processing tools if any are runnable

# All prompts processed, continue to next iteration
continue
Expand Down

0 comments on commit 7f66dbf

Please sign in to comment.