fix(suggestions): cleanup response to ensure JSON (#900)

LLM is not always smart enough to follow the prompt
This commit is contained in:
Radosław Woźniak 2024-11-25 13:26:59 +01:00 committed by GitHub
parent 7413d5a508
commit 97f04f87f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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" })