diff --git a/README.md b/README.md index 5371a6bc..ad2e2b0d 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,7 @@ keybindings are available: - `` [Chat] Cycle over modes (center, stick to right). - `` [Both] to close chat window. - `` [Chat] Toggle sessions list. +- ``  [Chat] Select a session. - `` [Chat] scroll up chat window. - `` [Chat] scroll down chat window. - `` [Chat] to copy/yank code from last answer. 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 diff --git a/lua/chatgpt/flows/actions/chat/init.lua b/lua/chatgpt/flows/actions/chat/init.lua index b9c61b68..b500655d 100644 --- a/lua/chatgpt/flows/actions/chat/init.lua +++ b/lua/chatgpt/flows/actions/chat/init.lua @@ -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