Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ keybindings are available:
- `<C-f>` [Chat] Cycle over modes (center, stick to right).
- `<C-c>` [Both] to close chat window.
- `<C-p>` [Chat] Toggle sessions list.
- `<cr>` &nbsp;[Chat] Select a session.
- `<C-u>` [Chat] scroll up chat window.
- `<C-d>` [Chat] scroll down chat window.
- `<C-k>` [Chat] to copy/yank code from last answer.
Expand Down
49 changes: 27 additions & 22 deletions lua/chatgpt/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/chatgpt/flows/actions/chat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function ChatAction:render_template()
data = vim.tbl_extend("force", {}, data, self.variables)
local result = self.template
for key, value in pairs(data) do
value = value:gsub("%%", "%%%%")
result = result:gsub("{{" .. key .. "}}", value)
end
return result
Expand Down