122 lines
3.2 KiB
Lua
122 lines
3.2 KiB
Lua
local VERSION = "2023-06-01"
|
|
-- local USERAGENT = "anthropic.nvim/0.0.0"
|
|
|
|
local opts = require("anthropic").opts
|
|
|
|
local M = {}
|
|
|
|
--- # Make headers for an anthropic API call
|
|
---
|
|
--- This will generate the base anthropic API headers
|
|
---@return table<string, string>? headers # The headers
|
|
function M.make_headers()
|
|
if not opts.token then
|
|
vim.notify("anthropic: no API key provided in opts", vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
local headers = {
|
|
-- ["user-agent"] = USERAGENT,
|
|
["content-type"] = "application/json",
|
|
["anthropic-version"] = VERSION,
|
|
["x-api-key"] = opts.token,
|
|
}
|
|
|
|
return headers
|
|
end
|
|
|
|
--- # Make message request for anthropic
|
|
---
|
|
--- This will generate the json body for an anthropic message call
|
|
---@param session session
|
|
---@param model string?
|
|
---@param max_tokens integer?
|
|
---@param system string?
|
|
---@param temperature float?
|
|
---@param top_k integer?
|
|
---@param top_p integer?
|
|
---@return table<string, any>?
|
|
function M.make_body(session, model, max_tokens, system, temperature, top_k, top_p)
|
|
model = model or "claude-3-sonnet-20240229"
|
|
max_tokens = max_tokens or 1024
|
|
|
|
return {
|
|
messages = session,
|
|
model = model,
|
|
max_tokens = max_tokens,
|
|
system = system,
|
|
temperature = temperature,
|
|
top_k = top_k,
|
|
top_p = top_p,
|
|
}
|
|
end
|
|
|
|
--- # Curl exit code test
|
|
---
|
|
--- Tests the curl exit code and notifies the user if something went wrong.
|
|
---@param exit_code integer
|
|
---@return boolean
|
|
function M.test_code(exit_code)
|
|
if exit_code == 127 then
|
|
vim.notify("anthropic: couldn't find curl binary in path", vim.log.levels.ERROR)
|
|
return false
|
|
elseif exit_code ~= 0 then
|
|
vim.notify("anthropic: unkown curl exit code: " .. exit_code, vim.log.levels.ERROR)
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
--- # Curl callback
|
|
---
|
|
--- This function wraps the callback passed to transfarmer.util.call to perform
|
|
--- error checking and json decoding
|
|
---@param response curl_response
|
|
---@param callback curl_callback
|
|
local function callback_wrapper(response, callback)
|
|
if response.status ~= 200 then
|
|
response.body = response.body:gsub("%s+", " ")
|
|
print("Error: " .. response.status .. " " .. response.body)
|
|
return
|
|
end
|
|
|
|
if response.body == nil or response.body == "" then
|
|
vim.notify("anthropic: empty response body", vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
vim.schedule_wrap(function(body)
|
|
local ok, result = pcall(vim.fn.json_decode, body)
|
|
if ok then
|
|
callback(result)
|
|
else
|
|
vim.notify("anthropic: failed to decode json: " .. result, vim.log.levels.ERROR)
|
|
end
|
|
end)(response.body)
|
|
end
|
|
|
|
--- # Call a anthropic's API
|
|
---
|
|
--- This function calls the API with the given payload and passes the decoded
|
|
--- json response to the callback function.
|
|
---@param payload table<string, any>
|
|
---@param callback fun(decoded: table<string, any>): nil
|
|
function M.call(payload, callback)
|
|
local payload_str = vim.fn.json_encode(payload)
|
|
local url = opts.api_url
|
|
local headers = M.make_headers()
|
|
|
|
require("plenary.curl").post(url, {
|
|
body = payload_str,
|
|
headers = headers,
|
|
callback = function(response)
|
|
callback_wrapper(response, callback)
|
|
end,
|
|
on_error = function(err)
|
|
vim.notify("anthropic: curl error from: " .. err.message, vim.log.levels.WARNING)
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|