From 78be877a3ec69ada3b3826bc57336a5b4bdc7a05 Mon Sep 17 00:00:00 2001 From: fish Date: Fri, 16 Aug 2024 13:40:41 +0800 Subject: [PATCH] fix: invalid response err (#19) --- lua/avante/ai_bot.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lua/avante/ai_bot.lua b/lua/avante/ai_bot.lua index 921ab12..f0a4b3f 100644 --- a/lua/avante/ai_bot.lua +++ b/lua/avante/ai_bot.lua @@ -237,11 +237,13 @@ local function call_openai_api_stream(question, code_lang, code_content, on_chun error("Error: failed to parse json: " .. parsed) return end - if parsed and parsed.choices and parsed.choices[1].delta.content then - on_chunk(parsed.choices[1].delta.content) - elseif parsed and parsed.choices and parsed.choices[1].finish_reason == "stop" then - -- Stream request completed - on_complete(nil) + if parsed and parsed.choices and parsed.choices[1] then + local choice = parsed.choices[1] + if choice.finish_reason == "stop" then + on_complete(nil) + elseif choice.delta and choice.delta.content then + on_chunk(choice.delta.content) + end end end) end