diff --git a/pilot/database/database.py b/pilot/database/database.py index 7a43767da1..5e143ff4a6 100644 --- a/pilot/database/database.py +++ b/pilot/database/database.py @@ -290,10 +290,9 @@ def save_development_step(project, prompt_path, prompt_data, messages, llm_respo development_step = hash_and_save_step(DevelopmentSteps, project.args['app_id'], unique_data, data_fields, "Saved Development Step") - if development_step is not None: - project.checkpoints['last_development_step'] = development_step + project.checkpoints['last_development_step'] = development_step - project.save_files_snapshot(development_step.id) + project.save_files_snapshot(development_step.id) return development_step @@ -304,7 +303,7 @@ def get_saved_development_step(project): return development_step -def save_command_run(project, command, cli_response): +def save_command_run(project, command, cli_response, done_or_error_response, exit_code): if project.current_step != 'coding': return @@ -317,6 +316,8 @@ def save_command_run(project, command, cli_response): data_fields = { 'command': command, 'cli_response': cli_response, + 'done_or_error_response': done_or_error_response, + 'exit_code': exit_code, } command_run = hash_and_save_step(CommandRuns, project.args['app_id'], unique_data, data_fields, "Saved Command Run") diff --git a/pilot/database/models/command_runs.py b/pilot/database/models/command_runs.py index b0ecfc2662..63d4dd91d7 100644 --- a/pilot/database/models/command_runs.py +++ b/pilot/database/models/command_runs.py @@ -1,4 +1,4 @@ -from peewee import AutoField, ForeignKeyField, TextField, CharField +from peewee import AutoField, ForeignKeyField, TextField, CharField, IntegerField from database.models.components.base_models import BaseModel from database.models.app import App @@ -9,6 +9,8 @@ class CommandRuns(BaseModel): app = ForeignKeyField(App, on_delete='CASCADE') command = TextField(null=True) cli_response = TextField(null=True) + done_or_error_response = TextField(null=True) + exit_code = IntegerField(null=True) previous_step = ForeignKeyField('self', null=True, column_name='previous_step') high_level_step = CharField(null=True) diff --git a/pilot/helpers/cli.py b/pilot/helpers/cli.py index 119569207c..61022bd933 100644 --- a/pilot/helpers/cli.py +++ b/pilot/helpers/cli.py @@ -141,6 +141,14 @@ def execute_command(project, command, timeout=None, success_message=None, comman timeout = min(max(timeout, MIN_COMMAND_RUN_TIME), MAX_COMMAND_RUN_TIME) + project.command_runs_count += 1 + command_run = get_saved_command_run(project, command) + if command_run is not None and project.skip_steps: + # if we do, use it + project.checkpoints['last_command_run'] = command_run + print(color_yellow(f'Restoring command run response id {command_run.id}:\n```\n{command_run.cli_response}```')) + return command_run.cli_response, command_run.done_or_error_response, command_run.exit_code + if not force: print(color_yellow_bold('\n--------- EXECUTE COMMAND ----------')) question = f'Can I execute the command: `{color_yellow_bold(command)}`' @@ -153,26 +161,20 @@ def execute_command(project, command, timeout=None, success_message=None, comman # TODO can we use .confirm(question, default='yes').ask() https://questionary.readthedocs.io/en/stable/pages/types.html#confirmation print('answer: ' + answer) if answer.lower() in ['no', 'skip']: + save_command_run(project, command, None, 'DONE', None) return None, 'DONE', None elif answer.lower() not in ['', 'yes', 'ok', 'okay', 'sure']: # "That's not going to work, let's do X instead" # https://github.com/Pythagora-io/gpt-pilot/issues/198 # https://github.com/Pythagora-io/gpt-pilot/issues/43#issuecomment-1756352056 # TODO: https://github.com/Pythagora-io/gpt-pilot/issues/122 + save_command_run(project, command, None, answer, None) return None, answer, None # TODO when a shell built-in commands (like cd or source) is executed, the output is not captured properly - this will need to be changed at some point if platform.system() != 'Windows' and ("cd " in command or "source " in command): command = "bash -c '" + command + "'" - project.command_runs_count += 1 - command_run = get_saved_command_run(project, command) - if command_run is not None and project.skip_steps: - # if we do, use it - project.checkpoints['last_command_run'] = command_run - print(color_yellow(f'Restoring command run response id {command_run.id}:\n```\n{command_run.cli_response}```')) - return command_run.cli_response, None, None - return_value = None done_or_error_response = None @@ -284,7 +286,7 @@ def execute_command(project, command, timeout=None, success_message=None, comman return_value = 'stderr:\n```\n' + stderr_output[0:MAX_COMMAND_OUTPUT_LENGTH] + '\n```\n' return_value += 'stdout:\n```\n' + output[-MAX_COMMAND_OUTPUT_LENGTH:] + '\n```' - save_command_run(project, command, return_value) + save_command_run(project, command, return_value, done_or_error_response, process.returncode) return return_value, done_or_error_response, process.returncode diff --git a/pilot/utils/questionary.py b/pilot/utils/questionary.py index ade348b934..441231b6be 100644 --- a/pilot/utils/questionary.py +++ b/pilot/utils/questionary.py @@ -37,7 +37,7 @@ def styled_text(project, question, ignore_user_input_count=False, style=None): print(response) if not ignore_user_input_count: - user_input = save_user_input(project, question, response) + save_user_input(project, question, response) print('\n\n', end='') return response