-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy patheditblock_func_coder.py
More file actions
141 lines (121 loc) · 5.19 KB
/
editblock_func_coder.py
File metadata and controls
141 lines (121 loc) · 5.19 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
135
136
137
138
139
140
141
import json
from ..dump import dump # noqa: F401
from .base_coder import Coder
from .editblock_coder import do_replace
from .editblock_func_prompts import EditBlockFunctionPrompts
class EditBlockFunctionCoder(Coder):
functions = [
dict(
name="replace_lines",
description="create or update one or more files",
parameters=dict(
type="object",
required=["explanation", "edits"],
properties=dict(
explanation=dict(
type="string",
description=(
"Step by step plan for the changes to be made to the code (future"
" tense, markdown format)"
),
),
edits=dict(
type="array",
items=dict(
type="object",
required=["path", "original_lines", "updated_lines"],
properties=dict(
path=dict(
type="string",
description="Path of file to edit",
),
original_lines=dict(
type="array",
items=dict(
type="string",
),
description=(
"A unique stretch of lines from the original file,"
" including all whitespace, without skipping any lines"
),
),
updated_lines=dict(
type="array",
items=dict(
type="string",
),
description="New content to replace the `original_lines` with",
),
),
),
),
),
),
),
]
def __init__(self, code_format, *args, **kwargs):
raise RuntimeError("Deprecated, needs to be refactored to support get_edits/apply_edits")
self.code_format = code_format
if code_format == "string":
original_lines = dict(
type="string",
description=(
"A unique stretch of lines from the original file, including all"
" whitespace and newlines, without skipping any lines"
),
)
updated_lines = dict(
type="string",
description="New content to replace the `original_lines` with",
)
self.functions[0]["parameters"]["properties"]["edits"]["items"]["properties"][
"original_lines"
] = original_lines
self.functions[0]["parameters"]["properties"]["edits"]["items"]["properties"][
"updated_lines"
] = updated_lines
self.gpt_prompts = EditBlockFunctionPrompts()
super().__init__(*args, **kwargs)
def render_incremental_response(self, final=False):
if self.partial_response_content:
return self.partial_response_content
args = self.parse_partial_args()
res = json.dumps(args, indent=4)
return res
def _update_files(self):
name = self.partial_response_function_call.get("name")
if name and name != "replace_lines":
raise ValueError(f'Unknown function_call name="{name}", use name="replace_lines"')
args = self.parse_partial_args()
if not args:
return
edits = args.get("edits", [])
edited = set()
for edit in edits:
path = get_arg(edit, "path")
original = get_arg(edit, "original_lines")
updated = get_arg(edit, "updated_lines")
# gpt-3.5 returns lists even when instructed to return a string!
if self.code_format == "list" or type(original) is list:
original = "\n".join(original)
if self.code_format == "list" or type(updated) is list:
updated = "\n".join(updated)
if original and not original.endswith("\n"):
original += "\n"
if updated and not updated.endswith("\n"):
updated += "\n"
full_path = self.allowed_to_edit(path)
if not full_path:
continue
content = self.io.read_text(full_path)
content = do_replace(full_path, content, original, updated)
if content:
self.io.write_text(full_path, content)
edited.add(path)
continue
self.io.tool_error(f"Failed to apply edit to {path}")
return edited
def get_arg(edit, arg):
if arg not in edit:
raise ValueError(f"Missing `{arg}` parameter: {edit}")
return edit[arg]