diff --git a/lua/anthropic/init.lua b/lua/anthropic/init.lua index d5871d7..e2dd1c9 100644 --- a/lua/anthropic/init.lua +++ b/lua/anthropic/init.lua @@ -18,7 +18,16 @@ M.opts = { nerdfonts = false, } -M.resources = {} +M.resources = { + icons = { + user = "User:", + assistant = "Assistant:", + }, + spinner = { + fps = 12, + parts = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }, + }, +} function M.setup(opts) M.opts = vim.tbl_deep_extend("force", M.opts, opts) @@ -38,17 +47,6 @@ function M.setup(opts) }, }, } - else - M.resources = { - icons = { - user = "User:", - assistant = "Assistant:", - }, - spinner = { - fps = 12, - parts = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }, - }, - } end end diff --git a/lua/anthropic/util/api.lua b/lua/anthropic/util/api.lua index 8f59cc4..fddbe1e 100644 --- a/lua/anthropic/util/api.lua +++ b/lua/anthropic/util/api.lua @@ -1,4 +1,6 @@ local VERSION = "2023-06-01" +-- local USERAGENT = "anthropic.nvim/0.0.0" + local opts = require("anthropic").opts local M = {} @@ -13,7 +15,12 @@ function M.make_headers() return end - local headers = { ["content-type"] = "application/json", ["anthropic-version"] = VERSION, ["x-api-key"] = opts.token } + local headers = { + -- ["user-agent"] = USERAGENT, + ["content-type"] = "application/json", + ["anthropic-version"] = VERSION, + ["x-api-key"] = opts.token, + } return headers end diff --git a/lua/anthropic/util/init.lua b/lua/anthropic/util/init.lua index 9e46a21..498421d 100644 --- a/lua/anthropic/util/init.lua +++ b/lua/anthropic/util/init.lua @@ -1,3 +1,5 @@ +local session = require("anthropic.util.session") + local M = {} function M.load_session() @@ -5,10 +7,15 @@ function M.load_session() -- Redraw chat window with session end -function M.chat_message() - -- Append user message to session +--- # Message request wrapper function +--- +--- Calls the Anthropic API and writes the response to the chatbox +---@param message string +function M.send_message(message) + session.add_message(message, "user") -- Call API with session - -- Stream response to chat window and session + -- Stream response to chat window and + -- Update session end return M diff --git a/lua/anthropic/util/session.lua b/lua/anthropic/util/session.lua index 3497edd..e0769cc 100644 --- a/lua/anthropic/util/session.lua +++ b/lua/anthropic/util/session.lua @@ -25,6 +25,7 @@ function M.save() end --- # Load session from file +--- ---@param name string function M.load(name) local path = filename(name) @@ -47,6 +48,7 @@ function M.load(name) end --- # List saved session names +--- ---@return string[] function M.list() local sessions = vim.fn.glob(session_dir .. "/*.json", false, true) @@ -56,4 +58,18 @@ function M.list() return sessions end +--- # Add a message to the session +--- +---@param message string +---@param role "user" | "assistant" +function M.add_message(message, role) + local msgs = M.session.messages + if msgs[#msgs - 1].role == role then + vim.notify("anthropic: " .. role .. " message added after a " .. role .. " message", vim.log.levels.ERROR) + return + end + + table.insert(msgs, { role = role, content = message }) +end + return M