mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
Fix code repair for raw (non-markdown) source code input
The Python client sends raw source code, not markdown-wrapped blocks.
split_markdown_code() returned {} for raw input, making SearchAndReplaceDiff
have nothing to patch, so repairs always returned empty string.
Now falls back to {"file.py": raw_code} when markdown parsing yields nothing,
and is_valid() handles raw code blocks instead of only markdown-wrapped ones.
This commit is contained in:
parent
34eb74342d
commit
3d1b843da4
1 changed files with 16 additions and 12 deletions
|
|
@ -126,29 +126,33 @@ class CodeRepairContext:
|
||||||
|
|
||||||
def apply_patches_to_optimized_code(self, llm_res: str) -> str:
|
def apply_patches_to_optimized_code(self, llm_res: str) -> str:
|
||||||
file_to_code = split_markdown_code(self.data.modified_source_code, self.data.language)
|
file_to_code = split_markdown_code(self.data.modified_source_code, self.data.language)
|
||||||
|
raw_input = not file_to_code
|
||||||
|
if raw_input:
|
||||||
|
file_to_code = {"file.py": self.data.modified_source_code}
|
||||||
diff: Diff = SearchAndReplaceDiff(content=llm_res, source_code=file_to_code)
|
diff: Diff = SearchAndReplaceDiff(content=llm_res, source_code=file_to_code)
|
||||||
file_to_code = diff.run()
|
file_to_code = diff.run()
|
||||||
|
if raw_input:
|
||||||
|
return file_to_code.get("file.py", "")
|
||||||
return group_code(file_to_code, self.data.language)
|
return group_code(file_to_code, self.data.language)
|
||||||
|
|
||||||
def is_valid(self, new_refined_code: str) -> bool:
|
def is_valid(self, new_refined_code: str) -> bool:
|
||||||
if is_markdown_structure_changed(new_refined_code, self.data.modified_source_code, self.data.language):
|
code_blocks = split_markdown_code(new_refined_code, self.data.language)
|
||||||
return False
|
if code_blocks:
|
||||||
valid = True
|
if is_markdown_structure_changed(new_refined_code, self.data.modified_source_code, self.data.language):
|
||||||
for code in split_markdown_code(new_refined_code, self.data.language).values():
|
return False
|
||||||
|
code_items = code_blocks.values()
|
||||||
|
else:
|
||||||
|
code_items = [new_refined_code]
|
||||||
|
for code in code_items:
|
||||||
stripped_code = code.strip()
|
stripped_code = code.strip()
|
||||||
if not stripped_code:
|
if not stripped_code:
|
||||||
valid = False
|
return False
|
||||||
break
|
|
||||||
# Only validate Python syntax with libcst
|
|
||||||
if self.data.language == "python":
|
if self.data.language == "python":
|
||||||
try:
|
try:
|
||||||
parse_module_to_cst(code)
|
parse_module_to_cst(code)
|
||||||
except cst.ParserSyntaxError:
|
except cst.ParserSyntaxError:
|
||||||
valid = False
|
return False
|
||||||
break
|
return True
|
||||||
# For JavaScript/TypeScript, basic validation (non-empty code)
|
|
||||||
# More sophisticated validation could be added later
|
|
||||||
return valid
|
|
||||||
|
|
||||||
def validate_module(self) -> None:
|
def validate_module(self) -> None:
|
||||||
"""Validate the module syntax based on language."""
|
"""Validate the module syntax based on language."""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue