feat(AvanteClear): accept args to clear history, memory or cache (#930)

* chore: extract sidebar clear history/memory functions

* feat(AvanteClear): accept args to clear history, memory or cache

Usage:
```
:AvanteClear [history|memory|cache]
```

- changed default behavior from clearing the cache to clearing history.
- added args to clear history, memory or cache.
- added confirm dialog before clearing cache.
This commit is contained in:
Maddison Hellstrom 2024-12-13 06:59:09 -08:00 committed by GitHub
parent 5c20cc1779
commit b102f673c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 34 deletions

View File

@ -1356,6 +1356,41 @@ function Sidebar:get_content_between_separators()
return content, start_line
end
function Sidebar:clear_history(args, cb)
local chat_history = Path.history.load(self.code.bufnr)
if next(chat_history) ~= nil then
chat_history = {}
Path.history.save(self.code.bufnr, chat_history)
self:update_content("Chat history cleared", { focus = false, scroll = false })
if cb then cb(args) end
else
self:update_content("Chat history is already empty", { focus = false, scroll = false })
end
end
function Sidebar:reset_memory(args, cb)
local chat_history = Path.history.load(self.code.bufnr)
if next(chat_history) ~= nil then
table.insert(chat_history, {
timestamp = get_timestamp(),
provider = Config.provider,
model = Config.get_provider(Config.provider).model,
request = "",
response = "",
original_response = "",
selected_file = nil,
selected_code = nil,
reset_memory = true,
})
Path.history.save(self.code.bufnr, chat_history)
local history_content = self:render_history_content(chat_history)
self:update_content(history_content, { focus = false, scroll = true })
if cb then cb(args) end
else
self:update_content("Chat history is already empty", { focus = false, scroll = false })
end
end
---@alias AvanteSlashCommandType "clear" | "help" | "lines" | "reset"
---@alias AvanteSlashCommandCallback fun(args: string, cb?: fun(args: string): nil): nil
---@alias AvanteSlashCommand {description: string, command: AvanteSlashCommandType, details: string, shorthelp?: string, callback?: AvanteSlashCommandCallback}
@ -1390,39 +1425,8 @@ function Sidebar:get_commands()
self:update_content(help_text, { focus = false, scroll = false })
if cb then cb(args) end
end,
clear = function(args, cb)
local chat_history = Path.history.load(self.code.bufnr)
if next(chat_history) ~= nil then
chat_history = {}
Path.history.save(self.code.bufnr, chat_history)
self:update_content("Chat history cleared", { focus = false, scroll = false })
if cb then cb(args) end
else
self:update_content("Chat history is already empty", { focus = false, scroll = false })
end
end,
reset = function(args, cb)
local chat_history = Path.history.load(self.code.bufnr)
if next(chat_history) ~= nil then
table.insert(chat_history, {
timestamp = get_timestamp(),
provider = Config.provider,
model = Config.get_provider(Config.provider).model,
request = "",
response = "",
original_response = "",
selected_file = nil,
selected_code = nil,
reset_memory = true,
})
Path.history.save(self.code.bufnr, chat_history)
local history_content = self:render_history_content(chat_history)
self:update_content(history_content, { focus = false, scroll = true })
if cb then cb(args) end
else
self:update_content("Chat history is already empty", { focus = false, scroll = false })
end
end,
clear = function(args, cb) self:clear_history(args, cb) end,
reset = function(args, cb) self:reset_memory(args, cb) end,
lines = function(args, cb)
if cb then cb(args) end
end,

View File

@ -15,6 +15,7 @@ vim.g.avante = 1
--- NOTE: We will override vim.paste if img-clip.nvim is available to work with avante.nvim internal logic paste
local Clipboard = require("avante.clipboard")
local Config = require("avante.config")
local Utils = require("avante.utils")
local api = vim.api
if Config.support_paste_image() then
@ -115,5 +116,33 @@ cmd("SwitchProvider", function(opts) require("avante.api").switch_provider(vim.t
return vim.tbl_filter(function(key) return key:find(prefix, 1, true) == 1 end, Config.providers)
end,
})
cmd("Clear", function() require("avante.path").clear() end, { desc = "avante: clear all chat history" })
cmd("Clear", function(opts)
local arg = vim.trim(opts.args or "")
arg = arg == "" and "history" or arg
if arg == "history" or arg == "memory" then
local sidebar = require("avante").get()
if not sidebar then
Utils.error("No sidebar found")
return
end
if arg == "history" then
sidebar:clear_history()
else
sidebar:reset_memory()
end
elseif arg == "cache" then
local P = require("avante.path")
local history_path = P.history_path:absolute()
local cache_path = P.cache_path:absolute()
local prompt = string.format("Recursively delete %s and %s?", history_path, cache_path)
if vim.fn.confirm(prompt, "&Yes\n&No", 2) == 1 then require("avante.path").clear() end
else
Utils.error("Invalid argument. Valid arguments: 'history', 'memory', 'cache'")
return
end
end, {
desc = "avante: clear history, memory or cache",
nargs = "?",
complete = function(_, _, _) return { "history", "memory", "cache" } end,
})
cmd("ShowRepoMap", function() require("avante.repo_map").show() end, { desc = "avante: show repo map" })