diff --git a/lua/anthropic/util/api.lua b/lua/anthropic/util/api.lua index c2cff2d..8f59cc4 100644 --- a/lua/anthropic/util/api.lua +++ b/lua/anthropic/util/api.lua @@ -9,7 +9,7 @@ local M = {} ---@return table? headers # The headers function M.make_headers() 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 end diff --git a/lua/anthropic/util/init.lua b/lua/anthropic/util/init.lua new file mode 100644 index 0000000..9e46a21 --- /dev/null +++ b/lua/anthropic/util/init.lua @@ -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 diff --git a/lua/anthropic/util/session.lua b/lua/anthropic/util/session.lua index b5a5281..3497edd 100644 --- a/lua/anthropic/util/session.lua +++ b/lua/anthropic/util/session.lua @@ -1,6 +1,8 @@ +local session_dir = vim.fn.stdpath("data") .. "anthropic" + local M = {} -local session_dir = vim.fn.stdpath("data") +M.session = {} ---@param name string ---@return string @@ -8,60 +10,43 @@ local function filename(name) return session_dir .. "/" .. name .. ".json" end ---- # Save a session ---- ---- Encodes a session in json and writes it to disk ----@param session session -function M.save(session) - local path = filename(session.name) - local json = vim.fn.json_encode(session) - vim.fn.writefile({ json }, path) -end - ---- # Validate a session's messages ---- ---- 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 +--- # Save current session +function M.save() + vim.schedule(function() + if vim.fn.isdirectory(session_dir) == 0 then + vim.fn.mkdir(session_dir, 'p') end - end - return true + + local path = filename(M.session.name) + + local json = vim.fn.json_encode(M.session) + vim.fn.writefile({ json }, path) + end) end ---- # Load a session ---- ---- Decodes a session from a jsonfile +--- # Load session from file ---@param name string ----@return session? # nil if the session is invalid function M.load(name) local path = filename(name) + 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) - if M.validate_messages(session) then - return session + if not session then + vim.notify("anthropic: failed decoding session from file: " .. path, vim.log.levels.ERROR) + return + end + + if session then + M.session = session end end ---- # List saved sessions ---- ---- Lists filenames of saved sessions without the .json extension +--- # List saved session names ---@return string[] function M.list() local sessions = vim.fn.glob(session_dir .. "/*.json", false, true)