diff --git a/lua/avante/providers/openai.lua b/lua/avante/providers/openai.lua index 5e255da..25b658f 100644 --- a/lua/avante/providers/openai.lua +++ b/lua/avante/providers/openai.lua @@ -243,8 +243,27 @@ function M:add_text_message(ctx, text, state, opts) }) ctx.content_uuid = msg.uuid local msgs = { msg } + local xml_content = ctx.content + local xml_lines = vim.split(xml_content, "\n") + local cleaned_xml_lines = {} + local prev_tool_name = nil + for _, line in ipairs(xml_lines) do + if line:match("") then + local tool_name = line:match("(.*)") + if tool_name then prev_tool_name = tool_name end + elseif line:match("") then + if prev_tool_name then table.insert(cleaned_xml_lines, "<" .. prev_tool_name .. ">") end + goto continue + elseif line:match("") then + if prev_tool_name then table.insert(cleaned_xml_lines, "") end + goto continue + end + table.insert(cleaned_xml_lines, line) + ::continue:: + end + local cleaned_xml_content = table.concat(cleaned_xml_lines, "\n") local stream_parser = XMLParser.createStreamParser() - stream_parser:addData(ctx.content) + stream_parser:addData(cleaned_xml_content) local has_tool_use = false local xml = stream_parser:getAllElements() if xml then