FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/coders/hashline_coder.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
11
(11)
Pull requests
7
(7)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
coders
/
hashline_coder.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
818 lines (629 loc) · 26.3 KB
Breadcrumbs
cecli
/
cecli
/
coders
/
hashline_coder.py
Copy path
File metadata and controls
818 lines (629 loc) · 26.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import
difflib
import
json
import
math
import
re
import
sys
from
difflib
import
SequenceMatcher
from
pathlib
import
Path
from
cecli
import
utils
from
cecli
.
coders
.
base_coder
import
Coder
from
cecli
.
helpers
.
hashline
import
(
ContentHashError
,
apply_hashline_operations
,
strip_hashline
,
)
from
..
dump
import
dump
# noqa: F401
class
HashLineCoder
(
Coder
):
"""A coder that uses hashlines blocks for code modifications."""
edit_format
=
"hashline"
prompt_format
=
"hashline"
hashlines
=
True
def
get_edits
(
self
):
content
=
self
.
partial_response_content
# might raise ValueError for malformed ORIG/UPD blocks
edits
=
list
(
find_original_update_blocks
(
content
,
self
.
fence
,
self
.
get_inchat_relative_files
(),
)
)
self
.
shell_commands
+=
[
edit
[
1
]
for
edit
in
edits
if
edit
[
0
]
is
None
]
edits
=
[
edit
for
edit
in
edits
if
edit
[
0
]
is
not
None
]
return
edits
def
apply_edits_dry_run
(
self
,
edits
):
return
self
.
apply_edits
(
edits
,
dry_run
=
True
)
def
apply_edits
(
self
,
edits
,
dry_run
=
False
):
failed
=
[]
passed
=
[]
updated_edits
=
[]
# Group hashline edits by file path
hashline_edits_by_file
=
{}
original_hashline_edits_by_file
=
{}
regular_edits
=
[]
for
edit
in
edits
:
path
,
original
,
updated
=
edit
# Check if this is a hashline edit (list with 3 elements)
is_hashline
=
False
if
isinstance
(
original
,
list
):
# Check if this is the original format: [start_hash, end_hash, operation]
if
len
(
original
)
==
3
and
all
(
isinstance
(
item
,
str
)
for
item
in
original
):
start_hash
,
end_hash
,
operation
=
original
# Validate operation
if
operation
in
[
"replace"
,
"insert"
,
"delete"
,
"cancel"
]:
# Validate hashline format
if
isinstance
(
start_hash
,
str
)
and
(
operation
==
"insert"
or
isinstance
(
end_hash
,
str
)
):
if
path
not
in
hashline_edits_by_file
:
hashline_edits_by_file
[
path
]
=
[]
original_hashline_edits_by_file
[
path
]
=
[]
hashline_edits_by_file
[
path
].
append
(
{
"start_line_hash"
:
start_hash
,
"end_line_hash"
: (
end_hash
if
operation
!=
"insert"
else
start_hash
),
"operation"
:
operation
,
"text"
:
updated
if
operation
in
[
"replace"
,
"insert"
]
else
None
,
}
)
original_hashline_edits_by_file
[
path
].
append
(
edit
)
is_hashline
=
True
# Check if this is the transformed format: list of dictionaries (from dry run)
elif
(
not
is_hashline
and
isinstance
(
original
,
list
)
and
len
(
original
)
>
0
and
isinstance
(
original
[
0
],
dict
)
and
"start_line_hash"
in
original
[
0
]
):
if
path
not
in
hashline_edits_by_file
:
hashline_edits_by_file
[
path
]
=
[]
original_hashline_edits_by_file
[
path
]
=
[]
for
op
in
original
:
hashline_edits_by_file
[
path
].
append
(
op
)
# If it's already transformed, we should ideally keep it as is for dry run
# but the issue is that it might have been transformed by a previous dry run
# and we want to avoid double-wrapping or losing the "original" nature.
original_hashline_edits_by_file
[
path
].
append
(
edit
)
is_hashline
=
True
if
not
is_hashline
:
regular_edits
.
append
(
edit
)
# Process hashline edits in batch for each file
for
path
,
operations
in
hashline_edits_by_file
.
items
():
full_path
=
self
.
abs_root_path
(
path
)
new_content
=
None
try
:
# Read existing content or use empty string for new files
if
Path
(
full_path
).
exists
():
content
=
self
.
io
.
read_text
(
full_path
)
else
:
content
=
""
# Apply all hashline operations for this file in batch
new_content
,
_
,
_
=
apply_hashline_operations
(
original_content
=
strip_hashline
(
content
),
operations
=
operations
,
)
if
dry_run
:
# For dry runs, preserve the original edit format
updated_edits
.
extend
(
original_hashline_edits_by_file
[
path
])
else
:
updated_edits
.
append
((
path
,
operations
,
"Batch hashline operations"
))
# Write the file if operation was successful (no exception)
# new_content could be empty string for empty file creation
if
not
dry_run
:
self
.
io
.
write_text
(
full_path
,
new_content
)
passed
.
append
((
path
,
operations
,
"Batch hashline operations"
))
except
(
ValueError
,
ContentHashError
)
as
e
:
# Record failure
failed
.
append
((
path
,
operations
,
f"Hashline batch operation failed:
{
e
}
"
))
continue
# Process regular edits one by one (existing logic)
for
edit
in
regular_edits
:
path
,
original
,
updated
=
edit
full_path
=
self
.
abs_root_path
(
path
)
new_content
=
None
if
not
isinstance
(
original
,
str
)
or
not
isinstance
(
updated
,
str
):
continue
# Always try do_replace, it handles new file creation
content
=
self
.
io
.
read_text
(
full_path
)
if
Path
(
full_path
).
exists
()
else
""
new_content
=
do_replace
(
full_path
,
content
,
original
,
updated
,
self
.
fence
)
# If the edit failed, and
# this is not a "create a new file" with an empty original...
# https://github.com/Aider-AI/aider/issues/2258
if
not
new_content
and
(
(
isinstance
(
original
,
str
)
and
original
.
strip
())
or
(
isinstance
(
original
,
list
)
and
len
(
original
)
>
0
)
):
# try patching any of the other files in the chat
for
full_path
in
self
.
abs_fnames
:
content
=
self
.
io
.
read_text
(
full_path
)
new_content
=
do_replace
(
full_path
,
content
,
original
,
updated
,
self
.
fence
)
if
new_content
:
path
=
self
.
get_rel_fname
(
full_path
)
break
updated_edits
.
append
((
path
,
original
,
updated
))
if
new_content
is
not
None
:
if
not
dry_run
:
self
.
io
.
write_text
(
full_path
,
new_content
)
passed
.
append
(
edit
)
else
:
failed
.
append
(
edit
)
if
dry_run
:
return
updated_edits
if
not
failed
:
return
blocks
=
"block"
if
len
(
failed
)
==
1
else
"blocks"
res
=
f"#
{
len
(
failed
)
}
LOCATE/CONTENTS
{
blocks
}
failed to match!
\n
"
for
edit
in
failed
:
path
,
original
,
updated
=
edit
full_path
=
self
.
abs_root_path
(
path
)
content
=
self
.
io
.
read_text
(
full_path
)
res
+=
f"""
## SearchReplaceNoExactMatch: This LOCATE block failed to exactly match lines in
{
path
}
<<<<<<< LOCATE
{
original
}
=======
{
updated
}
>>>>>>> CONTENTS
Please try this operation again using the hashline range that you want to modify
"""
if
isinstance
(
original
,
str
):
did_you_mean
=
find_similar_lines
(
original
,
content
)
if
did_you_mean
:
res
+=
f"""Did you mean to match some of these actual lines from
{
path
}
?
{
self
.
fence
[
0
]
}
{
did_you_mean
}
{
self
.
fence
[
1
]
}
"""
if
updated
in
content
and
updated
:
res
+=
f"""Are you sure you need this LOCATE/CONTENTS block?
The CONTENTS lines are already in
{
path
}
!
"""
res
+=
(
"The LOCATE section must be a valid JSON array in the format:
\n
"
'["{start hashline}", "{end hashline}", "{operation}"]
\n
'
"Hashline prefixes must have the structure `{4 char hash}` (e.g., `20Bv`)"
" and match one found directly in the file"
)
if
passed
:
pblocks
=
"block"
if
len
(
passed
)
==
1
else
"blocks"
res
+=
f"""
# The other
{
len
(
passed
)
}
LOCATE/CONTENTS
{
pblocks
}
were applied successfully.
Don't re-send them.
Just reply with fixed versions of the
{
blocks
}
above that failed to match.
"""
raise
ValueError
(
res
)
def
prep
(
content
):
if
content
and
not
content
.
endswith
(
"
\n
"
):
content
+=
"
\n
"
lines
=
content
.
splitlines
(
keepends
=
True
)
return
content
,
lines
def
perfect_or_whitespace
(
whole_lines
,
part_lines
,
replace_lines
):
# Try for a perfect match
res
=
perfect_replace
(
whole_lines
,
part_lines
,
replace_lines
)
if
res
:
return
res
# Try being flexible about leading whitespace
res
=
replace_part_with_missing_leading_whitespace
(
whole_lines
,
part_lines
,
replace_lines
)
if
res
:
return
res
def
perfect_replace
(
whole_lines
,
part_lines
,
replace_lines
):
part_tup
=
tuple
(
part_lines
)
part_len
=
len
(
part_lines
)
for
i
in
range
(
len
(
whole_lines
)
-
part_len
+
1
):
whole_tup
=
tuple
(
whole_lines
[
i
:
i
+
part_len
])
if
part_tup
==
whole_tup
:
res
=
whole_lines
[:
i
]
+
replace_lines
+
whole_lines
[
i
+
part_len
:]
return
""
.
join
(
res
)
def
replace_most_similar_chunk
(
whole
,
part
,
replace
):
"""Best efforts to find the `part` lines in `whole` and replace them with `replace`"""
whole
,
whole_lines
=
prep
(
whole
)
part
,
part_lines
=
prep
(
part
)
replace
,
replace_lines
=
prep
(
replace
)
res
=
perfect_or_whitespace
(
whole_lines
,
part_lines
,
replace_lines
)
if
res
:
return
res
# drop leading empty line, GPT sometimes adds them spuriously (issue #25)
if
len
(
part_lines
)
>
2
and
not
part_lines
[
0
].
strip
():
skip_blank_line_part_lines
=
part_lines
[
1
:]
res
=
perfect_or_whitespace
(
whole_lines
,
skip_blank_line_part_lines
,
replace_lines
)
if
res
:
return
res
# Try to handle when it elides code with ...
try
:
res
=
try_dotdotdots
(
whole
,
part
,
replace
)
if
res
:
return
res
except
ValueError
:
pass
return
# Try fuzzy matching
res
=
replace_closest_edit_distance
(
whole_lines
,
part
,
part_lines
,
replace_lines
)
if
res
:
return
res
def
try_dotdotdots
(
whole
,
part
,
replace
):
"""
See if the edit block has ... lines.
If not, return none.
If yes, try and do a perfect edit with the ... chunks.
If there's a mismatch or otherwise imperfect edit, raise ValueError.
If perfect edit succeeds, return the updated whole.
"""
dots_re
=
re
.
compile
(
r"(^\s*\.\.\.\n)"
,
re
.
MULTILINE
|
re
.
DOTALL
)
part_pieces
=
re
.
split
(
dots_re
,
part
)
replace_pieces
=
re
.
split
(
dots_re
,
replace
)
if
len
(
part_pieces
)
!=
len
(
replace_pieces
):
raise
ValueError
(
"Unpaired ... in LOCATE/CONTENTS block"
)
if
len
(
part_pieces
)
==
1
:
# no dots in this edit block, just return None
return
# Compare odd strings in part_pieces and replace_pieces
all_dots_match
=
all
(
part_pieces
[
i
]
==
replace_pieces
[
i
]
for
i
in
range
(
1
,
len
(
part_pieces
),
2
))
if
not
all_dots_match
:
raise
ValueError
(
"Unmatched ... in LOCATE/CONTENTS block"
)
part_pieces
=
[
part_pieces
[
i
]
for
i
in
range
(
0
,
len
(
part_pieces
),
2
)]
replace_pieces
=
[
replace_pieces
[
i
]
for
i
in
range
(
0
,
len
(
replace_pieces
),
2
)]
pairs
=
zip
(
part_pieces
,
replace_pieces
)
for
part
,
replace
in
pairs
:
if
not
part
and
not
replace
:
continue
if
not
part
and
replace
:
if
not
whole
.
endswith
(
"
\n
"
):
whole
+=
"
\n
"
whole
+=
replace
continue
if
whole
.
count
(
part
)
==
0
:
raise
ValueError
if
whole
.
count
(
part
)
>
1
:
raise
ValueError
whole
=
whole
.
replace
(
part
,
replace
,
1
)
return
whole
def
replace_part_with_missing_leading_whitespace
(
whole_lines
,
part_lines
,
replace_lines
):
# GPT often messes up leading whitespace.
# It usually does it uniformly across the ORIG and UPD blocks.
# Either omitting all leading whitespace, or including only some of it.
# Outdent everything in part_lines and replace_lines by the max fixed amount possible
leading
=
[
len
(
p
)
-
len
(
p
.
lstrip
())
for
p
in
part_lines
if
p
.
strip
()]
+
[
len
(
p
)
-
len
(
p
.
lstrip
())
for
p
in
replace_lines
if
p
.
strip
()
]
if
leading
and
min
(
leading
):
num_leading
=
min
(
leading
)
part_lines
=
[
p
[
num_leading
:]
if
p
.
strip
()
else
p
for
p
in
part_lines
]
replace_lines
=
[
p
[
num_leading
:]
if
p
.
strip
()
else
p
for
p
in
replace_lines
]
# can we find an exact match not including the leading whitespace
num_part_lines
=
len
(
part_lines
)
for
i
in
range
(
len
(
whole_lines
)
-
num_part_lines
+
1
):
add_leading
=
match_but_for_leading_whitespace
(
whole_lines
[
i
:
i
+
num_part_lines
],
part_lines
)
if
add_leading
is
None
:
continue
replace_lines
=
[
add_leading
+
rline
if
rline
.
strip
()
else
rline
for
rline
in
replace_lines
]
whole_lines
=
whole_lines
[:
i
]
+
replace_lines
+
whole_lines
[
i
+
num_part_lines
:]
return
""
.
join
(
whole_lines
)
return
None
def
match_but_for_leading_whitespace
(
whole_lines
,
part_lines
):
num
=
len
(
whole_lines
)
# does the non-whitespace all agree?
if
not
all
(
whole_lines
[
i
].
lstrip
()
==
part_lines
[
i
].
lstrip
()
for
i
in
range
(
num
)):
return
# are they all offset the same?
add
=
set
(
whole_lines
[
i
][:
len
(
whole_lines
[
i
])
-
len
(
part_lines
[
i
])]
for
i
in
range
(
num
)
if
whole_lines
[
i
].
strip
()
)
if
len
(
add
)
!=
1
:
return
return
add
.
pop
()
def
replace_closest_edit_distance
(
whole_lines
,
part
,
part_lines
,
replace_lines
):
similarity_thresh
=
0.8
max_similarity
=
0
most_similar_chunk_start
=
-
1
most_similar_chunk_end
=
-
1
scale
=
0.1
min_len
=
math
.
floor
(
len
(
part_lines
)
*
(
1
-
scale
))
max_len
=
math
.
ceil
(
len
(
part_lines
)
*
(
1
+
scale
))
for
length
in
range
(
min_len
,
max_len
):
for
i
in
range
(
len
(
whole_lines
)
-
length
+
1
):
chunk
=
whole_lines
[
i
:
i
+
length
]
chunk
=
""
.
join
(
chunk
)
similarity
=
SequenceMatcher
(
None
,
chunk
,
part
).
ratio
()
if
similarity
>
max_similarity
and
similarity
:
max_similarity
=
similarity
most_similar_chunk_start
=
i
most_similar_chunk_end
=
i
+
length
if
max_similarity
<
similarity_thresh
:
return
modified_whole
=
(
whole_lines
[:
most_similar_chunk_start
]
+
replace_lines
+
whole_lines
[
most_similar_chunk_end
:]
)
modified_whole
=
""
.
join
(
modified_whole
)
return
modified_whole
DEFAULT_FENCE
=
(
"`"
*
3
,
"`"
*
3
)
def
strip_quoted_wrapping
(
res
,
fname
=
None
,
fence
=
DEFAULT_FENCE
):
"""
Given an input string which may have extra "wrapping" around it, remove the wrapping.
For example:
filename.ext
```
We just want this content
Not the filename and triple quotes
```
"""
if
not
res
:
return
res
res
=
res
.
splitlines
()
if
fname
and
res
[
0
].
strip
().
endswith
(
Path
(
fname
).
name
):
res
=
res
[
1
:]
if
res
[
0
].
startswith
(
fence
[
0
])
and
res
[
-
1
].
startswith
(
fence
[
1
]):
res
=
res
[
1
:
-
1
]
res
=
"
\n
"
.
join
(
res
)
if
res
and
res
[
-
1
]
!=
"
\n
"
:
res
+=
"
\n
"
return
res
def
do_replace
(
fname
,
content
,
before_text
,
after_text
,
fence
=
None
):
# Check if before_text is a hashline JSON block (list with 3 elements)
if
isinstance
(
before_text
,
list
)
and
len
(
before_text
)
==
3
:
pass
# Original logic for regular edit blocks
before_text
=
strip_quoted_wrapping
(
before_text
,
fname
,
fence
)
after_text
=
strip_quoted_wrapping
(
after_text
,
fname
,
fence
)
fname
=
Path
(
fname
)
# does it want to make a new file?
if
not
fname
.
exists
()
and
not
before_text
.
strip
():
fname
.
touch
()
content
=
""
if
content
is
None
:
return
if
not
before_text
.
strip
():
# append to existing file, or start a new file
new_content
=
content
+
after_text
else
:
new_content
=
replace_most_similar_chunk
(
content
,
before_text
,
after_text
)
return
new_content
HEAD
=
r"^<{5,9} LOCATE>?\s*$"
DIVIDER
=
r"^={5,9}\s*$"
UPDATED
=
r"^>{5,9} CONTENTS\s*$"
HEAD_ERR
=
"<<<<<<< LOCATE"
DIVIDER_ERR
=
"======="
UPDATED_ERR
=
">>>>>>> CONTENTS"
separators
=
"|"
.
join
([
HEAD
,
DIVIDER
,
UPDATED
])
split_re
=
re
.
compile
(
r"^((?:"
+
separators
+
r")[ ]*\n)"
,
re
.
MULTILINE
|
re
.
DOTALL
)
missing_filename_err
=
(
"Bad/missing filename. The filename must be alone on the line before the opening fence"
" {fence[0]}"
)
# Always be willing to treat triple-backticks as a fence when searching for filenames
triple_backticks
=
"`"
*
3
def
strip_filename
(
filename
,
fence
):
filename
=
filename
.
strip
()
if
filename
==
"..."
:
return
start_fence
=
fence
[
0
]
if
filename
.
startswith
(
start_fence
):
candidate
=
filename
[
len
(
start_fence
) :]
if
candidate
and
(
"."
in
candidate
or
"/"
in
candidate
):
return
candidate
return
if
filename
.
startswith
(
triple_backticks
):
candidate
=
filename
[
len
(
triple_backticks
) :]
if
candidate
and
(
"."
in
candidate
or
"/"
in
candidate
):
return
candidate
return
filename
=
filename
.
rstrip
(
":"
)
filename
=
filename
.
lstrip
(
"#"
)
filename
=
filename
.
strip
()
filename
=
filename
.
strip
(
"`"
)
filename
=
filename
.
strip
(
"*"
)
# https://github.com/Aider-AI/aider/issues/1158
# filename = filename.replace("\\_", "_")
return
filename
def
find_original_update_blocks
(
content
,
fence
=
DEFAULT_FENCE
,
valid_fnames
=
None
):
lines
=
content
.
splitlines
(
keepends
=
True
)
i
=
0
current_filename
=
None
head_pattern
=
re
.
compile
(
HEAD
)
divider_pattern
=
re
.
compile
(
DIVIDER
)
updated_pattern
=
re
.
compile
(
UPDATED
)
while
i
<
len
(
lines
):
line
=
lines
[
i
]
# Check for shell code blocks
shell_starts
=
[
"```bash"
,
"```sh"
,
"```shell"
,
"```cmd"
,
"```batch"
,
"```powershell"
,
"```ps1"
,
"```zsh"
,
"```fish"
,
"```ksh"
,
"```csh"
,
"```tcsh"
,
]
# Check if the next line or the one after that is an editblock
next_is_editblock
=
(
i
+
1
<
len
(
lines
)
and
head_pattern
.
match
(
lines
[
i
+
1
].
strip
())
or
i
+
2
<
len
(
lines
)
and
head_pattern
.
match
(
lines
[
i
+
2
].
strip
())
)
if
any
(
line
.
strip
().
startswith
(
start
)
for
start
in
shell_starts
)
and
not
next_is_editblock
:
shell_content
=
[]
i
+=
1
while
i
<
len
(
lines
)
and
not
lines
[
i
].
strip
().
startswith
(
"```"
):
shell_content
.
append
(
lines
[
i
])
i
+=
1
if
i
<
len
(
lines
)
and
lines
[
i
].
strip
().
startswith
(
"```"
):
i
+=
1
# Skip the closing ```
yield
None
,
""
.
join
(
shell_content
)
continue
# Check for LOCATE/CONTENTS blocks
if
head_pattern
.
match
(
line
.
strip
()):
try
:
# if next line after HEAD exists and is DIVIDER, it's a new file
if
i
+
1
<
len
(
lines
)
and
divider_pattern
.
match
(
lines
[
i
+
1
].
strip
()):
filename
=
find_filename
(
lines
[
max
(
0
,
i
-
3
) :
i
],
fence
,
None
)
else
:
filename
=
find_filename
(
lines
[
max
(
0
,
i
-
3
) :
i
],
fence
,
valid_fnames
)
if
not
filename
:
if
current_filename
:
filename
=
current_filename
else
:
raise
ValueError
(
missing_filename_err
.
format
(
fence
=
fence
))
current_filename
=
filename
original_text
=
[]
i
+=
1
while
i
<
len
(
lines
)
and
not
divider_pattern
.
match
(
lines
[
i
].
strip
()):
original_text
.
append
(
lines
[
i
])
i
+=
1
if
i
>=
len
(
lines
)
or
not
divider_pattern
.
match
(
lines
[
i
].
strip
()):
raise
ValueError
(
f"Expected `
{
DIVIDER_ERR
}
`"
)
updated_text
=
[]
i
+=
1
while
i
<
len
(
lines
)
and
not
(
updated_pattern
.
match
(
lines
[
i
].
strip
())
or
divider_pattern
.
match
(
lines
[
i
].
strip
())
):
updated_text
.
append
(
lines
[
i
])
i
+=
1
if
i
>=
len
(
lines
)
or
not
(
updated_pattern
.
match
(
lines
[
i
].
strip
())
or
divider_pattern
.
match
(
lines
[
i
].
strip
())
):
raise
ValueError
(
f"Expected `
{
UPDATED_ERR
}
` or `
{
DIVIDER_ERR
}
`"
)
original_text_str
=
""
.
join
(
original_text
)
updated_text_str
=
""
.
join
(
updated_text
)
# Check if original_text is a hashline JSON block
try
:
# Try to parse as JSON
try
:
parsed
=
json
.
loads
(
original_text_str
.
strip
())
if
not
isinstance
(
parsed
,
list
)
or
len
(
parsed
)
!=
3
:
parsed
=
extract_base64url_parts
(
original_text_str
.
strip
())
except
Exception
:
parsed
=
extract_base64url_parts
(
original_text_str
.
strip
())
# Check if it's a list with 3 elements (start_hash, end_hash, operation)
if
isinstance
(
parsed
,
list
)
and
len
(
parsed
)
==
3
:
# Validate the format: all strings
if
all
(
isinstance
(
item
,
str
)
for
item
in
parsed
):
# Check if first two items look like hashline format (e.g., "1ab")
if
parsed
[
2
]
in
[
"replace"
,
"insert"
,
"delete"
,
"cancel"
]:
# This is a hashline JSON block
yield
filename
,
parsed
,
updated_text_str
continue
except
(
json
.
JSONDecodeError
,
ValueError
):
# Not a valid JSON, treat as regular edit block
pass
yield
filename
,
original_text_str
,
updated_text_str
except
ValueError
as
e
:
processed
=
""
.
join
(
lines
[:
i
+
1
])
err
=
e
.
args
[
0
]
raise
ValueError
(
f"
{
processed
}
\n
^^^
{
err
}
"
)
i
+=
1
def
extract_base64url_parts
(
input_string
):
# Remove any character that is NOT a-z, A-Z, 0-9, -, or _
clean_str
=
re
.
sub
(
r"[^a-zA-Z0-9\~_@]"
,
""
,
input_string
)
return
[
clean_str
[:
4
],
# First 4 chars
clean_str
[
4
:
8
],
# Second 4 chars
clean_str
[
8
:],
# The rest
]
def
find_filename
(
lines
,
fence
,
valid_fnames
):
"""
Deepseek Coder v2 has been doing this:
```python
word_count.py
```
```python
<<<<<<< LOCATE
...
This is a more flexible search back for filenames.
"""
if
valid_fnames
is
None
:
valid_fnames
=
[]
# Go back through the 3 preceding lines
lines
.
reverse
()
lines
=
lines
[:
3
]
filenames
=
[]
for
line
in
lines
:
# If we find a filename, done
filename
=
strip_filename
(
line
,
fence
)
if
filename
:
filenames
.
append
(
filename
)
# Only continue as long as we keep seeing fences
if
not
line
.
startswith
(
fence
[
0
])
and
not
line
.
startswith
(
triple_backticks
):
break
if
not
filenames
:
return
# pick the *best* filename found
# Check for exact match first
for
fname
in
filenames
:
if
fname
in
valid_fnames
:
return
fname
# Check for partial match (basename match)
for
fname
in
filenames
:
for
vfn
in
valid_fnames
:
if
fname
==
Path
(
vfn
).
name
:
return
vfn
# Perform fuzzy matching with valid_fnames
for
fname
in
filenames
:
close_matches
=
difflib
.
get_close_matches
(
fname
,
valid_fnames
,
n
=
1
,
cutoff
=
0.8
)
if
len
(
close_matches
)
==
1
:
return
close_matches
[
0
]
# If no fuzzy match, look for a file w/extension
for
fname
in
filenames
:
if
"."
in
fname
:
return
fname
if
filenames
:
return
filenames
[
0
]
def
find_similar_lines
(
search_lines
,
content_lines
,
threshold
=
0.6
):
search_lines
=
search_lines
.
splitlines
()
content_lines
=
content_lines
.
splitlines
()
# Handle empty search lines
if
not
search_lines
:
return
""
best_ratio
=
0
best_match
=
None
for
i
in
range
(
len
(
content_lines
)
-
len
(
search_lines
)
+
1
):
chunk
=
content_lines
[
i
:
i
+
len
(
search_lines
)]
ratio
=
SequenceMatcher
(
None
,
search_lines
,
chunk
).
ratio
()
if
ratio
>
best_ratio
:
best_ratio
=
ratio
best_match
=
chunk
best_match_i
=
i
if
best_ratio
<
threshold
:
return
""
if
best_match
[
0
]
==
search_lines
[
0
]
and
best_match
[
-
1
]
==
search_lines
[
-
1
]:
return
"
\n
"
.
join
(
best_match
)
N
=
5
best_match_end
=
min
(
len
(
content_lines
),
best_match_i
+
len
(
search_lines
)
+
N
)
best_match_i
=
max
(
0
,
best_match_i
-
N
)
best
=
content_lines
[
best_match_i
:
best_match_end
]
return
"
\n
"
.
join
(
best
)
def
main
():
history_md
=
Path
(
sys
.
argv
[
1
]).
read_text
()
if
not
history_md
:
return
messages
=
utils
.
split_chat_history_markdown
(
history_md
)
for
msg
in
messages
:
msg
=
msg
[
"content"
]
edits
=
list
(
find_original_update_blocks
(
msg
))
for
fname
,
before
,
after
in
edits
:
# Compute diff
diff
=
difflib
.
unified_diff
(
before
.
splitlines
(
keepends
=
True
),
after
.
splitlines
(
keepends
=
True
),
fromfile
=
"before"
,
tofile
=
"after"
,
)
diff
=
""
.
join
(
diff
)
dump
(
before
)
dump
(
after
)
dump
(
diff
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL