Skip to content

Commit f5b1c99

Browse files
committed
fix: strip opening code fence not on first line and discard preceding lines
1 parent 03dbc7f commit f5b1c99

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

lua/enlighten/ai/anthropic.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,25 @@ function M.create_text_filter(feature)
147147
pending = pending .. text
148148

149149
if not first_line_checked then
150-
local newline_pos = pending:find("\n")
151-
if not newline_pos then
152-
return "" -- Still accumulating first line
150+
-- Skip blank/whitespace-only lines to find the first line with content
151+
while true do
152+
local newline_pos = pending:find("\n")
153+
if not newline_pos then
154+
return "" -- Still accumulating a line
155+
end
156+
local line = pending:sub(1, newline_pos - 1)
157+
if line:match("^%s*$") then
158+
-- Blank line, discard and keep looking
159+
pending = pending:sub(newline_pos + 1)
160+
else
161+
-- First line with content: check for code fence
162+
if line:match("^```") then
163+
pending = pending:sub(newline_pos + 1)
164+
end
165+
first_line_checked = true
166+
break
167+
end
153168
end
154-
local first_line = pending:sub(1, newline_pos - 1)
155-
if first_line:match("^```") then
156-
pending = pending:sub(newline_pos + 1)
157-
end
158-
first_line_checked = true
159169
end
160170

161171
-- Find the last newline - hold back everything after it (potential closing fence)

tests/unit/anthropic_provider_spec.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,33 @@ describe("provider anthropic", function()
217217
collected = collected .. filter.flush()
218218
equals("print('hi')\n", collected)
219219
end)
220+
221+
it("should strip opening fence preceded by blank lines", function()
222+
local filter = anthropic.create_text_filter("edit")
223+
local collected = ""
224+
collected = collected .. filter.process("\n\n```go\nfunc main() {\n")
225+
collected = collected .. filter.process("}\n")
226+
collected = collected .. filter.flush()
227+
equals("func main() {\n}\n", collected)
228+
end)
229+
230+
it("should strip opening fence preceded by blank lines across chunks", function()
231+
local filter = anthropic.create_text_filter("edit")
232+
local collected = ""
233+
-- First chunk is just blank lines, no content line yet
234+
collected = collected .. filter.process("\n\n")
235+
-- Second chunk has the fence and code
236+
collected = collected .. filter.process("```go\nfunc main() {\n}\n")
237+
collected = collected .. filter.flush()
238+
equals("func main() {\n}\n", collected)
239+
end)
240+
241+
it("should strip opening fence preceded by whitespace-only lines", function()
242+
local filter = anthropic.create_text_filter("edit")
243+
local collected = ""
244+
collected = collected .. filter.process(" \n\t\n```lua\nlocal x = 1\n")
245+
collected = collected .. filter.flush()
246+
equals("local x = 1\n", collected)
247+
end)
220248
end)
221249
end)

0 commit comments

Comments
 (0)