From 3d14f31511970709ad25f0656d95f55195075fd4 Mon Sep 17 00:00:00 2001 From: Alessandro Annini Date: Fri, 14 Apr 2023 14:42:56 +0200 Subject: [PATCH 1/4] feat: :sparkles: can debug javascript files --- wolverine.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/wolverine.py b/wolverine.py index 42f7ff8..3b50d18 100644 --- a/wolverine.py +++ b/wolverine.py @@ -25,6 +25,17 @@ def run_script(script_name, script_args): return result.decode("utf-8"), 0 +def run_node(script_name, script_args): + script_args = [str(arg) for arg in script_args] + try: + result = subprocess.check_output( + ["node", script_name, *script_args], stderr=subprocess.STDOUT + ) + except subprocess.CalledProcessError as e: + return e.output.decode("utf-8"), e.returncode + return result.decode("utf-8"), 0 + + def send_error_to_gpt(file_path, args, error_message, model): with open(file_path, "r") as f: file_lines = f.readlines() @@ -38,8 +49,7 @@ def send_error_to_gpt(file_path, args, error_message, model): initial_prompt_text = f.read() prompt = ( - initial_prompt_text + - "\n\n" + initial_prompt_text + "\n\n" "Here is the script that needs fixing:\n\n" f"{file_with_lines}\n\n" "Here are the arguments it was provided:\n\n" @@ -114,7 +124,7 @@ def apply_changes(file_path, changes_json): print(line, end="") -def main(script_name, *script_args, revert=False, model="gpt-4"): +def main(script_name, *script_args, revert=False, model="gpt-3.5-turbo"): if revert: backup_file = script_name + ".bak" if os.path.exists(backup_file): @@ -129,7 +139,12 @@ def main(script_name, *script_args, revert=False, model="gpt-4"): shutil.copy(script_name, script_name + ".bak") while True: - output, returncode = run_script(script_name, script_args) + output = "" + returncode = 0 + if script_name.endswith(".js"): + output, returncode = run_node(script_name, script_args) + else: + output, returncode = run_script(script_name, script_args) if returncode == 0: cprint("Script ran successfully.", "blue") @@ -140,10 +155,10 @@ def main(script_name, *script_args, revert=False, model="gpt-4"): print("Output:", output) json_response = send_error_to_gpt( - file_path=script_name, - args=script_args, - error_message=output, - model=model, + file_path=script_name, + args=script_args, + error_message=output, + model=model, ) apply_changes(script_name, json_response) cprint("Changes applied. Rerunning...", "blue") From 49c6173c9b5216da0eff7d8fa4340e27b72be2d5 Mon Sep 17 00:00:00 2001 From: Alessandro Annini Date: Fri, 14 Apr 2023 14:43:32 +0200 Subject: [PATCH 2/4] chore: add buggy javascript file to test new feature --- buggy_script.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 buggy_script.js diff --git a/buggy_script.js b/buggy_script.js new file mode 100644 index 0000000..76ed038 --- /dev/null +++ b/buggy_script.js @@ -0,0 +1,25 @@ +const subtractNumbers = (a, b) => a - b; + +const multiplyNumbers = (a, b) => a * b; + +const divideNumbers = (a, b) => a / b; + +function calculate(operation, num1, num2) { + let result = ''; + if (operation == 'add') { + result = addNumbers(num1, num2); + } else if (operation == 'subtract') { + result = subtractNumbers(num1, num2); + } else if (operation == 'multiply') { + result = multiplyNumbers(num1, num2); + } else if (operation == 'divide') { + result = divideNumbers(num1, num2); + } else { + console.log('Invalid operation'); + } + + return res; +} + +const [, , operation, num1, num2] = process.argv; +calculate(operation, num1, num2); From 8abc8893bb0caf1eab6711d8a52665c0721c3e52 Mon Sep 17 00:00:00 2001 From: Alessandro Annini Date: Sat, 15 Apr 2023 10:10:34 +0200 Subject: [PATCH 3/4] refactor: better implementation fr py/js files execution --- wolverine.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/wolverine.py b/wolverine.py index e9d12d6..2e96b08 100644 --- a/wolverine.py +++ b/wolverine.py @@ -23,10 +23,18 @@ with open("prompt.txt") as f: def run_script(script_name, script_args): script_args = [str(arg) for arg in script_args] + """ + If script_name.endswith(".py") then run with python + else run with node + """ + subprocess_args = ( + [sys.executable, script_name, *script_args] + if script_name.endswith(".py") + else ["node", script_name, *script_args] + ) + try: - result = subprocess.check_output( - [sys.executable, script_name, *script_args], stderr=subprocess.STDOUT - ) + result = subprocess.check_output(subprocess_args, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: return e.output.decode("utf-8"), e.returncode return result.decode("utf-8"), 0 @@ -173,12 +181,7 @@ def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL): shutil.copy(script_name, script_name + ".bak") while True: - output = "" - returncode = 0 - if script_name.endswith(".js"): - output, returncode = run_node(script_name, script_args) - else: - output, returncode = run_script(script_name, script_args) + output, returncode = run_script(script_name, script_args) if returncode == 0: cprint("Script ran successfully.", "blue") From 0f538fecae4b129e0e9f19066f7f8d0548404ff2 Mon Sep 17 00:00:00 2001 From: Alessandro Annini Date: Sat, 15 Apr 2023 10:10:59 +0200 Subject: [PATCH 4/4] chore: update buggy js script file --- buggy_script.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/buggy_script.js b/buggy_script.js index 76ed038..ad05a00 100644 --- a/buggy_script.js +++ b/buggy_script.js @@ -1,8 +1,14 @@ -const subtractNumbers = (a, b) => a - b; +const subtractNumbers = (a, b) => { + return a - b; +}; -const multiplyNumbers = (a, b) => a * b; +const multiplyNumbers = (a, b) => { + return a * b; +}; -const divideNumbers = (a, b) => a / b; +const divideNumbers = (a, b) => { + return a / b; +}; function calculate(operation, num1, num2) { let result = '';