From f1cc73df990993b9dfb834d8748d5eaca8c3077e Mon Sep 17 00:00:00 2001 From: James Hiew Date: Wed, 29 Sep 2021 13:25:13 +0100 Subject: [PATCH 1/2] refactor: debug log most curl arguments Also raise a better error if parsing response body fails --- lua/nvim-magic-openai/_completion.lua | 7 +++++-- lua/nvim-magic-openai/_curl.lua | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lua/nvim-magic-openai/_completion.lua b/lua/nvim-magic-openai/_completion.lua index 9f0e087..229feca 100644 --- a/lua/nvim-magic-openai/_completion.lua +++ b/lua/nvim-magic-openai/_completion.lua @@ -20,8 +20,11 @@ function completion.new_request(prompt, max_tokens, stops) end function completion.extract_from(res_body) - local decoded = vim.fn.json_decode(res_body) - assert(decoded ~= nil, "couldn't decode body") + local ok, decoded = pcall(vim.fn.json_decode, res_body) + if not ok then + local errmsg = decoded + error(string.format("couldn't decode response body errmsg=%s body=%s", errmsg, res_body)) + end assert(decoded.choices ~= nil, 'no choices returned') return decoded.choices[1].text end diff --git a/lua/nvim-magic-openai/_curl.lua b/lua/nvim-magic-openai/_curl.lua index bc91b9d..62e26b4 100644 --- a/lua/nvim-magic-openai/_curl.lua +++ b/lua/nvim-magic-openai/_curl.lua @@ -62,6 +62,8 @@ local F = require('plenary.functional') local J = require('plenary.job') local P = require('plenary.path') +local log = require('nvim-magic-openai._log') + local DEFAULT_TIMEOUT = 10000 -- milliseconds local function is_windows() @@ -230,7 +232,8 @@ parse.request = function(opts) opts.raw_body = b end end - return vim.tbl_flatten({ + + local preargs = vim.tbl_flatten({ '-sSL', opts.dump, opts.compressed and '--compressed' or nil, @@ -241,12 +244,25 @@ parse.request = function(opts) parse.data_body(opts.data), parse.form(opts.form), parse.file(opts.in_file), - parse.auth(opts.auth), + }) + local postargs = vim.tbl_flatten({ opts.raw, opts.output and { '-o', opts.output } or nil, parse.url(opts.url, opts.query), - }), - opts + }) + local nonauth_args = vim.deepcopy(preargs) + vim.list_extend(nonauth_args, postargs) + log.fmt_debug('Parsed request options nonauth_curl_args=%s', nonauth_args) + + local args = preargs + vim.list_extend( + args, + vim.tbl_flatten({ + parse.auth(opts.auth), + }) + ) + vim.list_extend(args, postargs) + return preargs, opts end -- Parse response ------------------------------------------ From 730cae5b183d27ab5bd3ff3a7345668f427ce65f Mon Sep 17 00:00:00 2001 From: James Hiew Date: Wed, 29 Sep 2021 14:24:13 +0100 Subject: [PATCH 2/2] chore: release 0.3.1 --- CHANGELOG.md | 6 +++++- lua/nvim-magic-openai/init.lua | 2 +- lua/nvim-magic/init.lua | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7334b05..a0144cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## [0.3.1] - 2021-09-29 + +### Changed +* More logging for `curl` calls when debug logging is enabled ### Fixed * Selections made in visual lines mode ("V") should now work (thanks @naefl for debugging) @@ -55,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.0] - 2021-09-13 +[0.3.1]: https://github.com/jameshiew/nvim-magic/compare/0.3.0...0.3.1 [0.3.0]: https://github.com/jameshiew/nvim-magic/compare/0.2.3...0.3.0 [0.2.3]: https://github.com/jameshiew/nvim-magic/compare/v0.2.2...0.2.3 [0.2.2]: https://github.com/jameshiew/nvim-magic/compare/v0.2.1...v0.2.2 diff --git a/lua/nvim-magic-openai/init.lua b/lua/nvim-magic-openai/init.lua index e34abde..535b8ac 100644 --- a/lua/nvim-magic-openai/init.lua +++ b/lua/nvim-magic-openai/init.lua @@ -24,7 +24,7 @@ local function default_config() end function openai.version() - return '0.3.1-dev' + return '0.3.1' end function openai.new(override) diff --git a/lua/nvim-magic/init.lua b/lua/nvim-magic/init.lua index 1140188..4c1bb41 100644 --- a/lua/nvim-magic/init.lua +++ b/lua/nvim-magic/init.lua @@ -14,7 +14,7 @@ local function default_config() end function magic.version() - return '0.3.1-dev' + return '0.3.1' end function magic.setup(override)