From 4fed9ba353f59321d31f0321c7ab028d070259dd Mon Sep 17 00:00:00 2001 From: NoRePercussions Date: Wed, 19 Apr 2023 11:07:50 -0400 Subject: [PATCH 1/3] Added ^C to stop current command --- engshell.py | 68 ++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 32 deletions(-) mode change 100644 => 100755 engshell.py diff --git a/engshell.py b/engshell.py old mode 100644 new mode 100755 index f56aaa1..c007c0b --- a/engshell.py +++ b/engshell.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import openai import time from colorama import Fore, Style @@ -171,35 +172,38 @@ def clear_memory(): always_llm = '--llm' in sys.argv clear_memory() while user_input := input(engshell_PREVIX()): - if user_input == 'clear': - clear_memory() - os.system("cls" if platform.system() == "Windows" else "clear") - continue - if ('--llm' in user_input) or always_llm: user_input += CONGNITIVE_USER_MESSAGE - debug = ('--debug' in user_input) or always_debug - showcode = ('--showcode' in user_input) or always_showcode - gpt4 = ('--gpt4' in user_input) or always_gpt4 - user_input = user_input.replace('--llm','') - user_input = user_input.replace('--debug','') - user_input = user_input.replace('--showcode','') - user_input = user_input.replace('--gpt4','') - user_prompt = USER_MESSAGE(user_input, current_dir = os.getcwd()) - memory.append({"role": "user", "content": user_prompt}) - run_code = True - while run_code: - returned_code = LLM(user_prompt, mode='code', gpt4 = gpt4) - memory.append({"role": "assistant", "content": returned_code}) - try: - console_output = run_python(returned_code, debug, showcode, gpt4) - #if len(console_output) > MAX_PROMPT: - # print_status('output too large, summarizing...') - # console_output = summarize(console_output) - if console_output.strip() == '': console_output = 'done executing.' - print_success(console_output) - run_code = False - except Exception as e: - error_message = str(e) - console_output = error_message - run_code = any([err in error_message for err in RETRY_ERRORS]) - if len(console_output) < MAX_PROMPT: - memory.append({"role": "system", "content": console_output}) \ No newline at end of file + try: + if user_input == 'clear': + clear_memory() + os.system("cls" if platform.system() == "Windows" else "clear") + continue + if ('--llm' in user_input) or always_llm: user_input += CONGNITIVE_USER_MESSAGE + debug = ('--debug' in user_input) or always_debug + showcode = ('--showcode' in user_input) or always_showcode + gpt4 = ('--gpt4' in user_input) or always_gpt4 + user_input = user_input.replace('--llm','') + user_input = user_input.replace('--debug','') + user_input = user_input.replace('--showcode','') + user_input = user_input.replace('--gpt4','') + user_prompt = USER_MESSAGE(user_input, current_dir = os.getcwd()) + memory.append({"role": "user", "content": user_prompt}) + run_code = True + while run_code: + returned_code = LLM(user_prompt, mode='code', gpt4 = gpt4) + memory.append({"role": "assistant", "content": returned_code}) + try: + console_output = run_python(returned_code, debug, showcode, gpt4) + #if len(console_output) > MAX_PROMPT: + # print_status('output too large, summarizing...') + # console_output = summarize(console_output) + if console_output.strip() == '': console_output = 'done executing.' + print_success(console_output) + run_code = False + except Exception as e: + error_message = str(e) + console_output = error_message + run_code = any([err in error_message for err in RETRY_ERRORS]) + if len(console_output) < MAX_PROMPT: + memory.append({"role": "system", "content": console_output}) + except KeyboardInterrupt: + print("") From f0eb3e8d65b35adc84f35e0dadcee0d657b04b3d Mon Sep 17 00:00:00 2001 From: NoRePercussions Date: Wed, 19 Apr 2023 12:26:09 -0400 Subject: [PATCH 2/3] Standard shell behavior for ^C and arrow keys --- engshell.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/engshell.py b/engshell.py index c007c0b..9f98344 100755 --- a/engshell.py +++ b/engshell.py @@ -171,12 +171,22 @@ def clear_memory(): always_debug = '--debug' in sys.argv always_llm = '--llm' in sys.argv clear_memory() - while user_input := input(engshell_PREVIX()): - try: + + while True: # broken if user types "exit" + try: # catch KeyboardExceptions for standard shell behavior + user_input = input(engshell_PREVIX()) + + if len(user_input) == 0: # standard shell behavior on no input + continue + + if user_input == "exit": + break + if user_input == 'clear': clear_memory() os.system("cls" if platform.system() == "Windows" else "clear") continue + if ('--llm' in user_input) or always_llm: user_input += CONGNITIVE_USER_MESSAGE debug = ('--debug' in user_input) or always_debug showcode = ('--showcode' in user_input) or always_showcode @@ -205,5 +215,6 @@ def clear_memory(): run_code = any([err in error_message for err in RETRY_ERRORS]) if len(console_output) < MAX_PROMPT: memory.append({"role": "system", "content": console_output}) - except KeyboardInterrupt: - print("") + + except KeyboardInterrupt: # ^C standard shell behavior + print("") #newline so the next input line isn't shifted From 30626a78801cc257dc44c5af59433797a2e48b9e Mon Sep 17 00:00:00 2001 From: NoRePercussions Date: Wed, 19 Apr 2023 12:30:58 -0400 Subject: [PATCH 3/3] Add explicit how-to-exit message --- engshell.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/engshell.py b/engshell.py index 9f98344..1414a0f 100755 --- a/engshell.py +++ b/engshell.py @@ -172,6 +172,8 @@ def clear_memory(): always_llm = '--llm' in sys.argv clear_memory() + print("Type \"exit\" to exit the shell.") + while True: # broken if user types "exit" try: # catch KeyboardExceptions for standard shell behavior user_input = input(engshell_PREVIX())