Skip to content

Commit

Permalink
feat: write curl body to a temporary JSON file (yetone#673)
Browse files Browse the repository at this point in the history
* Write curl body to a temporary JSON file.

* Remove temp file after use

* feat: use vim.fn.delete to delete files

---------

Co-authored-by: yetone <yetoneful@gmail.com>
  • Loading branch information
Huite and yetone authored Oct 7, 2024
1 parent c3848d0 commit 2a72dfa
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lua/avante/llm.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local api = vim.api
local fn = vim.fn

local curl = require("plenary.curl")

Expand Down Expand Up @@ -101,8 +102,6 @@ M.stream = function(opts)
---@type AvanteCurlOutput
local spec = Provider.parse_curl_args(Provider, code_opts)

Utils.debug("curl spec:", spec)

---@param line string
local function parse_stream_data(line)
local event = line:match("^event: (.+)$")
Expand All @@ -122,11 +121,22 @@ M.stream = function(opts)

local active_job

local curl_body_file = fn.tempname() .. ".json"
local json_content = vim.json.encode(spec.body)
fn.writefile(vim.split(json_content, "\n"), curl_body_file)

Utils.debug("curl body file:", curl_body_file)

local function cleanup()
if Config.debug then return end
vim.schedule(function() fn.delete(curl_body_file) end)
end

active_job = curl.post(spec.url, {
headers = spec.headers,
proxy = spec.proxy,
insecure = spec.insecure,
body = vim.json.encode(spec.body),
body = curl_body_file,
stream = function(err, data, _)
if err then
completed = true
Expand Down Expand Up @@ -155,10 +165,12 @@ M.stream = function(opts)
on_error = function()
active_job = nil
completed = true
cleanup()
opts.on_complete(nil)
end,
callback = function(result)
active_job = nil
cleanup()
if result.status >= 400 then
if Provider.on_error then
Provider.on_error(result)
Expand Down

0 comments on commit 2a72dfa

Please sign in to comment.