Skip to content

Commit cb76c39

Browse files
committed
better check no API response
1 parent df53728 commit cb76c39

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

lua/chatgpt/api.lua

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +146,37 @@ Api.handle_response = vim.schedule_wrap(function(response, exit_code, cb)
146146
end
147147

148148
local result = table.concat(response:result(), "\n")
149-
local json = vim.fn.json_decode(result)
150-
if json == nil then
149+
150+
if result == "" then
151151
cb("No Response.")
152-
elseif json.error then
153-
cb("// API ERROR: " .. json.error.message)
154152
else
155-
local message = json.choices[1].message
156-
if message ~= nil then
157-
local message_response
158-
local first_message = json.choices[1].message
159-
if first_message.function_call then
160-
message_response = vim.fn.json_decode(first_message.function_call.arguments)
161-
else
162-
message_response = first_message.content
163-
end
164-
if (type(message_response) == "string" and message_response ~= "") or type(message_response) == "table" then
165-
cb(message_response, json.usage)
166-
else
167-
cb("...")
168-
end
153+
local json = vim.fn.json_decode(result)
154+
if json == nil then
155+
cb("No Response.")
156+
elseif json.error then
157+
cb("// API ERROR: " .. json.error.message)
169158
else
170-
local response_text = json.choices[1].text
171-
if type(response_text) == "string" and response_text ~= "" then
172-
cb(response_text, json.usage)
159+
local message = json.choices[1].message
160+
if message ~= nil then
161+
local message_response
162+
local first_message = json.choices[1].message
163+
if first_message.function_call then
164+
message_response = vim.fn.json_decode(first_message.function_call.arguments)
165+
else
166+
message_response = first_message.content
167+
end
168+
if (type(message_response) == "string" and message_response ~= "") or type(message_response) == "table" then
169+
cb(message_response, json.usage)
170+
else
171+
cb("...")
172+
end
173173
else
174-
cb("...")
174+
local response_text = json.choices[1].text
175+
if type(response_text) == "string" and response_text ~= "" then
176+
cb(response_text, json.usage)
177+
else
178+
cb("...")
179+
end
175180
end
176181
end
177182
end

0 commit comments

Comments
 (0)