From b4c9c8abdfab6340a0a540ab7d5261b886b61626 Mon Sep 17 00:00:00 2001 From: yorik1984 Date: Thu, 2 May 2024 06:31:47 +0300 Subject: [PATCH 1/2] Update api.lua --- lua/chatgpt/api.lua | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lua/chatgpt/api.lua b/lua/chatgpt/api.lua index 76f78a54..0d6fe64d 100644 --- a/lua/chatgpt/api.lua +++ b/lua/chatgpt/api.lua @@ -4,6 +4,18 @@ local logger = require("chatgpt.common.logger") local Api = {} +local function tmpMsgFileName(params) + local temp_file = os.tmpname() + local f = io.open(temp_file, "w+") + if f == nil then + vim.notify("Cannot open temporary message file: " .. temp_file, vim.log.levels.ERROR) + return + end + f:write(vim.fn.json_encode(params)) + f:close() + return temp_file +end + function Api.completions(custom_params, cb) local params = vim.tbl_extend("keep", custom_params, Config.options.openai_params) Api.make_call(Api.COMPLETIONS_URL, params, cb) @@ -29,7 +41,7 @@ function Api.chat_completions(custom_params, cb, should_stop) "-H", Api.AUTHORIZATION_HEADER, "-d", - vim.json.encode(params), + "@" .. TMP_MSG_FILENAME, } if extra_curl_params ~= nil then @@ -101,15 +113,7 @@ function Api.edits(custom_params, cb) end function Api.make_call(url, params, cb) - TMP_MSG_FILENAME = os.tmpname() - local f = io.open(TMP_MSG_FILENAME, "w+") - if f == nil then - vim.notify("Cannot open temporary message file: " .. TMP_MSG_FILENAME, vim.log.levels.ERROR) - return - end - f:write(vim.fn.json_encode(params)) - f:close() - + local TMP_MSG_FILENAME = tmpMsgFileName(params) local args = { url, "-H", @@ -132,14 +136,14 @@ function Api.make_call(url, params, cb) command = "curl", args = args, on_exit = vim.schedule_wrap(function(response, exit_code) - Api.handle_response(response, exit_code, cb) + Api.handle_response(response, exit_code, cb, TMP_MSG_FILENAME) end), }) :start() end -Api.handle_response = vim.schedule_wrap(function(response, exit_code, cb) - os.remove(TMP_MSG_FILENAME) +Api.handle_response = vim.schedule_wrap(function(response, exit_code, cb, tmpFile) + os.remove(tmpFile) if exit_code ~= 0 then vim.notify("An Error Occurred ...", vim.log.levels.ERROR) cb("ERROR: API Error") From 6512fef1e995db58078c7021ea968d3be813666d Mon Sep 17 00:00:00 2001 From: yorik1984 Date: Thu, 2 May 2024 07:04:19 +0300 Subject: [PATCH 2/2] Update api.lua --- lua/chatgpt/api.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/chatgpt/api.lua b/lua/chatgpt/api.lua index 0d6fe64d..72349479 100644 --- a/lua/chatgpt/api.lua +++ b/lua/chatgpt/api.lua @@ -30,6 +30,8 @@ function Api.chat_completions(custom_params, cb, should_stop) cb = vim.schedule_wrap(cb) + local TMP_MSG_FILENAME = tmpMsgFileName(params) + local extra_curl_params = Config.options.extra_curl_params local args = { "--silent",