diff --git a/pilot/database/database.py b/pilot/database/database.py index 20bc186d54..b2577a613d 100644 --- a/pilot/database/database.py +++ b/pilot/database/database.py @@ -300,9 +300,6 @@ def save_development_step(project, prompt_path, prompt_data, messages, llm_respo project.save_files_snapshot(development_step.id) - # todo check if we need to return anything - return development_step - def save_command_run(project, command, cli_response, done_or_error_response, exit_code): if project.current_step != 'coding': diff --git a/pilot/helpers/Project.py b/pilot/helpers/Project.py index 82950cfa36..da9fc48b75 100644 --- a/pilot/helpers/Project.py +++ b/pilot/helpers/Project.py @@ -1,18 +1,19 @@ import json import os +import sys from pathlib import Path from typing import Tuple import peewee from const.messages import CHECK_AND_CONTINUE, AFFIRMATIVE_ANSWERS, NEGATIVE_ANSWERS -from utils.style import color_yellow_bold, color_cyan, color_white_bold +from utils.style import color_yellow_bold, color_cyan, color_white_bold, color_red_bold from const.common import STEPS from database.database import delete_unconnected_steps_from, delete_all_app_development_data, \ get_all_app_development_steps, delete_all_subsequent_steps from const.ipc import MESSAGE_TYPE from prompts.prompts import ask_user -from helpers.exceptions import TokenLimitError +from helpers.exceptions import TokenLimitError, GracefulExit from utils.questionary import styled_text from helpers.files import get_directory_contents, get_file_contents, clear_directory, update_file from helpers.cli import build_directory_tree @@ -106,6 +107,12 @@ class Project: self.dot_pilot_gpt.with_root_path(root_path) def setup_loading(self): + if self.skip_until_dev_step == '0': + clear_directory(self.root_path) + delete_all_app_development_data(self.args['app_id']) + self.finish_loading(False) + return + self.skip_steps = True should_overwrite_files = None while should_overwrite_files is None or should_overwrite_files.lower() not in AFFIRMATIVE_ANSWERS + NEGATIVE_ANSWERS: @@ -124,21 +131,23 @@ class Project: self.should_overwrite_files = True break - if self.skip_until_dev_step is not None and self.skip_until_dev_step == '0': - clear_directory(self.root_path) - delete_all_app_development_data(self.args['app_id']) - self.finish_loading() - return - - self.dev_steps_to_load = get_all_app_development_steps(self.args['app_id'], last_step=self.skip_until_dev_step) load_step_before_coding = ('step' in self.args and self.args['step'] is not None and STEPS.index(self.args['step']) < STEPS.index('coding')) if load_step_before_coding: + if not self.should_overwrite_files: + print(color_red_bold('Cannot load step before "coding" without overwriting files. You have to reload ' + 'the app and select "Use GPT Pilot\'s code" but you will lose all coding progress' + ' on this project.')) + raise GracefulExit() + clear_directory(self.root_path) delete_all_app_development_data(self.args['app_id']) - elif self.dev_steps_to_load is not None and len(self.dev_steps_to_load): + return + + self.dev_steps_to_load = get_all_app_development_steps(self.args['app_id'], last_step=self.skip_until_dev_step) + if self.dev_steps_to_load is not None and len(self.dev_steps_to_load): self.checkpoints['last_development_step'] = self.dev_steps_to_load[-1] self.tasks_to_load = [el for el in self.dev_steps_to_load if 'breakdown.prompt' in el.get('prompt_path', '')] self.features_to_load = [el for el in self.dev_steps_to_load if 'feature_plan.prompt' in el.get('prompt_path', '')] @@ -189,7 +198,7 @@ class Project: """ while True: feature_description = '' - if not any(el for el in self.dev_steps_to_load if 'feature_plan.prompt' in el.get('prompt_path', '')): + if not self.features_to_load: self.finish_loading() if not self.skip_steps: feature_description = ask_user(self, "Project is finished! Do you want to add any features or changes? " @@ -202,7 +211,7 @@ class Project: self.tech_lead.create_feature_plan(feature_description) # loading of features - elif self.features_to_load: + else: num_of_features = len(self.features_to_load) # last feature is always the one we want to load diff --git a/pilot/helpers/agents/Developer.py b/pilot/helpers/agents/Developer.py index 61c17600de..3c085cdba6 100644 --- a/pilot/helpers/agents/Developer.py +++ b/pilot/helpers/agents/Developer.py @@ -390,8 +390,7 @@ class Developer(Agent): }, should_log_message=False) convo.remove_last_x_messages(2) else: - detailed_user_review_goal = self.project.last_detailed_user_review_goal['llm_response']['text'] if\ - self.project.last_detailed_user_review_goal is not None else None + detailed_user_review_goal = self.project.last_detailed_user_review_goal['llm_response']['text'] try: if continue_development: diff --git a/pilot/helpers/exceptions.py b/pilot/helpers/exceptions.py index 04c6c92b35..48b63788d6 100644 --- a/pilot/helpers/exceptions.py +++ b/pilot/helpers/exceptions.py @@ -30,3 +30,9 @@ class ApiError(Exception): def __init__(self, message): self.message = message super().__init__(message) + + +class GracefulExit(Exception): + def __init__(self, message='Graceful exit'): + self.message = message + super().__init__(message) diff --git a/pilot/main.py b/pilot/main.py index 1e2af7d437..2064a9f032 100644 --- a/pilot/main.py +++ b/pilot/main.py @@ -23,7 +23,7 @@ from database.database import database_exists, create_database, tables_exist, cr from utils.settings import settings, loader from utils.telemetry import telemetry -from helpers.exceptions import ApiError, TokenLimitError +from helpers.exceptions import ApiError, TokenLimitError, GracefulExit def init(): # Check if the "euclid" database exists, if not, create it @@ -108,12 +108,19 @@ if __name__ == "__main__": telemetry.send() run_exit_fn = False print('Exit', type='exit') + except KeyboardInterrupt: telemetry.set("end_result", "interrupt") if project is not None and project.check_ipc(): telemetry.send() run_exit_fn = False + except GracefulExit as err: + # can't call project.finish_loading() here because project can be None + run_exit_fn = False + print('', type='loadingFinished') + print('Exit', type='exit') + except Exception as err: print(color_red('---------- GPT PILOT EXITING WITH ERROR ----------')) traceback.print_exc() diff --git a/pilot/prompts/components/steps_list.prompt b/pilot/prompts/components/steps_list.prompt index c597daa817..6dbbe87d4f 100644 --- a/pilot/prompts/components/steps_list.prompt +++ b/pilot/prompts/components/steps_list.prompt @@ -1,5 +1,5 @@ {% if task_steps and step_index is not none -%} -Task is split into multiple steps. Previously generated steps can be one of the following: +The current task has been split into multiple steps, and each step is one of the following: * `command` - command to run * `save_file` or `code_change` - create new or update existing file * `modify_file` - update large existing file