Skip to content

Commit

Permalink
code deletions not working, fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Mar 3, 2025
1 parent 099e5c9 commit 41b7ac7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ai_bot/artifact_update_strategies/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def extract_search_replace_blocks(content)
blocks = []
remaining = content

pattern = /<<+\s*SEARCH\s*\n(.*?)\n=+\s*\n(.*?)\n>>+\s*REPLACE/m
pattern = /<<+\s*SEARCH\s*\n(.*?)\n==+\s*(\n(.*?))?\n>>+\s*REPLACE/m
while remaining =~ pattern
blocks << { search: $1.strip, replace: $2.strip }
blocks << { search: $1.strip, replace: $3.to_s.strip }
remaining = $'
end

Expand Down
28 changes: 28 additions & 0 deletions spec/lib/modules/ai_bot/artifact_update_strategies/diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@
expect(artifact.versions.last.css).to eq("body {\n color: red;\n}")
end

it "can handle removal with blank blocks" do
original_css = <<~CSS
body {
color: red;
}
.button {
color: blue;
}
CSS

artifact.update!(css: original_css)

response = <<~RESPONSE
[CSS]
<<<<<<< SEARCH
body {
color: red;
}
=======
>>>>>>> REPLACE
[/CSS]
RESPONSE

DiscourseAi::Completions::Llm.with_prepared_responses([response]) { strategy.apply }

expect(artifact.versions.last.css.strip).to eq(".button {\n color: blue;\n}")
end

it "tracks failed searches" do
original_css = ".button { color: blue; }"
artifact.update!(css: original_css)
Expand Down

0 comments on commit 41b7ac7

Please sign in to comment.