From 2d6527197db5891f8e872e589f8df33987921eb6 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 6 May 2025 20:57:58 +0800 Subject: [PATCH] feat: manual compact instead of automatic compact (#1996) --- lua/avante/highlights.lua | 1 + lua/avante/sidebar.lua | 32 +++++++++++++++++++++++++++++++- lua/avante/types.lua | 2 +- lua/avante/utils/init.lua | 2 ++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lua/avante/highlights.lua b/lua/avante/highlights.lua index 99e382f..5cfe4d8 100644 --- a/lua/avante/highlights.lua +++ b/lua/avante/highlights.lua @@ -46,6 +46,7 @@ local Highlights = { AVANTE_STATE_SPINNER_SUCCEEDED = { name = "AvanteStateSpinnerSucceeded", fg = "#1e222a", bg = "#98c379" }, AVANTE_STATE_SPINNER_SEARCHING = { name = "AvanteStateSpinnerSearching", fg = "#1e222a", bg = "#c678dd" }, AVANTE_STATE_SPINNER_THINKING = { name = "AvanteStateSpinnerThinking", fg = "#1e222a", bg = "#c678dd" }, + AVANTE_STATE_SPINNER_COMPACTING = { name = "AvanteStateSpinnerCompacting", fg = "#1e222a", bg = "#c678dd" }, } Highlights.conflict = { diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 774b43b..4b7cd54 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1849,7 +1849,15 @@ function Sidebar:render_state() if self.current_state == "succeeded" then hl = "AvanteStateSpinnerSucceeded" end if self.current_state == "searching" then hl = "AvanteStateSpinnerSearching" end if self.current_state == "thinking" then hl = "AvanteStateSpinnerThinking" end - if self.current_state ~= "generating" and self.current_state ~= "tool calling" then spinner_char = "" end + if self.current_state == "compacting" then hl = "AvanteStateSpinnerCompacting" end + if + self.current_state ~= "generating" + and self.current_state ~= "tool calling" + and self.current_state ~= "thinking" + and self.current_state ~= "compacting" + then + spinner_char = "" + end local virt_line if spinner_char == "" then virt_line = " " .. self.current_state .. " " @@ -1872,6 +1880,27 @@ function Sidebar:render_state() self.state_timer = vim.defer_fn(function() self:render_state() end, 160) end +function Sidebar:compact_history_messages(args, cb) + local history_memory = self.chat_history.memory + local messages = Utils.get_history_messages(self.chat_history) + self.current_state = "compacting" + self:render_state() + self:update_content( + "compacting history messsages", + { focus = false, scroll = true, callback = function() self:focus_input() end } + ) + Llm.summarize_memory(history_memory and history_memory.content, messages, function(memory) + if memory then + self.chat_history.memory = memory + Path.history.save(self.code.bufnr, self.chat_history) + end + self:update_content("compacted!", { focus = false, scroll = true, callback = function() self:focus_input() end }) + self.current_state = "compacted" + self:clear_state() + if cb then cb(args) end + end) +end + function Sidebar:new_chat(args, cb) local history = Path.history.new(self.code.bufnr) Path.history.save(self.code.bufnr, history) @@ -2204,6 +2233,7 @@ function Sidebar:get_generate_prompts_options(request, cb) history_messages = history_messages, code_lang = filetype, selected_code = selected_code, + disable_compact_history_messages = true, -- instructions = request, tools = tools, } diff --git a/lua/avante/types.lua b/lua/avante/types.lua index d3862aa..719ceb5 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -359,7 +359,7 @@ vim.g.avante_login = vim.g.avante_login ---@alias AvanteLLMMemorySummarizeCallback fun(pending_compaction_history_messages: avante.HistoryMessage[]): nil --- ---@alias AvanteLLMToolUseState "generating" | "generated" | "running" | "succeeded" | "failed" ----@alias avante.GenerateState "generating" | "tool calling" | "failed" | "succeeded" | "cancelled" | "searching" | "thinking" +---@alias avante.GenerateState "generating" | "tool calling" | "failed" | "succeeded" | "cancelled" | "searching" | "thinking" | "compacting" | "compacted" --- ---@class AvanteLLMStreamOptions: AvanteGeneratePromptsOptions ---@field on_start AvanteLLMStartCallback diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index c3c761b..60abfc6 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -1312,6 +1312,7 @@ function M.get_commands() { description = "Show help message", name = "help" }, { description = "Clear chat history", name = "clear" }, { description = "New chat", name = "new" }, + { description = "Compact history messages to save tokens", name = "compact" }, { shorthelp = "Ask a question about specific lines", description = "/lines - ", @@ -1329,6 +1330,7 @@ function M.get_commands() end, clear = function(sidebar, args, cb) sidebar:clear_history(args, cb) end, new = function(sidebar, args, cb) sidebar:new_chat(args, cb) end, + compact = function(sidebar, args, cb) sidebar:compact_history_messages(args, cb) end, lines = function(_, args, cb) if cb then cb(args) end end,