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

Refactor changes #94

Merged
merged 17 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
79 changes: 36 additions & 43 deletions mentat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

from termcolor import cprint

from .code_change import CodeChange, CodeChangeAction
from .code_change_display import print_change
from mentat.parsers.block_parser import BlockParser
from mentat.parsers.file_edit import FileEdit

from .code_context import CodeContext
from .code_file import parse_intervals
from .code_file_manager import CodeFileManager
Expand Down Expand Up @@ -115,6 +116,8 @@ def loop(
no_code_map: bool,
) -> None:
git_root = get_shared_git_root_for_paths([Path(path) for path in paths])
# The parser can be selected here
parser = BlockParser()
config = ConfigManager(git_root)
code_context = CodeContext(config, paths, exclude_paths or [])
code_context.display_context()
Expand All @@ -128,7 +131,7 @@ def loop(
"""
ctags_disabled_message = dedent(ctags_disabled_message)
cprint(ctags_disabled_message, color="yellow")
conv = Conversation(config, cost_tracker, code_file_manager, code_map)
conv = Conversation(parser, config, cost_tracker, code_file_manager, code_map)

cprint("Type 'q' or use Ctrl-C to quit at any time.\n", color="cyan")
cprint("What can I do for you?", color="light_blue")
Expand All @@ -138,22 +141,21 @@ def loop(
user_response = user_input_manager.collect_user_input()
conv.add_user_message(user_response)

_, code_changes = conv.get_model_response(config)

if code_changes:
need_user_request = get_user_feedback_on_changes(
config, conv, user_input_manager, code_file_manager, code_changes
file_edits = conv.get_model_response(parser, config)
if file_edits:
need_user_request = get_user_feedback_on_edits(
config, conv, user_input_manager, code_file_manager, file_edits
)
else:
need_user_request = True


def get_user_feedback_on_changes(
def get_user_feedback_on_edits(
config: ConfigManager,
conv: Conversation,
user_input_manager: UserInputManager,
code_file_manager: CodeFileManager,
code_changes: list[CodeChange],
file_edits: list[FileEdit],
) -> bool:
cprint(
"Apply these changes? 'Y/n/i' or provide feedback.",
Expand All @@ -164,36 +166,35 @@ def get_user_feedback_on_changes(
need_user_request = True
match user_response.lower():
case "y" | "":
code_changes_to_apply = code_changes
edits_to_apply = file_edits
conv.add_user_message("User chose to apply all your changes.")
case "n":
code_changes_to_apply = []
edits_to_apply = []
conv.add_user_message("User chose not to apply any of your changes.")
case "i":
code_changes_to_apply, indices = user_filter_changes(
user_input_manager, code_changes
edits_to_apply = user_filter_changes(
code_file_manager, user_input_manager, config, file_edits
)
conv.add_user_message(
"User chose to apply"
f" {len(code_changes_to_apply)}/{len(code_changes)} of your suggest"
" changes. The changes they applied were:"
f" {', '.join(map(str, indices))}"
f" {len(edits_to_apply)}/{len(file_edits)} of your suggested"
" changes."
)
case _:
need_user_request = False
code_changes_to_apply = []
edits_to_apply = []
conv.add_user_message(
"User chose not to apply any of your changes. User response:"
f" {user_response}\n\nPlease adjust your previous plan and changes to"
" reflect this. Respond with a full new set of changes."
)

if code_changes_to_apply:
code_file_manager.write_changes_to_files(code_changes_to_apply)
if len(code_changes_to_apply) == len(code_changes):
cprint("Changes applied.", color="light_blue")
else:
cprint("Selected changes applied.", color="light_blue")
for file_edit in edits_to_apply:
file_edit.resolve_conflicts(user_input_manager)

if edits_to_apply:
code_file_manager.write_changes_to_files(edits_to_apply)
cprint("Changes applied.", color="light_blue")
else:
cprint("No changes applied.", color="light_blue")

Expand All @@ -204,22 +205,14 @@ def get_user_feedback_on_changes(


def user_filter_changes(
user_input_manager: UserInputManager, code_changes: list[CodeChange]
) -> tuple[list[CodeChange], list[int]]:
new_changes = list[CodeChange]()
indices = list[int]()
for index, change in enumerate(code_changes, start=1):
print_change(change)
# Allowing the user to remove rename file changes introduces a lot of edge cases
if change.action == CodeChangeAction.RenameFile:
new_changes.append(change)
indices.append(index)
cprint("Cannot remove rename file change", "light_yellow")
continue

cprint("Keep this change?", "light_blue")
if user_input_manager.ask_yes_no(default_yes=True):
new_changes.append(change)
indices.append(index)

return new_changes, indices
code_file_manager: CodeFileManager,
user_input_manager: UserInputManager,
config: ConfigManager,
file_edits: list[FileEdit],
) -> list[FileEdit]:
new_edits = list[FileEdit]()
for file_edit in file_edits:
if file_edit.filter_replacements(code_file_manager, user_input_manager, config):
new_edits.append(file_edit)

return new_edits
110 changes: 0 additions & 110 deletions mentat/change_conflict_resolution.py

This file was deleted.

Loading