From cb76c392cfc78bf65b5ca57c0bb833c0ac0cf1cb Mon Sep 17 00:00:00 2001 From: ilan schemoul Date: Tue, 19 Mar 2024 23:32:02 +0100 Subject: [PATCH] better check no API response --- lua/chatgpt/api.lua | 49 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/lua/chatgpt/api.lua b/lua/chatgpt/api.lua index 76f78a54..0e36db91 100644 --- a/lua/chatgpt/api.lua +++ b/lua/chatgpt/api.lua @@ -146,32 +146,37 @@ Api.handle_response = vim.schedule_wrap(function(response, exit_code, cb) end local result = table.concat(response:result(), "\n") - local json = vim.fn.json_decode(result) - if json == nil then + + if result == "" then cb("No Response.") - elseif json.error then - cb("// API ERROR: " .. json.error.message) else - local message = json.choices[1].message - if message ~= nil then - local message_response - local first_message = json.choices[1].message - if first_message.function_call then - message_response = vim.fn.json_decode(first_message.function_call.arguments) - else - message_response = first_message.content - end - if (type(message_response) == "string" and message_response ~= "") or type(message_response) == "table" then - cb(message_response, json.usage) - else - cb("...") - end + local json = vim.fn.json_decode(result) + if json == nil then + cb("No Response.") + elseif json.error then + cb("// API ERROR: " .. json.error.message) else - local response_text = json.choices[1].text - if type(response_text) == "string" and response_text ~= "" then - cb(response_text, json.usage) + local message = json.choices[1].message + if message ~= nil then + local message_response + local first_message = json.choices[1].message + if first_message.function_call then + message_response = vim.fn.json_decode(first_message.function_call.arguments) + else + message_response = first_message.content + end + if (type(message_response) == "string" and message_response ~= "") or type(message_response) == "table" then + cb(message_response, json.usage) + else + cb("...") + end else - cb("...") + local response_text = json.choices[1].text + if type(response_text) == "string" and response_text ~= "" then + cb(response_text, json.usage) + else + cb("...") + end end end end