Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Claude doesn't include non-code in files (fixes #1030) #1116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions core/llm/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ def __call__(self, text: str) -> str:
return blocks[0]


class OptionalCodeBlockParser:
class OptionalCodeBlockParser(MultiCodeBlockParser):
def __call__(self, text: str) -> str:
text = text.strip()
if text.startswith("```") and text.endswith("\n```"):
# Remove the first and last line. Note the first line may include syntax
# highlighting, so we can't just remove the first 3 characters.
text = "\n".join(text.splitlines()[1:-1]).strip()
elif "\n" not in text and text.startswith("`") and text.endswith("`"):
# Single-line code blocks are wrapped in single backticks
text = text[1:-1]
return text
blocks = super().__call__(text)
# FIXME: if there are more than 1 code block, this means the output actually contains ```,
# so re-parse this with that in mind
if len(blocks) > 1:
raise ValueError(f"Expected a single code block, got {len(blocks)}")
if len(blocks) == 0:
return text.strip()
return blocks[0]


class JSONParser:
Expand Down