WIP
This commit is contained in:
parent
7c03ae048b
commit
0eea40e03e
|
@ -9,7 +9,7 @@ local M = {}
|
|||
---@return table<string, string>? 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
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue