Skip to content

Commit af7a108

Browse files
authored
feat: Allow config to keep original lines as comments (#12)
1 parent 9f549ca commit af7a108

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

llm4papers/editor_agents/OpenAIChatEditorAgent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def edit(self, document: Document, edit: EditRequest) -> str:
7777
edit_request=edit.request_text,
7878
)
7979

80-
edited = response["new_window"]
80+
edited = response["new_window"] + "\n"
8181
logger.info(f"Edited text for document {document.name}:")
8282
logger.info(f"- {editable_text}")
8383
logger.info(f"+ {edited}")

llm4papers/paper_remote/OverleafGitPaperRemote.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
import logging
88
import pathlib
99
import shutil
10+
import datetime
1011

11-
from git import Repo, Commit
12+
from git import Repo
13+
from llm4papers.config import Settings
1214

1315
from llm4papers.editor_agents.EditorAgent import EditorAgent
1416
from llm4papers.models import Document, EditRequest
1517
from llm4papers.paper_remote import PaperRemote, logger
1618

1719

18-
def _line_was_in_last_n_commit(
20+
def _too_close_to_human_edits(
1921
repo: Repo, filename: str, line_number: int, last_n: int = 2
2022
) -> bool:
2123
"""
@@ -27,6 +29,19 @@ def _line_was_in_last_n_commit(
2729
made without stomping on the user's edits.
2830
2931
"""
32+
# Get the date of the nth-back commit:
33+
last_commit_date = repo.head.commit.committed_datetime
34+
now = datetime.datetime.now(tz=datetime.timezone.utc)
35+
36+
print(f"Last commit date: {last_commit_date}")
37+
print(f"Now: {now}")
38+
39+
sec_since_last_commit = (now - last_commit_date).total_seconds()
40+
41+
# If the last commit was more than 10 seconds ago, any edit is fine:
42+
if sec_since_last_commit > 10:
43+
return False
44+
3045
# Get the diff for HEAD~n:
3146
total_diff = repo.git.diff(f"HEAD~{last_n}", filename, unified=0)
3247

@@ -40,22 +55,6 @@ def _line_was_in_last_n_commit(
4055
if current_line in total_diff:
4156
return True
4257

43-
# Get the n latest commits, in reverse chronological order
44-
# commits: list[Commit] = repo.iter_commits(max_count=last_n)
45-
# for commit in commits:
46-
# # Get the diffs for this commit
47-
# # https://gitpython.readthedocs.io/en/stable/reference.html?highlight=iter_commits#git.diff.Diffable
48-
# diffs = commit.diff()
49-
50-
# # Check if the line was changed in any of the diffs
51-
# for diff in diffs:
52-
# print("Path eq", diff.b_blob.path, filename)
53-
# if diff.b_blob is None or diff.b_blob.path != filename:
54-
# continue
55-
56-
# # TODO: Get the line number of the diff... Not clear to me this is
57-
# # even doable in a dependable way with "regular" git.
58-
5958
return False
6059

6160

@@ -150,7 +149,7 @@ def get_next_edit_request(self) -> EditRequest:
150149
repo_scoped_file = pathlib.Path(file).relative_to(
151150
self._repo.working_tree_dir
152151
)
153-
if _line_was_in_last_n_commit(
152+
if _too_close_to_human_edits(
154153
self._repo, str(repo_scoped_file), i
155154
):
156155
logging.info(
@@ -208,7 +207,17 @@ def perform_edit(self, edit: EditRequest, agent_cascade: list[EditorAgent]):
208207
# We replace instead of just setting
209208
# `lines[edit.line_range[0]:edit.line_range[1]] = new_lines`
210209
# because the number of lines may be different.
211-
lines = lines[: edit.line_range[0]] + [new_lines] + lines[edit.line_range[1] :]
210+
edited_lines = lines[edit.line_range[0] : edit.line_range[1]]
211+
if Settings().retain_originals_as_comments:
212+
edited_lines = [f"% {line.split('@ai')[0]}\n" for line in edited_lines]
213+
else:
214+
edited_lines = []
215+
lines = (
216+
lines[: edit.line_range[0]]
217+
+ edited_lines
218+
+ [new_lines]
219+
+ lines[edit.line_range[1] :]
220+
)
212221

213222
with open(edit.file_path, "w") as f:
214223
f.writelines(lines)

0 commit comments

Comments
 (0)