From 97f04f87f57fa56680b9d8190f3a0d61038cb608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wo=C5=BAniak?= <184065+radwo@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:26:59 +0100 Subject: [PATCH] fix(suggestions): cleanup response to ensure JSON (#900) LLM is not always smart enough to follow the prompt --- lua/avante/suggestion.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/avante/suggestion.lua b/lua/avante/suggestion.lua index baeaeab..3b3d348 100644 --- a/lua/avante/suggestion.lua +++ b/lua/avante/suggestion.lua @@ -85,7 +85,10 @@ function Suggestion:suggest() vim.schedule(function() local cursor_row, cursor_col = Utils.get_cursor_pos() if cursor_row ~= doc.position.row or cursor_col ~= doc.position.col then return end + -- Clean up markdown code blocks full_response = full_response:gsub("^```%w*\n(.-)\n```$", "%1") + -- Remove everything before the first '[' to ensure we get just the JSON array + full_response = full_response:gsub("^.-(%[.*)", "%1") local ok, suggestions = pcall(vim.json.decode, full_response) if not ok then Utils.error("Error while decoding suggestions: " .. full_response, { once = true, title = "Avante" })