Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic command defaults upgrade + delete messages #566

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ We welcome code contributions through pull requests. Here are some guidelines:

- Open a PR to `main` linking any related issues. Provide detailed context on your changes.

- Add your `OPENAI_API_KEY` environment variable to the repository secrets to make sure your tests can pass.

Comment on lines +34 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be removed as it's a bit confusing, and will likely lead to folks opening Issues or reaching out in the Discrd because they are trying to add their own OpenAI keys to a repo that they don't have access to the Environment Variables for - and I don't think anyone is running the actions on their own forks of the repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, I thought it was the reason for the failing tests initially but I realised it wasn't. Will delete.

Would be good to get to the bottom of it more generally since it's a recurring issue..

- We will review PRs when possible and work with you to integrate your contribution. Please be patient as reviews take time.

- Once approved, your code will be merged - thank you for improving Open Interpreter!
Expand Down
48 changes: 43 additions & 5 deletions interpreter/terminal_interface/magic_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ..utils.display_markdown_message import display_markdown_message
import json
import os
import datetime
import glob

def handle_undo(self, arguments):
# Removes all messages after the most recent user entry (and the entry itself).
Expand Down Expand Up @@ -38,8 +40,9 @@ def handle_help(self, arguments):
"%debug [true/false]": "Toggle debug mode. Without arguments or with 'true', it enters debug mode. With 'false', it exits debug mode.",
"%reset": "Resets the current session.",
"%undo": "Remove previous messages and its response from the message history.",
"%save_message [path]": "Saves messages to a specified JSON path. If no path is provided, it defaults to 'messages.json'.",
"%load_message [path]": "Loads messages from a specified JSON path. If no path is provided, it defaults to 'messages.json'.",
"%save_message [path]": "Saves messages to a specified JSON path. If no path is provided, it defaults to '[date]_messages_[counter].json'.",
"%load_message [path]": "Loads messages from a specified JSON path. If no path is provided, it loads the latest default message '[date]_messages_[counter].json'.",
Comment on lines +43 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor rendering thing, but I think the underscores (_) here need to be escaped with \_ or they try to render as markdown and end up italicized.

Before escaping underscores in filenames

Screen Shot 2023-10-08 at 11 55 58 AM

With filename underscores escaped in %load_message help text

Screen Shot 2023-10-08 at 11 57 15 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot - will update!

"%delete_message [number]": "Deletes saved messages, preserving the most recent 'number' files. If no number is provided, it deletes all but the most recent file.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%delete_message didn't seem to work. Maybe because it's defined as delete_messages on line 148 in the handle_magic_command() method?

Screen Shot 2023-10-08 at 12 02 04 PM

Also, it seems like %delete_message should require a path rather than potentially deleting multiple files when called without an argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be %delete_messages, and will make it so that you can supply a folder or file.

"%help": "Show this help message.",
}

Expand Down Expand Up @@ -82,7 +85,13 @@ def default_handle(self, arguments):

def handle_save_message(self, json_path):
if json_path == "":
json_path = "messages.json"
now = datetime.datetime.now()
msg_counter = 0
while True:
json_path = now.strftime("%Y-%m-%d") + "_messages_" + str(msg_counter) + ".json"
if not os.path.exists(json_path):
break
msg_counter += 1
if not json_path.endswith(".json"):
json_path += ".json"
with open(json_path, 'w') as f:
Expand All @@ -91,14 +100,42 @@ def handle_save_message(self, json_path):
display_markdown_message(f"> messages json export to {os.path.abspath(json_path)}")

def handle_load_message(self, json_path):
if json_path == "":
json_path = "messages.json"
if not json_path:
json_paths = glob.glob("*_messages_*.json")
if json_paths:
latest_path = max(json_paths, key=os.path.getmtime)
json_path = latest_path
else:
json_path = "messages.json"
if not json_path.endswith(".json"):
json_path += ".json"
with open(json_path, 'r') as f:
self.messages = json.load(f)

display_markdown_message(f"> messages json loaded from {os.path.abspath(json_path)}")

def handle_delete_messages(self, num_to_keep):
if num_to_keep == "":
num_to_keep = "1"
num_to_keep = int(num_to_keep)
print(num_to_keep)
json_paths = glob.glob("*_messages_*.json")
print(json_paths)
if len(json_paths) <= num_to_keep:
print("No files to delete")
return
json_paths.sort(key=os.path.getmtime)
print(json_paths)
if num_to_keep < 0:
print("Number of files to keep must be positive")
return
elif num_to_keep > 0:
json_paths = json_paths[:-num_to_keep]
print(json_paths)
for path in json_paths:
os.remove(path)
print(f"Deleted {len(json_paths)} old json files")


def handle_magic_command(self, user_input):
# split the command into the command and the arguments, by the first whitespace
Expand All @@ -108,6 +145,7 @@ def handle_magic_command(self, user_input):
"reset": handle_reset,
"save_message": handle_save_message,
"load_message": handle_load_message,
"delete_messages": handle_delete_messages,
"undo": handle_undo,
}

Expand Down