From 7912070c6f545e45ad55ef08ea84212004b087db Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Sun, 1 Sep 2024 18:47:35 -0400 Subject: [PATCH] fix(gemini): check if json can be decoded (#446) Signed-off-by: Aaron Pham --- lua/avante/config.lua | 1 + lua/avante/providers/gemini.lua | 13 ++++++++++--- lua/avante/sidebar.lua | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index d8fd12d..37db5b6 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -140,6 +140,7 @@ M.defaults = { wrap = true, -- similar to vim.o.wrap width = 30, -- default % based on available width in vertical layout height = 30, -- default % based on available height in horizontal layout + direction = "right", -- "left" | "right" sidebar_header = { align = "center", -- left, center, right for title rounded = true, diff --git a/lua/avante/providers/gemini.lua b/lua/avante/providers/gemini.lua index 88c0c7e..485e0c9 100644 --- a/lua/avante/providers/gemini.lua +++ b/lua/avante/providers/gemini.lua @@ -50,9 +50,16 @@ M.parse_message = function(opts) end M.parse_response = function(data_stream, _, opts) - local json = vim.json.decode(data_stream) - if json.candidates and #json.candidates > 0 then - opts.on_chunk(json.candidates[1].content.parts[1].text) + local ok, json = pcall(vim.json.decode, data_stream) + if not ok then + opts.on_complete(json) + end + if json.candidates then + if #json.candidates > 0 then + opts.on_chunk(json.candidates[1].content.parts[1].text) + elseif json.candidates.finishReason and json.candidates.finishReason == "STOP" then + opts.on_complete(nil) + end end end diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index f1fd147..016fc90 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1383,7 +1383,7 @@ function Sidebar:create_input() if self:get_layout() == "vertical" then return "bottom" end - return "right" + return Config.windows.direction end local get_size = function()