Skip to content
Merged
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
10 changes: 3 additions & 7 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ local curl = require('plenary.curl')
local utils = require('CopilotChat.utils')
local class = utils.class
local join = utils.join
local temp_file = utils.temp_file
local prompts = require('CopilotChat.prompts')
local tiktoken = require('CopilotChat.tiktoken')
local max_tokens = 8192
Expand Down Expand Up @@ -370,12 +371,10 @@ function Copilot:ask(prompt, opts)

self.current_job_on_cancel = on_done

local temp_file = vim.fn.tempname()
vim.fn.writefile({ body }, temp_file)
self.current_job = curl
.post(url, {
headers = headers,
body = temp_file,
body = temp_file(body),
proxy = self.proxy,
insecure = self.allow_insecure,
on_error = function(err)
Expand Down Expand Up @@ -487,12 +486,9 @@ function Copilot:embed(inputs, opts)
local body = vim.json.encode(generate_embedding_request(chunk, model))

table.insert(jobs, function(resolve)
local temp_file = vim.fn.tempname()
vim.fn.writefile({ body }, temp_file)

curl.post(url, {
headers = headers,
body = temp_file,
body = temp_file(body),
proxy = self.proxy,
insecure = self.allow_insecure,
on_error = function(err)
Expand Down
14 changes: 14 additions & 0 deletions lua/CopilotChat/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,18 @@ function M.show_virt_line(text, line, bufnr, mark_ns)
})
end

--- Writes text to a temporary file and returns path
---@param text string The text to write
---@return string?
function M.temp_file(text)
local temp_file = os.tmpname()
local f = io.open(temp_file, 'w+')
if f == nil then
error('Could not open file: ' .. temp_file)
end
f:write(text)
f:close()
return temp_file
end

return M