-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathwholefile_func_coder.py
More file actions
134 lines (108 loc) · 4.15 KB
/
wholefile_func_coder.py
File metadata and controls
134 lines (108 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from aider import diffs
from ..dump import dump # noqa: F401
from .base_coder import Coder
from .wholefile_func_prompts import WholeFileFunctionPrompts
class WholeFileFunctionCoder(Coder):
functions = [
dict(
name="write_file",
description="create or update one or more files",
parameters=dict(
type="object",
required=["explanation", "files"],
properties=dict(
explanation=dict(
type="string",
description=(
"Step by step plan for the changes to be made to the code (future"
" tense, markdown format)"
),
),
files=dict(
type="array",
items=dict(
type="object",
required=["path", "content"],
properties=dict(
path=dict(
type="string",
description="Path of file to write",
),
content=dict(
type="string",
description="Content to write to the file",
),
),
),
),
),
),
),
]
def __init__(self, *args, **kwargs):
raise RuntimeError("Deprecated, needs to be refactored to support get_edits/apply_edits")
self.gpt_prompts = WholeFileFunctionPrompts()
super().__init__(*args, **kwargs)
def add_assistant_reply_to_cur_messages(self, edited):
if edited:
self.cur_messages += [
dict(role="assistant", content=self.gpt_prompts.redacted_edit_message)
]
else:
self.cur_messages += [dict(role="assistant", content=self.partial_response_content)]
def render_incremental_response(self, final=False):
if self.partial_response_content:
return self.partial_response_content
args = self.parse_partial_args()
if not args:
return
explanation = args.get("explanation")
files = args.get("files", [])
res = ""
if explanation:
res += f"{explanation}\n\n"
for i, file_upd in enumerate(files):
path = file_upd.get("path")
if not path:
continue
content = file_upd.get("content")
if not content:
continue
this_final = (i < len(files) - 1) or final
res += self.live_diffs(path, content, this_final)
return res
def live_diffs(self, fname, content, final):
lines = content.splitlines(keepends=True)
# ending an existing block
full_path = self.abs_root_path(fname)
content = self.io.read_text(full_path)
if content is None:
orig_lines = []
else:
orig_lines = content.splitlines()
show_diff = diffs.diff_partial_update(
orig_lines,
lines,
final,
fname=fname,
).splitlines()
return "\n".join(show_diff)
def _update_files(self):
name = self.partial_response_function_call.get("name")
if name and name != "write_file":
raise ValueError(f'Unknown function_call name="{name}", use name="write_file"')
args = self.parse_partial_args()
if not args:
return
files = args.get("files", [])
edited = set()
for file_upd in files:
path = file_upd.get("path")
if not path:
raise ValueError(f"Missing path parameter: {file_upd}")
content = file_upd.get("content")
if not content:
raise ValueError(f"Missing content parameter: {file_upd}")
if self.allowed_to_edit(path, content):
edited.add(path)
return edited