2
0
Fork 0
This commit is contained in:
Luca Bilke 2024-05-04 21:12:31 +02:00
parent 7c03ae048b
commit 0eea40e03e
No known key found for this signature in database
GPG Key ID: AD6630D0A1E650AC
3 changed files with 44 additions and 45 deletions

View File

@ -9,7 +9,7 @@ local M = {}
---@return table<string, string>? headers # The headers ---@return table<string, string>? headers # The headers
function M.make_headers() function M.make_headers()
if not opts.token then if not opts.token then
vim.notify("anthropic.providers.anthropic no API key provided in opts", vim.log.levels.ERROR) vim.notify("anthropic: no API key provided in opts", vim.log.levels.ERROR)
return return
end end

View File

@ -0,0 +1,14 @@
local M = {}
function M.load_session()
-- Pick session from list of saved sessions
-- Redraw chat window with session
end
function M.chat_message()
-- Append user message to session
-- Call API with session
-- Stream response to chat window and session
end
return M

View File

@ -1,6 +1,8 @@
local session_dir = vim.fn.stdpath("data") .. "anthropic"
local M = {} local M = {}
local session_dir = vim.fn.stdpath("data") M.session = {}
---@param name string ---@param name string
---@return string ---@return string
@ -8,60 +10,43 @@ local function filename(name)
return session_dir .. "/" .. name .. ".json" return session_dir .. "/" .. name .. ".json"
end end
--- # Save a session --- # Save current session
--- function M.save()
--- Encodes a session in json and writes it to disk vim.schedule(function()
---@param session session if vim.fn.isdirectory(session_dir) == 0 then
function M.save(session) vim.fn.mkdir(session_dir, 'p')
local path = filename(session.name) end
local json = vim.fn.json_encode(session)
local path = filename(M.session.name)
local json = vim.fn.json_encode(M.session)
vim.fn.writefile({ json }, path) vim.fn.writefile({ json }, path)
end)
end end
--- # Validate a session's messages --- # Load session from file
---
--- Tests the order of message roles
---@param messages message[]
---@return boolean
function M.validate_messages(messages)
local want_user = true
for _, message in ipairs(messages) do
if message.role == "assistant" then
if not want_user then
want_user = true
else
return false
end
elseif message.role == "user" then
if want_user then
want_user = false
else
return false
end
else
return false
end
end
return true
end
--- # Load a session
---
--- Decodes a session from a jsonfile
---@param name string ---@param name string
---@return session? # nil if the session is invalid
function M.load(name) function M.load(name)
local path = filename(name) local path = filename(name)
local json = table.concat(vim.fn.readfile(path), "\n") local json = table.concat(vim.fn.readfile(path), "\n")
if not json then
vim.notify("anthropic: failed loading session from file: " .. path, vim.log.levels.ERROR)
return
end
local session = vim.fn.json_decode(json) local session = vim.fn.json_decode(json)
if M.validate_messages(session) then if not session then
return session vim.notify("anthropic: failed decoding session from file: " .. path, vim.log.levels.ERROR)
return
end
if session then
M.session = session
end end
end end
--- # List saved sessions --- # List saved session names
---
--- Lists filenames of saved sessions without the .json extension
---@return string[] ---@return string[]
function M.list() function M.list()
local sessions = vim.fn.glob(session_dir .. "/*.json", false, true) local sessions = vim.fn.glob(session_dir .. "/*.json", false, true)