FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/coders/wholefile_coder.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
11
(11)
Pull requests
8
(8)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
coders
/
wholefile_coder.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
143 lines (115 loc) · 4.96 KB
Breadcrumbs
cecli
/
cecli
/
coders
/
wholefile_coder.py
Copy path
File metadata and controls
143 lines (115 loc) · 4.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
142
143
from
pathlib
import
Path
from
cecli
import
diffs
from
..
dump
import
dump
# noqa: F401
from
.
base_coder
import
Coder
class
WholeFileCoder
(
Coder
):
"""A coder that operates on entire files for code modifications."""
edit_format
=
"whole"
prompt_format
=
"wholefile"
def
render_incremental_response
(
self
,
final
):
try
:
return
self
.
get_edits
(
mode
=
"diff"
)
except
ValueError
:
return
self
.
get_multi_response_content_in_progress
()
def
get_edits
(
self
,
mode
=
"update"
):
content
=
self
.
get_multi_response_content_in_progress
()
chat_files
=
self
.
get_inchat_relative_files
()
output
=
[]
lines
=
content
.
splitlines
(
keepends
=
True
)
edits
=
[]
saw_fname
=
None
fname
=
None
fname_source
=
None
new_lines
=
[]
for
i
,
line
in
enumerate
(
lines
):
if
line
.
startswith
(
self
.
fence
[
0
])
or
line
.
startswith
(
self
.
fence
[
1
]):
if
fname
is
not
None
:
# ending an existing block
saw_fname
=
None
full_path
=
self
.
abs_root_path
(
fname
)
if
mode
==
"diff"
:
output
+=
self
.
do_live_diff
(
full_path
,
new_lines
,
True
)
else
:
edits
.
append
((
fname
,
fname_source
,
new_lines
))
fname
=
None
fname_source
=
None
new_lines
=
[]
continue
# fname==None ... starting a new block
if
i
>
0
:
fname_source
=
"block"
fname
=
lines
[
i
-
1
].
strip
()
fname
=
fname
.
strip
(
"*"
)
# handle **filename.py**
fname
=
fname
.
rstrip
(
":"
)
fname
=
fname
.
strip
(
"`"
)
fname
=
fname
.
lstrip
(
"#"
)
fname
=
fname
.
strip
()
# Issue #1232
if
len
(
fname
)
>
250
:
fname
=
""
# Did gpt prepend a bogus dir? It especially likes to
# include the path/to prefix from the one-shot example in
# the prompt.
if
fname
and
fname
not
in
chat_files
and
Path
(
fname
).
name
in
chat_files
:
fname
=
Path
(
fname
).
name
if
not
fname
:
# blank line? or ``` was on first line i==0
if
saw_fname
:
fname
=
saw_fname
fname_source
=
"saw"
elif
len
(
chat_files
)
==
1
:
fname
=
chat_files
[
0
]
fname_source
=
"chat"
else
:
# TODO: sense which file it is by diff size
raise
ValueError
(
f"No filename provided before
{
self
.
fence
[
0
]
}
in file listing"
)
elif
fname
is
not
None
:
new_lines
.
append
(
line
)
else
:
for
word
in
line
.
strip
().
split
():
word
=
word
.
rstrip
(
".:,;!"
)
for
chat_file
in
chat_files
:
quoted_chat_file
=
f"`
{
chat_file
}
`"
if
word
==
quoted_chat_file
:
saw_fname
=
chat_file
output
.
append
(
line
)
if
mode
==
"diff"
:
if
fname
is
not
None
:
# ending an existing block
full_path
=
(
Path
(
self
.
root
)
/
fname
).
absolute
()
output
+=
self
.
do_live_diff
(
full_path
,
new_lines
,
False
)
return
"
\n
"
.
join
(
output
)
if
fname
:
edits
.
append
((
fname
,
fname_source
,
new_lines
))
seen
=
set
()
refined_edits
=
[]
# process from most reliable filename, to least reliable
for
source
in
(
"block"
,
"saw"
,
"chat"
):
for
fname
,
fname_source
,
new_lines
in
edits
:
if
fname_source
!=
source
:
continue
# if a higher priority source already edited the file, skip
if
fname
in
seen
:
continue
seen
.
add
(
fname
)
refined_edits
.
append
((
fname
,
fname_source
,
new_lines
))
return
refined_edits
def
apply_edits
(
self
,
edits
):
for
path
,
fname_source
,
new_lines
in
edits
:
full_path
=
self
.
abs_root_path
(
path
)
new_lines
=
""
.
join
(
new_lines
)
self
.
io
.
write_text
(
full_path
,
new_lines
)
def
do_live_diff
(
self
,
full_path
,
new_lines
,
final
):
if
Path
(
full_path
).
exists
():
orig_lines
=
self
.
io
.
read_text
(
full_path
)
if
orig_lines
is
not
None
:
orig_lines
=
orig_lines
.
splitlines
(
keepends
=
True
)
show_diff
=
diffs
.
diff_partial_update
(
orig_lines
,
new_lines
,
final
=
final
,
).
splitlines
()
return
show_diff
output
=
[
"```"
]
+
new_lines
+
[
"```"
]
return
output
Back
|
FazBrowse Home
|
New Git URL