diff --git a/pilot/main.py b/pilot/main.py index b483ce7..a13abfc 100644 --- a/pilot/main.py +++ b/pilot/main.py @@ -1,12 +1,15 @@ # main.py from __future__ import print_function, unicode_literals +import sys + from dotenv import load_dotenv load_dotenv() from helpers.Project import Project from utils.arguments import get_arguments +from utils.exit import exit_gpt_pilot from logger.logger import logger from database.database import database_exists, create_database, tables_exist, create_tables @@ -28,8 +31,13 @@ def init(): if __name__ == "__main__": - args = init() - - # TODO get checkpoint from database and fill the project with it - project = Project(args) - project.start() \ No newline at end of file + try: + args = init() + project = Project(args) + project.start() + except KeyboardInterrupt: + exit_gpt_pilot() + except Exception as e: + exit_gpt_pilot() + finally: + sys.exit(0) diff --git a/pilot/utils/exit.py b/pilot/utils/exit.py new file mode 100644 index 0000000..708419c --- /dev/null +++ b/pilot/utils/exit.py @@ -0,0 +1,51 @@ +# exit.py +import os +import hashlib +import requests + +from utils.questionary import get_user_feedback + + +def send_telemetry(path_id): + # Prepare the telemetry data + telemetry_data = { + "pathId": path_id, + "event": "pilot-exit" + } + + try: + response = requests.post("https://api.pythagora.io/telemetry", json=telemetry_data) + response.raise_for_status() + except requests.RequestException as err: + print(f"Failed to send telemetry data: {err}") + + +def send_feedback(feedback, path_id): + """Send the collected feedback to the endpoint.""" + # Prepare the feedback data (you can adjust the structure as per your backend needs) + feedback_data = { + "pathId": path_id, + "data": feedback, + "event": "pilot-feedback" + } + + try: + response = requests.post("https://api.pythagora.io/telemetry", json=feedback_data) + response.raise_for_status() + except requests.RequestException as err: + print(f"Failed to send feedback data: {err}") + + +def get_path_id(): + # Calculate the SHA-256 hash of the installation directory + installation_directory = os.path.abspath(os.path.join(os.getcwd(), "..")) + return hashlib.sha256(installation_directory.encode()).hexdigest() + + +def exit_gpt_pilot(): + path_id = get_path_id() + send_telemetry(path_id) + + feedback = get_user_feedback() + if feedback: # only send if user provided feedback + send_feedback(feedback, path_id) diff --git a/pilot/utils/questionary.py b/pilot/utils/questionary.py index a9a9506..b78c803 100644 --- a/pilot/utils/questionary.py +++ b/pilot/utils/questionary.py @@ -37,3 +37,10 @@ def styled_text(project, question): print('\n\n', end='') return response + + +def get_user_feedback(): + config = { + 'style': custom_style, + } + return questionary.text("Thank you for trying GPT-Pilot. Please give us your feedback or just press ENTER to exit: ", **config).unsafe_ask()