Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix -i flag after metadata was introduced #723

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions gpt_engineer/chat_to_files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from pathlib import Path
import re

from typing import List, Tuple

from gpt_engineer.db import DB
from gpt_engineer.db import DB, DBs
from gpt_engineer.file_selector import FILE_LIST_NAME


Expand Down Expand Up @@ -72,9 +73,9 @@ def to_files(chat: str, workspace: DB):
workspace[file_name] = file_content


def overwrite_files(chat, dbs):
def overwrite_files(chat: str, dbs: DBs) -> None:
"""
Replace the AI files with the older local files.
Parse the chat and overwrite all files in the workspace.

Parameters
----------
Expand All @@ -83,19 +84,17 @@ def overwrite_files(chat, dbs):
dbs : DBs
The database containing the workspace.
"""
dbs.workspace["all_output.txt"] = chat # TODO store this in memory db instead
dbs.memory["all_output_overwrite.txt"] = chat

files = parse_chat(chat)
for file_name, file_content in files:
if file_name == "README.md":
dbs.workspace[
"LAST_MODIFICATION_README.md"
] = file_content # TODO store this in memory db instead
dbs.memory["LAST_MODIFICATION_README.md"] = file_content
else:
dbs.workspace[file_name] = file_content


def get_code_strings(input: DB) -> dict[str, str]:
def get_code_strings(workspace_path: Path, metadata_db: DB) -> dict[str, str]:
"""
Read file_list.txt and return file names and their content.

Expand All @@ -109,13 +108,13 @@ def get_code_strings(input: DB) -> dict[str, str]:
dict[str, str]
A dictionary mapping file names to their content.
"""
files_paths = input[FILE_LIST_NAME].strip().split("\n")
files_paths = metadata_db[FILE_LIST_NAME].strip().split("\n")
files_dict = {}
for full_file_path in files_paths:
with open(full_file_path, "r") as file:
file_data = file.read()
if file_data:
file_name = os.path.relpath(full_file_path, input.path)
file_name = os.path.relpath(full_file_path, workspace_path)
files_dict[file_name] = file_data
return files_dict

Expand Down
16 changes: 9 additions & 7 deletions gpt_engineer/file_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import tkinter.filedialog as fd

from pathlib import Path
from typing import List, Union
from typing import List, Mapping, Union

from gpt_engineer.db import DB, DBs

IGNORE_FOLDERS = {"site-packages", "node_modules", "venv"}
FILE_LIST_NAME = "file_list.txt"
Expand Down Expand Up @@ -231,7 +233,7 @@ def is_in_ignoring_extensions(path: Path) -> bool:
return is_hidden and is_pycache


def ask_for_files(db_input) -> None:
def ask_for_files(metadata_db: DB, workspace_db: DB) -> None:
"""
Ask user to select files to improve.
It can be done by terminal, gui, or using the old selection.
Expand All @@ -240,10 +242,10 @@ def ask_for_files(db_input) -> None:
dict[str, str]: Dictionary where key = file name and value = file path
"""
use_last_string = ""
if FILE_LIST_NAME in db_input:
if FILE_LIST_NAME in metadata_db:
use_last_string = (
"3. Use previous file list (available at "
+ f"{os.path.join(db_input.path, FILE_LIST_NAME)})\n"
+ f"{os.path.join(metadata_db.path, FILE_LIST_NAME)})\n"
)
selection_number = 3
else:
Expand All @@ -270,10 +272,10 @@ def ask_for_files(db_input) -> None:

if selection_number == 1:
# Open GUI selection
file_path_list = gui_file_selector(db_input.path)
file_path_list = gui_file_selector(workspace_db.path)
elif selection_number == 2:
# Open terminal selection
file_path_list = terminal_file_selector(db_input.path)
file_path_list = terminal_file_selector(workspace_db.path)
if (
selection_number <= 0
or selection_number > 3
Expand All @@ -283,7 +285,7 @@ def ask_for_files(db_input) -> None:
sys.exit(1)

if not selection_number == 3:
db_input[FILE_LIST_NAME] = "\n".join(file_path_list)
metadata_db[FILE_LIST_NAME] = "\n".join(file_path_list)


def gui_file_selector(input_path: str) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion gpt_engineer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def main(
)

input_path = Path(project_path).absolute()
workspace_path = input_path / "workspace"
project_metadata_path = input_path / ".gpteng"
memory_path = project_metadata_path / "memory"
workspace_path = input_path / "workspace"
archive_path = project_metadata_path / "archive"

dbs = DBs(
Expand Down
9 changes: 5 additions & 4 deletions gpt_engineer/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def setup_sys_prompt_existing_code(dbs: DBs) -> str:
def curr_fn() -> str:
"""
Get the name of the current function
NOTE: This will be the name of the function that called this function,

This will be the name of the function that called this function,
so it serves to ensure we don't hardcode the function name in the step,
but allow the step names to be refactored
"""
Expand Down Expand Up @@ -295,7 +296,7 @@ def use_feedback(ai: AI, dbs: DBs):

def set_improve_filelist(ai: AI, dbs: DBs):
"""Sets the file list for files to work with in existing code mode."""
ask_for_files(dbs.project_metadata) # stores files as full paths.
ask_for_files(dbs.project_metadata, dbs.input) # stores files as full paths.
return []


Expand Down Expand Up @@ -347,8 +348,8 @@ def improve_existing_code(ai: AI, dbs: DBs):
"""

files_info = get_code_strings(
dbs.project_metadata
) # this only has file names not paths
dbs.input.path, dbs.project_metadata
) # this has file names relative to the workspace path

messages = [
ai.fsystem(setup_sys_prompt_existing_code(dbs)),
Expand Down